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

4.104.115
Last change on this file since 9ff6462 was 9ff6462, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/29/10 at 07:43:32

2010-04-29 Ralf Corsépius <ralf.corsepius@…>

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