1 /*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "_common.h"
8
9 #ifdef CONFIG_POSIX_API
10 #include <netinet/in.h>
11 #else
12 #include <zephyr/posix/netinet/in.h>
13 #endif
14
15 /**
16 * @brief existence test for `<netinet/in.h>`
17 *
18 * @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html">netinet/in.h</a>
19 */
ZTEST(posix_headers,test_netinet_in_h)20 ZTEST(posix_headers, test_netinet_in_h)
21 {
22 zexpect_equal(sizeof(in_port_t), sizeof(uint16_t));
23 zexpect_equal(sizeof(in_addr_t), sizeof(uint32_t));
24
25 zexpect_not_equal(-1, offsetof(struct in_addr, s_addr));
26
27 zexpect_not_equal(-1, offsetof(struct sockaddr_in, sin_family));
28 zexpect_not_equal(-1, offsetof(struct sockaddr_in, sin_port));
29 zexpect_not_equal(-1, offsetof(struct sockaddr_in, sin_addr));
30
31 zexpect_not_equal(-1, offsetof(struct in6_addr, s6_addr));
32 zexpect_equal(sizeof(((struct in6_addr *)NULL)->s6_addr), 16 * sizeof(uint8_t));
33
34 zexpect_not_equal(-1, offsetof(struct sockaddr_in6, sin6_family));
35 zexpect_not_equal(-1, offsetof(struct sockaddr_in6, sin6_port));
36 /* not implemented */
37 /* zexpect_not_equal(-1, offsetof(struct sockaddr_in6, sin6_flowinfo)); */
38 zexpect_not_equal(-1, offsetof(struct sockaddr_in6, sin6_addr));
39 zexpect_not_equal(-1, offsetof(struct sockaddr_in6, sin6_scope_id));
40
41 zexpect_not_null(&in6addr_any);
42 zexpect_not_null(&in6addr_loopback);
43 struct in6_addr any6 = IN6ADDR_ANY_INIT;
44 struct in6_addr lo6 = IN6ADDR_LOOPBACK_INIT;
45
46 struct in6_addr mcast6 = { { { 0xff, 0 } } };
47 struct in6_addr ll6 = { { { 0xfe, 0x80, 0x01, 0x02,
48 0, 0, 0, 0, 0, 0x01 } } };
49 struct in6_addr sl6 = { { { 0xfe, 0xc0, 0, 0x01, 0x02 } } };
50 struct in6_addr v4mapped = { { { 0, 0, 0, 0, 0, 0, 0, 0,
51 0xff, 0xff, 0xff, 0xff, 0xc0, 0, 0x02, 0x01 } } };
52 struct in6_addr mcnl6 = { { { 0xff, 0x01, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0x01 } } };
54 struct in6_addr mcll6 = { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0x01 } } };
56 struct in6_addr mcsl6 = { { { 0xff, 0x05, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0x01 } } };
58 struct in6_addr mcol6 = { { { 0xff, 0x08, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0x01 } } };
60 struct in6_addr mcg6 = { { { 0xff, 0x0e, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0x01 } } };
62
63 zexpect_not_equal(-1, offsetof(struct ipv6_mreq, ipv6mr_multiaddr));
64 zexpect_not_equal(-1, offsetof(struct ipv6_mreq, ipv6mr_ifindex));
65
66 zexpect_not_equal(-1, IPPROTO_IP);
67 zexpect_not_equal(-1, IPPROTO_IPV6);
68 zexpect_not_equal(-1, IPPROTO_ICMP);
69 zexpect_not_equal(-1, IPPROTO_RAW);
70 zexpect_not_equal(-1, IPPROTO_TCP);
71 zexpect_not_equal(-1, IPPROTO_UDP);
72
73 zexpect_not_equal(-1, INADDR_ANY);
74 zexpect_equal(0xffffffff, INADDR_BROADCAST);
75
76 zexpect_equal(INET_ADDRSTRLEN, 16);
77 zexpect_equal(INET6_ADDRSTRLEN, 46);
78
79 zexpect_equal(IPV6_ADD_MEMBERSHIP, IPV6_JOIN_GROUP);
80 zexpect_equal(IPV6_DROP_MEMBERSHIP, IPV6_LEAVE_GROUP);
81 zexpect_not_equal(-1, IPV6_MULTICAST_HOPS);
82 /* zexpect_not_equal(-1, IPV6_MULTICAST_IF); */ /* not implemented */
83 /* zexpect_not_equal(-1, IPV6_MULTICAST_LOOP); */ /* not implemented */
84 zexpect_not_equal(-1, IPV6_UNICAST_HOPS);
85 zexpect_not_equal(-1, IPV6_V6ONLY);
86
87 zexpect_true(IN6_IS_ADDR_UNSPECIFIED(&any6));
88 zexpect_true(IN6_IS_ADDR_LOOPBACK(&lo6));
89
90 zexpect_true(IN6_IS_ADDR_MULTICAST(&mcast6));
91 zexpect_true(IN6_IS_ADDR_LINKLOCAL(&ll6));
92 zexpect_true(IN6_IS_ADDR_SITELOCAL(&sl6));
93 zexpect_true(IN6_IS_ADDR_V4MAPPED(&v4mapped));
94 /* IN6_IS_ADDR_V4COMPAT(&lo6); */ /* not implemented */
95 zexpect_true(IN6_IS_ADDR_MC_NODELOCAL(&mcnl6));
96 zexpect_true(IN6_IS_ADDR_MC_LINKLOCAL(&mcll6));
97 zexpect_true(IN6_IS_ADDR_MC_SITELOCAL(&mcsl6));
98 zexpect_true(IN6_IS_ADDR_MC_ORGLOCAL(&mcol6));
99 zexpect_true(IN6_IS_ADDR_MC_GLOBAL(&mcg6));
100 }
101