1/*
2 * Copyright (c) 2015-2016 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 "tests.h"
29#include <assert.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
38static void
39print_pktinfo(const struct cmsghdr *c)
40{
41	printf("IP_PKTINFO, cmsg_data={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
46static void
47print_ttl(const struct cmsghdr *c)
48{
49	const unsigned int *ttl = (const unsigned int *) CMSG_DATA(c);
50
51	printf("IP_TTL, cmsg_data=[%u]", *ttl);
52}
53
54static void
55print_tos(const struct cmsghdr *c)
56{
57	const uint8_t *tos = (const uint8_t *) CMSG_DATA(c);
58
59	printf("IP_TOS, cmsg_data=[%#x]", *tos);
60}
61
62static void
63print_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(", cmsg_data=[");
71		size_t i;
72		for (i = 0; i < len; ++i)
73			printf("%s0x%02x", i ? ", " : "", opts[i]);
74		printf("]");
75	}
76}
77
78#ifdef IP_ORIGDSTADDR
79static void
80print_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, cmsg_data={sa_family=AF_INET, sin_port=htons(%u)"
86	       ", sin_addr=inet_addr(\"127.0.0.1\")}", ntohs(sin->sin_port));
87}
88#endif
89
90int
91main(void)
92{
93	int i;
94	while ((i = open("/dev/null", O_RDWR)) < 3)
95		assert(i >= 0);
96	assert(!close(0));
97	assert(!close(3));
98
99	if (socket(AF_INET, SOCK_DGRAM, 0))
100		perror_msg_and_skip("socket");
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_msg_and_skip("bind");
108	assert(!getsockname(0, (struct sockaddr *) &addr, &len));
109
110	assert(socket(AF_INET, SOCK_DGRAM, 0) == 3);
111	assert(!connect(3, (struct sockaddr *) &addr, len));
112
113	const int opt_1 = htonl(0x01000000);
114#define SETSOCKOPT(fd, name) assert(!setsockopt(fd, IPPROTO_IP, (name), &opt_1, sizeof(opt_1)))
115	SETSOCKOPT(3, IP_OPTIONS);
116	SETSOCKOPT(0, IP_PKTINFO);
117	SETSOCKOPT(0, IP_RECVTTL);
118	SETSOCKOPT(0, IP_RECVTOS);
119	SETSOCKOPT(0, IP_RECVOPTS);
120	SETSOCKOPT(0, IP_RETOPTS);
121#ifdef IP_RECVORIGDSTADDR
122	SETSOCKOPT(0, IP_RECVORIGDSTADDR);
123#endif
124
125	static const char data[] = "data";
126	const size_t size = sizeof(data) - 1;
127	assert(send(3, data, size, 0) == (int) size);
128	assert(!close(3));
129
130	char buf[size];
131	struct iovec iov = {
132		.iov_base = buf,
133		.iov_len = sizeof(buf)
134	};
135	struct cmsghdr control[16];
136	struct msghdr mh = {
137		.msg_name = &addr,
138		.msg_namelen = len,
139		.msg_iov = &iov,
140		.msg_iovlen = 1,
141		.msg_control = control,
142		.msg_controllen = sizeof(control)
143	};
144
145	assert(recvmsg(0, &mh, 0) == (int) size);
146	assert(!close(0));
147
148	printf("recvmsg(0, {msg_name={sa_family=AF_INET, sin_port=htons(%u)"
149	       ", sin_addr=inet_addr(\"127.0.0.1\")}, msg_namelen=%u"
150	       ", msg_iov=[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=1"
151	       ", msg_control=[",
152	       ntohs(addr.sin_port), (unsigned) mh.msg_namelen,
153	       data, (unsigned) size);
154
155	struct cmsghdr *c;
156	for (c = CMSG_FIRSTHDR(&mh); c; c = CMSG_NXTHDR(&mh, c)) {
157		if (IPPROTO_IP != c->cmsg_level)
158			continue;
159		if (c != control)
160			printf(", ");
161		printf("{cmsg_len=%lu, cmsg_level=SOL_IP, cmsg_type=",
162		       (unsigned long) c->cmsg_len);
163		switch (c->cmsg_type) {
164			case IP_PKTINFO:
165				print_pktinfo(c);
166				break;
167			case IP_TTL:
168				print_ttl(c);
169				break;
170			case IP_TOS:
171				print_tos(c);
172				break;
173			case IP_RECVOPTS:
174				print_opts("IP_RECVOPTS", c);
175				break;
176			case IP_RETOPTS:
177				print_opts("IP_RETOPTS", c);
178				break;
179#ifdef IP_ORIGDSTADDR
180			case IP_ORIGDSTADDR:
181				print_origdstaddr(c);
182				break;
183#endif
184			default:
185				printf("%d", c->cmsg_type);
186				break;
187		}
188		printf("}");
189	}
190	printf("], msg_controllen=%lu, msg_flags=0}, 0) = %u\n",
191	       (unsigned long) mh.msg_controllen, (unsigned) size);
192	puts("+++ exited with 0 +++");
193
194	return 0;
195}
196