1 This adds Service Location Protocol support.
3 To use this patch, run these commands for a successful build:
5 patch -p1 <patches/slp.diff
7 ./configure --enable-slp
10 TODO: the configure changes should abort if the user requests --enable-slp
11 and we can't honor that request.
13 based-on: 40afd365cc8ca968fd16e161d24df5b8a8a520cc
14 diff --git a/Makefile.in b/Makefile.in
17 @@ -13,6 +13,8 @@ CFLAGS=@CFLAGS@
26 @@ -37,7 +39,7 @@ OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
27 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
28 fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
29 OBJS3=progress.o pipe.o
30 -DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
31 +DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o $(SLPOBJ)
32 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
33 popt/popthelp.o popt/poptparse.o
34 OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@
35 @@ -73,7 +75,7 @@ install-strip:
36 $(MAKE) INSTALL_STRIP='-s' install
38 rsync$(EXEEXT): $(OBJS)
39 - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
40 + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LIBSLP)
43 $(CHECK_OBJS): $(HEADERS)
44 diff --git a/clientserver.c b/clientserver.c
47 @@ -1074,6 +1074,13 @@ int daemon_main(void)
48 * address too. In fact, why not just do getnameinfo on the
52 + if (lp_use_slp() && register_services() != 0) {
54 + "Couldn't register with service discovery protocol, continuing anyway\n");
58 start_accept_loop(rsync_port, start_daemon);
61 diff --git a/configure.ac b/configure.ac
64 @@ -668,6 +668,29 @@ if test $rsync_cv_can_hardlink_special = yes; then
65 AC_DEFINE(CAN_HARDLINK_SPECIAL, 1, [Define to 1 if link() can hard-link special files.])
68 +AC_ARG_ENABLE(slp, [ --disable-slp turn off SLP support, defaults to on])
69 +AC_ARG_WITH(openslp-libs, [ --with-openslp-libs set directory for OpenSLP library],
70 + LDFLAGS="-L$withval $LDFLAGS"
71 + DSOFLAGS="-L$withval $DSOFLAGS",)
72 +AC_ARG_WITH(openslp-includes, [ --with-openslp-includes set directory for OpenSLP includes],
73 + CFLAGS="-I$withval $CFLAGS"
74 + CXXFLAGS="-I$withval $CXXFLAGS"
75 + CPPFLAGS="-I$withval $CPPFLAGS",)
80 +if test x$enable_slp != xno; then
81 + AC_CHECK_HEADER(slp.h,
82 + AC_CHECK_LIB(slp, SLPOpen,
83 + AC_DEFINE(HAVE_LIBSLP, 1, [Define to 1 for SLP support])
84 + SLPOBJ="srvreg.o srvloc.o"
91 AC_CACHE_CHECK([for working socketpair],rsync_cv_HAVE_SOCKETPAIR,[
93 #include <sys/types.h>
94 diff --git a/loadparm.c b/loadparm.c
97 @@ -109,6 +109,13 @@ typedef struct
110 static global Globals;
111 @@ -300,7 +307,13 @@ static struct parm_struct parm_table[] =
112 {"motd file", P_STRING, P_GLOBAL,&Globals.motd_file, NULL,0},
113 {"pid file", P_STRING, P_GLOBAL,&Globals.pid_file, NULL,0},
114 {"port", P_INTEGER,P_GLOBAL,&Globals.rsync_port, NULL,0},
116 + {"slp refresh", P_INTEGER,P_GLOBAL,&Globals.slp_refresh, NULL,0},
118 {"socket options", P_STRING, P_GLOBAL,&Globals.socket_options, NULL,0},
120 + {"use slp", P_BOOL, P_GLOBAL,&Globals.use_slp, NULL,0},
123 {"auth users", P_STRING, P_LOCAL, &sDefault.auth_users, NULL,0},
124 {"charset", P_STRING, P_LOCAL, &sDefault.charset, NULL,0},
125 @@ -354,6 +367,9 @@ static struct parm_struct parm_table[] =
126 static void init_globals(void)
128 memset(&Globals, 0, sizeof Globals);
130 + Globals.use_slp = True;
134 /***************************************************************************
135 @@ -394,6 +410,13 @@ FN_GLOBAL_STRING(lp_pid_file, &Globals.pid_file)
136 FN_GLOBAL_STRING(lp_socket_options, &Globals.socket_options)
138 FN_GLOBAL_INTEGER(lp_rsync_port, &Globals.rsync_port)
140 +FN_GLOBAL_INTEGER(lp_slp_refresh, &Globals.slp_refresh)
144 +FN_GLOBAL_BOOL(lp_use_slp, &Globals.use_slp)
147 FN_LOCAL_STRING(lp_auth_users, auth_users)
148 FN_LOCAL_STRING(lp_charset, charset)
149 diff --git a/main.c b/main.c
152 @@ -1133,6 +1133,18 @@ static int start_client(int argc, char *argv[])
154 if (!read_batch) { /* for read_batch, NO source is specified */
155 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
157 + if (shell_machine && !shell_machine[0]) {
159 + /* User entered just rsync:// URI */
160 + print_service_list();
162 +#else /* No SLP, die here */
163 + rprintf(FINFO, "No SLP support, cannot browse\n");
164 + exit_cleanup(RERR_SYNTAX);
168 if (path) { /* source is remote */
171 diff --git a/options.c b/options.c
174 @@ -223,6 +223,7 @@ static void print_rsync_version(enum logcode f)
175 char const *links = "no ";
176 char const *iconv = "no ";
177 char const *ipv6 = "no ";
178 + char const *slp = "no ";
179 STRUCT_STAT *dumstat;
181 #if SUBPROTOCOL_VERSION != 0
182 @@ -256,6 +257,9 @@ static void print_rsync_version(enum logcode f)
183 #ifdef CAN_SET_SYMLINK_TIMES
190 rprintf(f, "%s version %s protocol version %d%s\n",
191 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
192 @@ -269,8 +273,8 @@ static void print_rsync_version(enum logcode f)
193 (int)(sizeof (int64) * 8));
194 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
195 got_socketpair, hardlinks, links, ipv6, have_inplace);
196 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
197 - have_inplace, acls, xattrs, iconv, symtimes);
198 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sSLP\n",
199 + have_inplace, acls, xattrs, iconv, symtimes, slp);
201 #ifdef MAINTAINER_MODE
202 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
203 diff --git a/rsync.h b/rsync.h
207 #define SIGNIFICANT_ITEM_FLAGS (~(\
208 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
210 +/* this is the minimum we'll use, irrespective of config setting */
211 +/* definately don't set to less than about 30 seconds */
212 +#define SLP_MIN_TIMEOUT 120
214 #define CFN_KEEP_DOT_DIRS (1<<0)
215 #define CFN_KEEP_TRAILING_SLASH (1<<1)
216 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
217 diff --git a/rsync.yo b/rsync.yo
220 @@ -148,7 +148,12 @@ particular rsync daemon by leaving off the module name:
222 quote(tt(rsync somehost.mydomain.com::))
224 -See the following section for more details.
225 +And, if Service Location Protocol is available, the following will list the
226 +available rsync servers:
228 +quote(tt(rsync rsync://))
230 +See the following section for even more usage details.
232 manpagesection(ADVANCED USAGE)
234 diff --git a/rsyncd.conf b/rsyncd.conf
242 diff --git a/rsyncd.conf.yo b/rsyncd.conf.yo
245 @@ -104,6 +104,19 @@ details on some of the options you may be able to set. By default no
246 special socket options are set. These settings can also be specified
247 via the bf(--sockopts) command-line option.
249 +dit(bf(use slp)) This parameter is used to determine if the module names are
250 +advertised via slp. The default is for this to be enabled, which will
251 +advertise your public modules.
253 +dit(bf(slp refresh)) This parameter is used to determine how long service
254 +advertisements are valid (measured in seconds), and is only applicable if
255 +you have Service Location Protocol support compiled in. If this is
256 +not set or is set to zero, then service advertisements never timeout. If
257 +this is set to less than 120 seconds, then 120 seconds is used. If it is
258 +set to more than 65535, then 65535 is used (which is a limitation of SLP).
259 +Using 3600 (one hour) is a good number if you tend to change your
264 manpagesection(MODULE PARAMETERS)
265 @@ -658,6 +671,7 @@ use chroot = yes
267 syslog facility = local5
268 pid file = /var/run/rsyncd.pid
272 path = /var/ftp/./pub
273 diff --git a/socket.c b/socket.c
276 @@ -544,6 +544,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
281 + time_t next_slp_refresh;
282 + short slp_timeout = lp_use_slp() ? lp_slp_refresh() : 0;
284 + if (slp_timeout < SLP_MIN_TIMEOUT)
285 + slp_timeout = SLP_MIN_TIMEOUT;
286 + /* re-register before slp times out */
291 #ifdef HAVE_SIGACTION
292 sigact.sa_flags = SA_NOCLDSTOP;
293 @@ -572,14 +582,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
298 + next_slp_refresh = time(NULL) + slp_timeout;
301 /* now accept incoming connections - forking a new process
302 * for each incoming connection */
308 struct sockaddr_storage addr;
309 socklen_t addrlen = sizeof addr;
311 + struct timeval slp_tv;
313 + slp_tv.tv_sec = 10;
314 + slp_tv.tv_usec = 0;
317 /* close log file before the potentially very long select so
318 * file can be trimmed by another process instead of growing
319 @@ -592,7 +613,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
323 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
325 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
326 + slp_timeout ? &slp_tv : NULL);
327 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
328 + rprintf(FINFO, "Service registration expired, refreshing it\n");
329 + register_services();
330 + next_slp_refresh = time(NULL) + slp_timeout;
333 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
338 for (i = 0, fd = -1; sp[i] >= 0; i++) {
339 diff --git a/srvloc.c b/srvloc.c
344 +/* -*- c-file-style: "linux"; -*-
346 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
348 + This program is free software; you can redistribute it and/or modify
349 + it under the terms of the GNU General Public License as published by
350 + the Free Software Foundation; either version 2 of the License, or
351 + (at your option) any later version.
353 + This program is distributed in the hope that it will be useful,
354 + but WITHOUT ANY WARRANTY; without even the implied warranty of
355 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
356 + GNU General Public License for more details.
358 + You should have received a copy of the GNU General Public License
359 + along with this program; if not, write to the Free Software
360 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
363 +/* This file implements the service location functionality */
364 +/* Basically, it uses normal Service Location Protocol API */
366 +/* It is really a cheap hack - just to show how it might work
367 + in a real application.
376 +/* This one just prints out the attributes */
377 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
378 + SLPError errcode, UNUSED(void *cookie))
382 + if (errcode == SLP_OK) {
383 + if (!strcmp(attrlist, "(comment=)"))
384 + rprintf(FINFO, "\t(No description)\n");
386 + cleanstr = strrchr(attrlist, ')') ;
387 + *cleanstr = ' '; /* remove last ')' */
388 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
394 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
395 + const char *srvurl, UNUSED(unsigned short lifetime),
396 + SLPError errcode, void *cookie)
399 + SLPHandle attrhslp;
401 + if (errcode == SLP_OK) {
402 + /* chop service: off the front */
403 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
404 + /* check for any attributes */
405 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
406 + result = SLPFindAttrs(attrhslp, srvurl,
407 + "", /* return all attributes */
408 + "", /* use configured scopes */
409 + getAttrCallback, NULL);
410 + if (result != SLP_OK) {
411 + rprintf(FERROR, "errorcode: %i\n",result);
413 + SLPClose(attrhslp);
415 + *(SLPError*)cookie = SLP_OK;
417 + *(SLPError*)cookie = errcode;
419 + /* Return SLP_TRUE because we want to be called again
420 + * if more services were found. */
425 +int print_service_list(void)
428 + SLPError callbackerr;
431 + err = SLPOpen("en",SLP_FALSE,&hslp);
432 + if (err != SLP_OK) {
433 + rprintf(FERROR, "Error opening slp handle %i\n", err);
437 + SLPFindSrvs(hslp, "rsync",
438 + 0, /* use configured scopes */
439 + 0, /* no attr filter */
440 + getSLPSrvURLCallback, &callbackerr);
442 + /* Now that we're done using slp, close the slp handle */
447 diff --git a/srvreg.c b/srvreg.c
452 +/* -*- c-file-style: "linux"; -*-
454 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
456 + This program is free software; you can redistribute it and/or modify
457 + it under the terms of the GNU General Public License as published by
458 + the Free Software Foundation; either version 2 of the License, or
459 + (at your option) any later version.
461 + This program is distributed in the hope that it will be useful,
462 + but WITHOUT ANY WARRANTY; without even the implied warranty of
463 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
464 + GNU General Public License for more details.
466 + You should have received a copy of the GNU General Public License
467 + along with this program; if not, write to the Free Software
468 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
471 +/* This file implements the service registration functionality */
473 +/* Basically, it uses normal Service Location Protocol API */
479 +extern int rsync_port;
481 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
483 + /* return the error code in the cookie */
484 + *(SLPError*)cookie = errcode;
486 + /* You could do something else here like print out
487 + * the errcode, etc. Remember, as a general rule,
488 + * do not try to do too much in a callback because
489 + * it is being executed by the same thread that is
490 + * reading slp packets from the wire. */
493 +int register_services(void)
495 + SLPError err, callbackerr;
501 + char localhost[256];
502 + extern char *config_file;
504 + struct addrinfo aih, *ai = 0;
506 + if (!lp_load(config_file, 0)) {
507 + exit_cleanup(RERR_SYNTAX);
510 + n = lp_numservices();
512 + if (0 == lp_slp_refresh())
513 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
514 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
515 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
516 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
517 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
519 + timeout = lp_slp_refresh();
521 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
522 + err = SLPOpen("en",SLP_FALSE,&hslp);
523 + if (err != SLP_OK) {
524 + rprintf(FINFO, "Error opening slp handle %i\n",err);
527 + if (gethostname(localhost, sizeof localhost)) {
528 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
531 + memset(&aih, 0, sizeof aih);
532 + aih.ai_family = PF_UNSPEC;
533 + aih.ai_flags = AI_CANONNAME;
534 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
535 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
538 + /* Register each service with SLP */
539 + for (i = 0; i < n; i++) {
543 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
547 + rprintf(FINFO, " %s\n", srv);
548 + if (lp_comment(i)) {
549 + snprintf(attr, sizeof attr, "(comment=%s)",
553 + srv, /* service to register */
555 + 0, /* this is ignored */
556 + attr, /* attributes */
557 + SLP_TRUE, /* new registration - don't change this */
558 + slp_callback, /* callback */
561 + /* err may contain an error code that occurred as the slp library
562 + * _prepared_ to make the call. */
563 + if (err != SLP_OK || callbackerr != SLP_OK)
564 + rprintf(FINFO, "Error registering service with slp %i\n", err);
566 + /* callbackerr may contain an error code (that was assigned through
567 + * the callback cookie) that occurred as slp packets were sent on
569 + if (callbackerr != SLP_OK)
570 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
573 + /* Now that we're done using slp, close the slp handle */
577 + /* refresh is done in main select loop */