pmap_rmt.c revision 50476
11901Swollman/*
21901Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
31901Swollman * unrestricted use provided that this legend is included on all tape
41901Swollman * media and as a part of the software program in whole or part.  Users
51901Swollman * may copy or modify Sun RPC without charge, but are not authorized
61901Swollman * to license or distribute it to anyone else except as part of a product or
71901Swollman * program developed by the user.
88870Srgrimes *
91901Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
101901Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
111901Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
128870Srgrimes *
131901Swollman * Sun RPC is provided with no support and without any obligation on the
141901Swollman * part of Sun Microsystems, Inc. to assist in its use, correction,
151901Swollman * modification or enhancement.
168870Srgrimes *
171901Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
181901Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
191901Swollman * OR ANY PART THEREOF.
208870Srgrimes *
211901Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue
221901Swollman * or profits or other special, indirect and consequential damages, even if
231901Swollman * Sun has been advised of the possibility of such damages.
248870Srgrimes *
251901Swollman * Sun Microsystems, Inc.
261901Swollman * 2550 Garcia Avenue
271901Swollman * Mountain View, California  94043
281901Swollman */
291901Swollman
301901Swollman#if defined(LIBC_SCCS) && !defined(lint)
311901Swollman/*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/
321901Swollman/*static char *sccsid = "from: @(#)pmap_rmt.c	2.2 88/08/01 4.0 RPCSRC";*/
3350476Speterstatic char *rcsid = "$FreeBSD: head/lib/libc/rpc/pmap_rmt.c 50476 1999-08-28 00:22:10Z peter $";
341901Swollman#endif
351901Swollman
361901Swollman/*
371901Swollman * pmap_rmt.c
381901Swollman * Client interface to pmap rpc service.
391901Swollman * remote call and broadcast service
401901Swollman *
411901Swollman * Copyright (C) 1984, Sun Microsystems, Inc.
421901Swollman */
431901Swollman
441901Swollman#include <rpc/rpc.h>
451901Swollman#include <rpc/pmap_prot.h>
461901Swollman#include <rpc/pmap_clnt.h>
471901Swollman#include <rpc/pmap_rmt.h>
481901Swollman#include <sys/socket.h>
491901Swollman#include <stdio.h>
5021081Speter#include <stdlib.h>
5111666Sphk#include <unistd.h>
521901Swollman#include <errno.h>
5311666Sphk#include <string.h>
541901Swollman#include <net/if.h>
551901Swollman#include <sys/ioctl.h>
561901Swollman#include <arpa/inet.h>
571901Swollman#define MAX_BROADCAST_SIZE 1400
581901Swollman
591901Swollmanstatic struct timeval timeout = { 3, 0 };
601901Swollman
611901Swollman/*
621901Swollman * pmapper remote-call-service interface.
631901Swollman * This routine is used to call the pmapper remote call service
641901Swollman * which will look up a service program in the port maps, and then
651901Swollman * remotely call that routine with the given parameters.  This allows
661901Swollman * programs to do a lookup and call in one step.
671901Swollman*/
681901Swollmanenum clnt_stat
691901Swollmanpmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
701901Swollman	struct sockaddr_in *addr;
711901Swollman	u_long prog, vers, proc;
721901Swollman	xdrproc_t xdrargs, xdrres;
731901Swollman	caddr_t argsp, resp;
741901Swollman	struct timeval tout;
751901Swollman	u_long *port_ptr;
761901Swollman{
771901Swollman	int socket = -1;
781901Swollman	register CLIENT *client;
791901Swollman	struct rmtcallargs a;
801901Swollman	struct rmtcallres r;
811901Swollman	enum clnt_stat stat;
821901Swollman
831901Swollman	addr->sin_port = htons(PMAPPORT);
841901Swollman	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
851901Swollman	if (client != (CLIENT *)NULL) {
861901Swollman		a.prog = prog;
871901Swollman		a.vers = vers;
881901Swollman		a.proc = proc;
891901Swollman		a.args_ptr = argsp;
901901Swollman		a.xdr_args = xdrargs;
911901Swollman		r.port_ptr = port_ptr;
921901Swollman		r.results_ptr = resp;
931901Swollman		r.xdr_results = xdrres;
941901Swollman		stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
951901Swollman		    xdr_rmtcallres, &r, tout);
961901Swollman		CLNT_DESTROY(client);
971901Swollman	} else {
981901Swollman		stat = RPC_FAILED;
991901Swollman	}
10021081Speter	if (socket != -1)
10121081Speter		(void)close(socket);
1021901Swollman	addr->sin_port = 0;
1031901Swollman	return (stat);
1041901Swollman}
1051901Swollman
1061901Swollman
1071901Swollman/*
1081901Swollman * XDR remote call arguments
1091901Swollman * written for XDR_ENCODE direction only
1101901Swollman */
1111901Swollmanbool_t
1121901Swollmanxdr_rmtcall_args(xdrs, cap)
1131901Swollman	register XDR *xdrs;
1141901Swollman	register struct rmtcallargs *cap;
1151901Swollman{
1161901Swollman	u_int lenposition, argposition, position;
1171901Swollman
1181901Swollman	if (xdr_u_long(xdrs, &(cap->prog)) &&
1191901Swollman	    xdr_u_long(xdrs, &(cap->vers)) &&
1201901Swollman	    xdr_u_long(xdrs, &(cap->proc))) {
1211901Swollman		lenposition = XDR_GETPOS(xdrs);
1221901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1231901Swollman		    return (FALSE);
1241901Swollman		argposition = XDR_GETPOS(xdrs);
1251901Swollman		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
1261901Swollman		    return (FALSE);
1271901Swollman		position = XDR_GETPOS(xdrs);
1281901Swollman		cap->arglen = (u_long)position - (u_long)argposition;
1291901Swollman		XDR_SETPOS(xdrs, lenposition);
1301901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1311901Swollman		    return (FALSE);
1321901Swollman		XDR_SETPOS(xdrs, position);
1331901Swollman		return (TRUE);
1341901Swollman	}
1351901Swollman	return (FALSE);
1361901Swollman}
1371901Swollman
1381901Swollman/*
1391901Swollman * XDR remote call results
1401901Swollman * written for XDR_DECODE direction only
1411901Swollman */
1421901Swollmanbool_t
1431901Swollmanxdr_rmtcallres(xdrs, crp)
1441901Swollman	register XDR *xdrs;
1451901Swollman	register struct rmtcallres *crp;
1461901Swollman{
1471901Swollman	caddr_t port_ptr;
1481901Swollman
1491901Swollman	port_ptr = (caddr_t)crp->port_ptr;
1501901Swollman	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
1511901Swollman	    xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
1521901Swollman		crp->port_ptr = (u_long *)port_ptr;
1531901Swollman		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
1541901Swollman	}
1551901Swollman	return (FALSE);
1561901Swollman}
1571901Swollman
1581901Swollman
1591901Swollman/*
1601901Swollman * The following is kludged-up support for simple rpc broadcasts.
1618870Srgrimes * Someday a large, complicated system will replace these trivial
1621901Swollman * routines which only support udp/ip .
1631901Swollman */
1641901Swollman
1651901Swollmanstatic int
1661901Swollmangetbroadcastnets(addrs, sock, buf)
1671901Swollman	struct in_addr *addrs;
1681901Swollman	int sock;  /* any valid socket will do */
1691901Swollman	char *buf;  /* why allocxate more when we can use existing... */
1701901Swollman{
1711901Swollman	struct ifconf ifc;
17221081Speter	struct ifreq ifreq, *ifr;
1731901Swollman	struct sockaddr_in *sin;
17414659Sguido	struct	in_addr addr;
17521081Speter	char *cp, *cplim;
17621081Speter	int n, i = 0;
1771901Swollman
1781901Swollman        ifc.ifc_len = UDPMSGSIZE;
1791901Swollman        ifc.ifc_buf = buf;
1801901Swollman        if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1811901Swollman                perror("broadcast: ioctl (get interface configuration)");
1821901Swollman                return (0);
1831901Swollman        }
1841901Swollman#define max(a, b) (a > b ? a : b)
1851901Swollman#define size(p)	max((p).sa_len, sizeof(p))
1861901Swollman	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
1871901Swollman	for (cp = buf; cp < cplim;
1881901Swollman			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
1891901Swollman		ifr = (struct ifreq *)cp;
1901901Swollman		if (ifr->ifr_addr.sa_family != AF_INET)
1911901Swollman			continue;
1921901Swollman		ifreq = *ifr;
1931901Swollman                if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
1941901Swollman                        perror("broadcast: ioctl (get interface flags)");
1951901Swollman                        continue;
1961901Swollman                }
1971901Swollman                if ((ifreq.ifr_flags & IFF_BROADCAST) &&
1981901Swollman		    (ifreq.ifr_flags & IFF_UP)) {
1991901Swollman			sin = (struct sockaddr_in *)&ifr->ifr_addr;
2001901Swollman#ifdef SIOCGIFBRDADDR   /* 4.3BSD */
2011901Swollman			if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
20214659Sguido				addr =
2031901Swollman				    inet_makeaddr(inet_netof(sin->sin_addr),
2041901Swollman				    INADDR_ANY);
2051901Swollman			} else {
20614659Sguido				addr = ((struct sockaddr_in*)
2071901Swollman				  &ifreq.ifr_addr)->sin_addr;
2081901Swollman			}
2091901Swollman#else /* 4.2 BSD */
21014659Sguido			addr = inet_makeaddr(inet_netof(sin->sin_addr),
2111901Swollman			    INADDR_ANY);
2121901Swollman#endif
21314659Sguido			for (n=i-1; n>=0; n--) {
21414659Sguido				if (addr.s_addr == addrs[n].s_addr)
21514659Sguido					break;
21614659Sguido			}
21714659Sguido			if (n<0) {
21814659Sguido				addrs[i++] = addr;
21914659Sguido			}
2201901Swollman		}
2211901Swollman	}
2221901Swollman	return (i);
2231901Swollman}
2241901Swollman
2251901Swollmantypedef bool_t (*resultproc_t)();
2261901Swollman
2278870Srgrimesenum clnt_stat
2281901Swollmanclnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
2291901Swollman	u_long		prog;		/* program number */
2301901Swollman	u_long		vers;		/* version number */
2311901Swollman	u_long		proc;		/* procedure number */
2321901Swollman	xdrproc_t	xargs;		/* xdr routine for args */
2331901Swollman	caddr_t		argsp;		/* pointer to args */
2341901Swollman	xdrproc_t	xresults;	/* xdr routine for results */
2351901Swollman	caddr_t		resultsp;	/* pointer to results */
2361901Swollman	resultproc_t	eachresult;	/* call with each result obtained */
2371901Swollman{
2381901Swollman	enum clnt_stat stat;
2391901Swollman	AUTH *unix_auth = authunix_create_default();
2401901Swollman	XDR xdr_stream;
2411901Swollman	register XDR *xdrs = &xdr_stream;
2421901Swollman	int outlen, inlen, fromlen, nets;
2431901Swollman	register int sock;
2441901Swollman	int on = 1;
24521081Speter	fd_set *fds, readfds;
2461901Swollman	register int i;
2471901Swollman	bool_t done = FALSE;
2481901Swollman	register u_long xid;
2491901Swollman	u_long port;
2501901Swollman	struct in_addr addrs[20];
2511901Swollman	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
2521901Swollman	struct rmtcallargs a;
2531901Swollman	struct rmtcallres r;
2541901Swollman	struct rpc_msg msg;
25521081Speter	struct timeval t, tv;
2561901Swollman	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
25721081Speter	static u_int32_t disrupt;
2581901Swollman
25921081Speter	if (disrupt == 0)
26021081Speter		disrupt = (u_int32_t)(long)resultsp;
26121081Speter
2621901Swollman	/*
2631901Swollman	 * initialization: create a socket, a broadcast address, and
2641901Swollman	 * preserialize the arguments into a send buffer.
2651901Swollman	 */
2661901Swollman	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
2671901Swollman		perror("Cannot create socket for broadcast rpc");
2681901Swollman		stat = RPC_CANTSEND;
2691901Swollman		goto done_broad;
2701901Swollman	}
2711901Swollman#ifdef SO_BROADCAST
2721901Swollman	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
2731901Swollman		perror("Cannot set socket option SO_BROADCAST");
2741901Swollman		stat = RPC_CANTSEND;
2751901Swollman		goto done_broad;
2761901Swollman	}
2771901Swollman#endif /* def SO_BROADCAST */
27821081Speter	if (sock + 1 > FD_SETSIZE) {
27921081Speter		int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
28021081Speter		fds = (fd_set *)malloc(bytes);
28121081Speter		if (fds == NULL) {
28221081Speter			stat = RPC_CANTSEND;
28321081Speter			goto done_broad;
28421081Speter		}
28521081Speter		memset(fds, 0, bytes);
28621081Speter	} else {
28721081Speter		fds = &readfds;
28821081Speter		FD_ZERO(fds);
28921081Speter	}
29021081Speter
2911901Swollman	nets = getbroadcastnets(addrs, sock, inbuf);
29221081Speter	memset(&baddr, 0, sizeof (baddr));
29317540Speter	baddr.sin_len = sizeof(struct sockaddr_in);
2941901Swollman	baddr.sin_family = AF_INET;
2951901Swollman	baddr.sin_port = htons(PMAPPORT);
2961901Swollman	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
2971901Swollman	(void)gettimeofday(&t, (struct timezone *)0);
29821081Speter	msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
2991901Swollman	t.tv_usec = 0;
3001901Swollman	msg.rm_direction = CALL;
3011901Swollman	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
3021901Swollman	msg.rm_call.cb_prog = PMAPPROG;
3031901Swollman	msg.rm_call.cb_vers = PMAPVERS;
3041901Swollman	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
3051901Swollman	msg.rm_call.cb_cred = unix_auth->ah_cred;
3061901Swollman	msg.rm_call.cb_verf = unix_auth->ah_verf;
3071901Swollman	a.prog = prog;
3081901Swollman	a.vers = vers;
3091901Swollman	a.proc = proc;
3101901Swollman	a.xdr_args = xargs;
3111901Swollman	a.args_ptr = argsp;
3121901Swollman	r.port_ptr = &port;
3131901Swollman	r.xdr_results = xresults;
3141901Swollman	r.results_ptr = resultsp;
3151901Swollman	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
3161901Swollman	if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
3171901Swollman		stat = RPC_CANTENCODEARGS;
3181901Swollman		goto done_broad;
3191901Swollman	}
3201901Swollman	outlen = (int)xdr_getpos(xdrs);
3211901Swollman	xdr_destroy(xdrs);
3221901Swollman	/*
3231901Swollman	 * Basic loop: broadcast a packet and wait a while for response(s).
3241901Swollman	 * The response timeout grows larger per iteration.
32521081Speter	 *
32621081Speter	 * XXX This will loop about 5 times the stop. If there are
32721081Speter	 * lots of signals being received by the process it will quit
32821081Speter	 * send them all in one quick burst, not paying attention to
32921081Speter	 * the intended function of sending them slowly over half a
33021081Speter	 * minute or so
3311901Swollman	 */
3321901Swollman	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
3331901Swollman		for (i = 0; i < nets; i++) {
3341901Swollman			baddr.sin_addr = addrs[i];
3351901Swollman			if (sendto(sock, outbuf, outlen, 0,
3361901Swollman				(struct sockaddr *)&baddr,
3371901Swollman				sizeof (struct sockaddr)) != outlen) {
3381901Swollman				perror("Cannot send broadcast packet");
3391901Swollman				stat = RPC_CANTSEND;
3401901Swollman				goto done_broad;
3411901Swollman			}
3421901Swollman		}
3431901Swollman		if (eachresult == NULL) {
3441901Swollman			stat = RPC_SUCCESS;
3451901Swollman			goto done_broad;
3461901Swollman		}
3471901Swollman	recv_again:
3481901Swollman		msg.acpted_rply.ar_verf = _null_auth;
3491901Swollman		msg.acpted_rply.ar_results.where = (caddr_t)&r;
35021081Speter		msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
35121081Speter		/* XXX we know the other bits are still clear */
35221081Speter		FD_SET(sock, fds);
35321081Speter		tv = t;		/* for select() that copies back */
35421081Speter		switch (select(sock + 1, fds, NULL, NULL, &tv)) {
3551901Swollman
3561901Swollman		case 0:  /* timed out */
3571901Swollman			stat = RPC_TIMEDOUT;
3581901Swollman			continue;
3591901Swollman
3601901Swollman		case -1:  /* some kind of error */
3611901Swollman			if (errno == EINTR)
3621901Swollman				goto recv_again;
3631901Swollman			perror("Broadcast select problem");
3641901Swollman			stat = RPC_CANTRECV;
3651901Swollman			goto done_broad;
3661901Swollman
3671901Swollman		}  /* end of select results switch */
3681901Swollman	try_again:
3691901Swollman		fromlen = sizeof(struct sockaddr);
3701901Swollman		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
3711901Swollman			(struct sockaddr *)&raddr, &fromlen);
3721901Swollman		if (inlen < 0) {
3731901Swollman			if (errno == EINTR)
3741901Swollman				goto try_again;
3751901Swollman			perror("Cannot receive reply to broadcast");
3761901Swollman			stat = RPC_CANTRECV;
3771901Swollman			goto done_broad;
3781901Swollman		}
37921081Speter		if (inlen < sizeof(u_int32_t))
3801901Swollman			goto recv_again;
3811901Swollman		/*
3821901Swollman		 * see if reply transaction id matches sent id.
3831901Swollman		 * If so, decode the results.
3841901Swollman		 */
3851901Swollman		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
3861901Swollman		if (xdr_replymsg(xdrs, &msg)) {
3871901Swollman			if ((msg.rm_xid == xid) &&
3881901Swollman				(msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
3891901Swollman				(msg.acpted_rply.ar_stat == SUCCESS)) {
3901901Swollman				raddr.sin_port = htons((u_short)port);
3911901Swollman				done = (*eachresult)(resultsp, &raddr);
3921901Swollman			}
3931901Swollman			/* otherwise, we just ignore the errors ... */
3941901Swollman		}
3951901Swollman		xdrs->x_op = XDR_FREE;
3961901Swollman		msg.acpted_rply.ar_results.proc = xdr_void;
3971901Swollman		(void)xdr_replymsg(xdrs, &msg);
3981901Swollman		(void)(*xresults)(xdrs, resultsp);
3991901Swollman		xdr_destroy(xdrs);
4001901Swollman		if (done) {
4011901Swollman			stat = RPC_SUCCESS;
4021901Swollman			goto done_broad;
4031901Swollman		} else {
4041901Swollman			goto recv_again;
4051901Swollman		}
4061901Swollman	}
4071901Swollmandone_broad:
40821081Speter	if (fds != &readfds)
40921081Speter		free(fds);
41021081Speter	if (sock >= 0)
41121081Speter		(void)close(sock);
4121901Swollman	AUTH_DESTROY(unix_auth);
4131901Swollman	return (stat);
4141901Swollman}
4151901Swollman
416