Don't define exp2 inline.
[glibc/history.git] / nis / nis_findserv.c
blob84508177e65be810b5893c1db36775738672f400
1 /* Copyright (C) 1997 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 <rpc/pmap_prot.h>
24 #include <rpc/pmap_clnt.h>
25 #include <rpcsvc/nis.h>
27 #include "nis_intern.h"
29 /* Private data kept per client handle, from sunrpc/clnt_udp.c */
30 struct cu_data
32 int cu_sock;
33 bool_t cu_closeit;
34 struct sockaddr_in cu_raddr;
35 int cu_rlen;
36 struct timeval cu_wait;
37 struct timeval cu_total;
38 struct rpc_err cu_error;
39 XDR cu_outxdrs;
40 u_int cu_xdrpos;
41 u_int cu_sendsz;
42 char *cu_outbuf;
43 u_int cu_recvsz;
44 char cu_inbuf[1];
48 /* The following is the original routine from sunrpc/pm_getport.c.
49 The only change is the much shorter timeout. */
51 * pmap_getport.c
52 * Client interface to pmap rpc service.
54 * Copyright (C) 1984, Sun Microsystems, Inc.
58 * Find the mapped port for program,version.
59 * Calls the pmap service remotely to do the lookup.
60 * Returns 0 if no map exists.
62 static u_short
63 __pmap_getport (struct sockaddr_in *address, u_long program,
64 u_long version, u_int protocol)
66 const struct timeval timeout = {1, 0};
67 const struct timeval tottimeout = {1, 0};
68 u_short port = 0;
69 int socket = -1;
70 CLIENT *client;
71 struct pmap parms;
73 address->sin_port = htons (PMAPPORT);
74 client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout, &socket,
75 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
76 if (client != (CLIENT *) NULL)
78 parms.pm_prog = program;
79 parms.pm_vers = version;
80 parms.pm_prot = protocol;
81 parms.pm_port = 0; /* not needed or used */
82 if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t) xdr_pmap,
83 (caddr_t) & parms, (xdrproc_t) xdr_u_short,
84 (caddr_t) & port, tottimeout) != RPC_SUCCESS)
86 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
87 clnt_geterr (client, &rpc_createerr.cf_error);
89 else
91 if (port == 0)
92 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
94 CLNT_DESTROY (client);
96 /* (void)close(socket); CLNT_DESTROY already closed it */
97 address->sin_port = 0;
98 return port;
101 /* This is now the public function, which should find the fastest server */
103 struct findserv_req
105 struct sockaddr_in sin;
106 u_int32_t xid;
107 u_int server_nr;
108 u_int server_ep;
111 long
112 __nis_findfastest (dir_binding * bind)
114 const struct timeval TIMEOUT50 = {5, 0};
115 const struct timeval TIMEOUT00 = {0, 0};
116 struct findserv_req **pings;
117 struct sockaddr_in sin, saved_sin;
118 int found = -1;
119 u_int32_t xid_seed, xid_lookup;
120 int sock, dontblock = 1;
121 CLIENT *clnt;
122 char clnt_res;
123 void *foo = NULL;
124 u_long i, j, pings_count, pings_max;
125 struct cu_data *cu;
127 pings_max = bind->server_len * 2; /* Reserve a little bit more memory
128 for multihomed hosts */
129 pings_count = 0;
130 pings = malloc (sizeof (struct findserv_req *) * pings_max);
131 xid_seed = (u_int32_t) (time (NULL) ^ getpid ());
133 memset (&sin, '\0', sizeof (sin));
134 sin.sin_family = AF_INET;
135 for (i = 0; i < bind->server_len; i++)
136 for (j = 0; j < bind->server_val[i].ep.ep_len; ++j)
137 if (strcmp (bind->server_val[i].ep.ep_val[j].family, "inet") == 0)
138 if ((bind->server_val[i].ep.ep_val[j].proto == NULL) ||
139 (strcmp (bind->server_val[i].ep.ep_val[j].proto, "-") == 0) ||
140 (strlen (bind->server_val[i].ep.ep_val[j].proto) == 0))
142 sin.sin_addr.s_addr =
143 inetstr2int (bind->server_val[i].ep.ep_val[j].uaddr);
144 if (sin.sin_addr.s_addr == 0)
145 continue;
146 sin.sin_port = htons (__pmap_getport (&sin, NIS_PROG,
147 NIS_VERSION, IPPROTO_UDP));
148 if (sin.sin_port == 0)
149 continue;
151 if (pings_count >= pings_max)
153 pings_max += 10;
154 pings = realloc (pings, sizeof (struct findserv_req) *
155 pings_max);
157 pings[pings_count] = calloc (1, sizeof (struct findserv_req));
158 memcpy ((char *) &pings[pings_count]->sin, (char *) &sin,
159 sizeof (sin));
160 memcpy ((char *)&saved_sin, (char *)&sin, sizeof(sin));
161 pings[pings_count]->xid = xid_seed;
162 pings[pings_count]->server_nr = i;
163 pings[pings_count]->server_ep = j;
164 ++xid_seed;
165 ++pings_count;
168 /* Make sure at least one server was assigned */
169 if (pings_count == 0)
171 free (pings);
172 return -1;
175 /* Create RPC handle */
176 sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
177 clnt = clntudp_create (&saved_sin, NIS_PROG, NIS_VERSION, TIMEOUT50, &sock);
178 if (clnt == NULL)
180 close (sock);
181 for (i = 0; i < pings_count; ++i)
182 free (pings[i]);
183 free (pings);
184 return -1;
186 clnt->cl_auth = authunix_create_default ();
187 cu = (struct cu_data *) clnt->cl_private;
188 clnt_control (clnt, CLSET_TIMEOUT, (char *) &TIMEOUT00);
189 ioctl (sock, FIONBIO, &dontblock);
191 /* Send to all servers the NULLPROC */
192 for (i = 0; i < pings_count; ++i)
194 /* clntudp_call() will increment, subtract one */
195 *((u_int32_t *) (cu->cu_outbuf)) = pings[i]->xid - 1;
196 memcpy ((char *) &cu->cu_raddr, (char *) &pings[i]->sin,
197 sizeof (struct sockaddr_in));
198 /* Transmit to NULLPROC, return immediately. */
199 clnt_call (clnt, NULLPROC, (xdrproc_t) xdr_void, (caddr_t) foo,
200 (xdrproc_t) xdr_void, (caddr_t) & clnt_res, TIMEOUT00);
203 /* Receive reply from NULLPROC asynchronously */
204 memset ((char *) &clnt_res, 0, sizeof (clnt_res));
205 clnt_call (clnt, NULLPROC, (xdrproc_t) NULL, (caddr_t) foo,
206 (xdrproc_t) xdr_void, (caddr_t) &clnt_res, TIMEOUT00);
208 xid_lookup = *((u_int32_t *) (cu->cu_inbuf));
209 for (i = 0; i < pings_count; i++)
211 if (pings[i]->xid == xid_lookup)
213 bind->server_used = pings[i]->server_nr;
214 bind->current_ep = pings[i]->server_ep;
215 found = 1;
219 auth_destroy (clnt->cl_auth);
220 clnt_destroy (clnt);
221 close (sock);
223 for (i = 0; i < pings_count; ++i)
224 free (pings[i]);
225 free (pings);
227 return found;