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