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: 8b1b81e054bdcc927ff26f20f424e034bd273175
14 diff --git a/Makefile.in b/Makefile.in
17 @@ -17,6 +17,8 @@ CXX=@CXX@
26 @@ -48,7 +50,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 usage.o fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
29 OBJS3=progress.o pipe.o @MD5_ASM@ @ROLL_SIMD@ @ROLL_ASM@
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) @BUILD_ZLIB@ @BUILD_POPT@
35 @@ -101,7 +103,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)
42 rrsync: support/rrsync
43 cp -p $(srcdir)/support/rrsync rrsync
44 diff --git a/clientserver.c b/clientserver.c
47 @@ -1527,6 +1527,13 @@ int daemon_main(void)
48 * address too. In fact, why not just do getnameinfo on the
52 + if (lp_use_slp() && register_services()) {
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 @@ -1049,6 +1049,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,[
92 AC_RUN_IFELSE([AC_LANG_SOURCE([[
93 #ifdef HAVE_SYS_TYPES_H
94 diff --git a/daemon-parm.txt b/daemon-parm.txt
97 @@ -10,8 +10,10 @@ STRING socket_options NULL
99 INTEGER listen_backlog 5
100 INTEGER rsync_port|port 0
101 +INTEGER slp_refresh 0
103 BOOL proxy_protocol False
106 Locals: =================================================================
108 diff --git a/main.c b/main.c
111 @@ -1402,6 +1402,22 @@ static int start_client(int argc, char *argv[])
113 if (!read_batch) { /* for read_batch, NO source is specified */
114 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
116 + if (shell_machine && !shell_machine[0]) {
118 + /* User entered just rsync:// URI */
119 + if (lp_use_slp()) {
120 + print_service_list();
123 + rprintf(FINFO, "SLP is disabled, cannot browse\n");
124 + exit_cleanup(RERR_SYNTAX);
125 +#else /* No SLP, die here */
126 + rprintf(FINFO, "No SLP support, cannot browse\n");
127 + exit_cleanup(RERR_SYNTAX);
131 if (path) { /* source is remote */
134 diff --git a/rsync.1.md b/rsync.1.md
137 @@ -152,7 +152,19 @@ rsync daemon by leaving off the module name:
139 > rsync somehost.mydomain.com::
141 -See the following section for more details.
142 +And, if Service Location Protocol is available, the following will list the
143 +available rsync servers:
147 +See the following section for even more usage details.
149 +One more thing, if Service Location Protocol is available, the following will
150 +list the available rsync servers:
154 +See the following section for even more usage details.
156 ## SORTED TRANSFER ORDER
158 diff --git a/rsync.h b/rsync.h
162 #define SIGNIFICANT_ITEM_FLAGS (~(\
163 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
165 +/* this is the minimum we'll use, irrespective of config setting */
166 +/* definitely don't set to less than about 30 seconds */
167 +#define SLP_MIN_TIMEOUT 120
169 #define CFN_KEEP_DOT_DIRS (1<<0)
170 #define CFN_KEEP_TRAILING_SLASH (1<<1)
171 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
172 diff --git a/rsyncd.conf b/rsyncd.conf
178 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
179 --- a/rsyncd.conf.5.md
180 +++ b/rsyncd.conf.5.md
181 @@ -138,6 +138,21 @@ a literal % into a value is to use %%.
182 You can override the default backlog value when the daemon listens for
183 connections. It defaults to 5.
187 + You can enable Service Location Protocol support by enabling this global
188 + parameter. The default is "false".
192 + This parameter is used to determine how long service advertisements are
193 + valid (measured in seconds), and is only applicable if you have Service
194 + Location Protocol support compiled in. If this is not set or is set to
195 + zero, then service advertisements never time out. If this is set to less
196 + than 120 seconds, then 120 seconds is used. If it is set to more than
197 + 65535, then 65535 is used (which is a limitation of SLP). Using 3600
198 + (one hour) is a good number if you tend to change your configuration.
202 After the global parameters you should define a number of modules, each module
203 @@ -1176,6 +1191,7 @@ A more sophisticated example would be:
204 > max connections = 4
205 > syslog facility = local5
206 > pid file = /var/run/rsyncd.pid
207 +> slp refresh = 3600
210 > path = /var/ftp/./pub
211 diff --git a/socket.c b/socket.c
214 @@ -534,6 +534,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
219 + time_t next_slp_refresh;
220 + short slp_timeout = lp_use_slp() ? lp_slp_refresh() : 0;
222 + if (slp_timeout < SLP_MIN_TIMEOUT)
223 + slp_timeout = SLP_MIN_TIMEOUT;
224 + /* re-register before slp times out */
229 #ifdef HAVE_SIGACTION
230 sigact.sa_flags = SA_NOCLDSTOP;
231 @@ -561,14 +571,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
236 + next_slp_refresh = time(NULL) + slp_timeout;
239 /* now accept incoming connections - forking a new process
240 * for each incoming connection */
246 struct sockaddr_storage addr;
247 socklen_t addrlen = sizeof addr;
249 + struct timeval slp_tv;
251 + slp_tv.tv_sec = 10;
252 + slp_tv.tv_usec = 0;
255 /* close log file before the potentially very long select so
256 * file can be trimmed by another process instead of growing
257 @@ -581,7 +602,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
261 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
263 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
264 + slp_timeout ? &slp_tv : NULL);
265 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
266 + rprintf(FINFO, "Service registration expired, refreshing it\n");
267 + register_services();
268 + next_slp_refresh = time(NULL) + slp_timeout;
271 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
276 for (i = 0, fd = -1; sp[i] >= 0; i++) {
277 diff --git a/srvloc.c b/srvloc.c
282 +/* -*- c-file-style: "linux"; -*-
284 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
286 + This program is free software; you can redistribute it and/or modify
287 + it under the terms of the GNU General Public License as published by
288 + the Free Software Foundation; either version 2 of the License, or
289 + (at your option) any later version.
291 + This program is distributed in the hope that it will be useful,
292 + but WITHOUT ANY WARRANTY; without even the implied warranty of
293 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
294 + GNU General Public License for more details.
296 + You should have received a copy of the GNU General Public License
297 + along with this program; if not, write to the Free Software
298 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
301 +/* This file implements the service location functionality */
302 +/* Basically, it uses normal Service Location Protocol API */
304 +/* It is really a cheap hack - just to show how it might work
305 + in a real application.
314 +/* This one just prints out the attributes */
315 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
316 + SLPError errcode, UNUSED(void *cookie))
320 + if (errcode == SLP_OK) {
321 + if (!strcmp(attrlist, "(comment=)"))
322 + rprintf(FINFO, "\t(No description)\n");
324 + cleanstr = strrchr(attrlist, ')') ;
325 + *cleanstr = ' '; /* remove last ')' */
326 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
332 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
333 + const char *srvurl, UNUSED(unsigned short lifetime),
334 + SLPError errcode, void *cookie)
337 + SLPHandle attrhslp;
339 + if (errcode == SLP_OK) {
340 + /* chop service: off the front */
341 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
342 + /* check for any attributes */
343 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
344 + result = SLPFindAttrs(attrhslp, srvurl,
345 + "", /* return all attributes */
346 + "", /* use configured scopes */
347 + getAttrCallback, NULL);
348 + if (result != SLP_OK) {
349 + rprintf(FERROR, "errorcode: %i\n",result);
351 + SLPClose(attrhslp);
353 + *(SLPError*)cookie = SLP_OK;
355 + *(SLPError*)cookie = errcode;
357 + /* Return SLP_TRUE because we want to be called again
358 + * if more services were found. */
363 +int print_service_list(void)
366 + SLPError callbackerr;
369 + err = SLPOpen("en",SLP_FALSE,&hslp);
370 + if (err != SLP_OK) {
371 + rprintf(FERROR, "Error opening slp handle %i\n", err);
375 + SLPFindSrvs(hslp, "rsync",
376 + 0, /* use configured scopes */
377 + 0, /* no attr filter */
378 + getSLPSrvURLCallback, &callbackerr);
380 + /* Now that we're done using slp, close the slp handle */
385 diff --git a/srvreg.c b/srvreg.c
390 +/* -*- c-file-style: "linux"; -*-
392 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
394 + This program is free software; you can redistribute it and/or modify
395 + it under the terms of the GNU General Public License as published by
396 + the Free Software Foundation; either version 2 of the License, or
397 + (at your option) any later version.
399 + This program is distributed in the hope that it will be useful,
400 + but WITHOUT ANY WARRANTY; without even the implied warranty of
401 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
402 + GNU General Public License for more details.
404 + You should have received a copy of the GNU General Public License
405 + along with this program; if not, write to the Free Software
406 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
409 +/* This file implements the service registration functionality */
411 +/* Basically, it uses normal Service Location Protocol API */
417 +extern int rsync_port;
419 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
421 + /* return the error code in the cookie */
422 + *(SLPError*)cookie = errcode;
424 + /* You could do something else here like print out
425 + * the errcode, etc. Remember, as a general rule,
426 + * do not try to do too much in a callback because
427 + * it is being executed by the same thread that is
428 + * reading slp packets from the wire. */
431 +int register_services(void)
433 + SLPError err, callbackerr;
439 + char localhost[256];
440 + extern char *config_file;
442 + struct addrinfo aih, *ai = 0;
444 + if (!lp_load(config_file, 0)) {
445 + exit_cleanup(RERR_SYNTAX);
448 + n = lp_num_modules();
450 + if (0 == lp_slp_refresh())
451 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
452 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
453 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
454 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
455 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
457 + timeout = lp_slp_refresh();
459 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
460 + err = SLPOpen("en",SLP_FALSE,&hslp);
461 + if (err != SLP_OK) {
462 + rprintf(FINFO, "Error opening slp handle %i\n",err);
465 + if (gethostname(localhost, sizeof localhost)) {
466 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
469 + memset(&aih, 0, sizeof aih);
470 + aih.ai_family = PF_UNSPEC;
471 + aih.ai_flags = AI_CANONNAME;
472 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
473 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
476 + /* Register each service with SLP */
477 + for (i = 0; i < n; i++) {
481 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
485 + rprintf(FINFO, " %s\n", srv);
486 + if (lp_comment(i)) {
487 + snprintf(attr, sizeof attr, "(comment=%s)",
491 + srv, /* service to register */
493 + 0, /* this is ignored */
494 + attr, /* attributes */
495 + SLP_TRUE, /* new registration - don't change this */
496 + slp_callback, /* callback */
499 + /* err may contain an error code that occurred as the slp library
500 + * _prepared_ to make the call. */
501 + if (err != SLP_OK || callbackerr != SLP_OK)
502 + rprintf(FINFO, "Error registering service with slp %i\n", err);
504 + /* callbackerr may contain an error code (that was assigned through
505 + * the callback cookie) that occurred as slp packets were sent on
507 + if (callbackerr != SLP_OK)
508 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
511 + /* Now that we're done using slp, close the slp handle */
515 + /* refresh is done in main select loop */
518 diff --git a/usage.c b/usage.c
521 @@ -138,6 +138,11 @@ static void print_info_flags(enum logcode f)
532 #ifndef USE_ROLL_SIMD