]> git.decadent.org.uk Git - nfs-utils.git/blob - support/rpc/svc_udp.c
remove some files.
[nfs-utils.git] / support / rpc / svc_udp.c
1 /*
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.
8  *
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.
12  *
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.
16  *
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.
20  *
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.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29
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 */
33
34 /*
35  * svc_udp.c,
36  * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
37  * achieving execute-at-most-once semantics.)
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <sys/socket.h>
47 #include <errno.h>
48 #include <unistd.h>
49
50
51 #define rpc_buffer(xprt) ((xprt)->xp_p1)
52 #ifndef MAX
53 #define MAX(a, b)     ((a > b) ? a : b)
54 #endif
55
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 *));
64
65 static struct xp_ops svcudp_op = {
66         svcudp_recv,
67         svcudp_stat,
68         svcudp_getargs,
69         svcudp_reply,
70         svcudp_freeargs,
71         svcudp_destroy
72 };
73
74 /*
75  * kept in xprt->xp_p2
76  */
77 struct svcudp_data {
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 */
83 };
84 #define su_data(xprt)   ((struct svcudp_data *)(xprt->xp_p2))
85
86 /*
87  * Usage:
88  *      xprt = svcudp_create(sock);
89  *
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.
98  */
99 SVCXPRT *
100 svcudp_bufcreate(sock, sendsz, recvsz)
101         register int sock;
102         u_int sendsz, recvsz;
103 {
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);
109
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);
114                 }
115                 madesock = TRUE;
116         }
117         memset(&addr, 0, sizeof (addr));
118 #ifndef __linux__
119         addr.sin_len = sizeof(struct sockaddr_in);
120 #endif
121         addr.sin_family = AF_INET;
122         if (bindresvport(sock, &addr)) {
123                 addr.sin_port = 0;
124                 (void)bind(sock, (struct sockaddr *)&addr, len);
125         }
126         if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
127                 perror("svcudp_create - cannot getsockname");
128                 if (madesock)
129                         (void)close(sock);
130                 return ((SVCXPRT *)NULL);
131         }
132         xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
133         if (xprt == NULL) {
134                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
135                 if (madesock)
136                         (void)close(sock);
137                 return (NULL);
138         }
139         su = (struct svcudp_data *)mem_alloc(sizeof(*su));
140         if (su == NULL) {
141                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
142                 if (madesock)
143                         (void)close(sock);
144                 free(xprt);
145                 return (NULL);
146         }
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");
150                 if (madesock)
151                         (void)close(sock);
152                 free(xprt);
153                 free(su);
154                 return (NULL);
155         }
156         xdrmem_create(
157             &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
158         su->su_cache = NULL;
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;
165         xprt_register(xprt);
166         return (xprt);
167 }
168
169 SVCXPRT *
170 svcudp_create(sock)
171         int sock;
172 {
173
174         return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
175 }
176
177 /* ARGSUSED */
178 static enum xprt_stat
179 svcudp_stat(xprt)
180         SVCXPRT *xprt;
181 {
182
183         return (XPRT_IDLE);
184 }
185
186 static bool_t
187 svcudp_recv(xprt, msg)
188         register SVCXPRT *xprt;
189         struct rpc_msg *msg;
190 {
191         register struct svcudp_data *su = su_data(xprt);
192         register XDR *xdrs = &(su->su_xdrs);
193         register int rlen;
194         char *reply;
195         u_long replylen;
196
197     again:
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)
202                 goto again;
203         if (rlen == -1 || rlen < 4*sizeof(u_int32_t))
204                 return (FALSE);
205         xdrs->x_op = XDR_DECODE;
206         XDR_SETPOS(xdrs, 0);
207         if (! xdr_callmsg(xdrs, msg))
208                 return (FALSE);
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);
214                         return (TRUE);
215                 }
216         }
217         return (TRUE);
218 }
219
220 static bool_t
221 svcudp_reply(xprt, msg)
222         register SVCXPRT *xprt;
223         struct rpc_msg *msg;
224 {
225         register struct svcudp_data *su = su_data(xprt);
226         register XDR *xdrs = &(su->su_xdrs);
227         register int slen;
228         xdrproc_t xdr_proc;
229         caddr_t xdr_where;
230
231         xdrs->x_op = XDR_ENCODE;
232         XDR_SETPOS(xdrs, 0);
233         msg->rm_xid = su->su_xid;
234
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;
241
242                 if (!xdr_replymsg(xdrs, msg) ||
243                     !SVCAUTH_WRAP(xprt->xp_auth, xdrs, xdr_proc, xdr_where))
244                         return (FALSE);
245         }
246         else if (!xdr_replymsg(xdrs, msg)) {
247                 return (FALSE);
248         }
249         slen = (int)XDR_GETPOS(xdrs);
250
251         if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
252                    (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
253             != slen) {
254                 return (FALSE);
255         }
256         if (su->su_cache && slen >= 0)
257                 cache_set(xprt, (u_long) slen);
258
259         return (TRUE);
260 }
261
262 static bool_t
263 svcudp_getargs(xprt, xdr_args, args_ptr)
264         SVCXPRT *xprt;
265         xdrproc_t xdr_args;
266         caddr_t args_ptr;
267 {
268         return (SVCAUTH_UNWRAP(xprt->xp_auth, &(su_data(xprt)->su_xdrs),
269                                xdr_args, args_ptr));
270 }
271
272 static bool_t
273 svcudp_freeargs(xprt, xdr_args, args_ptr)
274         SVCXPRT *xprt;
275         xdrproc_t xdr_args;
276         caddr_t args_ptr;
277 {
278         register XDR *xdrs = &(su_data(xprt)->su_xdrs);
279
280         xdrs->x_op = XDR_FREE;
281         return ((*xdr_args)(xdrs, args_ptr));
282 }
283
284 static void
285 svcudp_destroy(xprt)
286         register SVCXPRT *xprt;
287 {
288         register struct svcudp_data *su = su_data(xprt);
289
290         xprt_unregister(xprt);
291         if (xprt->xp_sock != -1)
292                 (void)close(xprt->xp_sock);
293         xprt->xp_sock = -1;
294         if (xprt->xp_auth != NULL) {
295                 SVCAUTH_DESTROY(xprt->xp_auth);
296                 xprt->xp_auth = NULL;
297         }
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));
302 }
303
304
305 /***********this could be a separate file*********************/
306
307 /*
308  * Fifo cache for udp server
309  * Copies pointers to reply buffers into fifo cache
310  * Buffers are sent again if retransmissions are detected.
311  */
312
313 #define SPARSENESS 4    /* 75% sparse */
314
315 #define CACHE_PERROR(msg)       \
316         (void) fprintf(stderr,"%s\n", msg)
317
318 #define ALLOC(type, size)       \
319         (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
320
321 #define BZERO(addr, type, size)  \
322         memset((char *) addr, 0, sizeof(type) * (int) (size))
323
324 /*
325  * An entry in the cache
326  */
327 typedef struct cache_node *cache_ptr;
328 struct cache_node {
329         /*
330          * Index into cache is xid, proc, vers, prog and address
331          */
332         u_long cache_xid;
333         u_long cache_proc;
334         u_long cache_vers;
335         u_long cache_prog;
336         struct sockaddr_in cache_addr;
337         /*
338          * The cached reply and length
339          */
340         char * cache_reply;
341         u_long cache_replylen;
342         /*
343          * Next node on the list, if there is a collision
344          */
345         cache_ptr cache_next;
346 };
347
348
349
350 /*
351  * The entire cache
352  */
353 struct udp_cache {
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 */
362 };
363
364
365 /*
366  * the hashing function
367  */
368 #define CACHE_LOC(transp, xid)  \
369  (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
370
371
372 /*
373  * Enable use of the cache.
374  * Note: there is no disable.
375  */
376 int
377 svcudp_enablecache(transp, size)
378         SVCXPRT *transp;
379         u_long size;
380 {
381         struct svcudp_data *su = su_data(transp);
382         struct udp_cache *uc;
383
384         if (su->su_cache != NULL) {
385                 CACHE_PERROR("enablecache: cache already enabled");
386                 return(0);
387         }
388         uc = ALLOC(struct udp_cache, 1);
389         if (uc == NULL) {
390                 CACHE_PERROR("enablecache: could not allocate cache");
391                 return(0);
392         }
393         uc->uc_size = size;
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");
398                 return(0);
399         }
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");
404                 return(0);
405         }
406         BZERO(uc->uc_fifo, cache_ptr, size);
407         su->su_cache = (char *) uc;
408         return(1);
409 }
410
411
412 /*
413  * Set an entry in the cache
414  */
415 static void
416 cache_set(xprt, replylen)
417         SVCXPRT *xprt;
418         u_long replylen;
419 {
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;
424         u_int loc;
425         char *newbuf;
426
427         /*
428          * Find space for the new entry, either by
429          * reusing an old entry, or by mallocing a new one
430          */
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)
437                                 ;
438                 if (*vicp == NULL) {
439                         CACHE_PERROR("cache_set: victim not found");
440                         return;
441                 }
442                 *vicp = victim->cache_next;     /* remote from cache */
443                 newbuf = victim->cache_reply;
444         } else {
445                 victim = ALLOC(struct cache_node, 1);
446                 if (victim == NULL) {
447                         CACHE_PERROR("cache_set: victim alloc failed");
448                         return;
449                 }
450                 newbuf = mem_alloc(su->su_iosz);
451                 if (newbuf == NULL) {
452                         CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
453                         return;
454                 }
455         }
456
457         /*
458          * Store it away
459          */
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;
474 }
475
476 /*
477  * Try to get an entry from the cache
478  * return 1 if found, 0 if not found
479  */
480 static int
481 cache_get(xprt, msg, replyp, replylenp)
482         SVCXPRT *xprt;
483         struct rpc_msg *msg;
484         char **replyp;
485         u_long *replylenp;
486 {
487         u_int loc;
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;
491
492 #       define EQADDR(a1, a2)   (memcmp(&a1, &a2, sizeof(a1)) == 0)
493
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;
503                         return(1);
504                 }
505         }
506         /*
507          * Failed to find entry
508          * Remember a few things so we can do a set later
509          */
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;
514         return(0);
515 }
516