1 /*********************************************************************
2 * RPC for the Windows NT Operating System
3 * 1993 by Martin F. Gergeleit
4 * Users may use, copy or modify Sun RPC for the Windows NT Operating
5 * System according to the Sun copyright below.
7 * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO
8 * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE
9 * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10 *********************************************************************/
12 /* @(#)svc_udp.c 2.2 88/07/29 4.0 RPCSRC */
14 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15 * unrestricted use provided that this legend is included on all tape
16 * media and as a part of the software program in whole or part. Users
17 * may copy or modify Sun RPC without charge, but are not authorized
18 * to license or distribute it to anyone else except as part of a product or
19 * program developed by the user.
21 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
25 * Sun RPC is provided with no support and without any obligation on the
26 * part of Sun Microsystems, Inc. to assist in its use, correction,
27 * modification or enhancement.
29 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31 * OR ANY PART THEREOF.
33 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34 * or profits or other special, indirect and consequential damages, even if
35 * Sun has been advised of the possibility of such damages.
37 * Sun Microsystems, Inc.
39 * Mountain View, California 94043
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
47 * Server side for UDP/IP based RPC. (Does some caching in the hopes of
48 * achieving execute-at-most-once semantics.)
50 * Copyright (C) 1984, Sun Microsystems, Inc.
56 #include <sys/socket.h>
61 #define rpc_buffer(xprt) ((xprt)->xp_p1)
62 #define MAX(a, b) ((a > b) ? a : b)
64 static bool_t svcudp_recv();
65 static bool_t svcudp_reply();
66 static enum xprt_stat svcudp_stat();
67 static bool_t svcudp_getargs();
68 static bool_t svcudp_freeargs();
69 static void svcudp_destroy();
75 static struct xp_ops svcudp_op = {
92 u_int su_iosz; /* byte size of send.recv buffer */
93 u_long su_xid; /* transaction id */
94 XDR su_xdrs; /* XDR handle */
95 char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */
96 char * su_cache; /* cached data, NULL if no cache */
98 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
102 * xprt = svcudp_create(sock);
104 * If sock<0 then a socket is created, else sock is used.
105 * If the socket, sock is not bound to a port then svcudp_create
106 * binds it to an arbitrary port. In any (successful) case,
107 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
108 * associated port number.
109 * Once *xprt is initialized, it is registered as a transporter;
110 * see (svc.h, xprt_register).
111 * The routines returns NULL if a problem occurred.
114 svcudp_bufcreate(sock, sendsz, recvsz)
116 u_int sendsz, recvsz;
118 bool_t madesock = FALSE;
119 register SVCXPRT *xprt;
120 register struct svcudp_data *su;
121 struct sockaddr_in addr;
122 int len = sizeof(struct sockaddr_in);
124 if (sock == RPC_ANYSOCK) {
126 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
128 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
130 perror("svcudp_create: socket creation problem");
131 return ((SVCXPRT *)NULL);
135 bzero((char *)&addr, sizeof (addr));
136 addr.sin_family = AF_INET;
137 if (bindresvport(sock, &addr)) {
139 (void)bind(sock, (struct sockaddr *)&addr, len);
141 if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
142 perror("svcudp_create - cannot getsockname");
145 (void)closesocket(sock);
149 return ((SVCXPRT *)NULL);
151 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
154 nt_rpc_report("svcudp_create: out of memory\n");
156 (void)fprintf(stderr, "svcudp_create: out of memory\n");
160 su = (struct svcudp_data *)mem_alloc(sizeof(*su));
163 nt_rpc_report("svcudp_create: out of memory\n");
165 (void)fprintf(stderr, "svcudp_create: out of memory\n");
169 su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
170 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
172 nt_rpc_report("svcudp_create: out of memory\n");
174 (void)fprintf(stderr, "svcudp_create: out of memory\n");
179 &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
181 xprt->xp_p2 = (caddr_t)su;
182 xprt->xp_verf.oa_base = su->su_verfbody;
183 xprt->xp_ops = &svcudp_op;
184 xprt->xp_port = ntohs(addr.sin_port);
185 xprt->xp_sock = sock;
195 return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
198 static enum xprt_stat
207 svcudp_recv(xprt, msg)
208 register SVCXPRT *xprt;
211 register struct svcudp_data *su = su_data(xprt);
212 register XDR *xdrs = &(su->su_xdrs);
218 xprt->xp_addrlen = sizeof(struct sockaddr_in);
219 rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
220 0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
222 if (rlen == -1 && WSAerrno == WSAEINTR)
224 if (rlen == -1 && errno == EINTR)
227 if (rlen < 4*sizeof(u_long))
229 xdrs->x_op = XDR_DECODE;
231 if (! xdr_callmsg(xdrs, msg))
233 su->su_xid = msg->rm_xid;
234 if (su->su_cache != NULL) {
235 if (cache_get(xprt, msg, &reply, &replylen)) {
237 sendto(xprt->xp_sock, reply, (int) replylen, 0,
239 (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
241 (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
249 svcudp_reply(xprt, msg)
250 register SVCXPRT *xprt;
253 register struct svcudp_data *su = su_data(xprt);
254 register XDR *xdrs = &(su->su_xdrs);
256 register bool_t stat = FALSE;
258 xdrs->x_op = XDR_ENCODE;
260 msg->rm_xid = su->su_xid;
261 if (xdr_replymsg(xdrs, msg)) {
262 slen = (int)XDR_GETPOS(xdrs);
263 if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
264 (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
267 if (su->su_cache && slen >= 0) {
268 cache_set(xprt, (u_long) slen);
276 svcudp_getargs(xprt, xdr_args, args_ptr)
282 return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
286 svcudp_freeargs(xprt, xdr_args, args_ptr)
291 register XDR *xdrs = &(su_data(xprt)->su_xdrs);
293 xdrs->x_op = XDR_FREE;
294 return ((*xdr_args)(xdrs, args_ptr));
299 register SVCXPRT *xprt;
301 register struct svcudp_data *su = su_data(xprt);
303 xprt_unregister(xprt);
305 (void)closesocket(xprt->xp_sock);
307 (void)close(xprt->xp_sock);
309 XDR_DESTROY(&(su->su_xdrs));
310 mem_free(rpc_buffer(xprt), su->su_iosz);
311 mem_free((caddr_t)su, sizeof(struct svcudp_data));
312 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
316 /***********this could be a separate file*********************/
319 * Fifo cache for udp server
320 * Copies pointers to reply buffers into fifo cache
321 * Buffers are sent again if retransmissions are detected.
324 #define SPARSENESS 4 /* 75% sparse */
327 #define CACHE_PERROR(msg) \
330 #define CACHE_PERROR(msg) (void) fprintf(stderr,"%s\n", msg)
333 #define ALLOC(type, size) (type *) mem_alloc((unsigned)(sizeof(type)*(size)))
335 #define BZERO(addr, type, size) bzero((char *) addr, sizeof(type) * (int) (size))
338 * An entry in the cache
340 typedef struct cache_node *cache_ptr;
343 * Index into cache is xid, proc, vers, prog and address
349 struct sockaddr_in cache_addr;
351 * The cached reply and length
354 u_long cache_replylen;
356 * Next node on the list, if there is a collision
358 cache_ptr cache_next;
366 u_long uc_size; /* size of cache */
367 cache_ptr *uc_entries; /* hash table of entries in cache */
368 cache_ptr *uc_fifo; /* fifo list of entries in cache */
369 u_long uc_nextvictim; /* points to next victim in fifo list */
370 u_long uc_prog; /* saved program number */
371 u_long uc_vers; /* saved version number */
372 u_long uc_proc; /* saved procedure number */
373 struct sockaddr_in uc_addr; /* saved caller's address */
378 * the hashing function
380 #define CACHE_LOC(transp, xid) (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
383 * Enable use of the cache.
384 * Note: there is no disable.
386 svcudp_enablecache(transp, size)
390 struct svcudp_data *su = su_data(transp);
391 struct udp_cache *uc;
393 if (su->su_cache != NULL) {
394 CACHE_PERROR("enablecache: cache already enabled");
397 uc = ALLOC(struct udp_cache, 1);
399 CACHE_PERROR("enablecache: could not allocate cache");
403 uc->uc_nextvictim = 0;
404 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
405 if (uc->uc_entries == NULL) {
406 CACHE_PERROR("enablecache: could not allocate cache data");
409 BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
410 uc->uc_fifo = ALLOC(cache_ptr, size);
411 if (uc->uc_fifo == NULL) {
412 CACHE_PERROR("enablecache: could not allocate cache fifo");
415 BZERO(uc->uc_fifo, cache_ptr, size);
416 su->su_cache = (char *) uc;
422 * Set an entry in the cache
425 cache_set(xprt, replylen)
429 register cache_ptr victim;
430 register cache_ptr *vicp;
431 register struct svcudp_data *su = su_data(xprt);
432 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
437 * Find space for the new entry, either by
438 * reusing an old entry, or by mallocing a new one
440 victim = uc->uc_fifo[uc->uc_nextvictim];
441 if (victim != NULL) {
442 loc = CACHE_LOC(xprt, victim->cache_xid);
443 for (vicp = &uc->uc_entries[loc];
444 *vicp != NULL && *vicp != victim;
445 vicp = &(*vicp)->cache_next)
448 CACHE_PERROR("cache_set: victim not found");
451 *vicp = victim->cache_next; /* remote from cache */
452 newbuf = victim->cache_reply;
454 victim = ALLOC(struct cache_node, 1);
455 if (victim == NULL) {
456 CACHE_PERROR("cache_set: victim alloc failed");
459 newbuf = mem_alloc(su->su_iosz);
460 if (newbuf == NULL) {
461 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
469 victim->cache_replylen = replylen;
470 victim->cache_reply = rpc_buffer(xprt);
471 rpc_buffer(xprt) = newbuf;
472 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
473 victim->cache_xid = su->su_xid;
474 victim->cache_proc = uc->uc_proc;
475 victim->cache_vers = uc->uc_vers;
476 victim->cache_prog = uc->uc_prog;
477 victim->cache_addr = uc->uc_addr;
478 loc = CACHE_LOC(xprt, victim->cache_xid);
479 victim->cache_next = uc->uc_entries[loc];
480 uc->uc_entries[loc] = victim;
481 uc->uc_fifo[uc->uc_nextvictim++] = victim;
482 uc->uc_nextvictim %= uc->uc_size;
486 * Try to get an entry from the cache
487 * return 1 if found, 0 if not found
490 cache_get(xprt, msg, replyp, replylenp)
497 register cache_ptr ent;
498 register struct svcudp_data *su = su_data(xprt);
499 register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
501 # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
503 loc = CACHE_LOC(xprt, su->su_xid);
504 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
505 if (ent->cache_xid == su->su_xid &&
506 ent->cache_proc == uc->uc_proc &&
507 ent->cache_vers == uc->uc_vers &&
508 ent->cache_prog == uc->uc_prog &&
509 EQADDR(ent->cache_addr, uc->uc_addr)) {
510 *replyp = ent->cache_reply;
511 *replylenp = ent->cache_replylen;
516 * Failed to find entry
517 * Remember a few things so we can do a set later
519 uc->uc_proc = msg->rm_call.cb_proc;
520 uc->uc_vers = msg->rm_call.cb_vers;
521 uc->uc_prog = msg->rm_call.cb_prog;
522 uc->uc_addr = xprt->xp_raddr;