add IPv4 & IPv6 frag reassemble test case

This commit is contained in:
luwenpeng
2024-02-23 18:19:52 +08:00
parent 5cd0571b4d
commit 2e748e0821
12 changed files with 1462 additions and 328 deletions

View File

@@ -100,8 +100,6 @@ static inline struct ip6_frag *ipv6_hdr_get_frag_ext(const struct ip6_hdr *hdr)
return (struct ip6_frag *)((char *)hdr + sizeof(struct ip6_hdr));
}
// TODO IPv6 extension headers
/******************************************************************************
* set
******************************************************************************/
@@ -146,7 +144,56 @@ static inline void ipv6_hdr_set_dst_in6_addr(struct ip6_hdr *hdr, struct in6_add
hdr->ip6_dst = dst_addr;
}
// TODO IPv6 extension headers
/******************************************************************************
* IPv6 frag extension headers
******************************************************************************/
static inline uint8_t ipv6_frag_get_next_header(const struct ip6_frag *frag)
{
return frag->ip6f_nxt;
}
static inline uint16_t ipv6_frag_get_offset(const struct ip6_frag *frag)
{
return ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
}
static inline uint32_t ipv6_frag_get_ident(const struct ip6_frag *frag)
{
return ntohl(frag->ip6f_ident);
}
static inline bool ipv6_frag_get_more(const struct ip6_frag *frag)
{
return (frag->ip6f_offlg & IP6F_MORE_FRAG);
}
static inline void ipv6_frag_set_next_header(struct ip6_frag *frag, uint8_t next_header)
{
frag->ip6f_nxt = next_header;
}
static inline void ipv6_frag_set_offset(struct ip6_frag *frag, uint16_t offset)
{
frag->ip6f_offlg = (frag->ip6f_offlg & ~IP6F_OFF_MASK) | htons(offset);
}
static inline void ipv6_frag_set_ident(struct ip6_frag *frag, uint32_t ident)
{
frag->ip6f_ident = htonl(ident);
}
static inline void ipv6_frag_set_more(struct ip6_frag *frag, bool more)
{
if (more)
{
frag->ip6f_offlg |= IP6F_MORE_FRAG;
}
else
{
frag->ip6f_offlg &= ~IP6F_MORE_FRAG;
}
}
#ifdef __cpluscplus
}

View File

@@ -62,6 +62,41 @@ TEST(IPV6_UTILS, SET)
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
}
/*
* Fragment Header for IPv6
* Next header: UDP (17)
* Reserved octet: 0x00
* 0000 0101 1010 1... = Offset: 181 (1448 bytes)
* .... .... .... .00. = Reserved bits: 0
* .... .... .... ...1 = More Fragments: Yes
* Identification: 0xf88eb466
*/
unsigned char frag[] = {
0x11, 0x00, 0x05, 0xa9, 0xf8, 0x8e, 0xb4, 0x66};
TEST(IPV6_FRAG_HDR, GET)
{
const struct ip6_frag *hdr = (struct ip6_frag *)frag;
EXPECT_TRUE(ipv6_frag_get_next_header(hdr) == 17);
EXPECT_TRUE(ipv6_frag_get_offset(hdr) == 1448);
EXPECT_TRUE(ipv6_frag_get_more(hdr) == 1);
EXPECT_TRUE(ipv6_frag_get_ident(hdr) == 0xf88eb466);
}
TEST(IPV6_FRAG_HDR, SET)
{
char buff[8] = {0};
struct ip6_frag *hdr = (struct ip6_frag *)buff;
ipv6_frag_set_next_header(hdr, 17);
ipv6_frag_set_offset(hdr, 1448);
ipv6_frag_set_more(hdr, 1);
ipv6_frag_set_ident(hdr, 0xf88eb466);
EXPECT_TRUE(memcmp(buff, frag, 8) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);

View File

@@ -98,7 +98,7 @@ TEST(TCP_UTILS, GET)
TEST(TCP_UTILS, SET1)
{
char buff[40];
char buff[40] = {0};
struct tcphdr *hdr = (struct tcphdr *)buff;
tcp_hdr_set_src_port(hdr, 55555);