]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/svc_udp.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
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.
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.
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.
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.
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.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
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 $";
61 * Server side for UDP/IP based RPC. (Does some caching in the hopes of
62 * achieving execute-at-most-once semantics.)
64 * Copyright (C) 1984, Sun Microsystems, Inc.
70 #include <sys/socket.h>
74 #define rpc_buffer(xprt) ((xprt)->xp_p1)
75 #define MAX(a, b) ((a > b) ? a : b)
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();
84 static struct xp_ops svcudp_op
= {
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 */
105 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
109 * xprt = svcudp_create(sock);
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.
121 svcudp_bufcreate(sock
, sendsz
, recvsz
)
123 u_int sendsz
, recvsz
;
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
);
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
);
138 bzero((char *)&addr
, sizeof (addr
));
139 addr
.sin_family
= AF_INET
;
140 if (bindresvport(sock
, &addr
)) {
142 (void)bind(sock
, (struct sockaddr
*)&addr
, len
);
144 if (getsockname(sock
, (struct sockaddr
*)&addr
, &len
) != 0) {
145 perror("svcudp_create - cannot getsockname");
148 return ((SVCXPRT
*)NULL
);
150 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
152 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
155 su
= (struct svcudp_data
*)mem_alloc(sizeof(*su
));
157 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
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");
166 &(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_DECODE
);
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
;
182 return(svcudp_bufcreate(sock
, UDPMSGSIZE
, UDPMSGSIZE
));
185 static enum xprt_stat
194 svcudp_recv(xprt
, msg
)
195 register SVCXPRT
*xprt
;
198 register struct svcudp_data
*su
= su_data(xprt
);
199 register XDR
*xdrs
= &(su
->su_xdrs
);
203 static int cache_get();
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
)
211 if (rlen
< 4*sizeof(u_long
))
213 xdrs
->x_op
= XDR_DECODE
;
215 if (! xdr_callmsg(xdrs
, msg
))
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
);
229 svcudp_reply(xprt
, msg
)
230 register SVCXPRT
*xprt
;
233 register struct svcudp_data
*su
= su_data(xprt
);
234 register XDR
*xdrs
= &(su
->su_xdrs
);
236 register bool_t stat
= FALSE
;
237 static void cache_set();
239 xdrs
->x_op
= XDR_ENCODE
;
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
)
248 if (su
->su_cache
&& slen
>= 0) {
249 cache_set(xprt
, (u_long
) slen
);
257 svcudp_getargs(xprt
, xdr_args
, args_ptr
)
263 return ((*xdr_args
)(&(su_data(xprt
)->su_xdrs
), args_ptr
));
267 svcudp_freeargs(xprt
, xdr_args
, args_ptr
)
272 register XDR
*xdrs
= &(su_data(xprt
)->su_xdrs
);
274 xdrs
->x_op
= XDR_FREE
;
275 return ((*xdr_args
)(xdrs
, args_ptr
));
280 register SVCXPRT
*xprt
;
282 register struct svcudp_data
*su
= su_data(xprt
);
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
));
293 /***********this could be a separate file*********************/
296 * Fifo cache for udp server
297 * Copies pointers to reply buffers into fifo cache
298 * Buffers are sent again if retransmissions are detected.
301 #define SPARSENESS 4 /* 75% sparse */
303 #define CACHE_PERROR(msg) \
304 (void) fprintf(stderr,"%s\n", msg)
306 #define ALLOC(type, size) \
307 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
309 #define BZERO(addr, type, size) \
310 bzero((char *) addr, sizeof(type) * (int) (size))
313 * An entry in the cache
315 typedef struct cache_node
*cache_ptr
;
318 * Index into cache is xid, proc, vers, prog and address
324 struct sockaddr_in cache_addr
;
326 * The cached reply and length
329 u_long cache_replylen
;
331 * Next node on the list, if there is a collision
333 cache_ptr cache_next
;
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 */
354 * the hashing function
356 #define CACHE_LOC(transp, xid) \
357 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
361 * Enable use of the cache.
362 * Note: there is no disable.
364 svcudp_enablecache(transp
, size
)
368 struct svcudp_data
*su
= su_data(transp
);
369 struct udp_cache
*uc
;
371 if (su
->su_cache
!= NULL
) {
372 CACHE_PERROR("enablecache: cache already enabled");
375 uc
= ALLOC(struct udp_cache
, 1);
377 CACHE_PERROR("enablecache: could not allocate cache");
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");
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");
393 BZERO(uc
->uc_fifo
, cache_ptr
, size
);
394 su
->su_cache
= (char *) uc
;
400 * Set an entry in the cache
403 cache_set(xprt
, replylen
)
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
;
415 * Find space for the new entry, either by
416 * reusing an old entry, or by mallocing a new one
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
)
426 CACHE_PERROR("cache_set: victim not found");
429 *vicp
= victim
->cache_next
; /* remote from cache */
430 newbuf
= victim
->cache_reply
;
432 victim
= ALLOC(struct cache_node
, 1);
433 if (victim
== NULL
) {
434 CACHE_PERROR("cache_set: victim alloc failed");
437 newbuf
= mem_alloc(su
->su_iosz
);
438 if (newbuf
== NULL
) {
439 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
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
;
464 * Try to get an entry from the cache
465 * return 1 if found, 0 if not found
468 cache_get(xprt
, msg
, replyp
, replylenp
)
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
;
479 # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
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
;
494 * Failed to find entry
495 * Remember a few things so we can do a set later
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
;