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