]>
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.5 2004/10/13 00:24:07 jkh 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>
73 #include <sys/param.h>
76 extern int bindresvport();
78 #define rpc_buffer(xprt) ((xprt)->xp_p1)
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();
87 static struct xp_ops svcudp_op
= {
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 */
108 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
112 * xprt = svcudp_create(sock);
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.
124 svcudp_bufcreate(sock
, sendsz
, recvsz
)
126 u_int sendsz
, recvsz
;
128 bool_t madesock
= FALSE
;
129 register SVCXPRT
*xprt
;
130 register struct svcudp_data
*su
;
131 struct sockaddr_in addr
;
132 int len
= sizeof(struct sockaddr_in
);
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
);
141 bzero((char *)&addr
, sizeof (addr
));
142 addr
.sin_family
= AF_INET
;
143 if (bindresvport(sock
, &addr
)) {
145 (void)bind(sock
, (struct sockaddr
*)&addr
, len
);
147 if (getsockname(sock
, (struct sockaddr
*)&addr
, &len
) != 0) {
148 perror("svcudp_create - cannot getsockname");
151 return ((SVCXPRT
*)NULL
);
153 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
155 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
158 su
= (struct svcudp_data
*)mem_alloc(sizeof(*su
));
160 (void)fprintf(stderr
, "svcudp_create: out of memory\n");
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");
169 &(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_DECODE
);
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
;
185 return(svcudp_bufcreate(sock
, UDPMSGSIZE
, UDPMSGSIZE
));
188 static enum xprt_stat
196 static int cache_get();
197 static void cache_set();
200 svcudp_recv(xprt
, msg
)
201 register SVCXPRT
*xprt
;
204 register struct svcudp_data
*su
= su_data(xprt
);
205 register XDR
*xdrs
= &(su
->su_xdrs
);
211 xprt
->xp_addrlen
= sizeof(struct sockaddr_in
);
212 rlen
= recvfrom(xprt
->xp_sock
, rpc_buffer(xprt
), (int) su
->su_iosz
,
213 0, (struct sockaddr
*)&(xprt
->xp_raddr
), &(xprt
->xp_addrlen
));
214 if (rlen
== -1 && errno
== EINTR
)
216 if (rlen
< 4*sizeof(u_long
))
218 xdrs
->x_op
= XDR_DECODE
;
220 if (! xdr_callmsg(xdrs
, msg
))
222 su
->su_xid
= msg
->rm_xid
;
223 if (su
->su_cache
!= NULL
) {
224 if (cache_get(xprt
, msg
, &reply
, &replylen
)) {
225 (void) sendto(xprt
->xp_sock
, reply
, (int) replylen
, 0,
226 (struct sockaddr
*) &xprt
->xp_raddr
, xprt
->xp_addrlen
);
234 svcudp_reply(xprt
, msg
)
235 register SVCXPRT
*xprt
;
238 register struct svcudp_data
*su
= su_data(xprt
);
239 register XDR
*xdrs
= &(su
->su_xdrs
);
241 register bool_t stat
= FALSE
;
243 xdrs
->x_op
= XDR_ENCODE
;
245 msg
->rm_xid
= su
->su_xid
;
246 if (xdr_replymsg(xdrs
, msg
)) {
247 slen
= (int)XDR_GETPOS(xdrs
);
248 if (sendto(xprt
->xp_sock
, rpc_buffer(xprt
), slen
, 0,
249 (struct sockaddr
*)&(xprt
->xp_raddr
), xprt
->xp_addrlen
)
252 if (su
->su_cache
&& slen
>= 0) {
253 cache_set(xprt
, (u_long
) slen
);
261 svcudp_getargs(xprt
, xdr_args
, args_ptr
)
267 return ((*xdr_args
)(&(su_data(xprt
)->su_xdrs
), args_ptr
));
271 svcudp_freeargs(xprt
, xdr_args
, args_ptr
)
276 register XDR
*xdrs
= &(su_data(xprt
)->su_xdrs
);
278 xdrs
->x_op
= XDR_FREE
;
279 return ((*xdr_args
)(xdrs
, args_ptr
));
284 register SVCXPRT
*xprt
;
286 register struct svcudp_data
*su
= su_data(xprt
);
288 xprt_unregister(xprt
);
289 (void)close(xprt
->xp_sock
);
290 XDR_DESTROY(&(su
->su_xdrs
));
291 mem_free(rpc_buffer(xprt
), su
->su_iosz
);
292 mem_free((caddr_t
)su
, sizeof(struct svcudp_data
));
293 mem_free((caddr_t
)xprt
, sizeof(SVCXPRT
));
297 /***********this could be a separate file*********************/
300 * Fifo cache for udp server
301 * Copies pointers to reply buffers into fifo cache
302 * Buffers are sent again if retransmissions are detected.
305 #define SPARSENESS 4 /* 75% sparse */
307 #define CACHE_PERROR(msg) \
308 (void) fprintf(stderr,"%s\n", msg)
310 #define ALLOC(type, size) \
311 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
313 #define BZERO(addr, type, size) \
314 bzero((char *) addr, sizeof(type) * (int) (size))
317 * An entry in the cache
319 typedef struct cache_node
*cache_ptr
;
322 * Index into cache is xid, proc, vers, prog and address
328 struct sockaddr_in cache_addr
;
330 * The cached reply and length
333 u_long cache_replylen
;
335 * Next node on the list, if there is a collision
337 cache_ptr cache_next
;
346 u_long uc_size
; /* size of cache */
347 cache_ptr
*uc_entries
; /* hash table of entries in cache */
348 cache_ptr
*uc_fifo
; /* fifo list of entries in cache */
349 u_long uc_nextvictim
; /* points to next victim in fifo list */
350 u_long uc_prog
; /* saved program number */
351 u_long uc_vers
; /* saved version number */
352 u_long uc_proc
; /* saved procedure number */
353 struct sockaddr_in uc_addr
; /* saved caller's address */
358 * the hashing function
360 #define CACHE_LOC(transp, xid) \
361 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
365 * Enable use of the cache.
366 * Note: there is no disable.
369 svcudp_enablecache(transp
, size
)
373 struct svcudp_data
*su
= su_data(transp
);
374 struct udp_cache
*uc
;
376 if (su
->su_cache
!= NULL
) {
377 CACHE_PERROR("enablecache: cache already enabled");
380 uc
= ALLOC(struct udp_cache
, 1);
382 CACHE_PERROR("enablecache: could not allocate cache");
386 uc
->uc_nextvictim
= 0;
387 uc
->uc_entries
= ALLOC(cache_ptr
, size
* SPARSENESS
);
388 if (uc
->uc_entries
== NULL
) {
389 CACHE_PERROR("enablecache: could not allocate cache data");
392 BZERO(uc
->uc_entries
, cache_ptr
, size
* SPARSENESS
);
393 uc
->uc_fifo
= ALLOC(cache_ptr
, size
);
394 if (uc
->uc_fifo
== NULL
) {
395 CACHE_PERROR("enablecache: could not allocate cache fifo");
398 BZERO(uc
->uc_fifo
, cache_ptr
, size
);
399 su
->su_cache
= (char *) uc
;
405 * Set an entry in the cache
408 cache_set(xprt
, replylen
)
412 register cache_ptr victim
;
413 register cache_ptr
*vicp
;
414 register struct svcudp_data
*su
= su_data(xprt
);
415 struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
420 * Find space for the new entry, either by
421 * reusing an old entry, or by mallocing a new one
423 victim
= uc
->uc_fifo
[uc
->uc_nextvictim
];
424 if (victim
!= NULL
) {
425 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
426 for (vicp
= &uc
->uc_entries
[loc
];
427 *vicp
!= NULL
&& *vicp
!= victim
;
428 vicp
= &(*vicp
)->cache_next
)
431 CACHE_PERROR("cache_set: victim not found");
434 *vicp
= victim
->cache_next
; /* remote from cache */
435 newbuf
= victim
->cache_reply
;
437 victim
= ALLOC(struct cache_node
, 1);
438 if (victim
== NULL
) {
439 CACHE_PERROR("cache_set: victim alloc failed");
442 newbuf
= mem_alloc(su
->su_iosz
);
443 if (newbuf
== NULL
) {
444 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
452 victim
->cache_replylen
= replylen
;
453 victim
->cache_reply
= rpc_buffer(xprt
);
454 rpc_buffer(xprt
) = newbuf
;
455 xdrmem_create(&(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_ENCODE
);
456 victim
->cache_xid
= su
->su_xid
;
457 victim
->cache_proc
= uc
->uc_proc
;
458 victim
->cache_vers
= uc
->uc_vers
;
459 victim
->cache_prog
= uc
->uc_prog
;
460 victim
->cache_addr
= uc
->uc_addr
;
461 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
462 victim
->cache_next
= uc
->uc_entries
[loc
];
463 uc
->uc_entries
[loc
] = victim
;
464 uc
->uc_fifo
[uc
->uc_nextvictim
++] = victim
;
465 uc
->uc_nextvictim
%= uc
->uc_size
;
469 * Try to get an entry from the cache
470 * return 1 if found, 0 if not found
473 cache_get(xprt
, msg
, replyp
, replylenp
)
480 register cache_ptr ent
;
481 register struct svcudp_data
*su
= su_data(xprt
);
482 register struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
484 # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
486 loc
= CACHE_LOC(xprt
, su
->su_xid
);
487 for (ent
= uc
->uc_entries
[loc
]; ent
!= NULL
; ent
= ent
->cache_next
) {
488 if (ent
->cache_xid
== su
->su_xid
&&
489 ent
->cache_proc
== uc
->uc_proc
&&
490 ent
->cache_vers
== uc
->uc_vers
&&
491 ent
->cache_prog
== uc
->uc_prog
&&
492 EQADDR(ent
->cache_addr
, uc
->uc_addr
)) {
493 *replyp
= ent
->cache_reply
;
494 *replylenp
= ent
->cache_replylen
;
499 * Failed to find entry
500 * Remember a few things so we can do a set later
502 uc
->uc_proc
= msg
->rm_call
.cb_proc
;
503 uc
->uc_vers
= msg
->rm_call
.cb_vers
;
504 uc
->uc_prog
= msg
->rm_call
.cb_prog
;
505 uc
->uc_addr
= xprt
->xp_raddr
;