Began use git manage source code since MAAT_FRAME_VERSION_1_2_20150724.

This commit is contained in:
zhengchao
2015-10-10 18:30:12 +08:00
commit 40f0ed7320
40 changed files with 8423 additions and 0 deletions

14
Makefile Normal file
View File

@@ -0,0 +1,14 @@
#opt: OPTFLAGS = -O2
#export OPTFLAGS
.PHONY: all clean opt
all:
cd src/entry/ && $(MAKE)
cd test && $(MAKE)
clean:
cd src/entry/ && $(MAKE) clean
cd test && $(MAKE) clean
opt:
$(MAKE) all

177
inc/Maat_rule.h Normal file
View File

@@ -0,0 +1,177 @@
/*
*****************Maat Network Flow Rule Manage Framework********
* Maat is the Goddess of truth and justice in ancient Egyptian concept.
* Her feather was the measure that determined whether the souls (considered
* to reside in the heart) of the departed would reach the paradise of afterlife
* successfully.
* Author: zhengchao@iie.ac.cn,MESA
* Version 2015-07-24 Maat_set_feather_opt
* NOTE: MUST compile with G++
* All right reserved by Institute of Infomation Engineering,Chinese Academic of Science 2014~2018
*********************************************************
*/
#ifndef H_MAAT_RULE_H_INCLUDE
#define H_MAAT_RULE_H_INCLUDE
#include "stream.h"
enum MAAT_CHARSET
{
CHARSET_NONE=0,
CHARSET_GBK,
CHARSET_BIG5,
CHARSET_UNICODE,
CHARSET_UTF8, // 4
CHARSET_BIN //5
};
enum MAAT_ACTION
{
MAAT_ACTION_BLOCK=0,
MAAT_ACTION_MONIT,
MAAT_ACTION_WHITE
};
enum MAAT_POS_TYPE
{
MAAT_POSTYPE_EXPR=0,
MAAT_POSTYPE_REGEX
};
typedef void* scan_status_t;
typedef void* stream_para_t;
typedef void* Maat_feather_t;
#define MAX_SERVICE_DEFINE_LEN 128
struct Maat_rule_t
{
int config_id;
int service_id;
char do_log;
char do_blacklist;
char action;
char resevered;
int serv_def_len;
char service_defined[MAX_SERVICE_DEFINE_LEN];
};
#define MAAT_RULE_UPDATE_TYPE_FULL 1
#define MAAT_RULE_UPDATE_TYPE_INC 2
typedef void Maat_start_callback_t(int update_type,void* u_para);
typedef void Maat_update_callback_t(int table_id,const char* table_line,void* u_para);
typedef void Maat_finish_callback_t(void* u_para);
//--------------------HITTING DETAIL DESCRIPTION BEGIN
#define MAAT_MAX_HIT_RULE_NUM 8
#define MAAT_MAX_EXPR_ITEM_NUM 8
#define MAAT_MAX_HIT_POS_NUM 8
#define MAAT_MAX_REGEX_GROUP_NUM 8
//NOTE position buffer as hitting_regex_pos and hit_pos,are ONLY valid before next scan or Maat_stream_scan_string_end
struct regex_pos_t
{
int group_num;
int hitting_regex_len;
const char* hitting_regex_pos;
int grouping_len[MAAT_MAX_REGEX_GROUP_NUM];
const char* grouping_pos[MAAT_MAX_REGEX_GROUP_NUM];
};
struct str_pos_t
{
int hit_len;
const char* hit_pos;
};
struct sub_item_pos_t
{
enum MAAT_POS_TYPE ruletype;
int hit_cnt;
union
{
struct regex_pos_t regex_pos[MAAT_MAX_HIT_POS_NUM];
struct str_pos_t substr_pos[MAAT_MAX_HIT_POS_NUM];
};
};
struct Maat_region_pos_t
{
int region_id;
int sub_item_num;
struct sub_item_pos_t sub_item_pos[MAAT_MAX_EXPR_ITEM_NUM];
};
struct Maat_hit_detail_t
{
int config_id;//set <0 if half hit;
int hit_region_cnt;
struct Maat_region_pos_t region_pos[MAAT_MAX_HIT_RULE_NUM];
};
//--------------------HITTING DETAIL DESCRIPTION END
Maat_feather_t Maat_summon_feather(int max_thread_num,
const char* table_info_path,
const char* ful_cfg_dir,
const char* inc_cfg_dir,
void*logger);
Maat_feather_t Maat_summon_feather_json(int max_thread_num,
const char* table_info_path,
const char* json_rule,
void* logger);
void Maat_burn_feather(Maat_feather_t feather);
//return table_id(>=0) if success,otherwise return -1;
int Maat_table_register(Maat_feather_t feather,const char* table_name);
//return 1 if success,otherwise return -1 incase invalid table_id or registed function number exceed 32;
int Maat_table_callback_register(Maat_feather_t feather,short table_id,
Maat_start_callback_t *start,//MAAT_RULE_UPDATE_TYPE_*,u_para
Maat_update_callback_t *update,//table line ,u_para
Maat_finish_callback_t *finish,//u_para
void* u_para);
//Return hit rule number, return -1 when error occurs,return -2 when hit current region
//mid MUST set NULL before fist call
int Maat_scan_intval(Maat_feather_t feather,int table_id
,unsigned int intval
,struct Maat_rule_t*result,int rule_num
,scan_status_t *mid,int thread_num);
int Maat_scan_addr(Maat_feather_t feather,int table_id
,struct ipaddr* addr
,struct Maat_rule_t*result,int rule_num
,scan_status_t *mid,int thread_num);
int Maat_scan_proto_addr(Maat_feather_t feather,int table_id
,struct ipaddr* addr,unsigned short int proto
,struct Maat_rule_t*result,int rule_num
,scan_status_t *mid,int thread_num);
int Maat_full_scan_string(Maat_feather_t feather,int table_id
,enum MAAT_CHARSET charset,const char* data,int data_len
,struct Maat_rule_t*result,int* found_pos,int rule_num
,scan_status_t* mid,int thread_num);
//hite_detail could be NULL if unconcern
int Maat_full_scan_string_detail(Maat_feather_t feather,int table_id
,enum MAAT_CHARSET charset,const char* data,int data_len
,struct Maat_rule_t*result,int rule_num,struct Maat_hit_detail_t *hit_detail,int detail_num
,int* detail_ret,scan_status_t* mid,int thread_num);
stream_para_t Maat_stream_scan_string_start(Maat_feather_t feather,int table_id,int thread_num);
int Maat_stream_scan_string(stream_para_t* stream_para
,enum MAAT_CHARSET charset,const char* data,int data_len
,struct Maat_rule_t*result,int* found_pos,int rule_num
,scan_status_t* mid);
//hite_detail could be NULL if unconcern
int Maat_stream_scan_string_detail(stream_para_t* stream_para
,enum MAAT_CHARSET charset,const char* data,int data_len
,struct Maat_rule_t*result,int rule_num,struct Maat_hit_detail_t *hit_detail,int detail_num
,int* detail_ret,scan_status_t* mid);
void Maat_stream_scan_string_end(stream_para_t* stream_para);
void Maat_clean_status(scan_status_t* mid);
#define MAAT_OPT_SCANDIR_INTERVAL_MS 1 //opt value is interger,size=sizeof(int),1,000 milliseconds as default
#define MAAT_OPT_EFFECT_INVERVAL_MS 2 //opt value is interger,size=sizeof(int),60,000 milliseconds as default
//return -1 if failed, return 0 on success;
int Maat_set_feather_opt(Maat_feather_t feather,int type,void* value,int size);
#endif // H_MAAT_RULE_H_INCLUDE

35
readme.txt Normal file
View File

@@ -0,0 +1,35 @@
Author:Zheng Chao zhengchao@iie.ac.cn
2014-04-01 create this project;
2014-08-19 first online version;
2014-09-09 support expr offset;
2014-09-26 add group function;
2014-12-03 support regex grouping;
2014-12-17 write some comments in Maat_rule.h;
2014-12-22 add group_num in compile config table;
2015-01-04 make string match not case sensitive,fix garbage_bagging malloc wrong size bug;
2015-01-07 replace assert of database rule format,unescape '&' of EXPR_TYPE_OFFSET;
2015-01-22 ouput IRIS index file format error;verify string scan input data
and data len;
2015-01-28 enfore parameter check in Maat_stream_scan_string_detail to
optimize performance;
2015-02-04 support stream scan data with offset;
2015-02-20 add JSON config mode and add a demo;
2015-03-02 when use iconv_convert from gbk to big5,automaticly alternate gbk to
gb2312;
2015-04-13 1)Maat_xx_scan will return 0 if table has no config instead of -1;
2)use my_scandir as replacement of glibc's scandir to adapt dictator
malloc wrap;
3)if iconv take no effect,jump over this string;
4)compile shortcut for performance;
5)verify if region id is unique;
2015-04-20 fix Maat_stream_scan_string_detail and Maat_stream_scan_string wrong
data len when open cross caching;
2015-04-21 fix add_group_to_compile bug on compatible to none-group mode;
2015-04-29 fix false hit compile rule of one more region bug;
2015-05-06 fix add_group_to_compile return add failed status bug;
2015-05-07 1)use rwlock instead of mutex in _mi_rule;
2)fix inc postpone update bug;
2015-05-21 1)addapt rulescan ip addr host order requirement;
2) use readdir_r in my_scandir;
3) fix segmentfault when have no config,again;
4) maat_json support plugin table;

3130
src/entry/Maat_rule.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,288 @@
#include "Maat_rule.h"
#include <MESA/MESA_htable.h>
#include <MESA/MESA_list_queue.h>
#include "dynamic_array.h"
#include "UniversalBoolMatch.h"
#include "rulescan.h"
#include <pthread.h>
#include <iconv.h>
#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 411)
#define atomic_inc(x) __sync_add_and_fetch((x),1)
#define atomic_dec(x) __sync_sub_and_fetch((x),1)
#define atomic_add(x,y) __sync_add_and_fetch((x),(y))
#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y))
typedef int atomic_t;
#define ATOMIC_INIT(i) { (i) }
#define atomic_read(x) __sync_add_and_fetch((x),0)
#define atomic_set(x,y) __sync_lock_test_and_set((x),y)
#else
#include <alsa/iatomic.h>
#endif
#define CPU_CACHE_ALIGMENT 64
#define TRUE 1
#define FALSE 0
#define MAX_TABLE_NUM 256
#define MAX_CHARSET_NUM 6
#define MAX_TABLE_NAME_LEN 256
#define MAX_TABLE_LINE_SIZE (1024*4)
#define MAX_EXPR_KEYLEN 1024
#define MAX_PLUGING_NUM 32
#define MAX_SCANNER_HIT_NUM 64
#define MAX_GROUP_CACHE 128
#define MAX_FAILED_NUM 128
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
typedef void* rule_scanner_t;
enum MAAT_TABLE_TYPE
{
TABLE_TYPE_EXPR=0,
TABLE_TYPE_IP,
TABLE_TYPE_COMPILE,
TABLE_TYPE_PLUGIN,
TABLE_TYPE_INTVAL,
TABLE_TYPE_GROUP
};
enum MAAT_EXPR_TYPE
{
EXPR_TYPE_STRING=0,
EXPR_TYPE_AND,
EXPR_TYPE_REGEX,
EXPR_TYPE_OFFSET
};
enum MAAT_MATCH_METHOD
{
MATCH_METHOD_SUB=0,
MATCH_METHOD_RIGHT,
MATCH_METHOD_LEFT,
MATCH_METHOD_FULL
};
struct db_str_rule_t
{
int region_id;
int group_id;
char keywords[MAX_EXPR_KEYLEN];
enum MAAT_EXPR_TYPE expr_type;
enum MAAT_MATCH_METHOD match_method;
int is_hexbin;
int is_case_sensitive;
int is_valid;
};
struct db_ip_rule_t
{
int region_id;
int group_id;
int addr_type;
union
{
//ip address use network order
//port use host order
ipv4_rule_t ipv4_rule;
ipv6_rule_t ipv6_rule;
};
int is_valid;
};
struct db_intval_rule_t
{
int region_id;
int group_id;
interval_rule_t intval;
int is_valid;
};
struct _head_Maat_rule_t
{
int config_id;
int service_id;
char do_log;
char do_blacklist;
char action;
char resevered;
int serv_def_len;
};
struct db_compile_rule_t
{
// Maat_rule_t m_rule_head;
struct _head_Maat_rule_t m_rule_head;// fix len of Maat_rule_t
char* service_defined;
long long effective_range;
int is_valid;
int declare_grp_num;
};
struct db_group_rule_t
{
int group_id;
int compile_id;
int is_valid;
};
struct op_expr_t
{
boolean_expr_t* p_expr;
scan_rule_t* p_rules[MAAT_MAX_EXPR_ITEM_NUM];
};
struct _Maat_region_rule_t
{
int region_id;
int expr_id;
enum MAAT_TABLE_TYPE region_type;
};
struct _Maat_group_rule_t
{
int group_id;
int region_boundary;
int region_cnt;
int ref_cnt;
dynamic_array_t *region_rules;
void* compile_shortcut;
pthread_mutex_t mutex;
};
struct _Maat_compile_rule_t
{
struct db_compile_rule_t *db_c_rule;
dynamic_array_t *groups;
int group_cnt;
int compile_id;//equal to db_c_rule->m_rule.config_id
pthread_rwlock_t rwlock;//reading compile rule is safe in update thread, rwlock lock called when delete or scan thread read
};
struct _compile_result_t
{
int compile_id;
universal_bool_expr_t group_set;
};
struct _callback_plugin
{
Maat_start_callback_t *start;
Maat_update_callback_t *update;
Maat_finish_callback_t *finish;
void* u_para;
};
struct _plugin_table_info
{
int cb_plug_cnt;
struct _callback_plugin cb_plug[MAX_PLUGING_NUM];
dynamic_array_t *cache_lines;
int line_num;
long cache_size;
};
struct _Maat_table_info_t
{
unsigned short table_id;
char table_name[MAX_TABLE_NAME_LEN];
enum MAAT_TABLE_TYPE table_type;
enum MAAT_CHARSET src_charset;
enum MAAT_CHARSET dst_charset[MAX_CHARSET_NUM];
int src_charset_in_dst;
int do_charset_merge;
int cfg_num;
int cross_cache_size;
int expr_rule_cnt; //expr_type=0,1,3
int regex_rule_cnt; //expr_type=2
struct _plugin_table_info *cb_info;
};
struct _scan_status_t
{
struct _Maat_feather_t* feather;
int thread_num;
int cur_hit_cnt;
int hit_group_cnt;
int hit_group_size;
unsigned int cur_hit_id[MAX_SCANNER_HIT_NUM];
unsigned int *hitted_group_id;
};
enum maat_garbage_type
{
GARBAGE_SCANNER=0,
GARBAGE_GROUP_RULE,
GARBAGE_COMPILE_RULE,
GARBAGE_BOOL_MATCHER
};
struct iconv_handle_t
{
int is_initialized;
iconv_t cd;
};
struct _stream_para_t
{
struct _Maat_feather_t* feather;
int version;
int thread_num;
int max_cross_size;
int caching_size;
unsigned short table_id;
char do_merge;
char do_expr:4;
char do_regex:4;
char* last_cache;
char* scan_buff;
void* rs_stream_para;
long acc_scan_len;
};
struct _Maat_scanner_t
{
int version;
time_t last_update_time;
int *ref_cnt; //optimized for cache_alignment 64
rule_scanner_t region;
MESA_htable_handle region_hash;
MESA_htable_handle group_hash;
MESA_htable_handle compile_hash;
unsigned int cfg_num;
unsigned int exprid_generator;
MESA_lqueue_head region_update_q;
void * expr_compiler;
scan_result_t *region_rslt_buff;
MESA_lqueue_head tomb_ref;//reference of feather->garbage_q
int max_thread_num;
iconv_t iconv_handle[MAX_CHARSET_NUM][MAX_CHARSET_NUM];//iconv_handle[to][from]
};
struct _Maat_feather_t
{
struct _Maat_scanner_t *scanner;
struct _Maat_scanner_t *update_tmp_scanner;
MESA_lqueue_head garbage_q;
int table_cnt;
struct _Maat_table_info_t *p_table_info[MAX_TABLE_NUM];
MESA_htable_handle map_tablename2id;
void* logger;
int maat_version;
int scan_thread_num;
char inc_dir[MAX_TABLE_NAME_LEN];
char full_dir[MAX_TABLE_NAME_LEN];
int GROUP_MODE_ON;
int still_working;
int scan_interval_ms;
int effect_interval_ms;
};
struct _maat_garbage_t
{
enum maat_garbage_type type;
time_t create_time;
int ok_times;
union
{
struct _Maat_scanner_t* scanner;
struct _Maat_group_rule_t* group_rule;
struct _Maat_compile_rule_t* compile_rule;
void* bool_matcher;
void * raw;
};
};
void garbage_bagging(enum maat_garbage_type type,void *p,MESA_lqueue_head garbage_q);
void garbage_bury(MESA_lqueue_head garbage_q,void *logger);

37
src/entry/Makefile Normal file
View File

@@ -0,0 +1,37 @@
#opt: OPTFLAGS = -O2
#export OPTFLAGS
CC = g++
CCC = g++
CFLAGS = -Wall -g -fPIC
CFLAGS += $(OPTFLAGS)
LDFLAGS = -lMESA_handle_logger -lMESA_htable -lpthread
MAILLIB = ../lib
G_H_DIR =../inc_internal
H_DIR =-I$(G_H_DIR) -I../../inc
LIBMAAT = libmaatframe.a
LIBMAAT_SO = libmaatframe.so
OBJS=config_monitor.o Maat_rule.o UniversalBoolMatch.o dynamic_array.o cJSON.o json2iris.o map_str2int.o
.c.o:
$(CC) -c $(CFLAGS) -I. $(H_DIR) $<
.cpp.o:
$(CCC) -c $(CFLAGS) -I. $(H_DIR) $<
all: $(LIBMAAT) $(LIBMAAT_SO)
$(LIBMAAT_SO): $(OBJS)
$(CCC) -o $(LIBMAAT_SO) -shared $(OBJS) $(LDFLAGS)
cp $(LIBMAAT_SO) ../../lib/
$(LIBMAAT): $(OBJS)
echo making dynamic lib ...
ar cqs $(LIBMAAT) $(OBJS)
cp $(LIBMAAT) ../../lib/
clean:
rm -rf *.o $(LIBMAAT) *~
opt:
$(MAKE) all

View File

@@ -0,0 +1,259 @@
#include "UniversalBoolMatch.h"
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
static const unsigned int MAX_ARRAY_SIZE=65536;
struct thread_local_data_t
{
unsigned int mapped_ids[MAX_ARRAY_SIZE];
unsigned int used_cells[MAX_ARRAY_SIZE];
unsigned char * bitmap;
unsigned int * matched_bitmap;
};
struct boolexpr_matcher_t
{
unsigned int max_thread_num;
unsigned int bool_expr_num;
unsigned int multi_expr_num;
void ** bool_expr_ids;
unsigned char * multi_expr_size;
unsigned int bool_item_id_num;
unsigned int min_item_id;
unsigned int max_item_id;
unsigned int * bool_item_ids;
unsigned int * mapped_ptr;
unsigned int * mapped_ids;
unsigned int theta;
unsigned int L[65537];
thread_local_data_t * thread_data;
};
void * boolexpr_initialize(universal_bool_expr_t * bool_exprs, unsigned int bool_expr_num, unsigned int max_thread_num, unsigned int * mem_size)
{
if(bool_exprs==NULL || bool_expr_num==0 || max_thread_num==0) return NULL;
for(unsigned int i=0; i<bool_expr_num; i++)
{
if(bool_exprs[i].bool_item_num==0 || bool_exprs[i].bool_item_num>MAX_ITEMS_PER_BOOL_EXPR)
{
return NULL;
}
}
int I=-1, J=(int)bool_expr_num;
while(I<J)
{
I++;
while(I<J && bool_exprs[I].bool_item_num>1) I++;
if(I==J) break;
J--;
while(J>I && bool_exprs[J].bool_item_num==1) J--;
if(J==I) break;
swap(bool_exprs[I], bool_exprs[J]);
}
for(int k=0; k<(int)bool_expr_num; k++)
{
if((k<I && bool_exprs[k].bool_item_num==1) || (k>=I && bool_exprs[k].bool_item_num>1))
{
printf("[%s:%d]: fatal error!\n", __FILE__, __LINE__);
return NULL;
}
}
unsigned int mem_bytes=0;
boolexpr_matcher_t * matcher=new boolexpr_matcher_t;
mem_bytes+=sizeof(boolexpr_matcher_t);
matcher->max_thread_num=max_thread_num;
matcher->bool_expr_num=bool_expr_num;
matcher->multi_expr_num=I;
matcher->bool_expr_ids=new void *[bool_expr_num];
mem_bytes+=bool_expr_num*sizeof(void *);
matcher->multi_expr_size=new unsigned char[matcher->multi_expr_num+1];
mem_bytes+=(matcher->multi_expr_num+1)*sizeof(unsigned char);
matcher->thread_data=new thread_local_data_t[max_thread_num];
mem_bytes+=max_thread_num*sizeof(thread_local_data_t);
for(unsigned int i=0; i<max_thread_num; i++)
{
matcher->thread_data[i].bitmap=new unsigned char[matcher->multi_expr_num+1];
mem_bytes+=(matcher->multi_expr_num+1)*sizeof(unsigned char);
unsigned int size=(bool_expr_num-matcher->multi_expr_num);
size=(size>>5)+1;
matcher->thread_data[i].matched_bitmap=new unsigned int[size];
mem_bytes+=size*sizeof(unsigned int);
}
map< unsigned int, vector<unsigned int> > M;
unsigned int count=0;
for(unsigned int i=0; i<bool_expr_num; i++)
{
matcher->bool_expr_ids[i] =bool_exprs[i].bool_expr_id;
if(i<matcher->multi_expr_num)
{
matcher->multi_expr_size[i]=bool_exprs[i].bool_item_num;
}
count+=bool_exprs[i].bool_item_num;
for(unsigned int j=0; j<bool_exprs[i].bool_item_num; j++)
{
M[bool_exprs[i].bool_item_ids[j]].push_back((i<<3)|j);
}
}
matcher->bool_item_id_num=(unsigned int)M.size();
matcher->bool_item_ids=new unsigned int[M.size()];
matcher->mapped_ptr =new unsigned int[M.size()+1];
matcher->mapped_ids =new unsigned int[count];
mem_bytes+=(2*(unsigned int)M.size()+1+count)*sizeof(unsigned int);
matcher->mapped_ptr[0]=0;
map< unsigned int, vector<unsigned int> >::const_iterator it=M.begin();
for(unsigned int k=0; k<M.size(); ++k, ++it)
{
matcher->bool_item_ids[k]=it->first;
copy(it->second.begin(), it->second.end(), matcher->mapped_ids+matcher->mapped_ptr[k]);
matcher->mapped_ptr[k+1]=matcher->mapped_ptr[k]+(unsigned int)it->second.size();
}
matcher->min_item_id=matcher->bool_item_ids[0];
matcher->max_item_id=matcher->bool_item_ids[M.size()-1];
for(unsigned int k=0; k<M.size(); ++k)
{
matcher->bool_item_ids[k]-=matcher->min_item_id;
}
unsigned long long ONE=1;
unsigned int theta=0;
while((ONE<<(theta+16))<=matcher->bool_item_ids[M.size()-1]) theta++;
matcher->theta=theta;
matcher->L[0]=0;
for(unsigned int i=1; i<65536; i++)
{
matcher->L[i]=(unsigned int)(lower_bound(matcher->bool_item_ids, matcher->bool_item_ids+M.size(), i*(1U<<theta))-matcher->bool_item_ids);
}
matcher->L[65536]=(unsigned int)M.size();
M.clear();
*mem_size=mem_bytes;
return matcher;
}
int boolexpr_match(void * instance, unsigned int thread_id, unsigned int * item_ids, unsigned int item_num, void ** result, unsigned int size)
{
if(instance==NULL) return -1;
boolexpr_matcher_t * matcher=(boolexpr_matcher_t *)instance;
if(thread_id>=matcher->max_thread_num) return -1;
unsigned int * mapped_ids=matcher->thread_data[thread_id].mapped_ids;
unsigned int ids_num=0;
for(unsigned int i=0; i<item_num; i++)
{
if(item_ids[i]<matcher->min_item_id || item_ids[i]>matcher->max_item_id) continue;
unsigned int id=item_ids[i]-matcher->min_item_id;
unsigned int k=id>>matcher->theta;
int l=matcher->L[k], h=(int)matcher->L[k+1]-1;
if(h<l) continue;
while(l<=h)
{
int m=(l+h)/2;
if(id<matcher->bool_item_ids[m]) h=m-1;
else l=m+1;
}
if(h<(int)matcher->L[k] || matcher->bool_item_ids[h]!=id) continue;
for(unsigned int j=matcher->mapped_ptr[h]; j<matcher->mapped_ptr[h+1]; j++)
{
if(ids_num==MAX_ARRAY_SIZE) return -1;
mapped_ids[ids_num++]=matcher->mapped_ids[j];
}
}
unsigned int * used_cells=matcher->thread_data[thread_id].used_cells;
unsigned int used_num=0;
for(unsigned int i=0; i<ids_num; i++)
{
if(used_num==MAX_ARRAY_SIZE) return -1;
used_cells[used_num++]=(mapped_ids[i]>>3);
}
unsigned char * bitmap=matcher->thread_data[thread_id].bitmap;
unsigned int * matched_bitmap=matcher->thread_data[thread_id].matched_bitmap;
for(unsigned int i=0; i<used_num; i++)
{
if(used_cells[i]<matcher->multi_expr_num)
{
bitmap[used_cells[i]]=0;
}
else
{
unsigned int j=used_cells[i]-matcher->multi_expr_num;
matched_bitmap[j>>5]&=~(1U<<(j&31));
}
}
unsigned int r=0;
for(unsigned int i=0; i<ids_num; i++)
{
unsigned int x=(mapped_ids[i]>>3);
if(x<matcher->multi_expr_num)
{
unsigned int y=(mapped_ids[i]&7);
if((bitmap[x]&(1U<<y))==0)
{
bitmap[x]|=(1U<<y);
if(bitmap[x]==(1U<<matcher->multi_expr_size[x])-1)
{
if(r<size) result[r++]=matcher->bool_expr_ids[x];
}
}
}
else
{
unsigned int j=x-matcher->multi_expr_num;
if((matched_bitmap[j>>5]&(1U<<(j&31)))==0)
{
if(r<size) result[r++]=matcher->bool_expr_ids[x];
matched_bitmap[j>>5]|=(1U<<(j&31));
}
}
}
return r;
}
void boolexpr_destroy(void * instance)
{
if(instance!=NULL)
{
boolexpr_matcher_t * matcher=(boolexpr_matcher_t *)instance;
delete [] matcher->bool_expr_ids;
delete [] matcher->multi_expr_size;
delete [] matcher->bool_item_ids;
delete [] matcher->mapped_ptr;
delete [] matcher->mapped_ids;
for(unsigned int i=0; i<matcher->max_thread_num; i++)
{
delete [] matcher->thread_data[i].bitmap;
}
delete [] matcher->thread_data;
delete matcher;
}
}

View File

@@ -0,0 +1,46 @@
/*
*
* Copyright (c) 2014
* String Algorithms Research Group
* Institute of Information Engineering, Chinese Academy of Sciences (IIE-CAS)
* National Engineering Laboratory for Information Security Technologies (NELIST)
* All rights reserved
*
* Written by: LIU YANBING (liuyanbing@iie.ac.cn)
* Last modification: 2014-12-09
*
* This code is the exclusive and proprietary property of IIE-CAS and NELIST.
* Usage for direct or indirect commercial advantage is not allowed without
* written permission from the authors.
*
*/
#ifndef H_UNIVERSAL_BOOL_MATCH_H
#define H_UNIVERSAL_BOOL_MATCH_H
#ifdef __cplusplus
extern "C"
{
#endif
#define MAX_ITEMS_PER_BOOL_EXPR 8
typedef struct _universal_bool_expr_t
{
void * bool_expr_id;
unsigned int bool_item_num;
unsigned int bool_item_ids[MAX_ITEMS_PER_BOOL_EXPR];
}universal_bool_expr_t;
/*ע<><EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ûύ<C3BB><E1BDBB>bool_exprs<72><73>Ԫ<EFBFBD>ص<EFBFBD>λ<EFBFBD><CEBB>*/
void * boolexpr_initialize(universal_bool_expr_t * bool_exprs, unsigned int bool_expr_num, unsigned int max_thread_num, unsigned int * mem_size);
int boolexpr_match(void * instance, unsigned int thread_id, unsigned int * item_ids, unsigned int item_num, void ** result, unsigned int size);
void boolexpr_destroy(void * instance);
#ifdef __cplusplus
}
#endif
#endif

596
src/entry/cJSON.c Normal file
View File

@@ -0,0 +1,596 @@
/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* cJSON */
/* JSON parser in C. */
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"
static const char *ep;
const char *cJSON_GetErrorPtr(void) {return ep;}
static int cJSON_strcasecmp(const char *s1,const char *s2)
{
if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
}
static void *(*cJSON_malloc)(size_t sz) = malloc;
static void (*cJSON_free)(void *ptr) = free;
static char* cJSON_strdup(const char* str)
{
size_t len;
char* copy;
len = strlen(str) + 1;
if (!(copy = (char*)cJSON_malloc(len))) return 0;
memcpy(copy,str,len);
return copy;
}
void cJSON_InitHooks(cJSON_Hooks* hooks)
{
if (!hooks) { /* Reset hooks */
cJSON_malloc = malloc;
cJSON_free = free;
return;
}
cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
}
/* Internal constructor. */
static cJSON *cJSON_New_Item(void)
{
cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
if (node) memset(node,0,sizeof(cJSON));
return node;
}
/* Delete a cJSON structure. */
void cJSON_Delete(cJSON *c)
{
cJSON *next;
while (c)
{
next=c->next;
if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
if (c->string) cJSON_free(c->string);
cJSON_free(c);
c=next;
}
}
/* Parse the input text to generate a number, and populate the result into item. */
static const char *parse_number(cJSON *item,const char *num)
{
double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
if (*num=='-') sign=-1,num++; /* Has sign? */
if (*num=='0') num++; /* is zero */
if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */
if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
if (*num=='e' || *num=='E') /* Exponent? */
{ num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */
}
n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
item->valuedouble=n;
item->valueint=(int)n;
item->type=cJSON_Number;
return num;
}
/* Render the number nicely from the given item into a string. */
static char *print_number(cJSON *item)
{
char *str;
double d=item->valuedouble;
if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
{
str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
if (str) sprintf(str,"%d",item->valueint);
}
else
{
str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */
if (str)
{
if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d);
else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
else sprintf(str,"%f",d);
}
}
return str;
}
static unsigned parse_hex4(const char *str)
{
unsigned h=0;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
return h;
}
/* Parse the input text into an unescaped cstring, and populate item. */
static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
static const char *parse_string(cJSON *item,const char *str)
{
const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
if (*str!='\"') {ep=str;return 0;} /* not a string! */
while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */
if (!out) return 0;
ptr=str+1;ptr2=out;
while (*ptr!='\"' && *ptr)
{
if (*ptr!='\\') *ptr2++=*ptr++;
else
{
ptr++;
switch (*ptr)
{
case 'b': *ptr2++='\b'; break;
case 'f': *ptr2++='\f'; break;
case 'n': *ptr2++='\n'; break;
case 'r': *ptr2++='\r'; break;
case 't': *ptr2++='\t'; break;
case 'u': /* transcode utf16 to utf8. */
uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */
if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */
if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */
{
if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */
uc2=parse_hex4(ptr+3);ptr+=6;
if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */
uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
}
len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
switch (len) {
case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 1: *--ptr2 =(uc | firstByteMark[len]);
}
ptr2+=len;
break;
default: *ptr2++=*ptr; break;
}
ptr++;
}
}
*ptr2=0;
if (*ptr=='\"') ptr++;
item->valuestring=out;
item->type=cJSON_String;
return ptr;
}
/* Render the cstring provided to an escaped version that can be printed. */
static char *print_string_ptr(const char *str)
{
const char *ptr;char *ptr2,*out;int len=0;unsigned char token;
if (!str) return cJSON_strdup("");
ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
out=(char*)cJSON_malloc(len+3);
if (!out) return 0;
ptr2=out;ptr=str;
*ptr2++='\"';
while (*ptr)
{
if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
else
{
*ptr2++='\\';
switch (token=*ptr++)
{
case '\\': *ptr2++='\\'; break;
case '\"': *ptr2++='\"'; break;
case '\b': *ptr2++='b'; break;
case '\f': *ptr2++='f'; break;
case '\n': *ptr2++='n'; break;
case '\r': *ptr2++='r'; break;
case '\t': *ptr2++='t'; break;
default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */
}
}
}
*ptr2++='\"';*ptr2++=0;
return out;
}
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);}
/* Predeclare these prototypes. */
static const char *parse_value(cJSON *item,const char *value);
static char *print_value(cJSON *item,int depth,int fmt);
static const char *parse_array(cJSON *item,const char *value);
static char *print_array(cJSON *item,int depth,int fmt);
static const char *parse_object(cJSON *item,const char *value);
static char *print_object(cJSON *item,int depth,int fmt);
/* Utility to jump whitespace and cr/lf */
static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}
/* Parse an object - create a new root, and populate. */
cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated)
{
const char *end=0;
cJSON *c=cJSON_New_Item();
ep=0;
if (!c) return 0; /* memory fail */
end=parse_value(c,skip(value));
if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */
/* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}}
if (return_parse_end) *return_parse_end=end;
return c;
}
/* Default options for cJSON_Parse */
cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}
/* Render a cJSON item/entity/structure to text. */
char *cJSON_Print(cJSON *item) {return print_value(item,0,1);}
char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);}
/* Parser core - when encountering text, process appropriately. */
static const char *parse_value(cJSON *item,const char *value)
{
if (!value) return 0; /* Fail on null. */
if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
if (*value=='\"') { return parse_string(item,value); }
if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
if (*value=='[') { return parse_array(item,value); }
if (*value=='{') { return parse_object(item,value); }
ep=value;return 0; /* failure. */
}
/* Render a value to text. */
static char *print_value(cJSON *item,int depth,int fmt)
{
char *out=0;
if (!item) return 0;
switch ((item->type)&255)
{
case cJSON_NULL: out=cJSON_strdup("null"); break;
case cJSON_False: out=cJSON_strdup("false");break;
case cJSON_True: out=cJSON_strdup("true"); break;
case cJSON_Number: out=print_number(item);break;
case cJSON_String: out=print_string(item);break;
case cJSON_Array: out=print_array(item,depth,fmt);break;
case cJSON_Object: out=print_object(item,depth,fmt);break;
}
return out;
}
/* Build an array from input text. */
static const char *parse_array(cJSON *item,const char *value)
{
cJSON *child;
if (*value!='[') {ep=value;return 0;} /* not an array! */
item->type=cJSON_Array;
value=skip(value+1);
if (*value==']') return value+1; /* empty array. */
item->child=child=cJSON_New_Item();
if (!item->child) return 0; /* memory fail */
value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */
if (!value) return 0;
while (*value==',')
{
cJSON *new_item;
if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
child->next=new_item;new_item->prev=child;child=new_item;
value=skip(parse_value(child,skip(value+1)));
if (!value) return 0; /* memory fail */
}
if (*value==']') return value+1; /* end of array */
ep=value;return 0; /* malformed. */
}
/* Render an array to text */
static char *print_array(cJSON *item,int depth,int fmt)
{
char **entries;
char *out=0,*ptr,*ret;int len=5;
cJSON *child=item->child;
int numentries=0,i=0,fail=0;
/* How many entries in the array? */
while (child) numentries++,child=child->next;
/* Explicitly handle numentries==0 */
if (!numentries)
{
out=(char*)cJSON_malloc(3);
if (out) strcpy(out,"[]");
return out;
}
/* Allocate an array to hold the values for each */
entries=(char**)cJSON_malloc(numentries*sizeof(char*));
if (!entries) return 0;
memset(entries,0,numentries*sizeof(char*));
/* Retrieve all the results: */
child=item->child;
while (child && !fail)
{
ret=print_value(child,depth+1,fmt);
entries[i++]=ret;
if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
child=child->next;
}
/* If we didn't fail, try to malloc the output string */
if (!fail) out=(char*)cJSON_malloc(len);
/* If that fails, we fail. */
if (!out) fail=1;
/* Handle failure. */
if (fail)
{
for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
cJSON_free(entries);
return 0;
}
/* Compose the output array. */
*out='[';
ptr=out+1;*ptr=0;
for (i=0;i<numentries;i++)
{
strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
cJSON_free(entries[i]);
}
cJSON_free(entries);
*ptr++=']';*ptr++=0;
return out;
}
/* Build an object from the text. */
static const char *parse_object(cJSON *item,const char *value)
{
cJSON *child;
if (*value!='{') {ep=value;return 0;} /* not an object! */
item->type=cJSON_Object;
value=skip(value+1);
if (*value=='}') return value+1; /* empty array. */
item->child=child=cJSON_New_Item();
if (!item->child) return 0;
value=skip(parse_string(child,skip(value)));
if (!value) return 0;
child->string=child->valuestring;child->valuestring=0;
if (*value!=':') {ep=value;return 0;} /* fail! */
value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
if (!value) return 0;
while (*value==',')
{
cJSON *new_item;
if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
child->next=new_item;new_item->prev=child;child=new_item;
value=skip(parse_string(child,skip(value+1)));
if (!value) return 0;
child->string=child->valuestring;child->valuestring=0;
if (*value!=':') {ep=value;return 0;} /* fail! */
value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
if (!value) return 0;
}
if (*value=='}') return value+1; /* end of array */
ep=value;return 0; /* malformed. */
}
/* Render an object to text. */
static char *print_object(cJSON *item,int depth,int fmt)
{
char **entries=0,**names=0;
char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
cJSON *child=item->child;
int numentries=0,fail=0;
/* Count the number of entries. */
while (child) numentries++,child=child->next;
/* Explicitly handle empty object case */
if (!numentries)
{
out=(char*)cJSON_malloc(fmt?depth+4:3);
if (!out) return 0;
ptr=out;*ptr++='{';
if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';}
*ptr++='}';*ptr++=0;
return out;
}
/* Allocate space for the names and the objects */
entries=(char**)cJSON_malloc(numentries*sizeof(char*));
if (!entries) return 0;
names=(char**)cJSON_malloc(numentries*sizeof(char*));
if (!names) {cJSON_free(entries);return 0;}
memset(entries,0,sizeof(char*)*numentries);
memset(names,0,sizeof(char*)*numentries);
/* Collect all the results into our arrays: */
child=item->child;depth++;if (fmt) len+=depth;
while (child)
{
names[i]=str=print_string_ptr(child->string);
entries[i++]=ret=print_value(child,depth,fmt);
if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
child=child->next;
}
/* Try to allocate the output string */
if (!fail) out=(char*)cJSON_malloc(len);
if (!out) fail=1;
/* Handle failure */
if (fail)
{
for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
cJSON_free(names);cJSON_free(entries);
return 0;
}
/* Compose the output: */
*out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
for (i=0;i<numentries;i++)
{
if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
strcpy(ptr,names[i]);ptr+=strlen(names[i]);
*ptr++=':';if (fmt) *ptr++='\t';
strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
if (i!=numentries-1) *ptr++=',';
if (fmt) *ptr++='\n';*ptr=0;
cJSON_free(names[i]);cJSON_free(entries[i]);
}
cJSON_free(names);cJSON_free(entries);
if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
*ptr++='}';*ptr++=0;
return out;
}
/* Get Array size/item / object item. */
int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
/* Utility for array list handling. */
static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
/* Utility for handling references. */
static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;}
/* Add item to array/object. */
void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}}
void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);}
void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;}
void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
/* Replace array/object items with new ones. */
void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
/* Create basic types: */
cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;}
cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
/* Create Arrays: */
cJSON *cJSON_CreateIntArray(const int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
cJSON *cJSON_CreateFloatArray(const float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
cJSON *cJSON_CreateDoubleArray(const double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
/* Duplication */
cJSON *cJSON_Duplicate(cJSON *item,int recurse)
{
cJSON *newitem,*cptr,*nptr=0,*newchild;
/* Bail on bad ptr */
if (!item) return 0;
/* Create new item */
newitem=cJSON_New_Item();
if (!newitem) return 0;
/* Copy over all vars */
newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble;
if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}}
if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}}
/* If non-recursive, then we're done! */
if (!recurse) return newitem;
/* Walk the ->next chain for the child. */
cptr=item->child;
while (cptr)
{
newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */
if (!newchild) {cJSON_Delete(newitem);return 0;}
if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */
else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */
cptr=cptr->next;
}
return newitem;
}
void cJSON_Minify(char *json)
{
char *into=json;
while (*json)
{
if (*json==' ') json++;
else if (*json=='\t') json++; // Whitespace characters.
else if (*json=='\r') json++;
else if (*json=='\n') json++;
else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; // double-slash comments, to end of line.
else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} // multiline comments.
else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} // string literals, which are \" sensitive.
else *into++=*json++; // All other characters.
}
*into=0; // and null-terminate.
}

143
src/entry/cJSON.h Normal file
View File

@@ -0,0 +1,143 @@
/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef cJSON__h
#define cJSON__h
#ifdef __cplusplus
extern "C"
{
#endif
/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
#define cJSON_IsReference 256
/* The cJSON structure: */
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. */
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
typedef struct cJSON_Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
/* Supply malloc, realloc and free functions to cJSON */
extern void cJSON_InitHooks(cJSON_Hooks* hooks);
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON *cJSON_Parse(const char *value);
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char *cJSON_Print(cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char *cJSON_PrintUnformatted(cJSON *item);
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
extern int cJSON_GetArraySize(cJSON *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
/* Get item "string" from object. Case insensitive. */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
extern const char *cJSON_GetErrorPtr(void);
/* These calls create a cJSON item of the appropriate type. */
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreateTrue(void);
extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
/* These utilities create an Array of count items. */
extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
/* Append item to the specified array/object. */
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
/* Remove/Detatch items from Arrays/Objects. */
extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
/* Update array items. */
extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
/* Duplicate a cJSON item */
extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
extern void cJSON_Minify(char *json);
/* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,321 @@
#include "MESA_handle_logger.h"
#include "config_monitor.h"
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const char* module_config_monitor="CONFIG_MONITOR";
#define CM_UPDATE_TYPE_ERR -1
#define CM_UPDATE_TYPE_NONE 0
#define CM_MAX_TABLE_NUM 256
#define MAX_CONFIG_FN_LEN 256
#define MAX_CONFIG_LINE 1024*4
//#define USING_DICTATOR 1
extern "C" void __real_free(void*p);
struct cm_table_info_t
{
char table_name[MAX_CONFIG_FN_LEN];
char cfg_path[MAX_CONFIG_FN_LEN];
int cfg_num;
};
//replacement of glibc scandir, to adapt dictator malloc wrap
#define ENLARGE_STEP 1024
int my_scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const void *, const void *))
{
DIR * od;
int n = 0;
int DIR_ENT_SIZE=ENLARGE_STEP;
struct dirent ** list = NULL;
struct dirent * p;
struct dirent entry,*result;
if((dir == NULL) || (namelist == NULL))
return -1;
od = opendir(dir);
if(od == NULL)
return -1;
list = (struct dirent **)malloc(DIR_ENT_SIZE*sizeof(struct dirent *));
while(0==readdir_r(od,&entry,&result))
{
if(result==NULL)
{
break;
}
if( filter && !filter(&entry))
continue;
p = (struct dirent *)malloc(sizeof(struct dirent));
memcpy((void *)p,(void *)(&entry),sizeof(struct dirent));
list[n] = p;
n++;
if(n >= DIR_ENT_SIZE)
{
DIR_ENT_SIZE+=ENLARGE_STEP;
list=(struct dirent **)realloc((void*)list,DIR_ENT_SIZE*sizeof(struct dirent *));
}
}
closedir(od);
*namelist = list;
if(compar)
qsort((void *)*namelist,n,sizeof(struct dirent *),compar);
return n;
}
int filter_fn(const struct dirent * ent)
{
if(ent->d_type != DT_REG)
return 0;
return (strncmp(ent->d_name,"full_config_index",strlen("full_config_index")) == 0||
strncmp(ent->d_name,"inc_config_index",strlen("inc_config_index")) == 0);
}
int get_new_idx_path(unsigned int current_version,const char*file_dir,void* logger,char*** idx_path,int*idx_num)
{
struct dirent **namelist;
int n=0,i=0,sscanf_ret;
char update_str[32]={0};
unsigned int latest_ful_version=0,latest_inc_version=0;
unsigned int config_seq=0;
int *inc_file_idx;
int full_file_idx=0,inc_idx_num=0,path_len=0;
int update_type=CM_UPDATE_TYPE_NONE;
n = my_scandir(file_dir, &namelist, filter_fn, (int (*)(const void*, const void*))alphasort);
if (n < 0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor,"scan dir error");
update_type=CM_UPDATE_TYPE_ERR;
return update_type;
}
inc_file_idx=(int*)calloc(sizeof(int),n);
inc_idx_num=0;
for(i=0;i<n;i++)
{
config_seq=0;
if((strcmp(namelist[i]->d_name, ".") == 0) || (strcmp(namelist[i]->d_name, "..") == 0))
{
continue;
}
if(strlen(namelist[i]->d_name)>32)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor
,"config file %s filename too long,should like full_config_index.0000000001"
,namelist[i]->d_name);
continue;
}
sscanf_ret=sscanf(namelist[i]->d_name,"%[a-zA-Z]_config_index.%u",update_str,&config_seq);
if(sscanf_ret!=2)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor
,"config file %s filename error,should like full_config_index.0000000001"
,namelist[i]->d_name);
continue;
}
if(strncasecmp(update_str,"full",strlen(update_str))==0)
{
if(config_seq>latest_ful_version)
{
latest_ful_version=config_seq;
full_file_idx=i;
}
}
else if(strncasecmp(update_str,"inc",strlen(update_str))==0)
{
if(config_seq>current_version)
{
inc_file_idx[inc_idx_num]=i;
inc_idx_num++;
if(config_seq>latest_inc_version)
{
latest_inc_version=config_seq;
}
}
}
else
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor
,"config file %s,not full or inc config"
,namelist[i]->d_name);
}
}
//full update
if(latest_ful_version>current_version)
{
*idx_path=(char**)malloc(sizeof(char**));
path_len=strlen(file_dir)+strlen(namelist[full_file_idx]->d_name)+1+1;
(*idx_path)[0]=(char*)malloc(path_len);
snprintf((*idx_path)[0],path_len,"%s/%s",file_dir,namelist[full_file_idx]->d_name);
*idx_num=1;
update_type=CM_UPDATE_TYPE_FULL;
}
//inc update,it's possible that do inc after full update in this function,but we'll process it at next call.
else if(latest_inc_version>current_version)
{
*idx_path=(char**)malloc(sizeof(char**)*inc_idx_num);
for(i=0;i<inc_idx_num;i++)
{
path_len=strlen(file_dir)+strlen(namelist[inc_file_idx[i]]->d_name)+1+1;
(*idx_path)[i]=(char*)malloc(path_len);
snprintf((*idx_path)[i],path_len,"%s/%s",file_dir,namelist[inc_file_idx[i]]->d_name);
}
*idx_num=inc_idx_num;
update_type=CM_UPDATE_TYPE_INC;
}
else
{
update_type=CM_UPDATE_TYPE_NONE;
}
free(inc_file_idx);
for(i=0;i<n;i++)
{
free(namelist[i]);
}
free(namelist);
return update_type;
}
int cm_read_cfg_index_file(const char* path,struct cm_table_info_t* idx,int size,void* logger)
{
FILE* fp=NULL;
fp=fopen(path,"r");
int ret=0,i=0;
while(!feof(fp))
{
ret=fscanf(fp,"%s\t%d\t%s",idx[i].table_name
,&(idx[i].cfg_num)
,idx[i].cfg_path);
if(ret==3&&idx[i].cfg_num>0)//jump over empty line
{
i++;
}
if(i==size)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor ,"Too much lines in %s",path);
break;
}
}
fclose(fp);
return i;
}
int cm_read_table_file(struct cm_table_info_t* index,
void (*update)(const char*,const char*,void*),
void* u_para,
void* logger)
{
int cfg_num=0,i=0;
char line[MAX_CONFIG_LINE]={0},*ret_str=NULL;
FILE*fp=fopen(index->cfg_path,"r");
if(fp==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor,"update error,open %s failed.",index->cfg_path);
return -1;
}
fscanf(fp,"%d\n",&cfg_num);
if(cfg_num!=index->cfg_num)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor ,"file %s config num not matched",index->cfg_path);
fclose(fp);
return -1;
}
for(i=0;i<cfg_num;i++)
{
line[sizeof(line)-1]='\0';
ret_str=fgets(line,sizeof(line),fp);
if(ret_str==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor ,
"update error,file %s line_num %d less than claimed %d",
index->cfg_path,i,cfg_num);
break;
}
if(line[sizeof(line)-1]!='\0')
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,module_config_monitor ,
"update error,line size more than %u at of file %s:%d",
sizeof(line),index->cfg_path,i);
continue;
}
update(index->table_name,line,u_para);
}
fclose(fp);
return 0;
}
const char* path2filename(const char*path)
{
int i=0;
for(i=strlen(path);i>0;i--)
{
if(path[i]=='/')
{
break;
}
}
return path+i+1;
}
void config_monitor_traverse(unsigned int version,const char*idx_dir,
void (*start)(unsigned int ,int ,void*),
void (*update)(const char* ,const char*,void* ),
void (*finish)(void*),
void* u_para,
void* logger)
{
int update_type=CM_UPDATE_TYPE_NONE;
unsigned int new_version=0;
char**idx_path_array=NULL;
const char* table_filename=NULL;
char str_not_care[256]={0};
int idx_num=0,table_num=0,i=0,j=0;
struct cm_table_info_t table_array[CM_MAX_TABLE_NUM];
update_type=get_new_idx_path(version, idx_dir,logger, &idx_path_array, &idx_num);
if(update_type==CM_UPDATE_TYPE_FULL||update_type==CM_UPDATE_TYPE_INC)
{
for(i=0;i<idx_num;i++)
{
MESA_handle_runtime_log(logger,RLOG_LV_INFO,module_config_monitor ,
"load %s",idx_path_array[i]);
table_num=cm_read_cfg_index_file(idx_path_array[i],table_array, CM_MAX_TABLE_NUM,logger);
table_filename=path2filename(idx_path_array[i]);
sscanf(table_filename,"%[a-zA-Z]_config_index.%u",str_not_care,&new_version);
start(new_version,update_type,u_para);
for(j=0;j<table_num;j++)
{
cm_read_table_file(table_array+j,update,u_para,logger);
}
finish(u_para);
}
}
for(i=0;i<idx_num;i++)
{
free(idx_path_array[i]);
}
free(idx_path_array);//free null is OK
return;
}

View File

@@ -0,0 +1,14 @@
#ifndef __CONFIG_MONITOR_H_INCLUDE_
#define __CONFIG_MONITOR_H_INCLUDE_
#define CM_UPDATE_TYPE_FULL 1
#define CM_UPDATE_TYPE_INC 2
void config_monitor_traverse(unsigned int version,const char*idx_dir,
void (*start)(unsigned int ,int ,void*),//vesion,CM_UPDATE_TYPE_*,u_para
void (*update)(const char* ,const char*,void* ),//table name ,line ,u_para
void (*finish)(void*),//u_para
void* u_para,
void* logger);
#endif

View File

@@ -0,0 +1,53 @@
#include"dynamic_array.h"
#include <stdlib.h>
#include <string.h>
int dynamic_array_VERSION_20141202=0;
struct dynamic_array_t* dynamic_array_create(int size,int step)
{
struct dynamic_array_t* d_array=(struct dynamic_array_t*)calloc(sizeof(struct dynamic_array_t),1);
d_array->array=(void**)calloc(sizeof(void*),size);
d_array->size=size;
d_array->enlarge_step=step;
return d_array;
}
void dynamic_array_destroy(struct dynamic_array_t* d_array,void (* free_data)(void *))
{
int i;
if(free_data!=NULL)
{
for(i=0;i<d_array->size;i++)
{
free_data(d_array->array[i]);
}
}
free(d_array->array);
free(d_array);
}
void* dynamic_array_read(struct dynamic_array_t* d_array,int i)
{
if(i<d_array->size)
{
return d_array->array[i];
}
else
{
return NULL;
}
}
void dynamic_array_write(struct dynamic_array_t* d_array,int i,void* data)
{
int new_size=0;
if(i<d_array->size)
{
d_array->array[i]=data;
}
else
{
new_size=i+d_array->enlarge_step;
d_array->array=(void**)realloc(d_array->array,new_size*sizeof(void*));
memset(d_array->array+d_array->size,0,(new_size-d_array->size)*sizeof(void*));
d_array->size=new_size;
d_array->array[i]=data;
return;
}
}

13
src/entry/dynamic_array.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef _DYNAMIC_ARRAY_H_INCLUDE_
#define _DYNAMIC_ARRAY_H_INCLUDE_
struct dynamic_array_t
{
void ** array;
int size;
int enlarge_step;
};
struct dynamic_array_t* dynamic_array_create(int size,int step);
void dynamic_array_destroy(struct dynamic_array_t* d_array,void (* free_data)(void *));
void* dynamic_array_read(struct dynamic_array_t* d_array,int i);
void dynamic_array_write(struct dynamic_array_t* d_array,int i,void* data);
#endif //_DYNAMIC_ARRAY_H_INCLUDE_

919
src/entry/json2iris.cpp Normal file
View File

@@ -0,0 +1,919 @@
#include <MESA/MESA_handle_logger.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <MESA/MESA_htable.h>
#include "cJSON.h"
#include "map_str2int.h"
#include "Maat_rule_internal.h"
const char* maat_json="maat json";
const int json_version=1;
#define MAX_PATH_LINE 256
#define MAX_COLUMN_NUM 32
struct group_info_t
{
int group_id;
};
struct iris_table_t
{
char table_name[MAX_PATH_LINE];
char table_path[MAX_PATH_LINE];
int line_count;
};
struct iris_description_t
{
int group_cnt;
int region_cnt;
char tmp_iris_dir[MAX_PATH_LINE];
char tmp_iris_index_dir[MAX_PATH_LINE];
char index_path[MAX_PATH_LINE];
struct iris_table_t group_table;
struct iris_table_t compile_table;
MESA_htable_handle group_name_map;
MESA_htable_handle iris_table_map;
MESA_htable_handle str2int_map;
};
struct traslate_command_t
{
const char* json_string;
char* json_value;
int json_type;
int str2int_flag;
int empty_allowed;
const char* default_string;
int default_int;
};
int set_iris_descriptor(const char* json_file,cJSON *json,struct iris_description_t *iris_cfg,void * logger)
{
cJSON* j=NULL;
char* table_name=NULL;
memset(iris_cfg,0,sizeof(struct iris_description_t));
snprintf(iris_cfg->tmp_iris_dir,sizeof(iris_cfg->tmp_iris_dir),"%s_iris_tmp",json_file);
snprintf(iris_cfg->tmp_iris_index_dir,sizeof(iris_cfg->tmp_iris_index_dir),"%s_iris_tmp/index",json_file);
snprintf(iris_cfg->index_path,sizeof(iris_cfg->index_path),"%s/full_config_index.%010d",iris_cfg->tmp_iris_index_dir,json_version);
j=cJSON_GetObjectItem(json, "compile_table");
if(j==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile_table not defined.");
return -1;
}
table_name=j->valuestring;
memcpy(iris_cfg->compile_table.table_name,table_name,strlen(table_name));
snprintf(iris_cfg->compile_table.table_path,sizeof(iris_cfg->compile_table.table_path),
"%s/%s.local",iris_cfg->tmp_iris_dir,iris_cfg->compile_table.table_name);
j=cJSON_GetObjectItem(json, "group_table");
if(j==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"group_table not defined.");
return -1;
}
table_name=j->valuestring;
memcpy(iris_cfg->group_table.table_name,table_name,strlen(table_name));
snprintf(iris_cfg->group_table.table_path,sizeof(iris_cfg->group_table.table_name),
"%s/%s.local",iris_cfg->tmp_iris_dir,iris_cfg->group_table.table_name);
MESA_htable_create_args_t hargs;
memset(&hargs,0,sizeof(hargs));
hargs.thread_safe=1;
hargs.hash_slot_size = 1024;
hargs.max_elem_num = 0;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_LRU;
hargs.expire_time = 0;
hargs.key_comp = NULL;
hargs.key2index = NULL;
hargs.recursive = 0;
hargs.data_free = free;
hargs.data_expire_with_condition = NULL;
iris_cfg->group_name_map=MESA_htable_create(&hargs, sizeof(hargs));
MESA_htable_print_crtl(iris_cfg->group_name_map, 0);
iris_cfg->iris_table_map=MESA_htable_create(&hargs, sizeof(hargs));
MESA_htable_print_crtl(iris_cfg->iris_table_map, 0);
iris_cfg->str2int_map=map_create();
map_register(iris_cfg->str2int_map, "yes",1);
map_register(iris_cfg->str2int_map, "no",0);
map_register(iris_cfg->str2int_map, "ip",TABLE_TYPE_IP);
map_register(iris_cfg->str2int_map, "string",TABLE_TYPE_EXPR);
map_register(iris_cfg->str2int_map, "intval",TABLE_TYPE_INTVAL);
map_register(iris_cfg->str2int_map, "ipv4",4);
map_register(iris_cfg->str2int_map, "ipv6",6);
map_register(iris_cfg->str2int_map, "double",0);
map_register(iris_cfg->str2int_map, "single",1);
map_register(iris_cfg->str2int_map, "none",0);
map_register(iris_cfg->str2int_map, "and",1);
map_register(iris_cfg->str2int_map, "regex",2);
map_register(iris_cfg->str2int_map, "offset",3);
map_register(iris_cfg->str2int_map, "sub",0);
map_register(iris_cfg->str2int_map, "right",1);
map_register(iris_cfg->str2int_map, "left",2);
map_register(iris_cfg->str2int_map, "complete",3);
map_register(iris_cfg->str2int_map, "uncase plain",0);
map_register(iris_cfg->str2int_map, "hexbin",1);
map_register(iris_cfg->str2int_map, "case plain",2);
return 0;
}
void clear_iris_descriptor(struct iris_description_t *iris_cfg)
{
if(iris_cfg->group_name_map!=NULL)
{
MESA_htable_destroy(iris_cfg->group_name_map, NULL);
}
if(iris_cfg->iris_table_map!=NULL)
{
MESA_htable_destroy(iris_cfg->iris_table_map, NULL);
}
return;
}
int create_tmp_dir(struct iris_description_t *p)
{
if((access(p->tmp_iris_dir,F_OK))<0)
{
if((mkdir(p->tmp_iris_dir, 0777)) < 0)
{
return -1;
}
}
if((access(p->tmp_iris_index_dir,F_OK))<0)
{
if((mkdir(p->tmp_iris_index_dir, 0777)) < 0)
{
return -1;
}
}
return 0;
}
int set_file_rulenum(const char* path,int rulenum,void* logger)
{
FILE* fp=NULL;
if(rulenum==0)
{
fp=fopen(path,"w");
}
else
{
fp=fopen(path,"r+");
}
if(fp==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"fopen %s failed %s at set rule num.",path,strerror(errno));
return -1;
}
fprintf(fp,"%010d\n",rulenum);
fclose(fp);
return 0;
}
int direct_write_rule(cJSON* json,MESA_htable_handle str2int,struct traslate_command_t*cmd,int cmd_cnt,const char* path,void* logger)
{
int i=0,ret=-1;
cJSON* item=NULL;
cJSON dummy;
char *p=NULL;
int int_value=0;
FILE* fp=NULL;
for(i=0;i<cmd_cnt;i++)
{
item=cJSON_GetObjectItem(json,cmd[i].json_string);
if(item==NULL&&cmd[i].empty_allowed==1)
{
dummy.valuestring=(char*)cmd[i].default_string;
dummy.valueint=cmd[i].default_int;
dummy.type=cmd[i].json_type;
item=&dummy;
}
if(item==NULL||item->type!=cmd[i].json_type)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"%s not defined or wrong format.",cmd[i].json_string);
goto error_out;
}
if(cmd[i].str2int_flag==1)
{
p=item->valuestring;
ret=map_str2int(str2int, p, &int_value);
if(ret<0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"%s's value %s is not valid format.",cmd[i].json_string,p);
free(p);
goto error_out;
}
cmd[i].json_value=(char*)malloc(21);/* 2^64+1 can be represented in 21 chars. */
snprintf(cmd[i].json_value,21,"%d",int_value);
}
else
{
switch(item->type)
{
case cJSON_Number:
cmd[i].json_value=cJSON_Print(item);
break;
case cJSON_String:
cmd[i].json_value=(char*)calloc(strlen(item->valuestring)+1,1);
memcpy(cmd[i].json_value,item->valuestring,strlen(item->valuestring));
break;
default://impossible ,already checked
assert(0);
break;
}
}
}
fp=fopen(path,"a");
if(fp==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"fopen %s error %s.",path,strerror(errno));
goto error_out;
}
for(i=0;i<cmd_cnt;i++)
{
fprintf(fp,"%s\t",cmd[i].json_value);
}
fprintf(fp,"\n");
fclose(fp);
for(i=0;i<cmd_cnt;i++)
{
if(cmd[i].json_value!=NULL)
{
free(cmd[i].json_value);
}
}
return 0;
error_out:
for(i=0;i<cmd_cnt;i++)
{
if(cmd[i].json_value!=NULL)
{
free(cmd[i].json_value);
}
}
return -1;
}
int write_ip_rule(cJSON *region_json,struct iris_description_t *p_iris,const char* path,void * logger)
{
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
int cmd_cnt=0;
memset(json_cmd,0,sizeof(json_cmd));
json_cmd[cmd_cnt].json_string="region_id";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="group_id";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="addr_type";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].str2int_flag=1;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="src_ip";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="0.0.0.0";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="mask_src_ip";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="255.255.255.255";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="src_port";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="0";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="mask_src_port";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="65535";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="dst_ip";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="0.0.0.0";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="mask_dst_ip";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="255.255.255.255";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="dst_port";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="0";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="mask_dst_port";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="65535";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="protocol";
json_cmd[cmd_cnt].json_type=cJSON_Number;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_int=0;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="direction";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].str2int_flag=1;
json_cmd[cmd_cnt].empty_allowed=1;
json_cmd[cmd_cnt].default_string="double";
cmd_cnt++;
json_cmd[cmd_cnt].json_string="is_valid";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt,path,logger);
}
int write_expr_rule(cJSON *region_json,struct iris_description_t *p_iris,const char* path,void * logger)
{
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
int cmd_cnt=0;
memset(json_cmd,0,sizeof(json_cmd));
json_cmd[cmd_cnt].json_string="region_id";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="group_id";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="keywords";
json_cmd[cmd_cnt].json_type=cJSON_String;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="expr_type";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].str2int_flag=1;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="match_method";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].str2int_flag=1;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="format";
json_cmd[cmd_cnt].json_type=cJSON_String;
json_cmd[cmd_cnt].str2int_flag=1;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="is_valid";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt,path,logger);
}
int write_intval_rule(cJSON *region_json,struct iris_description_t *p_iris,const char* path,void * logger)
{
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
int cmd_cnt=0;
memset(json_cmd,0,sizeof(json_cmd));
json_cmd[cmd_cnt].json_string="region_id";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="group_id";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="low_boundary";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="up_boundary";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string="is_valid";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt,path,logger);
}
struct iris_table_t* query_table_info(iris_description_t* p_iris,const char* table_name)
{
struct iris_table_t* table_info=NULL;
table_info=(struct iris_table_t*)MESA_htable_search(p_iris->iris_table_map, (const unsigned char*)table_name,strlen(table_name));
if(table_info==NULL)
{
table_info=(struct iris_table_t*)calloc(sizeof(struct iris_table_t),1);
table_info->line_count=0;
memcpy(table_info->table_name,table_name,MIN(sizeof(table_info->table_name)-1, strlen(table_name)));
snprintf(table_info->table_path,sizeof(table_info->table_path),"%s/%s.local",p_iris->tmp_iris_dir,table_info->table_name);
MESA_htable_add(p_iris->iris_table_map,(const unsigned char*)table_info->table_name,strlen(table_info->table_name),table_info);
}
return table_info;
}
int write_plugin_table(cJSON* plug_table_json,int sequence,iris_description_t* p_iris,void* logger)
{
cJSON* item=NULL,*table_content=NULL,*each_line=NULL;
struct iris_table_t* table_info=NULL;
const char* table_name=NULL,*line_content=NULL;
int ret=0,i=0,line_cnt=0;
FILE*fp=NULL;
item=cJSON_GetObjectItem(plug_table_json,"table_name");
if(item==NULL||item->type!=cJSON_String)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"The %d plugin_table's table_name not defined or format error.",sequence);
return -1;
}
table_name= item->valuestring;
table_info=query_table_info(p_iris, table_name);
table_content=cJSON_GetObjectItem(plug_table_json,"table_content");
if(table_content==NULL||table_content->type!=cJSON_Array)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"%d plugin_table's table_content not defined or format error."
,sequence);
return -1;
}
line_cnt=cJSON_GetArraySize(table_content);
if(table_info->line_count==0)
{
ret=set_file_rulenum(table_info->table_path,0,logger);
if(ret<0)
{
return -1;
}
}
fp=fopen(table_info->table_path,"a");
if(fp==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"fopen %s error %s.",table_info->table_path,strerror(errno));
return -1;
}
for(i=0;i<line_cnt;i++)
{
each_line=cJSON_GetArrayItem(table_content,i);
if(each_line==NULL||each_line->type!=cJSON_String)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"plugin_table %s's line %d format error.",table_info->table_name,i+1);
continue;
}
line_content=each_line->valuestring;
fprintf(fp,"%s\n",line_content);
table_info->line_count++;
}
fclose(fp);
set_file_rulenum(table_info->table_path,table_info->line_count,logger);
return 0;
}
int write_region_rule(cJSON* region_json,int compile_id,int group_id,iris_description_t* p_iris,void* logger)
{
cJSON* item=NULL,*table_content=NULL;
int ret=0;
int region_id=0;
const char* table_name=NULL,*table_type_str=NULL;
enum MAAT_TABLE_TYPE table_type=TABLE_TYPE_EXPR;
struct iris_table_t* table_info=NULL;
item=cJSON_GetObjectItem(region_json,"table_name");
if(item==NULL||item->type!=cJSON_String)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d's region table_name not defined or format error.",compile_id);
return -1;
}
table_name=item->valuestring;
table_info=query_table_info( p_iris, table_name);
item=cJSON_GetObjectItem(region_json,"table_type");
if(item==NULL||item->type!=cJSON_String)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d table name %s's table_type not defined or format error."
,compile_id,table_name);
return -1;
}
table_type_str=item->valuestring;
ret=map_str2int(p_iris->str2int_map,table_type_str,(int*)&(table_type));
if(ret!=1)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d table name %s's table_type %s invalid."
,compile_id,table_name,table_type_str);
return -1;
}
table_content=cJSON_GetObjectItem(region_json,"table_content");
if(table_content==NULL||table_content->type!=cJSON_Object)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d table name %s's table_content not defined or format error."
,compile_id,table_name);
return -1;
}
if(table_info->line_count==0)
{
ret=set_file_rulenum(table_info->table_path,0,logger);
if(ret<0)
{
return -1;
}
}
region_id=p_iris->region_cnt++;
cJSON_AddNumberToObject(table_content, "region_id", region_id);
cJSON_AddNumberToObject(table_content, "group_id", group_id);
cJSON_AddNumberToObject(table_content, "is_valid", 1);
switch(table_type)
{
case TABLE_TYPE_EXPR:
ret=write_expr_rule(table_content, p_iris, table_info->table_path, logger);
break;
case TABLE_TYPE_IP:
ret=write_ip_rule(table_content, p_iris, table_info->table_path, logger);
break;
case TABLE_TYPE_INTVAL:
ret=write_intval_rule(table_content, p_iris, table_info->table_path, logger);
break;
default:
assert(0);
break;
}
if(ret>=0)
{
table_info->line_count++;
set_file_rulenum(table_info->table_path,table_info->line_count,logger);
}
return ret;
}
int write_compile_rule(cJSON *compile,struct iris_description_t *p_iris,void * logger)
{
int compile_id=-1,cmd_cnt=0,ret=-1;
cJSON* item=NULL;
struct traslate_command_t compile_cmd[MAX_COLUMN_NUM];
memset(compile_cmd,0,sizeof(compile_cmd));
compile_cmd[cmd_cnt].json_string="compile_id";
compile_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
compile_cmd[cmd_cnt].json_string="service";
compile_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
compile_cmd[cmd_cnt].json_string="action";
compile_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
compile_cmd[cmd_cnt].json_string="do_blacklist";
compile_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
compile_cmd[cmd_cnt].json_string="do_log";
compile_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
compile_cmd[cmd_cnt].json_string="effective_rage";
compile_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
compile_cmd[cmd_cnt].json_string="user_region";
compile_cmd[cmd_cnt].json_type=cJSON_String;
cmd_cnt++;
compile_cmd[cmd_cnt].json_string="is_valid";
compile_cmd[cmd_cnt].json_type=cJSON_String;
compile_cmd[cmd_cnt].str2int_flag=1;
cmd_cnt++;
if(p_iris->compile_table.line_count==0)
{
ret=set_file_rulenum(p_iris->compile_table.table_path, 0,logger);
if(ret<0)
{
return -1;
}
}
ret=direct_write_rule(compile, p_iris->str2int_map,compile_cmd,cmd_cnt, p_iris->compile_table.table_path,logger);
if(ret<0)
{
return -1;
}
item=cJSON_GetObjectItem(compile,"compile_id");
if(item->type!=cJSON_Number)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile_id format not number.");
return -1;
}
compile_id=item->valueint;
p_iris->compile_table.line_count++;
set_file_rulenum(p_iris->compile_table.table_path,p_iris->compile_table.line_count,logger);
return compile_id;
}
int write_group_rule(int compile_id ,int group_id,struct iris_description_t *p_iris,void * logger)
{
FILE*fp=NULL;
int ret=0;
if(p_iris->group_table.line_count==0)
{
ret=set_file_rulenum(p_iris->group_table.table_path,0,logger);
if(ret<0)
{
return -1;
}
}
fp=fopen(p_iris->group_table.table_path,"a");
if(fp==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"fopen %s error %s.",p_iris->group_table.table_path,strerror(errno));
return -1;
}
fprintf(fp,"%d\t%d\t1\n",group_id,compile_id);
fclose(fp);
p_iris->group_table.line_count++;
ret=set_file_rulenum(p_iris->group_table.table_path,p_iris->group_table.line_count,logger);
return 0;
}
void table_idx_write_cb(const uchar * key, uint size, void * data, void * user)
{
struct iris_table_t* p_table=(struct iris_table_t*)data;
FILE* fp=(FILE*)user;
fprintf(fp,"%s\t%d\t%s\n",p_table->table_name,p_table->line_count,p_table->table_path);
}
int write_index_file(struct iris_description_t *p_iris,void* logger)
{
FILE*fp=NULL;
fp=fopen(p_iris->index_path,"w");
if(fp==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"index file %s fopen error %s.",p_iris->index_path,strerror(errno));
return -1;
}
fprintf(fp,"%s\t%d\t%s\n",p_iris->compile_table.table_name
,p_iris->compile_table.line_count
,p_iris->compile_table.table_path);
fprintf(fp,"%s\t%d\t%s\n",p_iris->group_table.table_name
,p_iris->group_table.line_count
,p_iris->group_table.table_path);
MESA_htable_iterate(p_iris->iris_table_map, table_idx_write_cb, fp);
fclose(fp);
return 0;
}
int write_iris(cJSON *json,struct iris_description_t *p_iris,void* logger)
{
int i=0,j=0,k=0;
int compile_id=-1,compile_cnt=0,group_cnt=0,region_cnt=0,plug_table_cnt=0;
int ret=0;
cJSON *c_rules=NULL,*g_rules=NULL,*r_rules=NULL,*item=NULL,*plug_tables=NULL;
cJSON *compile_rule=NULL,*group_rule=NULL,*region_rule=NULL,*each_plug_table=NULL;
const char* group_name=NULL;
struct group_info_t *group_info=NULL;
plug_tables=cJSON_GetObjectItem(json,"plugin_table");
if(NULL!=plug_tables)
{
plug_table_cnt=cJSON_GetArraySize(plug_tables);
for(i=0;i<plug_table_cnt;i++)
{
each_plug_table=cJSON_GetArrayItem(plug_tables,i);
write_plugin_table(each_plug_table, i, p_iris, logger);
}
}
c_rules=cJSON_GetObjectItem(json,"rules");
if(c_rules==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"have no rules.");
return -1;
}
compile_cnt=cJSON_GetArraySize(c_rules);
if(compile_cnt<=0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"have no rules.");
return -1;
}
for(i=0;i<compile_cnt;i++)
{
compile_rule=cJSON_GetArrayItem(c_rules,i);
compile_id=write_compile_rule(compile_rule,p_iris, logger);
if(compile_id<0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"In %d compile rule.",i);
return -1;
}
g_rules=cJSON_GetObjectItem(compile_rule,"groups");
if(g_rules==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d have no group.",compile_id);
return -1;
}
group_cnt=cJSON_GetArraySize(g_rules);
if(group_cnt<=0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d have no groups.",compile_id);
return -1;
}
for(j=0;j<group_cnt;j++)
{
group_rule=cJSON_GetArrayItem(g_rules,j);
item=cJSON_GetObjectItem(group_rule,"group_name");
if(item==NULL||item->type!=cJSON_String)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d's group has no name.",compile_id);
return -1;
}
group_name=item->valuestring;
group_info=(struct group_info_t*)MESA_htable_search(p_iris->group_name_map, (const unsigned char*)group_name, strlen(group_name));
if(group_info!=NULL)//exist group name ,region already read
{
ret=write_group_rule(compile_id, group_info->group_id, p_iris, logger);
if(ret<0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d write group error.",compile_id);
return -1;
}
r_rules=cJSON_GetObjectItem(group_rule,"regions");
if(r_rules!=NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_json,
"compile rule %d's %s declared in previous compile rule, regions NOT take effect."
,compile_id,group_name);
}
continue;
}
group_info=(struct group_info_t*)malloc(sizeof(struct group_info_t));
group_info->group_id=p_iris->group_cnt++;
MESA_htable_add(p_iris->group_name_map,(const unsigned char*)group_name, strlen(group_name),group_info);
r_rules=cJSON_GetObjectItem(group_rule,"regions");
if(r_rules==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d's %s has no region.",compile_id,group_name);
return -1;
}
region_cnt=cJSON_GetArraySize(r_rules);
if(region_cnt<=0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d's %s has no region.",compile_id,group_name);
return -1;
}
for(k=0;k<region_cnt;k++)
{
region_rule=cJSON_GetArrayItem(r_rules,k);
ret=write_region_rule(region_rule, compile_id, group_info->group_id, p_iris, logger);
if(ret<0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d write region error.",compile_id);
return -1;
}
}
ret=write_group_rule(compile_id, group_info->group_id, p_iris, logger);
if(ret<0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"compile rule %d write group error.",compile_id);
return -1;
}
}
}
ret=write_index_file(p_iris,logger);
if(ret<0)
{
return -1;
}
return 0;
}
int json2iris(const char* json_file,char* iris_dir_buf,int buf_len,void* logger)
{
FILE* json_fp=NULL;
cJSON *json=NULL;
struct stat fstat_buf;
int ret=-1;
char* json_buff=NULL;
unsigned long json_file_size=0,read_size=0;
struct iris_description_t iris_cfg;
memset(&iris_cfg,0,sizeof(iris_cfg));
ret=stat(json_file, &fstat_buf);
if(ret!=0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"fstat file %s error.",json_file);
goto error_out;
}
json_fp=fopen(json_file,"r");
if(json_fp==NULL)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"fopen file %s error %s.",json_file,strerror(errno));
goto error_out;
}
json_file_size=fstat_buf.st_size;
json_buff=(char*)calloc(1,json_file_size);
read_size=fread(json_buff,1,json_file_size,json_fp);
if(read_size!=json_file_size)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"fread file %s error.",json_file);
goto error_out;
}
json=cJSON_Parse(json_buff);
if (!json)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,"Error before: %-200.200s",cJSON_GetErrorPtr());
goto error_out;
}
ret=set_iris_descriptor(json_file,json, &iris_cfg,logger);
if(ret<0)
{
goto error_out;
}
ret=create_tmp_dir(&iris_cfg);
if(ret<0)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
"create tmp folder %s error",iris_cfg.tmp_iris_dir);
goto error_out;
}
ret=write_iris(json,&iris_cfg,logger);
if(ret<0)
{
goto error_out;
}
memcpy(iris_dir_buf,iris_cfg.tmp_iris_index_dir,MIN(strlen(iris_cfg.tmp_iris_index_dir)+1,(unsigned int)buf_len));
cJSON_Delete(json);
fclose(json_fp);
free(json_buff);
clear_iris_descriptor(&iris_cfg);
return 0;
error_out:
cJSON_Delete(json);
if(json_fp!=NULL)
{
fclose(json_fp);
}
free(json_buff);
clear_iris_descriptor(&iris_cfg);
return -1;
}

2
src/entry/json2iris.h Normal file
View File

@@ -0,0 +1,2 @@
int json2iris(const char* json_file,char* iris_dir_buf,int buf_len,void* logger);

55
src/entry/map_str2int.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include <MESA/MESA_htable.h>
void map_tmp_free(void* ptr)
{
free(ptr);
}
MESA_htable_handle map_create(void)
{
MESA_htable_handle string2int_map;
MESA_htable_create_args_t hargs;
memset(&hargs,0,sizeof(hargs));
hargs.thread_safe=1;
hargs.hash_slot_size = 1024*1024;
hargs.max_elem_num = 0;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_LRU;
hargs.expire_time = 0;
hargs.key_comp = NULL;
hargs.key2index = NULL;
hargs.recursive = 0;
hargs.data_free = map_tmp_free;
hargs.data_expire_with_condition = NULL;
string2int_map=MESA_htable_create(&hargs, sizeof(hargs));
MESA_htable_print_crtl(string2int_map, 0);
return string2int_map;
}
void map_destroy(MESA_htable_handle p)
{
MESA_htable_destroy(p,NULL);
return;
}
int map_register(MESA_htable_handle handle,const char* string,int value)
{
unsigned int size=strlen(string);
unsigned char *key=(unsigned char *)string;
int *data=(int*)malloc(sizeof(int));
int ret=0;
*data=value;
ret=MESA_htable_add(handle,key,size,data);
return ret;
}
int map_str2int(MESA_htable_handle handle,const char* string,int* value)
{
int *data=NULL;
unsigned int size=strlen(string);
data=(int*)MESA_htable_search(handle,(unsigned char*)string,size);
if(data!=NULL)
{
*value=*data;
return 1;
}
else
{
return -1;
}
}

8
src/entry/map_str2int.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __MAP_STR2INT_H_INCLUDE_
#define __MAP_STR2INT_H_INCLUDE_
MESA_htable_handle map_create(void);
void map_destroy(MESA_htable_handle p);
int map_register(MESA_htable_handle handle,const char* string,int value);
int map_str2int(MESA_htable_handle handle,const char* string,int* value);
#endif

View File

@@ -0,0 +1,75 @@
#ifndef MESA_HANDLE__LOGGER_H
#define MESA_HANDLE__LOGGER_H
/*
* runtime_log with handle,
* based on runtime_log.
* yang wei
* create time:2014-03-24
* version:20140324
*/
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define RLOG_LV_DEBUG 10
#define RLOG_LV_INFO 20
#define RLOG_LV_FATAL 30
#define MESA_HANDLE_RUNTIME_LOG(handle, lv, mod, fmt, args...) \
MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \
__FILE__, __LINE__, ##args)
/*
* name: MESA_create_runtime_log_handle
* functionality: get runtime_log handle;
* params:
* file_path: path of log file;
* level: level of log;
* returns:
* not NULL, if succeeded;
* NULL, if file is not absolute path, or failed to create log file;
*/
void *MESA_create_runtime_log_handle(const char *file_path, int level);
/*
* name: MESA_handle_runtime_log
* functionality: appends log message to runtime log file;
* params:
* handle:handle of runtime log, which is created by MESA_create_runtime_log_handle;
* level: log level, messages with level value smaller the global var
* "runtime_log_level" are ignored;
* module: name of loggin module;
* fmt: format string;
* returns:
* none;
*/
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...);
/*
* name: MESA_destroy_runtime_log_handle
* functionality: release runtime log handle memory.
* params:
* handle: runtime log handle which is going to be released;
* returns:
* none;
*/
void MESA_destroy_runtime_log_handle(void *handle);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,275 @@
#ifndef _MESA_HASH_V3_H_
#define _MESA_HASH_V3_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
* general purpose hash table implementation.
*
* xiang hong
* 2002-07-28
*History:
* 2012-03-23 zhengchao add thread safe option and link expire feature;
* 2014-01-27 lijia add reentrant feature.
*/
#define COMPLEX_KEY_SWITCH (1)
#define MESA_HASH_DEBUG (0)
#define ELIMINATE_TYPE_NUM (1)
#define ELIMINATE_TYPE_TIME (2)
#define ELIMINATE_TYPE_MANUAL (3) /* delete oldest item by manual */
typedef void * MESA_htable_handle;
#define HASH_MALLOC(_n_) malloc(_n_)
#define HASH_FREE(_p_) free(_p_)
#if 1
#define HASH_TIME_NOW() time(NULL)
#else
extern volatile time_t g_CurrentTime; /* <20>˱<EFBFBD><CBB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>ÿ<EFBFBD><C3BF>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> */
#define HASH_TIME_NOW() (time_t)g_CurrentTime
#endif
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
/* eliminate algorithm */
#define HASH_ELIMINATE_ALGO_FIFO (0) /* by default */
#define HASH_ELIMINATE_ALGO_LRU (1)
/*
* hash key compare function prototype, see hash_key_comp().
* return value:
* 0:key1 and key2 are equal;
* other:key1 and key2 not equal.
*/
typedef int key_comp_fun_t(const uchar * key1, uint size1, const uchar * key2, uint size2);
/*
* hash key->index computing function prototype, see hash_key2index().
*/
typedef uint key2index_fun_t(const MESA_htable_handle table, const uchar * key, uint size);
typedef long hash_cb_fun_t(void *data, const uchar *key, uint size, void *user_arg);
/*
* thread_safe: 0:create hash table without thread safe features;
* positive:the bigger number has more performance, less collide, but less timeout accuracy.
* max number is 1024.
* recursive: 0:can't recursive call MESA_htable_xxx series function
* 1:can recursive call MESA_htable_xxx series function.
* hash_slot_size: how big do you want the table to be, must be 2^N;
* max_elem_num: the maximum elements of the HASH-table,0 means infinite;
* key_comp: hash key compare function, use default function if NULL;
* suggest implement by yourself.
* key2index: hash key->index computing function, use default function if NULL;
* suggest use MESA_htable built-in function.
* data_free: release resources function, only free attached data pointer if NULL;
* data_expire_with_condition: if expire_time > 0, call this function when a element expire, eliminate always if NULL;
* args:
* data: pointer to attached data;
* type: eliminate reason, ELIMINATE_TYPE_NUM or ELIMINATE_TYPE_TIME;
* return value of 'data_expire_with_condition':
* 1: can be eliminated;
* 0: can't be eliminated, renew the item.
* eliminate_type: the algorithm of elimanate a expired element, 0:FIFO; 1:LRU.
* expire_time: the element expire time in second, 0 means infinite.
*/
typedef struct{
unsigned int thread_safe;
int recursive;
unsigned int hash_slot_size;
unsigned int max_elem_num;
int eliminate_type;
int expire_time;
key_comp_fun_t * key_comp;
key2index_fun_t * key2index;
void (* data_free)(void *data);
int (*data_expire_with_condition)(void *data, int type);
#if COMPLEX_KEY_SWITCH
uchar* (*complex_key_dup)(const uchar *key, uint key_size);
void (* complex_key_free)(uchar *key, uint key_size);
#endif
}MESA_htable_create_args_t;
/*
* name: MESA_htable_create
* functionality: allocats memory for hash slots, and initialize hash structure;
* param:
* args: argments set;
* args_len: length of argment set;
* returns:
* NULL : error;
* Non-NULL : success;
*/
MESA_htable_handle MESA_htable_create(const MESA_htable_create_args_t *args, int args_struct_len);
/*
* get total number of HASH element.
*/
unsigned int MESA_htable_get_elem_num(const MESA_htable_handle table);
/*
* name: MESA_htable_destroy
* functionality: cleans up hash structure, frees memory occupied;
* param:
* table: who is the victim;
* func: callback function to clean up data attached to hash items;
* returns:
* always returns 0;
*/
int MESA_htable_destroy(MESA_htable_handle table, void (* func)(void *));
/*
* name: MESA_htable_add
* functionality: adds item to table, call hash_expire() if elem_count gets
* bigger than threshold_hi, and adjust threshold;
* param:
* table: to which table do you want to add;
* key: what is the label;
* size: how long is the label;
* data: what data do you want to attach;
* returns:
* >0 success,return hash elems' linklist size
* -1, duplicates found and can't add this one;
* -2, memory failure;
* -3, other errors.
*/
int MESA_htable_add(MESA_htable_handle table, const uchar * key, uint size, const void *data);
#if 0
/*
* name: hash_add_with_expire
* functionality: adds item to table, than call hash_expire() on its list
* param:
* table: to which table do you want to add;
* key: what is the label;
* size: how long is the label;
* data: what data do you want to attach;
* returns:
* >0 success,return hash elems' linklist size
* -1, duplicates found and can't add this one;
* -2, memory failure;
*/
int MESA_hash_add_with_expire_v3(MESA_htable_inner_t * table, uchar * key, uint size, void * data);
#endif
/*
* name: MESA_htable_del
* functionality: deletes item from table.
* param:
* table: from which table do you want to delete;
* key : what is the label;
* size : how long is the label;
* func : callback function to clean up data attached to hash items,
if this pointer is NULL will call "data_free" in MESA_hash_create(),
* returns:
* 0, success;
* -1, no such thing;
*/
int MESA_htable_del(MESA_htable_handle table, const uchar * key, uint size,
void (* func)(void *));
/*
* name: MESA_htable_del_oldest_manual
* functionality: deletes oldest item from table.
* param:
* table: from which table do you want to delete;
* func : callback function to clean up data attached to hash items,
if this pointer is NULL will call "data_free" in MESA_hash_create(),
* batch_num: delete oldest items.
* returns:
* 0, success;
* -1, no such thing;
*/
int MESA_htable_del_oldest_manual(MESA_htable_handle table, void (* func)(void *), int batch_num);
/*
* name: MESA_htable_search
* functionality: selects item from table;
* param:
* table: from which table do you want to select;
* key : what is the label;
* size : how long is the label;
*
* return:
* not NULL :pointer to attached data;
* NULL :not found(thus be careful if you are attaching NULL data on purpose).
*/
void *MESA_htable_search(const MESA_htable_handle table, const uchar * key, uint size);
/*
* name: MESA_htable_search_cb
* functionality: selects item from table, and then call 'cb', reentrant;
* in param:
* table: from which table do you want to select;
* key : what is the label;
* size : how long is the label;
* cb : call this function when found the attached data;
* arg : the argument of "cb" function.
* out param:
* cb_ret: the return value of the function "cb".
* return:
* not NULL :pointer to attached data;
* NULL :not found(thus be careful if you are attaching NULL data on purpose).
*/
void *MESA_htable_search_cb(const MESA_htable_handle table, const uchar * key, uint size,
hash_cb_fun_t *cb, void *arg, long *cb_ret);
/*
* name: hash_iterate
* functionality: iterates each hash item;
* params:
* table: what table is to be iterated;
* func: what do you want to do to each attached data item;
* returns:
* 0: iterates all items;
* -1: error;
*/
int MESA_htable_iterate(MESA_htable_handle table, void (* func)(const uchar * key, uint size, void * data, void *user), void * user);
/*
args:
print_switch:
0: disable print message;
1: enable print message;
*/
void MESA_htable_print_crtl(MESA_htable_handle table, int print_switch);
#if 0
/*
* name: hash_expire
* functionality: iterates each item and deletes those that are expired;
* params:
* table: what table do you want to check;
* returns:
* always 0;
*/
int MESA_hash_expire(MESA_htable_inner_t * table);
#endif
#ifdef __cplusplus
}
#endif
#endif /* _LIB_HASH_H_INCLUDED_ */

View File

@@ -0,0 +1,31 @@
#ifndef _LIST_COMMON_H_
#define _LIST_COMMON_H_
typedef struct MESA_list{
struct MESA_list *nextele;
struct MESA_list *preele;
void *quiddity;
}MESA_list_t;
#ifdef __cplusplus
extern "C"
{
#endif
void MESA_list_init_head(struct MESA_list *head);
long MESA_list_get_count(const struct MESA_list *head);
int MESA_list_is_empty(const struct MESA_list *head);
void MESA_list_add(struct MESA_list *head, struct MESA_list *new_list);
void MESA_list_add_tail(struct MESA_list *head, struct MESA_list *new_list);
void MESA_list_del(struct MESA_list *head, struct MESA_list *del_list);
void MESA_list_move(struct MESA_list *head, struct MESA_list *list);
void MESA_list_move_tail(struct MESA_list *head, struct MESA_list *list);
struct MESA_list *MESA_list_join_n(struct MESA_list *head, struct MESA_list *op_place, struct MESA_list *new_obj);
struct MESA_list *MESA_list_join_p(struct MESA_list *head, struct MESA_list *new_obj, struct MESA_list *op_place);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,114 @@
#ifndef _MESA_LIST_V3_H_
#define _MESA_LIST_V3_H_
#ifdef __cplusplus
extern "C"
{
#endif
#define MESA_LQUEUE_VERSION "MESA_lqueue_2014.03.24_12:00:00"
/*
MESA_list <20><><EFBFBD><EFBFBD><EFBFBD>
1-<2D><><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>;
2-<2D><><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>ṹ, <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD>ӿڸ<D3BF><DAB8><EFBFBD><EFBFBD><EFBFBD>;
3-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><E1B9B9>ʹ<EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD>;
*/
#define MESA_LIST_OP_PLACE_HEAD (0x1)
#define MESA_LIST_OP_PLACE_TAIL (0x2)
#define MESA_list_GET (0x1)
#define MESA_list_JOIN (0x2)
#define MESA_list_BOLCK (0x4)
#define MESA_list_NONBOLCK (0x8)
#define MESA_list_JOIN_BLOCK (MESA_list_JOIN|MESA_list_BOLCK)
#define MESA_list_JOIN_NONBLOCK (MESA_list_JOIN|MESA_list_NONBOLCK)
#define MESA_list_GET_BLOCK (MESA_list_GET|MESA_list_BOLCK)
#define MESA_list_GET_NONBLOCK (MESA_list_GET|MESA_list_NONBOLCK)
typedef void * MESA_lqueue_head;
typedef int (* MESA_lqueue_cb_t)(void *data, long data_len, void *arg);
/* All of the following functions return value */
typedef enum{
MESA_QUEUE_RET_OK = 0, /* success */
MESA_QUEUE_RET_COMMON_ERR = -1, /* general<61><6C>undefined errors */
MESA_QUEUE_RET_ARG_ERR = -2, /* invalid args */
MESA_QUEUE_RET_NUM_FULL = -3, /* queue number full */
MESA_QUEUE_RET_MEM_FULL = -4, /* queue memory full */
MESA_QUEUE_RET_QEMPTY = -5, /* queue empty */
MESA_QUEUE_RET_LEN_ERR = -6, /* length error */
MESA_QUEUE_RET_CANT_GET_LOCK = -7, /* can't get lock in non-block mode */
MESA_QUEUE_RET_GET_LOCK_TMOUT = -8, /* get lock timeout */
}MESA_queue_errno_t;
/*
args description:
[IN]
thread_safe : 1:create thread safe queue; 0:without thread safe insurance.
max_item_num: maximum queue items of the queue, 0 means infinity.
*/
MESA_lqueue_head MESA_lqueue_create(int thread_safe, long max_item_num);
/*
attention:
The follow two functions is get some value of queue in a moment,
however, the value you got is not exactly,
because it's maybe changed immediately by other thread when this functions is return.
*/
long MESA_lqueue_get_mem_used(MESA_lqueue_head head);
long MESA_lqueue_get_count(MESA_lqueue_head head);
/*
args description:
[IN]:
lq_head : the handler of MESA_lqueue.
[OUT]:
data : receive buffer.
[IN && OUT]:
data_len:
is value-result argument, like "addrlen of recvfrom(2)",
the caller should initialize the size of the 'data',
will modified on return to indicate the actual size of the queue item.
*/
int MESA_lqueue_read_head(MESA_lqueue_head lq_head, void *data, long *data_len);
int MESA_lqueue_get_head(MESA_lqueue_head lqhead, void *data, long *data_len);
/*
if return value of "cb" is 0, the behaviour is like MESA_lqueue_read_head(),
else if return value of "cb" is not 0, the behaviour is like MESA_lqueue_get_head().
*/
int MESA_lqueue_detect_get_head(MESA_lqueue_head lq_head, MESA_lqueue_cb_t cb, void *data, long *data_len, void *cb_arg);
int MESA_lqueue_get_tail(MESA_lqueue_head lq_head, void *data, long *data_len);
int MESA_lqueue_join_head(MESA_lqueue_head lq_head, const void *data, long data_len);
int MESA_lqueue_join_tail(MESA_lqueue_head lq_head, const void *data, long data_len);
/* these functions features same with above no "try",
except shall return immediately, in other word is "Non-block mode"!
*/
int MESA_lqueue_try_read_head(MESA_lqueue_head lq_head, void *data, long *data_len);
int MESA_lqueue_try_get_head(MESA_lqueue_head lq_head, void *data, long *data_len);
int MESA_lqueue_try_get_tail(MESA_lqueue_head lq_head, void *data, long *data_len);
int MESA_lqueue_try_join_head(MESA_lqueue_head lq_head, const void *data, long data_len);
int MESA_lqueue_try_join_tail(MESA_lqueue_head lq_head, const void *data, long data_len);
void MESA_lqueue_destroy(MESA_lqueue_head head, MESA_lqueue_cb_t cb, void *cb_arg);
#ifdef __cplusplus
}
#endif
#endif

285
src/inc_internal/rulescan.h Normal file
View File

@@ -0,0 +1,285 @@
/*
*
* Copyright (c) 2014
* String Algorithms Research Group
* Institute of Information Engineering, Chinese Academy of Sciences (IIE-CAS)
* National Engineering Laboratory for Information Security Technologies (NELIST)
* All rights reserved
*
* Written by: LIU YANBING (liuyanbing@iie.ac.cn)
* Last modification: 2015-02-03
*
* This code is the exclusive and proprietary property of IIE-CAS and NELIST.
* Usage for direct or indirect commercial advantage is not allowed without
* written permission from the authors.
*
*/
#ifndef H_RULE_SCAN_H
#define H_RULE_SCAN_H
#ifdef __cplusplus
extern "C"
{
#endif
#define MAX_EXPR_ITEM_NUM (1U<<3) /* ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MAX_EXPR_ITEM_NUM<55><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#define MAX_MATCH_POS_NUM 1024 /* ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>õĸ<C3B5><C4B8><EFBFBD> */
/* <20><><EFBFBD>岻ͬ<E5B2BB>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
const unsigned int RULETYPE_STR = 0; /* <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD> */
const unsigned int RULETYPE_REG = 1; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> */
const unsigned int RULETYPE_INT = 2; /* <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
const unsigned int RULETYPE_IPv4 = 3; /* IPv4<76><34><EFBFBD><EFBFBD> */
const unsigned int RULETYPE_IPv6 = 4; /* IPv6<76><36><EFBFBD><EFBFBD> */
const unsigned int MAX_RULETYPE = 5; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
const unsigned int MAX_SUB_RULETYPE = 4096; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͹<EFBFBD><CDB9>򣨿ɱ<F2A3A8BF>ʾ<EFBFBD>ı<EFBFBD><C4B1>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD> */
typedef struct _string_rule_t
{
char * str; /* <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'\0'<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
unsigned int len; /* <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
unsigned char case_sensitive; /* <20>Ƿ<EFBFBD><C7B7><EFBFBD>Сд<D0A1><D0B4><EFBFBD><EFBFBD>ƥ<EFBFBD>䣨1<E4A3A8><31><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD> */
unsigned char match_mode; /* ƥ<><C6A5>ģʽ<C4A3><CABD><EFBFBD>Ӵ<EFBFBD>ƥ<EFBFBD>䣨0<E4A3A8><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD>䣨1<E4A3A8><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ծ<EFBFBD>ȷ<EFBFBD><C8B7>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>Ч */
int l_offset; /* <20><>ʾģʽ<C4A3><CABD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>Χ[l_offset, r_offset]<5D>г<EFBFBD><D0B3>֣<EFBFBD>-1<><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,-2<><32>ʾ<EFBFBD><CABE>ƥ<EFBFBD><EFBFBD><E4A3BB><EFBFBD>Ծ<EFBFBD>ȷ<EFBFBD><C8B7>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>Ч */
int r_offset; /* <20><>ʾģʽ<C4A3><CABD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>Χ[l_offset, r_offset]<5D>г<EFBFBD><D0B3>֣<EFBFBD>-1<><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,-2<><32>ʾ<EFBFBD><CABE>ƥ<EFBFBD><EFBFBD><E4A3BB><EFBFBD>Ծ<EFBFBD>ȷ<EFBFBD><C8B7>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>Ч */
}string_rule_t;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣬱<EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[lb, ub] */
typedef struct _interval_rule_t
{
unsigned int lb; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½磨<C2BD><E7A3A8><EFBFBD><EFBFBD>lb<6C><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ0 */
unsigned int ub; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½磨<C2BD><E7A3A8><EFBFBD><EFBFBD>ub<75><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ0 */
}interval_rule_t;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IPv4<76><34><EFBFBD><EFBFBD> */
typedef struct _ipv4_rule_t
{
unsigned int saddr; /* ԴIP<49><50>ַ<EFBFBD><D6B7>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned int smask; /* ԴIP<49><50>ַ<EFBFBD><D6B7><EFBFBD>룻0<EBA3BB><30>ʾ<EFBFBD>̶<EFBFBD>IP=saddr */
unsigned int daddr; /* Ŀ<><C4BF>IP<49><50>ַ<EFBFBD><D6B7>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned int dmask; /* Ŀ<><C4BF>IP<49><50>ַ<EFBFBD><D6B7><EFBFBD>룻0<EBA3BB><30>ʾ<EFBFBD>̶<EFBFBD>IP=daddr */
unsigned short int min_sport; /* Դ<>˿ڷ<CBBF>Χ<EFBFBD>½磻0<E7A3BB><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned short int max_sport; /* Դ<>˿ڷ<CBBF>Χ<EFBFBD>Ͻ磻0<E7A3BB><30>ʾ<EFBFBD>̶<EFBFBD><CCB6>˿<EFBFBD>=min_sport */
unsigned short int min_dport; /* Ŀ<>Ķ˿ڷ<CBBF>Χ<EFBFBD>½磻0<E7A3BB><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned short int max_dport; /* Ŀ<>Ķ˿ڷ<CBBF>Χ<EFBFBD>Ͻ磻0<E7A3BB><30>ʾ<EFBFBD>̶<EFBFBD><CCB6>˿<EFBFBD>=min_dport */
unsigned short int proto; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>飬6<E9A3AC><36>ʾTCP<43><50>17<31><37>ʾUDP<44><50>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned short int direction; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ˫<CABE><CBAB><EFBFBD><EFBFBD>1<EFBFBD><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD> */
}ipv4_rule_t;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IPv6<76><36><EFBFBD><EFBFBD> */
typedef struct _ipv6_rule_t
{
unsigned int saddr[4]; /* ԴIP<49><50>ַ<EFBFBD><D6B7>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned char smask_bits;/* ԴIP<49><50>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ<EFBFBD>̶<EFBFBD>IP=saddr */
unsigned int daddr[4]; /* Ŀ<><C4BF>IP<49><50>ַ<EFBFBD><D6B7>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned char dmask_bits;/* Ŀ<><C4BF>IP<49><50>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ<EFBFBD>̶<EFBFBD>IP=daddr */
unsigned short int min_sport; /* Դ<>˿ڷ<CBBF>Χ<EFBFBD>½磻0<E7A3BB><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned short int max_sport; /* Դ<>˿ڷ<CBBF>Χ<EFBFBD>Ͻ磻0<E7A3BB><30>ʾ<EFBFBD>̶<EFBFBD><CCB6>˿<EFBFBD>=min_sport */
unsigned short int min_dport; /* Ŀ<>Ķ˿ڷ<CBBF>Χ<EFBFBD>½磻0<E7A3BB><30>ʾ<EFBFBD><CABE><EFBFBD>Ա<EFBFBD><D4B1>ֶ<EFBFBD> */
unsigned short int max_dport; /* Ŀ<>Ķ˿ڷ<CBBF>Χ<EFBFBD>Ͻ磻0<E7A3BB><30>ʾ<EFBFBD>̶<EFBFBD><CCB6>˿<EFBFBD>=min_dport */
unsigned short int proto; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>飬6<E9A3AC><36>ʾTCP<43><50>17<31><37>ʾUDP<44><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ0 */
unsigned short int direction; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ˫<CABE><CBAB><EFBFBD><EFBFBD>1<EFBFBD><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD> */
}ipv6_rule_t;
/* ͨ<>õĹ<C3B5><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
typedef struct _scan_rule_t
{
unsigned int rule_type; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ö<EFBFBD>ٹ<EFBFBD><D9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮һ */
unsigned int sub_type; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6><EFBFBD><E5A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MAX_SUB_RULETYPE<50><45><EFBFBD><EFBFBD>ǰ<EFBFBD>Ķ<EFBFBD><C4B6>壩 */
union /* <20><><EFBFBD><EFBFBD>rule_type<70><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><E4A1A2><EFBFBD><EFBFBD>IP<49><50><EFBFBD><EFBFBD> */
{
string_rule_t string_rule; /* <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD><C6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD> */
interval_rule_t interval_rule; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
ipv4_rule_t ipv4_rule; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IPv4<76><34><EFBFBD><EFBFBD> */
ipv6_rule_t ipv6_rule; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IPv6<76><36><EFBFBD><EFBFBD> */
};
}scan_rule_t;
/* һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> */
typedef struct _boolean_expr_t
{
unsigned int expr_id; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ID */
unsigned int operation; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽִ<CABD>еIJ<D0B5><C4B2><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD>ӣ<EFBFBD>1<EFBFBD><31>ʾɾ<CABE><C9BE> */
unsigned int rnum; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٸ<EFBFBD><D9B8><EFBFBD><EEA3BB><EFBFBD><EFBFBD>operation=1<><31><EFBFBD><EFBFBD>rnum=0<><30><EFBFBD><EFBFBD> */
scan_rule_t * rules; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EEA3BB><EFBFBD><EFBFBD>operation=1<><31><EFBFBD><EFBFBD>rules=NULL<4C><4C><EFBFBD><EFBFBD> */
void * tag; /* <20>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
}boolean_expr_t;
/* <20><>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
typedef struct _text_data_t
{
const char * text; /* <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
unsigned int tlen; /* <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> */
int toffset;/* <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽɨ<CABD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0) */
}text_data_t;
/* <20><>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD>IPv4Ԫ<34><D4AA> */
typedef struct _ipv4_data_t
{
unsigned int saddr; /* ԴIP<49><50>ַ */
unsigned int daddr; /* Ŀ<><C4BF>IP<49><50>ַ */
unsigned short int sport; /* Դ<>˿<EFBFBD> */
unsigned short int dport; /* Ŀ<>Ķ˿<C4B6> */
unsigned short int proto; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>飬6<E9A3AC><36>ʾTCP<43><50>17<31><37>ʾUDP */
}ipv4_data_t;
/* <20><>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD>IPv6Ԫ<36><D4AA> */
typedef struct _ipv6_data_t
{
unsigned int saddr[4]; /* ԴIP<49><50>ַ */
unsigned int daddr[4]; /* Ŀ<><C4BF>IP<49><50>ַ */
unsigned short int sport; /* Դ<>˿<EFBFBD> */
unsigned short int dport; /* Ŀ<>Ķ˿<C4B6> */
unsigned short int proto; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>飬6<E9A3AC><36>ʾTCP<43><50>17<31><37>ʾUDP */
}ipv6_data_t;
/* ͨ<>õĴ<C3B5>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
typedef struct _scan_data_t
{
unsigned int rule_type; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ö<EFBFBD>ٹ<EFBFBD><D9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮һ */
unsigned int sub_type; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6><EFBFBD><E5A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MAX_SUB_RULETYPE<50><45><EFBFBD><EFBFBD>ǰ<EFBFBD>Ķ<EFBFBD><C4B6>壩 */
union /* <20><><EFBFBD><EFBFBD>rule_type<70><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IPԪ<50><D4AA> */
{
text_data_t text_data; /* <20><>ɨ<EFBFBD><C9A8><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD><C6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD> */
unsigned int int_data; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>䣩 */
ipv4_data_t ipv4_data; /* <20><>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD>IPv4Ԫ<34><D4AA> */
ipv6_data_t ipv6_data; /* <20><>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD>IPv6Ԫ<36><D4AA> */
};
}scan_data_t;
/*
ɨ<><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>scan_result_t<5F><74>rule_result_t˵<74><CBB5><EFBFBD><EFBFBD>
1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>Ӧһ<D3A6><D2BB>scan_result_t<5F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>rnum<75><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>scan_result_t::result[k]<5D><>0<=k<rnum<75><6D><EFBFBD><EFBFBD>
2<><32><EFBFBD><EFBFBD><EFBFBD>ھ<EFBFBD>ȷ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>position<6F><6E>length<74><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
<09><><EFBFBD>еľ<D0B5>ȷ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>result_num<75><6D>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>i<EFBFBD><69>0<=i<result_num<75><6D><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>úͳ<C3BA><CDB3>ȷֱ<C8B7><D6B1>ǣ<EFBFBD>
(position[i], length[i])
3<><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>position<6F><6E>length<74><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
<09><><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>result_num<75><6D>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>group_num+1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ء<EFBFBD><D8A1><EFBFBD><EFBFBD>ڵ<EFBFBD>i<EFBFBD><69>0<=i<result_num<75><6D><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<09><>a<EFBFBD><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽƥ<CABD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>úͳ<C3BA><CDB3>ȷֱ<C8B7><D6B1>ǣ<EFBFBD>
(position[(group_num+1)*i], length[(group_num+1)*i])
<09><>b<EFBFBD><62><EFBFBD><EFBFBD>j<EFBFBD><6A>0<=j<group_num<75><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>úͳ<C3BA><CDB3>ȷֱ<C8B7><D6B1>ǣ<EFBFBD>
(position[(group_num+1)*i+j+1], length[(group_num+1)*i+j+1])
*/
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
typedef struct _rule_result_t
{
unsigned int rule_type; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ö<EFBFBD>ٹ<EFBFBD><D9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮һ */
unsigned int group_num; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>͹<EFBFBD><CDB9>򣬱<EFBFBD><F2A3ACB1>ֶα<D6B6>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飨capturing group<75><70><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0 */
unsigned int result_num; /* <20>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD> */
unsigned int position[MAX_MATCH_POS_NUM]; /* <20>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC><CEBB> */
unsigned int length[MAX_MATCH_POS_NUM]; /* <20>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еģ<D0B5><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD>Ӧ<EFBFBD><D3A6>length=0, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<49><EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0*/
}rule_result_t;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
typedef struct _scan_result_t
{
unsigned int expr_id; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ID */
unsigned int rnum; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٸ<EFBFBD><D9B8><EFBFBD><EFBFBD><EFBFBD> */
rule_result_t result[MAX_EXPR_ITEM_NUM]; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void * tag; /* <20>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
}scan_result_t;
/*
<09><><EFBFBD>ܣ<EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
max_thread_num [in]: ɨ<><C9A8><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>ִ<EFBFBD>е<EFBFBD><D0B5>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
ɨ<><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪNULLʱ<4C><CAB1><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ʼ<EFBFBD><CABC>ʧ<EFBFBD>ܡ<EFBFBD>
*/
void * rulescan_initialize(unsigned int max_thread_num);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>̬ע<CCAC><D7A2>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󡣶<EFBFBD><F3A1A3B6><EFBFBD>ͬһ<CDAC><D2BB>instance<63><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<CDAC>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̡߳<DFB3>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
instance[in]: ɨ<><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>
expr_array[in]: һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>
epxr_num[in]: <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
failed_ids[out]: <09>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id<69><64>failed_ids[0]<5D><>ʾ<EFBFBD>Ƿ<EFBFBD>id<69>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>failed_ids[1...failed_ids[0]]<5D><>¼<EFBFBD>Ƿ<EFBFBD>id<69><64>Ŀǰֻ<C7B0>Ժ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0B2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>Ч<EFBFBD><D0A7>
failed_size[in]: failed_ids<64><73><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С<EFBFBD><D0A1>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
<09><><EFBFBD><EFBFBD>ֵΪ1ʱ<31><CAB1><EFBFBD><EFBFBD>ʾע<CABE><D7A2><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ-1ʱ<31><CAB1><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int rulescan_update(void * instance, boolean_expr_t * expr_array, unsigned int expr_num, unsigned int * failed_ids, unsigned int failed_size);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3>ͷ<EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣻱<EFBFBD><F3A3BBB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>Ρ<EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
instance [in]: ɨ<><C9A8><EFBFBD><EFBFBD>ָ<EFBFBD>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
<09>ޡ<EFBFBD>
*/
void rulescan_destroy(void * instance);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʽɨ<CABD><EFBFBD><E8A3AC><EFBFBD><EFBFBD><EBB1A3><EFBFBD><EFBFBD>״̬<D7B4>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
instance [in]: ɨ<><C9A8><EFBFBD><EFBFBD>ָ<EFBFBD>
thread_id [in]: <20><>ǰִ<C7B0><D6B4>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>id<69><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>Χ[0, max_thread_num-1]֮<>ڡ<EFBFBD>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
<09><><EFBFBD>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪNULLʱ<4C><CAB1><EFBFBD><EFBFBD>ʾʧ<CABE>ܡ<EFBFBD>
*/
void * rulescan_startstream(void * instance, unsigned int thread_id);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>ʽɨ<CABD><C9A8><EFBFBD>ӿڣ<D3BF>ɨ<EFBFBD><C9A8>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>scan_data<74><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬stream_param<61>У<EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8>ģʽ<C4A3><CABD>
<09><>1<EFBFBD><31>presults<74><73>ΪNULL<4C><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>*presults<74><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>˴ε<CBB4><CEB5><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD>
<09><>2<EFBFBD><32>presultsΪNULL <20><><EFBFBD><EFBFBD>ȫɨ<C8AB><C9A8>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>ʾ<EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ټ<EFBFBD><D9BC><EFBFBD><E3B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD>
<20><><EFBFBD><EFBFBD>rulescan_computeresult<6C><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
stream_param [in]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>
scan_data [in]: <20><>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
presults [out]: <20><><EFBFBD><EFBFBD>presults<74><73>ΪNULL<4C><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
size [in]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>presults<74>Ĵ<EFBFBD>С<EFBFBD><D0A1>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ȫɨ<C8AB>裨presultsΪNULL<4C><4C><EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD>ֵΪ<D6B5><CEAA><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD>еĹؼ<C4B9><D8BC>ʣ<EFBFBD><CAA3><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>䡢IP<49><50><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD>presult<6C><74>ΪNULL<4C><4C><EFBFBD>򷵻<EFBFBD>ֵΪ<D6B5><CEAA><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>-1<><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int rulescan_searchstream(void * stream_param, scan_data_t * scan_data, scan_result_t * presults, unsigned int size);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣬼<EFBFBD><F3A3ACBC><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
stream_param [in]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>ָ<EFBFBD>
presults [out]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>id<69><64>
size [in]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>presults<74>Ĵ<EFBFBD>С<EFBFBD><D0A1>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><=size<7A><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ-1<><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int rulescan_computeresult(void * stream_param, scan_result_t * presults, unsigned int size);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽɨ<CABD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
stream_param [in]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
<09>ޡ<EFBFBD>
*/
void rulescan_endstream(void * stream_param);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>ʽɨ<CABD><C9A8><EFBFBD>ӿڣ<D3BF>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󷵻<EFBFBD><F3B7B5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
instance [in]: ɨ<><C9A8><EFBFBD><EFBFBD>ָ<EFBFBD>
thread_id [in]: <20><>ǰִ<C7B0><D6B4>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>id<69><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>Χ[0, max_thread_num-1]֮<>ڣ<EFBFBD>
scan_data [in]: <20><>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
presults [out]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>id<69><64>
size [in]: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>presults<74>Ĵ<EFBFBD>С<EFBFBD><D0A1>
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><=size<7A><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ-1<><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int rulescan_search(void * instance, unsigned int thread_id, scan_data_t * scan_data, scan_result_t * presults, unsigned int size);
#ifdef __cplusplus
}
#endif
#endif /* !defined(H_RULE_SCAN_H) */

562
src/inc_internal/stream.h Normal file
View File

@@ -0,0 +1,562 @@
#ifndef _APP_STREAM_H_
#define _APP_STREAM_H_
/*************************************************************************************
Update Log:
2014-09-17 LiJia:
(1)<29><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>struct ipaddr<64><EFBFBD><E1B9B9>, <20><><EFBFBD><EFBFBD>ƽ̨<C6BD>ṹstruct layer_addr<64><72><EFBFBD>ڴ<EFBFBD><DAB4>ֲ<EFBFBD><D6B2>ϼ<EFBFBD><CFBC><EFBFBD>;
(2)<29><><EFBFBD><EFBFBD><EFBFBD>½ӿ<C2BD>:
int MESA_kill_tcp_synack(struct streaminfo *stream, const void *raw_pkt);
<09><><EFBFBD><EFBFBD>HMDģʽ<C4A3>µ<EFBFBD>FD.
2014-09-10 LiJia:
(1)<29><><EFBFBD><EFBFBD><EFBFBD>½ӿ<C2BD>:
int MESA_inject_pkt(struct streaminfo *stream, const char *payload,
int payload_len, const void *raw_pkt, UCHAR snd_routedir);
<09><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><>ԭ<EFBFBD><D4AD>MESA_fakepacket_send_tcp()<29><><EFBFBD><EFBFBD><EFBFBD>˺ܶ<CBBA><DCB6><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>.
**************************************************************************************/
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <string.h>
#define USE_MESA_STREAM_HASH (1) /* ʹ<>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3>µ<EFBFBD>HASH<53><48> */
extern int MESA_PLATFORM_VERSION_20140917; /* ƽ̨<C6BD><EFBFBD><E6B1BE> */
#define ADDR_UNSYMMETRY (0) /* 2014-04-25, <20><><EFBFBD>Դ<EFBFBD>XinJing<6E><67><EFBFBD><EFBFBD><EFBFBD>İ<EFBFBD>, <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>VLAN<41><4E>ַ<EFBFBD><D6B7><EFBFBD>Գ<EFBFBD><D4B3><EFBFBD><EFBFBD><EFBFBD>, <20>˺겻Ӧ<EAB2BB><D3A6><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD> */
#if 0
#define FOR_863 (0) /* for 863<36><33>Ŀ<EFBFBD>û<EFBFBD><C3BB><EFBFBD>ǩģ<C7A9><C4A3> */
#define FOR_108 (0)
#define RAW_IP_FRAG_PKT (0) /* 2013-08-21 LiJia add, <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>ԭʼIP<49><50>Ƭ<EFBFBD><C6AC> */
#endif
#ifndef UINT8
typedef unsigned char UINT8;
#endif
#ifndef UCHAR
typedef unsigned char UCHAR;
#endif
#ifndef UINT16
typedef unsigned short UINT16;
#endif
#ifndef UINT32
typedef unsigned int UINT32;
#endif
#ifndef UINT64
typedef unsigned long long UINT64;
#endif
//<2F><><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define DIR_C2S 0x01
#define DIR_S2C 0x02
#define DIR_DOUBLE 0x03
//<2F><><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define DIR_ROUTE_UP 0x00
#define DIR_ROUTE_DOWN 0x01
//<2F><>ַ<EFBFBD><D6B7><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD>
enum addr_type_t{
__ADDR_TYPE_INIT = 0,
ADDR_TYPE_IPV4, /* 1, <20><><EFBFBD><EFBFBD>IPv4<76><34>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>Ϣ */
ADDR_TYPE_IPV6, /* 2, <20><><EFBFBD><EFBFBD>IPv6<76><36>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>Ϣ */
ADDR_TYPE_VLAN, /* 3 */
ADDR_TYPE_MAC, /* 4 */
ADDR_TYPE_ARP = 5, /* 5 */
ADDR_TYPE_GRE, /* 6 */
ADDR_TYPE_MPLS, /* 7 */
ADDR_TYPE_PPPOE_SES, /* 8 */
ADDR_TYPE_TCP, /* 9 */
ADDR_TYPE_UDP = 10, /* 10 */
ADDR_TYPE_L2TP, /* 11 */
//ADDR_TYPE_STREAM_TUPLE4_V4, /* 12, <20><><EFBFBD>ϵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>IPv4<76><34>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>Ϣ */
//ADDR_TYPE_STREAM_TUPLE4_V6, /* 13, <20><><EFBFBD>ϵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>IPv6<76><36>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>Ϣ */
__ADDR_TYPE_IP_PAIR_V4, /* 14, <20><>IPv4<76><34>ַ<EFBFBD><D6B7> */
__ADDR_TYPE_IP_PAIR_V6, /* 15, <20><>IPv6<76><36>ַ<EFBFBD><D6B7> */
__ADDR_TYPE_MAX, /* 16 */
};
#define TCP_TAKEOVER_STATE_FLAG_OFF 0
#define TCP_TAKEOVER_STATE_FLAG_ON 1
//Ӧ<>ò㿴<C3B2><E3BFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>
#define OP_STATE_PENDING 0
#define OP_STATE_REMOVE_ME 1
#define OP_STATE_CLOSE 2
#define OP_STATE_DATA 3
//Ӧ<>ò㷵<C3B2>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define APP_STATE_GIVEME 0x00
#define APP_STATE_DROPME 0x01
#define APP_STATE_FAWPKT 0x00
#define APP_STATE_DROPPKT 0x10
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD>
enum stream_type_t{
STREAM_TYPE_NON = 0, /* <20><><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>, <20><>VLAN, IP<49><50><EFBFBD><EFBFBD> */
STREAM_TYPE_TCP,
STREAM_TYPE_UDP,
STREAM_TYPE_SOCKS4,
STREAM_TYPE_SOCKS5,
STREAM_TYPE_HTTP_PROXY,
STREAM_TYPE_PPPOE,
};
#define PROXY_STATE_SEL 0
#define PROXY_STATE_LINK_IN 1
/* ԭʼ<D4AD><CABC><EFBFBD>ṹ */
typedef struct {
enum addr_type_t low_layer_type; /* ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MAC(pcap<61><70><EFBFBD><EFBFBD>), Ҳ<><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IPv4(pag<61><67><EFBFBD><EFBFBD>) */
int raw_pkt_len; /* ԭʼ<D4AD><CABC><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD> */
const void *raw_pkt_data; /* ԭʼ<D4AD><CABC>ͷָ<CDB7><D6B8>, <20><><EFBFBD><EFBFBD>low_layer_type<70>ж<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
struct timeval raw_pkt_ts; /* ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>ȫΪ0<CEAA><30><EFBFBD><EFBFBD>֧<EFBFBD>ִ˹<D6B4><CBB9><EFBFBD>(<28><>pagģʽ) */
}raw_pkt_t;
// <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
struct proxy_node
{
UINT16 iType; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, 0 <20><>ʾ<EFBFBD><CABE>Ч
UINT16 uiPort; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
UINT16 uiUserLen;
UINT16 uiPwdLen;
UINT16 uiApendLen;
UCHAR opstate; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
UCHAR dealstate; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
UINT32 uiIP; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<49><50>ַv4, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
UCHAR *pIpv6; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<49><50>ַ, v6<76><36>ַ
UCHAR *pUser; // <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
UCHAR *pPwd; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UCHAR *append; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>url
void *apme; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>url
struct proxy_node *pnext;
} ;
typedef struct raw_ipfrag_list{
void *frag_packet;
int pkt_len;
int type; /* IPv4 or IPv6 */
struct raw_ipfrag_list *next;
}raw_ipfrag_list_t;
struct buf_unorder
{
struct buf_unorder *next;
struct buf_unorder *prev;
void *data;
UINT32 len;
UINT32 tcpdatalen;
UINT32 urg_ptr;
char fin;
char urg;
char rst;
unsigned char pad;
UINT32 seq;
UINT32 ack;
raw_ipfrag_list_t *ipfrag_list;
raw_pkt_t raw_pkt; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢ԭʼ<D4AD><CABC> */
};
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>壺*/
struct half_tcpstream
{
UCHAR *data;
UINT32 offset; /*data<74>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>*/
UINT32 count; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊֹ<CEAA><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>*/
UINT32 count_new; /*<2A><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>*/
UINT32 count_ideal; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊֹ<CEAA><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>*/
UINT32 pktcout; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ۼƵ<DBBC><C6B5><EFBFBD><EFBFBD>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD>*/
UINT32 totallost; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ۼƶ<DBBC><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT32 seq; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>seq<65><71><EFBFBD><EFBFBD>*/
UINT32 first_data_seq; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>ĵ<EFBFBD>seq<65><71><EFBFBD><EFBFBD>*/
UINT32 ack_seq; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>*/
UINT16 window; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>С*/
UCHAR maxunorder; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ*/
UCHAR finstate; /*fin״̬*/
UINT32 pktcout_new; /*<2A><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD>*/
UINT32 unorder_cnt;
struct buf_unorder *unorderlist; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
struct buf_unorder *unorderlisttail; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>ָ<EFBFBD><D6B8>*/
};
/* <20><><EFBFBD><EFBFBD>papp */
struct tuple4 {
u_int saddr;
u_int daddr;
u_short source;
u_short dest;
};
struct tuple6
{
UCHAR saddr[16] ;
UCHAR daddr[16] ;
UINT16 source;
UINT16 dest;
};
/* network-order */
struct stream_tuple4_v4{
UINT32 saddr; /* network order */
UINT32 daddr; /* network order */
UINT16 source; /* network order */
UINT16 dest; /* network order */
};
#ifndef IPV6_ADDR_LEN
#define IPV6_ADDR_LEN (sizeof(struct in6_addr))
#endif
struct stream_tuple4_v6
{
UCHAR saddr[IPV6_ADDR_LEN] ;
UCHAR daddr[IPV6_ADDR_LEN] ;
UINT16 source; /* network order */
UINT16 dest; /* network order */
};
#define GRE_TAG_LEN (4)
struct layer_addr_gre
{
UINT16 gre_id;
};
#define VLAN_ID_MASK (0x0FFF)
#define VLAN_TAG_LEN (4)
struct layer_addr_vlan
{
UINT16 vlan_id; /* network order */
};
struct layer_addr_pppoe_session
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ver:4;
unsigned int type:4;
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned int type:4;
unsigned int ver:4;
#endif
unsigned char code;
unsigned short session_id;
};
#ifndef MAC_ADDR_LEN
#define MAC_ADDR_LEN (6)
#endif
struct layer_addr_mac
{
UCHAR src_mac[MAC_ADDR_LEN]; /* network order */
UCHAR dst_mac[MAC_ADDR_LEN]; /* network order */
};
struct layer_addr_ipv4
{
UINT32 saddr; /* network order */
UINT32 daddr; /* network order */
/* 2014-04-21 lijia add,
Ϊ<>˿ռ<D5BC><E4A1A2><EFBFBD><EFBFBD><EFBFBD>ԡ<EFBFBD><D4A1><EFBFBD>Ч<EFBFBD><D0A7>, <20><>ǿ<EFBFBD>ư<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>,
IP<49><50><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>Ϣ, TCP<43><50>ֻ<EFBFBD><D6BB>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD>˿<EFBFBD><CBBF>ڴ<EFBFBD>,
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA>ʱ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫget_tuple4()<29><><EFBFBD><EFBFBD>.
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP, <20>˿<EFBFBD><CBBF><EFBFBD>ϢΪ0;
*/
UINT16 source; /* network order */
UINT16 dest; /* network order */
};
struct layer_addr_ipv6
{
UCHAR saddr[IPV6_ADDR_LEN] ; /* network order */
UCHAR daddr[IPV6_ADDR_LEN] ; /* network order */
/* 2014-04-21 lijia add,
Ϊ<>˿ռ<D5BC><E4A1A2><EFBFBD><EFBFBD><EFBFBD>ԡ<EFBFBD><D4A1><EFBFBD>Ч<EFBFBD><D0A7>, <20><>ǿ<EFBFBD>ư<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>,
IP<49><50><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>Ϣ, TCP<43><50>ֻ<EFBFBD><D6BB>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD>˿<EFBFBD><CBBF>ڴ<EFBFBD>,
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA>ʱ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫget_tuple4()<29><><EFBFBD><EFBFBD>.
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP, <20>˿<EFBFBD><CBBF><EFBFBD>ϢΪ0;
*/
UINT16 source;/* network order */
UINT16 dest;/* network order */
};
struct layer_addr_tcp
{
UINT16 source; /* network order */
UINT16 dest; /* network order */
};
struct layer_addr_udp
{
UINT16 source; /* network order */
UINT16 dest; /* network order */
};
struct layer_addr_l2tp
{
UINT32 tunnelid; /* network order */
UINT32 sessionid; /* network order */
};
struct layer_addr
{
UCHAR addrtype; /* <20><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD> enum addr_type_t */
/* Ϊ<>˷<EFBFBD><CBB7><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD>ȡ<EFBFBD><C8A1>ַ, <20>˴<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ʡȥָ<C8A5><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
union
{
struct stream_tuple4_v4 *tuple4_v4;
struct stream_tuple4_v6 *tuple4_v6;
struct layer_addr_ipv4 *ipv4;
struct layer_addr_ipv6 *ipv6;
struct layer_addr_vlan *vlan;
struct layer_addr_mac *mac;
struct layer_addr_gre *gre;
struct layer_addr_tcp *tcp;
struct layer_addr_udp *udp;
struct layer_addr_pppoe_session *pppoe_ses;
struct layer_addr_l2tp *l2tp;
void *paddr;
};
UCHAR addrlen; /* <20><>ַ<EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD> */
};
/* <20><><EFBFBD><EFBFBD><EFBFBD>˽ṹ<CBBD><E1B9B9><EFBFBD>ں<EFBFBD>papp<70><70><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ʱ, <20><><EFBFBD><EFBFBD>struct layer_addrǿת. */
struct ipaddr
{
UCHAR addrtype;
union
{
struct stream_tuple4_v4 *v4;
struct stream_tuple4_v6 *v6;
}paddr;
// struct tuple4 *paddr;
};
/* to do:
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD>, <20><>stream.h<><68><EFBFBD><EFBFBD>, <20>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>stream.h<>IJ<EFBFBD><C4B2><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>ͼ;
*/
struct streaminfo
{
struct layer_addr addr; //<2F><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Ϣ
struct streaminfo *pfather;//<2F>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><E1B9B9>
UCHAR type; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UCHAR threadnum; // <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
UCHAR dir:2; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч, <20><><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x01:c-->s; 0x02:s-->c; 0x03 c<-->s; */
UCHAR curdir:2; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч, <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD>, 0x01:c-->s; 0x02:s-->c */
UCHAR layer_dir:2; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч, <20><>ǰ<EFBFBD><C7B0><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ĭ<EFBFBD>Ϲ<EFBFBD><CFB9><EFBFBD>"<22><><EFBFBD>˿<EFBFBD><CBBF>ǿͻ<C7BF><CDBB><EFBFBD>"<22><>ͬ */
UCHAR stream_dir:2; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч, <20><><EFBFBD>Ĵ洢<C4B4>ĵ<EFBFBD>ַ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ĭ<EFBFBD>Ϲ<EFBFBD><CFB9><EFBFBD>"<22><><EFBFBD>˿<EFBFBD><CBBF>ǿͻ<C7BF><CDBB><EFBFBD>"<22><>ͬ */
UCHAR dirreverse; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ip<69><70>ַ<EFBFBD><D6B7>ת, <20><><EFBFBD><EFBFBD>"<22><><EFBFBD>˿<EFBFBD><CBBF>ǿͻ<C7BF><CDBB><EFBFBD>"<22><><EFBFBD><EFBFBD><EFBFBD>෴ */
UCHAR opstate; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
UCHAR pktstate; //<2F><><EFBFBD>ӵİ<D3B5><C4B0><EFBFBD><EFBFBD><EFBFBD>
UCHAR routedir; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч, <20><><EFBFBD>˹<EFBFBD>ָ<EFBFBD><D6B8>, <20><><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ͬ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, 0:<3A><><EFBFBD><EFBFBD>; 1:<3A><><EFBFBD><EFBFBD> */
UCHAR addr_use_as_hash; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>addr<64>Ƿ<EFBFBD><C7B7><EFBFBD>ΪHASH<53><48><EFBFBD><EFBFBD><EFBFBD>ͱȽϵIJ<CFB5><C4B2><EFBFBD>, <20><>:MAC<41><43>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
UCHAR stream_killed_flag; /* 2014-08-22 lijia add, <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>, <20>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Kill, ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>Drop<6F><70>Kill, <20><><EFBFBD><EFBFBD><EFBFBD>ٸ<EFBFBD><D9B8>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD> */
UCHAR __pad__[7]; //<2F><><EFBFBD><EFBFBD>8<EFBFBD>ֽڶ<D6BD><DAB6><EFBFBD>
void *pproject; //ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>̿<EFBFBD><CCBF><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʹ<EFBFBD>ã<EFBFBD>
void *pdetail; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><CFB8>Ϣ
const void *p_layer_header; /* ָ<>򱾲<EFBFBD><F2B1BEB2><EFBFBD>ͷ<EFBFBD><CDB7>ָ<EFBFBD><D6B8>, <20><><EFBFBD>ڹ<EFBFBD><DAB9><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ȡ<EFBFBD><C8A1><EFBFBD>ز<EFBFBD><D8B2><EFBFBD> */
#if 0 //FOR_108, ->pproject
unsigned long stream_id;
#endif
#if 0 //RAW_IP_FRAG_PKT, ->pproject
raw_ipfrag_list_t *ipfrag_list; /* 2013-08-21 LiJia add, ԭʼ<D4AD><CABC>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#endif
#if 0 //FOR_863, ->pproject
char terminal_tag[40]; /* <20>û<EFBFBD><C3BB><EFBFBD>ǩ */
#endif
};
struct tcpdetail
{
void *pdata; //<2F><><EFBFBD><EFBFBD>
UINT32 datalen; //<2F><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
UINT32 lostlen;
UCHAR multisynflag:4; // multi syn
UCHAR needackflag:2; //<2F><>Ҫ<EFBFBD>ϴ<EFBFBD>ack<63><6B><EFBFBD><EFBFBD>
UCHAR takeoverflag:2;
UCHAR tcpstateflag; // <20><><EFBFBD>ڼ<EFBFBD>¼tcp<63>ĻỰSYN<59><4E><EFBFBD><EFBFBD>״̬
UCHAR link_state; // <20><><EFBFBD>ӵ<EFBFBD>״̬
UCHAR creat_mod;
UINT32 regionid; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
struct half_tcpstream *pclient; //<2F><>client<6E><74>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
struct half_tcpstream *pserver; //<2F><> server<65><72>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
UINT32 serverpkt;
UINT32 clientpkt;
UINT32 servercount;
UINT32 clientcount;
UINT64 creattime;
UINT64 lastmtime;
UINT32 iserverseq; //<2F><><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>ʱ<EFBFBD><CAB1>ʱ<EFBFBD>洢seq
UINT32 iclientseq; //<2F><><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>ʱ<EFBFBD><CAB1>ʱ<EFBFBD>洢seq
void *apme; //Ӧ<>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void *pAllpktpme; //<2F><>״̬<D7B4><CCAC>tcp<63><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct proxy_node *pproxylist; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><E9B8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>
};
struct udpdetail
{
void *pdata; //<2F><><EFBFBD><EFBFBD>
UINT32 datalen; //<2F><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
UINT32 regionid; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
UINT32 serverpkt;
UINT32 clientpkt;
UINT32 servercount;
UINT32 clientcount;
UINT64 creattime;
UINT64 lastmtime;
void *apme; //Ӧ<>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
//ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD>ʱsession_state״̬
#define SESSION_STATE_PENDING 0x01
#define SESSION_STATE_DATA 0x02
#define SESSION_STATE_CLOSE 0x04
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>ʱ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>ֵ<EFBFBD><D6B5>
#define PROT_STATE_GIVEME 0x01
#define PROT_STATE_DROPME 0x02
#define PROT_STATE_KILLME 0x04
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
typedef struct _plugin_session_info
{
unsigned short plugid; //plugid<69><64>ƽ̨<C6BD><CCA8><EFBFBD><EFBFBD>
char session_state; //<2F>Ự״̬<D7B4><CCAC>PENDING,DATA,CLOSE
char _pad_; //<2F><><EFBFBD><EFBFBD>
int buflen; //<2F><>ǰ<EFBFBD>ֶγ<D6B6><CEB3><EFBFBD>
long long prot_flag; //<2F><>ǰ<EFBFBD>ֶε<D6B6>flagֵ
void *buf; //<2F><>ǰ<EFBFBD>ֶ<EFBFBD>
void* app_info; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
}stSessionInfo;
#ifdef __cplusplus
extern "C" {
#endif
/*
args:
[IN]:
stream:<3A><><EFBFBD><EFBFBD><E1B9B9>ָ<EFBFBD><D6B8>;
[OUT]
addr_type:<3A><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ADDR_TYPE_IPV4 or ADDR_TYPE_IPV6;
return value:
NULL : error;
NON-NULL : ָ<><D6B8><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>;
*/
struct layer_addr *get_stream_tuple4(struct streaminfo *this_stream);
int get_thread_count(void);
typedef char (*STREAM_CB_FUN_T)(struct streaminfo *pstream,void **pme, int thread_seq,const void *raw_pkt);
typedef char (*IPv4_CB_FUN_T)(struct streaminfo *pstream,unsigned char routedir,int thread_seq, const void *raw_pkt);
typedef char (*IPv6_CB_FUN_T)(struct streaminfo *pstream,unsigned char routedir,int thread_seq, const void *raw_pkt);
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
a_*<2A><> <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ;
f_*: <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>Ϣ;
raw_pkt: ԭʼ<D4AD><CABC>ָ<EFBFBD><D6B8>, ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ'raw_pkt_t';
pme: ˽<><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD>ʱΪNULL;
thread_seq<65><71><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD>;
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
APP_STATE_GIVEME<4D><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򱾺<EFBFBD><F2B1BEBA><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0><EFBFBD>
APP_STATE_DROPME<4D><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򱾺<EFBFBD><F2B1BEBA><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0><EFBFBD>
APP_STATE_FAWPKT<4B><54><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
APP_STATE_DROPPKT<4B><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
*/
char IPv4_ENTRY_EXAMPLE( struct streaminfo *f_stream,unsigned char routedir,int thread_seq, const void *raw_pkt);
char IPv6_ENTRY_EXAMPLE( struct streaminfo *f_stream,unsigned char routedir,int thread_seq,const void *raw_pkt);
char TCP_ENTRY_EXAMPLE(struct streaminfo *a_tcp, void **pme, int thread_seq,const void *raw_pkt);
char UDP_ENTRY_EXAMPLE(struct streaminfo *a_udp, void **pme, int thread_seq,const void *raw_pkt);
int stream_register_tcp_allpkt (STREAM_CB_FUN_T fun);
int stream_register_tcp_takeover (STREAM_CB_FUN_T fun);
int stream_register_tcp (STREAM_CB_FUN_T fun);
int stream_register_udp (STREAM_CB_FUN_T fun);
int stream_register_ip (IPv4_CB_FUN_T fun);
int stream_register_ipv6 (IPv6_CB_FUN_T fun);
void *dictator_malloc(int thread_seq,size_t size);
void dictator_free(int thread_seq,void *pbuf);
void *dictator_realloc(int thread_seq, void* pbuf, size_t size);
char PROT_PROCESS(stSessionInfo* session_info, void **pme, int thread_seq,struct streaminfo *a_stream,void *a_packet);
/* <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ǩ */
//int terminal_tag_probe(struct streaminfo *stream, struct mesa_tcp_hdr *this_tcphdr,void *rawippkt);
const unsigned char *get_terminal_tag(struct streaminfo *stream);
//int MESA_kill_tcp(struct streaminfo *stream, struct ip *a_packet);
int MESA_kill_tcp(struct streaminfo *stream, const void *raw_pkt);
int MESA_kill_tcp_synack(struct streaminfo *stream, const void *raw_pkt);
/*
ARG:
stream: <20><><EFBFBD><EFBFBD><E1B9B9>ָ<EFBFBD><D6B8>;
payload: Ҫ<><D2AA><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>;
payload_len: Ҫ<><D2AA><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8>س<EFBFBD><D8B3><EFBFBD>;
raw_pkt: ԭʼ<D4AD><CABC>ָ<EFBFBD><D6B8>;
snd_routedir: Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵķ<DDB5><C4B7><EFBFBD>, ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ:stream->routedir ,
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭʼ<D4AD><CABC>ͬ<EFBFBD><CDAC>, snd_dir = stream->routedir,
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, snd_dir = stream->routedir ^ 3(<28><>1<--->2, 2<--->1<><31><EFBFBD><EFBFBD>ѧת<D1A7><D7AA>);
return value:
-1: error.
>0: <20><><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD>ݰ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>(payload_len + <20>ײ<EFBFBD><D7B2><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>);
*/
int MESA_inject_pkt(struct streaminfo *stream, const char *payload, int payload_len, const void *raw_pkt, UCHAR snd_routedir);
/* 2014-07-31 LiJia add, for set one stream unorder number */
int tcp_set_single_stream_max_unorder(struct streaminfo *stream, UCHAR dir, UCHAR unorder_num);
raw_ipfrag_list_t *get_raw_frag_list(struct streaminfo *stream);
//void frags_list_free_one(raw_ipfrag_list_t *frags_list, int);
//raw_ipfrag_list_t *frags_list_merge(raw_ipfrag_list_t *new_merge_list, raw_ipfrag_list_t *old_list);
//raw_ipfrag_list_t *raw_ip_frag_list_attach(int thread_num);
//void raw_ip_frag_list_detach(int thread_num);
unsigned long get_stream_id(struct streaminfo *stream);
void MESA_stream_list_free(struct streaminfo *heap_stream);
struct streaminfo *MESA_stream_list_dup(struct streaminfo *stack_stream, int reverse);
int MESA_stream_list_cmp(struct streaminfo *stream1, struct streaminfo *stream2);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,336 @@
#ifndef _APP_STREAM_H_
#define _APP_STREAM_H_
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <string.h>
#define FOR_863 (0) /* for 863<36><33>Ŀ<EFBFBD>û<EFBFBD><C3BB><EFBFBD>ǩģ<C7A9><C4A3> */
#define FOR_108 (0)
#define RAW_IP_FRAG_PKT (0) /* 2013-08-21 LiJia add, <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>ԭʼIP<49><50>Ƭ<EFBFBD><C6AC> */
typedef unsigned char UINT8;
typedef unsigned long long UINT64;
typedef unsigned int UINT32;
typedef unsigned short UINT16;
typedef unsigned char UCHAR;
//<2F><><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define DIR_C2S 0x01
#define DIR_S2C 0x02
#define DIR_DOUBLE 0x03
//<2F><><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define DIR_ROUTE_UP 0x00
#define DIR_ROUTE_DOWN 0x01
//<2F><>ַ<EFBFBD><D6B7><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD>
#define ADDR_TYPE_IPV4 1
#define ADDR_TYPE_IPV6 2
#define ADDR_TYPE_VLAN 3
#define TCP_TAKEOVER_STATE_FLAG_OFF 0
#define TCP_TAKEOVER_STATE_FLAG_ON 1
//Ӧ<>ò㿴<C3B2><E3BFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>
#define OP_STATE_PENDING 0
#define OP_STATE_REMOVE_ME 1
#define OP_STATE_CLOSE 2
#define OP_STATE_DATA 3
//Ӧ<>ò㷵<C3B2>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define APP_STATE_GIVEME 0x00
#define APP_STATE_DROPME 0x01
#define APP_STATE_FAWPKT 0x00
#define APP_STATE_DROPPKT 0x10
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD>
#define STREAM_TYPE_TCP 1
#define STREAM_TYPE_UDP 2
#define STREAM_TYPE_VLAN 3
#define STREAM_TYPE_SOCKS4 4
#define STREAM_TYPE_SOCKS5 5
#define STREAM_TYPE_HTTP_PROXY 6
#define STREAM_TYPE_MPLS 7
#define PROXY_STATE_SEL 0
#define PROXY_STATE_LINK_IN 1
// <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
struct proxy_node
{
UINT16 iType; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, 0 <20><>ʾ<EFBFBD><CABE>Ч
UINT16 uiPort; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
UINT16 uiUserLen;
UINT16 uiPwdLen;
UINT16 uiApendLen;
UCHAR opstate; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
UCHAR dealstate; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
UINT32 uiIP; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<49><50>ַv4, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
UCHAR *pIpv6; // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<49><50>ַ, v6<76><36>ַ
UCHAR *pUser; // <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
UCHAR *pPwd; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UCHAR *append; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>url
void *apme; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>url
struct proxy_node *pnext;
} ;
#if RAW_IP_FRAG_PKT
typedef struct raw_ipfrag_list{
void *frag_packet;
int pkt_len;
int type; /* IPv4 or IPv6 */
struct raw_ipfrag_list *next;
}raw_ipfrag_list_t;
#endif
struct buf_unorder
{
struct buf_unorder *next;
struct buf_unorder *prev;
void *data;
UINT32 len;
UINT32 tcpdatalen;
UINT32 urg_ptr;
char fin;
char urg;
char rst;
unsigned char pad;
UINT32 seq;
UINT32 ack;
#if RAW_IP_FRAG_PKT
raw_ipfrag_list_t *ipfrag_list;
#endif
};
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>壺*/
struct half_tcpstream
{
UCHAR *data;
UINT32 offset; /*data<74>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>*/
UINT32 count; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊֹ<CEAA><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>*/
UINT32 count_new; /*<2A><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>*/
UINT32 count_ideal; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊֹ<CEAA><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>*/
UINT32 pktcout; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ۼƵ<DBBC><C6B5><EFBFBD><EFBFBD>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD>*/
UINT32 totallost; /*<2A><><EFBFBD><EFBFBD><EFBFBD>ۼƶ<DBBC><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT32 seq; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>seq<65><71><EFBFBD><EFBFBD>*/
UINT32 first_data_seq; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>ĵ<EFBFBD>seq<65><71><EFBFBD><EFBFBD>*/
UINT32 ack_seq; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>*/
UINT16 window; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>С*/
UCHAR maxunorder; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ*/
UCHAR finstate; /*fin״̬*/
UINT32 pktcout_new; /*<2A><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD>*/
UINT32 unorder_cnt;
struct buf_unorder *unorderlist; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
struct buf_unorder *unorderlisttail; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>ָ<EFBFBD><D6B8>*/
};
#ifndef STRUCT_TUPLE4_DEFINED
#define STRUCT_TUPLE4_DEFINED (1)
struct tuple4 {
UINT32 saddr;
UINT32 daddr;
UINT16 source;
UINT16 dest;
};
#endif
struct tuple6
{
UCHAR saddr[16] ;
UCHAR daddr[16] ;
UINT16 source;
UINT16 dest;
};
#define VLAN_ID_LEN 4
struct tuplevlan
{
UCHAR vlan_id[VLAN_ID_LEN];
};
struct tupell2tp
{
UINT32 tunnelid;
UINT32 sessionid;
};
struct ipaddr
{
UCHAR addrtype; //<2F><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD> ipv6 or ipv4
union
{
struct tuple4 *v4;
struct tuple6 *v6;
}paddr;
// struct tuple4 *paddr;
};
struct streaminfo
{
struct ipaddr addr; //ip<69>˿<EFBFBD><CBBF><EFBFBD>Ϣ
struct streaminfo *pfather;//<2F>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><E1B9B9>
UCHAR type; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UCHAR threadnum; // <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
UCHAR dir:2; // <20><><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x01 c-->s 0x02 s-->c 0x03 c<-->s
UCHAR curdir:2; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>0x01 c-->s 0x02 s-->c
UCHAR dirreverse:4; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ip<69><70>ַ<EFBFBD><D6B7>ת
UCHAR opstate; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
UCHAR pktstate; //<2F><><EFBFBD>ӵİ<D3B5><C4B0><EFBFBD><EFBFBD><EFBFBD>
UCHAR routedir; //<2F><><EFBFBD><EFBFBD>λΪ1<CEAA><31>ʾ<EFBFBD><CABE><EFBFBD>У<EFBFBD>Ϊ0<CEAA><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>; <20>ڴ<EFBFBD><DAB4><EFBFBD>ģʽ<C4A3><CABD>,<2C><><37><CEBB>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>豸ID
UCHAR __pad__[2]; //<2F><><EFBFBD><EFBFBD>8<EFBFBD>ֽڶ<D6BD><DAB6><EFBFBD>
void *pproject; //ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>̿<EFBFBD><CCBF><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʹ<EFBFBD>ã<EFBFBD>
void *pdetail; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><CFB8>Ϣ
#if FOR_108
unsigned long stream_id;
#endif
#if RAW_IP_FRAG_PKT
raw_ipfrag_list_t *ipfrag_list; /* 2013-08-21 LiJia add, ԭʼ<D4AD><CABC>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#endif
#if FOR_863
char terminal_tag[40]; /* <20>û<EFBFBD><C3BB><EFBFBD>ǩ */
#endif
};
struct tcpdetail
{
void *pdata; //<2F><><EFBFBD><EFBFBD>
UINT32 datalen; //<2F><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
UINT32 lostlen; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
UCHAR multisynflag:4; // multi syn
UCHAR needackflag:2; //<2F><>Ҫ<EFBFBD>ϴ<EFBFBD>ack<63><6B><EFBFBD><EFBFBD>
UCHAR takeoverflag:2;
UCHAR tcpstateflag; // <20><><EFBFBD>ڼ<EFBFBD>¼tcp<63>ĻỰSYN<59><4E><EFBFBD><EFBFBD>״̬
UCHAR link_state; // <20><><EFBFBD>ӵ<EFBFBD>״̬
UCHAR pad[5];
struct half_tcpstream *pclient; //<2F><>client<6E><74>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
struct half_tcpstream *pserver; //<2F><> server<65><72>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
UINT32 serverpkt;
UINT32 clientpkt;
UINT32 servercount;
UINT32 clientcount;
UINT64 creattime;
UINT64 lastmtime;
UINT32 iserverseq; //<2F><><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>ʱ<EFBFBD><CAB1>ʱ<EFBFBD>洢seq
UINT32 iclientseq; //<2F><><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>ʱ<EFBFBD><CAB1>ʱ<EFBFBD>洢seq
void *apme; //Ӧ<>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void *pAllpktpme; //<2F><>״̬<D7B4><CCAC>tcp<63><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct proxy_node *pproxylist; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><E9B8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>
};
struct udpdetail
{
void *pdata; //<2F><><EFBFBD><EFBFBD>
UINT32 datalen; //<2F><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
UINT32 pad;
UINT32 serverpkt;
UINT32 clientpkt;
UINT32 servercount;
UINT32 clientcount;
UINT64 creattime;
UINT64 lastmtime;
void *apme; //Ӧ<>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
//ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD>ʱsession_state״̬
#define SESSION_STATE_PENDING 0x01
#define SESSION_STATE_DATA 0x02
#define SESSION_STATE_CLOSE 0x04
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>ʱ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>ֵ<EFBFBD><D6B5>
#define PROT_STATE_GIVEME 0x01
#define PROT_STATE_DROPME 0x02
#define PROT_STATE_KILLME 0x04
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
typedef struct _plugin_session_info
{
unsigned short plugid; //plugid<69><64>ƽ̨<C6BD><CCA8><EFBFBD><EFBFBD>
char session_state; //<2F>Ự״̬<D7B4><CCAC>PENDING,DATA,CLOSE
char _pad_; //<2F><><EFBFBD><EFBFBD>
int buflen; //<2F><>ǰ<EFBFBD>ֶγ<D6B6><CEB3><EFBFBD>
long long prot_flag; //<2F><>ǰ<EFBFBD>ֶε<D6B6>flagֵ
void *buf; //<2F><>ǰ<EFBFBD>ֶ<EFBFBD>
void* app_info; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
}stSessionInfo;
#ifdef __cplusplus
extern "C" {
#endif
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
a_*<2A><> <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>
f_*: <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>
a_packet: ԭʼip<69><70><EFBFBD><EFBFBD>
pme: ˽<><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD>ʱΪNULL
thread_seq<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
APP_STATE_GIVEME<4D><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򱾺<EFBFBD><F2B1BEBA><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0><EFBFBD>
APP_STATE_DROPME<4D><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򱾺<EFBFBD><F2B1BEBA><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0><EFBFBD>
APP_STATE_FAWPKT<4B><54><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
APP_STATE_DROPPKT<4B><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
*/
char IP_ENTRY( struct streaminfo *f_stream,unsigned char routedir,int thread_seq,struct ip * a_packet);
char IPv6_ENTRY( struct streaminfo *f_stream,unsigned char routedir,int thread_seq,struct ip6_hdr *a_packet);
char TCP_ENTRY(struct streaminfo *a_tcp, void **pme, int thread_seq,void *a_packet);
char UDP_ENTRY(struct streaminfo *a_udp, void **pme, int thread_seq,void *a_packet);
int stream_register_tcp_allpkt (char (*x)(struct streaminfo *pstream,void **pme, int thread_seq,void *a_packet));
int stream_register_tcp_takeover (char (*x)(struct streaminfo *pstream,void **pme, int thread_seq,void *a_packet));
int stream_register_tcp (char (*x)(struct streaminfo *pstream,void **pme, int thread_seq,void *a_packet));
int stream_register_udp (char (*x)(struct streaminfo *pstream,void **pme, int thread_seq,void *a_packet));
//int stream_register_ip (char (*x)(struct streaminfo *pstream,void **pme, int thread_seq,void *a_packet));
int stream_register_ip (char (*x)(struct streaminfo *f_stream,unsigned char routedir,int thread_seq,struct ip * a_packet));
int stream_register_ipv6 (char (*x)(struct streaminfo *f_stream,unsigned char routedir,int thread_seq,struct ip6_hdr *a_packet));
char * printaddr (struct ipaddr *paddrinfo,int threadindex);
void *dictator_malloc(int thread_seq,int size);
void dictator_free(int thread_seq,void *pbuf);
char PROT_PROCESS(stSessionInfo* session_info, void **pme, int thread_seq,struct streaminfo *a_stream,void *a_packet);
/* <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ǩ */
int terminal_tag_probe(struct streaminfo *stream, struct tcphdr *this_tcphdr,void *rawippkt);
const unsigned char *get_terminal_tag(struct streaminfo *stream);
int MESA_kill_tcp(struct streaminfo *stream, struct ip *a_packet);
#if RAW_IP_FRAG_PKT
extern raw_ipfrag_list_t **G_IP_FRAG_LIST;
void frags_list_free(raw_ipfrag_list_t *frags_list);
raw_ipfrag_list_t *frags_list_merge(raw_ipfrag_list_t *new_merge_list, raw_ipfrag_list_t *old_list);
#endif
#if FOR_108
unsigned long papp_get_streamid();
#endif
#ifdef __cplusplus
}
#endif
#endif

6
test/Makefile Normal file
View File

@@ -0,0 +1,6 @@
LIBS=../lib/libmaatframe.so -lMESA_handle_logger -lMESA_htable -lrulescan -lpcre -lpthread
INC=-I../inc/ -I/usr/include/MESA/
all:
g++ -o maat_test -g -Wall maat_test.cpp $(INC) $(LIBS)
clean:
rm maat_test test.log* -f

185
test/maat_json.json Normal file
View File

@@ -0,0 +1,185 @@
{
"compile_table": "COMPILE",
"group_table": "GROUP",
"rules": [
{
"compile_id": 123,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_1",
"regions": [
{
"table_name": "IP_CONFIG",
"table_type": "ip",
"table_content": {
"addr_type": "ipv4",
"src_ip": "10.0.6.201",
"mask_src_ip": "255.255.0.0",
"src_port": "0",
"mask_src_port": "65535",
"dst_ip": "0.0.0.0",
"mask_dst_ip": "255.255.255.255",
"dst_port": "0",
"mask_dst_port": "65535",
"protocol": 6,
"direction": "double"
}
},
{
"table_name": "IP_CONFIG",
"table_type": "ip",
"table_content": {
"addr_type": "ipv6",
"src_ip": "2001:da8:205:1::101",
"mask_src_ip": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0000",
"src_port": "0",
"mask_src_port": "65535",
"dst_ip": "0::0",
"mask_dst_ip": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"dst_port": "0",
"mask_dst_port": "65535",
"protocol": 6,
"direction": "double"
}
}
]
},
{
"group_name": "group_2",
"regions": [
{
"table_name": "HTTP_URL",
"table_type": "string",
"table_content": {
"keywords": "abckkk&123",
"expr_type": "and",
"match_method": "sub",
"format": "uncase plain"
}
}
]
}
]
},
{
"compile_id": 124,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_1"
},
{
"group_name": "group_3",
"regions": [
{
"table_name": "CONTENT_SIZE",
"table_type": "intval",
"table_content": {
"low_boundary": 100,
"up_boundary": 500
}
}
]
}
]
},
{
"compile_id": 125,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_4",
"regions": [
{
"table_name": "HTTP_URL",
"table_type": "string",
"table_content": {
"keywords": "action=search\\&query=(.*)",
"expr_type": "regex",
"match_method": "sub",
"format": "uncase plain"
}
}
]
}
]
},
{
"compile_id": 126,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_5",
"regions": [
{
"table_name": "HTTP_URL",
"table_type": "string",
"table_content": {
"keywords": "should_not_hit_any_rule",
"expr_type": "none",
"match_method": "sub",
"format": "uncase plain"
}
}
]
},
{
"group_name": "group_6",
"regions": [
{
"table_name": "CONTENT_SIZE",
"table_type": "intval",
"table_content": {
"low_boundary": 2014,
"up_boundary": 2016
}
}
]
}
]
}
],
"plugin_table": [
{
"table_name": "QD_ENTRY_INFO",
"table_content": [
"1\t192.168.0.1\t101",
"2\t192.168.0.2\t101",
"3\t192.168.1.1\t102"
]
},
{
"table_name": "TEST_PLUGIN_TABLE",
"table_content": [
"1\t3388\t99\t1",
"2\t3355\t66\t1",
"3\tcccc\t11\t1"
]
}
]
}

185
test/maat_json.json.bak Normal file
View File

@@ -0,0 +1,185 @@
{
"compile_table": "COMPILE",
"group_table": "GROUP",
"rules": [
{
"compile_id": 123,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_1",
"regions": [
{
"table_name": "IP_CONFIG",
"table_type": "ip",
"table_content": {
"addr_type": "ipv4",
"src_ip": "10.0.6.201",
"mask_src_ip": "255.255.0.0",
"src_port": "0",
"mask_src_port": "65535",
"dst_ip": "0.0.0.0",
"mask_dst_ip": "255.255.255.255",
"dst_port": "0",
"mask_dst_port": "65535",
"protocol": 6,
"direction": "double"
}
},
{
"table_name": "IP_CONFIG",
"table_type": "ip",
"table_content": {
"addr_type": "ipv6",
"src_ip": "2001:da8:205:1::101",
"mask_src_ip": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"src_port": "0",
"mask_src_port": "65535",
"dst_ip": "0::0",
"mask_dst_ip": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"dst_port": "0",
"mask_dst_port": "65535",
"protocol": 6,
"direction": "double"
}
}
]
},
{
"group_name": "group_2",
"regions": [
{
"table_name": "HTTP_URL",
"table_type": "string",
"table_content": {
"keywords": "abckkk&123",
"expr_type": "and",
"match_method": "sub",
"format": "uncase plain"
}
}
]
}
]
},
{
"compile_id": 124,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_1"
},
{
"group_name": "group_3",
"regions": [
{
"table_name": "CONTENT_SIZE",
"table_type": "intval",
"table_content": {
"low_boundary": 100,
"up_boundary": 500
}
}
]
}
]
},
{
"compile_id": 125,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_4",
"regions": [
{
"table_name": "HTTP_URL",
"table_type": "string",
"table_content": {
"keywords": "action=search\\&query=(.*)",
"expr_type": "regex",
"match_method": "sub",
"format": "uncase plain"
}
}
]
}
]
},
{
"compile_id": 126,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"effective_rage": 0,
"user_region": "anything",
"is_valid": "yes",
"groups": [
{
"group_name": "group_5",
"regions": [
{
"table_name": "HTTP_URL",
"table_type": "string",
"table_content": {
"keywords": "should_not_hit_any_rule",
"expr_type": "none",
"match_method": "sub",
"format": "uncase plain"
}
}
]
},
{
"group_name": "group_6",
"regions": [
{
"table_name": "CONTENT_SIZE",
"table_type": "intval",
"table_content": {
"low_boundary": 2014,
"up_boundary": 2016
}
}
]
}
]
}
],
"plugin_table": [
{
"table_name": "QD_ENTRY_INFO",
"table_content": [
"1\t192.168.0.1\t101",
"2\t192.168.0.2\t101",
"3\t192.168.1.1\t102"
]
},
{
"table_name": "TEST_PLUGIN_TABLE",
"table_content": [
"1\t3388\t99\t1",
"2\t3355\t66\t1",
"3\tcccc\t11\t1"
]
}
]
}

View File

@@ -0,0 +1,5 @@
0000000004
123 1 1 1 1 0 anything 1
124 1 1 1 1 0 anything 1
125 1 1 1 1 0 anything 1
126 1 1 1 1 0 anything 1

View File

@@ -0,0 +1,3 @@
0000000002
3 2 100 500 1
6 5 2014 2016 1

View File

@@ -0,0 +1,8 @@
0000000007
0 123 1
1 123 1
0 124 1
2 124 1
3 125 1
4 126 1
5 126 1

View File

@@ -0,0 +1,4 @@
0000000003
2 1 abckkk&123 1 0 0 1
4 3 action=search\&query=(.*) 2 0 0 1
5 4 should_not_hit_any_rule 0 0 0 1

View File

@@ -0,0 +1,3 @@
0000000002
0 0 4 10.0.6.201 255.255.0.0 0 65535 0.0.0.0 255.255.255.255 0 65535 6 0 1
1 0 4 192.168.0.1 255.255.255.255 0 65535 0.0.0.0 255.255.255.255 0 65535 6 0 1

View File

@@ -0,0 +1,4 @@
0000000003
1 192.168.0.1 101
2 192.168.0.2 101
3 192.168.1.1 102

View File

@@ -0,0 +1,4 @@
0000000003
1 3388 99 1
2 3355 66 1
3 cccc 11 1

View File

@@ -0,0 +1,4 @@
0000000003
1 plugname1 1
2 plugname2 1
3 plugname3 1

View File

@@ -0,0 +1 @@
0000000000

View File

@@ -0,0 +1,7 @@
COMPILE 4 ./maat_json.json_iris_tmp/COMPILE.local
GROUP 7 ./maat_json.json_iris_tmp/GROUP.local
TEST_PLUGIN_TABLE 3 ./maat_json.json_iris_tmp/TEST_PLUGIN_TABLE.local
IP_CONFIG 2 ./maat_json.json_iris_tmp/IP_CONFIG.local
CONTENT_SIZE 2 ./maat_json.json_iris_tmp/CONTENT_SIZE.local
HTTP_URL 3 ./maat_json.json_iris_tmp/HTTP_URL.local
QD_ENTRY_INFO 3 ./maat_json.json_iris_tmp/QD_ENTRY_INFO.local

201
test/maat_test.cpp Normal file
View File

@@ -0,0 +1,201 @@
#include "Maat_rule.h"
#include <MESA/MESA_handle_logger.h>
#include <dlfcn.h>
#include <assert.h>
#include <sys/socket.h>//inet_addr
#include <netinet/in.h>//inet_addr
#include <arpa/inet.h>//inet_addr
#include <net/if.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <MESA/stream.h>
void Maat_read_entry_start_cb(int update_type,void* u_para)
{
return;
}
void Maat_read_entry_cb(int table_id,const char* table_line,void* u_para)
{
char ip_str[16]={0};
int entry_id=-1,seq=-1;
unsigned int ip_uint=0;
unsigned int local_ip_nr=16820416;//192.168.0.1
sscanf(table_line,"%d\t%s\t%d",&seq,ip_str,&entry_id);
inet_pton(AF_INET,ip_str,&ip_uint);
if(local_ip_nr==ip_uint)
{
printf("Load entry id %d SUCCESS.\n",entry_id);
}
return;
}
void Maat_read_entry_finish_cb(void* u_para)
{
return;
}
int main()
{
Maat_feather_t feather=NULL;
int cb_table_id=-1,url_scan_table_id=-1,size_scan_table_id=-1,ip_scan_table_id=-1;
int ret=-1;
int g_iThreadNum=4;
const char* table_info_path="./table_info.conf";
const char* json_path="./maat_json.json";
// const char* ful_cfg_dir="./maat_json.json_iris_tmp/index";
// const char* inc_cfg_dir="./rules/inc/index";
const char* log_file="./test.log";
const char* scan_data="http://www.cyberessays.com/search_results.php?action=search&query=yulingjing,abckkk,1234567";
int scan_val=2015;
struct Maat_rule_t result[4];
int found_pos[4];
scan_status_t mid=NULL;
void *logger=MESA_create_runtime_log_handle(log_file,0);
feather=Maat_summon_feather_json(g_iThreadNum,
table_info_path,
json_path,
logger);
// feather=Maat_summon_feather(g_iThreadNum,table_info_path,ful_cfg_dir,inc_cfg_dir,logger);
if(feather==NULL)
{
printf("Maat initial error, see %s\n",log_file);
return -1;
}
cb_table_id=Maat_table_register(feather,"QD_ENTRY_INFO");
if(cb_table_id==-1)
{
printf("Database table QD_ENTRY_INFO register failed.");
}
else
{
ret=Maat_table_callback_register(feather, cb_table_id,
Maat_read_entry_start_cb,
Maat_read_entry_cb,
Maat_read_entry_finish_cb,
logger);
if(ret<0)
{
printf("Maat callback register table QD_ENTRY_INFO error.");
}
}
url_scan_table_id=Maat_table_register(feather,"HTTP_URL");
if(url_scan_table_id==-1)
{
printf("Database table HTTP_URL register failed.");
}
else
{
ret=Maat_full_scan_string(feather, url_scan_table_id,CHARSET_GBK, scan_data, strlen(scan_data),
result,found_pos, 4,
&mid, 0);
switch(ret)
{
case -1:
printf("scan error.\n");
break;
case -2:
printf("hit current region,but not hit compile rule.\n");
break;
case 0:
printf("nothing hit\n");
break;
default://>0
printf("hit %d rules\n",ret);
break;
}
}
Maat_clean_status(&mid);
size_scan_table_id=Maat_table_register(feather,"CONTENT_SIZE");
if(size_scan_table_id==-1)
{
printf("Database table CONTENT_SIZE register failed.");
}
else
{
ret=Maat_scan_intval(feather, size_scan_table_id, scan_val, result,4,&mid, 0);
switch(ret)
{
case -1:
printf("scan error.\n");
break;
case -2:
printf("hit current region,but not hit compile rule.\n");
break;
case 0:
printf("nothing hit\n");
break;
default://>0
printf("hit %d rules\n",ret);
break;
}
}
Maat_clean_status(&mid);
struct Maat_hit_detail_t *hit_detail=(struct Maat_hit_detail_t *)malloc(sizeof(struct Maat_hit_detail_t)*10);
stream_para_t sp=Maat_stream_scan_string_start(feather,url_scan_table_id,0);
int detail_ret=0;
if(sp==NULL)
{
printf("stream scan start failed.\n");
return -1;
}
ret=Maat_stream_scan_string_detail(&sp,CHARSET_NONE,"www.cyberessays.com", strlen("www.cyberessays.com")
,result,4,hit_detail,10
,&detail_ret,&mid);
ret=Maat_stream_scan_string_detail(&sp,CHARSET_NONE,scan_data, strlen(scan_data)
,result,4,hit_detail,10
,&detail_ret,&mid);
Maat_stream_scan_string_end(&sp);
struct ipaddr ipv4_addr,ipv6_addr;
struct stream_tuple4_v4 v4_addr;
struct stream_tuple4_v6 v6_addr;
ipv4_addr.addrtype=ADDR_TYPE_IPV4;
inet_pton(AF_INET,"10.0.6.205",&(v4_addr.saddr));
v4_addr.source=htons(50001);
inet_pton(AF_INET,"10.0.6.201",&(v4_addr.daddr));
v4_addr.dest=htons(80);
ipv4_addr.v4=&v4_addr;
ip_scan_table_id=Maat_table_register(feather,"IP_CONFIG");
if(ip_scan_table_id==-1)
{
printf("Database table IP_CONFIG register failed.");
}
else
{
ret=Maat_scan_proto_addr(feather,ip_scan_table_id,&ipv4_addr,6,result,4, &mid,0);
if(ret>0)
{
printf("ipv4 scan hit compile rule id %d.\n",result[0].config_id);
}
}
Maat_clean_status(&mid);
ipv6_addr.addrtype=ADDR_TYPE_IPV6;
inet_pton(AF_INET6,"2001:da8:205:1::101",&(v6_addr.saddr));
v6_addr.source=htons(50001);
inet_pton(AF_INET6,"2001:da8:205:1::102",&(v6_addr.daddr));
v6_addr.dest=htons(80);
ipv6_addr.v6=&v6_addr;
if(ip_scan_table_id>=0)
{
ret=Maat_scan_proto_addr(feather,ip_scan_table_id,&ipv6_addr,6,result,4, &mid,0);
if(ret==-2)
{
printf("ipv6 scan hit region.\n");
}
else
{
printf("ipv6 scan result:%d ,shoulde be -2.\n",ret);
}
}
Maat_burn_feather(feather);
free(hit_detail);
return 0;
}

15
test/table_info.conf Normal file
View File

@@ -0,0 +1,15 @@
#each collumn seperate with '\t'
#id (0~65535)
#name string
#type one of ip,expr,compile or plugin
#src_charset one of GBK,BIG5,UNICODE,UTF8
#dst_charset combined by GBK,BIG5,UNICODE,UTF8,seperate with '/'
#do_merege yes or no
#id name type src_charset dst_charset do_merge
0 COMPILE compile GBK GBK no 0
1 GROUP group GBK GBK no 0
2 HTTP_URL expr GBK GBK/BIG5/UNICODE/UTF8 yes 128
3 KEYWORDS_TABLE expr GBK GBK/BIG5/UNICODE/UTF8 yes 0
4 IP_CONFIG ip GBK GBK no 0
5 CONTENT_SIZE intval GBK GBK no 0
6 QD_ENTRY_INFO plugin GBK GBK no 0