1 /* @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
31 static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
36 * Client interface to pmap rpc service.
37 * remote call and broadcast service
39 * Copyright (C) 1984, Sun Microsystems, Inc.
48 #include <rpc/pmap_prot.h>
49 #include <rpc/pmap_clnt.h>
50 #include <rpc/pmap_rmt.h>
52 #include <sys/socket.h>
55 #undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
56 #include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
58 #include <sys/ioctl.h>
59 #include <arpa/inet.h>
60 #define MAX_BROADCAST_SIZE 1400
62 /* Experimentally off - libc_hidden_proto(memset) */
63 libc_hidden_proto(ioctl)
64 libc_hidden_proto(perror)
65 libc_hidden_proto(socket)
66 libc_hidden_proto(close)
67 libc_hidden_proto(authunix_create_default)
68 libc_hidden_proto(xdrmem_create)
69 libc_hidden_proto(xdr_callmsg)
70 libc_hidden_proto(xdr_replymsg)
71 libc_hidden_proto(xdr_reference)
72 libc_hidden_proto(xdr_u_long)
73 libc_hidden_proto(xdr_void)
74 libc_hidden_proto(xdr_rmtcallres)
75 libc_hidden_proto(xdr_rmtcall_args)
76 libc_hidden_proto(inet_makeaddr)
77 libc_hidden_proto(inet_netof)
78 libc_hidden_proto(clntudp_create)
79 libc_hidden_proto(setsockopt)
80 libc_hidden_proto(recvfrom)
81 libc_hidden_proto(sendto)
82 libc_hidden_proto(poll)
83 libc_hidden_proto(fprintf)
86 extern u_long _create_xid (void) attribute_hidden;
88 static const struct timeval timeout = {3, 0};
91 * pmapper remote-call-service interface.
92 * This routine is used to call the pmapper remote call service
93 * which will look up a service program in the port maps, and then
94 * remotely call that routine with the given parameters. This allows
95 * programs to do a lookup and call in one step.
98 pmap_rmtcall (addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
99 struct sockaddr_in *addr;
100 u_long prog, vers, proc;
101 xdrproc_t xdrargs, xdrres;
108 struct rmtcallargs a;
112 addr->sin_port = htons (PMAPPORT);
113 client = clntudp_create (addr, PMAPPROG, PMAPVERS, timeout, &_socket);
114 if (client != (CLIENT *) NULL)
120 a.xdr_args = xdrargs;
121 r.port_ptr = port_ptr;
122 r.results_ptr = resp;
123 r.xdr_results = xdrres;
124 stat = CLNT_CALL (client, PMAPPROC_CALLIT, (xdrproc_t)xdr_rmtcall_args,
125 (caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
127 CLNT_DESTROY (client);
133 /* (void)close(_socket); CLNT_DESTROY already closed it */
140 * XDR remote call arguments
141 * written for XDR_ENCODE direction only
144 xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
146 u_int lenposition, argposition, position;
148 if (xdr_u_long (xdrs, &(cap->prog)) &&
149 xdr_u_long (xdrs, &(cap->vers)) &&
150 xdr_u_long (xdrs, &(cap->proc)))
152 u_long dummy_arglen = 0;
153 lenposition = XDR_GETPOS (xdrs);
154 if (!xdr_u_long (xdrs, &dummy_arglen))
156 argposition = XDR_GETPOS (xdrs);
157 if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
159 position = XDR_GETPOS (xdrs);
160 cap->arglen = (u_long) position - (u_long) argposition;
161 XDR_SETPOS (xdrs, lenposition);
162 if (!xdr_u_long (xdrs, &(cap->arglen)))
164 XDR_SETPOS (xdrs, position);
169 libc_hidden_def(xdr_rmtcall_args)
172 * XDR remote call results
173 * written for XDR_DECODE direction only
176 xdr_rmtcallres (xdrs, crp)
178 struct rmtcallres *crp;
182 port_ptr = (caddr_t) crp->port_ptr;
183 if (xdr_reference (xdrs, &port_ptr, sizeof (u_long), (xdrproc_t) xdr_u_long)
184 && xdr_u_long (xdrs, &crp->resultslen))
186 crp->port_ptr = (u_long *) port_ptr;
187 return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
191 libc_hidden_def(xdr_rmtcallres)
195 * The following is kludged-up support for simple rpc broadcasts.
196 * Someday a large, complicated system will replace these trivial
197 * routines which only support udp/ip .
202 getbroadcastnets (struct in_addr *addrs, int sock, char *buf)
203 /* int sock: any valid socket will do */
204 /* char *buf: why allocate more when we can use existing... */
207 struct ifreq ifreq, *ifr;
208 struct sockaddr_in *sin;
211 ifc.ifc_len = UDPMSGSIZE;
213 if (ioctl (sock, SIOCGIFCONF, (char *) &ifc) < 0)
215 perror (_("broadcast: ioctl (get interface configuration)"));
219 for (i = 0, n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
222 if (ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
224 perror (_("broadcast: ioctl (get interface flags)"));
227 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
228 (ifreq.ifr_flags & IFF_UP) &&
229 ifr->ifr_addr.sa_family == AF_INET)
231 sin = (struct sockaddr_in *) &ifr->ifr_addr;
232 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
233 if (ioctl (sock, SIOCGIFBRDADDR, (char *) &ifreq) < 0)
235 addrs[i++] = inet_makeaddr (inet_netof
236 /* Changed to pass struct instead of s_addr member
238 (sin->sin_addr), INADDR_ANY);
242 addrs[i++] = ((struct sockaddr_in *)
243 &ifreq.ifr_addr)->sin_addr;
246 addrs[i++] = inet_makeaddr (inet_netof
247 (sin->sin_addr.s_addr), INADDR_ANY);
256 clnt_broadcast (prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
257 u_long prog; /* program number */
258 u_long vers; /* version number */
259 u_long proc; /* procedure number */
260 xdrproc_t xargs; /* xdr routine for args */
261 caddr_t argsp; /* pointer to args */
262 xdrproc_t xresults; /* xdr routine for results */
263 caddr_t resultsp; /* pointer to results */
264 resultproc_t eachresult; /* call with each result obtained */
266 enum clnt_stat stat = RPC_FAILED;
267 AUTH *unix_auth = authunix_create_default ();
269 XDR *xdrs = &xdr_stream;
271 int outlen, inlen, nets;
281 struct in_addr addrs[20];
282 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
283 struct rmtcallargs a;
286 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
289 * initialization: create a socket, a broadcast address, and
290 * preserialize the arguments into a send buffer.
292 if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
294 perror (_("Cannot create socket for broadcast rpc"));
299 if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
301 perror (_("Cannot set socket option SO_BROADCAST"));
305 #endif /* def SO_BROADCAST */
308 nets = getbroadcastnets (addrs, sock, inbuf);
309 memset ((char *) &baddr, 0, sizeof (baddr));
310 baddr.sin_family = AF_INET;
311 baddr.sin_port = htons (PMAPPORT);
312 baddr.sin_addr.s_addr = htonl (INADDR_ANY);
313 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
314 msg.rm_xid = xid = _create_xid ();
316 msg.rm_direction = CALL;
317 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
318 msg.rm_call.cb_prog = PMAPPROG;
319 msg.rm_call.cb_vers = PMAPVERS;
320 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
321 msg.rm_call.cb_cred = unix_auth->ah_cred;
322 msg.rm_call.cb_verf = unix_auth->ah_verf;
329 r.xdr_results = xresults;
330 r.results_ptr = resultsp;
331 xdrmem_create (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
332 if ((!xdr_callmsg (xdrs, &msg)) || (!xdr_rmtcall_args (xdrs, &a)))
334 stat = RPC_CANTENCODEARGS;
337 outlen = (int) xdr_getpos (xdrs);
340 * Basic loop: broadcast a packet and wait a while for response(s).
341 * The response timeout grows larger per iteration.
343 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
345 for (i = 0; i < nets; i++)
347 baddr.sin_addr = addrs[i];
348 if (sendto (sock, outbuf, outlen, 0,
349 (struct sockaddr *) &baddr,
350 sizeof (struct sockaddr)) != outlen)
352 perror (_("Cannot send broadcast packet"));
357 if (eachresult == NULL)
363 msg.acpted_rply.ar_verf = _null_auth;
364 msg.acpted_rply.ar_results.where = (caddr_t) & r;
365 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
366 milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
367 switch (poll(&fd, 1, milliseconds))
370 case 0: /* timed out */
374 case -1: /* some kind of error */
377 perror (_("Broadcast poll problem"));
381 } /* end of poll results switch */
383 fromlen = sizeof (struct sockaddr);
384 inlen = recvfrom (sock, inbuf, UDPMSGSIZE, 0,
385 (struct sockaddr *) &raddr, &fromlen);
390 perror (_("Cannot receive reply to broadcast"));
394 if ((size_t) inlen < sizeof (u_long))
397 * see if reply transaction id matches sent id.
398 * If so, decode the results.
400 xdrmem_create (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
401 if (xdr_replymsg (xdrs, &msg))
403 if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
404 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
405 (msg.acpted_rply.ar_stat == SUCCESS))
407 raddr.sin_port = htons ((u_short) port);
408 done = (*eachresult) (resultsp, &raddr);
410 /* otherwise, we just ignore the errors ... */
415 /* some kind of deserialization problem ... */
416 if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
417 fprintf (stderr, "Broadcast deserialization problem");
418 /* otherwise, just random garbage */
421 xdrs->x_op = XDR_FREE;
422 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
423 (void) xdr_replymsg (xdrs, &msg);
424 (void) (*xresults) (xdrs, resultsp);
438 AUTH_DESTROY (unix_auth);