Update.
[platform/upstream/glibc.git] / sunrpc / svc_udp.c
1 /* @(#)svc_udp.c        2.2 88/07/29 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * svc_udp.c,
36  * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
37  * achieving execute-at-most-once semantics.)
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  */
41
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <sys/socket.h>
47 #include <errno.h>
48
49
50 #define rpc_buffer(xprt) ((xprt)->xp_p1)
51 #ifndef MAX
52 #define MAX(a, b)     ((a > b) ? a : b)
53 #endif
54
55 static bool_t svcudp_recv (SVCXPRT *, struct rpc_msg *);
56 static bool_t svcudp_reply (SVCXPRT *, struct rpc_msg *);
57 static enum xprt_stat svcudp_stat (SVCXPRT *);
58 static bool_t svcudp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
59 static bool_t svcudp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
60 static void svcudp_destroy (SVCXPRT *);
61
62 static const struct xp_ops svcudp_op =
63 {
64   svcudp_recv,
65   svcudp_stat,
66   svcudp_getargs,
67   svcudp_reply,
68   svcudp_freeargs,
69   svcudp_destroy
70 };
71
72 static int cache_get (SVCXPRT *, struct rpc_msg *, char **replyp,
73                       u_long *replylenp);
74 static void cache_set (SVCXPRT *xprt, u_long replylen);
75
76 /*
77  * kept in xprt->xp_p2
78  */
79 struct svcudp_data
80   {
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,
96  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
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  */
102 SVCXPRT *
103 svcudp_bufcreate (sock, sendsz, recvsz)
104      int sock;
105      u_int sendsz, recvsz;
106 {
107   bool_t madesock = FALSE;
108   SVCXPRT *xprt;
109   struct svcudp_data *su;
110   struct sockaddr_in addr;
111   size_t len = sizeof (struct sockaddr_in);
112
113   if (sock == RPC_ANYSOCK)
114     {
115       if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
116         {
117           perror (_("svcudp_create: socket creation problem"));
118           return (SVCXPRT *) NULL;
119         }
120       madesock = TRUE;
121     }
122   bzero ((char *) &addr, sizeof (addr));
123   addr.sin_family = AF_INET;
124   if (bindresvport (sock, &addr))
125     {
126       addr.sin_port = 0;
127       (void) bind (sock, (struct sockaddr *) &addr, len);
128     }
129   if (getsockname (sock, (struct sockaddr *) &addr, &len) != 0)
130     {
131       perror (_("svcudp_create - cannot getsockname"));
132       if (madesock)
133         (void) close (sock);
134       return (SVCXPRT *) NULL;
135     }
136   xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
137   if (xprt == NULL)
138     {
139       (void) fputs (_("svcudp_create: out of memory\n"), stderr);
140       return NULL;
141     }
142   su = (struct svcudp_data *) mem_alloc (sizeof (*su));
143   if (su == NULL)
144     {
145       (void) fputs (_("svcudp_create: out of memory\n"), stderr);
146       return NULL;
147     }
148   su->su_iosz = ((MAX (sendsz, recvsz) + 3) / 4) * 4;
149   if ((rpc_buffer (xprt) = mem_alloc (su->su_iosz)) == NULL)
150     {
151       (void) fputs (_("svcudp_create: out of memory\n"), stderr);
152       return NULL;
153     }
154   xdrmem_create (&(su->su_xdrs), rpc_buffer (xprt), su->su_iosz, XDR_DECODE);
155   su->su_cache = NULL;
156   xprt->xp_p2 = (caddr_t) su;
157   xprt->xp_verf.oa_base = su->su_verfbody;
158   xprt->xp_ops = &svcudp_op;
159   xprt->xp_port = ntohs (addr.sin_port);
160   xprt->xp_sock = sock;
161   xprt_register (xprt);
162   return xprt;
163 }
164
165 SVCXPRT *
166 svcudp_create (sock)
167      int sock;
168 {
169
170   return svcudp_bufcreate (sock, UDPMSGSIZE, UDPMSGSIZE);
171 }
172
173 static enum xprt_stat
174 svcudp_stat (xprt)
175      SVCXPRT *xprt;
176 {
177
178   return XPRT_IDLE;
179 }
180
181 static bool_t
182 svcudp_recv (xprt, msg)
183      SVCXPRT *xprt;
184      struct rpc_msg *msg;
185 {
186   struct svcudp_data *su = su_data (xprt);
187   XDR *xdrs = &(su->su_xdrs);
188   int rlen;
189   char *reply;
190   u_long replylen;
191   size_t len;
192
193 again:
194   /* FIXME -- should xp_addrlen be a size_t?  */
195   len = sizeof(struct sockaddr_in);
196   rlen = recvfrom (xprt->xp_sock, rpc_buffer (xprt), (int) su->su_iosz, 0,
197                    (struct sockaddr *) &(xprt->xp_raddr), &len);
198   xprt->xp_addrlen = len;
199   if (rlen == -1 && errno == EINTR)
200     goto again;
201   if (rlen < 16)                /* < 4 32-bit ints? */
202     return FALSE;
203   xdrs->x_op = XDR_DECODE;
204   XDR_SETPOS (xdrs, 0);
205   if (!xdr_callmsg (xdrs, msg))
206     return FALSE;
207   su->su_xid = msg->rm_xid;
208   if (su->su_cache != NULL)
209     {
210       if (cache_get (xprt, msg, &reply, &replylen))
211         {
212           (void) sendto (xprt->xp_sock, reply, (int) replylen, 0,
213                          (struct sockaddr *) &xprt->xp_raddr, len);
214           return TRUE;
215         }
216     }
217   return TRUE;
218 }
219
220 static bool_t
221 svcudp_reply (xprt, msg)
222      SVCXPRT *xprt;
223      struct rpc_msg *msg;
224 {
225   struct svcudp_data *su = su_data (xprt);
226   XDR *xdrs = &(su->su_xdrs);
227   int slen;
228   bool_t stat = FALSE;
229
230   xdrs->x_op = XDR_ENCODE;
231   XDR_SETPOS (xdrs, 0);
232   msg->rm_xid = su->su_xid;
233   if (xdr_replymsg (xdrs, msg))
234     {
235       slen = (int) XDR_GETPOS (xdrs);
236       if (sendto (xprt->xp_sock, rpc_buffer (xprt), slen, 0,
237                   (struct sockaddr *) &(xprt->xp_raddr), xprt->xp_addrlen)
238           == slen)
239         {
240           stat = TRUE;
241           if (su->su_cache && slen >= 0)
242             {
243               cache_set (xprt, (u_long) slen);
244             }
245         }
246     }
247   return stat;
248 }
249
250 static bool_t
251 svcudp_getargs (xprt, xdr_args, args_ptr)
252      SVCXPRT *xprt;
253      xdrproc_t xdr_args;
254      caddr_t args_ptr;
255 {
256
257   return (*xdr_args) (&(su_data (xprt)->su_xdrs), args_ptr);
258 }
259
260 static bool_t
261 svcudp_freeargs (xprt, xdr_args, args_ptr)
262      SVCXPRT *xprt;
263      xdrproc_t xdr_args;
264      caddr_t args_ptr;
265 {
266   XDR *xdrs = &(su_data (xprt)->su_xdrs);
267
268   xdrs->x_op = XDR_FREE;
269   return (*xdr_args) (xdrs, args_ptr);
270 }
271
272 static void
273 svcudp_destroy (xprt)
274      SVCXPRT *xprt;
275 {
276   struct svcudp_data *su = su_data (xprt);
277
278   xprt_unregister (xprt);
279   (void) close (xprt->xp_sock);
280   XDR_DESTROY (&(su->su_xdrs));
281   mem_free (rpc_buffer (xprt), su->su_iosz);
282   mem_free ((caddr_t) su, sizeof (struct svcudp_data));
283   mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
284 }
285
286
287 /***********this could be a separate file*********************/
288
289 /*
290  * Fifo cache for udp server
291  * Copies pointers to reply buffers into fifo cache
292  * Buffers are sent again if retransmissions are detected.
293  */
294
295 #define SPARSENESS 4            /* 75% sparse */
296
297 #define CACHE_PERROR(msg)       \
298         (void) fprintf(stderr,"%s\n", msg)
299
300 #define ALLOC(type, size)       \
301         (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
302
303 #define BZERO(addr, type, size)  \
304         bzero((char *) addr, sizeof(type) * (int) (size))
305
306 /*
307  * An entry in the cache
308  */
309 typedef struct cache_node *cache_ptr;
310 struct cache_node
311   {
312     /*
313      * Index into cache is xid, proc, vers, prog and address
314      */
315     u_long cache_xid;
316     u_long cache_proc;
317     u_long cache_vers;
318     u_long cache_prog;
319     struct sockaddr_in cache_addr;
320     /*
321      * The cached reply and length
322      */
323     char *cache_reply;
324     u_long cache_replylen;
325     /*
326      * Next node on the list, if there is a collision
327      */
328     cache_ptr cache_next;
329   };
330
331
332
333 /*
334  * The entire cache
335  */
336 struct udp_cache
337   {
338     u_long uc_size;             /* size of cache */
339     cache_ptr *uc_entries;      /* hash table of entries in cache */
340     cache_ptr *uc_fifo;         /* fifo list of entries in cache */
341     u_long uc_nextvictim;       /* points to next victim in fifo list */
342     u_long uc_prog;             /* saved program number */
343     u_long uc_vers;             /* saved version number */
344     u_long uc_proc;             /* saved procedure number */
345     struct sockaddr_in uc_addr; /* saved caller's address */
346   };
347
348
349 /*
350  * the hashing function
351  */
352 #define CACHE_LOC(transp, xid)  \
353  (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
354
355
356 /*
357  * Enable use of the cache.
358  * Note: there is no disable.
359  */
360 int
361 svcudp_enablecache (SVCXPRT *transp, u_long size)
362 {
363   struct svcudp_data *su = su_data (transp);
364   struct udp_cache *uc;
365
366   if (su->su_cache != NULL)
367     {
368       CACHE_PERROR (_("enablecache: cache already enabled"));
369       return 0;
370     }
371   uc = ALLOC (struct udp_cache, 1);
372   if (uc == NULL)
373     {
374       CACHE_PERROR (_("enablecache: could not allocate cache"));
375       return 0;
376     }
377   uc->uc_size = size;
378   uc->uc_nextvictim = 0;
379   uc->uc_entries = ALLOC (cache_ptr, size * SPARSENESS);
380   if (uc->uc_entries == NULL)
381     {
382       CACHE_PERROR (_("enablecache: could not allocate cache data"));
383       return 0;
384     }
385   BZERO (uc->uc_entries, cache_ptr, size * SPARSENESS);
386   uc->uc_fifo = ALLOC (cache_ptr, size);
387   if (uc->uc_fifo == NULL)
388     {
389       CACHE_PERROR (_("enablecache: could not allocate cache fifo"));
390       return 0;
391     }
392   BZERO (uc->uc_fifo, cache_ptr, size);
393   su->su_cache = (char *) uc;
394   return 1;
395 }
396
397
398 /*
399  * Set an entry in the cache
400  */
401 static void
402 cache_set (SVCXPRT *xprt, u_long replylen)
403 {
404   cache_ptr victim;
405   cache_ptr *vicp;
406   struct svcudp_data *su = su_data (xprt);
407   struct udp_cache *uc = (struct udp_cache *) su->su_cache;
408   u_int loc;
409   char *newbuf;
410
411   /*
412    * Find space for the new entry, either by
413    * reusing an old entry, or by mallocing a new one
414    */
415   victim = uc->uc_fifo[uc->uc_nextvictim];
416   if (victim != NULL)
417     {
418       loc = CACHE_LOC (xprt, victim->cache_xid);
419       for (vicp = &uc->uc_entries[loc];
420            *vicp != NULL && *vicp != victim;
421            vicp = &(*vicp)->cache_next)
422         ;
423       if (*vicp == NULL)
424         {
425           CACHE_PERROR (_("cache_set: victim not found"));
426           return;
427         }
428       *vicp = victim->cache_next;       /* remote from cache */
429       newbuf = victim->cache_reply;
430     }
431   else
432     {
433       victim = ALLOC (struct cache_node, 1);
434       if (victim == NULL)
435         {
436           CACHE_PERROR (_("cache_set: victim alloc failed"));
437           return;
438         }
439       newbuf = mem_alloc (su->su_iosz);
440       if (newbuf == NULL)
441         {
442           CACHE_PERROR (_("cache_set: could not allocate new rpc_buffer"));
443           return;
444         }
445     }
446
447   /*
448    * Store it away
449    */
450   victim->cache_replylen = replylen;
451   victim->cache_reply = rpc_buffer (xprt);
452   rpc_buffer (xprt) = newbuf;
453   xdrmem_create (&(su->su_xdrs), rpc_buffer (xprt), su->su_iosz, XDR_ENCODE);
454   victim->cache_xid = su->su_xid;
455   victim->cache_proc = uc->uc_proc;
456   victim->cache_vers = uc->uc_vers;
457   victim->cache_prog = uc->uc_prog;
458   victim->cache_addr = uc->uc_addr;
459   loc = CACHE_LOC (xprt, victim->cache_xid);
460   victim->cache_next = uc->uc_entries[loc];
461   uc->uc_entries[loc] = victim;
462   uc->uc_fifo[uc->uc_nextvictim++] = victim;
463   uc->uc_nextvictim %= uc->uc_size;
464 }
465
466 /*
467  * Try to get an entry from the cache
468  * return 1 if found, 0 if not found
469  */
470 static int
471 cache_get (xprt, msg, replyp, replylenp)
472      SVCXPRT *xprt;
473      struct rpc_msg *msg;
474      char **replyp;
475      u_long *replylenp;
476 {
477   u_int loc;
478   cache_ptr ent;
479   struct svcudp_data *su = su_data (xprt);
480   struct udp_cache *uc = (struct udp_cache *) su->su_cache;
481
482 #define EQADDR(a1, a2)  (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
483
484   loc = CACHE_LOC (xprt, su->su_xid);
485   for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next)
486     {
487       if (ent->cache_xid == su->su_xid &&
488           ent->cache_proc == uc->uc_proc &&
489           ent->cache_vers == uc->uc_vers &&
490           ent->cache_prog == uc->uc_prog &&
491           EQADDR (ent->cache_addr, uc->uc_addr))
492         {
493           *replyp = ent->cache_reply;
494           *replylenp = ent->cache_replylen;
495           return 1;
496         }
497     }
498   /*
499    * Failed to find entry
500    * Remember a few things so we can do a set later
501    */
502   uc->uc_proc = msg->rm_call.cb_proc;
503   uc->uc_vers = msg->rm_call.cb_vers;
504   uc->uc_prog = msg->rm_call.cb_prog;
505   uc->uc_addr = xprt->xp_raddr;
506   return 0;
507 }