📄 hostap_filter.c
字号:
/** we only set filter to get certain dst mac and src mac's frame*/#include <stdio.h>#include <sys/types.h>#include "hostap_filter.h"#include "hostap_common.h"#include <pcap.h>//#include "ieee80211.common.h"//define filter#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"static filter_t filter;static cfg_block_t *cfg;static cfg_block_t *cfg_false;static cfg_block_t *cfg_true;void start_to_filter(char *pcap_file);//when input is 0xff, the choice is none//param: type is 0xff indicate no type selected//stype is 0xff, indicate all type's subtype is selectedint add_rules(u8 type, u8 stype, u8 *src_mac, u8 *dst_mac){ static int first_m = 1; static int first_c = 1; static int first_d = 1; static int set_src = 0; static int set_dst = 0; if (type == 0xff){ filter.type_num = 0; }else{ if (type == WLAN_FC_TYPE_MGMT){ if (first_m == 1){ filter.type_num++; first_m = 0; } if (stype == 0xff){ filter.mag_type = 0xff; }else{ filter.mag_type++; filter.mag_stype[filter.mag_type-1] = stype; } } if (type == WLAN_FC_TYPE_CTRL){ if (first_c == 1){ filter.type_num++; first_c = 0; } if (stype == 0xff){ filter.ctl_type = 0xff; }else{ filter.ctl_type++; filter.ctl_stype[filter.ctl_type-1] = stype; } } if (type == WLAN_FC_TYPE_DATA){ if (first_d == 1){ filter.type_num++; first_d = 0; } if (stype == 0xff){ filter.data_type = 0xff; }else{ filter.data_type++; filter.data_stype[filter.data_type-1] = stype; } } } if (src_mac != NULL){ set_src = 1; printf(MACSTR, MAC2STR(filter.src_mac)); memcpy(filter.src_mac, src_mac, 6); }else{ if (set_src == 0) memset(filter.src_mac, 0xff, 6); } if (dst_mac != NULL){ set_dst = 1; memcpy(filter.dst_mac, dst_mac, 6); }else{ if (set_dst == 0) memset(filter.dst_mac, 0xff, 6); } return 0;}int add_rule(u8 type, u8 *stype, u8 stype_num, u8 *src_mac, u8 *des_mac){ int i = 0; if (type == WLAN_FC_TYPE_NO){ filter.type_num = WLAN_FC_TYPE_NO; }else{ filter.type_num = type; filter.stype_num = stype_num; filter.stype_num_tmp = stype_num; for (i = 0; i < stype_num; i++){ filter.stype[i] = stype[i]; } } if (src_mac == NULL){ // memset(filter.src_mac, 0xff, 6); printf("src = NUL\n"); filter.has_src_mac = 0; }else{ printf("src != NULL\n"); filter.has_src_mac = 1; memcpy(filter.src_mac, src_mac, 6); } if (des_mac == NULL){ // memset(filter.dst_mac, 0xff, 6); filter.has_dst_mac = 0; }else{ filter.has_dst_mac = 1; memcpy(filter.dst_mac, des_mac, 6); } return 0;}void show_filter(){ int i = 0; printf("----------------------------show filter content------------------------\n"); printf("filter type num is %d\n", filter.type_num); if (filter.mag_type != 0x0){ printf("management frame has %d kinds of frame\n", filter.mag_type); } if (filter.ctl_type != 0x0){ printf("control frame has %d kinds of frame\n", filter.ctl_type); } if (filter.data_type != 0x0){ printf("data frame has %d kinds of frame\n", filter.data_type); } printf("-----------------output each type of frame-----------------------------\n"); for (i=0; i<filter.mag_type; i++){ printf("mamagement subtype%d's num is %d\n", i, filter.mag_stype[i]); } for (i=0; i<filter.ctl_type; i++){ printf("control subtype%d's num is %d\n", i, filter.ctl_stype[i]); } for (i=0; i<filter.ctl_type; i++){ printf("data subtype%d's num is %d\n", i, filter.data_stype[i]); } if (filter.src_mac == NULL){ printf("source mac is all\n"); }else{ printf("source mac is: \n"); printf(MACSTR, MAC2STR(filter.src_mac)); } if (filter.src_mac == NULL){ printf("destination mac is all\n"); }else{ printf("\ndestination mac is: \n"); printf(MACSTR, MAC2STR(filter.dst_mac)); }}void show_new_filter(){ int i = 0; printf("------------------------------------------\n"); printf("------------show filter-------------------\n"); printf("filter type is %d\n", filter.type_num); if (filter.type_num != WLAN_FC_TYPE_NO){ printf("filter subtype is :"); for(i = 0; i < filter.stype_num; i++){ printf("%d ", filter.stype[i]); } }else{ printf("no filter subtype\n"); } printf("\n");printf("has src == %d\n", filter.has_src_mac); if (filter.has_src_mac == 0){ printf("filter has no src_mac \n"); }else{ printf("filter src_mac is 0x"); for (i = 0; i < 6; i++){ printf("%0x ", filter.src_mac[i]); } printf("\n"); } if (filter.has_dst_mac == 0){ printf("filter has no dst_mac \n"); }else{ printf("filter dst_mac is 0x"); for (i = 0; i < 6; i++){ printf("%0x ", filter.dst_mac[i]); } printf("\n"); } printf("------------------------show filter end------------\n");}/*cft_block_t cfg_node_standard[] = { CFG_BLOCK_TYPE_TYPE, , BIT_TYPE, , , 2, 2, , , };*/static cfg_block_t frame_type = { CFG_BLOCK_TYPE_TYPE, BIT_TYPE, 0, 0, 2, 2, 0, 0, 0,};static cfg_block_t frame_stype = { CFG_BLOCK_TYPE_STYPE, BIT_TYPE, 0, 0, 4, 4, 0, 0, 0,};static cfg_block_t frame_mgmt_sa = { CFG_BLOCK_TYPE_SA, BYTE_TYPE, 4, 6, 0, 0, 0, 0, 0,};static cfg_block_t frame_mgmt_da = { CFG_BLOCK_TYPE_DA, BYTE_TYPE, 10, 6, 0, 0, 0, 0, 0,};//two cases, form ds and to dsstatic cfg_block_t frame_data_fromds_sa = { CFG_BLOCK_TYPE_SA, BYTE_TYPE, 16, 6, 0, 0, 0, 0, 0};static cfg_block_t frame_data_fromds_da = { CFG_BLOCK_TYPE_DA, BYTE_TYPE, 4, 6, 0, 0, 0, 0, 0};static cfg_block_t frame_data_tods_sa = { CFG_BLOCK_TYPE_SA, BYTE_TYPE, 10, 6, 0, 0, 0, 0, 0};static cfg_block_t frame_data_tods_da = { CFG_BLOCK_TYPE_DA, BYTE_TYPE, 16, 6, 0, 0, 0, 0, 0};#define LEFT_SON 1#define RIGHT_SON 2// father_node_type -1 indicates first node of cfg, left 1, right 2// each node type has a reference, so we copy this reference,and give it a value//this will be okint get_current_node_type(char father_node_type, u8 left_or_right_son){ switch (father_node_type){ case CFG_BLOCK_TYPE_TYPE: if (left_or_right_son == RIGHT_SON){ if (filter.has_src_mac == 1){ return CFG_BLOCK_TYPE_SA; } if (filter.has_dst_mac == 1){ return CFG_BLOCK_TYPE_DA; } if (filter.stype_num > 0){ filter.stype_num_tmp--; return CFG_BLOCK_TYPE_STYPE; } return CFG_BLOCK_TYPE_TRUE; }else{//left son return CFG_BLOCK_TYPE_FALSE; } break; case CFG_BLOCK_TYPE_SA: if (left_or_right_son == RIGHT_SON){ if (filter.has_dst_mac == 1){ return CFG_BLOCK_TYPE_DA; } if (filter.stype_num > 0){ filter.stype_num_tmp--; return CFG_BLOCK_TYPE_STYPE; } return CFG_BLOCK_TYPE_TRUE; }else{//left son return CFG_BLOCK_TYPE_FALSE; } break; case CFG_BLOCK_TYPE_DA: if (left_or_right_son == RIGHT_SON){ if (filter.stype_num > 0){ filter.stype_num_tmp--; return CFG_BLOCK_TYPE_STYPE; } return CFG_BLOCK_TYPE_TRUE; }else{//left son return CFG_BLOCK_TYPE_FALSE; } break; case CFG_BLOCK_TYPE_STYPE: if (left_or_right_son == RIGHT_SON){ return CFG_BLOCK_TYPE_TRUE; } //left son maybe has many stype nodes, so it may be complicated if (filter.stype_num_tmp > 0){ filter.stype_num_tmp--; return CFG_BLOCK_TYPE_STYPE; }else{ return CFG_BLOCK_TYPE_FALSE; } break; default://true or false, both return return -1; break; }}static total_node_num;void make_cfg_node(char father_node_type, u8 left_or_right_son, cfg_block_t *father_node){ if (father_node == NULL){ printf("father node is null\n"); } total_node_num++; printf("this is %d node\n", total_node_num); printf("father node type is %d\n", father_node_type); cfg_block_t *p_cfg_node = NULL; int current_node_type = 0; if (father_node_type == -1){ //get first node type printf("make first node\n"); p_cfg_node = (cfg_block_t *)malloc(sizeof(cfg_block_t)); cfg = p_cfg_node; if (filter.type_num == WLAN_FC_TYPE_NO){ if (filter.has_src_mac == 0){ if (filter.has_dst_mac == 0){ printf("filter has no rules\n"); return ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -