xref: /netbsd-src/lib/libc/rpc/pmap_rmt.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: pmap_rmt.c,v 1.17 1998/11/15 17:32:43 christos 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.17 1998/11/15 17:32:43 christos 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 <err.h>
62 #include <errno.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <unistd.h>
66 
67 #include <rpc/rpc.h>
68 #include <rpc/pmap_prot.h>
69 #include <rpc/pmap_clnt.h>
70 #include <rpc/pmap_rmt.h>
71 
72 #ifdef __weak_alias
73 __weak_alias(clnt_broadcast,_clnt_broadcast);
74 __weak_alias(pmap_rmtcall,_pmap_rmtcall);
75 __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args);
76 __weak_alias(xdr_rmtcallres,_xdr_rmtcallres);
77 #endif
78 
79 #define MAX_BROADCAST_SIZE 1400
80 
81 static int getbroadcastnets __P((struct in_addr *, int, char *));
82 
83 static const struct timeval timeout = { 3, 0 };
84 
85 /*
86  * pmapper remote-call-service interface.
87  * This routine is used to call the pmapper remote call service
88  * which will look up a service program in the port maps, and then
89  * remotely call that routine with the given parameters.  This allows
90  * programs to do a lookup and call in one step.
91 */
92 enum clnt_stat
93 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
94 	struct sockaddr_in *addr;
95 	u_long prog, vers, proc;
96 	xdrproc_t xdrargs, xdrres;
97 	caddr_t argsp, resp;
98 	struct timeval tout;
99 	u_long *port_ptr;
100 {
101 	int sock = -1;
102 	CLIENT *client;
103 	struct rmtcallargs a;
104 	struct rmtcallres r;
105 	enum clnt_stat stat;
106 
107 	addr->sin_port = htons(PMAPPORT);
108 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
109 	if (client != (CLIENT *)NULL) {
110 		a.prog = prog;
111 		a.vers = vers;
112 		a.proc = proc;
113 		a.args_ptr = argsp;
114 		a.xdr_args = xdrargs;
115 		r.port_ptr = port_ptr;
116 		r.results_ptr = resp;
117 		r.xdr_results = xdrres;
118 		stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
119 		    xdr_rmtcallres, &r, tout);
120 		CLNT_DESTROY(client);
121 	} else {
122 		stat = RPC_FAILED;
123 	}
124 	(void)close(sock);
125 	addr->sin_port = 0;
126 	return (stat);
127 }
128 
129 
130 /*
131  * XDR remote call arguments
132  * written for XDR_ENCODE direction only
133  */
134 bool_t
135 xdr_rmtcall_args(xdrs, cap)
136 	XDR *xdrs;
137 	struct rmtcallargs *cap;
138 {
139 	u_int lenposition, argposition, position;
140 
141 	if (xdr_u_long(xdrs, &(cap->prog)) &&
142 	    xdr_u_long(xdrs, &(cap->vers)) &&
143 	    xdr_u_long(xdrs, &(cap->proc))) {
144 		lenposition = XDR_GETPOS(xdrs);
145 		if (! xdr_u_long(xdrs, &(cap->arglen)))
146 		    return (FALSE);
147 		argposition = XDR_GETPOS(xdrs);
148 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
149 		    return (FALSE);
150 		position = XDR_GETPOS(xdrs);
151 		cap->arglen = (u_long)position - (u_long)argposition;
152 		XDR_SETPOS(xdrs, lenposition);
153 		if (! xdr_u_long(xdrs, &(cap->arglen)))
154 		    return (FALSE);
155 		XDR_SETPOS(xdrs, position);
156 		return (TRUE);
157 	}
158 	return (FALSE);
159 }
160 
161 /*
162  * XDR remote call results
163  * written for XDR_DECODE direction only
164  */
165 bool_t
166 xdr_rmtcallres(xdrs, crp)
167 	XDR *xdrs;
168 	struct rmtcallres *crp;
169 {
170 	caddr_t port_ptr;
171 
172 	port_ptr = (caddr_t)(void *)crp->port_ptr;
173 	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
174 	    xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
175 		crp->port_ptr = (u_long *)(void *)port_ptr;
176 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
177 	}
178 	return (FALSE);
179 }
180 
181 
182 /*
183  * The following is kludged-up support for simple rpc broadcasts.
184  * Someday a large, complicated system will replace these trivial
185  * routines which only support udp/ip .
186  */
187 
188 static int
189 getbroadcastnets(addrs, sock, buf)
190 	struct in_addr *addrs;
191 	int sock;  /* any valid socket will do */
192 	char *buf;  /* why allocxate more when we can use existing... */
193 {
194 	struct ifconf ifc;
195         struct ifreq ifreq, *ifr;
196 	struct sockaddr_in *sin;
197         char *cp, *cplim;
198         int i = 0;
199 
200         ifc.ifc_len = UDPMSGSIZE;
201         ifc.ifc_buf = buf;
202         if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
203                 warnx("getbroadcastnets: ioctl (get interface configuration)");
204                 return (0);
205         }
206 #define max(a, b) (a > b ? a : b)
207 #define size(p)	max((p).sa_len, sizeof(p))
208 	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
209 	for (cp = buf; cp < cplim;
210 			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
211 		ifr = (struct ifreq *)(void *)cp;
212 		if (ifr->ifr_addr.sa_family != AF_INET)
213 			continue;
214 		ifreq = *ifr;
215                 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
216                         warnx("getbroadcastnets: ioctl (get interface flags)");
217                         continue;
218                 }
219                 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
220 		    (ifreq.ifr_flags & IFF_UP)) {
221 			sin = (struct sockaddr_in *)(void *)&ifr->ifr_addr;
222 #ifdef SIOCGIFBRDADDR   /* 4.3BSD */
223 			if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
224 				addrs[i++] =
225 				    inet_makeaddr(inet_netof(sin->sin_addr),
226 				    (unsigned long)INADDR_ANY);
227 			} else {
228 				addrs[i++] = ((struct sockaddr_in *)(void *)
229 				  &ifreq.ifr_addr)->sin_addr;
230 			}
231 #else /* 4.2 BSD */
232 			addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
233 			    INADDR_ANY);
234 #endif
235 		}
236 	}
237 	return (i);
238 }
239 
240 typedef bool_t (*resultproc_t) __P((caddr_t, struct sockaddr_in *));
241 
242 enum clnt_stat
243 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
244 	u_long		prog;		/* program number */
245 	u_long		vers;		/* version number */
246 	u_long		proc;		/* procedure number */
247 	xdrproc_t	xargs;		/* xdr routine for args */
248 	caddr_t		argsp;		/* pointer to args */
249 	xdrproc_t	xresults;	/* xdr routine for results */
250 	caddr_t		resultsp;	/* pointer to results */
251 	resultproc_t	eachresult;	/* call with each result obtained */
252 {
253 	enum clnt_stat stat;
254 	AUTH *unix_auth = authunix_create_default();
255 	XDR xdr_stream;
256 	XDR *xdrs = &xdr_stream;
257 	int inlen, fromlen, nets;
258 	size_t outlen;
259 	int sock;
260 	int on = 1;
261 	struct pollfd fd;
262 	int i;
263 	bool_t done = FALSE;
264 	u_int32_t xid;
265 	u_long port;
266 	struct in_addr addrs[20];
267 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
268 	struct rmtcallargs a;
269 	struct rmtcallres r;
270 	struct rpc_msg msg;
271 	struct timeval t;
272 	int milliseconds;
273 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
274 
275 	/*
276 	 * initialization: create a socket, a broadcast address, and
277 	 * preserialize the arguments into a send buffer.
278 	 */
279 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
280 		warnx("Cannot create socket for broadcast rpc");
281 		stat = RPC_CANTSEND;
282 		goto done_broad;
283 	}
284 #ifdef SO_BROADCAST
285 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
286 		warnx("Cannot set socket option SO_BROADCAST");
287 		stat = RPC_CANTSEND;
288 		goto done_broad;
289 	}
290 #endif /* def SO_BROADCAST */
291 	fd.fd = sock;
292 	fd.events = POLLIN;
293 	nets = getbroadcastnets(addrs, sock, inbuf);
294 	memset(&baddr, 0, sizeof (baddr));
295 	baddr.sin_len = sizeof(struct sockaddr_in);
296 	baddr.sin_family = AF_INET;
297 	baddr.sin_port = htons(PMAPPORT);
298 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
299 	(void)gettimeofday(&t, (struct timezone *)0);
300 	msg.rm_xid = xid = (u_int32_t)(getpid() ^ t.tv_sec ^ t.tv_usec);
301 	t.tv_usec = 0;
302 	msg.rm_direction = CALL;
303 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
304 	msg.rm_call.cb_prog = PMAPPROG;
305 	msg.rm_call.cb_vers = PMAPVERS;
306 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
307 	msg.rm_call.cb_cred = unix_auth->ah_cred;
308 	msg.rm_call.cb_verf = unix_auth->ah_verf;
309 	a.prog = prog;
310 	a.vers = vers;
311 	a.proc = proc;
312 	a.xdr_args = xargs;
313 	a.args_ptr = argsp;
314 	r.port_ptr = &port;
315 	r.xdr_results = xresults;
316 	r.results_ptr = resultsp;
317 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
318 	if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
319 		stat = RPC_CANTENCODEARGS;
320 		goto done_broad;
321 	}
322 	outlen = xdr_getpos(xdrs);
323 	xdr_destroy(xdrs);
324 	/*
325 	 * Basic loop: broadcast a packet and wait a while for response(s).
326 	 * The response timeout grows larger per iteration.
327 	 */
328 	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
329 		for (i = 0; i < nets; i++) {
330 			baddr.sin_addr = addrs[i];
331 			if (sendto(sock, outbuf, outlen, 0,
332 				(struct sockaddr *)(void *)&baddr,
333 				sizeof (struct sockaddr)) != outlen) {
334 				warnx("Cannot send broadcast packet");
335 				stat = RPC_CANTSEND;
336 				goto done_broad;
337 			}
338 		}
339 		if (eachresult == NULL) {
340 			stat = RPC_SUCCESS;
341 			goto done_broad;
342 		}
343 	recv_again:
344 		milliseconds = (int)(t.tv_sec * 1000 + t.tv_usec / 1000);
345 		msg.acpted_rply.ar_verf = _null_auth;
346 		msg.acpted_rply.ar_results.where = (caddr_t)(void *)&r;
347                 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
348 		switch (poll(&fd, 1, milliseconds)) {
349 
350 		case 0:  /* timed out */
351 			stat = RPC_TIMEDOUT;
352 			continue;
353 
354 		case -1:  /* some kind of error */
355 			if (errno == EINTR)
356 				goto recv_again;
357 			warnx("Broadcast poll problem");
358 			stat = RPC_CANTRECV;
359 			goto done_broad;
360 
361 		}  /* end of poll results switch */
362 	try_again:
363 		fromlen = sizeof(struct sockaddr);
364 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
365 			(struct sockaddr *)(void *)&raddr, &fromlen);
366 		if (inlen < 0) {
367 			if (errno == EINTR)
368 				goto try_again;
369 			warnx("Cannot receive reply to broadcast");
370 			stat = RPC_CANTRECV;
371 			goto done_broad;
372 		}
373 		if (inlen < sizeof(u_int32_t))
374 			goto recv_again;
375 		/*
376 		 * see if reply transaction id matches sent id.
377 		 * If so, decode the results.
378 		 */
379 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
380 		if (xdr_replymsg(xdrs, &msg)) {
381 			if ((msg.rm_xid == xid) &&
382 				(msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
383 				(msg.acpted_rply.ar_stat == SUCCESS)) {
384 				raddr.sin_port = htons((u_short)port);
385 				done = (*eachresult)(resultsp, &raddr);
386 			}
387 			/* otherwise, we just ignore the errors ... */
388 		} else {
389 #ifdef notdef
390 			/* some kind of deserialization problem ... */
391 			if (msg.rm_xid == xid)
392 				fprintf(stderr, "Broadcast deserialization problem");
393 			/* otherwise, just random garbage */
394 #endif
395 		}
396 		xdrs->x_op = XDR_FREE;
397 		msg.acpted_rply.ar_results.proc = xdr_void;
398 		(void)xdr_replymsg(xdrs, &msg);
399 		(void)(*xresults)(xdrs, resultsp);
400 		xdr_destroy(xdrs);
401 		if (done) {
402 			stat = RPC_SUCCESS;
403 			goto done_broad;
404 		} else {
405 			goto recv_again;
406 		}
407 	}
408 	stat = RPC_TIMEDOUT;
409 done_broad:
410 	(void)close(sock);
411 	AUTH_DESTROY(unix_auth);
412 	return (stat);
413 }
414