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