diff --git a/include/stellar/layer.h b/include/stellar/layer.h index 6250481..9cf7e4a 100644 --- a/include/stellar/layer.h +++ b/include/stellar/layer.h @@ -70,26 +70,24 @@ struct layer }; int packet_get_layer_count(const struct packet *pkt); -// return 0: success  -// return -1: failed -int packet_get_layer_by_idx(const struct packet *pkt, int idx, struct layer *out); +const struct layer *packet_get_layer_by_idx(const struct packet *pkt, int idx); -#define PACKET_FOREACH_LAYER_INORDER(pkt, layer) \ - for (int i = 0; i < packet_get_layer_count(pkt) && packet_get_layer_by_idx(pkt, i, &layer) == 0; i++) - -#define PACKET_FOREACH_LAYER_REVERSE(pkt, layer) \ - for (int i = packet_get_layer_count(pkt) - 1; i >= 0 && packet_get_layer_by_idx(pkt, i, &layer) == 0; i--) - -#define PACKET_GETALL_LAYERS(pkt, layers) \ - ({ \ - memset(layers, 0, sizeof(layers)); \ - int size = sizeof(layers) / sizeof(layers[0]); \ - int count = packet_get_layer_count(pkt); \ - int num = count > size ? size : count; \ - for (int i = 0; i < num && packet_get_layer_by_idx(pkt, i, &layers[i]) == 0; i++) \ - /* void */; \ - num; \ - }) +// // example: foreach layer in packet (inorder) +// int count = packet_get_layer_count(pkt); +// for (int i = 0; i < count; i++) +// { +// const struct layer *layer = packet_get_layer_by_idx(pkt, i); +// // do something with layer +// } +// +// +// // example: foreach layer in packet (reverse) +// int count = packet_get_layer_count(pkt); +// for (int i = count - 1; i >= 0; i--) +// { +// const struct layer *layer = packet_get_layer_by_idx(pkt, i); +// // do something with layer +// } #ifdef __cplusplus } diff --git a/include/stellar/tunnel.h b/include/stellar/tunnel.h index 07d2339..17d1d75 100644 --- a/include/stellar/tunnel.h +++ b/include/stellar/tunnel.h @@ -33,7 +33,7 @@ struct tunnel enum tunnel_type type; int layer_count; - struct layer layers[MAX_LAYERS_PER_TUNNEL]; + const struct layer *layers[MAX_LAYERS_PER_TUNNEL]; }; int packet_get_tunnel_count(const struct packet *pkt); @@ -41,23 +41,6 @@ int packet_get_tunnel_count(const struct packet *pkt); // return -1: failed int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out); -#define PACKET_FOREACH_TUNNEL_INORDER(pkt, tunnel) \ - for (int i = 0; i < packet_get_tunnel_count(pkt) && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i++) - -#define PACKET_FOREACH_TUNNEL_REVERSE(pkt, tunnel) \ - for (int i = packet_get_tunnel_count(pkt) - 1; i >= 0 && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i--) - -#define PACKET_GETALL_TUNNELS(pkt, tunnels) \ - ({ \ - memset(tunnels, 0, sizeof(tunnels)); \ - int size = sizeof(tunnels) / sizeof(tunnels[0]); \ - int count = packet_get_tunnel_count(pkt); \ - int num = count > size ? size : count; \ - for (int i = 0; i < num && packet_get_tunnel_by_idx(pkt, i, &tunnels[i]) == 0; i++) \ - /* void */; \ - num; \ - }) - #ifdef __cplusplus } #endif diff --git a/src/packet/packet.cpp b/src/packet/packet.cpp index a9363a1..f581240 100644 --- a/src/packet/packet.cpp +++ b/src/packet/packet.cpp @@ -464,17 +464,16 @@ int packet_get_layer_count(const struct packet *pkt) return pkt->layers_used; } -int packet_get_layer_by_idx(const struct packet *pkt, int idx, struct layer *out) +const struct layer *packet_get_layer_by_idx(const struct packet *pkt, int idx) { - const struct layer_private *raw = packet_get_layer(pkt, idx); - if (raw == NULL) + const struct layer_private *layer = packet_get_layer(pkt, idx); + if (layer == NULL) { - return -1; + return NULL; } else { - layer_convert(raw, out); - return 0; + return (const struct layer *)layer; } } @@ -681,11 +680,11 @@ int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *o out->type = hit->type; out->layer_count = hit->contain_layers; if (out->layer_count >= 1) - layer_convert(curr, &out->layers[0]); + out->layers[0] = (const struct layer *)curr; if (out->layer_count >= 2) - layer_convert(next1, &out->layers[1]); + out->layers[1] = (const struct layer *)next1; if (out->layer_count >= 3) - layer_convert(next2, &out->layers[2]); + out->layers[2] = (const struct layer *)next2; return 0; } } @@ -922,15 +921,3 @@ int packet_is_fragment(const struct packet *pkt) { return (pkt->frag_layer) ? 1 : 0; } - -void layer_convert(const struct layer_private *in, struct layer *out) -{ - if (in == NULL || out == NULL) - { - return; - } - - out->proto = in->proto; - out->hdr_len = in->hdr_len; - out->hdr.raw = (char *)in->hdr_ptr; -} diff --git a/src/packet/packet_private.h b/src/packet/packet_private.h index e8a58f9..000fdb5 100644 --- a/src/packet/packet_private.h +++ b/src/packet/packet_private.h @@ -36,11 +36,14 @@ struct metadata struct layer_private { enum layer_proto proto; - const char *hdr_ptr; // header pointer - const char *pld_ptr; // payload pointer - uint16_t hdr_offset; // header offset from data_ptr + uint16_t hdr_len; // header length + const char *hdr_ptr; // header pointer + uint16_t pld_len; // payload length + const char *pld_ptr; // payload pointer + + uint16_t hdr_offset; // header offset from data_ptr }; struct packet @@ -140,7 +143,6 @@ struct packet *packet_dup(const struct packet *pkt); void packet_free(struct packet *pkt); int packet_is_fragment(const struct packet *pkt); -void layer_convert(const struct layer_private *in, struct layer *out); #ifdef __cplusplus } diff --git a/src/packet/test/gtest_packet_builder.cpp b/src/packet/test/gtest_packet_builder.cpp index 0b9a8e7..f57f2d4 100644 --- a/src/packet/test/gtest_packet_builder.cpp +++ b/src/packet/test/gtest_packet_builder.cpp @@ -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; diff --git a/src/packet/test/gtest_tunnel.cpp b/src/packet/test/gtest_tunnel.cpp index 7f1e266..30c7f75 100644 --- a/src/packet/test/gtest_tunnel.cpp +++ b/src/packet/test/gtest_tunnel.cpp @@ -555,8 +555,8 @@ TEST(TUNNEL, IPV4) EXPECT_TRUE(out.type == TUNNEL_IPV4); EXPECT_TRUE(out.layer_count == 1); - EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4); - EXPECT_TRUE(out.layers[0].hdr_len == 20); + EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4); + EXPECT_TRUE(out.layers[0]->hdr_len == 20); // No tunnel EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1); @@ -583,19 +583,19 @@ TEST(TUNNEL, GRE) EXPECT_TRUE(out.type == TUNNEL_IPV6); EXPECT_TRUE(out.layer_count == 1); - EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV6); - EXPECT_TRUE(out.layers[0].hdr_len == 40); + EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV6); + EXPECT_TRUE(out.layers[0]->hdr_len == 40); // GRE tunnel EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == 0); EXPECT_TRUE(out.type == TUNNEL_GRE); EXPECT_TRUE(out.layer_count == 2); - EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4); - EXPECT_TRUE(out.layers[0].hdr_len == 20); + EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4); + EXPECT_TRUE(out.layers[0]->hdr_len == 20); - EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_GRE); - EXPECT_TRUE(out.layers[1].hdr_len == 16); + EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_GRE); + EXPECT_TRUE(out.layers[1]->hdr_len == 16); // No tunnel EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 2, &out) == -1); @@ -617,14 +617,14 @@ TEST(TUNNEL, GTP) EXPECT_TRUE(out.type == TUNNEL_GTP); EXPECT_TRUE(out.layer_count == 3); - EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV6); - EXPECT_TRUE(out.layers[0].hdr_len == 40); + EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV6); + EXPECT_TRUE(out.layers[0]->hdr_len == 40); - EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP); - EXPECT_TRUE(out.layers[1].hdr_len == 8); + EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP); + EXPECT_TRUE(out.layers[1]->hdr_len == 8); - EXPECT_TRUE(out.layers[2].proto == LAYER_PROTO_GTP_U); - EXPECT_TRUE(out.layers[2].hdr_len == 8); + EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_GTP_U); + EXPECT_TRUE(out.layers[2]->hdr_len == 8); // No tunnel EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1); @@ -646,14 +646,14 @@ TEST(TUNNEL, VXLAN) EXPECT_TRUE(out.type == TUNNEL_VXLAN); EXPECT_TRUE(out.layer_count == 3); - EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4); - EXPECT_TRUE(out.layers[0].hdr_len == 20); + EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4); + EXPECT_TRUE(out.layers[0]->hdr_len == 20); - EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP); - EXPECT_TRUE(out.layers[1].hdr_len == 8); + EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP); + EXPECT_TRUE(out.layers[1]->hdr_len == 8); - EXPECT_TRUE(out.layers[2].proto == LAYER_PROTO_VXLAN); - EXPECT_TRUE(out.layers[2].hdr_len == 8); + EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_VXLAN); + EXPECT_TRUE(out.layers[2]->hdr_len == 8); // No tunnel EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1); @@ -675,14 +675,14 @@ TEST(TUNNEL, L2TP) EXPECT_TRUE(out.type == TUNNEL_L2TP); EXPECT_TRUE(out.layer_count == 3); - EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4); - EXPECT_TRUE(out.layers[0].hdr_len == 20); + EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4); + EXPECT_TRUE(out.layers[0]->hdr_len == 20); - EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP); - EXPECT_TRUE(out.layers[1].hdr_len == 8); + EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP); + EXPECT_TRUE(out.layers[1]->hdr_len == 8); - EXPECT_TRUE(out.layers[2].proto == LAYER_PROTO_L2TP); - EXPECT_TRUE(out.layers[2].hdr_len == 8); + EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_L2TP); + EXPECT_TRUE(out.layers[2]->hdr_len == 8); // No tunnel EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1); @@ -704,11 +704,11 @@ TEST(TUNNEL, TEREDO) EXPECT_TRUE(out.type == TUNNEL_TEREDO); EXPECT_TRUE(out.layer_count == 2); - EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4); - EXPECT_TRUE(out.layers[0].hdr_len == 20); + EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4); + EXPECT_TRUE(out.layers[0]->hdr_len == 20); - EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP); - EXPECT_TRUE(out.layers[1].hdr_len == 8); + EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP); + EXPECT_TRUE(out.layers[1]->hdr_len == 8); // No tunnel EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1); diff --git a/test/packet_inject/packet_inject.cpp b/test/packet_inject/packet_inject.cpp index c9fb4e3..7c5d50a 100644 --- a/test/packet_inject/packet_inject.cpp +++ b/test/packet_inject/packet_inject.cpp @@ -270,31 +270,32 @@ static inline void packet_exdata_init(const struct packet *pkt, enum flow_direct pkt_exdata->flow_dir = dir; int get_inner_addr = 0; - struct layer layer; - PACKET_FOREACH_LAYER_REVERSE(pkt, layer) + int count = packet_get_layer_count(pkt); + for (int i = count - 1; i >= 0; i--) { - switch (layer.proto) + const struct layer *layer = packet_get_layer_by_idx(pkt, i); + switch (layer->proto) { case LAYER_PROTO_TCP: - pkt_exdata->src_port = ntohs(layer.hdr.tcp->th_sport); - pkt_exdata->dst_port = ntohs(layer.hdr.tcp->th_dport); - pkt_exdata->tcp_seq = ntohl(layer.hdr.tcp->th_seq); - pkt_exdata->tcp_ack = ntohl(layer.hdr.tcp->th_ack); - pkt_exdata->tcp_flags = layer.hdr.tcp->th_flags; + pkt_exdata->src_port = ntohs(layer->hdr.tcp->th_sport); + pkt_exdata->dst_port = ntohs(layer->hdr.tcp->th_dport); + pkt_exdata->tcp_seq = ntohl(layer->hdr.tcp->th_seq); + pkt_exdata->tcp_ack = ntohl(layer->hdr.tcp->th_ack); + pkt_exdata->tcp_flags = layer->hdr.tcp->th_flags; pkt_exdata->tcp_payload_len = packet_get_payload_len(pkt); break; case LAYER_PROTO_UDP: - pkt_exdata->src_port = ntohs(layer.hdr.udp->uh_sport); - pkt_exdata->dst_port = ntohs(layer.hdr.udp->uh_dport); + pkt_exdata->src_port = ntohs(layer->hdr.udp->uh_sport); + pkt_exdata->dst_port = ntohs(layer->hdr.udp->uh_dport); break; case LAYER_PROTO_IPV4: - pkt_exdata->src_addr.v4 = layer.hdr.ip4->ip_src; - pkt_exdata->dst_addr.v4 = layer.hdr.ip4->ip_dst; + pkt_exdata->src_addr.v4 = layer->hdr.ip4->ip_src; + pkt_exdata->dst_addr.v4 = layer->hdr.ip4->ip_dst; get_inner_addr = 1; break; case LAYER_PROTO_IPV6: - pkt_exdata->src_addr.v6 = layer.hdr.ip6->ip6_src; - pkt_exdata->dst_addr.v6 = layer.hdr.ip6->ip6_dst; + pkt_exdata->src_addr.v6 = layer->hdr.ip6->ip6_src; + pkt_exdata->dst_addr.v6 = layer->hdr.ip6->ip6_dst; get_inner_addr = 1; break; default: