• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
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 
28 #include <assert.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <unistd.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 
38 static void
print_pktinfo(const struct cmsghdr * c)39 print_pktinfo(const struct cmsghdr *c)
40 {
41 	printf("IP_PKTINFO, {ipi_ifindex=if_nametoindex(\"lo\")"
42 	       ", ipi_spec_dst=inet_addr(\"127.0.0.1\")"
43 	       ", ipi_addr=inet_addr(\"127.0.0.1\")}");
44 }
45 
46 static void
print_ttl(const struct cmsghdr * c)47 print_ttl(const struct cmsghdr *c)
48 {
49 	const unsigned int *ttl = (const unsigned int *) CMSG_DATA(c);
50 
51 	printf("IP_TTL, {ttl=%u}", *ttl);
52 }
53 
54 static void
print_tos(const struct cmsghdr * c)55 print_tos(const struct cmsghdr *c)
56 {
57 	const uint8_t *tos = (const uint8_t *) CMSG_DATA(c);
58 
59 	printf("IP_TOS, {tos=%x}", *tos);
60 }
61 
62 static void
print_opts(const char * name,const struct cmsghdr * c)63 print_opts(const char *name, const struct cmsghdr *c)
64 {
65 	const unsigned char *opts = (const unsigned char *) CMSG_DATA(c);
66 	const size_t len = c->cmsg_len - CMSG_ALIGN(sizeof(*c));
67 
68 	printf("%s", name);
69 	if (len) {
70 		printf(", {opts=0x");
71 		size_t i;
72 		for (i = 0; i < len; ++i)
73 			printf("%02x", opts[i]);
74 		printf("}");
75 	}
76 }
77 
78 static void
print_origdstaddr(const struct cmsghdr * c)79 print_origdstaddr(const struct cmsghdr *c)
80 {
81 	const struct sockaddr_in *sin =
82 		(const struct sockaddr_in *) CMSG_DATA(c);
83 
84 	printf("IP_ORIGDSTADDR, {sa_family=AF_INET, sin_port=htons(%u)"
85 	       ", sin_addr=inet_addr(\"127.0.0.1\")}", ntohs(sin->sin_port));
86 }
87 
88 int
main(void)89 main(void)
90 {
91 	int i;
92 	while ((i = open("/dev/null", O_RDWR)) < 3)
93 		assert(i >= 0);
94 	assert(!close(0));
95 	assert(!close(3));
96 
97 	if (socket(PF_INET, SOCK_DGRAM, 0)) {
98 		perror("socket");
99 		return 77;
100 	}
101 	struct sockaddr_in addr = {
102 		.sin_family = AF_INET,
103 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK)
104 	};
105 	socklen_t len = sizeof(addr);
106 	if (bind(0, (struct sockaddr *) &addr, len)) {
107 		perror("bind");
108 		return 77;
109 	}
110 	assert(!getsockname(0, (struct sockaddr *) &addr, &len));
111 
112 	assert(socket(PF_INET, SOCK_DGRAM, 0) == 3);
113 	assert(!connect(3, (struct sockaddr *) &addr, len));
114 
115 	const int opt_1 = htonl(0x01000000);
116 #define SETSOCKOPT(fd, name) assert(!setsockopt(fd, IPPROTO_IP, (name), &opt_1, sizeof(opt_1)))
117 	SETSOCKOPT(3, IP_OPTIONS);
118 	SETSOCKOPT(0, IP_PKTINFO);
119 	SETSOCKOPT(0, IP_RECVTTL);
120 	SETSOCKOPT(0, IP_RECVTOS);
121 	SETSOCKOPT(0, IP_RECVOPTS);
122 	SETSOCKOPT(0, IP_RETOPTS);
123 #ifdef IP_RECVORIGDSTADDR
124 	SETSOCKOPT(0, IP_RECVORIGDSTADDR);
125 #endif
126 
127 	static const char data[] = "data";
128 	const size_t size = sizeof(data) - 1;
129 	assert(send(3, data, size, 0) == (int) size);
130 	assert(!close(3));
131 
132 	char buf[size];
133 	struct iovec iov = {
134 		.iov_base = buf,
135 		.iov_len = sizeof(buf)
136 	};
137 	struct cmsghdr control[16];
138 	struct msghdr mh = {
139 		.msg_name = &addr,
140 		.msg_namelen = len,
141 		.msg_iov = &iov,
142 		.msg_iovlen = 1,
143 		.msg_control = control,
144 		.msg_controllen = sizeof(control)
145 	};
146 
147 	assert(recvmsg(0, &mh, 0) == (int) size);
148 	assert(!close(0));
149 
150 	printf("recvmsg(0, {msg_name(%u)={sa_family=AF_INET, sin_port=htons(%u)"
151 	       ", sin_addr=inet_addr(\"127.0.0.1\")}, msg_iov(1)=[{\"%s\", %zu}]"
152 	       ", msg_controllen=%zu, [",
153 	       (unsigned) mh.msg_namelen, ntohs(addr.sin_port),
154 	       data, size, mh.msg_controllen);
155 
156 	struct cmsghdr *c;
157 	for (c = CMSG_FIRSTHDR(&mh); c; c = CMSG_NXTHDR(&mh, c)) {
158 		if (IPPROTO_IP != c->cmsg_level)
159 			continue;
160 		if (c != control)
161 			printf(", ");
162 		printf("{cmsg_len=%zu, cmsg_level=SOL_IP, cmsg_type=",
163 		       c->cmsg_len);
164 		switch (c->cmsg_type) {
165 			case IP_PKTINFO:
166 				print_pktinfo(c);
167 				break;
168 			case IP_TTL:
169 				print_ttl(c);
170 				break;
171 			case IP_TOS:
172 				print_tos(c);
173 				break;
174 			case IP_RECVOPTS:
175 				print_opts("IP_RECVOPTS", c);
176 				break;
177 			case IP_RETOPTS:
178 				print_opts("IP_RETOPTS", c);
179 				break;
180 #ifdef IP_ORIGDSTADDR
181 			case IP_ORIGDSTADDR:
182 				print_origdstaddr(c);
183 				break;
184 #endif
185 			default:
186 				printf("%d", c->cmsg_type);
187 				break;
188 		}
189 		printf("}");
190 	}
191 	printf("], msg_flags=0}, 0) = %zu\n", size);
192 	puts("+++ exited with 0 +++");
193 
194 	return 0;
195 }
196