source: rtems/cpukit/librpc/src/rpc/svc_udp.c @ 456ebf81

4.104.114.95
Last change on this file since 456ebf81 was 456ebf81, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/01/08 at 05:59:32

Misc. ansifications.

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