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