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