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