Annotation of embedaddon/rsync/srvreg.c, revision 1.1.1.1

1.1       misho       1: /* -*- c-file-style: "linux"; -*-
                      2: 
                      3:    Copyright (C) 2002 by Brad Hards <bradh@frogmouth.net>
                      4: 
                      5:    This program is free software; you can redistribute it and/or modify
                      6:    it under the terms of the GNU General Public License as published by
                      7:    the Free Software Foundation; either version 2 of the License, or
                      8:    (at your option) any later version.
                      9: 
                     10:    This program is distributed in the hope that it will be useful,
                     11:    but WITHOUT ANY WARRANTY; without even the implied warranty of
                     12:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     13:    GNU General Public License for more details.
                     14: 
                     15:    You should have received a copy of the GNU General Public License
                     16:    along with this program; if not, write to the Free Software
                     17:    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     18: */
                     19: 
                     20: /* This file implements the service registration functionality */
                     21: 
                     22: /* Basically, it uses normal Service Location Protocol API */
                     23: 
                     24: #include "rsync.h"
                     25: #include "slp.h"
                     26: #include "netdb.h"
                     27: 
                     28: extern int rsync_port;
                     29: 
                     30: static void slp_callback(UNUSED(SLPHandle hslp), SLPError errcode, void *cookie)
                     31: {
                     32:        /* return the error code in the cookie */
                     33:        *(SLPError*)cookie = errcode;
                     34: 
                     35:        /* You could do something else here like print out
                     36:         * the errcode, etc.  Remember, as a general rule,
                     37:         * do not try to do too much in a callback because
                     38:         * it is being executed by the same thread that is
                     39:         * reading slp packets from the wire. */
                     40: }
                     41: 
                     42: int register_services(void)
                     43: {
                     44:        SLPError err, callbackerr;
                     45:        SLPHandle hslp;
                     46:        int n;
                     47:        int i;
                     48:        char srv[120];
                     49:        char attr[120];
                     50:        char localhost[256];
                     51:        extern char *config_file;
                     52:        short timeout;
                     53:        struct addrinfo aih, *ai = 0;
                     54: 
                     55:        if (!lp_load(config_file, 0)) {
                     56:                exit_cleanup(RERR_SYNTAX);
                     57:        }
                     58: 
                     59:        n = lp_num_modules();
                     60: 
                     61:        if (0 == lp_slp_refresh())
                     62:                timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
                     63:        else if (SLP_MIN_TIMEOUT > lp_slp_refresh())
                     64:                timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
                     65:        else if (SLP_LIFETIME_MAXIMUM <= lp_slp_refresh())
                     66:                timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
                     67:        else
                     68:                timeout = lp_slp_refresh();
                     69: 
                     70:        rprintf(FINFO, "rsyncd registering %d service%s with slpd for %d seconds:\n", n, ((n==1)? "":"s"), timeout);
                     71:        err = SLPOpen("en",SLP_FALSE,&hslp);
                     72:        if (err != SLP_OK) {
                     73:                rprintf(FINFO, "Error opening slp handle %i\n",err);
                     74:                return err;
                     75:        }
                     76:        if (gethostname(localhost, sizeof localhost)) {
                     77:               rprintf(FINFO, "Could not get hostname: %s\n", strerror(errno));
                     78:               return err;
                     79:        }
                     80:        memset(&aih, 0, sizeof aih);
                     81:        aih.ai_family = PF_UNSPEC;
                     82:        aih.ai_flags = AI_CANONNAME;
                     83:        if (0 != (err = getaddrinfo(localhost, 0, &aih, &ai)) || !ai) {
                     84:               rprintf(FINFO, "Could not resolve hostname: %s\n", gai_strerror(err));
                     85:               return err;
                     86:        }
                     87:        /* Register each service with SLP */
                     88:        for (i = 0; i < n; i++) {
                     89:                if (!lp_list(i))
                     90:                        continue;
                     91: 
                     92:                snprintf(srv, sizeof srv, "service:rsync://%s:%d/%s",
                     93:                         ai->ai_canonname,
                     94:                         rsync_port,
                     95:                         lp_name(i));
                     96:                rprintf(FINFO, "    %s\n", srv);
                     97:                if (lp_comment(i)) {
                     98:                        snprintf(attr, sizeof attr, "(comment=%s)",
                     99:                                 lp_comment(i));
                    100:                }
                    101:                err = SLPReg(hslp,
                    102:                             srv, /* service to register */
                    103:                             timeout,
                    104:                             0,  /* this is ignored */
                    105:                             attr, /* attributes */
                    106:                             SLP_TRUE, /* new registration - don't change this */
                    107:                             slp_callback, /* callback */
                    108:                             &callbackerr);
                    109: 
                    110:                /* err may contain an error code that occurred as the slp library
                    111:                 * _prepared_ to make the call. */
                    112:                if (err != SLP_OK || callbackerr != SLP_OK)
                    113:                        rprintf(FINFO, "Error registering service with slp %i\n", err);
                    114: 
                    115:                /* callbackerr may contain an error code (that was assigned through
                    116:                 * the callback cookie) that occurred as slp packets were sent on
                    117:                 * the wire. */
                    118:                if (callbackerr != SLP_OK)
                    119:                        rprintf(FINFO, "Error registering service with slp %i\n",callbackerr);
                    120:        }
                    121: 
                    122:        /* Now that we're done using slp, close the slp handle */
                    123:        freeaddrinfo(ai);
                    124:        SLPClose(hslp);
                    125: 
                    126:        /* refresh is done in main select loop */
                    127:        return 0;
                    128: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>