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
14 index feacb90..09e1547 100644
17 @@ -13,6 +13,8 @@ CFLAGS=@CFLAGS@
26 @@ -38,7 +40,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 @@ -74,7 +76,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
45 index b6afe00..a242809 100644
48 @@ -1153,6 +1153,13 @@ int daemon_main(void)
49 * address too. In fact, why not just do inet_ntop on the
53 + if (register_services()) {
55 + "Couldn't register with service discovery protocol, continuing anyway\n");
59 start_accept_loop(rsync_port, start_daemon);
62 diff --git a/configure.in b/configure.in
63 index bc7d4a7..f8dc177 100644
66 @@ -647,6 +647,29 @@ if test $rsync_cv_can_hardlink_special = yes; then
67 AC_DEFINE(CAN_HARDLINK_SPECIAL, 1, [Define to 1 if link() can hard-link special files.])
70 +AC_ARG_ENABLE(slp, [ --disable-slp turn off SLP support, defaults to on])
71 +AC_ARG_WITH(openslp-libs, [ --with-openslp-libs set directory for OpenSLP library],
72 + LDFLAGS="-L$withval $LDFLAGS"
73 + DSOFLAGS="-L$withval $DSOFLAGS",)
74 +AC_ARG_WITH(openslp-includes, [ --with-openslp-includes set directory for OpenSLP includes],
75 + CFLAGS="-I$withval $CFLAGS"
76 + CXXFLAGS="-I$withval $CXXFLAGS"
77 + CPPFLAGS="-I$withval $CPPFLAGS",)
82 +if test x$enable_slp != xno; then
83 + AC_CHECK_HEADER(slp.h,
84 + AC_CHECK_LIB(slp, SLPOpen,
85 + AC_DEFINE(HAVE_LIBSLP, 1, [Define to 1 for SLP support])
86 + SLPOBJ="srvreg.o srvloc.o"
93 AC_CACHE_CHECK([for working socketpair],rsync_cv_HAVE_SOCKETPAIR,[
95 #include <sys/types.h>
96 diff --git a/loadparm.c b/loadparm.c
97 index 8e48e6d..a833006 100644
100 @@ -97,6 +97,9 @@ typedef struct {
101 char *socket_options;
109 /* This structure describes a single section. Their order must match the
110 @@ -311,6 +314,9 @@ static struct parm_struct parm_table[] =
111 {"motd file", P_STRING, P_GLOBAL,&Vars.g.motd_file, NULL,0},
112 {"pid file", P_STRING, P_GLOBAL,&Vars.g.pid_file, NULL,0},
113 {"port", P_INTEGER,P_GLOBAL,&Vars.g.rsync_port, NULL,0},
115 + {"slp refresh", P_INTEGER,P_GLOBAL,&Vars.g.slp_refresh, NULL,0},
117 {"socket options", P_STRING, P_GLOBAL,&Vars.g.socket_options, NULL,0},
119 {"auth users", P_STRING, P_LOCAL, &Vars.l.auth_users, NULL,0},
120 @@ -392,6 +398,9 @@ FN_GLOBAL_STRING(lp_pid_file, &Vars.g.pid_file)
121 FN_GLOBAL_STRING(lp_socket_options, &Vars.g.socket_options)
123 FN_GLOBAL_INTEGER(lp_rsync_port, &Vars.g.rsync_port)
125 +FN_GLOBAL_INTEGER(lp_slp_refresh, &Vars.g.slp_refresh)
128 FN_LOCAL_STRING(lp_auth_users, auth_users)
129 FN_LOCAL_STRING(lp_charset, charset)
130 diff --git a/main.c b/main.c
131 index 2ef2f47..3535264 100644
134 @@ -1195,6 +1195,18 @@ static int start_client(int argc, char *argv[])
136 if (!read_batch) { /* for read_batch, NO source is specified */
137 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
139 + if (shell_machine && !shell_machine[0]) {
141 + /* User entered just rsync:// URI */
142 + print_service_list();
144 +#else /* No SLP, die here */
145 + rprintf(FINFO, "No SLP support, cannot browse\n");
146 + exit_cleanup(RERR_SYNTAX);
150 if (path) { /* source is remote */
153 diff --git a/options.c b/options.c
154 index e7c6c61..dae5716 100644
157 @@ -566,6 +566,7 @@ static void print_rsync_version(enum logcode f)
158 char const *links = "no ";
159 char const *iconv = "no ";
160 char const *ipv6 = "no ";
161 + char const *slp = "no ";
162 STRUCT_STAT *dumstat;
164 #if SUBPROTOCOL_VERSION != 0
165 @@ -599,6 +600,9 @@ static void print_rsync_version(enum logcode f)
166 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
173 rprintf(f, "%s version %s protocol version %d%s\n",
174 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
175 @@ -612,8 +616,8 @@ static void print_rsync_version(enum logcode f)
176 (int)(sizeof (int64) * 8));
177 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
178 got_socketpair, hardlinks, links, ipv6, have_inplace);
179 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
180 - have_inplace, acls, xattrs, iconv, symtimes);
181 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sSLP\n",
182 + have_inplace, acls, xattrs, iconv, symtimes, slp);
184 #ifdef MAINTAINER_MODE
185 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
186 diff --git a/rsync.h b/rsync.h
187 index be7cf8a..8353124 100644
191 #define SIGNIFICANT_ITEM_FLAGS (~(\
192 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
194 +/* this is the minimum we'll use, irrespective of config setting */
195 +/* definately don't set to less than about 30 seconds */
196 +#define SLP_MIN_TIMEOUT 120
198 #define CFN_KEEP_DOT_DIRS (1<<0)
199 #define CFN_KEEP_TRAILING_SLASH (1<<1)
200 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
201 diff --git a/rsync.yo b/rsync.yo
202 index 941f7a5..1518b3f 100644
205 @@ -148,7 +148,12 @@ particular rsync daemon by leaving off the module name:
207 quote(tt(rsync somehost.mydomain.com::))
209 -See the following section for more details.
210 +And, if Service Location Protocol is available, the following will list the
211 +available rsync servers:
213 +quote(tt(rsync rsync://))
215 +See the following section for even more usage details.
217 manpagesection(ADVANCED USAGE)
219 diff --git a/rsyncd.conf b/rsyncd.conf
221 index 0000000..971723f
228 diff --git a/rsyncd.conf.yo b/rsyncd.conf.yo
229 index d4978cd..318f0ec 100644
232 @@ -108,6 +108,15 @@ details on some of the options you may be able to set. By default no
233 special socket options are set. These settings can also be specified
234 via the bf(--sockopts) command-line option.
236 +dit(bf(slp refresh)) This parameter is used to determine how long service
237 +advertisements are valid (measured in seconds), and is only applicable if
238 +you have Service Location Protocol support compiled in. If this is
239 +not set or is set to zero, then service advertisements never time out. If
240 +this is set to less than 120 seconds, then 120 seconds is used. If it is
241 +set to more than 65535, then 65535 is used (which is a limitation of SLP).
242 +Using 3600 (one hour) is a good number if you tend to change your
247 manpagesection(MODULE PARAMETERS)
248 @@ -739,6 +748,7 @@ use chroot = yes
250 syslog facility = local5
251 pid file = /var/run/rsyncd.pid
255 path = /var/ftp/./pub
256 diff --git a/socket.c b/socket.c
257 index 5df3a50..a4a2b0a 100644
260 @@ -530,6 +530,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
265 + time_t next_slp_refresh;
266 + short slp_timeout = lp_slp_refresh();
268 + if (slp_timeout < SLP_MIN_TIMEOUT)
269 + slp_timeout = SLP_MIN_TIMEOUT;
270 + /* re-register before slp times out */
275 #ifdef HAVE_SIGACTION
276 sigact.sa_flags = SA_NOCLDSTOP;
277 @@ -558,14 +568,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
282 + next_slp_refresh = time(NULL) + slp_timeout;
285 /* now accept incoming connections - forking a new process
286 * for each incoming connection */
292 struct sockaddr_storage addr;
293 socklen_t addrlen = sizeof addr;
295 + struct timeval slp_tv;
297 + slp_tv.tv_sec = 10;
298 + slp_tv.tv_usec = 0;
301 /* close log file before the potentially very long select so
302 * file can be trimmed by another process instead of growing
303 @@ -578,7 +599,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
307 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
309 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
310 + slp_timeout ? &slp_tv : NULL);
311 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
312 + rprintf(FINFO, "Service registration expired, refreshing it\n");
313 + register_services();
314 + next_slp_refresh = time(NULL) + slp_timeout;
317 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
322 for (i = 0, fd = -1; sp[i] >= 0; i++) {
323 diff --git a/srvloc.c b/srvloc.c
325 index 0000000..99eea16
329 +/* -*- c-file-style: "linux"; -*-
331 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
333 + This program is free software; you can redistribute it and/or modify
334 + it under the terms of the GNU General Public License as published by
335 + the Free Software Foundation; either version 2 of the License, or
336 + (at your option) any later version.
338 + This program is distributed in the hope that it will be useful,
339 + but WITHOUT ANY WARRANTY; without even the implied warranty of
340 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
341 + GNU General Public License for more details.
343 + You should have received a copy of the GNU General Public License
344 + along with this program; if not, write to the Free Software
345 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
348 +/* This file implements the service location functionality */
349 +/* Basically, it uses normal Service Location Protocol API */
351 +/* It is really a cheap hack - just to show how it might work
352 + in a real application.
361 +/* This one just prints out the attributes */
362 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
363 + SLPError errcode, UNUSED(void *cookie))
367 + if (errcode == SLP_OK) {
368 + if (!strcmp(attrlist, "(comment=)"))
369 + rprintf(FINFO, "\t(No description)\n");
371 + cleanstr = strrchr(attrlist, ')') ;
372 + *cleanstr = ' '; /* remove last ')' */
373 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
379 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
380 + const char *srvurl, UNUSED(unsigned short lifetime),
381 + SLPError errcode, void *cookie)
384 + SLPHandle attrhslp;
386 + if (errcode == SLP_OK) {
387 + /* chop service: off the front */
388 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
389 + /* check for any attributes */
390 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
391 + result = SLPFindAttrs(attrhslp, srvurl,
392 + "", /* return all attributes */
393 + "", /* use configured scopes */
394 + getAttrCallback, NULL);
395 + if (result != SLP_OK) {
396 + rprintf(FERROR, "errorcode: %i\n",result);
398 + SLPClose(attrhslp);
400 + *(SLPError*)cookie = SLP_OK;
402 + *(SLPError*)cookie = errcode;
404 + /* Return SLP_TRUE because we want to be called again
405 + * if more services were found. */
410 +int print_service_list(void)
413 + SLPError callbackerr;
416 + err = SLPOpen("en",SLP_FALSE,&hslp);
417 + if (err != SLP_OK) {
418 + rprintf(FERROR, "Error opening slp handle %i\n", err);
422 + SLPFindSrvs(hslp, "rsync",
423 + 0, /* use configured scopes */
424 + 0, /* no attr filter */
425 + getSLPSrvURLCallback, &callbackerr);
427 + /* Now that we're done using slp, close the slp handle */
432 diff --git a/srvreg.c b/srvreg.c
434 index 0000000..04afe60
438 +/* -*- c-file-style: "linux"; -*-
440 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
442 + This program is free software; you can redistribute it and/or modify
443 + it under the terms of the GNU General Public License as published by
444 + the Free Software Foundation; either version 2 of the License, or
445 + (at your option) any later version.
447 + This program is distributed in the hope that it will be useful,
448 + but WITHOUT ANY WARRANTY; without even the implied warranty of
449 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
450 + GNU General Public License for more details.
452 + You should have received a copy of the GNU General Public License
453 + along with this program; if not, write to the Free Software
454 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
457 +/* This file implements the service registration functionality */
459 +/* Basically, it uses normal Service Location Protocol API */
465 +extern int rsync_port;
467 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
469 + /* return the error code in the cookie */
470 + *(SLPError*)cookie = errcode;
472 + /* You could do something else here like print out
473 + * the errcode, etc. Remember, as a general rule,
474 + * do not try to do too much in a callback because
475 + * it is being executed by the same thread that is
476 + * reading slp packets from the wire. */
479 +int register_services(void)
481 + SLPError err, callbackerr;
487 + char localhost[256];
488 + extern char *config_file;
490 + struct addrinfo aih, *ai = 0;
492 + if (!lp_load(config_file, 0)) {
493 + exit_cleanup(RERR_SYNTAX);
496 + n = lp_numservices();
498 + if (0 == lp_slp_refresh())
499 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
500 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
501 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
502 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
503 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
505 + timeout = lp_slp_refresh();
507 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
508 + err = SLPOpen("en",SLP_FALSE,&hslp);
509 + if (err != SLP_OK) {
510 + rprintf(FINFO, "Error opening slp handle %i\n",err);
513 + if (gethostname(localhost, sizeof localhost)) {
514 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
517 + memset(&aih, 0, sizeof aih);
518 + aih.ai_family = PF_UNSPEC;
519 + aih.ai_flags = AI_CANONNAME;
520 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
521 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
524 + /* Register each service with SLP */
525 + for (i = 0; i < n; i++) {
529 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
533 + rprintf(FINFO, " %s\n", srv);
534 + if (lp_comment(i)) {
535 + snprintf(attr, sizeof attr, "(comment=%s)",
539 + srv, /* service to register */
541 + 0, /* this is ignored */
542 + attr, /* attributes */
543 + SLP_TRUE, /* new registration - don't change this */
544 + slp_callback, /* callback */
547 + /* err may contain an error code that occurred as the slp library
548 + * _prepared_ to make the call. */
549 + if (err != SLP_OK || callbackerr != SLP_OK)
550 + rprintf(FINFO, "Error registering service with slp %i\n", err);
552 + /* callbackerr may contain an error code (that was assigned through
553 + * the callback cookie) that occurred as slp packets were sent on
555 + if (callbackerr != SLP_OK)
556 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
559 + /* Now that we're done using slp, close the slp handle */
563 + /* refresh is done in main select loop */