Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Denys Vlasenko |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 6 | * |
| 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
| 8 | */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 9 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * This asks kernel to let us know dst addr/port of incoming packets |
| 14 | * We don't check for errors here. Not supported == won't be used |
| 15 | */ |
| 16 | void |
| 17 | socket_want_pktinfo(int fd) |
| 18 | { |
| 19 | #ifdef IP_PKTINFO |
| 20 | setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &const_int_1, sizeof(int)); |
| 21 | #endif |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 22 | #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 23 | setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &const_int_1, sizeof(int)); |
| 24 | #endif |
| 25 | } |
| 26 | |
| 27 | |
| 28 | ssize_t |
| 29 | send_to_from(int fd, void *buf, size_t len, int flags, |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 30 | const struct sockaddr *to, |
| 31 | const struct sockaddr *from, |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 32 | socklen_t tolen) |
| 33 | { |
| 34 | #ifndef IP_PKTINFO |
| 35 | return sendto(fd, buf, len, flags, to, tolen); |
| 36 | #else |
| 37 | struct iovec iov[1]; |
| 38 | struct msghdr msg; |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 39 | union { |
| 40 | char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 41 | #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 42 | char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 43 | #endif |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 44 | } u; |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 45 | struct cmsghdr* cmsgptr; |
| 46 | |
| 47 | if (from->sa_family != AF_INET |
| 48 | #if ENABLE_FEATURE_IPV6 |
| 49 | && from->sa_family != AF_INET6 |
| 50 | #endif |
| 51 | ) { |
| 52 | /* ANY local address */ |
| 53 | return sendto(fd, buf, len, flags, to, tolen); |
| 54 | } |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 56 | /* man recvmsg and man cmsg is needed to make sense of code below */ |
| 57 | |
| 58 | iov[0].iov_base = buf; |
| 59 | iov[0].iov_len = len; |
| 60 | |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 61 | memset(&u, 0, sizeof(u)); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 62 | |
| 63 | memset(&msg, 0, sizeof(msg)); |
| 64 | msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */ |
| 65 | msg.msg_namelen = tolen; |
| 66 | msg.msg_iov = iov; |
| 67 | msg.msg_iovlen = 1; |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 68 | msg.msg_control = &u; |
| 69 | msg.msg_controllen = sizeof(u); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 70 | msg.msg_flags = flags; |
| 71 | |
| 72 | cmsgptr = CMSG_FIRSTHDR(&msg); |
| 73 | if (to->sa_family == AF_INET && from->sa_family == AF_INET) { |
| 74 | struct in_pktinfo *pktptr; |
| 75 | cmsgptr->cmsg_level = IPPROTO_IP; |
| 76 | cmsgptr->cmsg_type = IP_PKTINFO; |
| 77 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 78 | pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr)); |
| 79 | /* pktptr->ipi_ifindex = 0; -- already done by memset(cbuf...) */ |
| 80 | pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr; |
| 81 | } |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 82 | #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 83 | else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) { |
| 84 | struct in6_pktinfo *pktptr; |
| 85 | cmsgptr->cmsg_level = IPPROTO_IPV6; |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 86 | cmsgptr->cmsg_type = IPV6_PKTINFO; |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 87 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
| 88 | pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr)); |
| 89 | /* pktptr->ipi6_ifindex = 0; -- already done by memset(cbuf...) */ |
| 90 | pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr; |
| 91 | } |
| 92 | #endif |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 93 | msg.msg_controllen = cmsgptr->cmsg_len; |
| 94 | |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 95 | return sendmsg(fd, &msg, flags); |
| 96 | #endif |
| 97 | } |
| 98 | |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 99 | /* NB: this will never set port# in 'to'! |
| 100 | * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified. |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 101 | * Typical usage is to preinit 'to' with "default" value |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 102 | * before calling recv_from_to(). */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 103 | ssize_t |
| 104 | recv_from_to(int fd, void *buf, size_t len, int flags, |
| 105 | struct sockaddr *from, struct sockaddr *to, |
| 106 | socklen_t sa_size) |
| 107 | { |
| 108 | #ifndef IP_PKTINFO |
| 109 | return recvfrom(fd, buf, len, flags, from, &sa_size); |
| 110 | #else |
| 111 | /* man recvmsg and man cmsg is needed to make sense of code below */ |
| 112 | struct iovec iov[1]; |
| 113 | union { |
| 114 | char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 115 | #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 116 | char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 117 | #endif |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 118 | } u; |
| 119 | struct cmsghdr *cmsgptr; |
| 120 | struct msghdr msg; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 121 | ssize_t recv_length; |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 122 | |
| 123 | iov[0].iov_base = buf; |
| 124 | iov[0].iov_len = len; |
| 125 | |
| 126 | memset(&msg, 0, sizeof(msg)); |
| 127 | msg.msg_name = (struct sockaddr *)from; |
| 128 | msg.msg_namelen = sa_size; |
| 129 | msg.msg_iov = iov; |
| 130 | msg.msg_iovlen = 1; |
| 131 | msg.msg_control = &u; |
| 132 | msg.msg_controllen = sizeof(u); |
| 133 | |
| 134 | recv_length = recvmsg(fd, &msg, flags); |
| 135 | if (recv_length < 0) |
| 136 | return recv_length; |
| 137 | |
| 138 | /* Here we try to retrieve destination IP and memorize it */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 139 | for (cmsgptr = CMSG_FIRSTHDR(&msg); |
| 140 | cmsgptr != NULL; |
| 141 | cmsgptr = CMSG_NXTHDR(&msg, cmsgptr) |
| 142 | ) { |
| 143 | if (cmsgptr->cmsg_level == IPPROTO_IP |
| 144 | && cmsgptr->cmsg_type == IP_PKTINFO |
| 145 | ) { |
| 146 | #define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) ) |
| 147 | to->sa_family = AF_INET; |
| 148 | ((struct sockaddr_in*)to)->sin_addr = pktinfo(cmsgptr)->ipi_addr; |
| 149 | /* ((struct sockaddr_in*)to)->sin_port = 123; */ |
| 150 | #undef pktinfo |
| 151 | break; |
| 152 | } |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 153 | #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 154 | if (cmsgptr->cmsg_level == IPPROTO_IPV6 |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 155 | && cmsgptr->cmsg_type == IPV6_PKTINFO |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 156 | ) { |
| 157 | #define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) ) |
| 158 | to->sa_family = AF_INET6; |
| 159 | ((struct sockaddr_in6*)to)->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; |
| 160 | /* ((struct sockaddr_in6*)to)->sin6_port = 123; */ |
| 161 | #undef pktinfo |
| 162 | break; |
| 163 | } |
| 164 | #endif |
| 165 | } |
| 166 | return recv_length; |
| 167 | #endif |
| 168 | } |