* 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
* 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.
* Mountain View, California 94043
#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/
/*static char *sccsid = "from: @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";*/
static char *rcsid
= "$Id: pmap_rmt.c,v 1.3 1993/08/26 00:53:32 jtc Exp $";
* Client interface to pmap rpc service.
* remote call and broadcast service
* Copyright (C) 1984, Sun Microsystems, Inc.
#include <rpc/pmap_prot.h>
#include <rpc/pmap_clnt.h>
#include <rpc/pmap_rmt.h>
#define MAX_BROADCAST_SIZE 1400
static struct timeval timeout
= { 3, 0 };
* 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.
pmap_rmtcall(addr
, prog
, vers
, proc
, xdrargs
, argsp
, xdrres
, resp
, tout
, port_ptr
)
struct sockaddr_in
*addr
;
xdrproc_t xdrargs
, xdrres
;
addr
->sin_port
= htons(PMAPPORT
);
client
= clntudp_create(addr
, PMAPPROG
, PMAPVERS
, timeout
, &socket
);
if (client
!= (CLIENT
*)NULL
) {
stat
= CLNT_CALL(client
, PMAPPROC_CALLIT
, xdr_rmtcall_args
, &a
,
xdr_rmtcallres
, &r
, tout
);
* XDR remote call arguments
* written for XDR_ENCODE direction only
xdr_rmtcall_args(xdrs
, cap
)
register struct rmtcallargs
*cap
;
u_int lenposition
, argposition
, position
;
if (xdr_u_long(xdrs
, &(cap
->prog
)) &&
xdr_u_long(xdrs
, &(cap
->vers
)) &&
xdr_u_long(xdrs
, &(cap
->proc
))) {
lenposition
= XDR_GETPOS(xdrs
);
if (! xdr_u_long(xdrs
, &(cap
->arglen
)))
argposition
= XDR_GETPOS(xdrs
);
if (! (*(cap
->xdr_args
))(xdrs
, cap
->args_ptr
))
position
= XDR_GETPOS(xdrs
);
cap
->arglen
= (u_long
)position
- (u_long
)argposition
;
XDR_SETPOS(xdrs
, lenposition
);
if (! xdr_u_long(xdrs
, &(cap
->arglen
)))
XDR_SETPOS(xdrs
, position
);
* XDR remote call results
* written for XDR_DECODE direction only
xdr_rmtcallres(xdrs
, crp
)
register struct rmtcallres
*crp
;
port_ptr
= (caddr_t
)crp
->port_ptr
;
if (xdr_reference(xdrs
, &port_ptr
, sizeof (u_long
),
xdr_u_long
) && xdr_u_long(xdrs
, &crp
->resultslen
)) {
crp
->port_ptr
= (u_long
*)port_ptr
;
return ((*(crp
->xdr_results
))(xdrs
, crp
->results_ptr
));
* 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 .
getbroadcastnets(addrs
, sock
, buf
)
int sock
; /* any valid socket will do */
char *buf
; /* why allocxate more when we can use existing... */
struct ifreq ifreq
, *ifr
;
ifc
.ifc_len
= UDPMSGSIZE
;
if (ioctl(sock
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
perror("broadcast: ioctl (get interface configuration)");
#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
+= sizeof (ifr
->ifr_name
) + size(ifr
->ifr_addr
)) {
ifr
= (struct ifreq
*)cp
;
if (ifr
->ifr_addr
.sa_family
!= AF_INET
)
if (ioctl(sock
, SIOCGIFFLAGS
, (char *)&ifreq
) < 0) {
perror("broadcast: ioctl (get interface flags)");
if ((ifreq
.ifr_flags
& IFF_BROADCAST
) &&
(ifreq
.ifr_flags
& IFF_UP
)) {
sin
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
#ifdef SIOCGIFBRDADDR /* 4.3BSD */
if (ioctl(sock
, SIOCGIFBRDADDR
, (char *)&ifreq
) < 0) {
inet_makeaddr(inet_netof(sin
->sin_addr
),
addrs
[i
++] = ((struct sockaddr_in
*)
&ifreq
.ifr_addr
)->sin_addr
;
addrs
[i
++] = inet_makeaddr(inet_netof(sin
->sin_addr
),
typedef bool_t (*resultproc_t
)();
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 */
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 */
AUTH
*unix_auth
= authunix_create_default();
register XDR
*xdrs
= &xdr_stream
;
int outlen
, inlen
, fromlen
, nets
;
#endif /* def FD_SETSIZE */
struct in_addr addrs
[20];
struct sockaddr_in baddr
, raddr
; /* broadcast and response addresses */
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) {
perror("Cannot create socket for broadcast rpc");
if (setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof (on
)) < 0) {
perror("Cannot set socket option SO_BROADCAST");
#endif /* def SO_BROADCAST */
#endif /* def FD_SETSIZE */
nets
= getbroadcastnets(addrs
, sock
, inbuf
);
bzero((char *)&baddr
, sizeof (baddr
));
baddr
.sin_family
= AF_INET
;
baddr
.sin_port
= htons(PMAPPORT
);
baddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
/* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
(void)gettimeofday(&t
, (struct timezone
*)0);
msg
.rm_xid
= xid
= getpid() ^ t
.tv_sec
^ t
.tv_usec
;
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
;
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
;
outlen
= (int)xdr_getpos(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,
(struct sockaddr
*)&baddr
,
sizeof (struct sockaddr
)) != outlen
) {
perror("Cannot send broadcast packet");
if (eachresult
== NULL
) {
msg
.acpted_rply
.ar_verf
= _null_auth
;
msg
.acpted_rply
.ar_results
.where
= (caddr_t
)&r
;
msg
.acpted_rply
.ar_results
.proc
= xdr_rmtcallres
;
switch (select(_rpc_dtablesize(), &readfds
, (int *)NULL
,
case -1: /* some kind of error */
perror("Broadcast select problem");
} /* end of select results switch */
fromlen
= sizeof(struct sockaddr
);
inlen
= recvfrom(sock
, inbuf
, UDPMSGSIZE
, 0,
(struct sockaddr
*)&raddr
, &fromlen
);
perror("Cannot receive reply to broadcast");
if (inlen
< sizeof(u_long
))
* see if reply transaction id matches sent id.
* If so, decode the results.
xdrmem_create(xdrs
, inbuf
, (u_int
)inlen
, XDR_DECODE
);
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
);
done
= (*eachresult
)(resultsp
, &raddr
);
/* otherwise, we just ignore the errors ... */
/* some kind of deserialization problem ... */
fprintf(stderr
, "Broadcast deserialization problem");
/* otherwise, just random garbage */
msg
.acpted_rply
.ar_results
.proc
= xdr_void
;
(void)xdr_replymsg(xdrs
, &msg
);
(void)(*xresults
)(xdrs
, resultsp
);