summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test/Makefile7
-rw-r--r--test/gtest_jump_layer.cpp1185
-rw-r--r--test/sample_pcap/tcp_simple.pcapbin0 -> 114 bytes
-rw-r--r--test/test_jump_layer.c111
4 files changed, 1284 insertions, 19 deletions
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 <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <netinet/ip.h>
+#include <netinet/tcp.h>
+#include <netinet/udp.h>
+#include <assert.h>
+#include <time.h>
+#include <arpa/inet.h>
+#include <sys/types.h> /* See NOTES */
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <errno.h>
+#include <linux/if_tun.h>
+#include <net/if.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/limits.h>
+#include <linux/if_ether.h>
+#include <pthread.h>
+#include "stream.h"
+#include <gtest/gtest.h>
+#include <pcap/pcap.h>
+#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 0000000..d38a612
--- /dev/null
+++ b/test/sample_pcap/tcp_simple.pcap
Binary files differ
diff --git a/test/test_jump_layer.c b/test/test_jump_layer.c
index 3cdd4fe..6d09b62 100644
--- a/test/test_jump_layer.c
+++ b/test/test_jump_layer.c
@@ -11,6 +11,9 @@ static char *g_input_pcap_name;
static char *g_input_bpf_string;
static struct bpf_program g_bpf_filter;
+static int g_raw_input_layer_type = (int )ADDR_TYPE_MAC;
+static int g_expect_layer_type = (int )ADDR_TYPE_IPV4;
+
static void usage(const char *prog)
{
printf("Usage:\n");
@@ -59,39 +62,109 @@ static int pcap_init(void)
return 0;
}
-static void _pcap_pkt_handle(u_char *user, const struct pcap_pkthdr *hdr, const u_char *data)
+static void _jump_from_udp(const void *uhdr)
+{
+ const void *next_hdr;
+
+ next_hdr = MESA_jump_layer(uhdr, ADDR_TYPE_UDP, ADDR_TYPE_GPRS_TUNNEL);
+ if(next_hdr){
+ printf("bingo! jump to gtp from udp succ!\n");
+ }
+
+ next_hdr = MESA_jump_layer(uhdr, ADDR_TYPE_UDP, ADDR_TYPE_L2TP);
+ if(next_hdr){
+ printf("bingo! jump to l2tp from udp succ!\n");
+ }
+}
+
+
+static void _jump_from_ipv4(const void *ip4_hdr)
+{
+ const void *next_hdr;
+
+ next_hdr = MESA_jump_layer(ip4_hdr, ADDR_TYPE_IPV4, ADDR_TYPE_TCP);
+ if(next_hdr){
+ printf("bingo! jump to tcp from ipv4 succ!\n");
+ }
+
+ next_hdr = MESA_jump_layer(ip4_hdr, ADDR_TYPE_IPV4, ADDR_TYPE_UDP);
+ if(next_hdr){
+ printf("bingo! jump to udp from ipv4 succ!\n");
+ _jump_from_udp(next_hdr);
+ }
+}
+
+static void _jump_from_ipv6(const void *ip4_hdr)
+{
+
+}
+
+
+
+static void _jump_from_ethernet(const void *ehdr)
+{
+ const void *next_hdr;
+
+ next_hdr = MESA_jump_layer(ehdr, ADDR_TYPE_MAC, ADDR_TYPE_IPV4);
+ if(next_hdr){
+ printf("bingo! jump to ipv4 from ethernet succ!\n");
+ _jump_from_ipv4(next_hdr);
+ }
+
+ next_hdr = MESA_jump_layer(ehdr, ADDR_TYPE_MAC, ADDR_TYPE_IPV6);
+ if(next_hdr){
+ printf("bingo! jump to ipv6 from ethernet succ!\n");
+ _jump_from_ipv6(next_hdr);
+ }
+
+}
+
+
+static void _jump_greedy(const struct pcap_pkthdr *hdr, const u_char *data)
{
const void *ip4_hdr, *ip6_h;
+
char print_buf[128];
int offset_to_eth;
static int pkt_index = 0;
ip4_hdr = MESA_jump_layer_greedy(data, ADDR_TYPE_MAC, ADDR_TYPE_IPV4);
ip6_h = MESA_jump_layer_greedy(data, ADDR_TYPE_MAC, ADDR_TYPE_IPV6);
+
- 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("-----------------------------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)