xref: /netbsd-src/lib/libc/rpc/pmap_rmt.c (revision 95d875fb90b1458e4f1de6950286ddcd6644bc61)
1 /*	$NetBSD: pmap_rmt.c,v 1.23 1999/09/20 04:39:23 lukem Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)pmap_rmt.c	2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: pmap_rmt.c,v 1.23 1999/09/20 04:39:23 lukem Exp $");
39 #endif
40 #endif
41 
42 /*
43  * pmap_rmt.c
44  * Client interface to pmap rpc service.
45  * remote call and broadcast service
46  *
47  * Copyright (C) 1984, Sun Microsystems, Inc.
48  */
49 
50 #include "namespace.h"
51 
52 #include <sys/types.h>
53 #include <sys/ioctl.h>
54 #include <sys/poll.h>
55 #include <sys/socket.h>
56 
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 
61 #include <assert.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67 
68 #include <rpc/rpc.h>
69 #include <rpc/pmap_prot.h>
70 #include <rpc/pmap_clnt.h>
71 #include <rpc/pmap_rmt.h>
72 
73 #ifdef __weak_alias
74 __weak_alias(clnt_broadcast,_clnt_broadcast);
75 __weak_alias(pmap_rmtcall,_pmap_rmtcall);
76 __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args);
77 __weak_alias(xdr_rmtcallres,_xdr_rmtcallres);
78 #endif
79 
80 #define MAX_BROADCAST_SIZE 1400
81 
82 static int getbroadcastnets __P((struct in_addr *, int, char *));
83 
84 static const struct timeval timeout = { 3, 0 };
85 
86 /*
87  * pmapper remote-call-service interface.
88  * This routine is used to call the pmapper remote call service
89  * which will look up a service program in the port maps, and then
90  * remotely call that routine with the given parameters.  This allows
91  * programs to do a lookup and call in one step.
92 */
93 enum clnt_stat
94 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
95     port_ptr)
96 	struct sockaddr_in *addr;
97 	u_long prog, vers, proc;
98 	xdrproc_t xdrargs, xdrres;
99 	caddr_t argsp, resp;
100 	struct timeval tout;
101 	u_long *port_ptr;
102 {
103 	int sock = -1;
104 	CLIENT *client;
105 	struct rmtcallargs a;
106 	struct rmtcallres r;
107 	enum clnt_stat stat;
108 
109 	_DIAGASSERT(addr != NULL);
110 	_DIAGASSERT(port_ptr != NULL);
111 
112 	addr->sin_port = htons(PMAPPORT);
113 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
114 	if (client != (CLIENT *)NULL) {
115 		a.prog = prog;
116 		a.vers = vers;
117 		a.proc = proc;
118 		a.args_ptr = argsp;
119 		a.xdr_args = xdrargs;
120 		r.port_ptr = port_ptr;
121 		r.results_ptr = resp;
122 		r.xdr_results = xdrres;
123 		stat = CLNT_CALL(client, PMAPPROC_CALLIT,
124 		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
125 		    &r, tout);
126 		CLNT_DESTROY(client);
127 	} else {
128 		stat = RPC_FAILED;
129 	}
130 	addr->sin_port = 0;
131 	return (stat);
132 }
133 
134 
135 /*
136  * XDR remote call arguments
137  * written for XDR_ENCODE direction only
138  */
139 bool_t
140 xdr_rmtcall_args(xdrs, cap)
141 	XDR *xdrs;
142 	struct rmtcallargs *cap;
143 {
144 	u_int lenposition, argposition, position;
145 
146 	_DIAGASSERT(xdrs != NULL);
147 	_DIAGASSERT(cap != NULL);
148 
149 	if (xdr_u_long(xdrs, &(cap->prog)) &&
150 	    xdr_u_long(xdrs, &(cap->vers)) &&
151 	    xdr_u_long(xdrs, &(cap->proc))) {
152 		lenposition = XDR_GETPOS(xdrs);
153 		if (! xdr_u_long(xdrs, &(cap->arglen)))
154 		    return (FALSE);
155 		argposition = XDR_GETPOS(xdrs);
156 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
157 		    return (FALSE);
158 		position = XDR_GETPOS(xdrs);
159 		cap->arglen = (u_long)position - (u_long)argposition;
160 		XDR_SETPOS(xdrs, lenposition);
161 		if (! xdr_u_long(xdrs, &(cap->arglen)))
162 		    return (FALSE);
163 		XDR_SETPOS(xdrs, position);
164 		return (TRUE);
165 	}
166 	return (FALSE);
167 }
168 
169 /*
170  * XDR remote call results
171  * written for XDR_DECODE direction only
172  */
173 bool_t
174 xdr_rmtcallres(xdrs, crp)
175 	XDR *xdrs;
176 	struct rmtcallres *crp;
177 {
178 	caddr_t port_ptr;
179 
180 	_DIAGASSERT(xdrs != NULL);
181 	_DIAGASSERT(crp != NULL);
182 
183 	port_ptr = (caddr_t)(void *)crp->port_ptr;
184 	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
185 	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
186 		crp->port_ptr = (u_long *)(void *)port_ptr;
187 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
188 	}
189 	return (FALSE);
190 }
191 
192 
193 /*
194  * The following is kludged-up support for simple rpc broadcasts.
195  * Someday a large, complicated system will replace these trivial
196  * routines which only support udp/ip .
197  */
198 
199 static int
200 getbroadcastnets(addrs, sock, buf)
201 	struct in_addr *addrs;
202 	int sock;  /* any valid socket will do */
203 	char *buf;  /* why allocxate more when we can use existing... */
204 {
205 	struct ifconf ifc;
206         struct ifreq ifreq, *ifr;
207 	struct sockaddr_in *sin;
208         char *cp, *cplim;
209         int i = 0;
210 
211 	_DIAGASSERT(addrs != NULL);
212 	_DIAGASSERT(buf != NULL);
213 
214         ifc.ifc_len = UDPMSGSIZE;
215         ifc.ifc_buf = buf;
216         if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
217                 warn("getbroadcastnets: ioctl (get interface configuration)");
218                 return (0);
219         }
220 #define max(a, b) (a > b ? a : b)
221 #define size(p)	max((p).sa_len, sizeof(p))
222 	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
223 	for (cp = buf; cp < cplim;
224 			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
225 		ifr = (struct ifreq *)(void *)cp;
226 		if (ifr->ifr_addr.sa_family != AF_INET)
227 			continue;
228 		ifreq = *ifr;
229                 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
230                         warn("getbroadcastnets: ioctl (get interface flags)");
231                         continue;
232                 }
233                 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
234 		    (ifreq.ifr_flags & IFF_UP)) {
235 			sin = (struct sockaddr_in *)(void *)&ifr->ifr_addr;
236 #ifdef SIOCGIFBRDADDR   /* 4.3BSD */
237 			if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
238 				addrs[i++] =
239 				    inet_makeaddr(inet_netof(sin->sin_addr),
240 				    (unsigned long)INADDR_ANY);
241 			} else {
242 				addrs[i++] = ((struct sockaddr_in *)(void *)
243 				  &ifreq.ifr_addr)->sin_addr;
244 			}
245 #else /* 4.2 BSD */
246 			addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
247 			    INADDR_ANY);
248 #endif
249 		}
250 	}
251 	return (i);
252 }
253 
254 typedef bool_t (*resultproc_t) __P((caddr_t, struct sockaddr_in *));
255 
256 enum clnt_stat
257 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
258 	u_long		prog;		/* program number */
259 	u_long		vers;		/* version number */
260 	u_long		proc;		/* procedure number */
261 	xdrproc_t	xargs;		/* xdr routine for args */
262 	caddr_t		argsp;		/* pointer to args */
263 	xdrproc_t	xresults;	/* xdr routine for results */
264 	caddr_t		resultsp;	/* pointer to results */
265 	resultproc_t	eachresult;	/* call with each result obtained */
266 {
267 	enum clnt_stat stat;
268 	AUTH *unix_auth = authunix_create_default();
269 	XDR xdr_stream;
270 	XDR *xdrs = &xdr_stream;
271 	int inlen, nets;
272 	socklen_t fromlen;
273 	size_t outlen;
274 	int sock;
275 	int on = 1;
276 	struct pollfd fd;
277 	int i;
278 	bool_t done = FALSE;
279 	u_int32_t xid;
280 	u_long port;
281 	struct in_addr addrs[20];
282 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
283 	struct rmtcallargs a;
284 	struct rmtcallres r;
285 	struct rpc_msg msg;
286 	struct timeval t;
287 	int milliseconds;
288 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
289 
290 	/*
291 	 * initialization: create a socket, a broadcast address, and
292 	 * preserialize the arguments into a send buffer.
293 	 */
294 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
295 		warn("Cannot create socket for broadcast rpc");
296 		stat = RPC_CANTSEND;
297 		goto done_broad;
298 	}
299 #ifdef SO_BROADCAST
300 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
301 		warn("Cannot set socket option SO_BROADCAST");
302 		stat = RPC_CANTSEND;
303 		goto done_broad;
304 	}
305 #endif /* def SO_BROADCAST */
306 	fd.fd = sock;
307 	fd.events = POLLIN;
308 	nets = getbroadcastnets(addrs, sock, inbuf);
309 	memset(&baddr, 0, sizeof (baddr));
310 	baddr.sin_len = sizeof(struct sockaddr_in);
311 	baddr.sin_family = AF_INET;
312 	baddr.sin_port = htons(PMAPPORT);
313 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
314 	(void)gettimeofday(&t, (struct timezone *)0);
315 	msg.rm_xid = xid = (u_int32_t)(getpid() ^ t.tv_sec ^ t.tv_usec);
316 	t.tv_usec = 0;
317 	msg.rm_direction = CALL;
318 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
319 	msg.rm_call.cb_prog = PMAPPROG;
320 	msg.rm_call.cb_vers = PMAPVERS;
321 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
322 	msg.rm_call.cb_cred = unix_auth->ah_cred;
323 	msg.rm_call.cb_verf = unix_auth->ah_verf;
324 	a.prog = prog;
325 	a.vers = vers;
326 	a.proc = proc;
327 	a.xdr_args = xargs;
328 	a.args_ptr = argsp;
329 	r.port_ptr = &port;
330 	r.xdr_results = xresults;
331 	r.results_ptr = resultsp;
332 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
333 	if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
334 		stat = RPC_CANTENCODEARGS;
335 		goto done_broad;
336 	}
337 	outlen = xdr_getpos(xdrs);
338 	xdr_destroy(xdrs);
339 	/*
340 	 * Basic loop: broadcast a packet and wait a while for response(s).
341 	 * The response timeout grows larger per iteration.
342 	 */
343 	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
344 		for (i = 0; i < nets; i++) {
345 			baddr.sin_addr = addrs[i];
346 			if (sendto(sock, outbuf, outlen, 0,
347 				(struct sockaddr *)(void *)&baddr,
348 				sizeof (struct sockaddr)) != outlen) {
349 				warn("Cannot send broadcast packet");
350 				stat = RPC_CANTSEND;
351 				goto done_broad;
352 			}
353 		}
354 		if (eachresult == NULL) {
355 			stat = RPC_SUCCESS;
356 			goto done_broad;
357 		}
358 	recv_again:
359 		milliseconds = (int)(t.tv_sec * 1000 + t.tv_usec / 1000);
360 		msg.acpted_rply.ar_verf = _null_auth;
361 		msg.acpted_rply.ar_results.where = (caddr_t)(void *)&r;
362                 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
363 		switch (poll(&fd, 1, milliseconds)) {
364 
365 		case 0:  /* timed out */
366 			stat = RPC_TIMEDOUT;
367 			continue;
368 
369 		case -1:  /* some kind of error */
370 			if (errno == EINTR)
371 				goto recv_again;
372 			warn("Broadcast poll problem");
373 			stat = RPC_CANTRECV;
374 			goto done_broad;
375 
376 		}  /* end of poll results switch */
377 	try_again:
378 		fromlen = sizeof(struct sockaddr);
379 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
380 			(struct sockaddr *)(void *)&raddr, &fromlen);
381 		if (inlen < 0) {
382 			if (errno == EINTR)
383 				goto try_again;
384 			warn("Cannot receive reply to broadcast");
385 			stat = RPC_CANTRECV;
386 			goto done_broad;
387 		}
388 		if (inlen < sizeof(u_int32_t))
389 			goto recv_again;
390 		/*
391 		 * see if reply transaction id matches sent id.
392 		 * If so, decode the results.
393 		 */
394 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
395 		if (xdr_replymsg(xdrs, &msg)) {
396 			if ((msg.rm_xid == xid) &&
397 				(msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
398 				(msg.acpted_rply.ar_stat == SUCCESS)) {
399 				raddr.sin_port = htons((u_short)port);
400 				done = (*eachresult)(resultsp, &raddr);
401 			}
402 			/* otherwise, we just ignore the errors ... */
403 		} else {
404 #ifdef notdef
405 			/* some kind of deserialization problem ... */
406 			if (msg.rm_xid == xid)
407 				fprintf(stderr,
408 				    "Broadcast deserialization problem");
409 			/* otherwise, just random garbage */
410 #endif
411 		}
412 		xdrs->x_op = XDR_FREE;
413 		msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
414 		(void)xdr_replymsg(xdrs, &msg);
415 		(void)(*xresults)(xdrs, resultsp);
416 		xdr_destroy(xdrs);
417 		if (done) {
418 			stat = RPC_SUCCESS;
419 			goto done_broad;
420 		} else {
421 			goto recv_again;
422 		}
423 	}
424 	stat = RPC_TIMEDOUT;
425 done_broad:
426 	if (sock != -1)
427 		(void)close(sock);
428 	AUTH_DESTROY(unix_auth);
429 	return (stat);
430 }
431