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