From daef36bc337d8f99d2e8eaa8f48fdec2a92a8b66 Mon Sep 17 00:00:00 2001 From: lijia Date: Tue, 26 Oct 2021 18:41:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0gtest=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/Makefile | 7 + test/gtest_jump_layer.cpp | 1185 ++++++++++++++++++++++++++++++ test/sample_pcap/tcp_simple.pcap | Bin 0 -> 114 bytes test/test_jump_layer.c | 111 ++- 4 files changed, 1284 insertions(+), 19 deletions(-) create mode 100644 test/gtest_jump_layer.cpp create mode 100644 test/sample_pcap/tcp_simple.pcap diff --git a/test/Makefile b/test/Makefile index af0990a..5d0dca0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,2 +1,9 @@ +all: test_jump_layer gtest_jump_layer + +CFLAGS = -g -Wall -fPIC -std=c++11 -DSAPP_V4=1 -D_XOPEN_SOURCE + test_jump_layer:test_jump_layer.c gcc -g -o $@ test_jump_layer.c -D_GNU_SOURCE -L/opt/MESA/lib -lMESA_jump_layer -I ../inc -I/opt/MESA/include/MESA -l pcap + +gtest_jump_layer:gtest_jump_layer.cpp + g++ -g -o $@ $^ $(CFLAGS) -D_GNU_SOURCE -L/opt/MESA/lib -lMESA_jump_layer -I ../inc -I/opt/MESA/include/MESA -lpcap -lgtest -lpthread diff --git a/test/gtest_jump_layer.cpp b/test/gtest_jump_layer.cpp new file mode 100644 index 0000000..9e4b7d1 --- /dev/null +++ b/test/gtest_jump_layer.cpp @@ -0,0 +1,1185 @@ +#ifndef __USE_BSD +#define __USE_BSD +#endif +#ifndef __FAVOR_BSD +#define __FAVOR_BSD +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* See NOTES */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "stream.h" +#include +#include +#include "MESA_jump_layer.h" + +#define GTEST_SAPP_ERR (-1) +#define GTEST_SAPP_SUCC 0 + +static void sendto_test_result(int n) +{ + +} + +static pcap_t *g_jmp_pcap_handle; + + +static int jump_check_ipv4_pkt(const struct ip *ip4hdr, int expect_tot_len, unsigned char expect_protocol, const char *expect_src_addr_str, const char *expect_dst_addr_str) +{ + unsigned int expect_src_addr_net, expect_dst_addr_net; + + /* 简单判断一下ipv4头部是否正确 */ + if(ip4hdr->ip_v != 4){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4 header check version error!\033[0m\n"); + return -1; + } + if(ip4hdr->ip_p != expect_protocol){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4 header check protocol error!\033[0m\n"); + return -1; + } + if(ip4hdr->ip_hl*4 < 20){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4 header check header length error!\033[0m\n"); + return -1; + } + if(ntohs(ip4hdr->ip_len) != expect_tot_len){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4 header check length error!\033[0m\n"); + return -1; + } + + inet_pton(AF_INET, expect_src_addr_str, &expect_src_addr_net); + if(ip4hdr->ip_src.s_addr != expect_src_addr_net){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4 header check src addr error!\033[0m\n"); + return -1; + } + inet_pton(AF_INET, expect_dst_addr_str, &expect_dst_addr_net); + if(ip4hdr->ip_dst.s_addr != expect_dst_addr_net){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4 header check dst addr error!\033[0m\n"); + return -1; + } + + return 0; +} + + +static int jump_check_ipv6_pkt(const struct ip6_hdr *ip6h, int expect_payload_len, unsigned char expect_protocol, const char *expect_src_addr_str, const char *expect_dst_addr_str) +{ + if((*(char *)ip6h & 0xF0) != 0x60){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6 header check version error!\033[0m\n"); + return -1; + } + if(ip6h->ip6_nxt != expect_protocol){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6 header check protocol error!\033[0m\n"); + return -1; + } + + if(ntohs(ip6h->ip6_plen) != expect_payload_len){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6 header check length error!\033[0m\n"); + return -1; + } + + struct in6_addr tmp_ip6_addr; + inet_pton(AF_INET6, expect_src_addr_str, &tmp_ip6_addr); + if(memcmp(&tmp_ip6_addr, &ip6h->ip6_src, sizeof(struct in6_addr) ) != 0){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6 header check src addr error!\033[0m\n"); + return -1; + } + + inet_pton(AF_INET6, expect_dst_addr_str, &tmp_ip6_addr); + if(memcmp(&tmp_ip6_addr, &ip6h->ip6_dst, sizeof(struct in6_addr) ) != 0){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6 header check dst addr error!\033[0m\n"); + return -1; + } + + return 0; +} + +static int jump_check_tcp_pkt(const struct tcphdr *thdr, int expect_tcp_hdr_offset, unsigned short expect_sport_host, unsigned short expect_dport_host) +{ + if(thdr->doff * 4 != expect_tcp_hdr_offset){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): tcp header offset error!\033[0m\n"); + return -1; + } + if(ntohs(thdr->source) != expect_sport_host){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): tcp header sport error!\033[0m\n"); + return -1; + } + if(ntohs(thdr->dest) != expect_dport_host){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): tcp header dport error!\033[0m\n"); + return -1; + } + + return 0; +} + +static int jump_check_udp_pkt(const struct udphdr *uhdr, unsigned short expect_udp_tot_len, unsigned short expect_sport_host, unsigned short expect_dport_host) +{ + if(ntohs(uhdr->len) != expect_udp_tot_len){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): udp header length error!\033[0m\n"); + return -1; + } + + if(ntohs(uhdr->source) != expect_sport_host){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): udp header sport error!\033[0m\n"); + return -1; + } + if(ntohs(uhdr->dest) != expect_dport_host){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): udp header dport error!\033[0m\n"); + return -1; + } + + return 0; +} + +static int jump_check_vlan_pkt(const void *vlan_hdr, unsigned int expect_vlan_hex_data) +{ + if(memcmp(vlan_hdr, &expect_vlan_hex_data, sizeof(int)) != 0){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): vlan header error!\033[0m\n"); + return -1; + } + + return 0; +} + + + +static int jmp_pcap_init(const char *pcap_file, pcap_handler pcap_cb_fun, u_char *result_val) +{ + char err_string[PCAP_ERRBUF_SIZE]; + + g_jmp_pcap_handle = pcap_open_offline(pcap_file, err_string); + if(NULL == g_jmp_pcap_handle){ + printf("open file:%s error, %s\n", pcap_file, err_string); + return -1; + } + + pcap_loop(g_jmp_pcap_handle, -1, pcap_cb_fun, (u_char *)result_val); + + return 0; +} + + +static int jmp_file_md5_checksum(const char *filename, const char *expect_md5sum) +{ + FILE *cmd_res_fp; + char file_real_md5sum[33]; + char cmd_buf[1024]; + int ret = 0; + + snprintf(cmd_buf, 1024, "%s %s", "md5sum", filename); + + cmd_res_fp = popen(cmd_buf, "r"); + if(NULL == cmd_res_fp){ + return -1; + } + + fgets(file_real_md5sum, 33, cmd_res_fp); + + if(strncasecmp(file_real_md5sum, expect_md5sum, 32) != 0){ + printf("file md5:%s, but expect md5:%s\n", file_real_md5sum, expect_md5sum); + ret = -1; + } + + fclose(cmd_res_fp); + + return ret; +} + +static void jump_layer_eth_ipv4_tcp(u_char *result_val, const struct pcap_pkthdr *hdr, const u_char *data) +{ + int ret; + int tot_pkt_len; + const void *eth_header, *ipv4_header, *tcp_header; + const void *next_header; + + eth_header = (void *)data; + + ipv4_header = MESA_net_jump_to_layer(eth_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == ipv4_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4 error!\033[0m\n"); + *result_val = -1; + pcap_breakloop(g_jmp_pcap_handle); + } + + const struct ip* ip4hdr = (struct ip*)ipv4_header; + jump_check_ipv4_pkt(ip4hdr, tot_pkt_len-sizeof(struct ethhdr), IPPROTO_TCP, "192.168.10.250", "192.168.10.234"); + + tcp_header = MESA_net_jump_to_layer((void *)ipv4_header, ADDR_TYPE_IPV4, ADDR_TYPE_TCP); + if(NULL == tcp_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4->tcp error!\033[0m\n"); + *result_val = -1; + pcap_breakloop(g_jmp_pcap_handle); + } + + tcp_header = MESA_net_jump_to_layer(eth_header, ADDR_TYPE_MAC, ADDR_TYPE_TCP); + if(NULL == tcp_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->tcp error!\033[0m\n"); + *result_val = -1; + pcap_breakloop(g_jmp_pcap_handle); + } + + tcp_header = MESA_net_jump_to_layer(ipv4_header, __ADDR_TYPE_IP_PAIR_V4, ADDR_TYPE_TCP); + if(NULL == tcp_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4->tcp error!\033[0m\n"); + *result_val = -1; + pcap_breakloop(g_jmp_pcap_handle); + } + + const struct tcphdr* thdr = (struct tcphdr *)tcp_header; + jump_check_tcp_pkt(thdr, 40, 58725, 22); + + printf("\033[32mjump_layer_eth_ipv4_tcp_entry() test succ\033[0m\n"); + + *result_val = 0; + + pcap_breakloop(g_jmp_pcap_handle); +} + +TEST(jump_layer, eth_ipv4_tcp) +{ + int fun_ret, chk_res = -1; + + fun_ret = jmp_file_md5_checksum("./sample_pcap/tcp_simple.pcap", "eb4b176720c698e86c1acbacf2f30ede"); + ASSERT_EQ(fun_ret, 0); + + fun_ret = jmp_pcap_init("./sample_pcap/tcp_simple.pcap", jump_layer_eth_ipv4_tcp, (u_char *)&chk_res); + ASSERT_EQ(fun_ret, 0); + + ASSERT_EQ(chk_res, 0); +} + +#if 0 + +extern "C" char jump_layer_eth_ipv4_udp_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->opstate == OP_STATE_PENDING){ + next_header = MESA_net_jump_to_layer((void *)a_packet, ADDR_TYPE_IPV4, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + if(next_header != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ip header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct ip* ip4hdr = (struct ip*)next_header; + + jump_check_ipv4_pkt(ip4hdr, tot_pkt_len-sizeof(struct ethhdr), IPPROTO_UDP, "192.168.210.153", "111.161.107.181"); + + next_header = MESA_net_jump_to_layer((void *)ip4hdr, __ADDR_TYPE_IP_PAIR_V4, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct udphdr* uhdr = (struct udphdr *)next_header; + jump_check_udp_pkt(uhdr, 155, 4001, 8000); + + printf("\033[32mjump_layer_eth_ipv4_udp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ipv6_tcp_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->pktstate == OP_STATE_PENDING){ + next_header = MESA_net_jump_to_layer((void *)a_packet, ADDR_TYPE_IPV6, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv6_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv6_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV6); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv6 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + if(next_header != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ip header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct ip6_hdr* ip6h = (struct ip6_hdr *)next_header; + + jump_check_ipv6_pkt(ip6h, tot_pkt_len - sizeof(struct ethhdr) - sizeof(struct ip6_hdr), IPPROTO_TCP, "2001::192:168:40:134", "2001::192:168:40:133"); + + next_header = MESA_net_jump_to_layer((void *)ip6h, __ADDR_TYPE_IP_PAIR_V6, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct tcphdr* thdr = (struct tcphdr *)next_header; + jump_check_tcp_pkt(thdr, 40, 37948, 22); + + printf("\033[32mjump_layer_eth_ipv6_tcp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ipv6_udp_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->opstate == OP_STATE_PENDING){ + next_header = MESA_net_jump_to_layer((void *)a_packet, ADDR_TYPE_IPV6, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv6->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv6_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv6_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV6); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv6 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + if(next_header != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ip6 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct ip6_hdr* ip6h = (struct ip6_hdr*)next_header; + jump_check_ipv6_pkt(ip6h, tot_pkt_len - sizeof(struct ethhdr) - sizeof(struct ip6_hdr), IPPROTO_UDP, "2001::192:168:40:134", "2001::192:168:40:133"); + + next_header = MESA_net_jump_to_layer((void *)ip6h, __ADDR_TYPE_IP_PAIR_V6, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): ipv4->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct udphdr* uhdr = (struct udphdr *)next_header; + jump_check_udp_pkt(uhdr, 10, 33225, 31337); + + printf("\033[32mjump_layer_eth_ipv6_udp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ipv4_gtp_ipv4_tcpall_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->pktstate == OP_STATE_PENDING){ + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ipv4_gtp_ipv4_tcpall_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ipv4_gtp_ipv4_tcpall_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); /* 外层ipv4 */ + if(NULL == next_header){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): eth->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* outer_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(outer_ip4hdr, 76, IPPROTO_UDP, "10.166.20.10", "10.2.3.35"); + + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_GPRS_TUNNEL); /* gtp层 */ + if(NULL == next_header){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): eth->ipv4->gtp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct gtp_hdr *net_gtp_hdr = (struct gtp_hdr *)next_header; + if(net_gtp_hdr->flags != 0x30){ + printf("\033[1;31;40m gtp hdr flags is not 0x30!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + if(net_gtp_hdr->msg_type != 0xFF){ + printf("\033[1;31;40m gtp hdr msg_type is not 0xFF!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + if(ntohs(net_gtp_hdr->len) != 40){ + printf("\033[1;31;40m gtp hdr length is not 40!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + if(ntohl(net_gtp_hdr->teid) != 0xb68311b){ + printf("\033[1;31;40m gtp hdr teid is not 0xb68311b!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); /* 内层ipv4 */ + if(NULL == next_header){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): eth->ipv4->gtp->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* inner_ip4hdr = (struct ip*)next_header; + if(next_header != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): inner ip header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + jump_check_ipv4_pkt(inner_ip4hdr, 40, IPPROTO_TCP, "10.58.121.62", "217.76.78.112"); + + + next_header = MESA_net_jump_to_layer((void *)pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->gtp->ipv4->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct tcphdr* thdr = (struct tcphdr *)next_header; + jump_check_tcp_pkt(thdr, 20, 52144, 443); + + printf("\033[32mjump_layer_eth_ipv4_gtp_ipv4_tcpall_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ipv4_pptp_ipv4_tcpall_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->pktstate == OP_STATE_PENDING){ + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* outer_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(outer_ip4hdr, 88, IPPROTO_GRE, "172.16.0.100", "172.16.0.254"); + + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->pptp->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* inner_ip4hdr = (struct ip*)next_header; + if(next_header != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): inner ip header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + jump_check_ipv4_pkt(inner_ip4hdr, 52, IPPROTO_TCP, "172.16.2.100", "10.0.6.229"); + + + next_header = MESA_net_jump_to_layer((void *)pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->pptp->ipv4->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct tcphdr* thdr = (struct tcphdr *)next_header; + jump_check_tcp_pkt(thdr, 32, 50072, 80); + + printf("\033[32mjump_layer_eth_ipv4_pptp_ipv4_tcpall_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + +extern "C" char jump_layer_eth_ip4_l2tp_ip4_tcpall_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->pktstate == OP_STATE_PENDING){ + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* outer_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(outer_ip4hdr, 92, IPPROTO_UDP, "172.16.0.100", "172.16.0.254"); + + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_L2TP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->l2tp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct l2tp_hdr_v2 *p_l2tp_hdr = (struct l2tp_hdr_v2 *)next_header; + if(p_l2tp_hdr->length_present != 1){ + printf("\033[1;31;40mMESA_net_jump_to_layer():l2tp header length flag is not 1!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + if(p_l2tp_hdr->version != 2){ + printf("\033[1;31;40mMESA_net_jump_to_layer():l2tp header version is not 2!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const unsigned short *short_ptr = (unsigned short *)((char *)next_header + sizeof(struct l2tp_hdr_v2)); + if(ntohs(*short_ptr) != 64){ + printf("\033[1;31;40mMESA_net_jump_to_layer():l2tp header lenth is not 64!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + short_ptr++; + if(ntohs(*short_ptr) != 28998){ + printf("\033[1;31;40mMESA_net_jump_to_layer():l2tp header tunnel id is not 28998!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + short_ptr++; + if(ntohs(*short_ptr) != 2){ + printf("\033[1;31;40mMESA_net_jump_to_layer():l2tp header session id is not 2!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + /* 跳转到内层ipv4需要用greedy接口 */ + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->l2tp->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* inner_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(inner_ip4hdr, 52, IPPROTO_TCP, "172.16.2.100", "10.0.6.229"); + + if(inner_ip4hdr != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): inner ip4 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->l2tp->ipv4->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct tcphdr* thdr = (struct tcphdr *)next_header; + jump_check_tcp_pkt(thdr, 32, 49250, 80); + + printf("\033[32mjump_layer_eth_ip4_l2tp_ip4_tcpall_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ip4_l2tp_ip4_udp_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret, opt_len; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + unsigned short tunnel_type = STREAM_TUNNLE_NON; + + if(pstream->opstate == OP_STATE_PENDING){ + + opt_len = sizeof(short); + ret = MESA_get_stream_opt(pstream, MSO_STREAM_TUNNEL_TYPE, &tunnel_type, &opt_len); + if((ret < 0) || (tunnel_type != STREAM_TUNNLE_L2TP)){ + return APP_STATE_DROPME; /* l2tp隧道外层udp流, 不处理 */ + } + + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + /* 跳转到内层ipv4需要用greedy接口 */ + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->l2tp->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* inner_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(inner_ip4hdr, 66, IPPROTO_UDP, "172.16.2.100", "8.8.8.8"); + + if(inner_ip4hdr != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): inner ip4 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->l2tp->ipv4->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct udphdr* uhdr = (struct udphdr *)next_header; + jump_check_udp_pkt(uhdr, 46, 53483, 53); + + printf("\033[32m jump_layer_eth_ip4_l2tp_ip4_udp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ip4_l2tp_ppp_compress_ip4_udp_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + int opt_len; + unsigned short tunnel_type = STREAM_TUNNLE_NON; + + if(pstream->pktstate == OP_STATE_PENDING){ + opt_len = sizeof(short); + ret = MESA_get_stream_opt(pstream, MSO_STREAM_TUNNEL_TYPE, &tunnel_type, &opt_len); + if((ret < 0) || (tunnel_type != STREAM_TUNNLE_L2TP)){ + return APP_STATE_DROPME; /* l2tp隧道外层udp流, 不处理 */ + } + + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ip4_l2tp_ppp_compress_ip4_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ip4_l2tp_ppp_compress_ip4_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + /* 跳转到内层ipv4需要用greedy接口 */ + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): eth->ipv4->l2tp->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* inner_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(inner_ip4hdr, 1378, IPPROTO_UDP, "10.24.0.36", "64.233.185.99"); + + if(inner_ip4hdr != a_packet){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): inner ip4 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->udp->l2tp->ipv4->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct udphdr* uhdr = (struct udphdr *)next_header; + jump_check_udp_pkt(uhdr, 1358, 52139, 443); + + printf("\033[32m jump_layer_eth_ip4_l2tp_ppp_compress_ip4_udp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ipv4_gre_ipv4_gre_ipv4_udp_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->opstate == OP_STATE_PENDING){ + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* outer_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(outer_ip4hdr, 124, IPPROTO_GRE, "115.153.75.246", "122.112.114.66"); + + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->gre->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* inner_ip4hdr = (struct ip*)next_header; + if(inner_ip4hdr != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): inner ip header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + jump_check_ipv4_pkt(inner_ip4hdr, 72, IPPROTO_UDP, "122.112.114.73", "115.238.253.235"); + + next_header = MESA_net_jump_to_layer((void *)pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->gre->ipv4->gre->ipv4->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct udphdr* uhdr = (struct udphdr *)next_header; + jump_check_udp_pkt(uhdr, 52, 20755, 53); + + printf("\033[32mjump_layer_eth_ipv4_gre_ipv4_gre_ipv4_udp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_vlan_ip6_ip4_pptp_ip4_ip6_tcpall_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + + if(pstream->pktstate == OP_STATE_PENDING){ + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40mjump_layer_eth_ipv4_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_VLAN); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->vlan error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + jump_check_vlan_pkt(next_header, htonl(0x006486DD)); + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV6); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->vlan->ipv6 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + jump_check_ipv6_pkt((const ip6_hdr *)next_header, 1412, 4, "2607:fcd0:100:2300::b108:2a6b", "2402:f000:1:8e01::5555"); + + next_header = MESA_net_jump_to_layer(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->vlan->ipv6->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* outer_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(outer_ip4hdr, 1412, IPPROTO_GRE, "192.52.166.154", "16.0.0.200"); + + /* 跳转到内层ipv4需要用greedy接口 */ + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->vlan->ipv6->ipv4->pptp->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip* inner_ip4hdr = (struct ip*)next_header; + jump_check_ipv4_pkt(inner_ip4hdr, 1376, IPPROTO_TCP, "173.194.72.18", "172.16.44.3"); + + if(inner_ip4hdr != a_packet){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): inner ip4 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->vlan->ipv6->ipv4->pptp->ipv4->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct tcphdr* thdr = (struct tcphdr *)next_header; + jump_check_tcp_pkt(thdr, 20, 443, 56461); + + printf("\033[32mjump_layer_eth_vlan_ip6_ip4_pptp_ip4_ip6_tcpall_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ip4_udp_teredo_udp_ip6_tcpall_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + int opt_len; + unsigned short tunnel_type = STREAM_TUNNLE_NON; + + if(pstream->pktstate == OP_STATE_PENDING){ + opt_len = sizeof(short); + ret = MESA_get_stream_opt(pstream, MSO_STREAM_TUNNEL_TYPE, &tunnel_type, &opt_len); + if((ret < 0) || (tunnel_type != STREAM_TUNNLE_TEREDO)){ + return APP_STATE_DROPME; /*teredo隧道外层udp流, 不处理 */ + } + + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ip4_udp_teredo_udp_ip6_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ip4_udp_teredo_udp_ip6_tcp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + /* 跳转到内层ipv6需要用greedy接口 */ + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV6); + if(NULL == next_header){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): eth->ipv4->teredo->ipv6 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip6_hdr* inner_ip6hdr = (struct ip6_hdr*)next_header; + jump_check_ipv6_pkt(inner_ip6hdr, 489, IPPROTO_TCP, "2001:0:4137:9E76:2463:F227:88D7:DABE", "2001:252:0:2::2000"); + + if(inner_ip6hdr != a_packet){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): inner ip6 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->udp->teredo->ipv6->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct tcphdr* thdr = (struct tcphdr *)next_header; + jump_check_tcp_pkt(thdr, 20, 63622, 80); + + printf("\033[32m jump_layer_eth_ip4_udp_teredo_udp_ip6_tcp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + + +extern "C" char jump_layer_eth_ip4_udp_teredo_udp_ip6_udp_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + int opt_len; + unsigned short tunnel_type = STREAM_TUNNLE_NON; + + if(pstream->opstate == OP_STATE_PENDING){ + opt_len = sizeof(short); + ret = MESA_get_stream_opt(pstream, MSO_STREAM_TUNNEL_TYPE, &tunnel_type, &opt_len); + if((ret < 0) || (tunnel_type != STREAM_TUNNLE_TEREDO)){ + return APP_STATE_DROPME; /*teredo隧道外层udp流, 不处理 */ + } + + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ip4_udp_teredo_udp_ip6_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_ip4_udp_teredo_udp_ip6_udp_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + /* 跳转到内层ipv6需要用greedy接口 */ + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV6); + if(NULL == next_header){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): eth->ipv4->teredo->ipv6 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip6_hdr* inner_ip6hdr = (struct ip6_hdr*)next_header; + jump_check_ipv6_pkt(inner_ip6hdr, 56, IPPROTO_UDP, "FE80::8000:F227:3EFF:FFFE", "FE80::FFFF:FFFF:FFFF"); + + if(inner_ip6hdr != a_packet){ + printf("\033[1;31;40m MESA_net_jump_to_layer(): inner ip6 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_UDP); + if(NULL == next_header){ + printf("\033[1;31;40mMESA_net_jump_to_layer(): eth->ipv4->udp->teredo->ipv6->udp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct udphdr* uhdr = (struct udphdr *)next_header; + jump_check_udp_pkt(uhdr, 56, 32768, 20480); + + printf("\033[32m jump_layer_eth_ip4_udp_teredo_udp_ip6_udp_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} + +extern "C" char jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry(struct streaminfo *pstream,void **pme, int thread_seq, void *a_packet) +{ + int ret; + int tot_pkt_len; + const void *pkt_header; + const void *next_header; + int opt_len; + unsigned short tunnel_type = STREAM_TUNNLE_NON; + + if(pstream->pktstate == OP_STATE_PENDING){ + opt_len = sizeof(short); + ret = MESA_get_stream_opt(pstream, MSO_STREAM_TUNNEL_TYPE, &tunnel_type, &opt_len); + if((ret < 0) || (tunnel_type != STREAM_TUNNEL_GPRS_TUNNEL)){ + return APP_STATE_DROPME; + } + + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_DATA, &pkt_header); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_DATA error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + ret = get_rawpkt_opt_from_streaminfo(pstream, RAW_PKT_GET_TOT_LEN, &tot_pkt_len); + if(ret < 0){ + printf("\033[1;31;40m jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry(), get_rawpkt_opt_from_streaminfo()->RAW_PKT_GET_TOT_LEN error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV6); + if(NULL == next_header){ + printf("\033[1;31;40m jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry(): eth->vlan->ipv6 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + const struct ip6_hdr* inner_ip6hdr = (struct ip6_hdr*)next_header; + jump_check_ipv6_pkt(inner_ip6hdr, 64, IPPROTO_UDP, "2409:8034:4025::1:941", "2409:8034:4040:5300::205"); + + + const struct ip * inner_ip4hdr; + inner_ip4hdr = ( struct ip *)MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_IPV4); + if(NULL == next_header){ + printf("\033[1;31;jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry(): eth->ipv6->udp->gtp->ipv4 error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + if(inner_ip4hdr != a_packet){ + printf("\033[1;31;40m jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry(): inner ip4 header is not equal with plug_entry->a_packet!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + next_header = MESA_net_jump_to_layer_greedy(pkt_header, ADDR_TYPE_MAC, ADDR_TYPE_TCP); + if(NULL == next_header){ + printf("\033[1;31;jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry(): eth->ipv6->udp->gtp->ipv6->tcp error!\033[0m\n"); + sendto_test_result(GTEST_SAPP_ERR); + exit(1); + } + + const struct tcphdr* thdr = (struct tcphdr *)next_header; + jump_check_tcp_pkt(thdr, 20, 47892, 80); + + printf("\033[32m jump_layer_eth_vlan_ip6_udp_gtpext_ip4_tcpall_entry() test succ\033[0m\n"); + sendto_test_result(GTEST_SAPP_SUCC); + } + + return APP_STATE_DROPME; +} +#endif + +int main(int argc, char *argv[]) +{ + ::testing::InitGoogleTest(&argc, argv); + + int ret=RUN_ALL_TESTS(); + + return ret; +} + diff --git a/test/sample_pcap/tcp_simple.pcap b/test/sample_pcap/tcp_simple.pcap new file mode 100644 index 0000000000000000000000000000000000000000..d38a61283adfde7eebe25f16d0700a1e40177aab GIT binary patch literal 114 zcmca|c+)~A1{MYcU}0bcawaXUi21UWnZXOl24Mz+6z_caplen-offset_to_eth, hdr->caplen-offset_to_eth))){ - goto done; + printf("-----------------------------packet index:%d------------------------------------------\n", pkt_index++); + if(ip4_hdr){ + offset_to_eth = (u_char *)ip4_hdr-data; + if(g_input_bpf_string + && (0 == bpf_filter(g_bpf_filter.bf_insns, (const unsigned char *)ip4_hdr, hdr->caplen-offset_to_eth, hdr->caplen-offset_to_eth))){ + goto done; + } + printf("Innermost layer ipv4 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv4_ntop((struct ip *)ip4_hdr, print_buf, sizeof(print_buf))); } - printf("Innermost layer ipv4 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv4_ntop((struct ip *)ip4_hdr, print_buf, sizeof(print_buf))); - } - - if(ip6_h){ - offset_to_eth = (u_char *)ip6_h-data; - if(g_input_bpf_string - && (0 == bpf_filter(g_bpf_filter.bf_insns, (const unsigned char *)ip6_h, hdr->caplen-offset_to_eth, hdr->caplen-offset_to_eth))){ - goto done; + + if(ip6_h){ + offset_to_eth = (u_char *)ip6_h-data; + if(g_input_bpf_string + && (0 == bpf_filter(g_bpf_filter.bf_insns, (const unsigned char *)ip6_h, hdr->caplen-offset_to_eth, hdr->caplen-offset_to_eth))){ + goto done; + } + + printf("Innermost layer ipv6 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv6_ntop((struct ip6_hdr *)ip6_h, print_buf, sizeof(print_buf))); } + + done: + + printf("--------------------------------------------------------------------------------------\n\n"); - printf("Innermost layer ipv6 offset:%d, addr: %s\n", offset_to_eth, MESA_jump_layer_ipv6_ntop((struct ip6_hdr *)ip6_h, print_buf, sizeof(print_buf))); - } +} -done: +static void _pcap_pkt_handle(u_char *user, const struct pcap_pkthdr *hdr, const u_char *data) +{ + + _jump_from_ethernet(data); + + _jump_greedy(hdr, data); - printf("--------------------------------------------------------------------------------------\n\n"); } static void pcap_run(void)