Skip to content

Commit 8f49925

Browse files
jukkarnashif
authored andcommitted
tests: posix: headers: net: Add various IPv6 address check macros
Add these Posix specified IPv6 address checking macros IN6_IS_ADDR_UNSPECIFIED IN6_IS_ADDR_LOOPBACK IN6_IS_ADDR_MULTICAST IN6_IS_ADDR_LINKLOCAL IN6_IS_ADDR_SITELOCAL IN6_IS_ADDR_V4MAPPED IN6_IS_ADDR_MC_NODELOCAL IN6_IS_ADDR_MC_LINKLOCAL IN6_IS_ADDR_MC_SITELOCAL IN6_IS_ADDR_MC_ORGLOCAL IN6_IS_ADDR_MC_GLOBAL and tests for them. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 606eeed commit 8f49925

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

include/zephyr/net/socket.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,46 @@ struct in6_pktinfo {
12951295
#define SOMAXCONN 128
12961296
/** @} */
12971297

1298+
/**
1299+
* @name Macros for checking special IPv6 addresses.
1300+
* @{
1301+
*/
1302+
/** Check unspecified IPv6 address. */
1303+
#define IN6_IS_ADDR_UNSPECIFIED(addr) \
1304+
net_ipv6_addr_cmp(net_ipv6_unspecified_address(), addr)
1305+
1306+
/** Check loopback IPv6 address. */
1307+
#define IN6_IS_ADDR_LOOPBACK(addr) net_ipv6_is_addr_loopback(addr)
1308+
1309+
/** Check IPv6 multicast address */
1310+
#define IN6_IS_ADDR_MULTICAST(addr) net_ipv6_is_addr_mcast(addr)
1311+
1312+
/** Check IPv6 link local address */
1313+
#define IN6_IS_ADDR_LINKLOCAL(addr) net_ipv6_is_ll_addr(addr)
1314+
1315+
/** Check IPv6 site local address */
1316+
#define IN6_IS_ADDR_SITELOCAL(addr) net_ipv6_is_sl_addr(addr)
1317+
1318+
/** Check IPv6 v4 mapped address */
1319+
#define IN6_IS_ADDR_V4MAPPED(addr) net_ipv6_addr_is_v4_mapped(addr)
1320+
1321+
/** Check IPv6 multicast global address */
1322+
#define IN6_IS_ADDR_MC_GLOBAL(addr) net_ipv6_is_addr_mcast_global(addr)
1323+
1324+
/** Check IPv6 multicast node local address */
1325+
#define IN6_IS_ADDR_MC_NODELOCAL(addr) net_ipv6_is_addr_mcast_iface(addr)
1326+
1327+
/** Check IPv6 multicast link local address */
1328+
#define IN6_IS_ADDR_MC_LINKLOCAL(addr) net_ipv6_is_addr_mcast_link(addr)
1329+
1330+
/** Check IPv6 multicast site local address */
1331+
#define IN6_IS_ADDR_MC_SITELOCAL(addr) net_ipv6_is_addr_mcast_site(addr)
1332+
1333+
/** Check IPv6 multicast organization local address */
1334+
#define IN6_IS_ADDR_MC_ORGLOCAL(addr) net_ipv6_is_addr_mcast_org(addr)
1335+
1336+
/** @} */
1337+
12981338
/** @cond INTERNAL_HIDDEN */
12991339
/**
13001340
* @brief Registration information for a given BSD socket family.

tests/posix/headers/src/netinet_in_h.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ ZTEST(posix_headers, test_netinet_in_h)
4545
struct in6_addr any6 = IN6ADDR_ANY_INIT;
4646
struct in6_addr lo6 = IN6ADDR_LOOPBACK_INIT;
4747

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+
4865
/* not implemented */
4966
/* zassert_not_equal(-1, offsetof(struct ipv6_mreq, ipv6mr_multiaddr)); */
5067
/* not implemented */
@@ -71,18 +88,18 @@ ZTEST(posix_headers, test_netinet_in_h)
7188
zassert_not_equal(-1, IPV6_UNICAST_HOPS);
7289
zassert_not_equal(-1, IPV6_V6ONLY);
7390

74-
/* IN6_IS_ADDR_UNSPECIFIED(&any6); */ /* not implemented */
75-
/* IN6_IS_ADDR_LOOPBACK(&lo6); */ /* not implemented */
91+
zassert_true(IN6_IS_ADDR_UNSPECIFIED(&any6));
92+
zassert_true(IN6_IS_ADDR_LOOPBACK(&lo6));
7693

77-
/* IN6_IS_ADDR_MULTICAST(&lo6); */ /* not implemented */
78-
/* IN6_IS_ADDR_LINKLOCAL(&lo6); */ /* not implemented */
79-
/* IN6_IS_ADDR_SITELOCAL(&lo6); */ /* not implemented */
80-
/* IN6_IS_ADDR_V4MAPPED(&lo6); */ /* not implemented */
94+
zassert_true(IN6_IS_ADDR_MULTICAST(&mcast6));
95+
zassert_true(IN6_IS_ADDR_LINKLOCAL(&ll6));
96+
zassert_true(IN6_IS_ADDR_SITELOCAL(&sl6));
97+
zassert_true(IN6_IS_ADDR_V4MAPPED(&v4mapped));
8198
/* IN6_IS_ADDR_V4COMPAT(&lo6); */ /* not implemented */
82-
/* IN6_IS_ADDR_MC_NODELOCAL(&lo6); */ /* not implemented */
83-
/* IN6_IS_ADDR_MC_LINKLOCAL(&lo6); */ /* not implemented */
84-
/* IN6_IS_ADDR_MC_SITELOCAL(&lo6); */ /* not implemented */
85-
/* IN6_IS_ADDR_MC_ORGLOCAL(&lo6); */ /* not implemented */
86-
/* IN6_IS_ADDR_MC_GLOBAL(&lo6); */ /* not implemented */
99+
zassert_true(IN6_IS_ADDR_MC_NODELOCAL(&mcnl6));
100+
zassert_true(IN6_IS_ADDR_MC_LINKLOCAL(&mcll6));
101+
zassert_true(IN6_IS_ADDR_MC_SITELOCAL(&mcsl6));
102+
zassert_true(IN6_IS_ADDR_MC_ORGLOCAL(&mcol6));
103+
zassert_true(IN6_IS_ADDR_MC_GLOBAL(&mcg6));
87104
}
88105
#pragma GCC diagnostic pop

0 commit comments

Comments
 (0)