refactor: update packet get layer/tunnel API
This commit is contained in:
@@ -145,19 +145,20 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_TCP)
|
||||
|
||||
EXPECT_TRUE(orig_pkt_len - 6 == // trim Eth padding: 000000000000
|
||||
new_pkt_len - 12 - 5); // trim TCP options, TCP payload
|
||||
struct layer layer;
|
||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
||||
int count = packet_get_layer_count(new_pkt);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV4)
|
||||
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV4)
|
||||
{
|
||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
||||
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 57);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xb7e1);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1);
|
||||
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2);
|
||||
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
|
||||
@@ -282,25 +283,26 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_IP6_TCP)
|
||||
|
||||
EXPECT_TRUE(orig_pkt_len - 12 == // trim TCP options
|
||||
new_pkt_len - 5); // trim TCP payload
|
||||
struct layer layer;
|
||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
||||
int count = packet_get_layer_count(new_pkt);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV4)
|
||||
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV4)
|
||||
{
|
||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
||||
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 85);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x09cf);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 25);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1234);
|
||||
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2345);
|
||||
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
|
||||
@@ -498,39 +500,40 @@ TEST(PACKET_BUILD_TCP, ETH_IP6_UDP_GTP_IP4_TCP)
|
||||
|
||||
EXPECT_TRUE(orig_pkt_len - 12 - 1348 == // trim TCP options, TCP payload
|
||||
new_pkt_len - 5); // trim TCP payload
|
||||
struct layer layer;
|
||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
||||
int count = packet_get_layer_count(new_pkt);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV6)
|
||||
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 61);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_UDP)
|
||||
if (layer->proto == LAYER_PROTO_UDP)
|
||||
{
|
||||
const struct udphdr *udp = (const struct udphdr *)layer.hdr.raw;
|
||||
const struct udphdr *udp = (const struct udphdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(udp_hdr_get_total_len(udp) == 61);
|
||||
EXPECT_TRUE(udp_hdr_get_checksum(udp) == 0xd375);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_GTP_U)
|
||||
if (layer->proto == LAYER_PROTO_GTP_U)
|
||||
{
|
||||
EXPECT_TRUE(peek_gtp_version(layer.hdr.raw, layer.hdr_len) == 1);
|
||||
const struct gtp1_hdr *gtp1 = (const struct gtp1_hdr *)layer.hdr.raw;
|
||||
EXPECT_TRUE(peek_gtp_version(layer->hdr.raw, layer->hdr_len) == 1);
|
||||
const struct gtp1_hdr *gtp1 = (const struct gtp1_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(gtp1_hdr_get_msg_len(gtp1) == 45);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_IPV4)
|
||||
if (layer->proto == LAYER_PROTO_IPV4)
|
||||
{
|
||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
||||
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 45);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x4906);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1);
|
||||
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2);
|
||||
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
|
||||
@@ -719,32 +722,33 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_GRE_IP6_TCP)
|
||||
|
||||
EXPECT_TRUE(orig_pkt_len ==
|
||||
new_pkt_len - 5); // trim TCP payload
|
||||
struct layer layer;
|
||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
||||
int count = packet_get_layer_count(new_pkt);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV4)
|
||||
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV4)
|
||||
{
|
||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
||||
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 93);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xaec7);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_GRE)
|
||||
if (layer->proto == LAYER_PROTO_GRE)
|
||||
{
|
||||
const struct gre0_hdr *gre = (const struct gre0_hdr *)layer.hdr.raw;
|
||||
const struct gre0_hdr *gre = (const struct gre0_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(gre0_hdr_get_version(gre) == 0);
|
||||
EXPECT_TRUE(gre0_hdr_get_checksum(gre) == 0x92e7);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 25);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
|
||||
const struct tcphdr *tcp = (const struct tcphdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1);
|
||||
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2);
|
||||
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
|
||||
@@ -946,19 +950,20 @@ TEST(PACKET_BUILD_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS)
|
||||
|
||||
EXPECT_TRUE(orig_pkt_len - 71 == // trim DNS payload
|
||||
new_pkt_len - 5); // trim UDP payload
|
||||
int count = 0;
|
||||
struct layer layer;
|
||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
||||
int flag = 0;
|
||||
int count = packet_get_layer_count(new_pkt);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV6)
|
||||
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 73);
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_IPV4)
|
||||
if (layer->proto == LAYER_PROTO_IPV4)
|
||||
{
|
||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
||||
if (count == 0)
|
||||
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||
if (flag == 0)
|
||||
{
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 73);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x7640);
|
||||
@@ -968,17 +973,17 @@ TEST(PACKET_BUILD_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS)
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 33);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x56a9);
|
||||
}
|
||||
count++;
|
||||
flag++;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_GRE)
|
||||
if (layer->proto == LAYER_PROTO_GRE)
|
||||
{
|
||||
const struct gre1_hdr *gre = (const struct gre1_hdr *)layer.hdr.raw;
|
||||
const struct gre1_hdr *gre = (const struct gre1_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(gre1_hdr_get_version(gre) == 1);
|
||||
EXPECT_TRUE(gre1_hdr_get_payload_length(gre) == 37);
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_UDP)
|
||||
if (layer->proto == LAYER_PROTO_UDP)
|
||||
{
|
||||
const struct udphdr *udp = (const struct udphdr *)layer.hdr.raw;
|
||||
const struct udphdr *udp = (const struct udphdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(udp_hdr_get_total_len(udp) == 13);
|
||||
EXPECT_TRUE(udp_hdr_get_checksum(udp) == 0x5469);
|
||||
}
|
||||
@@ -1074,17 +1079,18 @@ TEST(PACKET_BUILD_L3, ETH_IP4_ICMP)
|
||||
|
||||
EXPECT_TRUE(orig_pkt_len - 20 - 6 == // trim Eth padding, trim TCP header
|
||||
new_pkt_len - 64); // trim ICMP
|
||||
struct layer layer;
|
||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
||||
int count = packet_get_layer_count(new_pkt);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV4)
|
||||
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV4)
|
||||
{
|
||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
||||
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 84);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xb7cb);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_ICMP)
|
||||
if (layer->proto == LAYER_PROTO_ICMP)
|
||||
{
|
||||
// TODO
|
||||
break;
|
||||
@@ -1142,12 +1148,13 @@ TEST(PACKET_BUILD_L3, ETH_IP6_ICMP)
|
||||
struct icmp6_hdr *icmp = (struct icmp6_hdr *)icmp_resp;
|
||||
icmp->icmp6_cksum = 0;
|
||||
|
||||
struct layer layer;
|
||||
PACKET_FOREACH_LAYER_REVERSE(&orig_pkt, layer)
|
||||
int count = packet_get_layer_count(&orig_pkt);
|
||||
for (int i = count - 1; i >= 0; i--)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV6)
|
||||
const struct layer *layer = packet_get_layer_by_idx(&orig_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
struct ip6_hdr *ip6 = (struct ip6_hdr *)layer.hdr.raw;
|
||||
struct ip6_hdr *ip6 = (struct ip6_hdr *)layer->hdr.raw;
|
||||
icmp->icmp6_cksum = checksum_v6(icmp, sizeof(icmp_resp), IPPROTO_ICMPV6, &ip6->ip6_src, &ip6->ip6_dst);
|
||||
break;
|
||||
}
|
||||
@@ -1169,22 +1176,24 @@ TEST(PACKET_BUILD_L3, ETH_IP6_ICMP)
|
||||
|
||||
EXPECT_TRUE(orig_pkt_len - 32 == // trim TCP header
|
||||
new_pkt_len - 60); // trim ICMPv6 header
|
||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
||||
count = packet_get_layer_count(new_pkt);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (layer.proto == LAYER_PROTO_IPV4)
|
||||
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||
if (layer->proto == LAYER_PROTO_IPV4)
|
||||
{
|
||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
||||
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 120);
|
||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x09ac);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
|
||||
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer->hdr.raw;
|
||||
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 60);
|
||||
break;
|
||||
}
|
||||
if (layer.proto == LAYER_PROTO_ICMP6)
|
||||
if (layer->proto == LAYER_PROTO_ICMP6)
|
||||
{
|
||||
// TODO
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user