Bug fixes for ____longjmp_chk on sparc.
[glibc.git] / sunrpc / svc_udp.c
blob71be4f8221ed8a8c6f2d89fd0e14250951166800
1 /*
2 * svc_udp.c,
3 * Server side for UDP/IP based RPC. (Does some caching in the hopes of
4 * achieving execute-at-most-once semantics.)
6 * Copyright (c) 2010, Oracle America, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 * * Neither the name of the "Oracle America, Inc." nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <rpc/rpc.h>
40 #include <sys/socket.h>
41 #include <errno.h>
42 #include <libintl.h>
44 #ifdef IP_PKTINFO
45 #include <sys/uio.h>
46 #endif
48 #ifdef USE_IN_LIBIO
49 # include <wchar.h>
50 # include <libio/iolibio.h>
51 #endif
53 #define rpc_buffer(xprt) ((xprt)->xp_p1)
54 #ifndef MAX
55 #define MAX(a, b) ((a > b) ? a : b)
56 #endif
58 static bool_t svcudp_recv (SVCXPRT *, struct rpc_msg *);
59 static bool_t svcudp_reply (SVCXPRT *, struct rpc_msg *);
60 static enum xprt_stat svcudp_stat (SVCXPRT *);
61 static bool_t svcudp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
62 static bool_t svcudp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
63 static void svcudp_destroy (SVCXPRT *);
65 static const struct xp_ops svcudp_op =
67 svcudp_recv,
68 svcudp_stat,
69 svcudp_getargs,
70 svcudp_reply,
71 svcudp_freeargs,
72 svcudp_destroy
75 static int cache_get (SVCXPRT *, struct rpc_msg *, char **replyp,
76 u_long *replylenp);
77 static void cache_set (SVCXPRT *xprt, u_long replylen);
80 * kept in xprt->xp_p2
82 struct svcudp_data
84 u_int su_iosz; /* byte size of send.recv buffer */
85 u_long su_xid; /* transaction id */
86 XDR su_xdrs; /* XDR handle */
87 char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */
88 char *su_cache; /* cached data, NULL if no cache */
90 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
93 * Usage:
94 * xprt = svcudp_create(sock);
96 * If sock<0 then a socket is created, else sock is used.
97 * If the socket, sock is not bound to a port then svcudp_create
98 * binds it to an arbitrary port. In any (successful) case,
99 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
100 * associated port number.
101 * Once *xprt is initialized, it is registered as a transporter;
102 * see (svc.h, xprt_register).
103 * The routines returns NULL if a problem occurred.
105 SVCXPRT *
106 svcudp_bufcreate (sock, sendsz, recvsz)
107 int sock;
108 u_int sendsz, recvsz;
110 bool_t madesock = FALSE;
111 SVCXPRT *xprt;
112 struct svcudp_data *su;
113 struct sockaddr_in addr;
114 socklen_t len = sizeof (struct sockaddr_in);
115 int pad;
116 void *buf;
118 if (sock == RPC_ANYSOCK)
120 if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
122 perror (_("svcudp_create: socket creation problem"));
123 return (SVCXPRT *) NULL;
125 madesock = TRUE;
127 __bzero ((char *) &addr, sizeof (addr));
128 addr.sin_family = AF_INET;
129 if (bindresvport (sock, &addr))
131 addr.sin_port = 0;
132 (void) __bind (sock, (struct sockaddr *) &addr, len);
134 if (__getsockname (sock, (struct sockaddr *) &addr, &len) != 0)
136 perror (_("svcudp_create - cannot getsockname"));
137 if (madesock)
138 (void) __close (sock);
139 return (SVCXPRT *) NULL;
141 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
142 su = (struct svcudp_data *) mem_alloc (sizeof (*su));
143 buf = mem_alloc (((MAX (sendsz, recvsz) + 3) / 4) * 4);
144 if (xprt == NULL || su == NULL || buf == NULL)
146 (void) __fxprintf (NULL, "%s: %s",
147 "svcudp_create", _("out of memory\n"));
148 mem_free (xprt, sizeof (SVCXPRT));
149 mem_free (su, sizeof (*su));
150 mem_free (buf, ((MAX (sendsz, recvsz) + 3) / 4) * 4);
151 return NULL;
153 su->su_iosz = ((MAX (sendsz, recvsz) + 3) / 4) * 4;
154 rpc_buffer (xprt) = buf;
155 xdrmem_create (&(su->su_xdrs), rpc_buffer (xprt), su->su_iosz, XDR_DECODE);
156 su->su_cache = NULL;
157 xprt->xp_p2 = (caddr_t) su;
158 xprt->xp_verf.oa_base = su->su_verfbody;
159 xprt->xp_ops = &svcudp_op;
160 xprt->xp_port = ntohs (addr.sin_port);
161 xprt->xp_sock = sock;
163 #ifdef IP_PKTINFO
164 if ((sizeof (struct iovec) + sizeof (struct msghdr)
165 + sizeof(struct cmsghdr) + sizeof (struct in_pktinfo))
166 > sizeof (xprt->xp_pad))
168 (void) __fxprintf (NULL,"%s", _("\
169 svcudp_create: xp_pad is too small for IP_PKTINFO\n"));
170 return NULL;
172 pad = 1;
173 if (__setsockopt (sock, SOL_IP, IP_PKTINFO, (void *) &pad,
174 sizeof (pad)) == 0)
175 /* Set the padding to all 1s. */
176 pad = 0xff;
177 else
178 #endif
179 /* Clear the padding. */
180 pad = 0;
181 memset (&xprt->xp_pad [0], pad, sizeof (xprt->xp_pad));
183 xprt_register (xprt);
184 return xprt;
186 #ifdef EXPORT_RPC_SYMBOLS
187 libc_hidden_def (svcudp_bufcreate)
188 #else
189 libc_hidden_nolink (svcudp_bufcreate, GLIBC_2_0)
190 #endif
192 SVCXPRT *
193 svcudp_create (sock)
194 int sock;
196 return svcudp_bufcreate (sock, UDPMSGSIZE, UDPMSGSIZE);
198 #ifdef EXPORT_RPC_SYMBOLS
199 libc_hidden_def (svcudp_create)
200 #else
201 libc_hidden_nolink (svcudp_create, GLIBC_2_0)
202 #endif
204 static enum xprt_stat
205 svcudp_stat (xprt)
206 SVCXPRT *xprt;
209 return XPRT_IDLE;
212 static bool_t
213 svcudp_recv (xprt, msg)
214 SVCXPRT *xprt;
215 struct rpc_msg *msg;
217 struct svcudp_data *su = su_data (xprt);
218 XDR *xdrs = &(su->su_xdrs);
219 int rlen;
220 char *reply;
221 u_long replylen;
222 socklen_t len;
224 /* It is very tricky when you have IP aliases. We want to make sure
225 that we are sending the packet from the IP address where the
226 incoming packet is addressed to. H.J. */
227 #ifdef IP_PKTINFO
228 struct iovec *iovp;
229 struct msghdr *mesgp;
230 #endif
232 again:
233 /* FIXME -- should xp_addrlen be a size_t? */
234 len = (socklen_t) sizeof(struct sockaddr_in);
235 #ifdef IP_PKTINFO
236 iovp = (struct iovec *) &xprt->xp_pad [0];
237 mesgp = (struct msghdr *) &xprt->xp_pad [sizeof (struct iovec)];
238 if (mesgp->msg_iovlen)
240 iovp->iov_base = rpc_buffer (xprt);
241 iovp->iov_len = su->su_iosz;
242 mesgp->msg_iov = iovp;
243 mesgp->msg_iovlen = 1;
244 mesgp->msg_name = &(xprt->xp_raddr);
245 mesgp->msg_namelen = len;
246 mesgp->msg_control = &xprt->xp_pad [sizeof (struct iovec)
247 + sizeof (struct msghdr)];
248 mesgp->msg_controllen = sizeof(xprt->xp_pad)
249 - sizeof (struct iovec) - sizeof (struct msghdr);
250 rlen = __recvmsg (xprt->xp_sock, mesgp, 0);
251 if (rlen >= 0)
253 struct cmsghdr *cmsg;
254 len = mesgp->msg_namelen;
255 cmsg = CMSG_FIRSTHDR (mesgp);
256 if (cmsg == NULL
257 || CMSG_NXTHDR (mesgp, cmsg) != NULL
258 || cmsg->cmsg_level != SOL_IP
259 || cmsg->cmsg_type != IP_PKTINFO
260 || cmsg->cmsg_len < (sizeof (struct cmsghdr)
261 + sizeof (struct in_pktinfo)))
263 /* Not a simple IP_PKTINFO, ignore it. */
264 mesgp->msg_control = NULL;
265 mesgp->msg_controllen = 0;
267 else
269 /* It was a simple IP_PKTIFO as we expected, discard the
270 interface field. */
271 struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
272 pkti->ipi_ifindex = 0;
276 else
277 #endif
278 rlen = __recvfrom (xprt->xp_sock, rpc_buffer (xprt),
279 (int) su->su_iosz, 0,
280 (struct sockaddr *) &(xprt->xp_raddr), &len);
281 xprt->xp_addrlen = len;
282 if (rlen == -1 && errno == EINTR)
283 goto again;
284 if (rlen < 16) /* < 4 32-bit ints? */
285 return FALSE;
286 xdrs->x_op = XDR_DECODE;
287 XDR_SETPOS (xdrs, 0);
288 if (!xdr_callmsg (xdrs, msg))
289 return FALSE;
290 su->su_xid = msg->rm_xid;
291 if (su->su_cache != NULL)
293 if (cache_get (xprt, msg, &reply, &replylen))
295 #ifdef IP_PKTINFO
296 if (mesgp->msg_iovlen)
298 iovp->iov_base = reply;
299 iovp->iov_len = replylen;
300 (void) __sendmsg (xprt->xp_sock, mesgp, 0);
302 else
303 #endif
304 (void) __sendto (xprt->xp_sock, reply, (int) replylen, 0,
305 (struct sockaddr *) &xprt->xp_raddr, len);
306 return TRUE;
309 return TRUE;
312 static bool_t
313 svcudp_reply (xprt, msg)
314 SVCXPRT *xprt;
315 struct rpc_msg *msg;
317 struct svcudp_data *su = su_data (xprt);
318 XDR *xdrs = &(su->su_xdrs);
319 int slen, sent;
320 bool_t stat = FALSE;
321 #ifdef IP_PKTINFO
322 struct iovec *iovp;
323 struct msghdr *mesgp;
324 #endif
326 xdrs->x_op = XDR_ENCODE;
327 XDR_SETPOS (xdrs, 0);
328 msg->rm_xid = su->su_xid;
329 if (xdr_replymsg (xdrs, msg))
331 slen = (int) XDR_GETPOS (xdrs);
332 #ifdef IP_PKTINFO
333 mesgp = (struct msghdr *) &xprt->xp_pad [sizeof (struct iovec)];
334 if (mesgp->msg_iovlen)
336 iovp = (struct iovec *) &xprt->xp_pad [0];
337 iovp->iov_base = rpc_buffer (xprt);
338 iovp->iov_len = slen;
339 sent = __sendmsg (xprt->xp_sock, mesgp, 0);
341 else
342 #endif
343 sent = __sendto (xprt->xp_sock, rpc_buffer (xprt), slen, 0,
344 (struct sockaddr *) &(xprt->xp_raddr),
345 xprt->xp_addrlen);
346 if (sent == slen)
348 stat = TRUE;
349 if (su->su_cache && slen >= 0)
351 cache_set (xprt, (u_long) slen);
355 return stat;
358 static bool_t
359 svcudp_getargs (xprt, xdr_args, args_ptr)
360 SVCXPRT *xprt;
361 xdrproc_t xdr_args;
362 caddr_t args_ptr;
365 return (*xdr_args) (&(su_data (xprt)->su_xdrs), args_ptr);
368 static bool_t
369 svcudp_freeargs (xprt, xdr_args, args_ptr)
370 SVCXPRT *xprt;
371 xdrproc_t xdr_args;
372 caddr_t args_ptr;
374 XDR *xdrs = &(su_data (xprt)->su_xdrs);
376 xdrs->x_op = XDR_FREE;
377 return (*xdr_args) (xdrs, args_ptr);
380 static void
381 svcudp_destroy (xprt)
382 SVCXPRT *xprt;
384 struct svcudp_data *su = su_data (xprt);
386 xprt_unregister (xprt);
387 (void) __close (xprt->xp_sock);
388 XDR_DESTROY (&(su->su_xdrs));
389 mem_free (rpc_buffer (xprt), su->su_iosz);
390 mem_free ((caddr_t) su, sizeof (struct svcudp_data));
391 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
395 /***********this could be a separate file*********************/
398 * Fifo cache for udp server
399 * Copies pointers to reply buffers into fifo cache
400 * Buffers are sent again if retransmissions are detected.
403 #define SPARSENESS 4 /* 75% sparse */
405 #define CACHE_PERROR(msg) \
406 (void) __fxprintf(NULL, "%s\n", msg)
408 #define ALLOC(type, size) \
409 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
411 #define CALLOC(type, size) \
412 (type *) calloc (sizeof (type), size)
415 * An entry in the cache
417 typedef struct cache_node *cache_ptr;
418 struct cache_node
421 * Index into cache is xid, proc, vers, prog and address
423 u_long cache_xid;
424 u_long cache_proc;
425 u_long cache_vers;
426 u_long cache_prog;
427 struct sockaddr_in cache_addr;
429 * The cached reply and length
431 char *cache_reply;
432 u_long cache_replylen;
434 * Next node on the list, if there is a collision
436 cache_ptr cache_next;
442 * The entire cache
444 struct udp_cache
446 u_long uc_size; /* size of cache */
447 cache_ptr *uc_entries; /* hash table of entries in cache */
448 cache_ptr *uc_fifo; /* fifo list of entries in cache */
449 u_long uc_nextvictim; /* points to next victim in fifo list */
450 u_long uc_prog; /* saved program number */
451 u_long uc_vers; /* saved version number */
452 u_long uc_proc; /* saved procedure number */
453 struct sockaddr_in uc_addr; /* saved caller's address */
458 * the hashing function
460 #define CACHE_LOC(transp, xid) \
461 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
465 * Enable use of the cache.
466 * Note: there is no disable.
469 svcudp_enablecache (SVCXPRT *transp, u_long size)
471 struct svcudp_data *su = su_data (transp);
472 struct udp_cache *uc;
474 if (su->su_cache != NULL)
476 CACHE_PERROR (_("enablecache: cache already enabled"));
477 return 0;
479 uc = ALLOC (struct udp_cache, 1);
480 if (uc == NULL)
482 CACHE_PERROR (_("enablecache: could not allocate cache"));
483 return 0;
485 uc->uc_size = size;
486 uc->uc_nextvictim = 0;
487 uc->uc_entries = CALLOC (cache_ptr, size * SPARSENESS);
488 if (uc->uc_entries == NULL)
490 mem_free (uc, sizeof (struct udp_cache));
491 CACHE_PERROR (_("enablecache: could not allocate cache data"));
492 return 0;
494 uc->uc_fifo = CALLOC (cache_ptr, size);
495 if (uc->uc_fifo == NULL)
497 mem_free (uc->uc_entries, size * SPARSENESS);
498 mem_free (uc, sizeof (struct udp_cache));
499 CACHE_PERROR (_("enablecache: could not allocate cache fifo"));
500 return 0;
502 su->su_cache = (char *) uc;
503 return 1;
505 libc_hidden_nolink (svcudp_enablecache, GLIBC_2_0)
509 * Set an entry in the cache
511 static void
512 cache_set (SVCXPRT *xprt, u_long replylen)
514 cache_ptr victim;
515 cache_ptr *vicp;
516 struct svcudp_data *su = su_data (xprt);
517 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
518 u_int loc;
519 char *newbuf;
522 * Find space for the new entry, either by
523 * reusing an old entry, or by mallocing a new one
525 victim = uc->uc_fifo[uc->uc_nextvictim];
526 if (victim != NULL)
528 loc = CACHE_LOC (xprt, victim->cache_xid);
529 for (vicp = &uc->uc_entries[loc];
530 *vicp != NULL && *vicp != victim;
531 vicp = &(*vicp)->cache_next)
533 if (*vicp == NULL)
535 CACHE_PERROR (_("cache_set: victim not found"));
536 return;
538 *vicp = victim->cache_next; /* remote from cache */
539 newbuf = victim->cache_reply;
541 else
543 victim = ALLOC (struct cache_node, 1);
544 if (victim == NULL)
546 CACHE_PERROR (_("cache_set: victim alloc failed"));
547 return;
549 newbuf = mem_alloc (su->su_iosz);
550 if (newbuf == NULL)
552 mem_free (victim, sizeof (struct cache_node));
553 CACHE_PERROR (_("cache_set: could not allocate new rpc_buffer"));
554 return;
559 * Store it away
561 victim->cache_replylen = replylen;
562 victim->cache_reply = rpc_buffer (xprt);
563 rpc_buffer (xprt) = newbuf;
564 xdrmem_create (&(su->su_xdrs), rpc_buffer (xprt), su->su_iosz, XDR_ENCODE);
565 victim->cache_xid = su->su_xid;
566 victim->cache_proc = uc->uc_proc;
567 victim->cache_vers = uc->uc_vers;
568 victim->cache_prog = uc->uc_prog;
569 victim->cache_addr = uc->uc_addr;
570 loc = CACHE_LOC (xprt, victim->cache_xid);
571 victim->cache_next = uc->uc_entries[loc];
572 uc->uc_entries[loc] = victim;
573 uc->uc_fifo[uc->uc_nextvictim++] = victim;
574 uc->uc_nextvictim %= uc->uc_size;
578 * Try to get an entry from the cache
579 * return 1 if found, 0 if not found
581 static int
582 cache_get (xprt, msg, replyp, replylenp)
583 SVCXPRT *xprt;
584 struct rpc_msg *msg;
585 char **replyp;
586 u_long *replylenp;
588 u_int loc;
589 cache_ptr ent;
590 struct svcudp_data *su = su_data (xprt);
591 struct udp_cache *uc = (struct udp_cache *) su->su_cache;
593 #define EQADDR(a1, a2) (memcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
595 loc = CACHE_LOC (xprt, su->su_xid);
596 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next)
598 if (ent->cache_xid == su->su_xid &&
599 ent->cache_proc == uc->uc_proc &&
600 ent->cache_vers == uc->uc_vers &&
601 ent->cache_prog == uc->uc_prog &&
602 EQADDR (ent->cache_addr, uc->uc_addr))
604 *replyp = ent->cache_reply;
605 *replylenp = ent->cache_replylen;
606 return 1;
610 * Failed to find entry
611 * Remember a few things so we can do a set later
613 uc->uc_proc = msg->rm_call.cb_proc;
614 uc->uc_vers = msg->rm_call.cb_vers;
615 uc->uc_prog = msg->rm_call.cb_prog;
616 memcpy (&uc->uc_addr, &xprt->xp_raddr, sizeof (uc->uc_addr));
617 return 0;