summaryrefslogtreecommitdiff
path: root/include/MESA/rulescan.h
blob: c5d28014eaec1be4dab0055dc88ab1123957a6e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 *
 * 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    ([email protected])
 * Last modification: 2016-06-05
 *
 * 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

	/* rulescan_set_param���������õIJ������� */
	enum RULESCAN_PARA_NAME
	{
		RULESCAN_DETAIL_RESULT=1,		/* ����־λ��ʾ��������ϸ����λ�õ���Ϣ, optval��ΪNULL��optlen��Ϊ0��Ĭ�ϲ�������ϸ��Ϣ*/
		RULESCAN_REGEX_GROUP  =2,		/* ����־λ��ʾ�������������ʽƥ��ķ�����Ϣ���������ֶΣ���Ҫ������RULESCAN_DETAIL_RESULT��־λ,optval��ΪNULL��optlen��Ϊ0��Ĭ�ϲ����ط�����Ϣ */
		RULESCAN_QUICK_SCAN,		    /* ������Ҫ����ɨ��ģʽ��sub_type, ���û��Լ����壬optval��ֵΪ0-4096��optlen��Ϊ4��Ĭ����������ͨɨ��ģʽ��һ������ʽ�б��뱣֤�ñ���ʽ�������������Ҫô
										����quickģʽ����Ҫô���Ƿ�quickģʽ */
		RULEACAN_ERRLOG_CLOSE,			/* ����־λ��ʾ���ر�Rulescan������־�����optval��ΪNULL��optlen��Ϊ0�������õĻ�Ĭ�ϴ�Rulescan������־��� */
		RULESCAN_ERRLOG_FILE_PATH,		/* ����Rulescan������־��·�����������ļ����������û����룬optval��ֵΪ�����ļ�������־·����optlenΪ·�����ȡ����û���趨��
										����־Ĭ�ϴ洢�ڿ�ִ�г���ǰĿ¼�µ�rulescan_tmp�� */
	};

	#define MAX_REGEX_GROUP_NUM 5		 /* �����������ʽ����֧�ֵ�������ĸ��� */	

	#define MAX_EXPR_ITEM_NUM  (1U<<3)   /* ÿ�������ʽ�����MAX_EXPR_ITEM_NUM��������� */
	#define MAX_MATCH_POS_NUM  1024      /* ÿ����������������ص�����λ�õĸ��� */
	#define MATCH_POS_NUM_INC  64		 /* ÿ�������������ص�����λ�õĸ�����ʼֵ������ֵ */

	/* ���岻ͬ�Ĺ�������  */
	const unsigned int RULETYPE_STR  = 0; /* �ַ���������ƹ��� */
	const unsigned int RULETYPE_REG  = 1; /* �������ʽ���� */
	const unsigned int RULETYPE_INT  = 2; /* ��ֵ������� */
	const unsigned int RULETYPE_IPv4 = 3; /* IPv4���� */
	const unsigned int RULETYPE_IPv6 = 4; /* IPv6���� */

	const unsigned int MAX_RULETYPE     = 5;          /* ������������ */
	const unsigned int MAX_SUB_RULETYPE = 4096;        /* �������������� */

	/* �ַ������͹��򣨿ɱ�ʾ�ı��ַ������������ַ������������ʽ�� */
	typedef struct _string_rule_t
	{
		char *        str;            /* �ַ������ݣ�������������ʽ������'\0'�������ɲ�ָ������ */
		unsigned int  len;            /* �ַ������� */
		unsigned char case_sensitive; /* �Ƿ��Сд����ƥ�䣨1�����У�0�������У� */
		unsigned char match_mode;     /* ƥ��ģʽ���Ӵ�ƥ�䣨0��������ƥ�䣨1�������Ծ�ȷ��ƥ����Ч */
		int  l_offset;                /* ��ʾģʽ��ֻ�����ı���Χ[l_offset, r_offset]�г��֣�-1��ʾ������,-2��ʾ��ƥ�䣻���Ծ�ȷ��ƥ����Ч */
		int  r_offset;                /* ��ʾģʽ��ֻ�����ı���Χ[l_offset, r_offset]�г��֣�-1��ʾ������,-2��ʾ��ƥ�䣻���Ծ�ȷ��ƥ����Ч */
	}string_rule_t;

	/* ������ֵ������򣬱�ʾ��������[lb, ub] */
	typedef struct _interval_rule_t
	{
		unsigned int lb; /* ����������½磨����lb����������Ĭ��Ϊ0 */
		unsigned int ub; /* ����������½磨����ub����������Ĭ��Ϊ0 */
	}interval_rule_t;

	/* �������IPv4���� */
	typedef struct _ipv4_rule_t
	{
		unsigned int       saddr;     /* ԴIP��ַ��0��ʾ���Ա��ֶ� */
		unsigned int       smask;     /* ԴIP��ַ���룻0��ʾ�̶�IP=saddr */
		unsigned int       daddr;     /* Ŀ��IP��ַ��0��ʾ���Ա��ֶ� */
		unsigned int       dmask;     /* Ŀ��IP��ַ���룻0��ʾ�̶�IP=daddr */
		unsigned short int min_sport; /* Դ�˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
		unsigned short int max_sport; /* Դ�˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_sport */
		unsigned short int min_dport; /* Ŀ�Ķ˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
		unsigned short int max_dport; /* Ŀ�Ķ˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_dport */
		unsigned short int proto;     /* �����Э�飬6��ʾTCP��17��ʾUDP��0��ʾ���Ա��ֶ� */
		unsigned short int direction; /* ����0��ʾ˫��1��ʾ���� */
	}ipv4_rule_t;

	/* �������IPv6���� */
	typedef struct _ipv6_rule_t
	{
		unsigned int       saddr[4];  /* ԴIP��ַ��0��ʾ���Ա��ֶ� */
		unsigned int       smask[4];  /* ԴIP��ַ���룻0��ʾ�̶�IP=saddr */
		unsigned int       daddr[4];  /* Ŀ��IP��ַ��0��ʾ���Ա��ֶ� */
		unsigned int       dmask[4];  /* Ŀ��IP��ַ���룻0��ʾ�̶�IP=daddr */
		unsigned short int min_sport; /* Դ�˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
		unsigned short int max_sport; /* Դ�˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_sport */
		unsigned short int min_dport; /* Ŀ�Ķ˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
		unsigned short int max_dport; /* Ŀ�Ķ˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_dport */
		unsigned short int proto;     /* �����Э�飬6��ʾTCP��17��ʾUDP��������Ĭ��Ϊ0 */
		unsigned short int direction; /* ����0��ʾ˫��1��ʾ���� */
	}ipv6_rule_t;

	/* ͨ�õĹ������� */
	typedef struct _scan_rule_t
	{
		unsigned int rule_type;              /* �������ͣ�����Ϊ����ö�ٹ�������֮һ */
		unsigned int sub_type;               /* �������ͣ��û��Զ��壬�������͸�������������MAX_SUB_RULETYPE����ǰ�Ķ��壩 */
		union                                /* ����rule_type�����������ַ�������ֵ���䡢����IP���� */
		{
			string_rule_t    string_rule;    /* �ַ��������ַ����������ơ��������ʽ�� */
			interval_rule_t  interval_rule;  /* ������ֵ������� */
			ipv4_rule_t      ipv4_rule;      /* �������IPv4���� */
			ipv6_rule_t      ipv6_rule;      /* �������IPv6���� */
		};
	}scan_rule_t;

	/* һ�������ʽ���� */
	typedef struct _boolean_expr_t
	{
		unsigned int  expr_id;   /* �����ʽ��ID */
		unsigned int  operation; /* �������ʽִ�еIJ�����0��ʾ���ӣ�1��ʾɾ�� */
		unsigned int  rnum;      /* �������ʽ�������ٸ�����operation=1����rnum=0���� */
		scan_rule_t * rules;     /* ��������ʽ������operation=1����rules=NULL���� */
		void *        tag;       /* �û��Զ������ݣ�����ʱ��ƥ�������� */
	}boolean_expr_t;


	/* ��ɨ����ı��������� */
	typedef struct _text_data_t
	{
		const char * text;   /* �ı��������� */
		unsigned int tlen;   /* �ı����ݳ��� */
		int			 toffset;/* �����ı������������������е�ƫ��������ʽɨ���������Ч�����û����룬���������Ϊ0(���������Ϊ0) */  
	}text_data_t;

	/* ��ɨ���IPv4Ԫ�� */
	typedef struct _ipv4_data_t
	{
		unsigned int       saddr; /* ԴIP��ַ */
		unsigned int       daddr; /* Ŀ��IP��ַ */
		unsigned short int sport; /* Դ�˿� */
		unsigned short int dport; /* Ŀ�Ķ˿� */
		unsigned short int proto; /* �����Э�飬6��ʾTCP��17��ʾUDP */
	}ipv4_data_t;

	/* ��ɨ���IPv6Ԫ�� */
	typedef struct _ipv6_data_t
	{
		unsigned int       saddr[4]; /* ԴIP��ַ */
		unsigned int       daddr[4]; /* Ŀ��IP��ַ */
		unsigned short int sport;    /* Դ�˿� */
		unsigned short int dport;    /* Ŀ�Ķ˿� */
		unsigned short int proto;    /* �����Э�飬6��ʾTCP��17��ʾUDP */
	}ipv6_data_t;

	/* ͨ�õĴ�ɨ���������� */
	typedef struct _scan_data_t
	{
		unsigned int rule_type;     /* �������ͣ�����Ϊ����ö�ٹ�������֮һ */
		unsigned int sub_type;      /* �������ͣ��û��Զ��壬�������͸�������������MAX_SUB_RULETYPE����ǰ�Ķ��壩 */
		union                       /* ����rule_type�������ݸ������ַ�������ֵ������IPԪ�� */
		{
			text_data_t  text_data; /* ��ɨ���ı����ݣ���ƥ���ַ����������ơ��������ʽ�� */
			unsigned int int_data;  /* ������ֵ����ƥ����ֵ���䣩 */
			ipv4_data_t  ipv4_data; /* ��ɨ���IPv4Ԫ�� */
			ipv6_data_t  ipv6_data; /* ��ɨ���IPv6Ԫ�� */
		};
	}scan_data_t;

	/*
	   ɨ��������scan_result_t��rule_result_t˵����
	1���������е�ÿ����������ʽ��һ������ʽ��Ӧһ��scan_result_t������ò�������ʽ����rnum������ÿ���������Ӧ��һ�����scan_result_t::result[k]��0<=k<rnum����

	2�����ھ�ȷ�ַ������������position��length�������£�
	���еľ�ȷ�ַ�������result_num��ƥ��������i��0<=i<result_num����ƥ��������ʼλ�úͳ��ȷֱ��ǣ�
	   (position[i], length[i])

	3�������������ʽ���������position��length�������£�
	���е��������ʽ����result_num��ƥ������ÿ��ƥ��������group_num+1������Ԫ�ء����ڵ�i��0<=i<result_num����ƥ������
	��a�������������ʽƥ�����ʼλ�úͳ��ȷֱ��ǣ�
	   (position[(group_num+1)*i],     length[(group_num+1)*i])
	��b����j��0<=j<group_num�����������ʼλ�úͳ��ȷֱ��ǣ�
	   (position[(group_num+1)*i+j+1], length[(group_num+1)*i+j+1])
	*/

	/* �����ɨ�������� */
	typedef struct _rule_result_t
	{
		unsigned int rule_type;                   /* ��������ͣ�����Ϊ����ö�ٹ�������֮һ */
		unsigned int group_num;                   /* �����������ʽ���͹��򣬱��ֶα�ʾ������飨capturing group���ĸ�������������Ϊ0 */
		unsigned int result_num;                  /* �ù����������н���ĸ��� */
		unsigned int position[MAX_MATCH_POS_NUM]; /* �ù����������н������ʼλ�� */
		unsigned int length[MAX_MATCH_POS_NUM];   /* �ù����������н���ij��ȣ���������н��������ǰ�����ݰ������еģ������ö�Ӧ��length=0, ��������������IP�࣬������Ϊ0*/
	}rule_result_t;

	/* ��������ʽ��ɨ�������ͣ�quickģʽ��ֻ��expr_id�Լ�tag������Ч */
	typedef struct _scan_result_t
	{
		unsigned int  expr_id;                     /* �����ʽ��ID */
		unsigned int  rnum;                        /* �������ʽ�������ٸ����� */
		rule_result_t result[MAX_EXPR_ITEM_NUM];  /* ��������ʽ��ÿ�������Ӧ��ɨ���� */
		void *        tag;                         /* �û��Զ������ݣ�����ʱ��ƥ�������� */
	}scan_result_t;

	/*
	���ܣ�ɨ������ʼ��������ɨ��������
	������
		max_thread_num [in]: ɨ�����ɲ���ִ�е��߳�����
	����ֵ��
		ɨ�������������ֵΪNULLʱ����ʾ��ʼ��ʧ�ܡ�
	*/
	void * rulescan_initialize(unsigned int max_thread_num);

	/*
	���ܣ�
		����ɨ���������������rulescan_update֮ǰ�ɶ�ε��ã�ÿ������һ��ɨ�����͡�Rulescan��Ĭ�ϲ���������λ�õ���ϸ��Ϣ
	������
		instance[in]:			ɨ��������ָ�룻
		optname [in]:           �������ͣ�
		optval [in]:            optval��optlen��ʾ�����ľ������ݣ�
		optlen [in]:            optval��optlen��ʾ�����ľ������ݡ�
	����ֵ��
		1����ȷ���ã�-1������ʧ�ܡ�
	*/
	int rulescan_set_param(void * instance, enum RULESCAN_PARA_NAME optname, const void * optval, unsigned int optlen);

	/*
	���ܣ���̬ע��һ�������ʽ������ɨ�������󡣶���ͬһ��instance�����������ͬʱ�ж�������̡߳�
	������
		instance[in]:		ɨ��������ָ�룻
		expr_array[in]:		һ�������ʽ��
		epxr_num[in]:		�����ʽ�ĸ�����
		failed_ids[out]:	�Ƿ������id��failed_ids[0]��ʾ�Ƿ�id�ĸ�����failed_ids[1...failed_ids[0]]��¼�Ƿ�id��Ŀǰֻ�Ժ�һ�������������಼������ʽ��Ч��
		failed_size[in]:	failed_ids����Ĵ�С��
	����ֵ��
		����ֵΪ1ʱ����ʾע��ɹ�������ֵΪ-1ʱ����ʾ������
	*/
	int rulescan_update(void * instance, boolean_expr_t * expr_array, unsigned int expr_num, unsigned int * failed_ids, unsigned int failed_size);

	/*
	���ܣ��ͷ�ɨ�������󣻱�����������һ�Ρ�
	������
		instance [in]: ɨ����ָ�롣
	����ֵ��
		�ޡ�
	*/
	void rulescan_destroy(void * instance);

	/*
	���ܣ�����һ����ʽɨ�裬���뱣����״̬�IJ�����
	������
		instance [in]:  ɨ����ָ�룻
		thread_id [in]: ��ǰִ��ɨ����߳�id�������ڷ�Χ[0, max_thread_num-1]֮�ڡ�
	����ֵ��
		���ر�����״̬�IJ���������ֵΪNULLʱ����ʾʧ�ܡ�
	*/
	void * rulescan_startstream(void * instance, unsigned int thread_id);

	/*
	���ܣ���ʽɨ��ӿڣ�ɨ��һ������scan_data�������м�ɨ������������״̬stream_param�У�
		������֧������ɨ��ģʽ��
		��1��presults��ΪNULL������������ɨ�����������ͨ��*presults����һ��˴ε����н����
		��2��presultsΪNULL  ����ȫɨ��ģʽ����ʾ�ȴ�������ɨ��������ټ��㲢������������ȫ�����н����
			 ���rulescan_computeresult������
	������
		stream_param [in]: ������״̬�IJ�����
		scan_data [in]:    ��ɨ�����ݣ�
		presults [out]:    ���presults��ΪNULL�������DZ������е�һ��ɨ������
		size [in]:		   �������presults�Ĵ�С��
	����ֵ��
		�������ʽ����ȫɨ�裨presultsΪNULL�����򷵻�ֵΪ����ɨ�����еĹؼ��ʣ���ֵ���䡢IP����ȣ��ĸ�����
		���presult��ΪNULL���򷵻�ֵΪ����ɨ�����е������ʽ�ĸ�����-1��ʾ������
	*/
	int rulescan_searchstream(void * stream_param,  scan_data_t * scan_data, scan_result_t * presults, unsigned int size);

	/*
	���ܣ�������ɨ������󣬼���ɨ������
	������
		stream_param [in]: ������״̬��ָ�룻
		presults [out]:    ������飬�����������е������ʽ����id��
		size [in]:         �������presults�Ĵ�С��
	����ֵ��
		���������ʽ�����������<=size��������ֵΪ-1��ʾ������
	*/
	int rulescan_computeresult(void * stream_param, scan_result_t * presults, unsigned int size);

	/*
	���ܣ�������ʽɨ�裬�ͷ���������������������µ��øýӿ��ͷ�����������������ָ�ڽ�����ɨ��֮ǰ��û�е���
		  rulescan_destroy����rulescan�����
	������
		stream_param [in]: ������ָ�롣
	����ֵ��
		�ޡ�
	*/ 
	void rulescan_endstream(void * stream_param);

	/*
	���ܣ�������ʽɨ�裬�ͷ��������������ȵ���rulescan_destroy������rulescan���֮���ٽ�����ɨ��������
		  ��Ҫ����rulescan_endstream_simple���ͷ�����������������������²ŵ��øýӿڡ�
	������
		stream_param [in]: ������ָ�롣
	����ֵ��
		�ޡ�
	*/ 
	void rulescan_endstream_simple(void * stream_param);

	/*
	���ܣ�����ʽɨ��ӿڣ�ɨ������󷵻��������н����
	������
		instance [in]:	   ɨ����ָ�룻
		thread_id [in]:	   ��ǰִ��ɨ����߳�id�������ڷ�Χ[0, max_thread_num-1]֮�ڣ�
		scan_data [in]:    ��ɨ�����ݣ�
		presults [out]:    ������飬�����������е������ʽ����id��
		size [in]:         �������presults�Ĵ�С��
	����ֵ��
		���������ʽ�����������<=size��������ֵΪ-1��ʾ������
	*/
	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) */