2 static char sfs_pmap_rmt_id[] = "@(#)pmap_rmt.c 2.1 97/10/23";
4 /* @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC */
6 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8 * Standard Performance Evaluation Corporation (SPEC)
9 * 6585 Merchant Place, Suite 100
12 * This product contains benchmarks acquired from several sources who
13 * understand and agree with SPEC's goal of creating fair and objective
14 * benchmarks to measure computer performance.
16 * This copyright notice is placed here only to protect SPEC in the
17 * event the source is misused in any manner that is contrary to the
18 * spirit, the goals and the intent of SPEC.
20 * The source code is provided to the user or company under the license
21 * agreement for the SPEC Benchmark Suite for this product.
24 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25 * unrestricted use provided that this legend is included on all tape
26 * media and as a part of the software program in whole or part. Users
27 * may copy or modify Sun RPC without charge, but are not authorized
28 * to license or distribute it to anyone else except as part of a product or
29 * program developed by the user.
31 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 * Sun RPC is provided with no support and without any obligation on the
36 * part of Sun Microsystems, Inc. to assist in its use, correction,
37 * modification or enhancement.
39 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41 * OR ANY PART THEREOF.
43 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44 * or profits or other special, indirect and consequential damages, even if
45 * Sun has been advised of the possibility of such damages.
47 * Sun Microsystems, Inc.
49 * Mountain View, California 94043
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
57 * Client interface to pmap rpc service.
58 * remote call and broadcast service
60 * Copyright (C) 1984, Sun Microsystems, Inc.
68 #endif /* ndef FreeBSD */
72 #include "rpc/pmap_prot.h"
73 #include "rpc/pmap_clnt.h"
74 #include "rpc/pmap_rmt.h"
75 #include "rpc/osdep.h"
77 #define MAX_BROADCAST_SIZE 1400
79 static struct timeval timeout = { 3, 0 };
83 * pmapper remote-call-service interface.
84 * This routine is used to call the pmapper remote call service
85 * which will look up a service program in the port maps, and then
86 * remotely call that routine with the given parameters. This allows
87 * programs to do a lookup and call in one step.
91 struct sockaddr_in *addr,
103 register CLIENT *client;
104 struct rmtcallargs a;
108 addr->sin_port = htons(PMAPPORT);
109 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
110 if (client != (CLIENT *)NULL) {
115 a.xdr_args = xdrargs;
116 r.port_ptr = port_ptr;
117 r.results_ptr = resp;
118 r.xdr_results = xdrres;
119 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
120 xdr_rmtcallres, &r, tout);
121 CLNT_DESTROY(client);
132 * XDR remote call arguments
133 * written for XDR_ENCODE direction only
138 struct rmtcallargs *cap)
140 uint_t lenposition, argposition, position;
142 if (xdr_uint32_t(xdrs, &(cap->prog)) &&
143 xdr_uint32_t(xdrs, &(cap->vers)) &&
144 xdr_uint32_t(xdrs, &(cap->proc))) {
145 lenposition = XDR_GETPOS(xdrs);
146 if (! xdr_uint32_t(xdrs, &(cap->arglen)))
148 argposition = XDR_GETPOS(xdrs);
149 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
151 position = XDR_GETPOS(xdrs);
152 cap->arglen = (uint32_t)position - (uint32_t)argposition;
153 XDR_SETPOS(xdrs, lenposition);
154 if (! xdr_uint32_t(xdrs, &(cap->arglen)))
156 XDR_SETPOS(xdrs, position);
163 * XDR remote call results
164 * written for XDR_DECODE direction only
169 struct rmtcallres *crp)
173 port_ptr = (void *)crp->port_ptr;
174 if (xdr_reference(xdrs, &port_ptr, sizeof (uint32_t),
175 xdr_uint32_t) && xdr_uint32_t(xdrs, &crp->resultslen)) {
176 crp->port_ptr = (uint32_t *)port_ptr;
177 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
184 * The following is kludged-up support for simple rpc broadcasts.
185 * Someday a large, complicated system will replace these trivial
186 * routines which only support udp/ip .
191 struct in_addr *addrs,
196 struct ifreq ifreq, *ifr;
197 struct sockaddr_in *sin;
200 ifc.ifc_len = UDPMSGSIZE;
202 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
203 perror("broadcast: ioctl (get interface configuration)");
207 for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
209 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
210 perror("broadcast: ioctl (get interface flags)");
213 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
214 (ifreq.ifr_flags & IFF_UP) &&
215 ifr->ifr_addr.sa_family == AF_INET) {
216 sin = (struct sockaddr_in *)&ifr->ifr_addr;
217 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
218 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
219 addrs[i++] = inet_makeaddr((int)inet_netof
220 (sin->sin_addr), INADDR_ANY);
222 addrs[i++] = ((struct sockaddr_in*)
223 &ifreq.ifr_addr)->sin_addr;
226 addrs[i++] = inet_makeaddr(inet_netof
227 (sin->sin_addr.s_addr), INADDR_ANY);
236 uint32_t prog, /* program number */
237 uint32_t vers, /* version number */
238 uint32_t proc, /* procedure number */
239 xdrproc_t xargs, /* xdr routine for args */
240 void * argsp, /* pointer to args */
241 xdrproc_t xresults, /* xdr routine for results */
242 void * resultsp, /* pointer to results */
243 resultproc_t eachresult) /* call with each result obtained */
246 AUTH *unix_auth = authunix_create_default();
248 register XDR *xdrs = &xdr_stream;
249 int outlen, inlen, nets;
263 #endif /* def FD_SETSIZE */
266 register uint32_t xid;
268 struct in_addr addrs[20];
269 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
270 struct rmtcallargs a;
274 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
277 * initialization: create a socket, a broadcast address, and
278 * preserialize the arguments into a send buffer.
280 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
281 perror("Cannot create socket for broadcast rpc");
286 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof (on)) < 0) {
287 perror("Cannot set socket option SO_BROADCAST");
291 #endif /* def SO_BROADCAST */
297 #endif /* def FD_SETSIZE */
298 nets = getbroadcastnets(addrs, sock, inbuf);
299 memset((char *)&baddr, '\0', sizeof (baddr));
300 baddr.sin_family = AF_INET;
301 baddr.sin_port = htons(PMAPPORT);
302 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
303 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
304 (void)gettimeofday(&t, (struct timezone *)0);
305 msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
307 msg.rm_direction = CALL;
308 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
309 msg.rm_call.cb_prog = PMAPPROG;
310 msg.rm_call.cb_vers = PMAPVERS;
311 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
312 msg.rm_call.cb_cred = unix_auth->ah_cred;
313 msg.rm_call.cb_verf = unix_auth->ah_verf;
320 r.xdr_results = xresults;
321 r.results_ptr = resultsp;
322 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
323 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
324 stat = RPC_CANTENCODEARGS;
327 outlen = (int)xdr_getpos(xdrs);
330 * Basic loop: broadcast a packet and wait a while for response(s).
331 * The response timeout grows larger per iteration.
333 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) {
339 perror("Cannot send broadcast packet");
344 if (eachresult == NULL) {
349 msg.acpted_rply.ar_verf = _null_auth;
350 msg.acpted_rply.ar_results.where = (void *)&r;
351 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
353 switch (select(_rpc_dtablesize(), &readfds, NULL,
356 case 0: /* timed out */
360 case -1: /* some kind of error */
363 perror("Broadcast select problem");
367 } /* end of select results switch */
369 fromlen = sizeof(struct sockaddr);
370 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
371 (struct sockaddr *)&raddr, &fromlen);
375 perror("Cannot receive reply to broadcast");
379 if (inlen < sizeof(uint32_t))
382 * see if reply transaction id matches sent id.
383 * If so, decode the results.
385 xdrmem_create(xdrs, inbuf, (uint_t)inlen, XDR_DECODE);
386 if (xdr_replymsg(xdrs, &msg)) {
387 if ((msg.rm_xid == xid) &&
388 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
389 (msg.acpted_rply.ar_stat == SUCCESS)) {
390 raddr.sin_port = htons((uint16_t)port);
391 done = (*eachresult)(resultsp, &raddr);
393 /* otherwise, we just ignore the errors ... */
396 /* some kind of deserialization problem ... */
397 if (msg.rm_xid == xid)
398 fprintf(stderr, "Broadcast deserialization problem");
399 /* otherwise, just random garbage */
402 xdrs->x_op = XDR_FREE;
403 msg.acpted_rply.ar_results.proc = xdr_void;
404 (void)xdr_replymsg(xdrs, &msg);
405 (void)(*xresults)(xdrs, resultsp);
416 AUTH_DESTROY(unix_auth);