]> git.ipfire.org Git - thirdparty/glibc.git/blob - sunrpc/svc_udp.c
update from main archive
[thirdparty/glibc.git] / sunrpc / svc_udp.c
1 /* @(#)svc_udp.c 2.2 88/07/29 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
9 *
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 *
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
17 *
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
21 *
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
32 #endif
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 <rpc/rpc.h>
44 #include <sys/socket.h>
45 #include <errno.h>
46
47
48 #define rpc_buffer(xprt) ((xprt)->xp_p1)
49 #define MAX(a, b) ((a > b) ? a : b)
50
51 static bool_t svcudp_recv();
52 static bool_t svcudp_reply();
53 static enum xprt_stat svcudp_stat();
54 static bool_t svcudp_getargs();
55 static bool_t svcudp_freeargs();
56 static void svcudp_destroy();
57
58 static struct xp_ops svcudp_op = {
59 svcudp_recv,
60 svcudp_stat,
61 svcudp_getargs,
62 svcudp_reply,
63 svcudp_freeargs,
64 svcudp_destroy
65 };
66
67 #ifndef errno
68 extern int errno;
69 #endif
70
71 /*
72 * kept in xprt->xp_p2
73 */
74 struct svcudp_data {
75 u_int su_iosz; /* byte size of send.recv buffer */
76 u_long su_xid; /* transaction id */
77 XDR su_xdrs; /* XDR handle */
78 char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */
79 char * su_cache; /* cached data, NULL if no cache */
80 };
81 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
82
83 /*
84 * Usage:
85 * xprt = svcudp_create(sock);
86 *
87 * If sock<0 then a socket is created, else sock is used.
88 * If the socket, sock is not bound to a port then svcudp_create
89 * binds it to an arbitrary port. In any (successful) case,
90 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
91 * associated port number.
92 * Once *xprt is initialized, it is registered as a transporter;
93 * see (svc.h, xprt_register).
94 * The routines returns NULL if a problem occurred.
95 */
96 SVCXPRT *
97 svcudp_bufcreate(sock, sendsz, recvsz)
98 register int sock;
99 u_int sendsz, recvsz;
100 {
101 bool_t madesock = FALSE;
102 register SVCXPRT *xprt;
103 register struct svcudp_data *su;
104 struct sockaddr_in addr;
105 int len = sizeof(struct sockaddr_in);
106
107 if (sock == RPC_ANYSOCK) {
108 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
109 perror(_("svcudp_create: socket creation problem"));
110 return ((SVCXPRT *)NULL);
111 }
112 madesock = TRUE;
113 }
114 bzero((char *)&addr, sizeof (addr));
115 addr.sin_family = AF_INET;
116 if (bindresvport(sock, &addr)) {
117 addr.sin_port = 0;
118 (void)bind(sock, (struct sockaddr *)&addr, len);
119 }
120 if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
121 perror(_("svcudp_create - cannot getsockname"));
122 if (madesock)
123 (void)close(sock);
124 return ((SVCXPRT *)NULL);
125 }
126 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
127 if (xprt == NULL) {
128 (void)fprintf(stderr, "svcudp_create: out of memory\n");
129 return (NULL);
130 }
131 su = (struct svcudp_data *)mem_alloc(sizeof(*su));
132 if (su == NULL) {
133 (void)fprintf(stderr, "svcudp_create: out of memory\n");
134 return (NULL);
135 }
136 su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
137 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
138 (void)fprintf(stderr, "svcudp_create: out of memory\n");
139 return (NULL);
140 }
141 xdrmem_create(
142 &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
143 su->su_cache = NULL;
144 xprt->xp_p2 = (caddr_t)su;
145 xprt->xp_verf.oa_base = su->su_verfbody;
146 xprt->xp_ops = &svcudp_op;
147 xprt->xp_port = ntohs(addr.sin_port);
148 xprt->xp_sock = sock;
149 xprt_register(xprt);
150 return (xprt);
151 }
152
153 SVCXPRT *
154 svcudp_create(sock)
155 int sock;
156 {
157
158 return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
159 }
160
161 static enum xprt_stat
162 svcudp_stat(xprt)
163 SVCXPRT *xprt;
164 {
165
166 return (XPRT_IDLE);
167 }
168
169 static bool_t
170 svcudp_recv(xprt, msg)
171 register SVCXPRT *xprt;
172 struct rpc_msg *msg;
173 {
174 register struct svcudp_data *su = su_data(xprt);
175 register XDR *xdrs = &(su->su_xdrs);
176 register int rlen;
177 char *reply;
178 u_long replylen;
179
180 again:
181 xprt->xp_addrlen = sizeof(struct sockaddr_in);
182 rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
183 0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
184 if (rlen == -1 && errno == EINTR)
185 goto again;
186 if (rlen < 16) /* < 4 32-bit ints? */
187 return (FALSE);
188 xdrs->x_op = XDR_DECODE;
189 XDR_SETPOS(xdrs, 0);
190 if (! xdr_callmsg(xdrs, msg))
191 return (FALSE);
192 su->su_xid = msg->rm_xid;
193 if (su->su_cache != NULL) {
194 if (cache_get(xprt, msg, &reply, &replylen)) {
195 (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
196 (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
197 return (TRUE);
198 }
199 }
200 return (TRUE);
201 }
202
203 static bool_t
204 svcudp_reply(xprt, msg)
205 register SVCXPRT *xprt;
206 struct rpc_msg *msg;
207 {
208 register struct svcudp_data *su = su_data(xprt);
209 register XDR *xdrs = &(su->su_xdrs);
210 register int slen;
211 register bool_t stat = FALSE;
212
213 xdrs->x_op = XDR_ENCODE;
214 XDR_SETPOS(xdrs, 0);
215 msg->rm_xid = su->su_xid;
216 if (xdr_replymsg(xdrs, msg)) {
217 slen = (int)XDR_GETPOS(xdrs);
218 if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
219 (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
220 == slen) {
221 stat = TRUE;
222 if (su->su_cache && slen >= 0) {
223 cache_set(xprt, (u_long) slen);
224 }
225 }
226 }
227 return (stat);
228 }
229
230 static bool_t
231 svcudp_getargs(xprt, xdr_args, args_ptr)
232 SVCXPRT *xprt;
233 xdrproc_t xdr_args;
234 caddr_t args_ptr;
235 {
236
237 return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
238 }
239
240 static bool_t
241 svcudp_freeargs(xprt, xdr_args, args_ptr)
242 SVCXPRT *xprt;
243 xdrproc_t xdr_args;
244 caddr_t args_ptr;
245 {
246 register XDR *xdrs = &(su_data(xprt)->su_xdrs);
247
248 xdrs->x_op = XDR_FREE;
249 return ((*xdr_args)(xdrs, args_ptr));
250 }
251
252 static void
253 svcudp_destroy(xprt)
254 register SVCXPRT *xprt;
255 {
256 register struct svcudp_data *su = su_data(xprt);
257
258 xprt_unregister(xprt);
259 (void)close(xprt->xp_sock);
260 XDR_DESTROY(&(su->su_xdrs));
261 mem_free(rpc_buffer(xprt), su->su_iosz);
262 mem_free((caddr_t)su, sizeof(struct svcudp_data));
263 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
264 }
265
266
267 /***********this could be a separate file*********************/
268
269 /*
270 * Fifo cache for udp server
271 * Copies pointers to reply buffers into fifo cache
272 * Buffers are sent again if retransmissions are detected.
273 */
274
275 #define SPARSENESS 4 /* 75% sparse */
276
277 #define CACHE_PERROR(msg) \
278 (void) fprintf(stderr,"%s\n", msg)
279
280 #define ALLOC(type, size) \
281 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
282
283 #define BZERO(addr, type, size) \
284 bzero((char *) addr, sizeof(type) * (int) (size))
285
286 /*
287 * An entry in the cache
288 */
289 typedef struct cache_node *cache_ptr;
290 struct cache_node {
291 /*
292 * Index into cache is xid, proc, vers, prog and address
293 */
294 u_long cache_xid;
295 u_long cache_proc;
296 u_long cache_vers;
297 u_long cache_prog;
298 struct sockaddr_in cache_addr;
299 /*
300 * The cached reply and length
301 */
302 char * cache_reply;
303 u_long cache_replylen;
304 /*
305 * Next node on the list, if there is a collision
306 */
307 cache_ptr cache_next;
308 };
309
310
311
312 /*
313 * The entire cache
314 */
315 struct udp_cache {
316 u_long uc_size; /* size of cache */
317 cache_ptr *uc_entries; /* hash table of entries in cache */
318 cache_ptr *uc_fifo; /* fifo list of entries in cache */
319 u_long uc_nextvictim; /* points to next victim in fifo list */
320 u_long uc_prog; /* saved program number */
321 u_long uc_vers; /* saved version number */
322 u_long uc_proc; /* saved procedure number */
323 struct sockaddr_in uc_addr; /* saved caller's address */
324 };
325
326
327 /*
328 * the hashing function
329 */
330 #define CACHE_LOC(transp, xid) \
331 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
332
333
334 /*
335 * Enable use of the cache.
336 * Note: there is no disable.
337 */
338 svcudp_enablecache(transp, size)
339 SVCXPRT *transp;
340 u_long size;
341 {
342 struct svcudp_data *su = su_data(transp);
343 struct udp_cache *uc;
344
345 if (su->su_cache != NULL) {
346 CACHE_PERROR(_("enablecache: cache already enabled"));
347 return(0);
348 }
349 uc = ALLOC(struct udp_cache, 1);
350 if (uc == NULL) {
351 CACHE_PERROR(_("enablecache: could not allocate cache"));
352 return(0);
353 }
354 uc->uc_size = size;
355 uc->uc_nextvictim = 0;
356 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
357 if (uc->uc_entries == NULL) {
358 CACHE_PERROR(_("enablecache: could not allocate cache data"));
359 return(0);
360 }
361 BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
362 uc->uc_fifo = ALLOC(cache_ptr, size);
363 if (uc->uc_fifo == NULL) {
364 CACHE_PERROR(_("enablecache: could not allocate cache fifo"));
365 return(0);
366 }
367 BZERO(uc->uc_fifo, cache_ptr, size);
368 su->su_cache = (char *) uc;
369 return(1);
370 }
371
372
373 /*
374 * Set an entry in the cache
375 */
376 static
377 cache_set(xprt, replylen)
378 SVCXPRT *xprt;
379 u_long replylen;
380 {
381 register cache_ptr victim;
382 register cache_ptr *vicp;
383 register struct svcudp_data *su = su_data(xprt);
384 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
385 u_int loc;
386 char *newbuf;
387
388 /*
389 * Find space for the new entry, either by
390 * reusing an old entry, or by mallocing a new one
391 */
392 victim = uc->uc_fifo[uc->uc_nextvictim];
393 if (victim != NULL) {
394 loc = CACHE_LOC(xprt, victim->cache_xid);
395 for (vicp = &uc->uc_entries[loc];
396 *vicp != NULL && *vicp != victim;
397 vicp = &(*vicp)->cache_next)
398 ;
399 if (*vicp == NULL) {
400 CACHE_PERROR(_("cache_set: victim not found"));
401 return;
402 }
403 *vicp = victim->cache_next; /* remote from cache */
404 newbuf = victim->cache_reply;
405 } else {
406 victim = ALLOC(struct cache_node, 1);
407 if (victim == NULL) {
408 CACHE_PERROR("cache_set: victim alloc failed");
409 return;
410 }
411 newbuf = mem_alloc(su->su_iosz);
412 if (newbuf == NULL) {
413 CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
414 return;
415 }
416 }
417
418 /*
419 * Store it away
420 */
421 victim->cache_replylen = replylen;
422 victim->cache_reply = rpc_buffer(xprt);
423 rpc_buffer(xprt) = newbuf;
424 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
425 victim->cache_xid = su->su_xid;
426 victim->cache_proc = uc->uc_proc;
427 victim->cache_vers = uc->uc_vers;
428 victim->cache_prog = uc->uc_prog;
429 victim->cache_addr = uc->uc_addr;
430 loc = CACHE_LOC(xprt, victim->cache_xid);
431 victim->cache_next = uc->uc_entries[loc];
432 uc->uc_entries[loc] = victim;
433 uc->uc_fifo[uc->uc_nextvictim++] = victim;
434 uc->uc_nextvictim %= uc->uc_size;
435 }
436
437 /*
438 * Try to get an entry from the cache
439 * return 1 if found, 0 if not found
440 */
441 static
442 cache_get(xprt, msg, replyp, replylenp)
443 SVCXPRT *xprt;
444 struct rpc_msg *msg;
445 char **replyp;
446 u_long *replylenp;
447 {
448 u_int loc;
449 register cache_ptr ent;
450 register struct svcudp_data *su = su_data(xprt);
451 register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
452
453 # define EQADDR(a1, a2) (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
454
455 loc = CACHE_LOC(xprt, su->su_xid);
456 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
457 if (ent->cache_xid == su->su_xid &&
458 ent->cache_proc == uc->uc_proc &&
459 ent->cache_vers == uc->uc_vers &&
460 ent->cache_prog == uc->uc_prog &&
461 EQADDR(ent->cache_addr, uc->uc_addr)) {
462 *replyp = ent->cache_reply;
463 *replylenp = ent->cache_replylen;
464 return(1);
465 }
466 }
467 /*
468 * Failed to find entry
469 * Remember a few things so we can do a set later
470 */
471 uc->uc_proc = msg->rm_call.cb_proc;
472 uc->uc_vers = msg->rm_call.cb_vers;
473 uc->uc_prog = msg->rm_call.cb_prog;
474 uc->uc_addr = xprt->xp_raddr;
475 return(0);
476 }