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