]>
Commit | Line | Data |
---|---|---|
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 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
3996f34b UD |
18 | |
19 | #include <string.h> | |
306eeae5 | 20 | #include <time.h> |
3996f34b UD |
21 | #include <unistd.h> |
22 | #include <sys/ioctl.h> | |
18de8c73 | 23 | #include <sys/socket.h> |
3996f34b UD |
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 | ||
3996f34b UD |
49 | /* |
50 | * Find the mapped port for program,version. | |
51 | * Calls the pmap service remotely to do the lookup. | |
52 | * Returns 0 if no map exists. | |
53 | */ | |
26a60f90 UD |
54 | u_short |
55 | __pmap_getnisport (struct sockaddr_in *address, u_long program, | |
56 | u_long version, u_int protocol) | |
3996f34b | 57 | { |
2f3e3dc7 | 58 | return __libc_rpc_getport (address, program, version, protocol, 1, 1); |
3996f34b UD |
59 | } |
60 | ||
650425ce | 61 | /* This is now the public function, which should find the fastest server */ |
3996f34b UD |
62 | |
63 | struct findserv_req | |
64 | { | |
65 | struct sockaddr_in sin; | |
55c14926 | 66 | u_int32_t xid; |
3996f34b UD |
67 | u_int server_nr; |
68 | u_int server_ep; | |
69 | }; | |
70 | ||
e852e889 | 71 | |
495818ce UD |
72 | static long int |
73 | __nis_findfastest_with_timeout (dir_binding *bind, | |
74 | const struct timeval *timeout) | |
75 | { | |
76 | static const struct timeval TIMEOUT00 = { 0, 0 }; | |
54eb84d0 | 77 | struct findserv_req *pings; |
650425ce | 78 | struct sockaddr_in sin, saved_sin; |
3996f34b | 79 | int found = -1; |
6c3ebebd | 80 | u_int32_t xid_seed; |
3996f34b UD |
81 | int sock, dontblock = 1; |
82 | CLIENT *clnt; | |
6c3ebebd | 83 | u_long i, j, pings_count, pings_max, fastest = -1; |
3996f34b UD |
84 | struct cu_data *cu; |
85 | ||
86 | pings_max = bind->server_len * 2; /* Reserve a little bit more memory | |
87 | for multihomed hosts */ | |
88 | pings_count = 0; | |
54eb84d0 | 89 | pings = malloc (sizeof (struct findserv_req) * pings_max); |
650425ce | 90 | xid_seed = (u_int32_t) (time (NULL) ^ getpid ()); |
3996f34b | 91 | |
54eb84d0 UD |
92 | if (__builtin_expect (pings == NULL, 0)) |
93 | return -1; | |
94 | ||
3996f34b UD |
95 | memset (&sin, '\0', sizeof (sin)); |
96 | sin.sin_family = AF_INET; | |
97 | for (i = 0; i < bind->server_len; i++) | |
98 | for (j = 0; j < bind->server_val[i].ep.ep_len; ++j) | |
99 | if (strcmp (bind->server_val[i].ep.ep_val[j].family, "inet") == 0) | |
650425ce | 100 | if ((bind->server_val[i].ep.ep_val[j].proto == NULL) || |
e852e889 | 101 | (bind->server_val[i].ep.ep_val[j].proto[0] == '-') || |
a53bad16 | 102 | (bind->server_val[i].ep.ep_val[j].proto[0] == '\0')) |
3996f34b UD |
103 | { |
104 | sin.sin_addr.s_addr = | |
105 | inetstr2int (bind->server_val[i].ep.ep_val[j].uaddr); | |
106 | if (sin.sin_addr.s_addr == 0) | |
107 | continue; | |
26a60f90 UD |
108 | sin.sin_port = htons (__pmap_getnisport (&sin, NIS_PROG, |
109 | NIS_VERSION, | |
110 | IPPROTO_UDP)); | |
3996f34b UD |
111 | if (sin.sin_port == 0) |
112 | continue; | |
113 | ||
114 | if (pings_count >= pings_max) | |
115 | { | |
54eb84d0 UD |
116 | struct findserv_req *new_pings; |
117 | ||
3996f34b | 118 | pings_max += 10; |
54eb84d0 UD |
119 | new_pings = realloc (pings, sizeof (struct findserv_req) * |
120 | pings_max); | |
121 | if (__builtin_expect (new_pings == NULL, 0)) | |
122 | { | |
123 | free (pings); | |
124 | return -1; | |
125 | } | |
126 | pings = new_pings; | |
3996f34b | 127 | } |
54eb84d0 | 128 | memcpy ((char *) &pings[pings_count].sin, (char *) &sin, |
3996f34b | 129 | sizeof (sin)); |
650425ce | 130 | memcpy ((char *)&saved_sin, (char *)&sin, sizeof(sin)); |
6c3ebebd | 131 | pings[pings_count].xid = xid_seed + pings_count; |
54eb84d0 UD |
132 | pings[pings_count].server_nr = i; |
133 | pings[pings_count].server_ep = j; | |
3996f34b UD |
134 | ++pings_count; |
135 | } | |
136 | ||
137 | /* Make sure at least one server was assigned */ | |
138 | if (pings_count == 0) | |
139 | { | |
140 | free (pings); | |
141 | return -1; | |
142 | } | |
143 | ||
144 | /* Create RPC handle */ | |
145 | sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
495818ce | 146 | clnt = clntudp_create (&saved_sin, NIS_PROG, NIS_VERSION, *timeout, &sock); |
3996f34b UD |
147 | if (clnt == NULL) |
148 | { | |
149 | close (sock); | |
3996f34b UD |
150 | free (pings); |
151 | return -1; | |
152 | } | |
26a60f90 | 153 | auth_destroy (clnt->cl_auth); |
3996f34b UD |
154 | clnt->cl_auth = authunix_create_default (); |
155 | cu = (struct cu_data *) clnt->cl_private; | |
3996f34b | 156 | ioctl (sock, FIONBIO, &dontblock); |
3996f34b UD |
157 | /* Send to all servers the NULLPROC */ |
158 | for (i = 0; i < pings_count; ++i) | |
159 | { | |
160 | /* clntudp_call() will increment, subtract one */ | |
54eb84d0 UD |
161 | *((u_int32_t *) (cu->cu_outbuf)) = pings[i].xid - 1; |
162 | memcpy ((char *) &cu->cu_raddr, (char *) &pings[i].sin, | |
650425ce UD |
163 | sizeof (struct sockaddr_in)); |
164 | /* Transmit to NULLPROC, return immediately. */ | |
495818ce | 165 | clnt_call (clnt, NULLPROC, |
6c3ebebd UD |
166 | (xdrproc_t) xdr_void, (caddr_t) 0, |
167 | (xdrproc_t) xdr_void, (caddr_t) 0, TIMEOUT00); | |
3996f34b | 168 | } |
495818ce UD |
169 | |
170 | while (found == -1) { | |
171 | /* Receive reply from NULLPROC asynchronously. Note null inproc. */ | |
172 | int rc = clnt_call (clnt, NULLPROC, | |
173 | (xdrproc_t) NULL, (caddr_t) 0, | |
174 | (xdrproc_t) xdr_void, (caddr_t) 0, | |
175 | *timeout); | |
176 | if (RPC_SUCCESS == rc) { | |
3a965496 UD |
177 | u_int32_t val; |
178 | memcpy (&val, cu->cu_inbuf, sizeof (u_int32_t)); | |
179 | fastest = val - xid_seed; | |
6c3ebebd | 180 | if (fastest < pings_count) { |
495818ce UD |
181 | bind->server_used = pings[fastest].server_nr; |
182 | bind->current_ep = pings[fastest].server_ep; | |
183 | found = 1; | |
6c3ebebd | 184 | } |
495818ce UD |
185 | } else { |
186 | /* clnt_perror(clnt, "__nis_findfastest"); */ | |
187 | break; | |
3996f34b | 188 | } |
495818ce UD |
189 | } |
190 | ||
191 | ||
3996f34b UD |
192 | auth_destroy (clnt->cl_auth); |
193 | clnt_destroy (clnt); | |
194 | close (sock); | |
195 | ||
3996f34b UD |
196 | free (pings); |
197 | ||
198 | return found; | |
495818ce UD |
199 | } |
200 | ||
201 | ||
202 | long int | |
203 | __nis_findfastest (dir_binding *bind) | |
204 | { | |
205 | struct timeval timeout = { __NIS_PING_TIMEOUT_START, 0 }; | |
206 | long int found = -1; | |
207 | long int retry = __NIS_PING_RETRY + 1; | |
208 | ||
209 | while (retry--) | |
210 | { | |
211 | found = __nis_findfastest_with_timeout (bind, &timeout); | |
212 | if (found != -1) | |
213 | break; | |
214 | timeout.tv_sec += __NIS_PING_TIMEOUT_INCREMENT; | |
215 | } | |
216 | ||
217 | return found; | |
3996f34b | 218 | } |