source: rtems-libbsd/services/librpc/src/rpc/clnt_udp.c @ cd450d2

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since cd450d2 was cd450d2, checked in by Joel Sherrill <joel.sherrill@…>, on 08/06/12 at 00:10:35

librpc: now compiles

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