2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #if defined(LIBC_SCCS) && !defined(lint)
31 /*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";*/
33 static char *rcsid = "$FreeBSD: src/lib/libc/rpc/pmap_rmt.c,v 1.16.2.1 2002/06/30 23:34:58 iedowse Exp $";
38 * Client interface to pmap rpc service.
39 * remote call and broadcast service
41 * Copyright (C) 1984, Sun Microsystems, Inc.
45 #include <rpc/pmap_prot.h>
46 #include <rpc/pmap_clnt.h>
47 #include <rpc/pmap_rmt.h>
48 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <arpa/inet.h>
57 #define MAX_BROADCAST_SIZE 1400
59 static struct timeval timeout = { 3, 0 };
62 * pmapper remote-call-service interface.
63 * This routine is used to call the pmapper remote call service
64 * which will look up a service program in the port maps, and then
65 * remotely call that routine with the given parameters. This allows
66 * programs to do a lookup and call in one step.
69 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
70 struct sockaddr_in *addr;
71 u_long prog, vers, proc;
72 xdrproc_t xdrargs, xdrres;
78 register CLIENT *client;
83 addr->sin_port = htons(PMAPPORT);
84 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
85 if (client != (CLIENT *)NULL) {
91 r.port_ptr = port_ptr;
93 r.xdr_results = xdrres;
94 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
95 xdr_rmtcallres, &r, tout);
101 (void)_close(socket);
108 * XDR remote call arguments
109 * written for XDR_ENCODE direction only
112 xdr_rmtcall_args(xdrs, cap)
114 register struct rmtcallargs *cap;
116 u_int lenposition, argposition, position;
118 if (xdr_u_long(xdrs, &(cap->prog)) &&
119 xdr_u_long(xdrs, &(cap->vers)) &&
120 xdr_u_long(xdrs, &(cap->proc))) {
121 lenposition = XDR_GETPOS(xdrs);
122 if (! xdr_u_long(xdrs, &(cap->arglen)))
124 argposition = XDR_GETPOS(xdrs);
125 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
127 position = XDR_GETPOS(xdrs);
128 cap->arglen = (u_long)position - (u_long)argposition;
129 XDR_SETPOS(xdrs, lenposition);
130 if (! xdr_u_long(xdrs, &(cap->arglen)))
132 XDR_SETPOS(xdrs, position);
139 * XDR remote call results
140 * written for XDR_DECODE direction only
143 xdr_rmtcallres(xdrs, crp)
145 register struct rmtcallres *crp;
149 port_ptr = (caddr_t)crp->port_ptr;
150 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
151 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
152 crp->port_ptr = (u_long *)port_ptr;
153 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
160 * The following is kludged-up support for simple rpc broadcasts.
161 * Someday a large, complicated system will replace these trivial
162 * routines which only support udp/ip .
166 getbroadcastnets(addrs, sock, buf)
167 struct in_addr *addrs;
168 int sock; /* any valid socket will do */
169 char *buf; /* why allocxate more when we can use existing... */
172 struct ifreq ifreq, *ifr;
173 struct sockaddr_in *sin;
178 ifc.ifc_len = UDPMSGSIZE;
180 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
181 perror("broadcast: ioctl (get interface configuration)");
184 #define max(a, b) (a > b ? a : b)
185 #define size(p) max((p).sa_len, sizeof(p))
186 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
187 for (cp = buf; cp < cplim;
188 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
189 ifr = (struct ifreq *)cp;
190 if (ifr->ifr_addr.sa_family != AF_INET)
192 memcpy(&ifreq, ifr, sizeof(ifreq));
193 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
194 perror("broadcast: ioctl (get interface flags)");
197 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
198 (ifreq.ifr_flags & IFF_UP)) {
199 sin = (struct sockaddr_in *)&ifr->ifr_addr;
200 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
201 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
203 inet_makeaddr(inet_netof(sin->sin_addr),
206 addr = ((struct sockaddr_in*)
207 &ifreq.ifr_addr)->sin_addr;
210 addr = inet_makeaddr(inet_netof(sin->sin_addr),
213 for (n=i-1; n>=0; n--) {
214 if (addr.s_addr == addrs[n].s_addr)
225 typedef bool_t (*resultproc_t)();
228 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
229 u_long prog; /* program number */
230 u_long vers; /* version number */
231 u_long proc; /* procedure number */
232 xdrproc_t xargs; /* xdr routine for args */
233 caddr_t argsp; /* pointer to args */
234 xdrproc_t xresults; /* xdr routine for results */
235 caddr_t resultsp; /* pointer to results */
236 resultproc_t eachresult; /* call with each result obtained */
239 AUTH *unix_auth = authunix_create_default();
241 register XDR *xdrs = &xdr_stream;
242 int outlen, inlen, fromlen, nets;
245 fd_set *fds, readfds;
250 struct in_addr addrs[20];
251 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
252 struct rmtcallargs a;
255 struct timeval t, tv;
256 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
257 static u_int32_t disrupt;
260 disrupt = (u_int32_t)(long)resultsp;
263 * initialization: create a socket, a broadcast address, and
264 * preserialize the arguments into a send buffer.
266 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
267 perror("Cannot create socket for broadcast rpc");
272 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
273 perror("Cannot set socket option SO_BROADCAST");
277 #endif /* def SO_BROADCAST */
278 if (sock + 1 > FD_SETSIZE) {
279 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
280 fds = (fd_set *)malloc(bytes);
285 memset(fds, 0, bytes);
291 nets = getbroadcastnets(addrs, sock, inbuf);
292 memset(&baddr, 0, sizeof (baddr));
293 baddr.sin_len = sizeof(struct sockaddr_in);
294 baddr.sin_family = AF_INET;
295 baddr.sin_port = htons(PMAPPORT);
296 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
297 (void)gettimeofday(&t, (struct timezone *)0);
298 msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
300 msg.rm_direction = CALL;
301 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
302 msg.rm_call.cb_prog = PMAPPROG;
303 msg.rm_call.cb_vers = PMAPVERS;
304 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
305 msg.rm_call.cb_cred = unix_auth->ah_cred;
306 msg.rm_call.cb_verf = unix_auth->ah_verf;
313 r.xdr_results = xresults;
314 r.results_ptr = resultsp;
315 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
316 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
317 stat = RPC_CANTENCODEARGS;
320 outlen = (int)xdr_getpos(xdrs);
323 * Basic loop: broadcast a packet and wait a while for response(s).
324 * The response timeout grows larger per iteration.
326 * XXX This will loop about 5 times the stop. If there are
327 * lots of signals being received by the process it will quit
328 * send them all in one quick burst, not paying attention to
329 * the intended function of sending them slowly over half a
332 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
334 for (i = 0; i < nets; i++) {
335 baddr.sin_addr = addrs[i];
336 if (sendto(sock, outbuf, outlen, 0,
337 (struct sockaddr *)&baddr,
338 sizeof (struct sockaddr)) == outlen) {
343 perror("Cannot send broadcast packet");
347 if (eachresult == NULL) {
352 msg.acpted_rply.ar_verf = _null_auth;
353 msg.acpted_rply.ar_results.where = (caddr_t)&r;
354 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
355 /* XXX we know the other bits are still clear */
357 tv = t; /* for select() that copies back */
358 switch (select(sock + 1, fds, NULL, NULL, &tv)) {
360 case 0: /* timed out */
364 case -1: /* some kind of error */
367 perror("Broadcast select problem");
371 } /* end of select results switch */
373 fromlen = sizeof(struct sockaddr);
374 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
375 (struct sockaddr *)&raddr, &fromlen);
379 perror("Cannot receive reply to broadcast");
383 if (inlen < sizeof(u_int32_t))
386 * see if reply transaction id matches sent id.
387 * If so, decode the results.
389 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
390 if (xdr_replymsg(xdrs, &msg)) {
391 if ((msg.rm_xid == xid) &&
392 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
393 (msg.acpted_rply.ar_stat == SUCCESS)) {
394 raddr.sin_port = htons((u_short)port);
395 done = (*eachresult)(resultsp, &raddr);
397 /* otherwise, we just ignore the errors ... */
399 xdrs->x_op = XDR_FREE;
400 msg.acpted_rply.ar_results.proc = xdr_void;
401 (void)xdr_replymsg(xdrs, &msg);
402 (void)(*xresults)(xdrs, resultsp);
416 AUTH_DESTROY(unix_auth);