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