Update.
[glibc.git] / nis / nis_findserv.c
blob2659878e2058f07d021c061cff64e2627d0f4ca1
1 /* Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <rpc/pmap_prot.h>
25 #include <rpc/pmap_clnt.h>
26 #include <rpcsvc/nis.h>
28 #include "nis_intern.h"
30 /* Private data kept per client handle, from sunrpc/clnt_udp.c */
31 struct cu_data
33 int cu_sock;
34 bool_t cu_closeit;
35 struct sockaddr_in cu_raddr;
36 int cu_rlen;
37 struct timeval cu_wait;
38 struct timeval cu_total;
39 struct rpc_err cu_error;
40 XDR cu_outxdrs;
41 u_int cu_xdrpos;
42 u_int cu_sendsz;
43 char *cu_outbuf;
44 u_int cu_recvsz;
45 char cu_inbuf[1];
49 /* The following is the original routine from sunrpc/pm_getport.c.
50 The only change is the much shorter timeout. */
52 * pmap_getport.c
53 * Client interface to pmap rpc service.
55 * Copyright (C) 1984, Sun Microsystems, Inc.
59 * Find the mapped port for program,version.
60 * Calls the pmap service remotely to do the lookup.
61 * Returns 0 if no map exists.
63 u_short
64 __pmap_getnisport (struct sockaddr_in *address, u_long program,
65 u_long version, u_int protocol)
67 const struct timeval timeout = {1, 0};
68 const struct timeval tottimeout = {1, 0};
69 u_short port = 0;
70 int socket = -1;
71 CLIENT *client;
72 struct pmap parms;
74 address->sin_port = htons (PMAPPORT);
75 client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout, &socket,
76 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
77 if (client != (CLIENT *) NULL)
79 parms.pm_prog = program;
80 parms.pm_vers = version;
81 parms.pm_prot = protocol;
82 parms.pm_port = 0; /* not needed or used */
83 if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t) xdr_pmap,
84 (caddr_t) & parms, (xdrproc_t) xdr_u_short,
85 (caddr_t) & port, tottimeout) != RPC_SUCCESS)
87 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
88 clnt_geterr (client, &rpc_createerr.cf_error);
90 else
92 if (port == 0)
93 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
95 CLNT_DESTROY (client);
97 /* (void)close(socket); CLNT_DESTROY already closed it */
98 address->sin_port = 0;
99 return port;
102 /* This is now the public function, which should find the fastest server */
104 struct findserv_req
106 struct sockaddr_in sin;
107 u_int32_t xid;
108 u_int server_nr;
109 u_int server_ep;
112 long
113 __nis_findfastest (dir_binding *bind)
115 #if 0
116 unsigned long i, j;
118 for (i = 0; i < bind->server_len; i++)
119 for (j = 0; j < bind->server_val[i].ep.ep_len; ++j)
120 if (strcmp (bind->server_val[i].ep.ep_val[j].family, "inet") == 0)
121 if ((bind->server_val[i].ep.ep_val[j].proto == NULL) ||
122 (bind->server_val[i].ep.ep_val[j].proto[0] == '-') ||
123 (bind->server_val[i].ep.ep_val[j].proto[0] == '\0'))
125 bind->server_used = i;
126 bind->current_ep = j;
127 return 1;
130 return 0;
131 #else
132 const struct timeval TIMEOUT50 = {5, 0};
133 const struct timeval TIMEOUT00 = {0, 0};
134 struct findserv_req *pings;
135 struct sockaddr_in sin, saved_sin;
136 int found = -1;
137 u_int32_t xid_seed, xid_lookup;
138 int sock, dontblock = 1;
139 CLIENT *clnt;
140 char clnt_res;
141 void *foo = NULL;
142 u_long i, j, pings_count, pings_max;
143 struct cu_data *cu;
145 pings_max = bind->server_len * 2; /* Reserve a little bit more memory
146 for multihomed hosts */
147 pings_count = 0;
148 pings = malloc (sizeof (struct findserv_req) * pings_max);
149 xid_seed = (u_int32_t) (time (NULL) ^ getpid ());
151 if (__builtin_expect (pings == NULL, 0))
152 return -1;
154 memset (&sin, '\0', sizeof (sin));
155 sin.sin_family = AF_INET;
156 for (i = 0; i < bind->server_len; i++)
157 for (j = 0; j < bind->server_val[i].ep.ep_len; ++j)
158 if (strcmp (bind->server_val[i].ep.ep_val[j].family, "inet") == 0)
159 if ((bind->server_val[i].ep.ep_val[j].proto == NULL) ||
160 (bind->server_val[i].ep.ep_val[j].proto[0] == '-') ||
161 (bind->server_val[i].ep.ep_val[j].proto[0] == '\0'))
163 sin.sin_addr.s_addr =
164 inetstr2int (bind->server_val[i].ep.ep_val[j].uaddr);
165 if (sin.sin_addr.s_addr == 0)
166 continue;
167 sin.sin_port = htons (__pmap_getnisport (&sin, NIS_PROG,
168 NIS_VERSION,
169 IPPROTO_UDP));
170 if (sin.sin_port == 0)
171 continue;
173 if (pings_count >= pings_max)
175 struct findserv_req *new_pings;
177 pings_max += 10;
178 new_pings = realloc (pings, sizeof (struct findserv_req) *
179 pings_max);
180 if (__builtin_expect (new_pings == NULL, 0))
182 free (pings);
183 return -1;
185 pings = new_pings;
187 memcpy ((char *) &pings[pings_count].sin, (char *) &sin,
188 sizeof (sin));
189 memcpy ((char *)&saved_sin, (char *)&sin, sizeof(sin));
190 pings[pings_count].xid = xid_seed;
191 pings[pings_count].server_nr = i;
192 pings[pings_count].server_ep = j;
193 ++xid_seed;
194 ++pings_count;
197 /* Make sure at least one server was assigned */
198 if (pings_count == 0)
200 free (pings);
201 return -1;
204 /* Create RPC handle */
205 sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
206 clnt = clntudp_create (&saved_sin, NIS_PROG, NIS_VERSION, TIMEOUT50, &sock);
207 if (clnt == NULL)
209 close (sock);
210 free (pings);
211 return -1;
213 auth_destroy (clnt->cl_auth);
214 clnt->cl_auth = authunix_create_default ();
215 cu = (struct cu_data *) clnt->cl_private;
216 clnt_control (clnt, CLSET_TIMEOUT, (char *) &TIMEOUT00);
217 ioctl (sock, FIONBIO, &dontblock);
219 /* Send to all servers the NULLPROC */
220 for (i = 0; i < pings_count; ++i)
222 /* clntudp_call() will increment, subtract one */
223 *((u_int32_t *) (cu->cu_outbuf)) = pings[i].xid - 1;
224 memcpy ((char *) &cu->cu_raddr, (char *) &pings[i].sin,
225 sizeof (struct sockaddr_in));
226 /* Transmit to NULLPROC, return immediately. */
227 clnt_call (clnt, NULLPROC, (xdrproc_t) xdr_void, (caddr_t) foo,
228 (xdrproc_t) xdr_void, (caddr_t) & clnt_res, TIMEOUT00);
231 /* Receive reply from NULLPROC asynchronously */
232 memset ((char *) &clnt_res, 0, sizeof (clnt_res));
233 clnt_call (clnt, NULLPROC, (xdrproc_t) NULL, (caddr_t) foo,
234 (xdrproc_t) xdr_void, (caddr_t) &clnt_res, TIMEOUT00);
236 xid_lookup = *((u_int32_t *) (cu->cu_inbuf));
237 for (i = 0; i < pings_count; i++)
239 if (pings[i].xid == xid_lookup)
241 bind->server_used = pings[i].server_nr;
242 bind->current_ep = pings[i].server_ep;
243 found = 1;
247 auth_destroy (clnt->cl_auth);
248 clnt_destroy (clnt);
249 close (sock);
251 free (pings);
253 return found;
254 #endif