source: rtems-libbsd/services/librpc/src/rpc/svc_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: 12.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: @(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";*/
32/*static char *sccsid = "from: @(#)svc_udp.c    2.2 88/07/29 4.0 RPCSRC";*/
33static char *rcsid = "$FreeBSD: src/lib/libc/rpc/svc_udp.c,v 1.13 2000/01/27 23:06:41 jasone Exp $";
34#endif
35
36/*
37 * svc_udp.c,
38 * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
39 * achieving execute-at-most-once semantics.)
40 *
41 * Copyright (C) 1984, Sun Microsystems, Inc.
42 */
43
44#ifdef HAVE_CONFIG_H
45#include "config.h"
46#endif
47
48#include <freebsd/bsd.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <string.h>
53#include <rpc/rpc.h>
54#include <sys/socket.h>
55#include <errno.h>
56
57#define rpc_buffer(xprt) ((xprt)->xp_p1)
58#define MAX(a, b)     ((a > b) ? a : b)
59
60static bool_t           svcudp_recv(SVCXPRT *xprt, struct rpc_msg *msg);
61static bool_t           svcudp_reply(SVCXPRT *xprt, struct rpc_msg *msg);
62static enum xprt_stat   svcudp_stat(SVCXPRT *xprt);
63static bool_t           svcudp_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr);
64static bool_t           svcudp_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr);
65static void             svcudp_destroy(SVCXPRT *xprt);
66static void             cache_set (SVCXPRT *, u_long);
67static int              cache_get (SVCXPRT *, struct rpc_msg *, char **, u_long *);
68
69static struct xp_ops svcudp_op = {
70        svcudp_recv,
71        svcudp_stat,
72        svcudp_getargs,
73        svcudp_reply,
74        svcudp_freeargs,
75        svcudp_destroy
76};
77
78/*
79 * kept in xprt->xp_p2
80 */
81struct svcudp_data {
82        u_int   su_iosz;        /* byte size of send.recv buffer */
83        u_long  su_xid;         /* transaction id */
84        XDR     su_xdrs;        /* XDR handle */
85        char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
86        char *  su_cache;       /* cached data, NULL if no cache */
87};
88#define su_data(xprt)   ((struct svcudp_data *)(xprt->xp_p2))
89
90/*
91 * Usage:
92 *      xprt = svcudp_create(sock);
93 *
94 * If sock<0 then a socket is created, else sock is used.
95 * If the socket, sock is not bound to a port then svcudp_create
96 * binds it to an arbitrary port.  In any (successful) case,
97 * xprt->xp_fd is the registered socket number and xprt->xp_port is the
98 * associated port number.
99 * Once *xprt is initialized, it is registered as a transporter;
100 * see (svc.h, xprt_register).
101 * The routines returns NULL if a problem occurred.
102 */
103SVCXPRT *
104svcudp_bufcreate(
105        int sock,
106        u_int sendsz,
107        u_int recvsz )
108{
109        bool_t madesock = FALSE;
110        register SVCXPRT *xprt;
111        register struct svcudp_data *su;
112        struct sockaddr_in addr;
113        socklen_t len = sizeof(struct sockaddr_in);
114
115        if (sock == RPC_ANYSOCK) {
116                if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
117                        perror("svcudp_create: socket creation problem");
118                        return ((SVCXPRT *)NULL);
119                }
120                madesock = TRUE;
121        }
122        memset((char *)&addr, 0, sizeof (addr));
123        addr.sin_len = sizeof(struct sockaddr_in);
124        addr.sin_family = AF_INET;
125        if (bindresvport(sock, &addr)) {
126                addr.sin_port = 0;
127                (void)bind(sock, (struct sockaddr *)&addr, len);
128        }
129        if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
130                perror("svcudp_create - cannot getsockname");
131                if (madesock)
132                        (void)_RPC_close(sock);
133                return ((SVCXPRT *)NULL);
134        }
135        xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
136        if (xprt == NULL) {
137                (void)fprintf(stderr, "svcudp_create: out of memory\n");
138                return (NULL);
139        }
140        su = (struct svcudp_data *)mem_alloc(sizeof(*su));
141        if (su == NULL) {
142                (void)fprintf(stderr, "svcudp_create: out of memory\n");
143                return (NULL);
144        }
145        su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
146        if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
147                (void)fprintf(stderr, "svcudp_create: out of memory\n");
148                return (NULL);
149        }
150        xdrmem_create(
151            &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
152        su->su_cache = NULL;
153        xprt->xp_p2 = (caddr_t)su;
154        xprt->xp_verf.oa_base = su->su_verfbody;
155        xprt->xp_ops = &svcudp_op;
156        xprt->xp_port = ntohs(addr.sin_port);
157        xprt->xp_fd = sock;
158        xprt_register(xprt);
159        return (xprt);
160}
161
162SVCXPRT *
163svcudp_create(
164        int sock)
165{
166
167        return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
168}
169
170static enum xprt_stat
171svcudp_stat(
172        SVCXPRT *xprt)
173{
174
175        return (XPRT_IDLE);
176}
177
178static bool_t
179svcudp_recv(
180        SVCXPRT *xprt,
181        struct rpc_msg *msg)
182{
183        register struct svcudp_data *su = su_data(xprt);
184        register XDR *xdrs = &(su->su_xdrs);
185        register int rlen;
186        char *reply;
187        u_long replylen;
188
189    again:
190        xprt->xp_addrlen = sizeof(struct sockaddr_in);
191        rlen = recvfrom(xprt->xp_fd, rpc_buffer(xprt), (int) su->su_iosz,
192            0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
193        if (rlen == -1 && errno == EINTR)
194                goto again;
195        if (rlen == -1 || rlen < 4*sizeof(u_int32_t))
196                return (FALSE);
197        xdrs->x_op = XDR_DECODE;
198        XDR_SETPOS(xdrs, 0);
199        if (! xdr_callmsg(xdrs, msg))
200                return (FALSE);
201        su->su_xid = msg->rm_xid;
202        if (su->su_cache != NULL) {
203                if (cache_get(xprt, msg, &reply, &replylen)) {
204                        (void) sendto(xprt->xp_fd, reply, (int) replylen, 0,
205                          (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
206                        return (TRUE);
207                }
208        }
209        return (TRUE);
210}
211
212static bool_t
213svcudp_reply(
214        SVCXPRT *xprt,
215        struct rpc_msg *msg)
216{
217        register struct svcudp_data *su = su_data(xprt);
218        register XDR *xdrs = &(su->su_xdrs);
219        register int slen;
220        register bool_t stat = FALSE;
221
222        xdrs->x_op = XDR_ENCODE;
223        XDR_SETPOS(xdrs, 0);
224        msg->rm_xid = su->su_xid;
225        if (xdr_replymsg(xdrs, msg)) {
226                slen = (int)XDR_GETPOS(xdrs);
227                if (sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 0,
228                    (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
229                    == slen) {
230                        stat = TRUE;
231                        if (su->su_cache && slen >= 0) {
232                                cache_set(xprt, (u_long) slen);
233                        }
234                }
235        }
236        return (stat);
237}
238
239static bool_t
240svcudp_getargs(
241        SVCXPRT *xprt,
242        xdrproc_t xdr_args,
243        caddr_t args_ptr)
244{
245
246        return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
247}
248
249static bool_t
250svcudp_freeargs(
251        SVCXPRT *xprt,
252        xdrproc_t xdr_args,
253        caddr_t args_ptr)
254{
255        register XDR *xdrs = &(su_data(xprt)->su_xdrs);
256
257        xdrs->x_op = XDR_FREE;
258        return ((*xdr_args)(xdrs, args_ptr));
259}
260
261static void
262svcudp_destroy(
263        SVCXPRT *xprt)
264{
265        register struct svcudp_data *su = su_data(xprt);
266
267        xprt_unregister(xprt);
268        (void)_RPC_close(xprt->xp_fd);
269        XDR_DESTROY(&(su->su_xdrs));
270        mem_free(rpc_buffer(xprt), su->su_iosz);
271        mem_free((caddr_t)su, sizeof(struct svcudp_data));
272        mem_free((caddr_t)xprt, sizeof(SVCXPRT));
273}
274
275
276/***********this could be a separate file*********************/
277
278/*
279 * Fifo cache for udp server
280 * Copies pointers to reply buffers into fifo cache
281 * Buffers are sent again if retransmissions are detected.
282 */
283
284#define SPARSENESS 4    /* 75% sparse */
285
286#define CACHE_PERROR(msg)       \
287        (void) fprintf(stderr,"%s\n", msg)
288
289#define ALLOC(type, size)       \
290        (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
291
292#define BZERO(addr, type, size)  \
293        memset((char *) addr, 0, sizeof(type) * (int) (size))
294
295/*
296 * An entry in the cache
297 */
298typedef struct cache_node *cache_ptr;
299struct cache_node {
300        /*
301         * Index into cache is xid, proc, vers, prog and address
302         */
303        u_long cache_xid;
304        u_long cache_proc;
305        u_long cache_vers;
306        u_long cache_prog;
307        struct sockaddr_in cache_addr;
308        /*
309         * The cached reply and length
310         */
311        char * cache_reply;
312        u_long cache_replylen;
313        /*
314         * Next node on the list, if there is a collision
315         */
316        cache_ptr cache_next;
317};
318
319
320
321/*
322 * The entire cache
323 */
324struct udp_cache {
325        u_long uc_size;         /* size of cache */
326        cache_ptr *uc_entries;  /* hash table of entries in cache */
327        cache_ptr *uc_fifo;     /* fifo list of entries in cache */
328        u_long uc_nextvictim;   /* points to next victim in fifo list */
329        u_long uc_prog;         /* saved program number */
330        u_long uc_vers;         /* saved version number */
331        u_long uc_proc;         /* saved procedure number */
332        struct sockaddr_in uc_addr; /* saved caller's address */
333};
334
335
336/*
337 * the hashing function
338 */
339#define CACHE_LOC(transp, xid)  \
340 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
341
342
343/*
344 * Enable use of the cache.
345 * Note: there is no disable.
346 */
347int svcudp_enablecache(
348        SVCXPRT *transp,
349        u_long size)
350{
351        struct svcudp_data *su = su_data(transp);
352        struct udp_cache *uc;
353
354        if (su->su_cache != NULL) {
355                CACHE_PERROR("enablecache: cache already enabled");
356                return(0);
357        }
358        uc = ALLOC(struct udp_cache, 1);
359        if (uc == NULL) {
360                CACHE_PERROR("enablecache: could not allocate cache");
361                return(0);
362        }
363        uc->uc_size = size;
364        uc->uc_nextvictim = 0;
365        uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
366        if (uc->uc_entries == NULL) {
367                CACHE_PERROR("enablecache: could not allocate cache data");
368                return(0);
369        }
370        BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
371        uc->uc_fifo = ALLOC(cache_ptr, size);
372        if (uc->uc_fifo == NULL) {
373                CACHE_PERROR("enablecache: could not allocate cache fifo");
374                return(0);
375        }
376        BZERO(uc->uc_fifo, cache_ptr, size);
377        su->su_cache = (char *) uc;
378        return(1);
379}
380
381
382/*
383 * Set an entry in the cache
384 */
385static void
386cache_set(
387        SVCXPRT *xprt,
388        u_long replylen)
389{
390        register cache_ptr victim;
391        register cache_ptr *vicp;
392        register struct svcudp_data *su = su_data(xprt);
393        struct udp_cache *uc = (struct udp_cache *) su->su_cache;
394        u_int loc;
395        char *newbuf;
396
397        /*
398         * Find space for the new entry, either by
399         * reusing an old entry, or by mallocing a new one
400         */
401        victim = uc->uc_fifo[uc->uc_nextvictim];
402        if (victim != NULL) {
403                loc = CACHE_LOC(xprt, victim->cache_xid);
404                for (vicp = &uc->uc_entries[loc];
405                  *vicp != NULL && *vicp != victim;
406                  vicp = &(*vicp)->cache_next)
407                                ;
408                if (*vicp == NULL) {
409                        CACHE_PERROR("cache_set: victim not found");
410                        return;
411                }
412                *vicp = victim->cache_next;     /* remote from cache */
413                newbuf = victim->cache_reply;
414        } else {
415                victim = ALLOC(struct cache_node, 1);
416                if (victim == NULL) {
417                        CACHE_PERROR("cache_set: victim alloc failed");
418                        return;
419                }
420                newbuf = mem_alloc(su->su_iosz);
421                if (newbuf == NULL) {
422                        CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
423                        return;
424                }
425        }
426
427        /*
428         * Store it away
429         */
430        victim->cache_replylen = replylen;
431        victim->cache_reply = rpc_buffer(xprt);
432        rpc_buffer(xprt) = newbuf;
433        xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
434        victim->cache_xid = su->su_xid;
435        victim->cache_proc = uc->uc_proc;
436        victim->cache_vers = uc->uc_vers;
437        victim->cache_prog = uc->uc_prog;
438        victim->cache_addr = uc->uc_addr;
439        loc = CACHE_LOC(xprt, victim->cache_xid);
440        victim->cache_next = uc->uc_entries[loc];
441        uc->uc_entries[loc] = victim;
442        uc->uc_fifo[uc->uc_nextvictim++] = victim;
443        uc->uc_nextvictim %= uc->uc_size;
444}
445
446/*
447 * Try to get an entry from the cache
448 * return 1 if found, 0 if not found
449 */
450static int
451cache_get(
452        SVCXPRT *xprt,
453        struct rpc_msg *msg,
454        char **replyp,
455        u_long *replylenp)
456{
457        u_int loc;
458        register cache_ptr ent;
459        register struct svcudp_data *su = su_data(xprt);
460        register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
461
462#       define EQADDR(a1, a2)   (memcmp(&a1, &a2, sizeof(a1)) == 0)
463
464        loc = CACHE_LOC(xprt, su->su_xid);
465        for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
466                if (ent->cache_xid == su->su_xid &&
467                  ent->cache_proc == uc->uc_proc &&
468                  ent->cache_vers == uc->uc_vers &&
469                  ent->cache_prog == uc->uc_prog &&
470                  EQADDR(ent->cache_addr, uc->uc_addr)) {
471                        *replyp = ent->cache_reply;
472                        *replylenp = ent->cache_replylen;
473                        return(1);
474                }
475        }
476        /*
477         * Failed to find entry
478         * Remember a few things so we can do a set later
479         */
480        uc->uc_proc = msg->rm_call.cb_proc;
481        uc->uc_vers = msg->rm_call.cb_vers;
482        uc->uc_prog = msg->rm_call.cb_prog;
483        uc->uc_addr = xprt->xp_raddr;
484        return(0);
485}
Note: See TracBrowser for help on using the repository browser.