X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libbb%2Fudp_io.c;h=6e3ef484e4a1f4419bdfce8807ba7ec14f922425;hb=045327a418d1cf0a99405ca0b70ed61b5c1a1869;hp=e968ecb6600b177be9c78cec2b63aeaf51ba7a72;hpb=081eb71ebd7954a67287816a9a6fff80e8c5319a;p=oweals%2Fbusybox.git diff --git a/libbb/udp_io.c b/libbb/udp_io.c index e968ecb66..6e3ef484e 100644 --- a/libbb/udp_io.c +++ b/libbb/udp_io.c @@ -4,49 +4,50 @@ * * Copyright (C) 2007 Denys Vlasenko * - * Licensed under GPL version 2, see file LICENSE in this tarball for details. + * Licensed under GPLv2, see file LICENSE in this source tree. */ - #include "libbb.h" /* * This asks kernel to let us know dst addr/port of incoming packets * We don't check for errors here. Not supported == won't be used */ -void -socket_want_pktinfo(int fd) +void FAST_FUNC +socket_want_pktinfo(int fd UNUSED_PARAM) { #ifdef IP_PKTINFO - setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &const_int_1, sizeof(int)); + setsockopt_1(fd, IPPROTO_IP, IP_PKTINFO); #endif #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) - setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &const_int_1, sizeof(int)); + setsockopt_1(fd, IPPROTO_IPV6, IPV6_PKTINFO); #endif } -ssize_t +ssize_t FAST_FUNC send_to_from(int fd, void *buf, size_t len, int flags, const struct sockaddr *to, const struct sockaddr *from, socklen_t tolen) { #ifndef IP_PKTINFO + (void)from; /* suppress "unused from" warning */ return sendto(fd, buf, len, flags, to, tolen); #else struct iovec iov[1]; struct msghdr msg; - char cbuf[sizeof(struct in_pktinfo) -#if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) - | sizeof(struct in6_pktinfo) /* (a|b) is poor man's max(a,b) */ -#endif - ]; + union { + char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))]; +# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) + char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; +# endif + } u; struct cmsghdr* cmsgptr; if (from->sa_family != AF_INET -#if ENABLE_FEATURE_IPV6 +# if ENABLE_FEATURE_IPV6 && from->sa_family != AF_INET6 -#endif +# endif ) { /* ANY local address */ return sendto(fd, buf, len, flags, to, tolen); @@ -57,38 +58,52 @@ send_to_from(int fd, void *buf, size_t len, int flags, iov[0].iov_base = buf; iov[0].iov_len = len; - memset(cbuf, 0, sizeof(cbuf)); + memset(&u, 0, sizeof(u)); memset(&msg, 0, sizeof(msg)); msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */ msg.msg_namelen = tolen; msg.msg_iov = iov; msg.msg_iovlen = 1; - msg.msg_control = cbuf; - msg.msg_controllen = sizeof(cbuf); + msg.msg_control = &u; + msg.msg_controllen = sizeof(u); msg.msg_flags = flags; cmsgptr = CMSG_FIRSTHDR(&msg); - if (to->sa_family == AF_INET && from->sa_family == AF_INET) { + /* + * Users report that to->sa_family can be AF_INET6 too, + * if "to" was acquired by recv_from_to(). IOW: recv_from_to() + * was seen showing IPv6 "from" even when the destination + * of received packet (our local address) was IPv4. + */ + if (/* to->sa_family == AF_INET && */ from->sa_family == AF_INET) { struct in_pktinfo *pktptr; cmsgptr->cmsg_level = IPPROTO_IP; cmsgptr->cmsg_type = IP_PKTINFO; cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr)); - /* pktptr->ipi_ifindex = 0; -- already done by memset(cbuf...) */ + /*pktptr->ipi_ifindex = 0; -- already done by memset(u...) */ + /* In general, CMSG_DATA() can be unaligned, but in this case + * we know for sure it is sufficiently aligned: + * CMSG_FIRSTHDR simply returns &u above, + * and CMSG_DATA returns &u + size_t + int + int. + * Thus direct assignment is ok: + */ pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr; } -#if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) - else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) { +# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) + else if (/* to->sa_family == AF_INET6 && */ from->sa_family == AF_INET6) { struct in6_pktinfo *pktptr; cmsgptr->cmsg_level = IPPROTO_IPV6; cmsgptr->cmsg_type = IPV6_PKTINFO; cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr)); - /* pktptr->ipi6_ifindex = 0; -- already done by memset(cbuf...) */ + /* pktptr->ipi6_ifindex = 0; -- already done by memset(u...) */ pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr; } -#endif +# endif + msg.msg_controllen = cmsgptr->cmsg_len; + return sendmsg(fd, &msg, flags); #endif } @@ -97,23 +112,26 @@ send_to_from(int fd, void *buf, size_t len, int flags, * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified. * Typical usage is to preinit 'to' with "default" value * before calling recv_from_to(). */ -ssize_t +ssize_t FAST_FUNC recv_from_to(int fd, void *buf, size_t len, int flags, struct sockaddr *from, struct sockaddr *to, socklen_t sa_size) { #ifndef IP_PKTINFO + (void)to; /* suppress "unused to" warning */ return recvfrom(fd, buf, len, flags, from, &sa_size); #else /* man recvmsg and man cmsg is needed to make sense of code below */ struct iovec iov[1]; union { char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))]; +# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; +# endif } u; struct cmsghdr *cmsgptr; struct msghdr msg; - socklen_t recv_length; + ssize_t recv_length; iov[0].iov_base = buf; iov[0].iov_len = len; @@ -130,6 +148,8 @@ recv_from_to(int fd, void *buf, size_t len, int flags, if (recv_length < 0) return recv_length; +# define to4 ((struct sockaddr_in*)to) +# define to6 ((struct sockaddr_in6*)to) /* Here we try to retrieve destination IP and memorize it */ for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; @@ -138,25 +158,27 @@ recv_from_to(int fd, void *buf, size_t len, int flags, if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_PKTINFO ) { -#define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) ) + const int IPI_ADDR_OFF = offsetof(struct in_pktinfo, ipi_addr); to->sa_family = AF_INET; - ((struct sockaddr_in*)to)->sin_addr = pktinfo(cmsgptr)->ipi_addr; - /* ((struct sockaddr_in*)to)->sin_port = 123; */ -#undef pktinfo + /*# define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )*/ + /*to4->sin_addr = pktinfo(cmsgptr)->ipi_addr; - may be unaligned */ + memcpy(&to4->sin_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI_ADDR_OFF, sizeof(to4->sin_addr)); + /*to4->sin_port = 123; - this data is not supplied by kernel */ break; } -#if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) +# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) if (cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_PKTINFO ) { -#define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) ) + const int IPI6_ADDR_OFF = offsetof(struct in6_pktinfo, ipi6_addr); to->sa_family = AF_INET6; - ((struct sockaddr_in6*)to)->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - /* ((struct sockaddr_in6*)to)->sin6_port = 123; */ -#undef pktinfo + /*# define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )*/ + /*to6->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - may be unaligned */ + memcpy(&to6->sin6_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI6_ADDR_OFF, sizeof(to6->sin6_addr)); + /*to6->sin6_port = 123; */ break; } -#endif +# endif } return recv_length; #endif