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

4.104.114.84.95
Last change on this file since f26145b was 77b6a10, checked in by Joel Sherrill <joel.sherrill@…>, on 01/09/05 at 17:12:03

2005-01-09 Joel Sherrill <joel@…>

  • librpc/include/rpc/clnt.h, librpc/src/rpc/authunix_prot.c, librpc/src/rpc/clnt_tcp.c, librpc/src/rpc/pmap_prot2.c, librpc/src/rpc/pmap_rmt.c, librpc/src/rpc/rtems_portmapper.c, librpc/src/rpc/svc_simple.c: Fix warnings.
  • Property mode set to 100644
File size: 11.8 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#include <rpc/rpc.h>
45#include <rpc/pmap_prot.h>
46#include <rpc/pmap_clnt.h>
47#include <rpc/pmap_rmt.h>
48#include <sys/socket.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <errno.h>
53#include <string.h>
54#include <net/if.h>
55#include <sys/ioctl.h>
56#include <arpa/inet.h>
57#include <sys/select.h>
58#define MAX_BROADCAST_SIZE 1400
59
60static struct timeval timeout = { 3, 0 };
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*/
69enum clnt_stat
70pmap_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        if (socket != -1)
102                (void)_RPC_close(socket);
103        addr->sin_port = 0;
104        return (stat);
105}
106
107
108/*
109 * XDR remote call arguments
110 * written for XDR_ENCODE direction only
111 */
112bool_t
113xdr_rmtcall_args(xdrs, cap)
114        register XDR *xdrs;
115        register struct rmtcallargs *cap;
116{
117        u_int lenposition, argposition, position;
118
119        if (xdr_u_long(xdrs, &(cap->prog)) &&
120            xdr_u_long(xdrs, &(cap->vers)) &&
121            xdr_u_long(xdrs, &(cap->proc))) {
122                lenposition = XDR_GETPOS(xdrs);
123                if (! xdr_u_long(xdrs, &(cap->arglen)))
124                    return (FALSE);
125                argposition = XDR_GETPOS(xdrs);
126                if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
127                    return (FALSE);
128                position = XDR_GETPOS(xdrs);
129                cap->arglen = (u_long)position - (u_long)argposition;
130                XDR_SETPOS(xdrs, lenposition);
131                if (! xdr_u_long(xdrs, &(cap->arglen)))
132                    return (FALSE);
133                XDR_SETPOS(xdrs, position);
134                return (TRUE);
135        }
136        return (FALSE);
137}
138
139/*
140 * XDR remote call results
141 * written for XDR_DECODE direction only
142 */
143bool_t
144xdr_rmtcallres(xdrs, crp)
145        register XDR *xdrs;
146        register struct rmtcallres *crp;
147{
148        caddr_t port_ptr;
149
150        port_ptr = (caddr_t)crp->port_ptr;
151        if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
152            (xdrproc_t) xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
153                crp->port_ptr = (u_long *)port_ptr;
154                return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
155        }
156        return (FALSE);
157}
158
159
160/*
161 * The following is kludged-up support for simple rpc broadcasts.
162 * Someday a large, complicated system will replace these trivial
163 * routines which only support udp/ip .
164 */
165
166static int
167getbroadcastnets(addrs, sock, buf)
168        struct in_addr *addrs;
169        int sock;  /* any valid socket will do */
170        char *buf;  /* why allocxate more when we can use existing... */
171{
172        struct ifconf ifc;
173        struct ifreq ifreq, *ifr;
174        struct sockaddr_in *sin;
175        struct  in_addr addr;
176        char *cp, *cplim;
177        int n, i = 0;
178
179        ifc.ifc_len = UDPMSGSIZE;
180        ifc.ifc_buf = buf;
181        if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
182                perror("broadcast: ioctl (get interface configuration)");
183                return (0);
184        }
185#define max(a, b) (a > b ? a : b)
186#define size(p) max((p).sa_len, sizeof(p))
187        cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
188        for (cp = buf; cp < cplim;
189                        cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
190                ifr = (struct ifreq *)cp;
191                if (ifr->ifr_addr.sa_family != AF_INET)
192                        continue;
193                ifreq = *ifr;
194                if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
195                        perror("broadcast: ioctl (get interface flags)");
196                        continue;
197                }
198                if ((ifreq.ifr_flags & IFF_BROADCAST) &&
199                    (ifreq.ifr_flags & IFF_UP)) {
200                        sin = (struct sockaddr_in *)&ifr->ifr_addr;
201#ifdef SIOCGIFBRDADDR   /* 4.3BSD */
202                        if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
203                                addr =
204                                    inet_makeaddr(inet_netof(sin->sin_addr),
205                                    INADDR_ANY);
206                        } else {
207                                addr = ((struct sockaddr_in*)
208                                  &ifreq.ifr_addr)->sin_addr;
209                        }
210#else /* 4.2 BSD */
211                        addr = inet_makeaddr(inet_netof(sin->sin_addr),
212                            INADDR_ANY);
213#endif
214                        for (n=i-1; n>=0; n--) {
215                                if (addr.s_addr == addrs[n].s_addr)
216                                        break;
217                        }
218                        if (n<0) {
219                                addrs[i++] = addr;
220                        }
221                }
222        }
223        return (i);
224}
225
226typedef bool_t (*resultproc_t)();
227
228enum clnt_stat
229clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
230        u_long          prog;           /* program number */
231        u_long          vers;           /* version number */
232        u_long          proc;           /* procedure number */
233        xdrproc_t       xargs;          /* xdr routine for args */
234        caddr_t         argsp;          /* pointer to args */
235        xdrproc_t       xresults;       /* xdr routine for results */
236        caddr_t         resultsp;       /* pointer to results */
237        resultproc_t    eachresult;     /* call with each result obtained */
238{
239        enum clnt_stat stat = RPC_SUCCESS; /* to avoid warning */
240        AUTH *unix_auth = authunix_create_default();
241        XDR xdr_stream;
242        register XDR *xdrs = &xdr_stream;
243        int outlen, inlen, fromlen, nets;
244        register int sock;
245        int on = 1;
246        fd_set *fds = 0, readfds; /* initialized to avoid warning */
247        register int i;
248        bool_t done = FALSE;
249        register u_long xid;
250        u_long port;
251        struct in_addr addrs[20];
252        struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
253        struct rmtcallargs a;
254        struct rmtcallres r;
255        struct rpc_msg msg;
256        struct timeval t, tv;
257        char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
258        static u_int32_t disrupt;
259
260        if (disrupt == 0)
261                disrupt = (u_int32_t)(long)resultsp;
262
263        /*
264         * initialization: create a socket, a broadcast address, and
265         * preserialize the arguments into a send buffer.
266         */
267        if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
268                perror("Cannot create socket for broadcast rpc");
269                stat = RPC_CANTSEND;
270                goto done_broad;
271        }
272#ifdef SO_BROADCAST
273        if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
274                perror("Cannot set socket option SO_BROADCAST");
275                stat = RPC_CANTSEND;
276                goto done_broad;
277        }
278#endif /* def SO_BROADCAST */
279        if (sock + 1 > FD_SETSIZE) {
280                int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
281                fds = (fd_set *)malloc(bytes);
282                if (fds == NULL) {
283                        stat = RPC_CANTSEND;
284                        goto done_broad;
285                }
286                memset(fds, 0, bytes);
287        } else {
288                fds = &readfds;
289                FD_ZERO(fds);
290        }
291
292        nets = getbroadcastnets(addrs, sock, inbuf);
293        memset(&baddr, 0, sizeof (baddr));
294        baddr.sin_len = sizeof(struct sockaddr_in);
295        baddr.sin_family = AF_INET;
296        baddr.sin_port = htons(PMAPPORT);
297        baddr.sin_addr.s_addr = htonl(INADDR_ANY);
298        (void)gettimeofday(&t, (struct timezone *)0);
299        msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
300        t.tv_usec = 0;
301        msg.rm_direction = CALL;
302        msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
303        msg.rm_call.cb_prog = PMAPPROG;
304        msg.rm_call.cb_vers = PMAPVERS;
305        msg.rm_call.cb_proc = PMAPPROC_CALLIT;
306        msg.rm_call.cb_cred = unix_auth->ah_cred;
307        msg.rm_call.cb_verf = unix_auth->ah_verf;
308        a.prog = prog;
309        a.vers = vers;
310        a.proc = proc;
311        a.xdr_args = xargs;
312        a.args_ptr = argsp;
313        r.port_ptr = &port;
314        r.xdr_results = xresults;
315        r.results_ptr = resultsp;
316        xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
317        if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
318                stat = RPC_CANTENCODEARGS;
319                goto done_broad;
320        }
321        outlen = (int)xdr_getpos(xdrs);
322        xdr_destroy(xdrs);
323        /*
324         * Basic loop: broadcast a packet and wait a while for response(s).
325         * The response timeout grows larger per iteration.
326         *
327         * XXX This will loop about 5 times the stop. If there are
328         * lots of signals being received by the process it will quit
329         * send them all in one quick burst, not paying attention to
330         * the intended function of sending them slowly over half a
331         * minute or so
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 = (caddr_t)&r;
351                msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
352                /* XXX we know the other bits are still clear */
353                FD_SET(sock, fds);
354                tv = t;         /* for select() that copies back */
355                switch (select(sock + 1, fds, NULL, NULL, &tv)) {
356
357                case 0:  /* timed out */
358                        stat = RPC_TIMEDOUT;
359                        continue;
360
361                case -1:  /* some kind of error */
362                        if (errno == EINTR)
363                                goto recv_again;
364                        perror("Broadcast select problem");
365                        stat = RPC_CANTRECV;
366                        goto done_broad;
367
368                }  /* end of select results switch */
369        try_again:
370                fromlen = sizeof(struct sockaddr);
371                inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
372                        (struct sockaddr *)&raddr, &fromlen);
373                if (inlen < 0) {
374                        if (errno == EINTR)
375                                goto try_again;
376                        perror("Cannot receive reply to broadcast");
377                        stat = RPC_CANTRECV;
378                        goto done_broad;
379                }
380                if (inlen < sizeof(u_int32_t))
381                        goto recv_again;
382                /*
383                 * see if reply transaction id matches sent id.
384                 * If so, decode the results.
385                 */
386                xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
387                if (xdr_replymsg(xdrs, &msg)) {
388                        if ((msg.rm_xid == xid) &&
389                                (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
390                                (msg.acpted_rply.ar_stat == SUCCESS)) {
391                                raddr.sin_port = htons((u_short)port);
392                                done = (*eachresult)(resultsp, &raddr);
393                        }
394                        /* otherwise, we just ignore the errors ... */
395                }
396                xdrs->x_op = XDR_FREE;
397                msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
398                (void)xdr_replymsg(xdrs, &msg);
399                (void)(*xresults)(xdrs, resultsp);
400                xdr_destroy(xdrs);
401                if (done) {
402                        stat = RPC_SUCCESS;
403                        goto done_broad;
404                } else {
405                        goto recv_again;
406                }
407        }
408done_broad:
409        if (fds != &readfds)
410                free(fds);
411        if (sock >= 0)
412                (void)_RPC_close(sock);
413        AUTH_DESTROY(unix_auth);
414        return (stat);
415}
Note: See TracBrowser for help on using the repository browser.