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.3 2002/02/19 20:36:25 epeyton 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.
72 #include <sys/socket.h>
75 extern int bindresvport();
77 #define rpc_buffer(xprt) ((xprt)->xp_p1)
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();
86 static struct xp_ops svcudp_op
= {
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 */
107 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
111 * xprt = svcudp_create(sock);
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.
123 svcudp_bufcreate(sock
, sendsz
, recvsz
)
125 u_int sendsz
, recvsz
;
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
);
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
);
140 bzero((char *)&addr
, sizeof (addr
));
141 addr
.sin_family
= AF_INET
;
142 if (bindresvport(sock
, &addr
)) {
144 (void)bind(sock
, (struct sockaddr
*)&addr
, len
);
146 if (getsockname(sock
, (struct sockaddr
*)&addr
, &len
) != 0) {
147 perror("svcudp_create - cannot getsockname");
150 return ((SVCXPRT
*)NULL
);
152 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
154 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
157 su
= (struct svcudp_data
*)mem_alloc(sizeof(*su
));
159 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
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");
168 &(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_DECODE
);
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
;
184 return(svcudp_bufcreate(sock
, UDPMSGSIZE
, UDPMSGSIZE
));
187 static enum xprt_stat
196 svcudp_recv(xprt
, msg
)
197 register SVCXPRT
*xprt
;
200 register struct svcudp_data
*su
= su_data(xprt
);
201 register XDR
*xdrs
= &(su
->su_xdrs
);
205 static int cache_get();
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
)
213 if (rlen
< 4*sizeof(u_long
))
215 xdrs
->x_op
= XDR_DECODE
;
217 if (! xdr_callmsg(xdrs
, msg
))
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
);
231 svcudp_reply(xprt
, msg
)
232 register SVCXPRT
*xprt
;
235 register struct svcudp_data
*su
= su_data(xprt
);
236 register XDR
*xdrs
= &(su
->su_xdrs
);
238 register bool_t stat
= FALSE
;
239 static void cache_set();
241 xdrs
->x_op
= XDR_ENCODE
;
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
)
250 if (su
->su_cache
&& slen
>= 0) {
251 cache_set(xprt
, (u_long
) slen
);
259 svcudp_getargs(xprt
, xdr_args
, args_ptr
)
265 return ((*xdr_args
)(&(su_data(xprt
)->su_xdrs
), args_ptr
));
269 svcudp_freeargs(xprt
, xdr_args
, args_ptr
)
274 register XDR
*xdrs
= &(su_data(xprt
)->su_xdrs
);
276 xdrs
->x_op
= XDR_FREE
;
277 return ((*xdr_args
)(xdrs
, args_ptr
));
282 register SVCXPRT
*xprt
;
284 register struct svcudp_data
*su
= su_data(xprt
);
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
));
295 /***********this could be a separate file*********************/
298 * Fifo cache for udp server
299 * Copies pointers to reply buffers into fifo cache
300 * Buffers are sent again if retransmissions are detected.
303 #define SPARSENESS 4 /* 75% sparse */
305 #define CACHE_PERROR(msg) \
306 (void) fprintf(stderr,"%s\n", msg)
308 #define ALLOC(type, size) \
309 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
311 #define BZERO(addr, type, size) \
312 bzero((char *) addr, sizeof(type) * (int) (size))
315 * An entry in the cache
317 typedef struct cache_node
*cache_ptr
;
320 * Index into cache is xid, proc, vers, prog and address
326 struct sockaddr_in cache_addr
;
328 * The cached reply and length
331 u_long cache_replylen
;
333 * Next node on the list, if there is a collision
335 cache_ptr cache_next
;
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 */
356 * the hashing function
358 #define CACHE_LOC(transp, xid) \
359 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
363 * Enable use of the cache.
364 * Note: there is no disable.
367 svcudp_enablecache(transp
, size
)
371 struct svcudp_data
*su
= su_data(transp
);
372 struct udp_cache
*uc
;
374 if (su
->su_cache
!= NULL
) {
375 CACHE_PERROR("enablecache: cache already enabled");
378 uc
= ALLOC(struct udp_cache
, 1);
380 CACHE_PERROR("enablecache: could not allocate cache");
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");
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");
396 BZERO(uc
->uc_fifo
, cache_ptr
, size
);
397 su
->su_cache
= (char *) uc
;
403 * Set an entry in the cache
406 cache_set(xprt
, replylen
)
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
;
418 * Find space for the new entry, either by
419 * reusing an old entry, or by mallocing a new one
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
)
429 CACHE_PERROR("cache_set: victim not found");
432 *vicp
= victim
->cache_next
; /* remote from cache */
433 newbuf
= victim
->cache_reply
;
435 victim
= ALLOC(struct cache_node
, 1);
436 if (victim
== NULL
) {
437 CACHE_PERROR("cache_set: victim alloc failed");
440 newbuf
= mem_alloc(su
->su_iosz
);
441 if (newbuf
== NULL
) {
442 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
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
;
467 * Try to get an entry from the cache
468 * return 1 if found, 0 if not found
471 cache_get(xprt
, msg
, replyp
, replylenp
)
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
;
482 # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
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
;
497 * Failed to find entry
498 * Remember a few things so we can do a set later
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
;