Libinfo-129.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.3 2002/02/19 20:36:25 epeyton 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 <string.h>
70 #include <unistd.h>
71 #include <rpc/rpc.h>
72 #include <sys/socket.h>
73 #include <errno.h>
74
75 extern int bindresvport();
76
77 #define rpc_buffer(xprt) ((xprt)->xp_p1)
78
79 static bool_t svcudp_recv();
80 static bool_t svcudp_reply();
81 static enum xprt_stat svcudp_stat();
82 static bool_t svcudp_getargs();
83 static bool_t svcudp_freeargs();
84 static void svcudp_destroy();
85
86 static struct xp_ops svcudp_op = {
87 svcudp_recv,
88 svcudp_stat,
89 svcudp_getargs,
90 svcudp_reply,
91 svcudp_freeargs,
92 svcudp_destroy
93 };
94
95 extern int errno;
96
97 /*
98 * kept in xprt->xp_p2
99 */
100 struct svcudp_data {
101 u_int su_iosz; /* byte size of send.recv buffer */
102 u_long su_xid; /* transaction id */
103 XDR su_xdrs; /* XDR handle */
104 char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */
105 char * su_cache; /* cached data, NULL if no cache */
106 };
107 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
108
109 /*
110 * Usage:
111 * xprt = svcudp_create(sock);
112 *
113 * If sock<0 then a socket is created, else sock is used.
114 * If the socket, sock is not bound to a port then svcudp_create
115 * binds it to an arbitrary port. In any (successful) case,
116 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
117 * associated port number.
118 * Once *xprt is initialized, it is registered as a transporter;
119 * see (svc.h, xprt_register).
120 * The routines returns NULL if a problem occurred.
121 */
122 SVCXPRT *
123 svcudp_bufcreate(sock, sendsz, recvsz)
124 register int sock;
125 u_int sendsz, recvsz;
126 {
127 bool_t madesock = FALSE;
128 register SVCXPRT *xprt;
129 register struct svcudp_data *su;
130 struct sockaddr_in addr;
131 int len = sizeof(struct sockaddr_in);
132
133 if (sock == RPC_ANYSOCK) {
134 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
135 perror("svcudp_create: socket creation problem");
136 return ((SVCXPRT *)NULL);
137 }
138 madesock = TRUE;
139 }
140 bzero((char *)&addr, sizeof (addr));
141 addr.sin_family = AF_INET;
142 if (bindresvport(sock, &addr)) {
143 addr.sin_port = 0;
144 (void)bind(sock, (struct sockaddr *)&addr, len);
145 }
146 if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
147 perror("svcudp_create - cannot getsockname");
148 if (madesock)
149 (void)close(sock);
150 return ((SVCXPRT *)NULL);
151 }
152 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
153 if (xprt == NULL) {
154 (void)fprintf(stderr, "svcudp_create: out of memory\n");
155 return (NULL);
156 }
157 su = (struct svcudp_data *)mem_alloc(sizeof(*su));
158 if (su == NULL) {
159 (void)fprintf(stderr, "svcudp_create: out of memory\n");
160 return (NULL);
161 }
162 su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
163 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
164 (void)fprintf(stderr, "svcudp_create: out of memory\n");
165 return (NULL);
166 }
167 xdrmem_create(
168 &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
169 su->su_cache = NULL;
170 xprt->xp_p2 = (caddr_t)su;
171 xprt->xp_verf.oa_base = su->su_verfbody;
172 xprt->xp_ops = &svcudp_op;
173 xprt->xp_port = ntohs(addr.sin_port);
174 xprt->xp_sock = sock;
175 xprt_register(xprt);
176 return (xprt);
177 }
178
179 SVCXPRT *
180 svcudp_create(sock)
181 int sock;
182 {
183
184 return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
185 }
186
187 static enum xprt_stat
188 svcudp_stat(xprt)
189 SVCXPRT *xprt;
190 {
191
192 return (XPRT_IDLE);
193 }
194
195 static bool_t
196 svcudp_recv(xprt, msg)
197 register SVCXPRT *xprt;
198 struct rpc_msg *msg;
199 {
200 register struct svcudp_data *su = su_data(xprt);
201 register XDR *xdrs = &(su->su_xdrs);
202 register int rlen;
203 char *reply;
204 u_long replylen;
205 static int cache_get();
206
207 again:
208 xprt->xp_addrlen = sizeof(struct sockaddr_in);
209 rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
210 0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
211 if (rlen == -1 && errno == EINTR)
212 goto again;
213 if (rlen < 4*sizeof(u_long))
214 return (FALSE);
215 xdrs->x_op = XDR_DECODE;
216 XDR_SETPOS(xdrs, 0);
217 if (! xdr_callmsg(xdrs, msg))
218 return (FALSE);
219 su->su_xid = msg->rm_xid;
220 if (su->su_cache != NULL) {
221 if (cache_get(xprt, msg, &reply, &replylen)) {
222 (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
223 (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
224 return (TRUE);
225 }
226 }
227 return (TRUE);
228 }
229
230 static bool_t
231 svcudp_reply(xprt, msg)
232 register SVCXPRT *xprt;
233 struct rpc_msg *msg;
234 {
235 register struct svcudp_data *su = su_data(xprt);
236 register XDR *xdrs = &(su->su_xdrs);
237 register int slen;
238 register bool_t stat = FALSE;
239 static void cache_set();
240
241 xdrs->x_op = XDR_ENCODE;
242 XDR_SETPOS(xdrs, 0);
243 msg->rm_xid = su->su_xid;
244 if (xdr_replymsg(xdrs, msg)) {
245 slen = (int)XDR_GETPOS(xdrs);
246 if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
247 (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
248 == slen) {
249 stat = TRUE;
250 if (su->su_cache && slen >= 0) {
251 cache_set(xprt, (u_long) slen);
252 }
253 }
254 }
255 return (stat);
256 }
257
258 static bool_t
259 svcudp_getargs(xprt, xdr_args, args_ptr)
260 SVCXPRT *xprt;
261 xdrproc_t xdr_args;
262 caddr_t args_ptr;
263 {
264
265 return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
266 }
267
268 static bool_t
269 svcudp_freeargs(xprt, xdr_args, args_ptr)
270 SVCXPRT *xprt;
271 xdrproc_t xdr_args;
272 caddr_t args_ptr;
273 {
274 register XDR *xdrs = &(su_data(xprt)->su_xdrs);
275
276 xdrs->x_op = XDR_FREE;
277 return ((*xdr_args)(xdrs, args_ptr));
278 }
279
280 static void
281 svcudp_destroy(xprt)
282 register SVCXPRT *xprt;
283 {
284 register struct svcudp_data *su = su_data(xprt);
285
286 xprt_unregister(xprt);
287 (void)close(xprt->xp_sock);
288 XDR_DESTROY(&(su->su_xdrs));
289 mem_free(rpc_buffer(xprt), su->su_iosz);
290 mem_free((caddr_t)su, sizeof(struct svcudp_data));
291 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
292 }
293
294
295 /***********this could be a separate file*********************/
296
297 /*
298 * Fifo cache for udp server
299 * Copies pointers to reply buffers into fifo cache
300 * Buffers are sent again if retransmissions are detected.
301 */
302
303 #define SPARSENESS 4 /* 75% sparse */
304
305 #define CACHE_PERROR(msg) \
306 (void) fprintf(stderr,"%s\n", msg)
307
308 #define ALLOC(type, size) \
309 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
310
311 #define BZERO(addr, type, size) \
312 bzero((char *) addr, sizeof(type) * (int) (size))
313
314 /*
315 * An entry in the cache
316 */
317 typedef struct cache_node *cache_ptr;
318 struct cache_node {
319 /*
320 * Index into cache is xid, proc, vers, prog and address
321 */
322 u_long cache_xid;
323 u_long cache_proc;
324 u_long cache_vers;
325 u_long cache_prog;
326 struct sockaddr_in cache_addr;
327 /*
328 * The cached reply and length
329 */
330 char * cache_reply;
331 u_long cache_replylen;
332 /*
333 * Next node on the list, if there is a collision
334 */
335 cache_ptr cache_next;
336 };
337
338
339
340 /*
341 * The entire cache
342 */
343 struct udp_cache {
344 u_long uc_size; /* size of cache */
345 cache_ptr *uc_entries; /* hash table of entries in cache */
346 cache_ptr *uc_fifo; /* fifo list of entries in cache */
347 u_long uc_nextvictim; /* points to next victim in fifo list */
348 u_long uc_prog; /* saved program number */
349 u_long uc_vers; /* saved version number */
350 u_long uc_proc; /* saved procedure number */
351 struct sockaddr_in uc_addr; /* saved caller's address */
352 };
353
354
355 /*
356 * the hashing function
357 */
358 #define CACHE_LOC(transp, xid) \
359 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
360
361
362 /*
363 * Enable use of the cache.
364 * Note: there is no disable.
365 */
366 int
367 svcudp_enablecache(transp, size)
368 SVCXPRT *transp;
369 u_long size;
370 {
371 struct svcudp_data *su = su_data(transp);
372 struct udp_cache *uc;
373
374 if (su->su_cache != NULL) {
375 CACHE_PERROR("enablecache: cache already enabled");
376 return(0);
377 }
378 uc = ALLOC(struct udp_cache, 1);
379 if (uc == NULL) {
380 CACHE_PERROR("enablecache: could not allocate cache");
381 return(0);
382 }
383 uc->uc_size = size;
384 uc->uc_nextvictim = 0;
385 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
386 if (uc->uc_entries == NULL) {
387 CACHE_PERROR("enablecache: could not allocate cache data");
388 return(0);
389 }
390 BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
391 uc->uc_fifo = ALLOC(cache_ptr, size);
392 if (uc->uc_fifo == NULL) {
393 CACHE_PERROR("enablecache: could not allocate cache fifo");
394 return(0);
395 }
396 BZERO(uc->uc_fifo, cache_ptr, size);
397 su->su_cache = (char *) uc;
398 return(1);
399 }
400
401
402 /*
403 * Set an entry in the cache
404 */
405 static void
406 cache_set(xprt, replylen)
407 SVCXPRT *xprt;
408 u_long replylen;
409 {
410 register cache_ptr victim;
411 register cache_ptr *vicp;
412 register struct svcudp_data *su = su_data(xprt);
413 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
414 u_int loc;
415 char *newbuf;
416
417 /*
418 * Find space for the new entry, either by
419 * reusing an old entry, or by mallocing a new one
420 */
421 victim = uc->uc_fifo[uc->uc_nextvictim];
422 if (victim != NULL) {
423 loc = CACHE_LOC(xprt, victim->cache_xid);
424 for (vicp = &uc->uc_entries[loc];
425 *vicp != NULL && *vicp != victim;
426 vicp = &(*vicp)->cache_next)
427 ;
428 if (*vicp == NULL) {
429 CACHE_PERROR("cache_set: victim not found");
430 return;
431 }
432 *vicp = victim->cache_next; /* remote from cache */
433 newbuf = victim->cache_reply;
434 } else {
435 victim = ALLOC(struct cache_node, 1);
436 if (victim == NULL) {
437 CACHE_PERROR("cache_set: victim alloc failed");
438 return;
439 }
440 newbuf = mem_alloc(su->su_iosz);
441 if (newbuf == NULL) {
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 register cache_ptr ent;
479 register struct svcudp_data *su = su_data(xprt);
480 register 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 if (ent->cache_xid == su->su_xid &&
487 ent->cache_proc == uc->uc_proc &&
488 ent->cache_vers == uc->uc_vers &&
489 ent->cache_prog == uc->uc_prog &&
490 EQADDR(ent->cache_addr, uc->uc_addr)) {
491 *replyp = ent->cache_reply;
492 *replylenp = ent->cache_replylen;
493 return(1);
494 }
495 }
496 /*
497 * Failed to find entry
498 * Remember a few things so we can do a set later
499 */
500 uc->uc_proc = msg->rm_call.cb_proc;
501 uc->uc_vers = msg->rm_call.cb_vers;
502 uc->uc_prog = msg->rm_call.cb_prog;
503 uc->uc_addr = xprt->xp_raddr;
504 return(0);
505 }
506