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: d4fc18f3755987ccea6e7f43609f862aa0254c5a
14 diff --git a/Makefile.in b/Makefile.in
17 @@ -16,6 +16,8 @@ CXX=@CXX@
26 @@ -46,7 +48,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) @SIMD@ $(DAEMON_OBJ) $(LIBOBJ) @BUILD_ZLIB@ @BUILD_POPT@
35 @@ -95,7 +97,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 @@ -1353,6 +1353,13 @@ int daemon_main(void)
48 * address too. In fact, why not just do getnameinfo on the
52 + if (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 @@ -833,6 +833,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/loadparm.c b/loadparm.c
97 @@ -111,6 +111,9 @@ typedef struct {
107 @@ -406,6 +409,9 @@ static struct parm_struct parm_table[] =
108 {"motd file", P_STRING, P_GLOBAL,&Vars.g.motd_file, NULL,0},
109 {"pid file", P_STRING, P_GLOBAL,&Vars.g.pid_file, NULL,0},
110 {"port", P_INTEGER,P_GLOBAL,&Vars.g.rsync_port, NULL,0},
112 + {"slp refresh", P_INTEGER,P_GLOBAL,&Vars.g.slp_refresh, NULL,0},
114 {"socket options", P_STRING, P_GLOBAL,&Vars.g.socket_options, NULL,0},
115 {"haproxy header", P_BOOL, P_LOCAL, &Vars.g.haproxy_header, NULL,0},
117 @@ -548,6 +554,9 @@ FN_GLOBAL_STRING(lp_socket_options, socket_options)
119 FN_GLOBAL_INTEGER(lp_listen_backlog, listen_backlog)
120 FN_GLOBAL_INTEGER(lp_rsync_port, rsync_port)
122 +FN_GLOBAL_INTEGER(lp_slp_refresh, slp_refresh)
125 FN_GLOBAL_BOOL(lp_haproxy_header, haproxy_header)
127 diff --git a/main.c b/main.c
130 @@ -1372,6 +1372,18 @@ static int start_client(int argc, char *argv[])
132 if (!read_batch) { /* for read_batch, NO source is specified */
133 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
135 + if (shell_machine && !shell_machine[0]) {
137 + /* User entered just rsync:// URI */
138 + print_service_list();
140 +#else /* No SLP, die here */
141 + rprintf(FINFO, "No SLP support, cannot browse\n");
142 + exit_cleanup(RERR_SYNTAX);
146 if (path) { /* source is remote */
149 diff --git a/options.c b/options.c
152 @@ -647,6 +647,11 @@ static void print_capabilities(enum logcode f)
164 diff --git a/rsync.1.md b/rsync.1.md
167 @@ -151,6 +151,13 @@ rsync daemon by leaving off the module name:
169 See the following section for more details.
171 +One more thing, if Service Location Protocol is available, the following will
172 +list the available rsync servers:
176 +See the following section for even more usage details.
180 The syntax for requesting multiple files from a remote host is done by
181 diff --git a/rsync.h b/rsync.h
185 #define SIGNIFICANT_ITEM_FLAGS (~(\
186 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
188 +/* this is the minimum we'll use, irrespective of config setting */
189 +/* definitely don't set to less than about 30 seconds */
190 +#define SLP_MIN_TIMEOUT 120
192 #define CFN_KEEP_DOT_DIRS (1<<0)
193 #define CFN_KEEP_TRAILING_SLASH (1<<1)
194 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
195 diff --git a/rsyncd.conf b/rsyncd.conf
201 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
202 --- a/rsyncd.conf.5.md
203 +++ b/rsyncd.conf.5.md
204 @@ -136,6 +136,16 @@ a literal % into a value is to use %%.
205 You can override the default backlog value when the daemon listens for
206 connections. It defaults to 5.
210 + This parameter is used to determine how long service advertisements are
211 + valid (measured in seconds), and is only applicable if you have Service
212 + Location Protocol support compiled in. If this is not set or is set to
213 + zero, then service advertisements never time out. If this is set to less
214 + than 120 seconds, then 120 seconds is used. If it is set to more than
215 + 65535, then 65535 is used (which is a limitation of SLP). Using 3600
216 + (one hour) is a good number if you tend to change your configuration.
220 After the global parameters you should define a number of modules, each module
221 diff --git a/socket.c b/socket.c
224 @@ -544,6 +544,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
229 + time_t next_slp_refresh;
230 + short slp_timeout = lp_slp_refresh();
232 + if (slp_timeout < SLP_MIN_TIMEOUT)
233 + slp_timeout = SLP_MIN_TIMEOUT;
234 + /* re-register before slp times out */
239 #ifdef HAVE_SIGACTION
240 sigact.sa_flags = SA_NOCLDSTOP;
241 @@ -572,14 +582,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
246 + next_slp_refresh = time(NULL) + slp_timeout;
249 /* now accept incoming connections - forking a new process
250 * for each incoming connection */
256 struct sockaddr_storage addr;
257 socklen_t addrlen = sizeof addr;
259 + struct timeval slp_tv;
261 + slp_tv.tv_sec = 10;
262 + slp_tv.tv_usec = 0;
265 /* close log file before the potentially very long select so
266 * file can be trimmed by another process instead of growing
267 @@ -592,7 +613,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
271 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
273 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
274 + slp_timeout ? &slp_tv : NULL);
275 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
276 + rprintf(FINFO, "Service registration expired, refreshing it\n");
277 + register_services();
278 + next_slp_refresh = time(NULL) + slp_timeout;
281 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
286 for (i = 0, fd = -1; sp[i] >= 0; i++) {
287 diff --git a/srvloc.c b/srvloc.c
292 +/* -*- c-file-style: "linux"; -*-
294 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
296 + This program is free software; you can redistribute it and/or modify
297 + it under the terms of the GNU General Public License as published by
298 + the Free Software Foundation; either version 2 of the License, or
299 + (at your option) any later version.
301 + This program is distributed in the hope that it will be useful,
302 + but WITHOUT ANY WARRANTY; without even the implied warranty of
303 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 + GNU General Public License for more details.
306 + You should have received a copy of the GNU General Public License
307 + along with this program; if not, write to the Free Software
308 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
311 +/* This file implements the service location functionality */
312 +/* Basically, it uses normal Service Location Protocol API */
314 +/* It is really a cheap hack - just to show how it might work
315 + in a real application.
324 +/* This one just prints out the attributes */
325 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
326 + SLPError errcode, UNUSED(void *cookie))
330 + if (errcode == SLP_OK) {
331 + if (!strcmp(attrlist, "(comment=)"))
332 + rprintf(FINFO, "\t(No description)\n");
334 + cleanstr = strrchr(attrlist, ')') ;
335 + *cleanstr = ' '; /* remove last ')' */
336 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
342 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
343 + const char *srvurl, UNUSED(unsigned short lifetime),
344 + SLPError errcode, void *cookie)
347 + SLPHandle attrhslp;
349 + if (errcode == SLP_OK) {
350 + /* chop service: off the front */
351 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
352 + /* check for any attributes */
353 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
354 + result = SLPFindAttrs(attrhslp, srvurl,
355 + "", /* return all attributes */
356 + "", /* use configured scopes */
357 + getAttrCallback, NULL);
358 + if (result != SLP_OK) {
359 + rprintf(FERROR, "errorcode: %i\n",result);
361 + SLPClose(attrhslp);
363 + *(SLPError*)cookie = SLP_OK;
365 + *(SLPError*)cookie = errcode;
367 + /* Return SLP_TRUE because we want to be called again
368 + * if more services were found. */
373 +int print_service_list(void)
376 + SLPError callbackerr;
379 + err = SLPOpen("en",SLP_FALSE,&hslp);
380 + if (err != SLP_OK) {
381 + rprintf(FERROR, "Error opening slp handle %i\n", err);
385 + SLPFindSrvs(hslp, "rsync",
386 + 0, /* use configured scopes */
387 + 0, /* no attr filter */
388 + getSLPSrvURLCallback, &callbackerr);
390 + /* Now that we're done using slp, close the slp handle */
395 diff --git a/srvreg.c b/srvreg.c
400 +/* -*- c-file-style: "linux"; -*-
402 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
404 + This program is free software; you can redistribute it and/or modify
405 + it under the terms of the GNU General Public License as published by
406 + the Free Software Foundation; either version 2 of the License, or
407 + (at your option) any later version.
409 + This program is distributed in the hope that it will be useful,
410 + but WITHOUT ANY WARRANTY; without even the implied warranty of
411 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
412 + GNU General Public License for more details.
414 + You should have received a copy of the GNU General Public License
415 + along with this program; if not, write to the Free Software
416 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
419 +/* This file implements the service registration functionality */
421 +/* Basically, it uses normal Service Location Protocol API */
427 +extern int rsync_port;
429 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
431 + /* return the error code in the cookie */
432 + *(SLPError*)cookie = errcode;
434 + /* You could do something else here like print out
435 + * the errcode, etc. Remember, as a general rule,
436 + * do not try to do too much in a callback because
437 + * it is being executed by the same thread that is
438 + * reading slp packets from the wire. */
441 +int register_services(void)
443 + SLPError err, callbackerr;
449 + char localhost[256];
450 + extern char *config_file;
452 + struct addrinfo aih, *ai = 0;
454 + if (!lp_load(config_file, 0)) {
455 + exit_cleanup(RERR_SYNTAX);
458 + n = lp_num_modules();
460 + if (0 == lp_slp_refresh())
461 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
462 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
463 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
464 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
465 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
467 + timeout = lp_slp_refresh();
469 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
470 + err = SLPOpen("en",SLP_FALSE,&hslp);
471 + if (err != SLP_OK) {
472 + rprintf(FINFO, "Error opening slp handle %i\n",err);
475 + if (gethostname(localhost, sizeof localhost)) {
476 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
479 + memset(&aih, 0, sizeof aih);
480 + aih.ai_family = PF_UNSPEC;
481 + aih.ai_flags = AI_CANONNAME;
482 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
483 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
486 + /* Register each service with SLP */
487 + for (i = 0; i < n; i++) {
491 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
495 + rprintf(FINFO, " %s\n", srv);
496 + if (lp_comment(i)) {
497 + snprintf(attr, sizeof attr, "(comment=%s)",
501 + srv, /* service to register */
503 + 0, /* this is ignored */
504 + attr, /* attributes */
505 + SLP_TRUE, /* new registration - don't change this */
506 + slp_callback, /* callback */
509 + /* err may contain an error code that occurred as the slp library
510 + * _prepared_ to make the call. */
511 + if (err != SLP_OK || callbackerr != SLP_OK)
512 + rprintf(FINFO, "Error registering service with slp %i\n", err);
514 + /* callbackerr may contain an error code (that was assigned through
515 + * the callback cookie) that occurred as slp packets were sent on
517 + if (callbackerr != SLP_OK)
518 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
521 + /* Now that we're done using slp, close the slp handle */
525 + /* refresh is done in main select loop */