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