/* @(#)pmapbrd.c 1.2 90/01/03 NFS Rev 2 Testsuite
* 1.4 Lachman ONC Test Suite source
* Test portmap broadcast rpc facility
#include <rpc/pmap_prot.h>
#include <rpc/pmap_clnt.h>
#include <sys/fs/nfs/time.h>
#define MAX_BROADCAST_SIZE 1400
static struct timeval timeout
= { 3, 0 };
struct sockaddr_in baddr
; /* broadcast addresses */
typedef bool_t (*resultproc_t
)();
char outbuf
[MAX_BROADCAST_SIZE
], inbuf
[MAX_BROADCAST_SIZE
];
* Structures and XDR routines for parameters to and replys from
* the pmapper remote-call-service.
ulong prog
, vers
, proc
, arglen
;
u_long prog
, vers
, proc
, arglen
;
static bool_t
xdr_rmtcall_args();
static bool_t
xdr_rmtcallres();
#define RPROG (ulong)40000010
#define RPROC_NUM (ulong)1
#define RPROG (u_long)40000010
#define RPROC_NUM (u_long)1
enum clnt_stat clnt_stat
;
fprintf(stderr
, "usage: %s pktspersec count\n", argv
[0]);
pktspersec
= atoi(argv
[1]);
"%s: packet rate must greater than or equal to 1\n",
sock
= socket(AF_INET
,SOCK_DGRAM
,0);
sin
.sin_family
= AF_INET
;
sin
.sin_addr
.s_addr
= INADDR_ANY
;
sin
.sin_port
= htons(3300);
if(bind(sock
, (char *)&sin
, sizeof (sin
)) == -1) {
if (setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &i
, sizeof(i
)) == -1) {
perror("brd: setsockopt");
getbroadcastnets(sock
, inbuf
);
baddr
.sin_family
= AF_INET
;
baddr
.sin_port
= htons(PMAPPORT
);
printf("broadcast addr %x\n", ntohl(baddr
.sin_addr
.s_addr
));
t
.tv_usec
= 1000000 / pktspersec
;
printf("%d/sec for %d\n", pktspersec
, count
);
for (i
=0; i
<count
; i
++) {
* modified verison of clnt_broadcast is called
clnt_broadcast(sock
, RPROG
, RVERS
, RPROC_NUM
, xdr_void
, &a
,
xdr_void
, &b
, eachresult
, &t
);
if(clnt_stat
!= RPC_TIMEDOUT
) {
printf("error: clnt_stat = %d\n", clnt_stat
);
* XDR remote call arguments
* written for XDR_ENCODE direction only
xdr_rmtcall_args(xdrs
, cap
)
register struct rmtcallargs
*cap
;
uint lenposition
, argposition
, position
;
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
= (ulong
)position
- (ulong
)argposition
;
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
;
if (xdr_reference(xdrs
, &crp
->port_ptr
, sizeof (ulong
), xdr_u_long
) &&
if (xdr_reference(xdrs
, &crp
->port_ptr
, sizeof (u_long
), xdr_u_long
) &&
xdr_u_long(xdrs
, &crp
->resultslen
))
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(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
= MAX_BROADCAST_SIZE
;
if (ioctl(sock
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
perror("broadcast: ioctl (get interface configuration)");
for (i
= 0, n
= ifc
.ifc_len
/sizeof (struct ifreq
); n
> 0; n
--, ifr
++) {
if (ioctl(sock
, SIOCGIFFLAGS
, (char *)&ifreq
) < 0) {
perror("broadcast: ioctl (get interface flags)");
if ((ifreq
.ifr_flags
& IFF_BROADCAST
) &&
(ifreq
.ifr_flags
& IFF_UP
) &&
ifr
->ifr_addr
.sa_family
== AF_INET
) {
sin
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
if (ioctl(sock
, SIOCGIFBRDADDR
, (char *)&ifreq
) < 0) {
baddr
.sin_addr
= inet_makeaddr(inet_netof(sin
->sin_addr
),
baddr
.sin_addr
= ((struct sockaddr_in
*)&ifreq
.ifr_addr
)->sin_addr
;
baddr
.sin_addr
= inet_makeaddr(inet_netof
(sin
->sin_addr
.s_addr
), INADDR_ANY
);
clnt_broadcast(sock
, prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
, eachresult
, t
)
ulong prog
; /* program number */
ulong vers
; /* version number */
ulong proc
; /* procedure number */
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();
int outlen
, inlen
, fromlen
, readfds
;
struct sockaddr_in raddr
; /* broadcast and response addresses */
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.
if (sendto(sock
, outbuf
, outlen
, 0,
(struct socketaddr
*)&baddr
,
sizeof (struct sockaddr
)) != outlen
) {
perror("Cannot send broadcast packet");
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(32, &readfds
, (int *)NULL
, (int *)NULL
, t
)) {
case -1: /* some kind of error */
perror("Broadcast select problem");
} /* end of select results switch */
if ((readfds
& mask
) == 0)
fromlen
= sizeof(struct sockaddr
);
inlen
= recvfrom(sock
, inbuf
, MAX_BROADCAST_SIZE
, 0,
(struct sockaddr
*)&raddr
, &fromlen
);
perror("Cannot receive reply to broadcast");
if (inlen
< sizeof(ulong
))
if (inlen
< sizeof(u_long
))
* see if reply transaction id matches sent id.
* If so, decode the results.
xdrmem_create(xdrs
, inbuf
, 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((ushort
)port
);
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
);