]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_findserv.c
Remove aliasing problems in NIS code
[thirdparty/glibc.git] / nis / nis_findserv.c
CommitLineData
3a965496 1/* Copyright (C) 1997, 1998, 2000, 2001, 2010, 2011 Free Software Foundation, Inc.
3996f34b
UD
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
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
3996f34b
UD
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
41bdb6e2 13 Lesser General Public License for more details.
3996f34b 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
3996f34b
UD
19
20#include <string.h>
306eeae5 21#include <time.h>
3996f34b
UD
22#include <unistd.h>
23#include <sys/ioctl.h>
18de8c73 24#include <sys/socket.h>
3996f34b
UD
25#include <rpc/pmap_prot.h>
26#include <rpc/pmap_clnt.h>
27#include <rpcsvc/nis.h>
28
29#include "nis_intern.h"
30
31/* Private data kept per client handle, from sunrpc/clnt_udp.c */
32struct cu_data
33 {
34 int cu_sock;
35 bool_t cu_closeit;
36 struct sockaddr_in cu_raddr;
37 int cu_rlen;
38 struct timeval cu_wait;
39 struct timeval cu_total;
40 struct rpc_err cu_error;
41 XDR cu_outxdrs;
42 u_int cu_xdrpos;
43 u_int cu_sendsz;
44 char *cu_outbuf;
45 u_int cu_recvsz;
46 char cu_inbuf[1];
47 };
48
49
3996f34b
UD
50/*
51 * Find the mapped port for program,version.
52 * Calls the pmap service remotely to do the lookup.
53 * Returns 0 if no map exists.
54 */
26a60f90
UD
55u_short
56__pmap_getnisport (struct sockaddr_in *address, u_long program,
57 u_long version, u_int protocol)
3996f34b 58{
2f3e3dc7 59 return __libc_rpc_getport (address, program, version, protocol, 1, 1);
3996f34b
UD
60}
61
650425ce 62/* This is now the public function, which should find the fastest server */
3996f34b
UD
63
64struct findserv_req
65{
66 struct sockaddr_in sin;
55c14926 67 u_int32_t xid;
3996f34b
UD
68 u_int server_nr;
69 u_int server_ep;
70};
71
e852e889 72
495818ce
UD
73static long int
74__nis_findfastest_with_timeout (dir_binding *bind,
75 const struct timeval *timeout)
76{
77 static const struct timeval TIMEOUT00 = { 0, 0 };
54eb84d0 78 struct findserv_req *pings;
650425ce 79 struct sockaddr_in sin, saved_sin;
3996f34b 80 int found = -1;
6c3ebebd 81 u_int32_t xid_seed;
3996f34b
UD
82 int sock, dontblock = 1;
83 CLIENT *clnt;
6c3ebebd 84 u_long i, j, pings_count, pings_max, fastest = -1;
3996f34b
UD
85 struct cu_data *cu;
86
87 pings_max = bind->server_len * 2; /* Reserve a little bit more memory
88 for multihomed hosts */
89 pings_count = 0;
54eb84d0 90 pings = malloc (sizeof (struct findserv_req) * pings_max);
650425ce 91 xid_seed = (u_int32_t) (time (NULL) ^ getpid ());
3996f34b 92
54eb84d0
UD
93 if (__builtin_expect (pings == NULL, 0))
94 return -1;
95
3996f34b
UD
96 memset (&sin, '\0', sizeof (sin));
97 sin.sin_family = AF_INET;
98 for (i = 0; i < bind->server_len; i++)
99 for (j = 0; j < bind->server_val[i].ep.ep_len; ++j)
100 if (strcmp (bind->server_val[i].ep.ep_val[j].family, "inet") == 0)
650425ce 101 if ((bind->server_val[i].ep.ep_val[j].proto == NULL) ||
e852e889 102 (bind->server_val[i].ep.ep_val[j].proto[0] == '-') ||
a53bad16 103 (bind->server_val[i].ep.ep_val[j].proto[0] == '\0'))
3996f34b
UD
104 {
105 sin.sin_addr.s_addr =
106 inetstr2int (bind->server_val[i].ep.ep_val[j].uaddr);
107 if (sin.sin_addr.s_addr == 0)
108 continue;
26a60f90
UD
109 sin.sin_port = htons (__pmap_getnisport (&sin, NIS_PROG,
110 NIS_VERSION,
111 IPPROTO_UDP));
3996f34b
UD
112 if (sin.sin_port == 0)
113 continue;
114
115 if (pings_count >= pings_max)
116 {
54eb84d0
UD
117 struct findserv_req *new_pings;
118
3996f34b 119 pings_max += 10;
54eb84d0
UD
120 new_pings = realloc (pings, sizeof (struct findserv_req) *
121 pings_max);
122 if (__builtin_expect (new_pings == NULL, 0))
123 {
124 free (pings);
125 return -1;
126 }
127 pings = new_pings;
3996f34b 128 }
54eb84d0 129 memcpy ((char *) &pings[pings_count].sin, (char *) &sin,
3996f34b 130 sizeof (sin));
650425ce 131 memcpy ((char *)&saved_sin, (char *)&sin, sizeof(sin));
6c3ebebd 132 pings[pings_count].xid = xid_seed + pings_count;
54eb84d0
UD
133 pings[pings_count].server_nr = i;
134 pings[pings_count].server_ep = j;
3996f34b
UD
135 ++pings_count;
136 }
137
138 /* Make sure at least one server was assigned */
139 if (pings_count == 0)
140 {
141 free (pings);
142 return -1;
143 }
144
145 /* Create RPC handle */
146 sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
495818ce 147 clnt = clntudp_create (&saved_sin, NIS_PROG, NIS_VERSION, *timeout, &sock);
3996f34b
UD
148 if (clnt == NULL)
149 {
150 close (sock);
3996f34b
UD
151 free (pings);
152 return -1;
153 }
26a60f90 154 auth_destroy (clnt->cl_auth);
3996f34b
UD
155 clnt->cl_auth = authunix_create_default ();
156 cu = (struct cu_data *) clnt->cl_private;
3996f34b 157 ioctl (sock, FIONBIO, &dontblock);
3996f34b
UD
158 /* Send to all servers the NULLPROC */
159 for (i = 0; i < pings_count; ++i)
160 {
161 /* clntudp_call() will increment, subtract one */
54eb84d0
UD
162 *((u_int32_t *) (cu->cu_outbuf)) = pings[i].xid - 1;
163 memcpy ((char *) &cu->cu_raddr, (char *) &pings[i].sin,
650425ce
UD
164 sizeof (struct sockaddr_in));
165 /* Transmit to NULLPROC, return immediately. */
495818ce 166 clnt_call (clnt, NULLPROC,
6c3ebebd
UD
167 (xdrproc_t) xdr_void, (caddr_t) 0,
168 (xdrproc_t) xdr_void, (caddr_t) 0, TIMEOUT00);
3996f34b 169 }
495818ce
UD
170
171 while (found == -1) {
172 /* Receive reply from NULLPROC asynchronously. Note null inproc. */
173 int rc = clnt_call (clnt, NULLPROC,
174 (xdrproc_t) NULL, (caddr_t) 0,
175 (xdrproc_t) xdr_void, (caddr_t) 0,
176 *timeout);
177 if (RPC_SUCCESS == rc) {
3a965496
UD
178 u_int32_t val;
179 memcpy (&val, cu->cu_inbuf, sizeof (u_int32_t));
180 fastest = val - xid_seed;
6c3ebebd 181 if (fastest < pings_count) {
495818ce
UD
182 bind->server_used = pings[fastest].server_nr;
183 bind->current_ep = pings[fastest].server_ep;
184 found = 1;
6c3ebebd 185 }
495818ce
UD
186 } else {
187 /* clnt_perror(clnt, "__nis_findfastest"); */
188 break;
3996f34b 189 }
495818ce
UD
190 }
191
192
3996f34b
UD
193 auth_destroy (clnt->cl_auth);
194 clnt_destroy (clnt);
195 close (sock);
196
3996f34b
UD
197 free (pings);
198
199 return found;
495818ce
UD
200}
201
202
203long int
204__nis_findfastest (dir_binding *bind)
205{
206 struct timeval timeout = { __NIS_PING_TIMEOUT_START, 0 };
207 long int found = -1;
208 long int retry = __NIS_PING_RETRY + 1;
209
210 while (retry--)
211 {
212 found = __nis_findfastest_with_timeout (bind, &timeout);
213 if (found != -1)
214 break;
215 timeout.tv_sec += __NIS_PING_TIMEOUT_INCREMENT;
216 }
217
218 return found;
3996f34b 219}