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

4.104.114.84.95
Last change on this file since 5220ae2 was 6f07dbc, checked in by Joel Sherrill <joel.sherrill@…>, on 10/28/02 at 13:50:14

2002-10-28 Joel Sherrill <joel@…>

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