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: 8695bcc2b140b2518254234659a03b96f04055fc
14 diff --git a/Makefile.in b/Makefile.in
17 @@ -16,6 +16,8 @@ CXX=@CXX@
26 @@ -45,7 +47,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 @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) @SIMD@ $(DAEMON_OBJ) $(LIBOBJ) @BUILD_ZLIB@ @BUILD_POPT@
35 @@ -94,7 +96,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 @@ -1456,6 +1456,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 @@ -994,6 +994,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 #include <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 @@ -1405,6 +1405,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/options.c b/options.c
137 @@ -681,6 +681,11 @@ static void print_info_flags(enum logcode f)
149 diff --git a/rsync.1.md b/rsync.1.md
152 @@ -149,7 +149,19 @@ rsync daemon by leaving off the module name:
154 > rsync somehost.mydomain.com::
156 -See the following section for more details.
157 +And, if Service Location Protocol is available, the following will list the
158 +available rsync servers:
162 +See the following section for even more usage details.
164 +One more thing, if Service Location Protocol is available, the following will
165 +list the available rsync servers:
169 +See the following section for even more usage details.
173 diff --git a/rsync.h b/rsync.h
177 #define SIGNIFICANT_ITEM_FLAGS (~(\
178 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
180 +/* this is the minimum we'll use, irrespective of config setting */
181 +/* definitely don't set to less than about 30 seconds */
182 +#define SLP_MIN_TIMEOUT 120
184 #define CFN_KEEP_DOT_DIRS (1<<0)
185 #define CFN_KEEP_TRAILING_SLASH (1<<1)
186 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
187 diff --git a/rsyncd.conf b/rsyncd.conf
193 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
194 --- a/rsyncd.conf.5.md
195 +++ b/rsyncd.conf.5.md
196 @@ -136,6 +136,21 @@ a literal % into a value is to use %%.
197 You can override the default backlog value when the daemon listens for
198 connections. It defaults to 5.
202 + You can enable Service Location Protocol support by enabling this global
203 + parameter. The default is "false".
207 + This parameter is used to determine how long service advertisements are
208 + valid (measured in seconds), and is only applicable if you have Service
209 + Location Protocol support compiled in. If this is not set or is set to
210 + zero, then service advertisements never time out. If this is set to less
211 + than 120 seconds, then 120 seconds is used. If it is set to more than
212 + 65535, then 65535 is used (which is a limitation of SLP). Using 3600
213 + (one hour) is a good number if you tend to change your configuration.
217 After the global parameters you should define a number of modules, each module
218 @@ -1168,6 +1183,7 @@ A more sophisticated example would be:
219 > max connections = 4
220 > syslog facility = local5
221 > pid file = /var/run/rsyncd.pid
222 +> slp refresh = 3600
225 > path = /var/ftp/./pub
226 diff --git a/socket.c b/socket.c
229 @@ -534,6 +534,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
234 + time_t next_slp_refresh;
235 + short slp_timeout = lp_use_slp() ? lp_slp_refresh() : 0;
237 + if (slp_timeout < SLP_MIN_TIMEOUT)
238 + slp_timeout = SLP_MIN_TIMEOUT;
239 + /* re-register before slp times out */
244 #ifdef HAVE_SIGACTION
245 sigact.sa_flags = SA_NOCLDSTOP;
246 @@ -561,14 +571,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
251 + next_slp_refresh = time(NULL) + slp_timeout;
254 /* now accept incoming connections - forking a new process
255 * for each incoming connection */
261 struct sockaddr_storage addr;
262 socklen_t addrlen = sizeof addr;
264 + struct timeval slp_tv;
266 + slp_tv.tv_sec = 10;
267 + slp_tv.tv_usec = 0;
270 /* close log file before the potentially very long select so
271 * file can be trimmed by another process instead of growing
272 @@ -581,7 +602,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
276 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
278 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
279 + slp_timeout ? &slp_tv : NULL);
280 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
281 + rprintf(FINFO, "Service registration expired, refreshing it\n");
282 + register_services();
283 + next_slp_refresh = time(NULL) + slp_timeout;
286 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
291 for (i = 0, fd = -1; sp[i] >= 0; i++) {
292 diff --git a/srvloc.c b/srvloc.c
297 +/* -*- c-file-style: "linux"; -*-
299 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
301 + This program is free software; you can redistribute it and/or modify
302 + it under the terms of the GNU General Public License as published by
303 + the Free Software Foundation; either version 2 of the License, or
304 + (at your option) any later version.
306 + This program is distributed in the hope that it will be useful,
307 + but WITHOUT ANY WARRANTY; without even the implied warranty of
308 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
309 + GNU General Public License for more details.
311 + You should have received a copy of the GNU General Public License
312 + along with this program; if not, write to the Free Software
313 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
316 +/* This file implements the service location functionality */
317 +/* Basically, it uses normal Service Location Protocol API */
319 +/* It is really a cheap hack - just to show how it might work
320 + in a real application.
329 +/* This one just prints out the attributes */
330 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
331 + SLPError errcode, UNUSED(void *cookie))
335 + if (errcode == SLP_OK) {
336 + if (!strcmp(attrlist, "(comment=)"))
337 + rprintf(FINFO, "\t(No description)\n");
339 + cleanstr = strrchr(attrlist, ')') ;
340 + *cleanstr = ' '; /* remove last ')' */
341 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
347 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
348 + const char *srvurl, UNUSED(unsigned short lifetime),
349 + SLPError errcode, void *cookie)
352 + SLPHandle attrhslp;
354 + if (errcode == SLP_OK) {
355 + /* chop service: off the front */
356 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
357 + /* check for any attributes */
358 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
359 + result = SLPFindAttrs(attrhslp, srvurl,
360 + "", /* return all attributes */
361 + "", /* use configured scopes */
362 + getAttrCallback, NULL);
363 + if (result != SLP_OK) {
364 + rprintf(FERROR, "errorcode: %i\n",result);
366 + SLPClose(attrhslp);
368 + *(SLPError*)cookie = SLP_OK;
370 + *(SLPError*)cookie = errcode;
372 + /* Return SLP_TRUE because we want to be called again
373 + * if more services were found. */
378 +int print_service_list(void)
381 + SLPError callbackerr;
384 + err = SLPOpen("en",SLP_FALSE,&hslp);
385 + if (err != SLP_OK) {
386 + rprintf(FERROR, "Error opening slp handle %i\n", err);
390 + SLPFindSrvs(hslp, "rsync",
391 + 0, /* use configured scopes */
392 + 0, /* no attr filter */
393 + getSLPSrvURLCallback, &callbackerr);
395 + /* Now that we're done using slp, close the slp handle */
400 diff --git a/srvreg.c b/srvreg.c
405 +/* -*- c-file-style: "linux"; -*-
407 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
409 + This program is free software; you can redistribute it and/or modify
410 + it under the terms of the GNU General Public License as published by
411 + the Free Software Foundation; either version 2 of the License, or
412 + (at your option) any later version.
414 + This program is distributed in the hope that it will be useful,
415 + but WITHOUT ANY WARRANTY; without even the implied warranty of
416 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
417 + GNU General Public License for more details.
419 + You should have received a copy of the GNU General Public License
420 + along with this program; if not, write to the Free Software
421 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
424 +/* This file implements the service registration functionality */
426 +/* Basically, it uses normal Service Location Protocol API */
432 +extern int rsync_port;
434 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
436 + /* return the error code in the cookie */
437 + *(SLPError*)cookie = errcode;
439 + /* You could do something else here like print out
440 + * the errcode, etc. Remember, as a general rule,
441 + * do not try to do too much in a callback because
442 + * it is being executed by the same thread that is
443 + * reading slp packets from the wire. */
446 +int register_services(void)
448 + SLPError err, callbackerr;
454 + char localhost[256];
455 + extern char *config_file;
457 + struct addrinfo aih, *ai = 0;
459 + if (!lp_load(config_file, 0)) {
460 + exit_cleanup(RERR_SYNTAX);
463 + n = lp_num_modules();
465 + if (0 == lp_slp_refresh())
466 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
467 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
468 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
469 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
470 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
472 + timeout = lp_slp_refresh();
474 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
475 + err = SLPOpen("en",SLP_FALSE,&hslp);
476 + if (err != SLP_OK) {
477 + rprintf(FINFO, "Error opening slp handle %i\n",err);
480 + if (gethostname(localhost, sizeof localhost)) {
481 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
484 + memset(&aih, 0, sizeof aih);
485 + aih.ai_family = PF_UNSPEC;
486 + aih.ai_flags = AI_CANONNAME;
487 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
488 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
491 + /* Register each service with SLP */
492 + for (i = 0; i < n; i++) {
496 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
500 + rprintf(FINFO, " %s\n", srv);
501 + if (lp_comment(i)) {
502 + snprintf(attr, sizeof attr, "(comment=%s)",
506 + srv, /* service to register */
508 + 0, /* this is ignored */
509 + attr, /* attributes */
510 + SLP_TRUE, /* new registration - don't change this */
511 + slp_callback, /* callback */
514 + /* err may contain an error code that occurred as the slp library
515 + * _prepared_ to make the call. */
516 + if (err != SLP_OK || callbackerr != SLP_OK)
517 + rprintf(FINFO, "Error registering service with slp %i\n", err);
519 + /* callbackerr may contain an error code (that was assigned through
520 + * the callback cookie) that occurred as slp packets were sent on
522 + if (callbackerr != SLP_OK)
523 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
526 + /* Now that we're done using slp, close the slp handle */
530 + /* refresh is done in main select loop */