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