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
29 * @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro
30 * @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/pmap_rmt.c,v 1.16.2.1 2002/06/30 23:34:58 iedowse Exp $
32 * $DragonFly: src/lib/libc/rpc/pmap_rmt.c,v 1.4 2005/01/31 22:29:38 dillon Exp $
37 * Client interface to pmap rpc service.
38 * remote call and broadcast service
40 * Copyright (C) 1984, Sun Microsystems, Inc.
43 #include "namespace.h"
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 #include "un-namespace.h"
59 #define MAX_BROADCAST_SIZE 1400
61 static struct timeval timeout = { 3, 0 };
64 * pmapper remote-call-service interface.
65 * This routine is used to call the pmapper remote call service
66 * which will look up a service program in the port maps, and then
67 * remotely call that routine with the given parameters. This allows
68 * programs to do a lookup and call in one step.
71 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
72 struct sockaddr_in *addr;
73 u_long prog, vers, proc;
74 xdrproc_t xdrargs, xdrres;
85 addr->sin_port = htons(PMAPPORT);
86 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
87 if (client != (CLIENT *)NULL) {
93 r.port_ptr = port_ptr;
95 r.xdr_results = xdrres;
96 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
97 xdr_rmtcallres, &r, tout);
103 (void)_close(socket);
110 * XDR remote call arguments
111 * written for XDR_ENCODE direction only
114 xdr_rmtcall_args(xdrs, cap)
116 struct rmtcallargs *cap;
118 u_int lenposition, argposition, position;
120 if (xdr_u_long(xdrs, &(cap->prog)) &&
121 xdr_u_long(xdrs, &(cap->vers)) &&
122 xdr_u_long(xdrs, &(cap->proc))) {
123 lenposition = XDR_GETPOS(xdrs);
124 if (! xdr_u_long(xdrs, &(cap->arglen)))
126 argposition = XDR_GETPOS(xdrs);
127 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
129 position = XDR_GETPOS(xdrs);
130 cap->arglen = (u_long)position - (u_long)argposition;
131 XDR_SETPOS(xdrs, lenposition);
132 if (! xdr_u_long(xdrs, &(cap->arglen)))
134 XDR_SETPOS(xdrs, position);
141 * XDR remote call results
142 * written for XDR_DECODE direction only
145 xdr_rmtcallres(xdrs, crp)
147 struct rmtcallres *crp;
151 port_ptr = (caddr_t)crp->port_ptr;
152 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
153 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
154 crp->port_ptr = (u_long *)port_ptr;
155 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
162 * The following is kludged-up support for simple rpc broadcasts.
163 * Someday a large, complicated system will replace these trivial
164 * routines which only support udp/ip .
168 getbroadcastnets(addrs, sock, buf)
169 struct in_addr *addrs;
170 int sock; /* any valid socket will do */
171 char *buf; /* why allocxate more when we can use existing... */
174 struct ifreq ifreq, *ifr;
175 struct sockaddr_in *sin;
180 ifc.ifc_len = UDPMSGSIZE;
182 if (_ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
183 perror("broadcast: ioctl (get interface configuration)");
186 #define max(a, b) (a > b ? a : b)
187 #define size(p) max((p).sa_len, sizeof(p))
188 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
189 for (cp = buf; cp < cplim;
190 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
191 ifr = (struct ifreq *)cp;
192 if (ifr->ifr_addr.sa_family != AF_INET)
194 memcpy(&ifreq, ifr, sizeof(ifreq));
195 if (_ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
196 perror("broadcast: ioctl (get interface flags)");
199 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
200 (ifreq.ifr_flags & IFF_UP)) {
201 sin = (struct sockaddr_in *)&ifr->ifr_addr;
202 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
203 if (_ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
205 inet_makeaddr(inet_netof(sin->sin_addr),
208 addr = ((struct sockaddr_in*)
209 &ifreq.ifr_addr)->sin_addr;
212 addr = inet_makeaddr(inet_netof(sin->sin_addr),
215 for (n=i-1; n>=0; n--) {
216 if (addr.s_addr == addrs[n].s_addr)
227 typedef bool_t (*resultproc_t)();
230 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
231 u_long prog; /* program number */
232 u_long vers; /* version number */
233 u_long proc; /* procedure number */
234 xdrproc_t xargs; /* xdr routine for args */
235 caddr_t argsp; /* pointer to args */
236 xdrproc_t xresults; /* xdr routine for results */
237 caddr_t resultsp; /* pointer to results */
238 resultproc_t eachresult; /* call with each result obtained */
241 AUTH *unix_auth = authunix_create_default();
243 XDR *xdrs = &xdr_stream;
244 int outlen, inlen, fromlen, nets;
247 fd_set *fds, readfds;
252 struct in_addr addrs[20];
253 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
254 struct rmtcallargs a;
257 struct timeval t, tv;
258 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
259 static u_int32_t disrupt;
262 disrupt = (u_int32_t)(long)resultsp;
265 * initialization: create a socket, a broadcast address, and
266 * preserialize the arguments into a send buffer.
268 if ((sock = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
269 perror("Cannot create socket for broadcast rpc");
274 if (_setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
275 perror("Cannot set socket option SO_BROADCAST");
279 #endif /* def SO_BROADCAST */
280 if (sock + 1 > FD_SETSIZE) {
281 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
282 fds = (fd_set *)malloc(bytes);
287 memset(fds, 0, bytes);
293 nets = getbroadcastnets(addrs, sock, inbuf);
294 memset(&baddr, 0, sizeof (baddr));
295 baddr.sin_len = sizeof(struct sockaddr_in);
296 baddr.sin_family = AF_INET;
297 baddr.sin_port = htons(PMAPPORT);
298 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
299 (void)gettimeofday(&t, (struct timezone *)0);
300 msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
302 msg.rm_direction = CALL;
303 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
304 msg.rm_call.cb_prog = PMAPPROG;
305 msg.rm_call.cb_vers = PMAPVERS;
306 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
307 msg.rm_call.cb_cred = unix_auth->ah_cred;
308 msg.rm_call.cb_verf = unix_auth->ah_verf;
315 r.xdr_results = xresults;
316 r.results_ptr = resultsp;
317 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
318 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
319 stat = RPC_CANTENCODEARGS;
322 outlen = (int)xdr_getpos(xdrs);
325 * Basic loop: broadcast a packet and wait a while for response(s).
326 * The response timeout grows larger per iteration.
328 * XXX This will loop about 5 times the stop. If there are
329 * lots of signals being received by the process it will quit
330 * send them all in one quick burst, not paying attention to
331 * the intended function of sending them slowly over half a
334 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
336 for (i = 0; i < nets; i++) {
337 baddr.sin_addr = addrs[i];
338 if (_sendto(sock, outbuf, outlen, 0,
339 (struct sockaddr *)&baddr,
340 sizeof (struct sockaddr)) == outlen) {
345 perror("Cannot send broadcast packet");
349 if (eachresult == NULL) {
354 msg.acpted_rply.ar_verf = _null_auth;
355 msg.acpted_rply.ar_results.where = (caddr_t)&r;
356 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
357 /* XXX we know the other bits are still clear */
359 tv = t; /* for _select() that copies back */
360 switch (_select(sock + 1, fds, NULL, NULL, &tv)) {
362 case 0: /* timed out */
366 case -1: /* some kind of error */
369 perror("Broadcast select problem");
373 } /* end of select results switch */
375 fromlen = sizeof(struct sockaddr);
376 inlen = _recvfrom(sock, inbuf, UDPMSGSIZE, 0,
377 (struct sockaddr *)&raddr, &fromlen);
381 perror("Cannot receive reply to broadcast");
385 if (inlen < sizeof(u_int32_t))
388 * see if reply transaction id matches sent id.
389 * If so, decode the results.
391 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
392 if (xdr_replymsg(xdrs, &msg)) {
393 if ((msg.rm_xid == xid) &&
394 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
395 (msg.acpted_rply.ar_stat == SUCCESS)) {
396 raddr.sin_port = htons((u_short)port);
397 done = (*eachresult)(resultsp, &raddr);
399 /* otherwise, we just ignore the errors ... */
401 xdrs->x_op = XDR_FREE;
402 msg.acpted_rply.ar_results.proc = xdr_void;
403 (void)xdr_replymsg(xdrs, &msg);
404 (void)(*xresults)(xdrs, resultsp);
418 AUTH_DESTROY(unix_auth);