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: 6c8ca91c731b7bf2b081694bda85b7dadc2b7aff
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 @@ -103,7 +105,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 @@ -1533,6 +1533,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 @@ -1047,6 +1047,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 @@ -1400,6 +1400,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,6 +152,18 @@ rsync daemon by leaving off the module name:
139 > rsync somehost.mydomain.com::
141 +And, if Service Location Protocol is available, the following will list the
142 +available rsync servers:
146 +See the following section for even more usage details.
148 +One more thing, if Service Location Protocol is available, the following will
149 +list the available rsync servers:
153 ## COPYING TO A DIFFERENT NAME
155 When you want to copy a directory to a different name, use a trailing slash on
156 diff --git a/rsync.h b/rsync.h
160 #define SIGNIFICANT_ITEM_FLAGS (~(\
161 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
163 +/* this is the minimum we'll use, irrespective of config setting */
164 +/* definitely don't set to less than about 30 seconds */
165 +#define SLP_MIN_TIMEOUT 120
167 #define CFN_KEEP_DOT_DIRS (1<<0)
168 #define CFN_KEEP_TRAILING_SLASH (1<<1)
169 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
170 diff --git a/rsyncd.conf b/rsyncd.conf
176 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
177 --- a/rsyncd.conf.5.md
178 +++ b/rsyncd.conf.5.md
179 @@ -120,6 +120,21 @@ parameters:
180 You can override the default backlog value when the daemon listens for
181 connections. It defaults to 5.
185 + You can enable Service Location Protocol support by enabling this global
186 + parameter. The default is "false".
190 + This parameter is used to determine how long service advertisements are
191 + valid (measured in seconds), and is only applicable if you have Service
192 + Location Protocol support compiled in. If this is not set or is set to
193 + zero, then service advertisements never time out. If this is set to less
194 + than 120 seconds, then 120 seconds is used. If it is set to more than
195 + 65535, then 65535 is used (which is a limitation of SLP). Using 3600
196 + (one hour) is a good number if you tend to change your configuration.
198 You may also include any [MODULE PARAMETERS](#) in the global part of the
199 config file, in which case the supplied value will override the default for
201 @@ -1208,6 +1223,7 @@ A more sophisticated example would be:
202 > max connections = 4
203 > syslog facility = local5
204 > pid file = /var/run/rsyncd.pid
205 +> slp refresh = 3600
208 > path = /var/ftp/./pub
209 diff --git a/socket.c b/socket.c
212 @@ -534,6 +534,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
217 + time_t next_slp_refresh;
218 + short slp_timeout = lp_use_slp() ? lp_slp_refresh() : 0;
220 + if (slp_timeout < SLP_MIN_TIMEOUT)
221 + slp_timeout = SLP_MIN_TIMEOUT;
222 + /* re-register before slp times out */
227 #ifdef HAVE_SIGACTION
228 sigact.sa_flags = SA_NOCLDSTOP;
229 @@ -561,14 +571,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
234 + next_slp_refresh = time(NULL) + slp_timeout;
237 /* now accept incoming connections - forking a new process
238 * for each incoming connection */
244 struct sockaddr_storage addr;
245 socklen_t addrlen = sizeof addr;
247 + struct timeval slp_tv;
249 + slp_tv.tv_sec = 10;
250 + slp_tv.tv_usec = 0;
253 /* close log file before the potentially very long select so
254 * file can be trimmed by another process instead of growing
255 @@ -581,7 +602,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
259 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
261 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
262 + slp_timeout ? &slp_tv : NULL);
263 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
264 + rprintf(FINFO, "Service registration expired, refreshing it\n");
265 + register_services();
266 + next_slp_refresh = time(NULL) + slp_timeout;
269 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
274 for (i = 0, fd = -1; sp[i] >= 0; i++) {
275 diff --git a/srvloc.c b/srvloc.c
280 +/* -*- c-file-style: "linux"; -*-
282 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
284 + This program is free software; you can redistribute it and/or modify
285 + it under the terms of the GNU General Public License as published by
286 + the Free Software Foundation; either version 2 of the License, or
287 + (at your option) any later version.
289 + This program is distributed in the hope that it will be useful,
290 + but WITHOUT ANY WARRANTY; without even the implied warranty of
291 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
292 + GNU General Public License for more details.
294 + You should have received a copy of the GNU General Public License
295 + along with this program; if not, write to the Free Software
296 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
299 +/* This file implements the service location functionality */
300 +/* Basically, it uses normal Service Location Protocol API */
302 +/* It is really a cheap hack - just to show how it might work
303 + in a real application.
312 +/* This one just prints out the attributes */
313 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
314 + SLPError errcode, UNUSED(void *cookie))
318 + if (errcode == SLP_OK) {
319 + if (!strcmp(attrlist, "(comment=)"))
320 + rprintf(FINFO, "\t(No description)\n");
322 + cleanstr = strrchr(attrlist, ')') ;
323 + *cleanstr = ' '; /* remove last ')' */
324 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
330 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
331 + const char *srvurl, UNUSED(unsigned short lifetime),
332 + SLPError errcode, void *cookie)
335 + SLPHandle attrhslp;
337 + if (errcode == SLP_OK) {
338 + /* chop service: off the front */
339 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
340 + /* check for any attributes */
341 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
342 + result = SLPFindAttrs(attrhslp, srvurl,
343 + "", /* return all attributes */
344 + "", /* use configured scopes */
345 + getAttrCallback, NULL);
346 + if (result != SLP_OK) {
347 + rprintf(FERROR, "errorcode: %i\n",result);
349 + SLPClose(attrhslp);
351 + *(SLPError*)cookie = SLP_OK;
353 + *(SLPError*)cookie = errcode;
355 + /* Return SLP_TRUE because we want to be called again
356 + * if more services were found. */
361 +int print_service_list(void)
364 + SLPError callbackerr;
367 + err = SLPOpen("en",SLP_FALSE,&hslp);
368 + if (err != SLP_OK) {
369 + rprintf(FERROR, "Error opening slp handle %i\n", err);
373 + SLPFindSrvs(hslp, "rsync",
374 + 0, /* use configured scopes */
375 + 0, /* no attr filter */
376 + getSLPSrvURLCallback, &callbackerr);
378 + /* Now that we're done using slp, close the slp handle */
383 diff --git a/srvreg.c b/srvreg.c
388 +/* -*- c-file-style: "linux"; -*-
390 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
392 + This program is free software; you can redistribute it and/or modify
393 + it under the terms of the GNU General Public License as published by
394 + the Free Software Foundation; either version 2 of the License, or
395 + (at your option) any later version.
397 + This program is distributed in the hope that it will be useful,
398 + but WITHOUT ANY WARRANTY; without even the implied warranty of
399 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
400 + GNU General Public License for more details.
402 + You should have received a copy of the GNU General Public License
403 + along with this program; if not, write to the Free Software
404 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
407 +/* This file implements the service registration functionality */
409 +/* Basically, it uses normal Service Location Protocol API */
415 +extern int rsync_port;
417 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
419 + /* return the error code in the cookie */
420 + *(SLPError*)cookie = errcode;
422 + /* You could do something else here like print out
423 + * the errcode, etc. Remember, as a general rule,
424 + * do not try to do too much in a callback because
425 + * it is being executed by the same thread that is
426 + * reading slp packets from the wire. */
429 +int register_services(void)
431 + SLPError err, callbackerr;
437 + char localhost[256];
438 + extern char *config_file;
440 + struct addrinfo aih, *ai = 0;
442 + if (!lp_load(config_file, 0)) {
443 + exit_cleanup(RERR_SYNTAX);
446 + n = lp_num_modules();
448 + if (0 == lp_slp_refresh())
449 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
450 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
451 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
452 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
453 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
455 + timeout = lp_slp_refresh();
457 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
458 + err = SLPOpen("en",SLP_FALSE,&hslp);
459 + if (err != SLP_OK) {
460 + rprintf(FINFO, "Error opening slp handle %i\n",err);
463 + if (gethostname(localhost, sizeof localhost)) {
464 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
467 + memset(&aih, 0, sizeof aih);
468 + aih.ai_family = PF_UNSPEC;
469 + aih.ai_flags = AI_CANONNAME;
470 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
471 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
474 + /* Register each service with SLP */
475 + for (i = 0; i < n; i++) {
479 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
483 + rprintf(FINFO, " %s\n", srv);
484 + if (lp_comment(i)) {
485 + snprintf(attr, sizeof attr, "(comment=%s)",
489 + srv, /* service to register */
491 + 0, /* this is ignored */
492 + attr, /* attributes */
493 + SLP_TRUE, /* new registration - don't change this */
494 + slp_callback, /* callback */
497 + /* err may contain an error code that occurred as the slp library
498 + * _prepared_ to make the call. */
499 + if (err != SLP_OK || callbackerr != SLP_OK)
500 + rprintf(FINFO, "Error registering service with slp %i\n", err);
502 + /* callbackerr may contain an error code (that was assigned through
503 + * the callback cookie) that occurred as slp packets were sent on
505 + if (callbackerr != SLP_OK)
506 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
509 + /* Now that we're done using slp, close the slp handle */
513 + /* refresh is done in main select loop */
516 diff --git a/usage.c b/usage.c
519 @@ -138,6 +138,11 @@ static void print_info_flags(enum logcode f)
530 #ifndef USE_ROLL_SIMD