Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 91a6ef6 | 2016-01-05 23:13:41 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Dmitry V. Levin | 91a6ef6 | 2016-01-05 23:13:41 +0000 | [diff] [blame] | 28 | #include "tests.h" |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 29 | #include <assert.h> |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdint.h> |
| 34 | #include <unistd.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <arpa/inet.h> |
| 38 | |
| 39 | static void |
| 40 | print_pktinfo(const struct cmsghdr *c) |
| 41 | { |
| 42 | printf("IP_PKTINFO, {ipi_ifindex=if_nametoindex(\"lo\")" |
| 43 | ", ipi_spec_dst=inet_addr(\"127.0.0.1\")" |
| 44 | ", ipi_addr=inet_addr(\"127.0.0.1\")}"); |
| 45 | } |
| 46 | |
| 47 | static void |
| 48 | print_ttl(const struct cmsghdr *c) |
| 49 | { |
| 50 | const unsigned int *ttl = (const unsigned int *) CMSG_DATA(c); |
| 51 | |
| 52 | printf("IP_TTL, {ttl=%u}", *ttl); |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | print_tos(const struct cmsghdr *c) |
| 57 | { |
| 58 | const uint8_t *tos = (const uint8_t *) CMSG_DATA(c); |
| 59 | |
| 60 | printf("IP_TOS, {tos=%x}", *tos); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | print_opts(const char *name, const struct cmsghdr *c) |
| 65 | { |
| 66 | const unsigned char *opts = (const unsigned char *) CMSG_DATA(c); |
| 67 | const size_t len = c->cmsg_len - CMSG_ALIGN(sizeof(*c)); |
| 68 | |
| 69 | printf("%s", name); |
| 70 | if (len) { |
| 71 | printf(", {opts=0x"); |
| 72 | size_t i; |
| 73 | for (i = 0; i < len; ++i) |
| 74 | printf("%02x", opts[i]); |
| 75 | printf("}"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | print_origdstaddr(const struct cmsghdr *c) |
| 81 | { |
| 82 | const struct sockaddr_in *sin = |
| 83 | (const struct sockaddr_in *) CMSG_DATA(c); |
| 84 | |
| 85 | printf("IP_ORIGDSTADDR, {sa_family=AF_INET, sin_port=htons(%u)" |
| 86 | ", sin_addr=inet_addr(\"127.0.0.1\")}", ntohs(sin->sin_port)); |
| 87 | } |
| 88 | |
| 89 | int |
| 90 | main(void) |
| 91 | { |
| 92 | int i; |
| 93 | while ((i = open("/dev/null", O_RDWR)) < 3) |
| 94 | assert(i >= 0); |
| 95 | assert(!close(0)); |
| 96 | assert(!close(3)); |
| 97 | |
Dmitry V. Levin | 71fe62e | 2016-04-04 01:35:28 +0000 | [diff] [blame] | 98 | if (socket(AF_INET, SOCK_DGRAM, 0)) |
Dmitry V. Levin | 91a6ef6 | 2016-01-05 23:13:41 +0000 | [diff] [blame] | 99 | perror_msg_and_skip("socket"); |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 100 | struct sockaddr_in addr = { |
| 101 | .sin_family = AF_INET, |
| 102 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK) |
| 103 | }; |
| 104 | socklen_t len = sizeof(addr); |
Dmitry V. Levin | 91a6ef6 | 2016-01-05 23:13:41 +0000 | [diff] [blame] | 105 | if (bind(0, (struct sockaddr *) &addr, len)) |
| 106 | perror_msg_and_skip("bind"); |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 107 | assert(!getsockname(0, (struct sockaddr *) &addr, &len)); |
| 108 | |
Dmitry V. Levin | 71fe62e | 2016-04-04 01:35:28 +0000 | [diff] [blame] | 109 | assert(socket(AF_INET, SOCK_DGRAM, 0) == 3); |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 110 | assert(!connect(3, (struct sockaddr *) &addr, len)); |
| 111 | |
| 112 | const int opt_1 = htonl(0x01000000); |
| 113 | #define SETSOCKOPT(fd, name) assert(!setsockopt(fd, IPPROTO_IP, (name), &opt_1, sizeof(opt_1))) |
| 114 | SETSOCKOPT(3, IP_OPTIONS); |
| 115 | SETSOCKOPT(0, IP_PKTINFO); |
| 116 | SETSOCKOPT(0, IP_RECVTTL); |
| 117 | SETSOCKOPT(0, IP_RECVTOS); |
| 118 | SETSOCKOPT(0, IP_RECVOPTS); |
| 119 | SETSOCKOPT(0, IP_RETOPTS); |
| 120 | #ifdef IP_RECVORIGDSTADDR |
| 121 | SETSOCKOPT(0, IP_RECVORIGDSTADDR); |
| 122 | #endif |
| 123 | |
| 124 | static const char data[] = "data"; |
| 125 | const size_t size = sizeof(data) - 1; |
| 126 | assert(send(3, data, size, 0) == (int) size); |
| 127 | assert(!close(3)); |
| 128 | |
| 129 | char buf[size]; |
| 130 | struct iovec iov = { |
| 131 | .iov_base = buf, |
| 132 | .iov_len = sizeof(buf) |
| 133 | }; |
| 134 | struct cmsghdr control[16]; |
| 135 | struct msghdr mh = { |
| 136 | .msg_name = &addr, |
| 137 | .msg_namelen = len, |
| 138 | .msg_iov = &iov, |
| 139 | .msg_iovlen = 1, |
| 140 | .msg_control = control, |
| 141 | .msg_controllen = sizeof(control) |
| 142 | }; |
| 143 | |
| 144 | assert(recvmsg(0, &mh, 0) == (int) size); |
| 145 | assert(!close(0)); |
| 146 | |
| 147 | printf("recvmsg(0, {msg_name(%u)={sa_family=AF_INET, sin_port=htons(%u)" |
| 148 | ", sin_addr=inet_addr(\"127.0.0.1\")}, msg_iov(1)=[{\"%s\", %zu}]" |
Dmitry V. Levin | 4776e33 | 2016-01-12 00:00:47 +0000 | [diff] [blame] | 149 | ", msg_controllen=%lu, [", |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 150 | (unsigned) mh.msg_namelen, ntohs(addr.sin_port), |
Dmitry V. Levin | 4776e33 | 2016-01-12 00:00:47 +0000 | [diff] [blame] | 151 | data, size, (unsigned long) mh.msg_controllen); |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 152 | |
| 153 | struct cmsghdr *c; |
| 154 | for (c = CMSG_FIRSTHDR(&mh); c; c = CMSG_NXTHDR(&mh, c)) { |
| 155 | if (IPPROTO_IP != c->cmsg_level) |
| 156 | continue; |
| 157 | if (c != control) |
| 158 | printf(", "); |
Dmitry V. Levin | 4776e33 | 2016-01-12 00:00:47 +0000 | [diff] [blame] | 159 | printf("{cmsg_len=%lu, cmsg_level=SOL_IP, cmsg_type=", |
| 160 | (unsigned long) c->cmsg_len); |
Dmitry V. Levin | 1f111cf | 2015-11-21 03:03:54 +0300 | [diff] [blame] | 161 | switch (c->cmsg_type) { |
| 162 | case IP_PKTINFO: |
| 163 | print_pktinfo(c); |
| 164 | break; |
| 165 | case IP_TTL: |
| 166 | print_ttl(c); |
| 167 | break; |
| 168 | case IP_TOS: |
| 169 | print_tos(c); |
| 170 | break; |
| 171 | case IP_RECVOPTS: |
| 172 | print_opts("IP_RECVOPTS", c); |
| 173 | break; |
| 174 | case IP_RETOPTS: |
| 175 | print_opts("IP_RETOPTS", c); |
| 176 | break; |
| 177 | #ifdef IP_ORIGDSTADDR |
| 178 | case IP_ORIGDSTADDR: |
| 179 | print_origdstaddr(c); |
| 180 | break; |
| 181 | #endif |
| 182 | default: |
| 183 | printf("%d", c->cmsg_type); |
| 184 | break; |
| 185 | } |
| 186 | printf("}"); |
| 187 | } |
| 188 | printf("], msg_flags=0}, 0) = %zu\n", size); |
| 189 | puts("+++ exited with 0 +++"); |
| 190 | |
| 191 | return 0; |
| 192 | } |