2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro
30 * @(#)svc_udp.c 2.2 88/07/29 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/svc_udp.c,v 1.13 2000/01/27 23:06:41 jasone Exp $
32 * $DragonFly: src/lib/libc/rpc/svc_udp.c,v 1.6 2005/11/13 12:27:04 swildner Exp $
37 * Server side for UDP/IP based RPC. (Does some caching in the hopes of
38 * achieving execute-at-most-once semantics.)
40 * Copyright (C) 1984, Sun Microsystems, Inc.
43 #include "namespace.h"
49 #include <sys/socket.h>
51 #include "un-namespace.h"
53 #define rpc_buffer(xprt) ((xprt)->xp_p1)
54 #define MAX(a, b) ((a > b) ? a : b)
56 static bool_t
svcudp_recv();
57 static bool_t
svcudp_reply();
58 static enum xprt_stat
svcudp_stat();
59 static bool_t
svcudp_getargs();
60 static bool_t
svcudp_freeargs();
61 static void svcudp_destroy();
62 static void cache_set (SVCXPRT
*, u_long
);
63 static int cache_get (SVCXPRT
*, struct rpc_msg
*, char **, u_long
*);
65 static struct xp_ops svcudp_op
= {
78 u_int su_iosz
; /* byte size of send.recv buffer */
79 u_long su_xid
; /* transaction id */
80 XDR su_xdrs
; /* XDR handle */
81 char su_verfbody
[MAX_AUTH_BYTES
]; /* verifier body */
82 char * su_cache
; /* cached data, NULL if no cache */
84 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
88 * xprt = svcudp_create(sock);
90 * If sock<0 then a socket is created, else sock is used.
91 * If the socket, sock is not bound to a port then svcudp_create
92 * binds it to an arbitrary port. In any (successful) case,
93 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
94 * associated port number.
95 * Once *xprt is initialized, it is registered as a transporter;
96 * see (svc.h, xprt_register).
97 * The routines returns NULL if a problem occurred.
100 svcudp_bufcreate(int sock
, u_int sendsz
, u_int recvsz
)
102 bool_t madesock
= FALSE
;
104 struct svcudp_data
*su
;
105 struct sockaddr_in addr
;
106 int len
= sizeof(struct sockaddr_in
);
108 if (sock
== RPC_ANYSOCK
) {
109 if ((sock
= _socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0) {
110 perror("svcudp_create: socket creation problem");
111 return ((SVCXPRT
*)NULL
);
115 memset((char *)&addr
, 0, sizeof (addr
));
116 addr
.sin_len
= sizeof(struct sockaddr_in
);
117 addr
.sin_family
= AF_INET
;
118 if (bindresvport(sock
, &addr
)) {
120 _bind(sock
, (struct sockaddr
*)&addr
, len
);
122 if (_getsockname(sock
, (struct sockaddr
*)&addr
, &len
) != 0) {
123 perror("svcudp_create - cannot getsockname");
126 return ((SVCXPRT
*)NULL
);
128 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
130 fprintf(stderr
, "svcudp_create: out of memory\n");
133 su
= (struct svcudp_data
*)mem_alloc(sizeof(*su
));
135 fprintf(stderr
, "svcudp_create: out of memory\n");
138 su
->su_iosz
= ((MAX(sendsz
, recvsz
) + 3) / 4) * 4;
139 if ((rpc_buffer(xprt
) = mem_alloc(su
->su_iosz
)) == NULL
) {
140 fprintf(stderr
, "svcudp_create: out of memory\n");
144 &(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_DECODE
);
146 xprt
->xp_p2
= (caddr_t
)su
;
147 xprt
->xp_verf
.oa_base
= su
->su_verfbody
;
148 xprt
->xp_ops
= &svcudp_op
;
149 xprt
->xp_port
= ntohs(addr
.sin_port
);
150 xprt
->xp_sock
= sock
;
156 svcudp_create(int sock
)
159 return(svcudp_bufcreate(sock
, UDPMSGSIZE
, UDPMSGSIZE
));
162 static enum xprt_stat
163 svcudp_stat(SVCXPRT
*xprt
)
170 svcudp_recv(SVCXPRT
*xprt
, struct rpc_msg
*msg
)
172 struct svcudp_data
*su
= su_data(xprt
);
173 XDR
*xdrs
= &(su
->su_xdrs
);
179 xprt
->xp_addrlen
= sizeof(struct sockaddr_in
);
180 rlen
= _recvfrom(xprt
->xp_sock
, rpc_buffer(xprt
), (int) su
->su_iosz
,
181 0, (struct sockaddr
*)&(xprt
->xp_raddr
), &(xprt
->xp_addrlen
));
182 if (rlen
== -1 && errno
== EINTR
)
184 if (rlen
== -1 || rlen
< 4*sizeof(u_int32_t
))
186 xdrs
->x_op
= XDR_DECODE
;
188 if (! xdr_callmsg(xdrs
, msg
))
190 su
->su_xid
= msg
->rm_xid
;
191 if (su
->su_cache
!= NULL
) {
192 if (cache_get(xprt
, msg
, &reply
, &replylen
)) {
193 _sendto(xprt
->xp_sock
, reply
, (int) replylen
, 0,
194 (struct sockaddr
*) &xprt
->xp_raddr
, xprt
->xp_addrlen
);
202 svcudp_reply(SVCXPRT
*xprt
, struct rpc_msg
*msg
)
204 struct svcudp_data
*su
= su_data(xprt
);
205 XDR
*xdrs
= &(su
->su_xdrs
);
209 xdrs
->x_op
= XDR_ENCODE
;
211 msg
->rm_xid
= su
->su_xid
;
212 if (xdr_replymsg(xdrs
, msg
)) {
213 slen
= (int)XDR_GETPOS(xdrs
);
214 if (_sendto(xprt
->xp_sock
, rpc_buffer(xprt
), slen
, 0,
215 (struct sockaddr
*)&(xprt
->xp_raddr
), xprt
->xp_addrlen
)
218 if (su
->su_cache
&& slen
>= 0) {
219 cache_set(xprt
, (u_long
) slen
);
227 svcudp_getargs(SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
230 return ((*xdr_args
)(&(su_data(xprt
)->su_xdrs
), args_ptr
));
234 svcudp_freeargs(SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
236 XDR
*xdrs
= &(su_data(xprt
)->su_xdrs
);
238 xdrs
->x_op
= XDR_FREE
;
239 return ((*xdr_args
)(xdrs
, args_ptr
));
243 svcudp_destroy(SVCXPRT
*xprt
)
245 struct svcudp_data
*su
= su_data(xprt
);
247 xprt_unregister(xprt
);
248 _close(xprt
->xp_sock
);
249 XDR_DESTROY(&(su
->su_xdrs
));
250 mem_free(rpc_buffer(xprt
), su
->su_iosz
);
251 mem_free((caddr_t
)su
, sizeof(struct svcudp_data
));
252 mem_free((caddr_t
)xprt
, sizeof(SVCXPRT
));
256 /***********this could be a separate file*********************/
259 * Fifo cache for udp server
260 * Copies pointers to reply buffers into fifo cache
261 * Buffers are sent again if retransmissions are detected.
264 #define SPARSENESS 4 /* 75% sparse */
266 #define CACHE_PERROR(msg) \
267 fprintf(stderr,"%s\n", msg)
269 #define ALLOC(type, size) \
270 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
272 #define BZERO(addr, type, size) \
273 memset((char *) addr, 0, sizeof(type) * (int) (size))
276 * An entry in the cache
278 typedef struct cache_node
*cache_ptr
;
281 * Index into cache is xid, proc, vers, prog and address
287 struct sockaddr_in cache_addr
;
289 * The cached reply and length
292 u_long cache_replylen
;
294 * Next node on the list, if there is a collision
296 cache_ptr cache_next
;
305 u_long uc_size
; /* size of cache */
306 cache_ptr
*uc_entries
; /* hash table of entries in cache */
307 cache_ptr
*uc_fifo
; /* fifo list of entries in cache */
308 u_long uc_nextvictim
; /* points to next victim in fifo list */
309 u_long uc_prog
; /* saved program number */
310 u_long uc_vers
; /* saved version number */
311 u_long uc_proc
; /* saved procedure number */
312 struct sockaddr_in uc_addr
; /* saved caller's address */
317 * the hashing function
319 #define CACHE_LOC(transp, xid) \
320 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
324 * Enable use of the cache.
325 * Note: there is no disable.
328 svcudp_enablecache(SVCXPRT
*transp
, u_long size
)
330 struct svcudp_data
*su
= su_data(transp
);
331 struct udp_cache
*uc
;
333 if (su
->su_cache
!= NULL
) {
334 CACHE_PERROR("enablecache: cache already enabled");
337 uc
= ALLOC(struct udp_cache
, 1);
339 CACHE_PERROR("enablecache: could not allocate cache");
343 uc
->uc_nextvictim
= 0;
344 uc
->uc_entries
= ALLOC(cache_ptr
, size
* SPARSENESS
);
345 if (uc
->uc_entries
== NULL
) {
346 CACHE_PERROR("enablecache: could not allocate cache data");
349 BZERO(uc
->uc_entries
, cache_ptr
, size
* SPARSENESS
);
350 uc
->uc_fifo
= ALLOC(cache_ptr
, size
);
351 if (uc
->uc_fifo
== NULL
) {
352 CACHE_PERROR("enablecache: could not allocate cache fifo");
355 BZERO(uc
->uc_fifo
, cache_ptr
, size
);
356 su
->su_cache
= (char *) uc
;
362 * Set an entry in the cache
365 cache_set(SVCXPRT
*xprt
, u_long replylen
)
369 struct svcudp_data
*su
= su_data(xprt
);
370 struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
375 * Find space for the new entry, either by
376 * reusing an old entry, or by mallocing a new one
378 victim
= uc
->uc_fifo
[uc
->uc_nextvictim
];
379 if (victim
!= NULL
) {
380 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
381 for (vicp
= &uc
->uc_entries
[loc
];
382 *vicp
!= NULL
&& *vicp
!= victim
;
383 vicp
= &(*vicp
)->cache_next
)
386 CACHE_PERROR("cache_set: victim not found");
389 *vicp
= victim
->cache_next
; /* remote from cache */
390 newbuf
= victim
->cache_reply
;
392 victim
= ALLOC(struct cache_node
, 1);
393 if (victim
== NULL
) {
394 CACHE_PERROR("cache_set: victim alloc failed");
397 newbuf
= mem_alloc(su
->su_iosz
);
398 if (newbuf
== NULL
) {
399 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
407 victim
->cache_replylen
= replylen
;
408 victim
->cache_reply
= rpc_buffer(xprt
);
409 rpc_buffer(xprt
) = newbuf
;
410 xdrmem_create(&(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
, XDR_ENCODE
);
411 victim
->cache_xid
= su
->su_xid
;
412 victim
->cache_proc
= uc
->uc_proc
;
413 victim
->cache_vers
= uc
->uc_vers
;
414 victim
->cache_prog
= uc
->uc_prog
;
415 victim
->cache_addr
= uc
->uc_addr
;
416 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
417 victim
->cache_next
= uc
->uc_entries
[loc
];
418 uc
->uc_entries
[loc
] = victim
;
419 uc
->uc_fifo
[uc
->uc_nextvictim
++] = victim
;
420 uc
->uc_nextvictim
%= uc
->uc_size
;
424 * Try to get an entry from the cache
425 * return 1 if found, 0 if not found
428 cache_get(SVCXPRT
*xprt
, struct rpc_msg
*msg
, char **replyp
, u_long
*replylenp
)
432 struct svcudp_data
*su
= su_data(xprt
);
433 struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
435 # define EQADDR(a1, a2) (memcmp(&a1, &a2, sizeof(a1)) == 0)
437 loc
= CACHE_LOC(xprt
, su
->su_xid
);
438 for (ent
= uc
->uc_entries
[loc
]; ent
!= NULL
; ent
= ent
->cache_next
) {
439 if (ent
->cache_xid
== su
->su_xid
&&
440 ent
->cache_proc
== uc
->uc_proc
&&
441 ent
->cache_vers
== uc
->uc_vers
&&
442 ent
->cache_prog
== uc
->uc_prog
&&
443 EQADDR(ent
->cache_addr
, uc
->uc_addr
)) {
444 *replyp
= ent
->cache_reply
;
445 *replylenp
= ent
->cache_replylen
;
450 * Failed to find entry
451 * Remember a few things so we can do a set later
453 uc
->uc_proc
= msg
->rm_call
.cb_proc
;
454 uc
->uc_vers
= msg
->rm_call
.cb_vers
;
455 uc
->uc_prog
= msg
->rm_call
.cb_prog
;
456 uc
->uc_addr
= xprt
->xp_raddr
;