update from main archive 961119
[glibc.git] / sunrpc / svc_udp.c
blobc9efc9702405c4c37115e12095e5c4820a6e8611
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
32 #endif
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.)
39 * Copyright (C) 1984, Sun Microsystems, Inc.
42 #include <stdio.h>
43 #include <rpc/rpc.h>
44 #include <sys/socket.h>
45 #include <errno.h>
48 #define rpc_buffer(xprt) ((xprt)->xp_p1)
49 #ifndef MAX
50 # define MAX(a, b) ((a > b) ? a : b)
51 #endif
53 static bool_t svcudp_recv();
54 static bool_t svcudp_reply();
55 static enum xprt_stat svcudp_stat();
56 static bool_t svcudp_getargs();
57 static bool_t svcudp_freeargs();
58 static void svcudp_destroy();
60 static struct xp_ops svcudp_op = {
61 svcudp_recv,
62 svcudp_stat,
63 svcudp_getargs,
64 svcudp_reply,
65 svcudp_freeargs,
66 svcudp_destroy
69 #ifndef errno
70 extern int errno;
71 #endif
74 * kept in xprt->xp_p2
76 struct 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 */
83 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
86 * Usage:
87 * xprt = svcudp_create(sock);
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.
98 SVCXPRT *
99 svcudp_bufcreate(sock, sendsz, recvsz)
100 register int sock;
101 u_int sendsz, recvsz;
103 bool_t madesock = FALSE;
104 register SVCXPRT *xprt;
105 register struct svcudp_data *su;
106 struct sockaddr_in addr;
107 int len = sizeof(struct sockaddr_in);
109 if (sock == RPC_ANYSOCK) {
110 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
111 perror(_("svcudp_create: socket creation problem"));
112 return ((SVCXPRT *)NULL);
114 madesock = TRUE;
116 bzero((char *)&addr, sizeof (addr));
117 addr.sin_family = AF_INET;
118 if (bindresvport(sock, &addr)) {
119 addr.sin_port = 0;
120 (void)bind(sock, (struct sockaddr *)&addr, len);
122 if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
123 perror(_("svcudp_create - cannot getsockname"));
124 if (madesock)
125 (void)close(sock);
126 return ((SVCXPRT *)NULL);
128 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
129 if (xprt == NULL) {
130 (void)fprintf(stderr, "svcudp_create: out of memory\n");
131 return (NULL);
133 su = (struct svcudp_data *)mem_alloc(sizeof(*su));
134 if (su == NULL) {
135 (void)fprintf(stderr, "svcudp_create: out of memory\n");
136 return (NULL);
138 su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
139 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
140 (void)fprintf(stderr, "svcudp_create: out of memory\n");
141 return (NULL);
143 xdrmem_create(
144 &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
145 su->su_cache = NULL;
146 xprt->xp_p2 = (caddr_t)su;
147 xprt->xp_verf.oa_base = su->su_verfbody;
148 xprt->xp_ops = &svcudp_op;
149 xprt->xp_port = ntohs(addr.sin_port);
150 xprt->xp_sock = sock;
151 xprt_register(xprt);
152 return (xprt);
155 SVCXPRT *
156 svcudp_create(sock)
157 int sock;
160 return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
163 static enum xprt_stat
164 svcudp_stat(xprt)
165 SVCXPRT *xprt;
168 return (XPRT_IDLE);
171 static bool_t
172 svcudp_recv(xprt, msg)
173 register SVCXPRT *xprt;
174 struct rpc_msg *msg;
176 register struct svcudp_data *su = su_data(xprt);
177 register XDR *xdrs = &(su->su_xdrs);
178 register int rlen;
179 char *reply;
180 u_long replylen;
182 again:
183 xprt->xp_addrlen = sizeof(struct sockaddr_in);
184 rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
185 0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
186 if (rlen == -1 && errno == EINTR)
187 goto again;
188 if (rlen < 16) /* < 4 32-bit ints? */
189 return (FALSE);
190 xdrs->x_op = XDR_DECODE;
191 XDR_SETPOS(xdrs, 0);
192 if (! xdr_callmsg(xdrs, msg))
193 return (FALSE);
194 su->su_xid = msg->rm_xid;
195 if (su->su_cache != NULL) {
196 if (cache_get(xprt, msg, &reply, &replylen)) {
197 (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
198 (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
199 return (TRUE);
202 return (TRUE);
205 static bool_t
206 svcudp_reply(xprt, msg)
207 register SVCXPRT *xprt;
208 struct rpc_msg *msg;
210 register struct svcudp_data *su = su_data(xprt);
211 register XDR *xdrs = &(su->su_xdrs);
212 register int slen;
213 register bool_t stat = FALSE;
215 xdrs->x_op = XDR_ENCODE;
216 XDR_SETPOS(xdrs, 0);
217 msg->rm_xid = su->su_xid;
218 if (xdr_replymsg(xdrs, msg)) {
219 slen = (int)XDR_GETPOS(xdrs);
220 if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
221 (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
222 == slen) {
223 stat = TRUE;
224 if (su->su_cache && slen >= 0) {
225 cache_set(xprt, (u_long) slen);
229 return (stat);
232 static bool_t
233 svcudp_getargs(xprt, xdr_args, args_ptr)
234 SVCXPRT *xprt;
235 xdrproc_t xdr_args;
236 caddr_t args_ptr;
239 return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
242 static bool_t
243 svcudp_freeargs(xprt, xdr_args, args_ptr)
244 SVCXPRT *xprt;
245 xdrproc_t xdr_args;
246 caddr_t args_ptr;
248 register XDR *xdrs = &(su_data(xprt)->su_xdrs);
250 xdrs->x_op = XDR_FREE;
251 return ((*xdr_args)(xdrs, args_ptr));
254 static void
255 svcudp_destroy(xprt)
256 register SVCXPRT *xprt;
258 register struct svcudp_data *su = su_data(xprt);
260 xprt_unregister(xprt);
261 (void)close(xprt->xp_sock);
262 XDR_DESTROY(&(su->su_xdrs));
263 mem_free(rpc_buffer(xprt), su->su_iosz);
264 mem_free((caddr_t)su, sizeof(struct svcudp_data));
265 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
269 /***********this could be a separate file*********************/
272 * Fifo cache for udp server
273 * Copies pointers to reply buffers into fifo cache
274 * Buffers are sent again if retransmissions are detected.
277 #define SPARSENESS 4 /* 75% sparse */
279 #define CACHE_PERROR(msg) \
280 (void) fprintf(stderr,"%s\n", msg)
282 #define ALLOC(type, size) \
283 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
285 #define BZERO(addr, type, size) \
286 bzero((char *) addr, sizeof(type) * (int) (size))
289 * An entry in the cache
291 typedef struct cache_node *cache_ptr;
292 struct cache_node {
294 * Index into cache is xid, proc, vers, prog and address
296 u_long cache_xid;
297 u_long cache_proc;
298 u_long cache_vers;
299 u_long cache_prog;
300 struct sockaddr_in cache_addr;
302 * The cached reply and length
304 char * cache_reply;
305 u_long cache_replylen;
307 * Next node on the list, if there is a collision
309 cache_ptr cache_next;
315 * The entire cache
317 struct udp_cache {
318 u_long uc_size; /* size of cache */
319 cache_ptr *uc_entries; /* hash table of entries in cache */
320 cache_ptr *uc_fifo; /* fifo list of entries in cache */
321 u_long uc_nextvictim; /* points to next victim in fifo list */
322 u_long uc_prog; /* saved program number */
323 u_long uc_vers; /* saved version number */
324 u_long uc_proc; /* saved procedure number */
325 struct sockaddr_in uc_addr; /* saved caller's address */
330 * the hashing function
332 #define CACHE_LOC(transp, xid) \
333 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
337 * Enable use of the cache.
338 * Note: there is no disable.
340 svcudp_enablecache(transp, size)
341 SVCXPRT *transp;
342 u_long size;
344 struct svcudp_data *su = su_data(transp);
345 struct udp_cache *uc;
347 if (su->su_cache != NULL) {
348 CACHE_PERROR(_("enablecache: cache already enabled"));
349 return(0);
351 uc = ALLOC(struct udp_cache, 1);
352 if (uc == NULL) {
353 CACHE_PERROR(_("enablecache: could not allocate cache"));
354 return(0);
356 uc->uc_size = size;
357 uc->uc_nextvictim = 0;
358 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
359 if (uc->uc_entries == NULL) {
360 CACHE_PERROR(_("enablecache: could not allocate cache data"));
361 return(0);
363 BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
364 uc->uc_fifo = ALLOC(cache_ptr, size);
365 if (uc->uc_fifo == NULL) {
366 CACHE_PERROR(_("enablecache: could not allocate cache fifo"));
367 return(0);
369 BZERO(uc->uc_fifo, cache_ptr, size);
370 su->su_cache = (char *) uc;
371 return(1);
376 * Set an entry in the cache
378 static
379 cache_set(xprt, replylen)
380 SVCXPRT *xprt;
381 u_long replylen;
383 register cache_ptr victim;
384 register cache_ptr *vicp;
385 register struct svcudp_data *su = su_data(xprt);
386 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
387 u_int loc;
388 char *newbuf;
391 * Find space for the new entry, either by
392 * reusing an old entry, or by mallocing a new one
394 victim = uc->uc_fifo[uc->uc_nextvictim];
395 if (victim != NULL) {
396 loc = CACHE_LOC(xprt, victim->cache_xid);
397 for (vicp = &uc->uc_entries[loc];
398 *vicp != NULL && *vicp != victim;
399 vicp = &(*vicp)->cache_next)
401 if (*vicp == NULL) {
402 CACHE_PERROR(_("cache_set: victim not found"));
403 return;
405 *vicp = victim->cache_next; /* remote from cache */
406 newbuf = victim->cache_reply;
407 } else {
408 victim = ALLOC(struct cache_node, 1);
409 if (victim == NULL) {
410 CACHE_PERROR("cache_set: victim alloc failed");
411 return;
413 newbuf = mem_alloc(su->su_iosz);
414 if (newbuf == NULL) {
415 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
416 return;
421 * Store it away
423 victim->cache_replylen = replylen;
424 victim->cache_reply = rpc_buffer(xprt);
425 rpc_buffer(xprt) = newbuf;
426 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
427 victim->cache_xid = su->su_xid;
428 victim->cache_proc = uc->uc_proc;
429 victim->cache_vers = uc->uc_vers;
430 victim->cache_prog = uc->uc_prog;
431 victim->cache_addr = uc->uc_addr;
432 loc = CACHE_LOC(xprt, victim->cache_xid);
433 victim->cache_next = uc->uc_entries[loc];
434 uc->uc_entries[loc] = victim;
435 uc->uc_fifo[uc->uc_nextvictim++] = victim;
436 uc->uc_nextvictim %= uc->uc_size;
440 * Try to get an entry from the cache
441 * return 1 if found, 0 if not found
443 static
444 cache_get(xprt, msg, replyp, replylenp)
445 SVCXPRT *xprt;
446 struct rpc_msg *msg;
447 char **replyp;
448 u_long *replylenp;
450 u_int loc;
451 register cache_ptr ent;
452 register struct svcudp_data *su = su_data(xprt);
453 register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
455 # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
457 loc = CACHE_LOC(xprt, su->su_xid);
458 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
459 if (ent->cache_xid == su->su_xid &&
460 ent->cache_proc == uc->uc_proc &&
461 ent->cache_vers == uc->uc_vers &&
462 ent->cache_prog == uc->uc_prog &&
463 EQADDR(ent->cache_addr, uc->uc_addr)) {
464 *replyp = ent->cache_reply;
465 *replylenp = ent->cache_replylen;
466 return(1);
470 * Failed to find entry
471 * Remember a few things so we can do a set later
473 uc->uc_proc = msg->rm_call.cb_proc;
474 uc->uc_vers = msg->rm_call.cb_vers;
475 uc->uc_prog = msg->rm_call.cb_prog;
476 uc->uc_addr = xprt->xp_raddr;
477 return(0);