]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nis_findserv.c
Update.
[thirdparty/glibc.git] / nis / nis_findserv.c
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.
4
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.
9
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.
14
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. */
19
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>
26
27 #include "nis_intern.h"
28
29 /* Private data kept per client handle, from sunrpc/clnt_udp.c */
30 struct cu_data
31 {
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];
45 };
46
47
48 /* The following is the original routine from sunrpc/pm_getport.c.
49 The only change is the much shorter timeout. */
50 /*
51 * pmap_getport.c
52 * Client interface to pmap rpc service.
53 *
54 * Copyright (C) 1984, Sun Microsystems, Inc.
55 */
56
57 /*
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.
61 */
62 static u_short
63 __pmap_getport (struct sockaddr_in *address, u_long program,
64 u_long version, u_int protocol)
65 {
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;
72
73 address->sin_port = htons (PMAPPORT);
74 client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout, &socket,
75 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
76 if (client != (CLIENT *) NULL)
77 {
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)
85 {
86 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
87 clnt_geterr (client, &rpc_createerr.cf_error);
88 }
89 else if (port == 0)
90 {
91 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
92 }
93 CLNT_DESTROY (client);
94 }
95 /* (void)close(socket); CLNT_DESTROY already closed it */
96 address->sin_port = 0;
97 return port;
98 }
99
100 /* Transmit to NULLPROC, return immediately. */
101 static void *
102 nis_null_3_send (void *argp, CLIENT * clnt)
103 {
104 static char clnt_res;
105 struct timeval TIMEOUT = {0, 0};
106
107 memset ((char *) &clnt_res, 0, sizeof (clnt_res));
108 if (clnt_call (clnt, NULLPROC,
109 (xdrproc_t) xdr_void, (caddr_t) argp,
110 (xdrproc_t) xdr_void, (caddr_t) & clnt_res,
111 TIMEOUT) != RPC_SUCCESS)
112 return NULL;
113 return (void *) &clnt_res;
114 }
115
116 /* Receive request from NULLPROC asynchronously. */
117 static void *
118 nis_null_3_recv (void *argp, CLIENT * clnt)
119 {
120 static char clnt_res;
121 struct timeval TIMEOUT = {0, 0};
122
123 memset ((char *) &clnt_res, 0, sizeof (clnt_res));
124 if (clnt_call (clnt, NULLPROC,
125 (xdrproc_t) NULL, (caddr_t) argp,
126 (xdrproc_t) xdr_void, (caddr_t) & clnt_res,
127 TIMEOUT) != RPC_SUCCESS)
128 return NULL;
129 return (void *) &clnt_res;
130 }
131
132 /* This is now the public functions, which should find the fastest server */
133
134 struct findserv_req
135 {
136 struct sockaddr_in sin;
137 u_long xid;
138 u_int server_nr;
139 u_int server_ep;
140 };
141
142 long
143 __nis_findfastest (dir_binding * bind)
144 {
145 struct timeval TIMEOUT = {5, 0};
146 struct findserv_req **pings;
147 struct sockaddr_in sin;
148 int found = -1;
149 time_t xid_seed, xid_lookup;
150 int sock, dontblock = 1;
151 CLIENT *clnt;
152 void *foo = NULL;
153 u_long i, j, pings_count, pings_max;
154 struct cu_data *cu;
155
156 pings_max = bind->server_len * 2; /* Reserve a little bit more memory
157 for multihomed hosts */
158 pings_count = 0;
159 pings = malloc (sizeof (struct findserv_req *) * pings_max);
160 xid_seed = time (NULL) ^ getpid ();
161
162 memset (&sin, '\0', sizeof (sin));
163 sin.sin_family = AF_INET;
164 for (i = 0; i < bind->server_len; i++)
165 for (j = 0; j < bind->server_val[i].ep.ep_len; ++j)
166 if (strcmp (bind->server_val[i].ep.ep_val[j].family, "inet") == 0)
167 if (strcmp (bind->server_val[i].ep.ep_val[j].proto, "-") == 0)
168 {
169 sin.sin_addr.s_addr =
170 inetstr2int (bind->server_val[i].ep.ep_val[j].uaddr);
171 if (sin.sin_addr.s_addr == 0)
172 continue;
173 sin.sin_port = htons (__pmap_getport (&sin, NIS_PROG,
174 NIS_VERSION, IPPROTO_UDP));
175 if (sin.sin_port == 0)
176 continue;
177
178 if (pings_count >= pings_max)
179 {
180 pings_max += 10;
181 pings = realloc (pings, sizeof (struct findserv_req) *
182 pings_max);
183 }
184 pings[pings_count] = calloc (1, sizeof (struct findserv_req));
185 memcpy ((char *) &pings[pings_count]->sin, (char *) &sin,
186 sizeof (sin));
187 pings[pings_count]->xid = xid_seed;
188 pings[pings_count]->server_nr = i;
189 pings[pings_count]->server_ep = j;
190 ++xid_seed;
191 ++pings_count;
192 }
193
194 /* Make sure at least one server was assigned */
195 if (pings_count == 0)
196 {
197 free (pings);
198 return -1;
199 }
200
201 /* Create RPC handle */
202 sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
203 clnt = clntudp_create (&sin, NIS_PROG, NIS_VERSION, TIMEOUT, &sock);
204 if (clnt == NULL)
205 {
206 close (sock);
207 for (i = 0; i < pings_count; ++i)
208 free (pings[i]);
209 free (pings);
210 return -1;
211 }
212 clnt->cl_auth = authunix_create_default ();
213 cu = (struct cu_data *) clnt->cl_private;
214 TIMEOUT.tv_sec = 0;
215 clnt_control (clnt, CLSET_TIMEOUT, (char *) &TIMEOUT);
216 ioctl (sock, FIONBIO, &dontblock);
217
218 /* Send to all servers the NULLPROC */
219 for (i = 0; i < pings_count; ++i)
220 {
221 /* clntudp_call() will increment, subtract one */
222 *((u_int32_t *) (cu->cu_outbuf)) = pings[i]->xid - 1;
223 bcopy ((char *) &pings[i]->sin, (char *) &cu->cu_raddr,
224 sizeof (struct sockaddr_in));
225 nis_null_3_send (foo, clnt);
226 }
227
228 /* Receive reply */
229 nis_null_3_recv (foo, clnt);
230
231 xid_lookup = *((u_int32_t *) (cu->cu_inbuf));
232 for (i = 0; i < pings_count; i++)
233 {
234 if (pings[i]->xid == xid_lookup)
235 {
236 bind->server_used = pings[i]->server_nr;
237 bind->current_ep = pings[i]->server_ep;
238 found = 1;
239 }
240 }
241
242 auth_destroy (clnt->cl_auth);
243 clnt_destroy (clnt);
244 close (sock);
245
246 for (i = 0; i < pings_count; ++i)
247 free (pings[i]);
248 free (pings);
249
250 return found;
251 }