source: rtems/cpukit/librpc/src/rpc/pmap_rmt.c @ 4202a31

5
Last change on this file since 4202a31 was 4202a31, checked in by Nick Withers <nick.withers@…>, on 12/10/15 at 09:01:52

Chase Newlib sys/types.h / sys/select.h changes

  • Property mode set to 100644
File size: 11.9 KB
Line 
1/*
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.
8 *
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.
12 *
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.
16 *
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.
20 *
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.
24 *
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California  94043
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31/*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/
32/*static char *sccsid = "from: @(#)pmap_rmt.c   2.2 88/08/01 4.0 RPCSRC";*/
33static char *rcsid = "$FreeBSD: src/lib/libc/rpc/pmap_rmt.c,v 1.15 2000/01/27 23:06:40 jasone Exp $";
34#endif
35
36/*
37 * pmap_rmt.c
38 * Client interface to pmap rpc service.
39 * remote call and broadcast service
40 *
41 * Copyright (C) 1984, Sun Microsystems, Inc.
42 */
43
44#ifdef HAVE_CONFIG_H
45#include "config.h"
46#endif
47
48#include <sys/ioctl.h>
49#include <sys/param.h>
50#include <sys/socket.h>
51
52#include <net/if.h>
53#include <netinet/in.h>
54#include <arpa/inet.h>
55
56#include <assert.h>
57#include <errno.h>
58#include <stdio.h>
59#include <string.h>
60#include <unistd.h>
61
62#include <rpc/rpc.h>
63#include <rpc/pmap_prot.h>
64#include <rpc/pmap_clnt.h>
65#include <rpc/pmap_rmt.h>
66
67#include <stdlib.h>
68#include <sys/select.h>
69
70#define MAX_BROADCAST_SIZE 1400
71
72static const struct timeval timeout = { 3, 0 };
73
74/*
75 * pmapper remote-call-service interface.
76 * This routine is used to call the pmapper remote call service
77 * which will look up a service program in the port maps, and then
78 * remotely call that routine with the given parameters.  This allows
79 * programs to do a lookup and call in one step.
80*/
81enum clnt_stat
82pmap_rmtcall(
83        struct sockaddr_in *addr,
84        u_long prog,
85        u_long vers,
86        u_long proc,
87        xdrproc_t xdrargs,
88        caddr_t argsp,
89        xdrproc_t xdrres,
90        caddr_t resp,
91        struct timeval tout,
92        u_long *port_ptr)
93{
94        int sock = -1;
95        CLIENT *client;
96        struct rmtcallargs a;
97        struct rmtcallres r;
98        enum clnt_stat stat;
99
100        assert(addr != NULL);
101        assert(port_ptr != NULL);
102
103        addr->sin_port = htons(PMAPPORT);
104        client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
105        if (client != NULL) {
106                a.prog = prog;
107                a.vers = vers;
108                a.proc = proc;
109                a.args_ptr = argsp;
110                a.xdr_args = xdrargs;
111                r.port_ptr = port_ptr;
112                r.results_ptr = resp;
113                r.xdr_results = xdrres;
114                stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
115                    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
116                    &r, tout);
117                CLNT_DESTROY(client);
118        } else {
119                stat = RPC_FAILED;
120        }
121        if (sock != -1)
122                (void)_RPC_close(sock);
123        addr->sin_port = 0;
124        return (stat);
125}
126
127
128/*
129 * XDR remote call arguments
130 * written for XDR_ENCODE direction only
131 */
132bool_t
133xdr_rmtcall_args(
134        XDR *xdrs,
135        struct rmtcallargs *cap)
136{
137        u_int lenposition, argposition, position;
138
139        assert(xdrs != NULL);
140        assert(cap != NULL);
141
142        if (xdr_u_long(xdrs, &(cap->prog)) &&
143            xdr_u_long(xdrs, &(cap->vers)) &&
144            xdr_u_long(xdrs, &(cap->proc))) {
145                lenposition = XDR_GETPOS(xdrs);
146                if (! xdr_u_long(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 = (u_long)position - (u_long)argposition;
153                XDR_SETPOS(xdrs, lenposition);
154                if (! xdr_u_long(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 */
166bool_t
167xdr_rmtcallres(
168        XDR *xdrs,
169        struct rmtcallres *crp)
170{
171        caddr_t port_ptr;
172
173        assert(xdrs != NULL);
174        assert(crp != NULL);
175
176        port_ptr = (caddr_t)(void *)crp->port_ptr;
177        if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
178            (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
179                crp->port_ptr = (u_long *)(void *)port_ptr;
180                return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
181        }
182        return (FALSE);
183}
184
185
186/*
187 * The following is kludged-up support for simple rpc broadcasts.
188 * Someday a large, complicated system will replace these trivial
189 * routines which only support udp/ip .
190 */
191
192static int
193getbroadcastnets(
194        struct in_addr *addrs,
195        int sock,  /* any valid socket will do */
196        char *buf  /* why allocxate more when we can use existing... */  )
197{
198        struct ifconf ifc;
199        struct ifreq ifreq, *ifr;
200        struct sockaddr_in *sin;
201        struct  in_addr addr;
202        char *cp, *cplim;
203        int n, i = 0;
204
205        ifc.ifc_len = UDPMSGSIZE;
206        ifc.ifc_buf = buf;
207        if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
208                perror("broadcast: ioctl (get interface configuration)");
209                return (0);
210        }
211#define max(a, b) (a > b ? a : b)
212#define size(p) max((p).sa_len, sizeof(p))
213        cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
214        for (cp = buf; cp < cplim;
215                        cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
216                ifr = (struct ifreq *)cp;
217                if (ifr->ifr_addr.sa_family != AF_INET)
218                        continue;
219                ifreq = *ifr;
220                if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
221                        perror("broadcast: ioctl (get interface flags)");
222                        continue;
223                }
224                if ((ifreq.ifr_flags & IFF_BROADCAST) &&
225                    (ifreq.ifr_flags & IFF_UP)) {
226                        sin = (struct sockaddr_in *)&ifr->ifr_addr;
227#ifdef SIOCGIFBRDADDR   /* 4.3BSD */
228                        if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
229                                addr =
230                                    inet_makeaddr(inet_netof(sin->sin_addr),
231                                    INADDR_ANY);
232                        } else {
233                                addr = ((struct sockaddr_in*)
234                                  &ifreq.ifr_addr)->sin_addr;
235                        }
236#else /* 4.2 BSD */
237                        addr = inet_makeaddr(inet_netof(sin->sin_addr),
238                            INADDR_ANY);
239#endif
240                        for (n=i-1; n>=0; n--) {
241                                if (addr.s_addr == addrs[n].s_addr)
242                                        break;
243                        }
244                        if (n<0) {
245                                addrs[i++] = addr;
246                        }
247                }
248        }
249        return (i);
250}
251
252typedef bool_t (*resultproc_t)(caddr_t, struct sockaddr_in *);
253
254enum clnt_stat
255clnt_broadcast(
256        u_long          prog,           /* program number */
257        u_long          vers,           /* version number */
258        u_long          proc,           /* procedure number */
259        xdrproc_t       xargs,          /* xdr routine for args */
260        caddr_t         argsp,          /* pointer to args */
261        xdrproc_t       xresults,       /* xdr routine for results */
262        caddr_t         resultsp,       /* pointer to results */
263        resultproc_t    eachresult      /* call with each result obtained */ )
264{
265        enum clnt_stat stat = RPC_SUCCESS; /* to avoid warning */
266        AUTH *unix_auth = authunix_create_default();
267        XDR xdr_stream;
268        register XDR *xdrs = &xdr_stream;
269        int outlen, inlen, nets;
270        socklen_t fromlen;
271        register int sock;
272        int on = 1;
273        fd_set *fds = 0, readfds; /* initialized to avoid warning */
274        register int i;
275        bool_t done = FALSE;
276        register u_long xid;
277        u_long port;
278        struct in_addr addrs[20];
279        struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
280        struct rmtcallargs a;
281        struct rmtcallres r;
282        struct rpc_msg msg;
283        struct timeval t, tv;
284        char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
285        static uintptr_t disrupt;
286
287        if (disrupt == 0)
288                disrupt = (uintptr_t) resultsp;
289
290        /*
291         * initialization: create a socket, a broadcast address, and
292         * preserialize the arguments into a send buffer.
293         */
294        if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
295                perror("Cannot create socket for broadcast rpc");
296                stat = RPC_CANTSEND;
297                goto done_broad;
298        }
299#ifdef SO_BROADCAST
300        if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
301                perror("Cannot set socket option SO_BROADCAST");
302                stat = RPC_CANTSEND;
303                goto done_broad;
304        }
305#endif /* def SO_BROADCAST */
306        if (sock + 1 > FD_SETSIZE) {
307                int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
308                fds = (fd_set *)malloc(bytes);
309                if (fds == NULL) {
310                        stat = RPC_CANTSEND;
311                        goto done_broad;
312                }
313                memset(fds, 0, bytes);
314        } else {
315                fds = &readfds;
316                FD_ZERO(fds);
317        }
318
319        nets = getbroadcastnets(addrs, sock, inbuf);
320        memset(&baddr, 0, sizeof (baddr));
321        baddr.sin_len = sizeof(struct sockaddr_in);
322        baddr.sin_family = AF_INET;
323        baddr.sin_port = htons(PMAPPORT);
324        baddr.sin_addr.s_addr = htonl(INADDR_ANY);
325        (void)gettimeofday(&t, (struct timezone *)0);
326        msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
327        t.tv_usec = 0;
328        msg.rm_direction = CALL;
329        msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
330        msg.rm_call.cb_prog = PMAPPROG;
331        msg.rm_call.cb_vers = PMAPVERS;
332        msg.rm_call.cb_proc = PMAPPROC_CALLIT;
333        msg.rm_call.cb_cred = unix_auth->ah_cred;
334        msg.rm_call.cb_verf = unix_auth->ah_verf;
335        a.prog = prog;
336        a.vers = vers;
337        a.proc = proc;
338        a.xdr_args = xargs;
339        a.args_ptr = argsp;
340        r.port_ptr = &port;
341        r.xdr_results = xresults;
342        r.results_ptr = resultsp;
343        xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
344        if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
345                stat = RPC_CANTENCODEARGS;
346                goto done_broad;
347        }
348        outlen = (int)xdr_getpos(xdrs);
349        xdr_destroy(xdrs);
350        /*
351         * Basic loop: broadcast a packet and wait a while for response(s).
352         * The response timeout grows larger per iteration.
353         *
354         * XXX This will loop about 5 times the stop. If there are
355         * lots of signals being received by the process it will quit
356         * send them all in one quick burst, not paying attention to
357         * the intended function of sending them slowly over half a
358         * minute or so
359         */
360        for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
361                for (i = 0; i < nets; i++) {
362                        baddr.sin_addr = addrs[i];
363                        if (sendto(sock, outbuf, outlen, 0,
364                                (struct sockaddr *)&baddr,
365                                sizeof (struct sockaddr)) != outlen) {
366                                perror("Cannot send broadcast packet");
367                                stat = RPC_CANTSEND;
368                                goto done_broad;
369                        }
370                }
371                if (eachresult == NULL) {
372                        stat = RPC_SUCCESS;
373                        goto done_broad;
374                }
375        recv_again:
376                msg.acpted_rply.ar_verf = _null_auth;
377                msg.acpted_rply.ar_results.where = (caddr_t)&r;
378                msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
379                /* XXX we know the other bits are still clear */
380                FD_SET(sock, fds);
381                tv = t;         /* for select() that copies back */
382                switch (select(sock + 1, fds, NULL, NULL, &tv)) {
383
384                case 0:  /* timed out */
385                        stat = RPC_TIMEDOUT;
386                        continue;
387
388                case -1:  /* some kind of error */
389                        if (errno == EINTR)
390                                goto recv_again;
391                        perror("Broadcast select problem");
392                        stat = RPC_CANTRECV;
393                        goto done_broad;
394
395                }  /* end of select results switch */
396        try_again:
397                fromlen = sizeof(struct sockaddr);
398                inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
399                        (struct sockaddr *)&raddr, &fromlen);
400                if (inlen < 0) {
401                        if (errno == EINTR)
402                                goto try_again;
403                        perror("Cannot receive reply to broadcast");
404                        stat = RPC_CANTRECV;
405                        goto done_broad;
406                }
407                if (inlen < sizeof(u_int32_t))
408                        goto recv_again;
409                /*
410                 * see if reply transaction id matches sent id.
411                 * If so, decode the results.
412                 */
413                xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
414                if (xdr_replymsg(xdrs, &msg)) {
415                        if ((msg.rm_xid == xid) &&
416                                (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
417                                (msg.acpted_rply.ar_stat == SUCCESS)) {
418                                raddr.sin_port = htons((u_short)port);
419                                done = (*eachresult)(resultsp, &raddr);
420                        }
421                        /* otherwise, we just ignore the errors ... */
422                }
423                xdrs->x_op = XDR_FREE;
424                msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
425                (void)xdr_replymsg(xdrs, &msg);
426                (void)(*xresults)(xdrs, resultsp);
427                xdr_destroy(xdrs);
428                if (done) {
429                        stat = RPC_SUCCESS;
430                        goto done_broad;
431                } else {
432                        goto recv_again;
433                }
434        }
435done_broad:
436        if (fds != &readfds)
437                free(fds);
438        if (sock >= 0)
439                (void)_RPC_close(sock);
440        AUTH_DESTROY(unix_auth);
441        return (stat);
442}
Note: See TracBrowser for help on using the repository browser.