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
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$OpenBSD: svc_udp.c,v 1.8 1998/03/19 00:27:26 millert Exp $";
32 #endif /* LIBC_SCCS and not lint */
36 * Server side for UDP/IP based RPC. (Does some caching in the hopes of
37 * achieving execute-at-most-once semantics.)
39 * Copyright (C) 1984, Sun Microsystems, Inc.
46 #include <sys/socket.h>
51 #define rpc_buffer(xprt) ((xprt)->xp_p1)
53 #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 __P((SVCXPRT *, u_long));
63 static int cache_get __P((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(sock, sendsz, recvsz)
102 u_int sendsz, recvsz;
104 bool_t madesock = FALSE;
105 register SVCXPRT *xprt;
106 register struct svcudp_data *su;
107 struct sockaddr_in addr;
108 int len = sizeof(struct sockaddr_in);
110 if (sock == RPC_ANYSOCK) {
111 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
112 perror("svcudp_create: socket creation problem");
113 return ((SVCXPRT *)NULL);
117 memset(&addr, 0, sizeof (addr));
119 addr.sin_len = sizeof(struct sockaddr_in);
121 addr.sin_family = AF_INET;
122 if (bindresvport(sock, &addr)) {
124 (void)bind(sock, (struct sockaddr *)&addr, len);
126 if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
127 perror("svcudp_create - cannot getsockname");
130 return ((SVCXPRT *)NULL);
132 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
134 (void)fprintf(stderr, "svcudp_create: out of memory\n");
139 su = (struct svcudp_data *)mem_alloc(sizeof(*su));
141 (void)fprintf(stderr, "svcudp_create: out of memory\n");
147 su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
148 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
149 (void)fprintf(stderr, "svcudp_create: out of memory\n");
157 &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
159 xprt->xp_p2 = (caddr_t)su;
160 xprt->xp_auth = NULL;
161 xprt->xp_verf.oa_base = su->su_verfbody;
162 xprt->xp_ops = &svcudp_op;
163 xprt->xp_port = ntohs(addr.sin_port);
164 xprt->xp_sock = sock;
174 return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
178 static enum xprt_stat
187 svcudp_recv(xprt, msg)
188 register SVCXPRT *xprt;
191 register struct svcudp_data *su = su_data(xprt);
192 register XDR *xdrs = &(su->su_xdrs);
198 xprt->xp_addrlen = sizeof(struct sockaddr_in);
199 rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
200 0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
201 if (rlen == -1 && errno == EINTR)
203 if (rlen == -1 || rlen < 4*sizeof(u_int32_t))
205 xdrs->x_op = XDR_DECODE;
207 if (! xdr_callmsg(xdrs, msg))
209 su->su_xid = msg->rm_xid;
210 if (su->su_cache != NULL) {
211 if (cache_get(xprt, msg, &reply, &replylen)) {
212 (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
213 (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
221 svcudp_reply(xprt, msg)
222 register SVCXPRT *xprt;
225 register struct svcudp_data *su = su_data(xprt);
226 register XDR *xdrs = &(su->su_xdrs);
231 xdrs->x_op = XDR_ENCODE;
233 msg->rm_xid = su->su_xid;
235 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
236 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
237 xdr_proc = msg->acpted_rply.ar_results.proc;
238 xdr_where = msg->acpted_rply.ar_results.where;
239 msg->acpted_rply.ar_results.proc = xdr_void;
240 msg->acpted_rply.ar_results.where = NULL;
242 if (!xdr_replymsg(xdrs, msg) ||
243 !SVCAUTH_WRAP(xprt->xp_auth, xdrs, xdr_proc, xdr_where))
246 else if (!xdr_replymsg(xdrs, msg)) {
249 slen = (int)XDR_GETPOS(xdrs);
251 if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
252 (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
256 if (su->su_cache && slen >= 0)
257 cache_set(xprt, (u_long) slen);
263 svcudp_getargs(xprt, xdr_args, args_ptr)
268 return (SVCAUTH_UNWRAP(xprt->xp_auth, &(su_data(xprt)->su_xdrs),
269 xdr_args, args_ptr));
273 svcudp_freeargs(xprt, xdr_args, args_ptr)
278 register XDR *xdrs = &(su_data(xprt)->su_xdrs);
280 xdrs->x_op = XDR_FREE;
281 return ((*xdr_args)(xdrs, args_ptr));
286 register SVCXPRT *xprt;
288 register struct svcudp_data *su = su_data(xprt);
290 xprt_unregister(xprt);
291 if (xprt->xp_sock != -1)
292 (void)close(xprt->xp_sock);
294 if (xprt->xp_auth != NULL) {
295 SVCAUTH_DESTROY(xprt->xp_auth);
296 xprt->xp_auth = NULL;
298 XDR_DESTROY(&(su->su_xdrs));
299 mem_free(rpc_buffer(xprt), su->su_iosz);
300 mem_free((caddr_t)su, sizeof(struct svcudp_data));
301 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
305 /***********this could be a separate file*********************/
308 * Fifo cache for udp server
309 * Copies pointers to reply buffers into fifo cache
310 * Buffers are sent again if retransmissions are detected.
313 #define SPARSENESS 4 /* 75% sparse */
315 #define CACHE_PERROR(msg) \
316 (void) fprintf(stderr,"%s\n", msg)
318 #define ALLOC(type, size) \
319 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
321 #define BZERO(addr, type, size) \
322 memset((char *) addr, 0, sizeof(type) * (int) (size))
325 * An entry in the cache
327 typedef struct cache_node *cache_ptr;
330 * Index into cache is xid, proc, vers, prog and address
336 struct sockaddr_in cache_addr;
338 * The cached reply and length
341 u_long cache_replylen;
343 * Next node on the list, if there is a collision
345 cache_ptr cache_next;
354 u_long uc_size; /* size of cache */
355 cache_ptr *uc_entries; /* hash table of entries in cache */
356 cache_ptr *uc_fifo; /* fifo list of entries in cache */
357 u_long uc_nextvictim; /* points to next victim in fifo list */
358 u_long uc_prog; /* saved program number */
359 u_long uc_vers; /* saved version number */
360 u_long uc_proc; /* saved procedure number */
361 struct sockaddr_in uc_addr; /* saved caller's address */
366 * the hashing function
368 #define CACHE_LOC(transp, xid) \
369 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
373 * Enable use of the cache.
374 * Note: there is no disable.
377 svcudp_enablecache(transp, size)
381 struct svcudp_data *su = su_data(transp);
382 struct udp_cache *uc;
384 if (su->su_cache != NULL) {
385 CACHE_PERROR("enablecache: cache already enabled");
388 uc = ALLOC(struct udp_cache, 1);
390 CACHE_PERROR("enablecache: could not allocate cache");
394 uc->uc_nextvictim = 0;
395 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
396 if (uc->uc_entries == NULL) {
397 CACHE_PERROR("enablecache: could not allocate cache data");
400 BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
401 uc->uc_fifo = ALLOC(cache_ptr, size);
402 if (uc->uc_fifo == NULL) {
403 CACHE_PERROR("enablecache: could not allocate cache fifo");
406 BZERO(uc->uc_fifo, cache_ptr, size);
407 su->su_cache = (char *) uc;
413 * Set an entry in the cache
416 cache_set(xprt, replylen)
420 register cache_ptr victim;
421 register cache_ptr *vicp;
422 register struct svcudp_data *su = su_data(xprt);
423 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
428 * Find space for the new entry, either by
429 * reusing an old entry, or by mallocing a new one
431 victim = uc->uc_fifo[uc->uc_nextvictim];
432 if (victim != NULL) {
433 loc = CACHE_LOC(xprt, victim->cache_xid);
434 for (vicp = &uc->uc_entries[loc];
435 *vicp != NULL && *vicp != victim;
436 vicp = &(*vicp)->cache_next)
439 CACHE_PERROR("cache_set: victim not found");
442 *vicp = victim->cache_next; /* remote from cache */
443 newbuf = victim->cache_reply;
445 victim = ALLOC(struct cache_node, 1);
446 if (victim == NULL) {
447 CACHE_PERROR("cache_set: victim alloc failed");
450 newbuf = mem_alloc(su->su_iosz);
451 if (newbuf == NULL) {
452 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
460 victim->cache_replylen = replylen;
461 victim->cache_reply = rpc_buffer(xprt);
462 rpc_buffer(xprt) = newbuf;
463 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
464 victim->cache_xid = su->su_xid;
465 victim->cache_proc = uc->uc_proc;
466 victim->cache_vers = uc->uc_vers;
467 victim->cache_prog = uc->uc_prog;
468 victim->cache_addr = uc->uc_addr;
469 loc = CACHE_LOC(xprt, victim->cache_xid);
470 victim->cache_next = uc->uc_entries[loc];
471 uc->uc_entries[loc] = victim;
472 uc->uc_fifo[uc->uc_nextvictim++] = victim;
473 uc->uc_nextvictim %= uc->uc_size;
477 * Try to get an entry from the cache
478 * return 1 if found, 0 if not found
481 cache_get(xprt, msg, replyp, replylenp)
488 register cache_ptr ent;
489 register struct svcudp_data *su = su_data(xprt);
490 register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
492 # define EQADDR(a1, a2) (memcmp(&a1, &a2, sizeof(a1)) == 0)
494 loc = CACHE_LOC(xprt, su->su_xid);
495 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
496 if (ent->cache_xid == su->su_xid &&
497 ent->cache_proc == uc->uc_proc &&
498 ent->cache_vers == uc->uc_vers &&
499 ent->cache_prog == uc->uc_prog &&
500 EQADDR(ent->cache_addr, uc->uc_addr)) {
501 *replyp = ent->cache_reply;
502 *replylenp = ent->cache_replylen;
507 * Failed to find entry
508 * Remember a few things so we can do a set later
510 uc->uc_proc = msg->rm_call.cb_proc;
511 uc->uc_vers = msg->rm_call.cb_vers;
512 uc->uc_prog = msg->rm_call.cb_prog;
513 uc->uc_addr = xprt->xp_raddr;