source: rtems/cpukit/librpc/src/rpc/clnt_udp.c @ d422a4d8

4.104.115
Last change on this file since d422a4d8 was d422a4d8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 05/27/10 at 12:39:33

2010-05-27 Ralf Corsépius <ralf.corsepius@…>

  • librpc/src/rpc/clnt_tcp.c, librpc/src/rpc/clnt_udp.c: Revert to using u_long instead of rpcprog_t, rpcvers_t to stay bug-ward compatible to freebsd.
  • Property mode set to 100644
File size: 15.3 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: @(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";*/
32/*static char *sccsid = "from: @(#)clnt_udp.c   2.2 88/08/01 4.0 RPCSRC";*/
33static char *rcsid = "$FreeBSD: src/lib/libc/rpc/clnt_udp.c,v 1.15 2000/01/27 23:06:36 jasone Exp $";
34#endif
35
36/*
37 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
38 *
39 * Copyright (C) 1984, Sun Microsystems, Inc.
40 */
41
42#ifdef HAVE_CONFIG_H
43#include "config.h"
44#endif
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <unistd.h>
49#include <string.h>
50#include <rpc/rpc.h>
51#include <sys/socket.h>
52#include <sys/ioctl.h>
53#include <netdb.h>
54#include <errno.h>
55#include <rpc/pmap_clnt.h>
56#include <sys/select.h>
57
58/*
59 * UDP bases client side rpc operations
60 */
61static enum clnt_stat   clntudp_call(CLIENT *, rpcproc_t, xdrproc_t, void*, xdrproc_t, void*, struct timeval);
62static void             clntudp_abort(void);
63static void             clntudp_geterr(CLIENT *, struct rpc_err*);
64static bool_t           clntudp_freeres(CLIENT *, xdrproc_t, void*);
65static bool_t           clntudp_control(CLIENT *, int, char *);
66static void             clntudp_destroy(CLIENT *);
67
68static struct clnt_ops udp_ops = {
69        clntudp_call,
70        clntudp_abort,
71        clntudp_geterr,
72        clntudp_freeres,
73        clntudp_destroy,
74        clntudp_control
75};
76
77/*
78 * Private data kept per client handle
79 */
80struct cu_data {
81        int                cu_sock;
82        bool_t                  cu_closeit;     /* opened by library */
83        struct sockaddr_in cu_raddr;
84        int                cu_rlen;
85        struct timeval          cu_wait;        /* retransmit interval */
86        struct timeval          cu_total;       /* total time for the call */
87        struct rpc_err     cu_error;
88        XDR                cu_outxdrs;
89        u_int              cu_xdrpos;
90        u_int                   cu_sendsz;      /* send size */
91        char               *cu_outbuf;
92        u_int                   cu_recvsz;      /* recv size */
93        char               cu_inbuf[1];
94};
95
96/*
97 * Create a UDP based client handle.
98 * If *sockp<0, *sockp is set to a newly created UPD socket.
99 * If raddr->sin_port is 0 a binder on the remote machine
100 * is consulted for the correct port number.
101 * NB: It is the clients responsibility to close *sockp.
102 * NB: The rpch->cl_auth is initialized to null authentication.
103 *     Caller may wish to set this something more useful.
104 *
105 * wait is the amount of time used between retransmitting a call if
106 * no response has been heard;  retransmition occurs until the actual
107 * rpc call times out.
108 *
109 * sendsz and recvsz are the maximum allowable packet sizes that can be
110 * sent and received.
111 */
112CLIENT *
113clntudp_bufcreate(
114        struct sockaddr_in *raddr,
115        u_long program,         /* program number */
116        u_long version,         /* version number */
117        struct timeval wait,
118        int *sockp,
119        u_int sendsz,
120        u_int recvsz)
121{
122        CLIENT *cl = NULL;              /* client handle */
123        struct cu_data *cu = NULL;      /* private data */
124        struct timeval now;
125        struct rpc_msg call_msg;
126        static uintptr_t disrupt;
127
128        if (disrupt == 0)
129                disrupt = (uintptr_t)raddr;
130
131        cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
132        if (cl == NULL) {
133                (void) fprintf(stderr, "clntudp_create: out of memory\n");
134                rpc_createerr.cf_stat = RPC_SYSTEMERROR;
135                rpc_createerr.cf_error.re_errno = errno;
136                goto fooy;
137        }
138        sendsz = ((sendsz + 3) / 4) * 4;
139        recvsz = ((recvsz + 3) / 4) * 4;
140        cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
141        if (cu == NULL) {
142                (void) fprintf(stderr, "clntudp_create: out of memory\n");
143                rpc_createerr.cf_stat = RPC_SYSTEMERROR;
144                rpc_createerr.cf_error.re_errno = errno;
145                goto fooy;
146        }
147        cu->cu_outbuf = &cu->cu_inbuf[recvsz];
148
149        (void)gettimeofday(&now, (struct timezone *)0);
150        if (raddr->sin_port == 0) {
151                u_short port;
152                if ((port =
153                    pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
154                        goto fooy;
155                }
156                raddr->sin_port = htons(port);
157        }
158        cl->cl_ops = &udp_ops;
159        cl->cl_private = (caddr_t)cu;
160        cu->cu_raddr = *raddr;
161        cu->cu_rlen = sizeof (cu->cu_raddr);
162        cu->cu_wait = wait;
163        cu->cu_total.tv_sec = -1;
164        cu->cu_total.tv_usec = -1;
165        cu->cu_sendsz = sendsz;
166        cu->cu_recvsz = recvsz;
167        call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
168        call_msg.rm_direction = CALL;
169        call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
170        call_msg.rm_call.cb_prog = program;
171        call_msg.rm_call.cb_vers = version;
172        xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
173            sendsz, XDR_ENCODE);
174        if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
175                goto fooy;
176        }
177        cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
178        if (*sockp < 0) {
179                int dontblock = 1;
180
181                *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
182                if (*sockp < 0) {
183                        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
184                        rpc_createerr.cf_error.re_errno = errno;
185                        goto fooy;
186                }
187                /* attempt to bind to priv port */
188                (void)bindresvport(*sockp, (struct sockaddr_in *)0);
189                /* the sockets rpc controls are non-blocking */
190                (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
191                cu->cu_closeit = TRUE;
192        } else {
193                cu->cu_closeit = FALSE;
194        }
195        cu->cu_sock = *sockp;
196        cl->cl_auth = authnone_create();
197        return (cl);
198fooy:
199        if (cu)
200                mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
201        if (cl)
202                mem_free((caddr_t)cl, sizeof(CLIENT));
203        return ((CLIENT *)NULL);
204}
205
206CLIENT *
207clntudp_create(
208        struct sockaddr_in *raddr,
209        u_long program,         /* program number */
210        u_long version,         /* version number */
211        struct timeval wait,
212        int *sockp)
213{
214
215        return(clntudp_bufcreate(raddr, program, version, wait, sockp,
216            UDPMSGSIZE, UDPMSGSIZE));
217}
218
219static enum clnt_stat
220clntudp_call(
221        CLIENT  *cl,                    /* client handle */
222        rpcproc_t       proc,           /* procedure number */
223        xdrproc_t       xargs,          /* xdr routine for args */
224        void            *argsp,         /* pointer to args */
225        xdrproc_t       xresults,       /* xdr routine for results */
226        void            *resultsp,      /* pointer to results */
227        struct timeval  utimeout )      /* seconds to wait before giving up */
228{
229        struct cu_data *cu = (struct cu_data *)cl->cl_private;
230        XDR *xdrs;
231        size_t outlen = 0;
232        int inlen;
233        socklen_t fromlen;
234        fd_set *fds, readfds;
235        struct sockaddr_in from;
236        struct rpc_msg reply_msg;
237        XDR reply_xdrs;
238        struct timeval time_waited, start, after, tmp1, tmp2, tv;
239        bool_t ok;
240        int nrefreshes = 2;     /* number of times to refresh cred */
241        struct timeval timeout;
242
243        if (cu->cu_total.tv_usec == -1)
244                timeout = utimeout;     /* use supplied timeout */
245        else
246                timeout = cu->cu_total; /* use default timeout */
247
248        if (cu->cu_sock + 1 > FD_SETSIZE) {
249                int bytes = howmany(cu->cu_sock + 1, NFDBITS) * sizeof(fd_mask);
250                fds = (fd_set *)malloc(bytes);
251                if (fds == NULL)
252                        return (cu->cu_error.re_status = RPC_CANTSEND);
253                memset(fds, 0, bytes);
254        } else {
255                fds = &readfds;
256                FD_ZERO(fds);
257        }
258
259        timerclear(&time_waited);
260
261call_again:
262        xdrs = &(cu->cu_outxdrs);
263        xdrs->x_op = XDR_ENCODE;
264        XDR_SETPOS(xdrs, cu->cu_xdrpos);
265        /*
266         * the transaction is the first thing in the out buffer
267         */
268        (*(u_short *)(cu->cu_outbuf))++;
269        if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
270            (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
271            (! (*xargs)(xdrs, argsp))) {
272                if (fds != &readfds)
273                        free(fds);
274                return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
275        }
276        outlen = (size_t)XDR_GETPOS(xdrs);
277
278send_again:
279        if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
280            (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen) != outlen) {
281                cu->cu_error.re_errno = errno;
282                if (fds != &readfds)
283                        free(fds);
284                return (cu->cu_error.re_status = RPC_CANTSEND);
285        }
286
287        /*
288         * Hack to provide rpc-based message passing
289         */
290        if (!timerisset(&timeout)) {
291                if (fds != &readfds)
292                        free(fds);
293                return (cu->cu_error.re_status = RPC_TIMEDOUT);
294        }
295        /*
296         * sub-optimal code appears here because we have
297         * some clock time to spare while the packets are in flight.
298         * (We assume that this is actually only executed once.)
299         */
300        reply_msg.acpted_rply.ar_verf = _null_auth;
301        reply_msg.acpted_rply.ar_results.where = resultsp;
302        reply_msg.acpted_rply.ar_results.proc = xresults;
303
304        gettimeofday(&start, NULL);
305        for (;;) {
306                /* XXX we know the other bits are still clear */
307                FD_SET(cu->cu_sock, fds);
308                tv = cu->cu_wait;
309                switch (select(cu->cu_sock+1, fds, NULL, NULL, &tv)) {
310
311                case 0:
312                        timeradd(&time_waited, &cu->cu_wait, &tmp1);
313                        time_waited = tmp1;
314                        if (timercmp(&time_waited, &timeout, <))
315                                goto send_again;
316                        if (fds != &readfds)
317                                free(fds);
318                        return (cu->cu_error.re_status = RPC_TIMEDOUT);
319
320                case -1:
321                        if (errno == EINTR) {
322                                gettimeofday(&after, NULL);
323                                timersub(&after, &start, &tmp1);
324                                timeradd(&time_waited, &tmp1, &tmp2);
325                                time_waited = tmp2;
326                                if (timercmp(&time_waited, &timeout, <))
327                                        continue;
328                                if (fds != &readfds)
329                                        free(fds);
330                                return (cu->cu_error.re_status = RPC_TIMEDOUT);
331                        }
332                        cu->cu_error.re_errno = errno;
333                        if (fds != &readfds)
334                                free(fds);
335                        return (cu->cu_error.re_status = RPC_CANTRECV);
336                }
337
338                do {
339                        fromlen = sizeof(struct sockaddr);
340                        inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
341                                (int) cu->cu_recvsz, 0,
342                                (struct sockaddr *)&from, &fromlen);
343                } while (inlen < 0 && errno == EINTR);
344                if (inlen < 0) {
345                        if (errno == EWOULDBLOCK)
346                                continue;
347                        cu->cu_error.re_errno = errno;
348                        if (fds != &readfds)
349                                free(fds);
350                        return (cu->cu_error.re_status = RPC_CANTRECV);
351                }
352                if (inlen < sizeof(u_int32_t))
353                        continue;
354                /* see if reply transaction id matches sent id */
355                if (*((u_int32_t *)(cu->cu_inbuf)) != *((u_int32_t *)(cu->cu_outbuf)))
356                        continue;
357                /* we now assume we have the proper reply */
358                break;
359        }
360
361        /*
362         * now decode and validate the response
363         */
364        xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
365        ok = xdr_replymsg(&reply_xdrs, &reply_msg);
366        /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
367        if (ok) {
368                _seterr_reply(&reply_msg, &(cu->cu_error));
369                if (cu->cu_error.re_status == RPC_SUCCESS) {
370                        if (! AUTH_VALIDATE(cl->cl_auth,
371                                &reply_msg.acpted_rply.ar_verf)) {
372                                cu->cu_error.re_status = RPC_AUTHERROR;
373                                cu->cu_error.re_why = AUTH_INVALIDRESP;
374                        }
375                        if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
376                                xdrs->x_op = XDR_FREE;
377                                (void)xdr_opaque_auth(xdrs,
378                                    &(reply_msg.acpted_rply.ar_verf));
379                        }
380                }  /* end successful completion */
381                else {
382                        /* maybe our credentials need to be refreshed ... */
383                        if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
384                                nrefreshes--;
385                                goto call_again;
386                        }
387                }  /* end of unsuccessful completion */
388        }  /* end of valid reply message */
389        else {
390                /*
391                 * It's possible for xdr_replymsg() to fail partway
392                 * through its attempt to decode the result from the
393                 * server. If this happens, it will leave the reply
394                 * structure partially populated with dynamically
395                 * allocated memory. (This can happen if someone uses
396                 * clntudp_bufcreate() to create a CLIENT handle and
397                 * specifies a receive buffer size that is too small.)
398                 * This memory must be free()ed to avoid a leak.
399                 */
400                int op = reply_xdrs.x_op;
401                reply_xdrs.x_op = XDR_FREE;
402                xdr_replymsg(&reply_xdrs, &reply_msg);
403                reply_xdrs.x_op = op;
404                cu->cu_error.re_status = RPC_CANTDECODERES;
405        }
406        if (fds != &readfds)
407                free(fds);
408        return (cu->cu_error.re_status);
409}
410
411static void
412clntudp_geterr(
413        CLIENT *cl,
414        struct rpc_err *errp)
415{
416        struct cu_data *cu = (struct cu_data *)cl->cl_private;
417
418        *errp = cu->cu_error;
419}
420
421
422static bool_t
423clntudp_freeres(
424        CLIENT *cl,
425        xdrproc_t xdr_res,
426        void *res_ptr)
427{
428        struct cu_data *cu = (struct cu_data *)cl->cl_private;
429        XDR *xdrs = &(cu->cu_outxdrs);
430
431        xdrs->x_op = XDR_FREE;
432        return ((*xdr_res)(xdrs, res_ptr));
433}
434
435static void
436clntudp_abort(void)
437{
438}
439
440
441static bool_t
442clntudp_control(
443        CLIENT *cl,
444        int request,
445        char *info)
446{
447        struct cu_data *cu = (struct cu_data *)cl->cl_private;
448        struct timeval *tv;
449        socklen_t len;
450
451        switch (request) {
452        case CLSET_FD_CLOSE:
453                cu->cu_closeit = TRUE;
454                break;
455        case CLSET_FD_NCLOSE:
456                cu->cu_closeit = FALSE;
457                break;
458        case CLSET_TIMEOUT:
459                if (info == NULL)
460                        return(FALSE);
461                tv = (struct timeval *)info;
462                cu->cu_total.tv_sec = tv->tv_sec;
463                cu->cu_total.tv_usec = tv->tv_usec;
464                break;
465        case CLGET_TIMEOUT:
466                if (info == NULL)
467                        return(FALSE);
468                *(struct timeval *)info = cu->cu_total;
469                break;
470        case CLSET_RETRY_TIMEOUT:
471                if (info == NULL)
472                        return(FALSE);
473                tv = (struct timeval *)info;
474                cu->cu_wait.tv_sec = tv->tv_sec;
475                cu->cu_wait.tv_usec = tv->tv_usec;
476                break;
477        case CLGET_RETRY_TIMEOUT:
478                if (info == NULL)
479                        return(FALSE);
480                *(struct timeval *)info = cu->cu_wait;
481                break;
482        case CLGET_SERVER_ADDR:
483                if (info == NULL)
484                        return(FALSE);
485                *(struct sockaddr_in *)info = cu->cu_raddr;
486                break;
487        case CLGET_FD:
488                if (info == NULL)
489                        return(FALSE);
490                *(int *)info = cu->cu_sock;
491                break;
492        case CLGET_XID:
493                /*
494                 * use the knowledge that xid is the
495                 * first element in the call structure *.
496                 * This will get the xid of the PREVIOUS call
497                 */
498                if (info == NULL)
499                        return(FALSE);
500                *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
501                break;
502        case CLSET_XID:
503                /* This will set the xid of the NEXT call */
504                if (info == NULL)
505                        return(FALSE);
506                *(u_long *)cu->cu_outbuf =  htonl(*(u_long *)info - 1);
507                /* decrement by 1 as clntudp_call() increments once */
508        case CLGET_VERS:
509                /*
510                 * This RELIES on the information that, in the call body,
511                 * the version number field is the fifth field from the
512                 * begining of the RPC header. MUST be changed if the
513                 * call_struct is changed
514                 */
515                if (info == NULL)
516                        return(FALSE);
517                *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
518                                                4 * BYTES_PER_XDR_UNIT));
519                break;
520        case CLSET_VERS:
521                if (info == NULL)
522                        return(FALSE);
523                *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
524                                = htonl(*(u_long *)info);
525                break;
526        case CLGET_PROG:
527                /*
528                 * This RELIES on the information that, in the call body,
529                 * the program number field is the fourth field from the
530                 * begining of the RPC header. MUST be changed if the
531                 * call_struct is changed
532                 */
533                if (info == NULL)
534                        return(FALSE);
535                *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
536                                                3 * BYTES_PER_XDR_UNIT));
537                break;
538        case CLSET_PROG:
539                if (info == NULL)
540                        return(FALSE);
541                *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
542                                = htonl(*(u_long *)info);
543                break;
544        case CLGET_LOCAL_ADDR:
545                len = sizeof(struct sockaddr);
546                if (getsockname(cu->cu_sock, (struct sockaddr *)info, &len) <0)
547                        return(FALSE);
548                break;
549        case CLGET_SVC_ADDR:
550        case CLSET_SVC_ADDR:
551        case CLSET_PUSH_TIMOD:
552        case CLSET_POP_TIMOD:
553        default:
554                return (FALSE);
555        }
556        return (TRUE);
557}
558
559static void
560clntudp_destroy(
561        CLIENT *cl)
562{
563        struct cu_data *cu = (struct cu_data *)cl->cl_private;
564
565        if (cu->cu_closeit) {
566                (void)_RPC_close(cu->cu_sock);
567        }
568        XDR_DESTROY(&(cu->cu_outxdrs));
569        mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
570        mem_free(cl, sizeof (CLIENT));
571}
Note: See TracBrowser for help on using the repository browser.