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/libcr/rpc/Attic/pmap_rmt.c,v 1.2 2003/06/17 04:26:45 dillon Exp $
37 * Client interface to pmap rpc service.
38 * remote call and broadcast service
40 * Copyright (C) 1984, Sun Microsystems, Inc.
44 #include <rpc/pmap_prot.h>
45 #include <rpc/pmap_clnt.h>
46 #include <rpc/pmap_rmt.h>
47 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <arpa/inet.h>
56 #define MAX_BROADCAST_SIZE 1400
58 static struct timeval timeout = { 3, 0 };
61 * pmapper remote-call-service interface.
62 * This routine is used to call the pmapper remote call service
63 * which will look up a service program in the port maps, and then
64 * remotely call that routine with the given parameters. This allows
65 * programs to do a lookup and call in one step.
68 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
69 struct sockaddr_in *addr;
70 u_long prog, vers, proc;
71 xdrproc_t xdrargs, xdrres;
77 register CLIENT *client;
82 addr->sin_port = htons(PMAPPORT);
83 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
84 if (client != (CLIENT *)NULL) {
90 r.port_ptr = port_ptr;
92 r.xdr_results = xdrres;
93 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
94 xdr_rmtcallres, &r, tout);
100 (void)_close(socket);
107 * XDR remote call arguments
108 * written for XDR_ENCODE direction only
111 xdr_rmtcall_args(xdrs, cap)
113 register struct rmtcallargs *cap;
115 u_int lenposition, argposition, position;
117 if (xdr_u_long(xdrs, &(cap->prog)) &&
118 xdr_u_long(xdrs, &(cap->vers)) &&
119 xdr_u_long(xdrs, &(cap->proc))) {
120 lenposition = XDR_GETPOS(xdrs);
121 if (! xdr_u_long(xdrs, &(cap->arglen)))
123 argposition = XDR_GETPOS(xdrs);
124 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
126 position = XDR_GETPOS(xdrs);
127 cap->arglen = (u_long)position - (u_long)argposition;
128 XDR_SETPOS(xdrs, lenposition);
129 if (! xdr_u_long(xdrs, &(cap->arglen)))
131 XDR_SETPOS(xdrs, position);
138 * XDR remote call results
139 * written for XDR_DECODE direction only
142 xdr_rmtcallres(xdrs, crp)
144 register struct rmtcallres *crp;
148 port_ptr = (caddr_t)crp->port_ptr;
149 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
150 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
151 crp->port_ptr = (u_long *)port_ptr;
152 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
159 * The following is kludged-up support for simple rpc broadcasts.
160 * Someday a large, complicated system will replace these trivial
161 * routines which only support udp/ip .
165 getbroadcastnets(addrs, sock, buf)
166 struct in_addr *addrs;
167 int sock; /* any valid socket will do */
168 char *buf; /* why allocxate more when we can use existing... */
171 struct ifreq ifreq, *ifr;
172 struct sockaddr_in *sin;
177 ifc.ifc_len = UDPMSGSIZE;
179 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
180 perror("broadcast: ioctl (get interface configuration)");
183 #define max(a, b) (a > b ? a : b)
184 #define size(p) max((p).sa_len, sizeof(p))
185 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
186 for (cp = buf; cp < cplim;
187 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
188 ifr = (struct ifreq *)cp;
189 if (ifr->ifr_addr.sa_family != AF_INET)
191 memcpy(&ifreq, ifr, sizeof(ifreq));
192 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
193 perror("broadcast: ioctl (get interface flags)");
196 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
197 (ifreq.ifr_flags & IFF_UP)) {
198 sin = (struct sockaddr_in *)&ifr->ifr_addr;
199 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
200 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
202 inet_makeaddr(inet_netof(sin->sin_addr),
205 addr = ((struct sockaddr_in*)
206 &ifreq.ifr_addr)->sin_addr;
209 addr = inet_makeaddr(inet_netof(sin->sin_addr),
212 for (n=i-1; n>=0; n--) {
213 if (addr.s_addr == addrs[n].s_addr)
224 typedef bool_t (*resultproc_t)();
227 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
228 u_long prog; /* program number */
229 u_long vers; /* version number */
230 u_long proc; /* procedure number */
231 xdrproc_t xargs; /* xdr routine for args */
232 caddr_t argsp; /* pointer to args */
233 xdrproc_t xresults; /* xdr routine for results */
234 caddr_t resultsp; /* pointer to results */
235 resultproc_t eachresult; /* call with each result obtained */
238 AUTH *unix_auth = authunix_create_default();
240 register XDR *xdrs = &xdr_stream;
241 int outlen, inlen, fromlen, nets;
244 fd_set *fds, readfds;
249 struct in_addr addrs[20];
250 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
251 struct rmtcallargs a;
254 struct timeval t, tv;
255 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
256 static u_int32_t disrupt;
259 disrupt = (u_int32_t)(long)resultsp;
262 * initialization: create a socket, a broadcast address, and
263 * preserialize the arguments into a send buffer.
265 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
266 perror("Cannot create socket for broadcast rpc");
271 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
272 perror("Cannot set socket option SO_BROADCAST");
276 #endif /* def SO_BROADCAST */
277 if (sock + 1 > FD_SETSIZE) {
278 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
279 fds = (fd_set *)malloc(bytes);
284 memset(fds, 0, bytes);
290 nets = getbroadcastnets(addrs, sock, inbuf);
291 memset(&baddr, 0, sizeof (baddr));
292 baddr.sin_len = sizeof(struct sockaddr_in);
293 baddr.sin_family = AF_INET;
294 baddr.sin_port = htons(PMAPPORT);
295 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
296 (void)gettimeofday(&t, (struct timezone *)0);
297 msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
299 msg.rm_direction = CALL;
300 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
301 msg.rm_call.cb_prog = PMAPPROG;
302 msg.rm_call.cb_vers = PMAPVERS;
303 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
304 msg.rm_call.cb_cred = unix_auth->ah_cred;
305 msg.rm_call.cb_verf = unix_auth->ah_verf;
312 r.xdr_results = xresults;
313 r.results_ptr = resultsp;
314 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
315 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
316 stat = RPC_CANTENCODEARGS;
319 outlen = (int)xdr_getpos(xdrs);
322 * Basic loop: broadcast a packet and wait a while for response(s).
323 * The response timeout grows larger per iteration.
325 * XXX This will loop about 5 times the stop. If there are
326 * lots of signals being received by the process it will quit
327 * send them all in one quick burst, not paying attention to
328 * the intended function of sending them slowly over half a
331 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
333 for (i = 0; i < nets; i++) {
334 baddr.sin_addr = addrs[i];
335 if (sendto(sock, outbuf, outlen, 0,
336 (struct sockaddr *)&baddr,
337 sizeof (struct sockaddr)) == outlen) {
342 perror("Cannot send broadcast packet");
346 if (eachresult == NULL) {
351 msg.acpted_rply.ar_verf = _null_auth;
352 msg.acpted_rply.ar_results.where = (caddr_t)&r;
353 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
354 /* XXX we know the other bits are still clear */
356 tv = t; /* for select() that copies back */
357 switch (select(sock + 1, fds, NULL, NULL, &tv)) {
359 case 0: /* timed out */
363 case -1: /* some kind of error */
366 perror("Broadcast select problem");
370 } /* end of select results switch */
372 fromlen = sizeof(struct sockaddr);
373 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
374 (struct sockaddr *)&raddr, &fromlen);
378 perror("Cannot receive reply to broadcast");
382 if (inlen < sizeof(u_int32_t))
385 * see if reply transaction id matches sent id.
386 * If so, decode the results.
388 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
389 if (xdr_replymsg(xdrs, &msg)) {
390 if ((msg.rm_xid == xid) &&
391 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
392 (msg.acpted_rply.ar_stat == SUCCESS)) {
393 raddr.sin_port = htons((u_short)port);
394 done = (*eachresult)(resultsp, &raddr);
396 /* otherwise, we just ignore the errors ... */
398 xdrs->x_op = XDR_FREE;
399 msg.acpted_rply.ar_results.proc = xdr_void;
400 (void)xdr_replymsg(xdrs, &msg);
401 (void)(*xresults)(xdrs, resultsp);
415 AUTH_DESTROY(unix_auth);