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: 974f49e22ac9f62fcfd750768d0835b900524578
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 @@ -1461,6 +1461,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 @@ -985,6 +985,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,6 +10,7 @@ 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
105 diff --git a/main.c b/main.c
108 @@ -1365,6 +1365,18 @@ static int start_client(int argc, char *argv[])
110 if (!read_batch) { /* for read_batch, NO source is specified */
111 char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
113 + if (shell_machine && !shell_machine[0]) {
115 + /* User entered just rsync:// URI */
116 + print_service_list();
118 +#else /* No SLP, die here */
119 + rprintf(FINFO, "No SLP support, cannot browse\n");
120 + exit_cleanup(RERR_SYNTAX);
124 if (path) { /* source is remote */
127 diff --git a/options.c b/options.c
130 @@ -676,6 +676,11 @@ static void print_info_flags(enum logcode f)
142 diff --git a/rsync.1.md b/rsync.1.md
145 @@ -151,6 +151,13 @@ rsync daemon by leaving off the module name:
147 See the following section for more 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.
158 The syntax for requesting multiple files from a remote host is done by
159 diff --git a/rsync.h b/rsync.h
163 #define SIGNIFICANT_ITEM_FLAGS (~(\
164 ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
166 +/* this is the minimum we'll use, irrespective of config setting */
167 +/* definitely don't set to less than about 30 seconds */
168 +#define SLP_MIN_TIMEOUT 120
170 #define CFN_KEEP_DOT_DIRS (1<<0)
171 #define CFN_KEEP_TRAILING_SLASH (1<<1)
172 #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
173 diff --git a/rsyncd.conf b/rsyncd.conf
179 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
180 --- a/rsyncd.conf.5.md
181 +++ b/rsyncd.conf.5.md
182 @@ -136,6 +136,16 @@ a literal % into a value is to use %%.
183 You can override the default backlog value when the daemon listens for
184 connections. It defaults to 5.
188 + This parameter is used to determine how long service advertisements are
189 + valid (measured in seconds), and is only applicable if you have Service
190 + Location Protocol support compiled in. If this is not set or is set to
191 + zero, then service advertisements never time out. If this is set to less
192 + than 120 seconds, then 120 seconds is used. If it is set to more than
193 + 65535, then 65535 is used (which is a limitation of SLP). Using 3600
194 + (one hour) is a good number if you tend to change your configuration.
198 After the global parameters you should define a number of modules, each module
199 diff --git a/socket.c b/socket.c
202 @@ -534,6 +534,16 @@ void start_accept_loop(int port, int (*fn)(int, int))
207 + time_t next_slp_refresh;
208 + short slp_timeout = lp_slp_refresh();
210 + if (slp_timeout < SLP_MIN_TIMEOUT)
211 + slp_timeout = SLP_MIN_TIMEOUT;
212 + /* re-register before slp times out */
217 #ifdef HAVE_SIGACTION
218 sigact.sa_flags = SA_NOCLDSTOP;
219 @@ -561,14 +571,25 @@ void start_accept_loop(int port, int (*fn)(int, int))
224 + next_slp_refresh = time(NULL) + slp_timeout;
227 /* now accept incoming connections - forking a new process
228 * for each incoming connection */
234 struct sockaddr_storage addr;
235 socklen_t addrlen = sizeof addr;
237 + struct timeval slp_tv;
239 + slp_tv.tv_sec = 10;
240 + slp_tv.tv_usec = 0;
243 /* close log file before the potentially very long select so
244 * file can be trimmed by another process instead of growing
245 @@ -581,7 +602,18 @@ void start_accept_loop(int port, int (*fn)(int, int))
249 - if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 1)
251 + sel_ret = select(maxfd + 1, &fds, NULL, NULL,
252 + slp_timeout ? &slp_tv : NULL);
253 + if (sel_ret == 0 && slp_timeout && time(NULL) > next_slp_refresh) {
254 + rprintf(FINFO, "Service registration expired, refreshing it\n");
255 + register_services();
256 + next_slp_refresh = time(NULL) + slp_timeout;
259 + sel_ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
264 for (i = 0, fd = -1; sp[i] >= 0; i++) {
265 diff --git a/srvloc.c b/srvloc.c
270 +/* -*- c-file-style: "linux"; -*-
272 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
274 + This program is free software; you can redistribute it and/or modify
275 + it under the terms of the GNU General Public License as published by
276 + the Free Software Foundation; either version 2 of the License, or
277 + (at your option) any later version.
279 + This program is distributed in the hope that it will be useful,
280 + but WITHOUT ANY WARRANTY; without even the implied warranty of
281 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
282 + GNU General Public License for more details.
284 + You should have received a copy of the GNU General Public License
285 + along with this program; if not, write to the Free Software
286 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
289 +/* This file implements the service location functionality */
290 +/* Basically, it uses normal Service Location Protocol API */
292 +/* It is really a cheap hack - just to show how it might work
293 + in a real application.
302 +/* This one just prints out the attributes */
303 +static SLPBoolean getAttrCallback(UNUSED(SLPHandle hslp), const char *attrlist,
304 + SLPError errcode, UNUSED(void *cookie))
308 + if (errcode == SLP_OK) {
309 + if (!strcmp(attrlist, "(comment=)"))
310 + rprintf(FINFO, "\t(No description)\n");
312 + cleanstr = strrchr(attrlist, ')') ;
313 + *cleanstr = ' '; /* remove last ')' */
314 + rprintf(FINFO, "\t%s\n", strchr(attrlist, '=') + 1);
320 +static SLPBoolean getSLPSrvURLCallback(UNUSED(SLPHandle hslp),
321 + const char *srvurl, UNUSED(unsigned short lifetime),
322 + SLPError errcode, void *cookie)
325 + SLPHandle attrhslp;
327 + if (errcode == SLP_OK) {
328 + /* chop service: off the front */
329 + rprintf(FINFO, " %s ", (strchr(srvurl, ':') + 1));
330 + /* check for any attributes */
331 + if (SLPOpen("en", SLP_FALSE,&attrhslp) == SLP_OK) {
332 + result = SLPFindAttrs(attrhslp, srvurl,
333 + "", /* return all attributes */
334 + "", /* use configured scopes */
335 + getAttrCallback, NULL);
336 + if (result != SLP_OK) {
337 + rprintf(FERROR, "errorcode: %i\n",result);
339 + SLPClose(attrhslp);
341 + *(SLPError*)cookie = SLP_OK;
343 + *(SLPError*)cookie = errcode;
345 + /* Return SLP_TRUE because we want to be called again
346 + * if more services were found. */
351 +int print_service_list(void)
354 + SLPError callbackerr;
357 + err = SLPOpen("en",SLP_FALSE,&hslp);
358 + if (err != SLP_OK) {
359 + rprintf(FERROR, "Error opening slp handle %i\n", err);
363 + SLPFindSrvs(hslp, "rsync",
364 + 0, /* use configured scopes */
365 + 0, /* no attr filter */
366 + getSLPSrvURLCallback, &callbackerr);
368 + /* Now that we're done using slp, close the slp handle */
373 diff --git a/srvreg.c b/srvreg.c
378 +/* -*- c-file-style: "linux"; -*-
380 + Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
382 + This program is free software; you can redistribute it and/or modify
383 + it under the terms of the GNU General Public License as published by
384 + the Free Software Foundation; either version 2 of the License, or
385 + (at your option) any later version.
387 + This program is distributed in the hope that it will be useful,
388 + but WITHOUT ANY WARRANTY; without even the implied warranty of
389 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
390 + GNU General Public License for more details.
392 + You should have received a copy of the GNU General Public License
393 + along with this program; if not, write to the Free Software
394 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
397 +/* This file implements the service registration functionality */
399 +/* Basically, it uses normal Service Location Protocol API */
405 +extern int rsync_port;
407 +static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
409 + /* return the error code in the cookie */
410 + *(SLPError*)cookie = errcode;
412 + /* You could do something else here like print out
413 + * the errcode, etc. Remember, as a general rule,
414 + * do not try to do too much in a callback because
415 + * it is being executed by the same thread that is
416 + * reading slp packets from the wire. */
419 +int register_services(void)
421 + SLPError err, callbackerr;
427 + char localhost[256];
428 + extern char *config_file;
430 + struct addrinfo aih, *ai = 0;
432 + if (!lp_load(config_file, 0)) {
433 + exit_cleanup(RERR_SYNTAX);
436 + n = lp_num_modules();
438 + if (0 == lp_slp_refresh())
439 + timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
440 + else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
441 + timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
442 + else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
443 + timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
445 + timeout = lp_slp_refresh();
447 + rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
448 + err = SLPOpen("en",SLP_FALSE,&hslp);
449 + if (err != SLP_OK) {
450 + rprintf(FINFO, "Error opening slp handle %i\n",err);
453 + if (gethostname(localhost, sizeof localhost)) {
454 + rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
457 + memset(&aih, 0, sizeof aih);
458 + aih.ai_family = PF_UNSPEC;
459 + aih.ai_flags = AI_CANONNAME;
460 + if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
461 + rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
464 + /* Register each service with SLP */
465 + for (i = 0; i < n; i++) {
469 + snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
473 + rprintf(FINFO, " %s\n", srv);
474 + if (lp_comment(i)) {
475 + snprintf(attr, sizeof attr, "(comment=%s)",
479 + srv, /* service to register */
481 + 0, /* this is ignored */
482 + attr, /* attributes */
483 + SLP_TRUE, /* new registration - don't change this */
484 + slp_callback, /* callback */
487 + /* err may contain an error code that occurred as the slp library
488 + * _prepared_ to make the call. */
489 + if (err != SLP_OK || callbackerr != SLP_OK)
490 + rprintf(FINFO, "Error registering service with slp %i\n", err);
492 + /* callbackerr may contain an error code (that was assigned through
493 + * the callback cookie) that occurred as slp packets were sent on
495 + if (callbackerr != SLP_OK)
496 + rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
499 + /* Now that we're done using slp, close the slp handle */
503 + /* refresh is done in main select loop */