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