struct timeval t;
struct timeval t, t1;
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 */{	enum clnt_stat stat;	AUTH *unix_auth = authunix_create_default();	XDR xdr_stream;	register XDR *xdrs = &xdr_stream;	int outlen, inlen, fromlen, nets;	register int sock;	int on = 1;#ifdef FD_SETSIZE	fd_set mask;	fd_set readfds;#else	int readfds;	register int mask;#endif /* def FD_SETSIZE */	register int i;	bool_t done = FALSE;	register u_long xid;	u_long port;	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;	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"));		stat = RPC_CANTSEND;		goto done_broad;	}#ifdef SO_BROADCAST	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {		perror(_("Cannot set socket option SO_BROADCAST"));		stat = RPC_CANTSEND;		goto done_broad;	}#endif /* def SO_BROADCAST */#ifdef FD_SETSIZE	FD_ZERO(&mask);	FD_SET(sock, &mask);#else	mask = (1 << sock);#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;	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;	}	outlen = (int)xdr_getpos(xdrs);	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,				(struct sockaddr *)&baddr,				sizeof (struct sockaddr)) != outlen) {				perror(_("Cannot send broadcast packet"));				stat = RPC_CANTSEND;				goto done_broad;			}		}		if (eachresult == NULL) {			stat = RPC_SUCCESS;			goto done_broad;		}	recv_again:		msg.acpted_rply.ar_verf = _null_auth;		msg.acpted_rply.ar_results.where = (caddr_t)&r;                msg.acpted_rply.ar_results.proc = xdr_rmtcallres;		readfds = mask;		switch (select(_rpc_dtablesize(), &readfds, (int *)NULL,			       (int *)NULL, &t)) {		case 0:  /* timed out */			stat = RPC_TIMEDOUT;			continue;		case -1:  /* some kind of error */			if (errno == EINTR)				goto recv_again;			perror(_("Broadcast select problem"));			stat = RPC_CANTRECV;			goto done_broad;		}  /* end of select results switch */	try_again:		fromlen = sizeof(struct sockaddr);		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,			(struct sockaddr *)&raddr, &fromlen);		if (inlen < 0) {			if (errno == EINTR)				goto try_again;			perror(_("Cannot receive reply to broadcast"));			stat = RPC_CANTRECV;			goto done_broad;		}		if (inlen < sizeof(u_long))			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);		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 ... */		} else {#ifdef notdef			/* some kind of deserialization problem ... */			if (msg.rm_xid == xid)				fprintf(stderr, "Broadcast deserialization problem");			/* otherwise, just random garbage */#endif		}		xdrs->x_op = XDR_FREE;		msg.acpted_rply.ar_results.proc = xdr_void;		(void)xdr_replymsg(xdrs, &msg);		(void)(*xresults)(xdrs, resultsp);		xdr_destroy(xdrs);		if (done) {			stat = RPC_SUCCESS;			goto done_broad;		} else {			goto recv_again;		}	}done_broad:	(void)close(sock);	AUTH_DESTROY(unix_auth);	return (stat);}