Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / pmap_rmt.c
1 #ifndef lint
2 static char sfs_pmap_rmt_id[] = "@(#)pmap_rmt.c     2.1     97/10/23";
3 #endif
4 /* @(#)pmap_rmt.c       2.2 88/08/01 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
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.
15  *
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.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
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.
30  * 
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.
34  * 
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.
38  * 
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.
42  * 
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.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * pmap_rmt.c
57  * Client interface to pmap rpc service.
58  * remote call and broadcast service
59  *
60  * Copyright (C) 1984, Sun Microsystems, Inc.
61  */
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66 #ifndef FreeBSD
67 #include <stropts.h>
68 #endif /* ndef FreeBSD */
69 #include <string.h>
70 #include <errno.h>
71 #include "rpc/rpc.h"
72 #include "rpc/pmap_prot.h"
73 #include "rpc/pmap_clnt.h"
74 #include "rpc/pmap_rmt.h"
75 #include "rpc/osdep.h"
76
77 #define MAX_BROADCAST_SIZE 1400
78
79 static struct timeval timeout = { 3, 0 };
80
81
82 /*
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.
88 */
89 enum clnt_stat
90 pmap_rmtcall(
91         struct sockaddr_in *addr,
92         uint32_t prog,
93         uint32_t vers,
94         uint32_t proc,
95         xdrproc_t xdrargs,
96         void *argsp,
97         xdrproc_t xdrres,
98         void *resp,
99         struct timeval tout,
100         uint32_t *port_ptr)
101 {
102         int socket = -1;
103         register CLIENT *client;
104         struct rmtcallargs a;
105         struct rmtcallres r;
106         enum clnt_stat stat;
107
108         addr->sin_port = htons(PMAPPORT);
109         client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
110         if (client != (CLIENT *)NULL) {
111                 a.prog = prog;
112                 a.vers = vers;
113                 a.proc = proc;
114                 a.args_ptr = argsp;
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);
122         } else {
123                 stat = RPC_FAILED;
124         }
125         (void)close(socket);
126         addr->sin_port = 0;
127         return (stat);
128 }
129
130
131 /*
132  * XDR remote call arguments
133  * written for XDR_ENCODE direction only
134  */
135 bool_t
136 xdr_rmtcall_args(
137         XDR *xdrs,
138         struct rmtcallargs *cap)
139 {
140         uint_t lenposition, argposition, position;
141
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)))
147                     return (FALSE);
148                 argposition = XDR_GETPOS(xdrs);
149                 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
150                     return (FALSE);
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)))
155                     return (FALSE);
156                 XDR_SETPOS(xdrs, position);
157                 return (TRUE);
158         }
159         return (FALSE);
160 }
161
162 /*
163  * XDR remote call results
164  * written for XDR_DECODE direction only
165  */
166 bool_t
167 xdr_rmtcallres(
168         XDR *xdrs,
169         struct rmtcallres *crp)
170 {
171         void *port_ptr;
172
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));
178         }
179         return (FALSE);
180 }
181
182
183 /*
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 .
187  */
188
189 static int
190 getbroadcastnets(
191         struct in_addr *addrs,
192         int sock,
193         char *buf)
194 {
195         struct ifconf ifc;
196         struct ifreq ifreq, *ifr;
197         struct sockaddr_in *sin;
198         int n, i;
199
200         ifc.ifc_len = UDPMSGSIZE;
201         ifc.ifc_buf = buf;
202         if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
203                 perror("broadcast: ioctl (get interface configuration)");
204                 return (0);
205         }
206         ifr = ifc.ifc_req;
207         for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
208                 ifreq = *ifr;
209                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
210                         perror("broadcast: ioctl (get interface flags)");
211                         continue;
212                 }
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);
221                         } else {
222                                 addrs[i++] = ((struct sockaddr_in*)
223                                   &ifreq.ifr_addr)->sin_addr;
224                         }
225 #else /* 4.2 BSD */
226                         addrs[i++] = inet_makeaddr(inet_netof
227                           (sin->sin_addr.s_addr), INADDR_ANY);
228 #endif
229                 }
230         }
231         return (i);
232 }
233
234 enum clnt_stat 
235 clnt_broadcast(
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 */
244 {
245         enum clnt_stat stat;
246         AUTH *unix_auth = authunix_create_default();
247         XDR xdr_stream;
248         register XDR *xdrs = &xdr_stream;
249         int outlen, inlen, nets;
250 #if defined(AIX)
251         size_t fromlen;
252 #else
253         int fromlen;
254 #endif /* AIX */
255         register int sock;
256         int on = 1;
257 #ifdef FD_SETSIZE
258         fd_set mask;
259         fd_set readfds;
260 #else
261         int readfds;
262         register int mask;
263 #endif /* def FD_SETSIZE */
264         register int i;
265         bool_t done = FALSE;
266         register uint32_t xid;
267         uint32_t port;
268         struct in_addr addrs[20];
269         struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
270         struct rmtcallargs a;
271         struct rmtcallres r;
272         struct rpc_msg msg;
273         struct timeval t; 
274         char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
275
276         /*
277          * initialization: create a socket, a broadcast address, and
278          * preserialize the arguments into a send buffer.
279          */
280         if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
281                 perror("Cannot create socket for broadcast rpc");
282                 stat = RPC_CANTSEND;
283                 goto done_broad;
284         }
285 #ifdef SO_BROADCAST
286         if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof (on)) < 0) {
287                 perror("Cannot set socket option SO_BROADCAST");
288                 stat = RPC_CANTSEND;
289                 goto done_broad;
290         }
291 #endif /* def SO_BROADCAST */
292 #ifdef FD_SETSIZE
293         FD_ZERO(&mask);
294         FD_SET(sock, &mask);
295 #else
296         mask = (1 << sock);
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;
306         t.tv_usec = 0;
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;
314         a.prog = prog;
315         a.vers = vers;
316         a.proc = proc;
317         a.xdr_args = xargs;
318         a.args_ptr = argsp;
319         r.port_ptr = &port;
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;
325                 goto done_broad;
326         }
327         outlen = (int)xdr_getpos(xdrs);
328         xdr_destroy(xdrs);
329         /*
330          * Basic loop: broadcast a packet and wait a while for response(s).
331          * The response timeout grows larger per iteration.
332          */
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");
340                                 stat = RPC_CANTSEND;
341                                 goto done_broad;
342                         }
343                 }
344                 if (eachresult == NULL) {
345                         stat = RPC_SUCCESS;
346                         goto done_broad;
347                 }
348         recv_again:
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;
352                 readfds = mask;
353                 switch (select(_rpc_dtablesize(), &readfds, NULL, 
354                                NULL, &t)) {
355
356                 case 0:  /* timed out */
357                         stat = RPC_TIMEDOUT;
358                         continue;
359
360                 case -1:  /* some kind of error */
361                         if (errno == EINTR)
362                                 goto recv_again;
363                         perror("Broadcast select problem");
364                         stat = RPC_CANTRECV;
365                         goto done_broad;
366
367                 }  /* end of select results switch */
368         try_again:
369                 fromlen = sizeof(struct sockaddr);
370                 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
371                         (struct sockaddr *)&raddr, &fromlen);
372                 if (inlen < 0) {
373                         if (errno == EINTR)
374                                 goto try_again;
375                         perror("Cannot receive reply to broadcast");
376                         stat = RPC_CANTRECV;
377                         goto done_broad;
378                 }
379                 if (inlen < sizeof(uint32_t))
380                         goto recv_again;
381                 /*
382                  * see if reply transaction id matches sent id.
383                  * If so, decode the results.
384                  */
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);
392                         }
393                         /* otherwise, we just ignore the errors ... */
394                 } else {
395 #ifdef notdef
396                         /* some kind of deserialization problem ... */
397                         if (msg.rm_xid == xid)
398                                 fprintf(stderr, "Broadcast deserialization problem");
399                         /* otherwise, just random garbage */
400 #endif
401                 }
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);
406                 xdr_destroy(xdrs);
407                 if (done) {
408                         stat = RPC_SUCCESS;
409                         goto done_broad;
410                 } else {
411                         goto recv_again;
412                 }
413         }
414 done_broad:
415         (void)close(sock);
416         AUTH_DESTROY(unix_auth);
417         return (stat);
418 }
419