8907929973151adfc27bcc03da6b0e58b87778ec
[platform/upstream/glibc.git] / sunrpc / pmap_rmt.c
1 /* @(#)pmap_rmt.c       2.2 88/08/01 4.0 RPCSRC */
2 /*
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.
9  *
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.
13  *
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.
17  *
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.
21  *
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.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * pmap_rmt.c
36  * Client interface to pmap rpc service.
37  * remote call and broadcast service
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  */
41
42 #include <rpc/rpc.h>
43 #include <rpc/pmap_prot.h>
44 #include <rpc/pmap_clnt.h>
45 #include <rpc/pmap_rmt.h>
46 #include <sys/socket.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #undef   _POSIX_SOURCE          /* Ultrix <sys/param.h> needs --roland@gnu */
50 #include <sys/param.h>          /* Ultrix needs before net/if --roland@gnu */
51 #include <net/if.h>
52 #include <sys/ioctl.h>
53 #include <arpa/inet.h>
54 #define MAX_BROADCAST_SIZE 1400
55
56 #ifndef errno
57 extern int errno;
58 #endif
59 static struct timeval timeout = { 3, 0 };
60
61
62 /*
63  * pmapper remote-call-service interface.
64  * This routine is used to call the pmapper remote call service
65  * which will look up a service program in the port maps, and then
66  * remotely call that routine with the given parameters.  This allows
67  * programs to do a lookup and call in one step.
68 */
69 enum clnt_stat
70 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
71         struct sockaddr_in *addr;
72         u_long prog, vers, proc;
73         xdrproc_t xdrargs, xdrres;
74         caddr_t argsp, resp;
75         struct timeval tout;
76         u_long *port_ptr;
77 {
78         int socket = -1;
79         register CLIENT *client;
80         struct rmtcallargs a;
81         struct rmtcallres r;
82         enum clnt_stat stat;
83
84         addr->sin_port = htons(PMAPPORT);
85         client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
86         if (client != (CLIENT *)NULL) {
87                 a.prog = prog;
88                 a.vers = vers;
89                 a.proc = proc;
90                 a.args_ptr = argsp;
91                 a.xdr_args = xdrargs;
92                 r.port_ptr = port_ptr;
93                 r.results_ptr = resp;
94                 r.xdr_results = xdrres;
95                 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
96                     xdr_rmtcallres, &r, tout);
97                 CLNT_DESTROY(client);
98         } else {
99                 stat = RPC_FAILED;
100         }
101         /* (void)close(socket); CLNT_DESTROY already closed it */
102         addr->sin_port = 0;
103         return (stat);
104 }
105
106
107 /*
108  * XDR remote call arguments
109  * written for XDR_ENCODE direction only
110  */
111 bool_t
112 xdr_rmtcall_args(xdrs, cap)
113         register XDR *xdrs;
114         register struct rmtcallargs *cap;
115 {
116         u_int lenposition, argposition, position;
117
118         if (xdr_u_long(xdrs, &(cap->prog)) &&
119             xdr_u_long(xdrs, &(cap->vers)) &&
120             xdr_u_long(xdrs, &(cap->proc))) {
121                 lenposition = XDR_GETPOS(xdrs);
122                 if (! xdr_u_long(xdrs, &(cap->arglen)))
123                     return (FALSE);
124                 argposition = XDR_GETPOS(xdrs);
125                 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
126                     return (FALSE);
127                 position = XDR_GETPOS(xdrs);
128                 cap->arglen = (u_long)position - (u_long)argposition;
129                 XDR_SETPOS(xdrs, lenposition);
130                 if (! xdr_u_long(xdrs, &(cap->arglen)))
131                     return (FALSE);
132                 XDR_SETPOS(xdrs, position);
133                 return (TRUE);
134         }
135         return (FALSE);
136 }
137
138 /*
139  * XDR remote call results
140  * written for XDR_DECODE direction only
141  */
142 bool_t
143 xdr_rmtcallres(xdrs, crp)
144         register XDR *xdrs;
145         register struct rmtcallres *crp;
146 {
147         caddr_t port_ptr;
148
149         port_ptr = (caddr_t)crp->port_ptr;
150         if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
151             xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
152                 crp->port_ptr = (u_long *)port_ptr;
153                 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
154         }
155         return (FALSE);
156 }
157
158
159 /*
160  * The following is kludged-up support for simple rpc broadcasts.
161  * Someday a large, complicated system will replace these trivial
162  * routines which only support udp/ip .
163  */
164
165 static int
166 getbroadcastnets(addrs, sock, buf)
167         struct in_addr *addrs;
168         int sock;  /* any valid socket will do */
169         char *buf;  /* why allocate more when we can use existing... */
170 {
171         struct ifconf ifc;
172         struct ifreq ifreq, *ifr;
173         struct sockaddr_in *sin;
174         int n, i;
175
176         ifc.ifc_len = UDPMSGSIZE;
177         ifc.ifc_buf = buf;
178         if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
179                 perror(_("broadcast: ioctl (get interface configuration)"));
180                 return (0);
181         }
182         ifr = ifc.ifc_req;
183         for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
184                 ifreq = *ifr;
185                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
186                         perror(_("broadcast: ioctl (get interface flags)"));
187                         continue;
188                 }
189                 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
190                     (ifreq.ifr_flags & IFF_UP) &&
191                     ifr->ifr_addr.sa_family == AF_INET) {
192                         sin = (struct sockaddr_in *)&ifr->ifr_addr;
193 #ifdef SIOCGIFBRDADDR   /* 4.3BSD */
194                         if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
195                                 addrs[i++] = inet_makeaddr(inet_netof
196                             /* Changed to pass struct instead of s_addr member
197                                by roland@gnu.  */
198                             (sin->sin_addr), INADDR_ANY);
199                         } else {
200                                 addrs[i++] = ((struct sockaddr_in*)
201                                   &ifreq.ifr_addr)->sin_addr;
202                         }
203 #else /* 4.2 BSD */
204                         addrs[i++] = inet_makeaddr(inet_netof
205                           (sin->sin_addr.s_addr), INADDR_ANY);
206 #endif
207                 }
208         }
209         return (i);
210 }
211
212
213 enum clnt_stat
214 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
215         u_long          prog;           /* program number */
216         u_long          vers;           /* version number */
217         u_long          proc;           /* procedure number */
218         xdrproc_t       xargs;          /* xdr routine for args */
219         caddr_t         argsp;          /* pointer to args */
220         xdrproc_t       xresults;       /* xdr routine for results */
221         caddr_t         resultsp;       /* pointer to results */
222         resultproc_t    eachresult;     /* call with each result obtained */
223 {
224         enum clnt_stat stat;
225         AUTH *unix_auth = authunix_create_default();
226         XDR xdr_stream;
227         register XDR *xdrs = &xdr_stream;
228         int outlen, inlen, fromlen, nets;
229         register int sock;
230         int on = 1;
231 #ifdef FD_SETSIZE
232         fd_set mask;
233         fd_set readfds;
234 #else
235         int readfds;
236         register int mask;
237 #endif /* def FD_SETSIZE */
238         register int i;
239         bool_t done = FALSE;
240         register u_long xid;
241         u_long port;
242         struct in_addr addrs[20];
243         struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
244         struct rmtcallargs a;
245         struct rmtcallres r;
246         struct rpc_msg msg;
247         struct timeval t, t1;
248         char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
249
250         /*
251          * initialization: create a socket, a broadcast address, and
252          * preserialize the arguments into a send buffer.
253          */
254         if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
255                 perror(_("Cannot create socket for broadcast rpc"));
256                 stat = RPC_CANTSEND;
257                 goto done_broad;
258         }
259 #ifdef SO_BROADCAST
260         if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
261                 perror(_("Cannot set socket option SO_BROADCAST"));
262                 stat = RPC_CANTSEND;
263                 goto done_broad;
264         }
265 #endif /* def SO_BROADCAST */
266 #ifdef FD_SETSIZE
267         FD_ZERO(&mask);
268         FD_SET(sock, &mask);
269 #else
270         mask = (1 << sock);
271 #endif /* def FD_SETSIZE */
272         nets = getbroadcastnets(addrs, sock, inbuf);
273         bzero((char *)&baddr, sizeof (baddr));
274         baddr.sin_family = AF_INET;
275         baddr.sin_port = htons(PMAPPORT);
276         baddr.sin_addr.s_addr = htonl(INADDR_ANY);
277 /*      baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
278         (void)gettimeofday(&t, (struct timezone *)0);
279         msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
280         t.tv_usec = 0;
281         msg.rm_direction = CALL;
282         msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
283         msg.rm_call.cb_prog = PMAPPROG;
284         msg.rm_call.cb_vers = PMAPVERS;
285         msg.rm_call.cb_proc = PMAPPROC_CALLIT;
286         msg.rm_call.cb_cred = unix_auth->ah_cred;
287         msg.rm_call.cb_verf = unix_auth->ah_verf;
288         a.prog = prog;
289         a.vers = vers;
290         a.proc = proc;
291         a.xdr_args = xargs;
292         a.args_ptr = argsp;
293         r.port_ptr = &port;
294         r.xdr_results = xresults;
295         r.results_ptr = resultsp;
296         xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
297         if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
298                 stat = RPC_CANTENCODEARGS;
299                 goto done_broad;
300         }
301         outlen = (int)xdr_getpos(xdrs);
302         xdr_destroy(xdrs);
303         /*
304          * Basic loop: broadcast a packet and wait a while for response(s).
305          * The response timeout grows larger per iteration.
306          */
307         for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
308                 for (i = 0; i < nets; i++) {
309                         baddr.sin_addr = addrs[i];
310                         if (sendto(sock, outbuf, outlen, 0,
311                                 (struct sockaddr *)&baddr,
312                                 sizeof (struct sockaddr)) != outlen) {
313                                 perror(_("Cannot send broadcast packet"));
314                                 stat = RPC_CANTSEND;
315                                 goto done_broad;
316                         }
317                 }
318                 if (eachresult == NULL) {
319                         stat = RPC_SUCCESS;
320                         goto done_broad;
321                 }
322         recv_again:
323                 msg.acpted_rply.ar_verf = _null_auth;
324                 msg.acpted_rply.ar_results.where = (caddr_t)&r;
325                 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
326                 readfds = mask;
327                 t1 = t;
328                 switch (select(_rpc_dtablesize(), &readfds, (int *)NULL,
329                                (int *)NULL, &t1)) {
330
331                 case 0:  /* timed out */
332                         stat = RPC_TIMEDOUT;
333                         continue;
334
335                 case -1:  /* some kind of error */
336                         if (errno == EINTR)
337                                 goto recv_again;
338                         perror(_("Broadcast select problem"));
339                         stat = RPC_CANTRECV;
340                         goto done_broad;
341
342                 }  /* end of select results switch */
343         try_again:
344                 fromlen = sizeof(struct sockaddr);
345                 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
346                         (struct sockaddr *)&raddr, &fromlen);
347                 if (inlen < 0) {
348                         if (errno == EINTR)
349                                 goto try_again;
350                         perror(_("Cannot receive reply to broadcast"));
351                         stat = RPC_CANTRECV;
352                         goto done_broad;
353                 }
354                 if (inlen < sizeof(u_long))
355                         goto recv_again;
356                 /*
357                  * see if reply transaction id matches sent id.
358                  * If so, decode the results.
359                  */
360                 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
361                 if (xdr_replymsg(xdrs, &msg)) {
362                         if ((msg.rm_xid == xid) &&
363                                 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
364                                 (msg.acpted_rply.ar_stat == SUCCESS)) {
365                                 raddr.sin_port = htons((u_short)port);
366                                 done = (*eachresult)(resultsp, &raddr);
367                         }
368                         /* otherwise, we just ignore the errors ... */
369                 } else {
370 #ifdef notdef
371                         /* some kind of deserialization problem ... */
372                         if (msg.rm_xid == xid)
373                                 fprintf(stderr, "Broadcast deserialization problem");
374                         /* otherwise, just random garbage */
375 #endif
376                 }
377                 xdrs->x_op = XDR_FREE;
378                 msg.acpted_rply.ar_results.proc = xdr_void;
379                 (void)xdr_replymsg(xdrs, &msg);
380                 (void)(*xresults)(xdrs, resultsp);
381                 xdr_destroy(xdrs);
382                 if (done) {
383                         stat = RPC_SUCCESS;
384                         goto done_broad;
385                 } else {
386                         goto recv_again;
387                 }
388         }
389 done_broad:
390         (void)close(sock);
391         AUTH_DESTROY(unix_auth);
392         return (stat);
393 }