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