Back to index

glibc  2.9
nis_findserv.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <string.h>
00021 #include <time.h>
00022 #include <unistd.h>
00023 #include <sys/ioctl.h>
00024 #include <sys/socket.h>
00025 #include <rpc/pmap_prot.h>
00026 #include <rpc/pmap_clnt.h>
00027 #include <rpcsvc/nis.h>
00028 
00029 #include "nis_intern.h"
00030 
00031 /* Private data kept per client handle, from sunrpc/clnt_udp.c */
00032 struct cu_data
00033   {
00034     int cu_sock;
00035     bool_t cu_closeit;
00036     struct sockaddr_in cu_raddr;
00037     int cu_rlen;
00038     struct timeval cu_wait;
00039     struct timeval cu_total;
00040     struct rpc_err cu_error;
00041     XDR cu_outxdrs;
00042     u_int cu_xdrpos;
00043     u_int cu_sendsz;
00044     char *cu_outbuf;
00045     u_int cu_recvsz;
00046     char cu_inbuf[1];
00047   };
00048 
00049 
00050 /* The following is the original routine from sunrpc/pm_getport.c.
00051    The only change is the much shorter timeout. */
00052 /*
00053  * pmap_getport.c
00054  * Client interface to pmap rpc service.
00055  *
00056  * Copyright (C) 1984, Sun Microsystems, Inc.
00057  */
00058 
00059 /*
00060  * Find the mapped port for program,version.
00061  * Calls the pmap service remotely to do the lookup.
00062  * Returns 0 if no map exists.
00063  */
00064 u_short
00065 __pmap_getnisport (struct sockaddr_in *address, u_long program,
00066                  u_long version, u_int protocol)
00067 {
00068   const struct timeval timeout = {1, 0};
00069   const struct timeval tottimeout = {1, 0};
00070   u_short port = 0;
00071   int socket = -1;
00072   CLIENT *client;
00073   struct pmap parms;
00074 
00075   address->sin_port = htons (PMAPPORT);
00076   client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout, &socket,
00077                            RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
00078   if (client != (CLIENT *) NULL)
00079     {
00080       parms.pm_prog = program;
00081       parms.pm_vers = version;
00082       parms.pm_prot = protocol;
00083       parms.pm_port = 0;    /* not needed or used */
00084       if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t) xdr_pmap,
00085                    (caddr_t) & parms, (xdrproc_t) xdr_u_short,
00086                    (caddr_t) & port, tottimeout) != RPC_SUCCESS)
00087        {
00088          rpc_createerr.cf_stat = RPC_PMAPFAILURE;
00089          clnt_geterr (client, &rpc_createerr.cf_error);
00090        }
00091       else
00092        {
00093          if (port == 0)
00094            rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
00095        }
00096       CLNT_DESTROY (client);
00097     }
00098   /* (void)close(socket); CLNT_DESTROY already closed it */
00099   address->sin_port = 0;
00100   return port;
00101 }
00102 
00103 /* This is now the public function, which should find the fastest server */
00104 
00105 struct findserv_req
00106 {
00107   struct sockaddr_in sin;
00108   u_int32_t xid;
00109   u_int server_nr;
00110   u_int server_ep;
00111 };
00112 
00113 
00114 static long int
00115 __nis_findfastest_with_timeout (dir_binding *bind,
00116                             const struct timeval *timeout)
00117 {
00118   static const struct timeval TIMEOUT00 = { 0, 0 };
00119   struct findserv_req *pings;
00120   struct sockaddr_in sin, saved_sin;
00121   int found = -1;
00122   u_int32_t xid_seed;
00123   int sock, dontblock = 1;
00124   CLIENT *clnt;
00125   u_long i, j, pings_count, pings_max, fastest = -1;
00126   struct cu_data *cu;
00127 
00128   pings_max = bind->server_len * 2;       /* Reserve a little bit more memory
00129                                       for multihomed hosts */
00130   pings_count = 0;
00131   pings = malloc (sizeof (struct findserv_req) * pings_max);
00132   xid_seed = (u_int32_t) (time (NULL) ^ getpid ());
00133 
00134   if (__builtin_expect (pings == NULL, 0))
00135     return -1;
00136 
00137   memset (&sin, '\0', sizeof (sin));
00138   sin.sin_family = AF_INET;
00139   for (i = 0; i < bind->server_len; i++)
00140     for (j = 0; j < bind->server_val[i].ep.ep_len; ++j)
00141       if (strcmp (bind->server_val[i].ep.ep_val[j].family, "inet") == 0)
00142        if ((bind->server_val[i].ep.ep_val[j].proto == NULL) ||
00143            (bind->server_val[i].ep.ep_val[j].proto[0] == '-') ||
00144            (bind->server_val[i].ep.ep_val[j].proto[0] == '\0'))
00145          {
00146            sin.sin_addr.s_addr =
00147              inetstr2int (bind->server_val[i].ep.ep_val[j].uaddr);
00148            if (sin.sin_addr.s_addr == 0)
00149              continue;
00150            sin.sin_port = htons (__pmap_getnisport (&sin, NIS_PROG,
00151                                                NIS_VERSION,
00152                                                IPPROTO_UDP));
00153            if (sin.sin_port == 0)
00154              continue;
00155 
00156            if (pings_count >= pings_max)
00157              {
00158               struct findserv_req *new_pings;
00159 
00160               pings_max += 10;
00161               new_pings = realloc (pings, sizeof (struct findserv_req) *
00162                                  pings_max);
00163               if (__builtin_expect (new_pings == NULL, 0))
00164                 {
00165                   free (pings);
00166                   return -1;
00167                 }
00168               pings = new_pings;
00169              }
00170            memcpy ((char *) &pings[pings_count].sin, (char *) &sin,
00171                   sizeof (sin));
00172            memcpy ((char *)&saved_sin, (char *)&sin, sizeof(sin));
00173            pings[pings_count].xid = xid_seed + pings_count;
00174            pings[pings_count].server_nr = i;
00175            pings[pings_count].server_ep = j;
00176            ++pings_count;
00177          }
00178 
00179   /* Make sure at least one server was assigned */
00180   if (pings_count == 0)
00181     {
00182       free (pings);
00183       return -1;
00184     }
00185 
00186   /* Create RPC handle */
00187   sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
00188   clnt = clntudp_create (&saved_sin, NIS_PROG, NIS_VERSION, *timeout, &sock);
00189   if (clnt == NULL)
00190     {
00191       close (sock);
00192       free (pings);
00193       return -1;
00194     }
00195   auth_destroy (clnt->cl_auth);
00196   clnt->cl_auth = authunix_create_default ();
00197   cu = (struct cu_data *) clnt->cl_private;
00198   ioctl (sock, FIONBIO, &dontblock);
00199   /* Send to all servers the NULLPROC */
00200   for (i = 0; i < pings_count; ++i)
00201     {
00202       /* clntudp_call() will increment, subtract one */
00203       *((u_int32_t *) (cu->cu_outbuf)) = pings[i].xid - 1;
00204       memcpy ((char *) &cu->cu_raddr, (char *) &pings[i].sin,
00205              sizeof (struct sockaddr_in));
00206       /* Transmit to NULLPROC, return immediately. */
00207       clnt_call (clnt, NULLPROC,
00208                (xdrproc_t) xdr_void, (caddr_t) 0,
00209                (xdrproc_t) xdr_void, (caddr_t) 0, TIMEOUT00);
00210     }
00211 
00212   while (found == -1) {
00213     /* Receive reply from NULLPROC asynchronously. Note null inproc. */
00214     int rc = clnt_call (clnt, NULLPROC,
00215                      (xdrproc_t) NULL, (caddr_t) 0,
00216                      (xdrproc_t) xdr_void, (caddr_t) 0,
00217                      *timeout);
00218     if (RPC_SUCCESS == rc) {
00219       fastest = *((u_int32_t *) (cu->cu_inbuf)) - xid_seed;
00220       if (fastest < pings_count) {
00221        bind->server_used = pings[fastest].server_nr;
00222        bind->current_ep = pings[fastest].server_ep;
00223        found = 1;
00224       }
00225     } else {
00226       /*      clnt_perror(clnt, "__nis_findfastest"); */
00227       break;
00228     }
00229   }
00230 
00231 
00232   auth_destroy (clnt->cl_auth);
00233   clnt_destroy (clnt);
00234   close (sock);
00235 
00236   free (pings);
00237 
00238   return found;
00239 }
00240 
00241 
00242 long int
00243 __nis_findfastest (dir_binding *bind)
00244 {
00245   struct timeval timeout = { __NIS_PING_TIMEOUT_START, 0 };
00246   long int found = -1;
00247   long int retry = __NIS_PING_RETRY + 1;
00248 
00249   while (retry--)
00250     {
00251       found = __nis_findfastest_with_timeout (bind, &timeout);
00252       if (found != -1)
00253        break;
00254       timeout.tv_sec += __NIS_PING_TIMEOUT_INCREMENT;
00255     }
00256 
00257   return found;
00258 }