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 diff --git a/Makefile.in b/Makefile.in
16 @@ -13,6 +13,8 @@ CFLAGS=@CFLAGS@
25 @@ -37,7 +39,7 @@ OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
26 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
27 fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
28 OBJS3=progress.o pipe.o
29 -DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
30 +DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o $(SLPOBJ)
31 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
32 popt/popthelp.o popt/poptparse.o
33 OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@
34 @@ -73,7 +75,7 @@ install-strip:
35 $(MAKE) INSTALL_STRIP='-s' install
37 rsync$(EXEEXT): $(OBJS)
38 - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
39 + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LIBSLP)
42 $(CHECK_OBJS): $(HEADERS)
43 diff --git a/clientserver.c b/clientserver.c
46 @@ -1062,6 +1062,13 @@ int daemon_main(void)
47 * address too. In fact, why not just do inet_ntop on the
51 + if (register_services()) {
53 + "Couldn't register with service discovery protocol, continuing anyway\n");
57 start_accept_loop(rsync_port, start_daemon);
60 diff --git a/configure.in b/configure.in
63 @@ -639,6 +639,29 @@ if test $rsync_cv_can_hardlink_special = yes; then
64 AC_DEFINE(CAN_HARDLINK_SPECIAL, 1, [Define to 1 if link() can hard-link special files.])
67 +AC_ARG_ENABLE(slp, [ --disable-slp turn off SLP support, defaults to on])
68 +AC_ARG_WITH(openslp-libs, [ --with-openslp-libs set directory for OpenSLP library],
69 + LDFLAGS="-L$withval $LDFLAGS"
70 + DSOFLAGS="-L$withval $DSOFLAGS",)
71 +AC_ARG_WITH(openslp-includes, [ --with-openslp-includes set directory for OpenSLP includes],
72 + CFLAGS="-I$withval $CFLAGS"
73 + CXXFLAGS="-I$withval $CXXFLAGS"
74 + CPPFLAGS="-I$withval $CPPFLAGS",)
79 +if test x$enable_slp != xno; then
80 + AC_CHECK_HEADER(slp.h,
81 + AC_CHECK_LIB(slp, SLPOpen,
82 + AC_DEFINE(HAVE_LIBSLP, 1, [Define to 1 for SLP support])
83 + SLPOBJ="srvreg.o srvloc.o"
90 AC_CACHE_CHECK([for working socketpair],rsync_cv_HAVE_SOCKETPAIR,[
92 #include <sys/types.h>
93 diff --git a/loadparm.c b/loadparm.c
96 @@ -111,6 +111,9 @@ typedef struct
105 static global Globals;
106 @@ -302,6 +305,9 @@ static struct parm_struct parm_table[] =
107 {"motd file", P_STRING, P_GLOBAL,&Globals.motd_file, NULL,0},
108 {"pid file", P_STRING, P_GLOBAL,&Globals.pid_file, NULL,0},
109 {"port", P_INTEGER,P_GLOBAL,&Globals.rsync_port, NULL,0},
111 + {"slp refresh", P_INTEGER,P_GLOBAL,&Globals.slp_refresh, NULL,0},
113 {"socket options", P_STRING, P_GLOBAL,&Globals.socket_options, NULL,0},
115 {"auth users", P_STRING, P_LOCAL, &sDefault.auth_users, NULL,0},
116 @@ -396,6 +402,9 @@ FN_GLOBAL_STRING(lp_pid_file, &Globals.pid_file)
117 FN_GLOBAL_STRING(lp_socket_options, &Globals.socket_options)
119 FN_GLOBAL_INTEGER(lp_rsync_port, &Globals.rsync_port)
121 +FN_GLOBAL_INTEGER(lp_slp_refresh, &Globals.slp_refresh)
124 FN_LOCAL_STRING(lp_auth_users, auth_users)
125 FN_LOCAL_STRING(lp_charset, charset)
126 diff --git a/main.c b/main.c
129 @@ -1120,6 +1120,18 @@ static int start_client(int argc, char *argv[])
131 if (!read_batch) { /* for read_batch, NO source is specified */
132 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
134 + if (shell_machine && !shell_machine[0]) {
136 + /* User entered just rsync:// URI */
137 + print_service_list();
139 +#else /* No SLP, die here */
140 + rprintf(FINFO, "No SLP support, cannot browse\n");
141 + exit_cleanup(RERR_SYNTAX);
145 if (path) { /* source is remote */
148 diff --git a/options.c b/options.c
151 @@ -225,6 +225,7 @@ static void print_rsync_version(enum logcode f)
152 char const *links = "no ";
153 char const *iconv = "no ";
154 char const *ipv6 = "no ";
155 + char const *slp = "no ";
156 STRUCT_STAT *dumstat;
158 #if SUBPROTOCOL_VERSION != 0
159 @@ -257,6 +258,9 @@ static void print_rsync_version(enum logcode f)
160 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
167 rprintf(f, "%s version %s protocol version %d%s\n",
168 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
169 @@ -270,8 +274,8 @@ static void print_rsync_version(enum logcode f)
170 (int)(sizeof (int64) * 8));
171 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
172 got_socketpair, hardlinks, links, ipv6, have_inplace);
173 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
174 - have_inplace, acls, xattrs, iconv, symtimes);
175 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sSLP\n",
176 + have_inplace, acls, xattrs, iconv, symtimes, slp);
178 #ifdef MAINTAINER_MODE
179 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
180 diff --git a/rsync.h b/rsync.h
184 #define SIGNIFICANT_ITEM_FLAGS (~(\
185 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
187 +/* this is the minimum we'll use, irrespective of config setting */
188 +/* definately don't set to less than about 30 seconds */
189 +#define SLP_MIN_TIMEOUT 120
191 #define CFN_KEEP_DOT_DIRS (1<<0)
192 #define CFN_KEEP_TRAILING_SLASH (1<<1)
193 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
194 diff --git a/rsync.yo b/rsync.yo
197 @@ -144,7 +144,12 @@ particular rsync daemon by leaving off the module name:
199 quote(tt(rsync somehost.mydomain.com::))
201 -See the following section for more details.
202 +And, if Service Location Protocol is available, the following will list the
203 +available rsync servers:
205 +quote(tt(rsync rsync://))
207 +See the following section for even more usage details.
209 manpagesection(ADVANCED USAGE)
211 diff --git a/rsyncd.conf b/rsyncd.conf
219 diff --git a/rsyncd.conf.yo b/rsyncd.conf.yo
222 @@ -104,6 +104,15 @@ details on some of the options you may be able to set. By default no
223 special socket options are set. These settings are superseded by the
224 bf(--sockopts) command-line option.
226 +dit(bf(slp refresh)) This parameter is used to determine how long service
227 +advertisements are valid (measured in seconds), and is only applicable if
228 +you have Service Location Protocol support compiled in. If this is
229 +not set or is set to zero, then service advertisements never time out. If
230 +this is set to less than 120 seconds, then 120 seconds is used. If it is
231 +set to more than 65535, then 65535 is used (which is a limitation of SLP).
232 +Using 3600 (one hour) is a good number if you tend to change your
238 @@ -652,6 +661,7 @@ use chroot = yes
240 syslog facility = local5
241 pid file = /var/run/rsyncd.pid
245 path = /var/ftp/./pub
246 diff --git a/socket.c b/socket.c
249 @@ -530,6 +530,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
254 + time_t next_slp_refresh;
255 + short slp_timeout = lp_slp_refresh();
257 + if (slp_timeout < SLP_MIN_TIMEOUT)
258 + slp_timeout = SLP_MIN_TIMEOUT;
259 + /* re-register before slp times out */
264 #ifdef HAVE_SIGACTION
265 sigact.sa_flags = SA_NOCLDSTOP;
266 @@ -558,14 +568,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
271 + next_slp_refresh = time(NULL) + slp_timeout;
274 /* now accept incoming connections - forking a new process
275 * for each incoming connection */
281 struct sockaddr_storage addr;
282 socklen_t addrlen = sizeof addr;
284 + struct timeval slp_tv;
286 + slp_tv.tv_sec = 10;
287 + slp_tv.tv_usec = 0;
290 /* close log file before the potentially very long select so
291 * file can be trimmed by another process instead of growing
292 @@ -577,8 +598,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
297 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) != 1)
299 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
300 + slp_timeout ? &slp_tv : NULL);
301 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
302 + rprintf(FINFO, "Service registration expired, refreshing it\n");
303 + register_services();
304 + next_slp_refresh = time(NULL) + slp_timeout;
307 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
312 for (i = 0, fd = -1; sp[i] >= 0; i++) {
313 diff --git a/srvloc.c b/srvloc.c
318 +/* -*- c-file-style: "linux"; -*-
320 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
322 + This program is free software; you can redistribute it and/or modify
323 + it under the terms of the GNU General Public License as published by
324 + the Free Software Foundation; either version 2 of the License, or
325 + (at your option) any later version.
327 + This program is distributed in the hope that it will be useful,
328 + but WITHOUT ANY WARRANTY; without even the implied warranty of
329 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
330 + GNU General Public License for more details.
332 + You should have received a copy of the GNU General Public License
333 + along with this program; if not, write to the Free Software
334 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
337 +/* This file implements the service location functionality */
338 +/* Basically, it uses normal Service Location Protocol API */
340 +/* It is really a cheap hack - just to show how it might work
341 + in a real application.
350 +/* This one just prints out the attributes */
351 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
352 + SLPError errcode, UNUSED(void *cookie))
356 + if (errcode == SLP_OK) {
357 + if (!strcmp(attrlist, "(comment=)"))
358 + rprintf(FINFO, "\t(No description)\n");
360 + cleanstr = strrchr(attrlist, ')') ;
361 + *cleanstr = ' '; /* remove last ')' */
362 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
368 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
369 + const char *srvurl, UNUSED(unsigned short lifetime),
370 + SLPError errcode, void *cookie)
373 + SLPHandle attrhslp;
375 + if (errcode == SLP_OK) {
376 + /* chop service: off the front */
377 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
378 + /* check for any attributes */
379 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
380 + result = SLPFindAttrs(attrhslp, srvurl,
381 + "", /* return all attributes */
382 + "", /* use configured scopes */
383 + getAttrCallback, NULL);
384 + if (result != SLP_OK) {
385 + rprintf(FERROR, "errorcode: %i\n",result);
387 + SLPClose(attrhslp);
389 + *(SLPError*)cookie = SLP_OK;
391 + *(SLPError*)cookie = errcode;
393 + /* Return SLP_TRUE because we want to be called again
394 + * if more services were found. */
399 +int print_service_list(void)
402 + SLPError callbackerr;
405 + err = SLPOpen("en",SLP_FALSE,&hslp);
406 + if (err != SLP_OK) {
407 + rprintf(FERROR, "Error opening slp handle %i\n", err);
411 + SLPFindSrvs(hslp, "rsync",
412 + 0, /* use configured scopes */
413 + 0, /* no attr filter */
414 + getSLPSrvURLCallback, &callbackerr);
416 + /* Now that we're done using slp, close the slp handle */
421 diff --git a/srvreg.c b/srvreg.c
426 +/* -*- c-file-style: "linux"; -*-
428 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
430 + This program is free software; you can redistribute it and/or modify
431 + it under the terms of the GNU General Public License as published by
432 + the Free Software Foundation; either version 2 of the License, or
433 + (at your option) any later version.
435 + This program is distributed in the hope that it will be useful,
436 + but WITHOUT ANY WARRANTY; without even the implied warranty of
437 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
438 + GNU General Public License for more details.
440 + You should have received a copy of the GNU General Public License
441 + along with this program; if not, write to the Free Software
442 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
445 +/* This file implements the service registration functionality */
447 +/* Basically, it uses normal Service Location Protocol API */
453 +extern int rsync_port;
455 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
457 + /* return the error code in the cookie */
458 + *(SLPError*)cookie = errcode;
460 + /* You could do something else here like print out
461 + * the errcode, etc. Remember, as a general rule,
462 + * do not try to do too much in a callback because
463 + * it is being executed by the same thread that is
464 + * reading slp packets from the wire. */
467 +int register_services(void)
469 + SLPError err, callbackerr;
475 + char localhost[256];
476 + extern char *config_file;
478 + struct addrinfo aih, *ai = 0;
480 + if (!lp_load(config_file, 0)) {
481 + exit_cleanup(RERR_SYNTAX);
484 + n = lp_numservices();
486 + if (0 == lp_slp_refresh())
487 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
488 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
489 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
490 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
491 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
493 + timeout = lp_slp_refresh();
495 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
496 + err = SLPOpen("en",SLP_FALSE,&hslp);
497 + if (err != SLP_OK) {
498 + rprintf(FINFO, "Error opening slp handle %i\n",err);
501 + if (gethostname(localhost, sizeof localhost)) {
502 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
505 + memset(&aih, 0, sizeof aih);
506 + aih.ai_family = PF_UNSPEC;
507 + aih.ai_flags = AI_CANONNAME;
508 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
509 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
512 + /* Register each service with SLP */
513 + for (i = 0; i < n; i++) {
517 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
521 + rprintf(FINFO, " %s\n", srv);
522 + if (lp_comment(i)) {
523 + snprintf(attr, sizeof attr, "(comment=%s)",
527 + srv, /* service to register */
529 + 0, /* this is ignored */
530 + attr, /* attributes */
531 + SLP_TRUE, /* new registration - don't change this */
532 + slp_callback, /* callback */
535 + /* err may contain an error code that occurred as the slp library
536 + * _prepared_ to make the call. */
537 + if (err != SLP_OK || callbackerr != SLP_OK)
538 + rprintf(FINFO, "Error registering service with slp %i\n", err);
540 + /* callbackerr may contain an error code (that was assigned through
541 + * the callback cookie) that occurred as slp packets were sent on
543 + if (callbackerr != SLP_OK)
544 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
547 + /* Now that we're done using slp, close the slp handle */
551 + /* refresh is done in main select loop */