2 * Copyright (c) 1996, 1997
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
35 * unrestricted use provided that this legend is included on all tape
36 * media and as a part of the software program in whole or part. Users
37 * may copy or modify Sun RPC without charge, but are not authorized
38 * to license or distribute it to anyone else except as part of a product or
39 * program developed by the user.
41 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
42 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
45 * Sun RPC is provided with no support and without any obligation on the
46 * part of Sun Microsystems, Inc. to assist in its use, correction,
47 * modification or enhancement.
49 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
50 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
51 * OR ANY PART THEREOF.
53 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
54 * or profits or other special, indirect and consequential damages, even if
55 * Sun has been advised of the possibility of such damages.
57 * Sun Microsystems, Inc.
59 * Mountain View, California 94043
64 static char *sccsid = "@(#)from: clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
65 static char *sccsid = "@(#)from: clnt_udp.c 2.2 88/08/01 4.0 RPCSRC";
67 static const char rcsid[] =
72 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
74 * Copyright (C) 1984, Sun Microsystems, Inc.
85 #include <rpc/pmap_clnt.h>
86 #include <rpc/pmap_prot.h>
87 #include <rpcsvc/yp.h>
88 #include <sys/types.h>
90 #include <sys/socket.h>
91 #include <sys/signal.h>
92 #include <sys/ioctl.h>
93 #include <arpa/inet.h>
99 int cu_fd; /* connections fd */
100 bool_t cu_closeit; /* opened by library */
101 struct sockaddr_storage cu_raddr; /* remote address */
103 struct timeval cu_wait; /* retransmit interval */
104 struct timeval cu_total; /* total time for the call */
105 struct rpc_err cu_error;
108 u_int cu_sendsz; /* send size */
110 u_int cu_recvsz; /* recv size */
118 * Client interface to pmap rpc service.
120 * Copyright (C) 1984, Sun Microsystems, Inc.
124 static struct timeval timeout = { 1, 0 };
125 static struct timeval tottimeout = { 1, 0 };
128 * Find the mapped port for program,version.
129 * Calls the pmap service remotely to do the lookup.
130 * Returns 0 if no map exists.
133 __pmap_getport(struct sockaddr_in *address, u_long program, u_long version,
138 register CLIENT *client;
141 address->sin_port = htons(PMAPPORT);
143 client = clntudp_bufcreate(address, PMAPPROG,
144 PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
145 if (client != (CLIENT *)NULL) {
146 parms.pm_prog = program;
147 parms.pm_vers = version;
148 parms.pm_prot = protocol;
149 parms.pm_port = 0; /* not needed or used */
150 if (CLNT_CALL(client, PMAPPROC_GETPORT, xdr_pmap, &parms,
151 xdr_u_short, &port, tottimeout) != RPC_SUCCESS){
152 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
153 clnt_geterr(client, &rpc_createerr.cf_error);
154 } else if (port == 0) {
155 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
157 CLNT_DESTROY(client);
161 address->sin_port = 0;
166 * Transmit to YPPROC_DOMAIN_NONACK, return immediately.
169 ypproc_domain_nonack_2_send(domainname *argp, CLIENT *clnt)
171 static bool_t clnt_res;
172 struct timeval TIMEOUT = { 0, 0 };
174 memset((char *)&clnt_res, 0, sizeof (clnt_res));
175 if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
176 (xdrproc_t) xdr_domainname, (caddr_t) argp,
177 (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
178 TIMEOUT) != RPC_SUCCESS) {
185 * Receive response from YPPROC_DOMAIN_NONACK asynchronously.
188 ypproc_domain_nonack_2_recv(domainname *argp, CLIENT *clnt)
190 static bool_t clnt_res;
191 struct timeval TIMEOUT = { 0, 0 };
193 memset((char *)&clnt_res, 0, sizeof (clnt_res));
194 if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
195 (xdrproc_t) NULL, (caddr_t) argp,
196 (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
197 TIMEOUT) != RPC_SUCCESS) {
204 * "We have the machine that goes 'ping!'" -- Monty Python
206 * This function blasts packets at the YPPROC_DOMAIN_NONACK procedures
207 * of the NIS servers listed in restricted_addrs structure.
208 * Whoever replies the fastest becomes our chosen server.
210 * Note: THIS IS NOT A BROADCAST OPERATION! We could use clnt_broadcast()
211 * for this, but that has the following problems:
212 * - We only get the address of the machine that replied in the
213 * 'eachresult' callback, and on multi-homed machines this can
215 * - clnt_broadcast() only transmits to local networks, whereas with
216 * NIS+ you can have a perfectly good server located anywhere on or
217 * off the local network.
218 * - clnt_broadcast() blocks for an arbitrary amount of time which the
219 * caller can't control -- we want to avoid that.
221 * Also note that this has nothing to do with the NIS_PING procedure used
222 * for replica updates.
226 struct sockaddr_in sin;
231 __yp_ping(struct in_addr *restricted_addrs, int cnt, char *dom, short *port)
233 struct timeval tv = { 5, 0 };
234 struct ping_req **reqs;
237 struct sockaddr_in sin, *any = NULL;
239 time_t xid_seed, xid_lookup;
240 int sock, dontblock = 1;
246 /* Set up handles. */
247 reqs = calloc(1, sizeof(struct ping_req *) * cnt);
248 xid_seed = time(NULL) ^ getpid();
250 for (i = 0; i < cnt; i++) {
251 bzero((char *)&sin, sizeof(sin));
252 sin.sin_family = AF_INET;
253 bcopy((char *)&restricted_addrs[i],
254 (char *)&sin.sin_addr, sizeof(struct in_addr));
255 sin.sin_port = htons(__pmap_getport(&sin, YPPROG,
256 YPVERS, IPPROTO_UDP));
257 if (sin.sin_port == 0)
259 reqs[i] = calloc(1, sizeof(struct ping_req));
260 bcopy((char *)&sin, (char *)&reqs[i]->sin, sizeof(sin));
262 reqs[i]->xid = xid_seed;
267 /* Make sure at least one server was assigned */
273 /* Create RPC handle */
274 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
275 clnt = clntudp_create(any, YPPROG, YPVERS, tv, &sock);
278 for (i = 0; i < cnt; i++)
284 clnt->cl_auth = authunix_create_default();
285 cu = (struct cu_data *)clnt->cl_private;
288 clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv);
290 clnt_control(clnt, CLSET_ASYNC, (char *)&async);
291 ioctl(sock, FIONBIO, &dontblock);
294 for (i = 0; i < cnt; i++) {
295 if (reqs[i] != NULL) {
296 clnt_control(clnt, CLSET_XID, (char *)&reqs[i]->xid);
297 bcopy((char *)&reqs[i]->sin, (char *)&cu->cu_raddr,
298 sizeof(struct sockaddr_in));
299 ypproc_domain_nonack_2_send(&foo, clnt);
304 ypproc_domain_nonack_2_recv(&foo, clnt);
306 /* Got a winner -- look him up. */
307 clnt_control(clnt, CLGET_XID, (char *)&xid_lookup);
308 for (i = 0; i < cnt; i++) {
309 if (reqs[i] != NULL && reqs[i]->xid == xid_lookup) {
311 *port = reqs[i]->sin.sin_port;
315 /* Shut everything down */
316 auth_destroy(clnt->cl_auth);
320 for (i = 0; i < cnt; i++)