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