]> sourceware.org Git - glibc.git/blame - sunrpc/svc_udp.c
Update.
[glibc.git] / sunrpc / svc_udp.c
CommitLineData
28f540f4
RM
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.
cbd3dceb 9 *
28f540f4
RM
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.
cbd3dceb 13 *
28f540f4
RM
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.
cbd3dceb 17 *
28f540f4
RM
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.
cbd3dceb 21 *
28f540f4
RM
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.
cbd3dceb 25 *
28f540f4
RM
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30#if !defined(lint) && defined(SCCSIDS)
31static 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>
e7fd8a39
UD
43#include <unistd.h>
44#include <string.h>
28f540f4
RM
45#include <rpc/rpc.h>
46#include <sys/socket.h>
47#include <errno.h>
48
49
50#define rpc_buffer(xprt) ((xprt)->xp_p1)
1f64ac13 51#ifndef MAX
e7fd8a39 52#define MAX(a, b) ((a > b) ? a : b)
1f64ac13 53#endif
28f540f4 54
e7fd8a39
UD
55static bool_t svcudp_recv (SVCXPRT *, struct rpc_msg *);
56static bool_t svcudp_reply (SVCXPRT *, struct rpc_msg *);
57static enum xprt_stat svcudp_stat (SVCXPRT *);
58static bool_t svcudp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
59static bool_t svcudp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
60static void svcudp_destroy (SVCXPRT *);
61
62static 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
28f540f4
RM
70};
71
e7fd8a39
UD
72static int cache_get (SVCXPRT *, struct rpc_msg *, char **replyp,
73 u_long *replylenp);
74static void cache_set (SVCXPRT *xprt, u_long replylen);
28f540f4
RM
75
76/*
77 * kept in xprt->xp_p2
78 */
e7fd8a39
UD
79struct 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 };
28f540f4
RM
87#define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
88
89/*
90 * Usage:
e7fd8a39 91 * xprt = svcudp_create(sock);
28f540f4
RM
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 */
102SVCXPRT *
e7fd8a39
UD
103svcudp_bufcreate (sock, sendsz, recvsz)
104 int sock;
105 u_int sendsz, recvsz;
28f540f4 106{
e7fd8a39
UD
107 bool_t madesock = FALSE;
108 SVCXPRT *xprt;
109 struct svcudp_data *su;
110 struct sockaddr_in addr;
111 int 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;
28f540f4 119 }
e7fd8a39
UD
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;
28f540f4
RM
163}
164
165SVCXPRT *
e7fd8a39
UD
166svcudp_create (sock)
167 int sock;
28f540f4
RM
168{
169
e7fd8a39 170 return svcudp_bufcreate (sock, UDPMSGSIZE, UDPMSGSIZE);
28f540f4
RM
171}
172
173static enum xprt_stat
e7fd8a39
UD
174svcudp_stat (xprt)
175 SVCXPRT *xprt;
28f540f4
RM
176{
177
e7fd8a39 178 return XPRT_IDLE;
28f540f4
RM
179}
180
181static bool_t
e7fd8a39
UD
182svcudp_recv (xprt, msg)
183 SVCXPRT *xprt;
184 struct rpc_msg *msg;
28f540f4 185{
e7fd8a39
UD
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
192again:
193 xprt->xp_addrlen = sizeof (struct sockaddr_in);
194 rlen = recvfrom (xprt->xp_sock, rpc_buffer (xprt), (int) su->su_iosz, 0,
195 (struct sockaddr *) &(xprt->xp_raddr), &(xprt->xp_addrlen));
196 if (rlen == -1 && errno == EINTR)
197 goto again;
198 if (rlen < 16) /* < 4 32-bit ints? */
199 return FALSE;
200 xdrs->x_op = XDR_DECODE;
201 XDR_SETPOS (xdrs, 0);
202 if (!xdr_callmsg (xdrs, msg))
203 return FALSE;
204 su->su_xid = msg->rm_xid;
205 if (su->su_cache != NULL)
206 {
207 if (cache_get (xprt, msg, &reply, &replylen))
208 {
209 (void) sendto (xprt->xp_sock, reply, (int) replylen, 0,
210 (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
211 return TRUE;
28f540f4 212 }
e7fd8a39
UD
213 }
214 return TRUE;
28f540f4
RM
215}
216
217static bool_t
e7fd8a39
UD
218svcudp_reply (xprt, msg)
219 SVCXPRT *xprt;
220 struct rpc_msg *msg;
28f540f4 221{
e7fd8a39
UD
222 struct svcudp_data *su = su_data (xprt);
223 XDR *xdrs = &(su->su_xdrs);
224 int slen;
225 bool_t stat = FALSE;
226
227 xdrs->x_op = XDR_ENCODE;
228 XDR_SETPOS (xdrs, 0);
229 msg->rm_xid = su->su_xid;
230 if (xdr_replymsg (xdrs, msg))
231 {
232 slen = (int) XDR_GETPOS (xdrs);
233 if (sendto (xprt->xp_sock, rpc_buffer (xprt), slen, 0,
234 (struct sockaddr *) &(xprt->xp_raddr), xprt->xp_addrlen)
235 == slen)
236 {
237 stat = TRUE;
238 if (su->su_cache && slen >= 0)
239 {
240 cache_set (xprt, (u_long) slen);
241 }
28f540f4 242 }
e7fd8a39
UD
243 }
244 return stat;
28f540f4
RM
245}
246
247static bool_t
e7fd8a39
UD
248svcudp_getargs (xprt, xdr_args, args_ptr)
249 SVCXPRT *xprt;
250 xdrproc_t xdr_args;
251 caddr_t args_ptr;
28f540f4
RM
252{
253
e7fd8a39 254 return (*xdr_args) (&(su_data (xprt)->su_xdrs), args_ptr);
28f540f4
RM
255}
256
257static bool_t
e7fd8a39
UD
258svcudp_freeargs (xprt, xdr_args, args_ptr)
259 SVCXPRT *xprt;
260 xdrproc_t xdr_args;
261 caddr_t args_ptr;
28f540f4 262{
e7fd8a39 263 XDR *xdrs = &(su_data (xprt)->su_xdrs);
28f540f4 264
e7fd8a39
UD
265 xdrs->x_op = XDR_FREE;
266 return (*xdr_args) (xdrs, args_ptr);
28f540f4
RM
267}
268
269static void
e7fd8a39
UD
270svcudp_destroy (xprt)
271 SVCXPRT *xprt;
28f540f4 272{
e7fd8a39
UD
273 struct svcudp_data *su = su_data (xprt);
274
275 xprt_unregister (xprt);
276 (void) close (xprt->xp_sock);
277 XDR_DESTROY (&(su->su_xdrs));
278 mem_free (rpc_buffer (xprt), su->su_iosz);
279 mem_free ((caddr_t) su, sizeof (struct svcudp_data));
280 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
28f540f4
RM
281}
282
283
284/***********this could be a separate file*********************/
285
286/*
287 * Fifo cache for udp server
288 * Copies pointers to reply buffers into fifo cache
289 * Buffers are sent again if retransmissions are detected.
290 */
291
e7fd8a39 292#define SPARSENESS 4 /* 75% sparse */
28f540f4
RM
293
294#define CACHE_PERROR(msg) \
295 (void) fprintf(stderr,"%s\n", msg)
296
297#define ALLOC(type, size) \
298 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
299
300#define BZERO(addr, type, size) \
cbd3dceb 301 bzero((char *) addr, sizeof(type) * (int) (size))
28f540f4
RM
302
303/*
304 * An entry in the cache
305 */
306typedef struct cache_node *cache_ptr;
e7fd8a39
UD
307struct cache_node
308 {
309 /*
310 * Index into cache is xid, proc, vers, prog and address
311 */
312 u_long cache_xid;
313 u_long cache_proc;
314 u_long cache_vers;
315 u_long cache_prog;
316 struct sockaddr_in cache_addr;
317 /*
318 * The cached reply and length
319 */
320 char *cache_reply;
321 u_long cache_replylen;
322 /*
323 * Next node on the list, if there is a collision
324 */
325 cache_ptr cache_next;
326 };
28f540f4
RM
327
328
329
330/*
331 * The entire cache
332 */
e7fd8a39
UD
333struct udp_cache
334 {
335 u_long uc_size; /* size of cache */
336 cache_ptr *uc_entries; /* hash table of entries in cache */
337 cache_ptr *uc_fifo; /* fifo list of entries in cache */
338 u_long uc_nextvictim; /* points to next victim in fifo list */
339 u_long uc_prog; /* saved program number */
340 u_long uc_vers; /* saved version number */
341 u_long uc_proc; /* saved procedure number */
342 struct sockaddr_in uc_addr; /* saved caller's address */
343 };
28f540f4
RM
344
345
346/*
347 * the hashing function
348 */
349#define CACHE_LOC(transp, xid) \
cbd3dceb 350 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
28f540f4
RM
351
352
353/*
cbd3dceb 354 * Enable use of the cache.
28f540f4
RM
355 * Note: there is no disable.
356 */
e7fd8a39
UD
357int
358svcudp_enablecache (SVCXPRT *transp, u_long size)
28f540f4 359{
e7fd8a39
UD
360 struct svcudp_data *su = su_data (transp);
361 struct udp_cache *uc;
362
363 if (su->su_cache != NULL)
364 {
365 CACHE_PERROR (_("enablecache: cache already enabled"));
366 return 0;
367 }
368 uc = ALLOC (struct udp_cache, 1);
369 if (uc == NULL)
370 {
371 CACHE_PERROR (_("enablecache: could not allocate cache"));
372 return 0;
373 }
374 uc->uc_size = size;
375 uc->uc_nextvictim = 0;
376 uc->uc_entries = ALLOC (cache_ptr, size * SPARSENESS);
377 if (uc->uc_entries == NULL)
378 {
379 CACHE_PERROR (_("enablecache: could not allocate cache data"));
380 return 0;
381 }
382 BZERO (uc->uc_entries, cache_ptr, size * SPARSENESS);
383 uc->uc_fifo = ALLOC (cache_ptr, size);
384 if (uc->uc_fifo == NULL)
385 {
386 CACHE_PERROR (_("enablecache: could not allocate cache fifo"));
387 return 0;
388 }
389 BZERO (uc->uc_fifo, cache_ptr, size);
390 su->su_cache = (char *) uc;
391 return 1;
28f540f4
RM
392}
393
394
395/*
396 * Set an entry in the cache
397 */
e7fd8a39
UD
398static void
399cache_set (SVCXPRT *xprt, u_long replylen)
28f540f4 400{
e7fd8a39
UD
401 cache_ptr victim;
402 cache_ptr *vicp;
403 struct svcudp_data *su = su_data (xprt);
404 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
405 u_int loc;
406 char *newbuf;
407
408 /*
409 * Find space for the new entry, either by
410 * reusing an old entry, or by mallocing a new one
411 */
412 victim = uc->uc_fifo[uc->uc_nextvictim];
413 if (victim != NULL)
414 {
415 loc = CACHE_LOC (xprt, victim->cache_xid);
416 for (vicp = &uc->uc_entries[loc];
417 *vicp != NULL && *vicp != victim;
418 vicp = &(*vicp)->cache_next)
419 ;
420 if (*vicp == NULL)
421 {
422 CACHE_PERROR (_("cache_set: victim not found"));
423 return;
28f540f4 424 }
e7fd8a39
UD
425 *vicp = victim->cache_next; /* remote from cache */
426 newbuf = victim->cache_reply;
427 }
428 else
429 {
430 victim = ALLOC (struct cache_node, 1);
431 if (victim == NULL)
432 {
433 CACHE_PERROR (_("cache_set: victim alloc failed"));
434 return;
435 }
436 newbuf = mem_alloc (su->su_iosz);
437 if (newbuf == NULL)
438 {
439 CACHE_PERROR (_("cache_set: could not allocate new rpc_buffer"));
440 return;
441 }
442 }
443
444 /*
445 * Store it away
446 */
447 victim->cache_replylen = replylen;
448 victim->cache_reply = rpc_buffer (xprt);
449 rpc_buffer (xprt) = newbuf;
450 xdrmem_create (&(su->su_xdrs), rpc_buffer (xprt), su->su_iosz, XDR_ENCODE);
451 victim->cache_xid = su->su_xid;
452 victim->cache_proc = uc->uc_proc;
453 victim->cache_vers = uc->uc_vers;
454 victim->cache_prog = uc->uc_prog;
455 victim->cache_addr = uc->uc_addr;
456 loc = CACHE_LOC (xprt, victim->cache_xid);
457 victim->cache_next = uc->uc_entries[loc];
458 uc->uc_entries[loc] = victim;
459 uc->uc_fifo[uc->uc_nextvictim++] = victim;
460 uc->uc_nextvictim %= uc->uc_size;
28f540f4
RM
461}
462
463/*
464 * Try to get an entry from the cache
465 * return 1 if found, 0 if not found
466 */
e7fd8a39
UD
467static int
468cache_get (xprt, msg, replyp, replylenp)
469 SVCXPRT *xprt;
470 struct rpc_msg *msg;
471 char **replyp;
472 u_long *replylenp;
28f540f4 473{
e7fd8a39
UD
474 u_int loc;
475 cache_ptr ent;
476 struct svcudp_data *su = su_data (xprt);
477 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
478
479#define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
480
481 loc = CACHE_LOC (xprt, su->su_xid);
482 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next)
483 {
484 if (ent->cache_xid == su->su_xid &&
485 ent->cache_proc == uc->uc_proc &&
486 ent->cache_vers == uc->uc_vers &&
487 ent->cache_prog == uc->uc_prog &&
488 EQADDR (ent->cache_addr, uc->uc_addr))
489 {
490 *replyp = ent->cache_reply;
491 *replylenp = ent->cache_replylen;
492 return 1;
28f540f4 493 }
e7fd8a39
UD
494 }
495 /*
496 * Failed to find entry
497 * Remember a few things so we can do a set later
498 */
499 uc->uc_proc = msg->rm_call.cb_proc;
500 uc->uc_vers = msg->rm_call.cb_vers;
501 uc->uc_prog = msg->rm_call.cb_prog;
502 uc->uc_addr = xprt->xp_raddr;
503 return 0;
28f540f4 504}
This page took 0.111752 seconds and 5 git commands to generate.