NetBSD/lib/libc/rpc/pmap_rmt.c

445 lines
12 KiB
C
Raw Normal View History

/* $NetBSD: pmap_rmt.c,v 1.25 2000/02/18 08:26:01 itojun Exp $ */
1995-02-25 06:01:33 +03:00
1993-10-07 10:29:33 +03:00
/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
* unrestricted use provided that this legend is included on all tape
* media and as a part of the software program in whole or part. Users
* may copy or modify Sun RPC without charge, but are not authorized
* to license or distribute it to anyone else except as part of a product or
* program developed by the user.
*
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* Sun RPC is provided with no support and without any obligation on the
* part of Sun Microsystems, Inc. to assist in its use, correction,
* modification or enhancement.
*
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
* OR ANY PART THEREOF.
*
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Sun has been advised of the possibility of such damages.
*
* Sun Microsystems, Inc.
* 2550 Garcia Avenue
* Mountain View, California 94043
*/
#include <sys/cdefs.h>
1993-10-07 10:29:33 +03:00
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char *sccsid = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";
#else
__RCSID("$NetBSD: pmap_rmt.c,v 1.25 2000/02/18 08:26:01 itojun Exp $");
#endif
1993-10-07 10:29:33 +03:00
#endif
/*
* pmap_rmt.c
* Client interface to pmap rpc service.
* remote call and broadcast service
*
* Copyright (C) 1984, Sun Microsystems, Inc.
*/
#include "namespace.h"
1996-12-17 06:55:20 +03:00
#include <sys/types.h>
#include <sys/ioctl.h>
1996-12-17 06:55:20 +03:00
#include <sys/poll.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <rpc/pmap_prot.h>
#include <rpc/pmap_clnt.h>
#include <rpc/pmap_rmt.h>
#ifdef __weak_alias
__weak_alias(clnt_broadcast,_clnt_broadcast)
__weak_alias(pmap_rmtcall,_pmap_rmtcall)
__weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
__weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
#endif
1993-10-07 10:29:33 +03:00
#define MAX_BROADCAST_SIZE 1400
static int getbroadcastnets __P((struct in_addr *, int, char *));
1993-10-07 10:29:33 +03:00
1998-07-26 18:13:00 +04:00
static const struct timeval timeout = { 3, 0 };
1993-10-07 10:29:33 +03:00
/*
* pmapper remote-call-service interface.
* This routine is used to call the pmapper remote call service
* which will look up a service program in the port maps, and then
* remotely call that routine with the given parameters. This allows
* programs to do a lookup and call in one step.
*/
enum clnt_stat
pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
port_ptr)
1993-10-07 10:29:33 +03:00
struct sockaddr_in *addr;
u_long prog, vers, proc;
1993-10-07 10:29:33 +03:00
xdrproc_t xdrargs, xdrres;
caddr_t argsp, resp;
struct timeval tout;
u_long *port_ptr;
1993-10-07 10:29:33 +03:00
{
1998-11-15 20:32:41 +03:00
int sock = -1;
CLIENT *client;
1993-10-07 10:29:33 +03:00
struct rmtcallargs a;
struct rmtcallres r;
enum clnt_stat stat;
_DIAGASSERT(addr != NULL);
_DIAGASSERT(port_ptr != NULL);
1993-10-07 10:29:33 +03:00
addr->sin_port = htons(PMAPPORT);
1998-11-15 20:32:41 +03:00
client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
1993-10-07 10:29:33 +03:00
if (client != (CLIENT *)NULL) {
a.prog = prog;
a.vers = vers;
a.proc = proc;
a.args_ptr = argsp;
a.xdr_args = xdrargs;
r.port_ptr = port_ptr;
r.results_ptr = resp;
r.xdr_results = xdrres;
1999-01-31 23:45:31 +03:00
stat = CLNT_CALL(client, PMAPPROC_CALLIT,
(xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
&r, tout);
1993-10-07 10:29:33 +03:00
CLNT_DESTROY(client);
} else {
stat = RPC_FAILED;
}
addr->sin_port = 0;
return (stat);
}
/*
* XDR remote call arguments
* written for XDR_ENCODE direction only
*/
bool_t
xdr_rmtcall_args(xdrs, cap)
XDR *xdrs;
struct rmtcallargs *cap;
1993-10-07 10:29:33 +03:00
{
u_int lenposition, argposition, position;
1993-10-07 10:29:33 +03:00
_DIAGASSERT(xdrs != NULL);
_DIAGASSERT(cap != NULL);
if (xdr_u_long(xdrs, &(cap->prog)) &&
xdr_u_long(xdrs, &(cap->vers)) &&
xdr_u_long(xdrs, &(cap->proc))) {
1993-10-07 10:29:33 +03:00
lenposition = XDR_GETPOS(xdrs);
if (! xdr_u_long(xdrs, &(cap->arglen)))
1993-10-07 10:29:33 +03:00
return (FALSE);
argposition = XDR_GETPOS(xdrs);
if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
return (FALSE);
position = XDR_GETPOS(xdrs);
cap->arglen = (u_long)position - (u_long)argposition;
1993-10-07 10:29:33 +03:00
XDR_SETPOS(xdrs, lenposition);
if (! xdr_u_long(xdrs, &(cap->arglen)))
1993-10-07 10:29:33 +03:00
return (FALSE);
XDR_SETPOS(xdrs, position);
return (TRUE);
}
return (FALSE);
}
/*
* XDR remote call results
* written for XDR_DECODE direction only
*/
bool_t
xdr_rmtcallres(xdrs, crp)
XDR *xdrs;
struct rmtcallres *crp;
1993-10-07 10:29:33 +03:00
{
caddr_t port_ptr;
_DIAGASSERT(xdrs != NULL);
_DIAGASSERT(crp != NULL);
1998-11-15 20:32:41 +03:00
port_ptr = (caddr_t)(void *)crp->port_ptr;
if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
1999-01-31 23:45:31 +03:00
(xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
1998-11-15 20:32:41 +03:00
crp->port_ptr = (u_long *)(void *)port_ptr;
1993-10-07 10:29:33 +03:00
return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
}
return (FALSE);
}
/*
* The following is kludged-up support for simple rpc broadcasts.
* Someday a large, complicated system will replace these trivial
* routines which only support udp/ip .
*/
static int
getbroadcastnets(addrs, sock, buf)
struct in_addr *addrs;
int sock; /* any valid socket will do */
char *buf; /* why allocxate more when we can use existing... */
{
struct ifconf ifc;
struct ifreq ifreq, *ifr;
struct sockaddr_in *sin;
char *cp, *cplim;
int i = 0;
size_t siz;
char ifrbuf[8192];
1993-10-07 10:29:33 +03:00
_DIAGASSERT(addrs != NULL);
_DIAGASSERT(buf != NULL);
1993-10-07 10:29:33 +03:00
ifc.ifc_len = UDPMSGSIZE;
ifc.ifc_buf = buf;
1998-11-15 20:32:41 +03:00
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
warn("getbroadcastnets: ioctl (get interface configuration)");
1993-10-07 10:29:33 +03:00
return (0);
}
#define max(a, b) (a > b ? a : b)
#define size(p) max((p).sa_len, sizeof(p))
cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
for (cp = buf; cp < cplim; cp += siz) {
1998-11-15 20:32:41 +03:00
ifr = (struct ifreq *)(void *)cp;
memcpy(ifrbuf, ifr, sizeof(*ifr));
siz = ((struct ifreq *)ifrbuf)->ifr_addr.sa_len;
if (siz < sizeof(ifr->ifr_addr))
siz = sizeof(ifr->ifr_addr);
siz += sizeof (ifr->ifr_name);
if (siz > sizeof(ifrbuf)) {
/* ifr too big */
break;
}
memcpy(ifrbuf, ifr, siz);
ifr = (struct ifreq *)ifrbuf;
1993-10-07 10:29:33 +03:00
if (ifr->ifr_addr.sa_family != AF_INET)
continue;
ifreq = *ifr;
if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
warn("getbroadcastnets: ioctl (get interface flags)");
continue;
}
if ((ifreq.ifr_flags & IFF_BROADCAST) &&
1993-10-07 10:29:33 +03:00
(ifreq.ifr_flags & IFF_UP)) {
1998-11-15 20:32:41 +03:00
sin = (struct sockaddr_in *)(void *)&ifr->ifr_addr;
1993-10-07 10:29:33 +03:00
#ifdef SIOCGIFBRDADDR /* 4.3BSD */
ifreq = *ifr;
1998-11-15 20:32:41 +03:00
if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
1993-10-07 10:29:33 +03:00
addrs[i++] =
inet_makeaddr(inet_netof(sin->sin_addr),
1998-11-15 20:32:41 +03:00
(unsigned long)INADDR_ANY);
1993-10-07 10:29:33 +03:00
} else {
1998-11-15 20:32:41 +03:00
addrs[i++] = ((struct sockaddr_in *)(void *)
1993-10-07 10:29:33 +03:00
&ifreq.ifr_addr)->sin_addr;
}
#else /* 4.2 BSD */
addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
INADDR_ANY);
#endif
}
}
return (i);
}
typedef bool_t (*resultproc_t) __P((caddr_t, struct sockaddr_in *));
1993-10-07 10:29:33 +03:00
enum clnt_stat
clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
u_long prog; /* program number */
u_long vers; /* version number */
u_long proc; /* procedure number */
1993-10-07 10:29:33 +03:00
xdrproc_t xargs; /* xdr routine for args */
caddr_t argsp; /* pointer to args */
xdrproc_t xresults; /* xdr routine for results */
caddr_t resultsp; /* pointer to results */
resultproc_t eachresult; /* call with each result obtained */
{
enum clnt_stat stat;
AUTH *unix_auth = authunix_create_default();
XDR xdr_stream;
XDR *xdrs = &xdr_stream;
1999-05-03 19:32:13 +04:00
int inlen, nets;
socklen_t fromlen;
1998-11-15 20:32:41 +03:00
size_t outlen;
int sock;
1993-10-07 10:29:33 +03:00
int on = 1;
1996-12-17 06:55:20 +03:00
struct pollfd fd;
int i;
1993-10-07 10:29:33 +03:00
bool_t done = FALSE;
1998-11-15 20:32:41 +03:00
u_int32_t xid;
u_long port;
1993-10-07 10:29:33 +03:00
struct in_addr addrs[20];
struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
struct rmtcallargs a;
struct rmtcallres r;
struct rpc_msg msg;
struct timeval t;
1996-12-17 06:55:20 +03:00
int milliseconds;
1993-10-07 10:29:33 +03:00
char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
/*
* initialization: create a socket, a broadcast address, and
* preserialize the arguments into a send buffer.
*/
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
warn("Cannot create socket for broadcast rpc");
1993-10-07 10:29:33 +03:00
stat = RPC_CANTSEND;
goto done_broad;
}
#ifdef SO_BROADCAST
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
warn("Cannot set socket option SO_BROADCAST");
1993-10-07 10:29:33 +03:00
stat = RPC_CANTSEND;
goto done_broad;
}
#endif /* def SO_BROADCAST */
1996-12-17 06:55:20 +03:00
fd.fd = sock;
fd.events = POLLIN;
1993-10-07 10:29:33 +03:00
nets = getbroadcastnets(addrs, sock, inbuf);
memset(&baddr, 0, sizeof (baddr));
1995-06-04 02:37:19 +04:00
baddr.sin_len = sizeof(struct sockaddr_in);
1993-10-07 10:29:33 +03:00
baddr.sin_family = AF_INET;
baddr.sin_port = htons(PMAPPORT);
baddr.sin_addr.s_addr = htonl(INADDR_ANY);
(void)gettimeofday(&t, (struct timezone *)0);
1998-11-15 20:32:41 +03:00
msg.rm_xid = xid = (u_int32_t)(getpid() ^ t.tv_sec ^ t.tv_usec);
1993-10-07 10:29:33 +03:00
t.tv_usec = 0;
msg.rm_direction = CALL;
msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
msg.rm_call.cb_prog = PMAPPROG;
msg.rm_call.cb_vers = PMAPVERS;
msg.rm_call.cb_proc = PMAPPROC_CALLIT;
msg.rm_call.cb_cred = unix_auth->ah_cred;
msg.rm_call.cb_verf = unix_auth->ah_verf;
a.prog = prog;
a.vers = vers;
a.proc = proc;
a.xdr_args = xargs;
a.args_ptr = argsp;
r.port_ptr = &port;
r.xdr_results = xresults;
r.results_ptr = resultsp;
xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
stat = RPC_CANTENCODEARGS;
goto done_broad;
}
1998-11-15 20:32:41 +03:00
outlen = xdr_getpos(xdrs);
1993-10-07 10:29:33 +03:00
xdr_destroy(xdrs);
/*
* Basic loop: broadcast a packet and wait a while for response(s).
* The response timeout grows larger per iteration.
*/
for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
for (i = 0; i < nets; i++) {
baddr.sin_addr = addrs[i];
if (sendto(sock, outbuf, outlen, 0,
1998-11-15 20:32:41 +03:00
(struct sockaddr *)(void *)&baddr,
1993-10-07 10:29:33 +03:00
sizeof (struct sockaddr)) != outlen) {
warn("Cannot send broadcast packet");
1993-10-07 10:29:33 +03:00
stat = RPC_CANTSEND;
goto done_broad;
}
}
if (eachresult == NULL) {
stat = RPC_SUCCESS;
goto done_broad;
}
recv_again:
1998-11-15 20:32:41 +03:00
milliseconds = (int)(t.tv_sec * 1000 + t.tv_usec / 1000);
1993-10-07 10:29:33 +03:00
msg.acpted_rply.ar_verf = _null_auth;
1998-11-15 20:32:41 +03:00
msg.acpted_rply.ar_results.where = (caddr_t)(void *)&r;
1999-01-31 23:45:31 +03:00
msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
1996-12-17 06:55:20 +03:00
switch (poll(&fd, 1, milliseconds)) {
1993-10-07 10:29:33 +03:00
case 0: /* timed out */
stat = RPC_TIMEDOUT;
continue;
case -1: /* some kind of error */
if (errno == EINTR)
goto recv_again;
warn("Broadcast poll problem");
1993-10-07 10:29:33 +03:00
stat = RPC_CANTRECV;
goto done_broad;
1996-12-17 06:55:20 +03:00
} /* end of poll results switch */
1993-10-07 10:29:33 +03:00
try_again:
fromlen = sizeof(struct sockaddr);
inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
1998-11-15 20:32:41 +03:00
(struct sockaddr *)(void *)&raddr, &fromlen);
1993-10-07 10:29:33 +03:00
if (inlen < 0) {
if (errno == EINTR)
goto try_again;
warn("Cannot receive reply to broadcast");
1993-10-07 10:29:33 +03:00
stat = RPC_CANTRECV;
goto done_broad;
}
if (inlen < sizeof(u_int32_t))
1993-10-07 10:29:33 +03:00
goto recv_again;
/*
* see if reply transaction id matches sent id.
* If so, decode the results.
*/
xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
1993-10-07 10:29:33 +03:00
if (xdr_replymsg(xdrs, &msg)) {
if ((msg.rm_xid == xid) &&
(msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
(msg.acpted_rply.ar_stat == SUCCESS)) {
raddr.sin_port = htons((u_short)port);
1993-10-07 10:29:33 +03:00
done = (*eachresult)(resultsp, &raddr);
}
/* otherwise, we just ignore the errors ... */
} else {
#ifdef notdef
/* some kind of deserialization problem ... */
if (msg.rm_xid == xid)
fprintf(stderr,
"Broadcast deserialization problem");
1993-10-07 10:29:33 +03:00
/* otherwise, just random garbage */
#endif
}
xdrs->x_op = XDR_FREE;
1999-01-31 23:45:31 +03:00
msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
1993-10-07 10:29:33 +03:00
(void)xdr_replymsg(xdrs, &msg);
(void)(*xresults)(xdrs, resultsp);
xdr_destroy(xdrs);
if (done) {
stat = RPC_SUCCESS;
goto done_broad;
} else {
goto recv_again;
}
}
stat = RPC_TIMEDOUT;
1993-10-07 10:29:33 +03:00
done_broad:
if (sock != -1)
(void)close(sock);
1993-10-07 10:29:33 +03:00
AUTH_DESTROY(unix_auth);
return (stat);
}