Merge up from branch.
[shibboleth/cpp-sp.git] / oncrpc / svc_udp.c
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.
6  *
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  *********************************************************************/
11
12 /* @(#)svc_udp.c        2.2 88/07/29 4.0 RPCSRC */
13 /*
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.
20  *
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.
24  *
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.
28  *
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.
32  *
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.
36  *
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
43 #endif
44
45 /*
46  * svc_udp.c,
47  * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
48  * achieving execute-at-most-once semantics.)
49  *
50  * Copyright (C) 1984, Sun Microsystems, Inc.
51  */
52
53 #include <stdio.h>
54 #include <rpc/rpc.h>
55 #ifndef WIN32
56 #include <sys/socket.h>
57 #endif
58 #include <errno.h>
59
60
61 #define rpc_buffer(xprt) ((xprt)->xp_p1)
62 #define MAX(a, b)     ((a > b) ? a : b)
63
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();
70
71 static                  cache_get();
72 static                  cache_set();
73
74
75 static struct xp_ops svcudp_op = {
76         svcudp_recv,
77         svcudp_stat,
78         svcudp_getargs,
79         svcudp_reply,
80         svcudp_freeargs,
81         svcudp_destroy
82 };
83
84 #ifndef WIN32
85 extern int errno;
86 #endif
87
88 /*
89  * kept in xprt->xp_p2
90  */
91 struct svcudp_data {
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 */
97 };
98 #define su_data(xprt)   ((struct svcudp_data *)(xprt->xp_p2))
99
100 /*
101  * Usage:
102  *      xprt = svcudp_create(sock);
103  *
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.
112  */
113 SVCXPRT *
114 svcudp_bufcreate(sock, sendsz, recvsz)
115         register int sock;
116         u_int sendsz, recvsz;
117 {
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);
123
124         if (sock == RPC_ANYSOCK) {
125 #ifdef WIN32
126                 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
127 #else
128                 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
129 #endif
130                         perror("svcudp_create: socket creation problem");
131                         return ((SVCXPRT *)NULL);
132                 }
133                 madesock = TRUE;
134         }
135         bzero((char *)&addr, sizeof (addr));
136         addr.sin_family = AF_INET;
137         if (bindresvport(sock, &addr)) {
138                 addr.sin_port = 0;
139                 (void)bind(sock, (struct sockaddr *)&addr, len);
140         }
141         if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
142                 perror("svcudp_create - cannot getsockname");
143                 if (madesock)
144 #ifdef WIN32
145                         (void)closesocket(sock);
146 #else
147                         (void)close(sock);
148 #endif
149                 return ((SVCXPRT *)NULL);
150         }
151         xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
152         if (xprt == NULL) {
153 #ifdef WIN32
154                 nt_rpc_report("svcudp_create: out of memory\n");
155 #else
156                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
157 #endif
158                 return (NULL);
159         }
160         su = (struct svcudp_data *)mem_alloc(sizeof(*su));
161         if (su == NULL) {
162 #ifdef WIN32
163                 nt_rpc_report("svcudp_create: out of memory\n");
164 #else
165                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
166 #endif
167                 return (NULL);
168         }
169         su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
170         if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
171 #ifdef WIN32
172                 nt_rpc_report("svcudp_create: out of memory\n");
173 #else
174                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
175 #endif
176                 return (NULL);
177         }
178         xdrmem_create(
179             &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
180         su->su_cache = NULL;
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;
186         xprt_register(xprt);
187         return (xprt);
188 }
189
190 SVCXPRT *
191 svcudp_create(sock)
192         int sock;
193 {
194
195         return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
196 }
197
198 static enum xprt_stat
199 svcudp_stat(xprt)
200         SVCXPRT *xprt;
201 {
202
203         return (XPRT_IDLE);
204 }
205
206 static bool_t
207 svcudp_recv(xprt, msg)
208         register SVCXPRT *xprt;
209         struct rpc_msg *msg;
210 {
211         register struct svcudp_data *su = su_data(xprt);
212         register XDR *xdrs = &(su->su_xdrs);
213         register int rlen;
214         char *reply;
215         u_long replylen;
216
217     again:
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));
221 #ifdef WIN32
222         if (rlen == -1 && WSAerrno == WSAEINTR)
223 #else
224         if (rlen == -1 && errno == EINTR)
225 #endif
226                 goto again;
227         if (rlen < 4*sizeof(u_long))
228                 return (FALSE);
229         xdrs->x_op = XDR_DECODE;
230         XDR_SETPOS(xdrs, 0);
231         if (! xdr_callmsg(xdrs, msg))
232                 return (FALSE);
233         su->su_xid = msg->rm_xid;
234         if (su->su_cache != NULL) {
235                 if (cache_get(xprt, msg, &reply, &replylen)) {
236 #ifdef WIN32
237                           sendto(xprt->xp_sock, reply, (int) replylen, 0,
238 #else
239                         (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
240 #endif
241                           (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
242                         return (TRUE);
243                 }
244         }
245         return (TRUE);
246 }
247
248 static bool_t
249 svcudp_reply(xprt, msg)
250         register SVCXPRT *xprt;
251         struct rpc_msg *msg;
252 {
253         register struct svcudp_data *su = su_data(xprt);
254         register XDR *xdrs = &(su->su_xdrs);
255         register int slen;
256         register bool_t stat = FALSE;
257
258         xdrs->x_op = XDR_ENCODE;
259         XDR_SETPOS(xdrs, 0);
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)
265                     == slen) {
266                         stat = TRUE;
267                         if (su->su_cache && slen >= 0) {
268                                 cache_set(xprt, (u_long) slen);
269                         }
270                 }
271         }
272         return (stat);
273 }
274
275 static bool_t
276 svcudp_getargs(xprt, xdr_args, args_ptr)
277         SVCXPRT *xprt;
278         xdrproc_t xdr_args;
279         caddr_t args_ptr;
280 {
281
282         return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
283 }
284
285 static bool_t
286 svcudp_freeargs(xprt, xdr_args, args_ptr)
287         SVCXPRT *xprt;
288         xdrproc_t xdr_args;
289         caddr_t args_ptr;
290 {
291         register XDR *xdrs = &(su_data(xprt)->su_xdrs);
292
293         xdrs->x_op = XDR_FREE;
294         return ((*xdr_args)(xdrs, args_ptr));
295 }
296
297 static void
298 svcudp_destroy(xprt)
299         register SVCXPRT *xprt;
300 {
301         register struct svcudp_data *su = su_data(xprt);
302
303         xprt_unregister(xprt);
304 #ifdef WIN32
305         (void)closesocket(xprt->xp_sock);
306 #else
307         (void)close(xprt->xp_sock);
308 #endif
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));
313 }
314
315
316 /***********this could be a separate file*********************/
317
318 /*
319  * Fifo cache for udp server
320  * Copies pointers to reply buffers into fifo cache
321  * Buffers are sent again if retransmissions are detected.
322  */
323
324 #define SPARSENESS 4    /* 75% sparse */
325
326 #ifdef WIN32
327 #define CACHE_PERROR(msg)       \
328         nt_rpc_report(msg)
329 #else
330 #define CACHE_PERROR(msg)       (void) fprintf(stderr,"%s\n", msg)
331 #endif
332
333 #define ALLOC(type, size)       (type *) mem_alloc((unsigned)(sizeof(type)*(size)))
334
335 #define BZERO(addr, type, size) bzero((char *) addr, sizeof(type) * (int) (size))
336
337 /*
338  * An entry in the cache
339  */
340 typedef struct cache_node *cache_ptr;
341 struct cache_node {
342         /*
343          * Index into cache is xid, proc, vers, prog and address
344          */
345         u_long cache_xid;
346         u_long cache_proc;
347         u_long cache_vers;
348         u_long cache_prog;
349         struct sockaddr_in cache_addr;
350         /*
351          * The cached reply and length
352          */
353         char * cache_reply;
354         u_long cache_replylen;
355         /*
356          * Next node on the list, if there is a collision
357          */
358         cache_ptr cache_next;
359 };
360
361
362 /*
363  * The entire cache
364  */
365 struct udp_cache {
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 */
374 };
375
376
377 /*
378  * the hashing function
379  */
380 #define CACHE_LOC(transp, xid)  (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
381
382 /*
383  * Enable use of the cache.
384  * Note: there is no disable.
385  */
386 svcudp_enablecache(transp, size)
387         SVCXPRT *transp;
388         u_long size;
389 {
390         struct svcudp_data *su = su_data(transp);
391         struct udp_cache *uc;
392
393         if (su->su_cache != NULL) {
394                 CACHE_PERROR("enablecache: cache already enabled");
395                 return(0);
396         }
397         uc = ALLOC(struct udp_cache, 1);
398         if (uc == NULL) {
399                 CACHE_PERROR("enablecache: could not allocate cache");
400                 return(0);
401         }
402         uc->uc_size = size;
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");
407                 return(0);
408         }
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");
413                 return(0);
414         }
415         BZERO(uc->uc_fifo, cache_ptr, size);
416         su->su_cache = (char *) uc;
417         return(1);
418 }
419
420
421 /*
422  * Set an entry in the cache
423  */
424 static
425 cache_set(xprt, replylen)
426         SVCXPRT *xprt;
427         u_long replylen;
428 {
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;
433         u_int loc;
434         char *newbuf;
435
436         /*
437          * Find space for the new entry, either by
438          * reusing an old entry, or by mallocing a new one
439          */
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)
446                                 ;
447                 if (*vicp == NULL) {
448                         CACHE_PERROR("cache_set: victim not found");
449                         return;
450                 }
451                 *vicp = victim->cache_next;     /* remote from cache */
452                 newbuf = victim->cache_reply;
453         } else {
454                 victim = ALLOC(struct cache_node, 1);
455                 if (victim == NULL) {
456                         CACHE_PERROR("cache_set: victim alloc failed");
457                         return;
458                 }
459                 newbuf = mem_alloc(su->su_iosz);
460                 if (newbuf == NULL) {
461                         CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
462                         return;
463                 }
464         }
465
466         /*
467          * Store it away
468          */
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;
483 }
484
485 /*
486  * Try to get an entry from the cache
487  * return 1 if found, 0 if not found
488  */
489 static
490 cache_get(xprt, msg, replyp, replylenp)
491         SVCXPRT *xprt;
492         struct rpc_msg *msg;
493         char **replyp;
494         u_long *replylenp;
495 {
496         u_int loc;
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;
500
501 #       define EQADDR(a1, a2)   (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
502
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;
512                         return(1);
513                 }
514         }
515         /*
516          * Failed to find entry
517          * Remember a few things so we can do a set later
518          */
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;
523         return(0);
524 }