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.
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. */
22 #include <sys/ioctl.h>
23 #include <rpc/pmap_prot.h>
24 #include <rpc/pmap_clnt.h>
25 #include <rpcsvc/nis.h>
27 #include "nis_intern.h"
29 /* Private data kept per client handle, from sunrpc/clnt_udp.c */
34 struct sockaddr_in cu_raddr
;
36 struct timeval cu_wait
;
37 struct timeval cu_total
;
38 struct rpc_err cu_error
;
48 /* The following is the original routine from sunrpc/pm_getport.c.
49 The only change is the much shorter timeout. */
52 * Client interface to pmap rpc service.
54 * Copyright (C) 1984, Sun Microsystems, Inc.
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.
63 __pmap_getport (struct sockaddr_in
*address
, u_long program
,
64 u_long version
, u_int protocol
)
66 const struct timeval timeout
= {1, 0};
67 const struct timeval tottimeout
= {1, 0};
73 address
->sin_port
= htons (PMAPPORT
);
74 client
= clntudp_bufcreate (address
, PMAPPROG
, PMAPVERS
, timeout
, &socket
,
75 RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
76 if (client
!= (CLIENT
*) NULL
)
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
)
86 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
87 clnt_geterr (client
, &rpc_createerr
.cf_error
);
92 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
94 CLNT_DESTROY (client
);
96 /* (void)close(socket); CLNT_DESTROY already closed it */
97 address
->sin_port
= 0;
101 /* This is now the public function, which should find the fastest server */
105 struct sockaddr_in sin
;
112 __nis_findfastest (dir_binding
* bind
)
114 const struct timeval TIMEOUT50
= {5, 0};
115 const struct timeval TIMEOUT00
= {0, 0};
116 struct findserv_req
**pings
;
117 struct sockaddr_in sin
, saved_sin
;
119 u_int32_t xid_seed
, xid_lookup
;
120 int sock
, dontblock
= 1;
124 u_long i
, j
, pings_count
, pings_max
;
127 pings_max
= bind
->server_len
* 2; /* Reserve a little bit more memory
128 for multihomed hosts */
130 pings
= malloc (sizeof (struct findserv_req
*) * pings_max
);
131 xid_seed
= (u_int32_t
) (time (NULL
) ^ getpid ());
133 memset (&sin
, '\0', sizeof (sin
));
134 sin
.sin_family
= AF_INET
;
135 for (i
= 0; i
< bind
->server_len
; i
++)
136 for (j
= 0; j
< bind
->server_val
[i
].ep
.ep_len
; ++j
)
137 if (strcmp (bind
->server_val
[i
].ep
.ep_val
[j
].family
, "inet") == 0)
138 if ((bind
->server_val
[i
].ep
.ep_val
[j
].proto
== NULL
) ||
139 (strcmp (bind
->server_val
[i
].ep
.ep_val
[j
].proto
, "-") == 0) ||
140 (strlen (bind
->server_val
[i
].ep
.ep_val
[j
].proto
) == 0))
142 sin
.sin_addr
.s_addr
=
143 inetstr2int (bind
->server_val
[i
].ep
.ep_val
[j
].uaddr
);
144 if (sin
.sin_addr
.s_addr
== 0)
146 sin
.sin_port
= htons (__pmap_getport (&sin
, NIS_PROG
,
147 NIS_VERSION
, IPPROTO_UDP
));
148 if (sin
.sin_port
== 0)
151 if (pings_count
>= pings_max
)
154 pings
= realloc (pings
, sizeof (struct findserv_req
) *
157 pings
[pings_count
] = calloc (1, sizeof (struct findserv_req
));
158 memcpy ((char *) &pings
[pings_count
]->sin
, (char *) &sin
,
160 memcpy ((char *)&saved_sin
, (char *)&sin
, sizeof(sin
));
161 pings
[pings_count
]->xid
= xid_seed
;
162 pings
[pings_count
]->server_nr
= i
;
163 pings
[pings_count
]->server_ep
= j
;
168 /* Make sure at least one server was assigned */
169 if (pings_count
== 0)
175 /* Create RPC handle */
176 sock
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
177 clnt
= clntudp_create (&saved_sin
, NIS_PROG
, NIS_VERSION
, TIMEOUT50
, &sock
);
181 for (i
= 0; i
< pings_count
; ++i
)
186 clnt
->cl_auth
= authunix_create_default ();
187 cu
= (struct cu_data
*) clnt
->cl_private
;
188 clnt_control (clnt
, CLSET_TIMEOUT
, (char *) &TIMEOUT00
);
189 ioctl (sock
, FIONBIO
, &dontblock
);
191 /* Send to all servers the NULLPROC */
192 for (i
= 0; i
< pings_count
; ++i
)
194 /* clntudp_call() will increment, subtract one */
195 *((u_int32_t
*) (cu
->cu_outbuf
)) = pings
[i
]->xid
- 1;
196 memcpy ((char *) &cu
->cu_raddr
, (char *) &pings
[i
]->sin
,
197 sizeof (struct sockaddr_in
));
198 /* Transmit to NULLPROC, return immediately. */
199 clnt_call (clnt
, NULLPROC
, (xdrproc_t
) xdr_void
, (caddr_t
) foo
,
200 (xdrproc_t
) xdr_void
, (caddr_t
) & clnt_res
, TIMEOUT00
);
203 /* Receive reply from NULLPROC asynchronously */
204 memset ((char *) &clnt_res
, 0, sizeof (clnt_res
));
205 clnt_call (clnt
, NULLPROC
, (xdrproc_t
) NULL
, (caddr_t
) foo
,
206 (xdrproc_t
) xdr_void
, (caddr_t
) &clnt_res
, TIMEOUT00
);
208 xid_lookup
= *((u_int32_t
*) (cu
->cu_inbuf
));
209 for (i
= 0; i
< pings_count
; i
++)
211 if (pings
[i
]->xid
== xid_lookup
)
213 bind
->server_used
= pings
[i
]->server_nr
;
214 bind
->current_ep
= pings
[i
]->server_ep
;
219 auth_destroy (clnt
->cl_auth
);
223 for (i
= 0; i
< pings_count
; ++i
)