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