source: rtems-libbsd/services/librpc/src/rpc/svc_udp.c @ e599318

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

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