1 /* Copyright (C) 1997, 1998, 2000, 2001 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. */
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <rpc/pmap_prot.h>
26 #include <rpc/pmap_clnt.h>
27 #include <rpcsvc/nis.h>
29 #include "nis_intern.h"
31 /* Private data kept per client handle, from sunrpc/clnt_udp.c */
36 struct sockaddr_in cu_raddr
;
38 struct timeval cu_wait
;
39 struct timeval cu_total
;
40 struct rpc_err cu_error
;
50 /* The following is the original routine from sunrpc/pm_getport.c.
51 The only change is the much shorter timeout. */
54 * Client interface to pmap rpc service.
56 * Copyright (C) 1984, Sun Microsystems, Inc.
60 * Find the mapped port for program,version.
61 * Calls the pmap service remotely to do the lookup.
62 * Returns 0 if no map exists.
65 __pmap_getnisport (struct sockaddr_in
*address
, u_long program
,
66 u_long version
, u_int protocol
)
68 const struct timeval timeout
= {1, 0};
69 const struct timeval tottimeout
= {1, 0};
75 address
->sin_port
= htons (PMAPPORT
);
76 client
= clntudp_bufcreate (address
, PMAPPROG
, PMAPVERS
, timeout
, &socket
,
77 RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
78 if (client
!= (CLIENT
*) NULL
)
80 parms
.pm_prog
= program
;
81 parms
.pm_vers
= version
;
82 parms
.pm_prot
= protocol
;
83 parms
.pm_port
= 0; /* not needed or used */
84 if (CLNT_CALL (client
, PMAPPROC_GETPORT
, (xdrproc_t
) xdr_pmap
,
85 (caddr_t
) & parms
, (xdrproc_t
) xdr_u_short
,
86 (caddr_t
) & port
, tottimeout
) != RPC_SUCCESS
)
88 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
89 clnt_geterr (client
, &rpc_createerr
.cf_error
);
94 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
96 CLNT_DESTROY (client
);
98 /* (void)close(socket); CLNT_DESTROY already closed it */
99 address
->sin_port
= 0;
103 /* This is now the public function, which should find the fastest server */
107 struct sockaddr_in sin
;
114 __nis_findfastest (dir_binding
*bind
)
119 for (i
= 0; i
< bind
->server_len
; i
++)
120 for (j
= 0; j
< bind
->server_val
[i
].ep
.ep_len
; ++j
)
121 if (strcmp (bind
->server_val
[i
].ep
.ep_val
[j
].family
, "inet") == 0)
122 if ((bind
->server_val
[i
].ep
.ep_val
[j
].proto
== NULL
) ||
123 (bind
->server_val
[i
].ep
.ep_val
[j
].proto
[0] == '-') ||
124 (bind
->server_val
[i
].ep
.ep_val
[j
].proto
[0] == '\0'))
126 bind
->server_used
= i
;
127 bind
->current_ep
= j
;
133 const struct timeval TIMEOUT50
= {5, 0};
134 const struct timeval TIMEOUT00
= {0, 0};
135 struct findserv_req
*pings
;
136 struct sockaddr_in sin
, saved_sin
;
138 u_int32_t xid_seed
, xid_lookup
;
139 int sock
, dontblock
= 1;
143 u_long i
, j
, pings_count
, pings_max
;
146 pings_max
= bind
->server_len
* 2; /* Reserve a little bit more memory
147 for multihomed hosts */
149 pings
= malloc (sizeof (struct findserv_req
) * pings_max
);
150 xid_seed
= (u_int32_t
) (time (NULL
) ^ getpid ());
152 if (__builtin_expect (pings
== NULL
, 0))
155 memset (&sin
, '\0', sizeof (sin
));
156 sin
.sin_family
= AF_INET
;
157 for (i
= 0; i
< bind
->server_len
; i
++)
158 for (j
= 0; j
< bind
->server_val
[i
].ep
.ep_len
; ++j
)
159 if (strcmp (bind
->server_val
[i
].ep
.ep_val
[j
].family
, "inet") == 0)
160 if ((bind
->server_val
[i
].ep
.ep_val
[j
].proto
== NULL
) ||
161 (bind
->server_val
[i
].ep
.ep_val
[j
].proto
[0] == '-') ||
162 (bind
->server_val
[i
].ep
.ep_val
[j
].proto
[0] == '\0'))
164 sin
.sin_addr
.s_addr
=
165 inetstr2int (bind
->server_val
[i
].ep
.ep_val
[j
].uaddr
);
166 if (sin
.sin_addr
.s_addr
== 0)
168 sin
.sin_port
= htons (__pmap_getnisport (&sin
, NIS_PROG
,
171 if (sin
.sin_port
== 0)
174 if (pings_count
>= pings_max
)
176 struct findserv_req
*new_pings
;
179 new_pings
= realloc (pings
, sizeof (struct findserv_req
) *
181 if (__builtin_expect (new_pings
== NULL
, 0))
188 memcpy ((char *) &pings
[pings_count
].sin
, (char *) &sin
,
190 memcpy ((char *)&saved_sin
, (char *)&sin
, sizeof(sin
));
191 pings
[pings_count
].xid
= xid_seed
;
192 pings
[pings_count
].server_nr
= i
;
193 pings
[pings_count
].server_ep
= j
;
198 /* Make sure at least one server was assigned */
199 if (pings_count
== 0)
205 /* Create RPC handle */
206 sock
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
207 clnt
= clntudp_create (&saved_sin
, NIS_PROG
, NIS_VERSION
, TIMEOUT50
, &sock
);
214 auth_destroy (clnt
->cl_auth
);
215 clnt
->cl_auth
= authunix_create_default ();
216 cu
= (struct cu_data
*) clnt
->cl_private
;
217 clnt_control (clnt
, CLSET_TIMEOUT
, (char *) &TIMEOUT00
);
218 ioctl (sock
, FIONBIO
, &dontblock
);
220 /* Send to all servers the NULLPROC */
221 for (i
= 0; i
< pings_count
; ++i
)
223 /* clntudp_call() will increment, subtract one */
224 *((u_int32_t
*) (cu
->cu_outbuf
)) = pings
[i
].xid
- 1;
225 memcpy ((char *) &cu
->cu_raddr
, (char *) &pings
[i
].sin
,
226 sizeof (struct sockaddr_in
));
227 /* Transmit to NULLPROC, return immediately. */
228 clnt_call (clnt
, NULLPROC
, (xdrproc_t
) xdr_void
, (caddr_t
) foo
,
229 (xdrproc_t
) xdr_void
, (caddr_t
) & clnt_res
, TIMEOUT00
);
232 /* Receive reply from NULLPROC asynchronously */
233 memset ((char *) &clnt_res
, 0, sizeof (clnt_res
));
234 clnt_call (clnt
, NULLPROC
, (xdrproc_t
) NULL
, (caddr_t
) foo
,
235 (xdrproc_t
) xdr_void
, (caddr_t
) &clnt_res
, TIMEOUT00
);
237 xid_lookup
= *((u_int32_t
*) (cu
->cu_inbuf
));
238 for (i
= 0; i
< pings_count
; i
++)
240 if (pings
[i
].xid
== xid_lookup
)
242 bind
->server_used
= pings
[i
].server_nr
;
243 bind
->current_ep
= pings
[i
].server_ep
;
248 auth_destroy (clnt
->cl_auth
);