summaryrefslogtreecommitdiff
path: root/src/quic_analysis.cpp
blob: 3aee5f1de38db78e9d7322c805f2e25e8182e929 (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
/*
 * quic_analysis.c
 *
 *  Created on: 2019��4��2��
 *      Author: root
 */
#include "gquic.h"
#include "quic_analysis.h"
#include "gquic_process.h"
#include "parser_quic.h"

#include <stdio.h>
#include <MESA/stream_inc/stream_base.h>
#include <MESA/MESA_handle_logger.h>
#include <MESA/MESA_prof_load.h>

struct _quic_param_t g_quic_param;
const char *g_quic_proto_conffile="./conf/quic/main.conf";
const char *g_quic_regionname_conffile="./conf/quic/quic.conf";

#ifdef __cplusplus
extern "C"
{
#endif

#define GIT_VERSION_CATTER(v) __attribute__((__used__)) const char * GIT_VERSION_##v = NULL
#define GIT_VERSION_EXPEND(v) GIT_VERSION_CATTER(v)

/* VERSION TAG */
#ifdef GIT_VERSION
GIT_VERSION_EXPEND(GIT_VERSION);
#else
static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
#endif
#undef GIT_VERSION_CATTER
#undef GIT_VERSION_EXPEND

#ifdef __cplusplus
}
#endif

const char QUIC_VERSION_20200603=0;

int quic_init_stream(void **pme, int thread_seq)
{
	struct _quic_context *_context=(struct _quic_context *)dictator_malloc(thread_seq, sizeof(struct _quic_context));
	memset(_context, 0, sizeof(struct _quic_context));

	*pme=(void*)_context;
	
	return 0;
}

void quic_release_exts(int thread_seq, quic_tlv_t *ext_tags, int ext_tag_num)
{
	int i=0;
	
	if(ext_tags!=NULL)
	{
		for(i=0; i<ext_tag_num; i++)
		{
			if(ext_tags[i].value!=NULL)
			{
				dictator_free(thread_seq, ext_tags[i].value);
				ext_tags[i].value=NULL;
			}
		}

		dictator_free(thread_seq, ext_tags);
		ext_tags=NULL;
	}

}

void quic_release_stream(void** pme, int thread_seq)
{
	struct _quic_context *_context = (struct _quic_context *)*pme;
	if(NULL!=_context)
	{	
		if(_context->quic_info.client_hello!=NULL)
		{
			quic_release_exts(thread_seq, _context->quic_info.client_hello->ext_tags, _context->quic_info.client_hello->ext_tag_num);
			dictator_free(thread_seq, _context->quic_info.client_hello);
			_context->quic_info.client_hello=NULL;
		}

		if(_context->quic_info.server_hello!=NULL)
		{
			quic_release_exts(thread_seq, _context->quic_info.server_hello->ext_tags, _context->quic_info.server_hello->ext_tag_num);
			dictator_free(thread_seq, _context->quic_info.server_hello);
			_context->quic_info.server_hello=NULL;
		}
		
		if(_context->quic_info.rejection!=NULL)
		{
			quic_release_exts(thread_seq, _context->quic_info.rejection->ext_tags, _context->quic_info.rejection->ext_tag_num);
			dictator_free(thread_seq, _context->quic_info.rejection);
			_context->quic_info.rejection=NULL;
		}

		dictator_free(thread_seq, _context);
		_context=NULL;
		*pme=NULL;
	}
	
    return;
}

extern "C" int QUIC_INIT(void)
{
	int ret=0;
	FILE *fp=NULL;
	char buf[2048]={0};
	int region_id=0;
	char region_name[REGION_NAME_LEN]={0};

	memset(&g_quic_param,0,sizeof(struct _quic_param_t));

	MESA_load_profile_int_def(g_quic_proto_conffile, "QUIC", "LOG_LEVEL", &g_quic_param.level, RLOG_LV_FATAL);
	MESA_load_profile_string_def(g_quic_proto_conffile, "QUIC", "LOG_PATH", g_quic_param.log_path, sizeof(g_quic_param.log_path), "./log/quic/quic");
	
	MESA_load_profile_int_def(g_quic_proto_conffile, "QUIC", "DUMP_PCAKET_SWITCH", &g_quic_param.dump_packet_switch, 0);

	g_quic_param.logger=MESA_create_runtime_log_handle(g_quic_param.log_path, g_quic_param.level);
	if(g_quic_param.logger==NULL)
	{
		printf("MESA_create_runtime_log_handle failed, level: %d log_path: %s", g_quic_param.level, g_quic_param.log_path);
		return -1;
	}

	if(((fp = fopen(g_quic_regionname_conffile, "r"))!=NULL))
	{
		 while(fgets(buf, sizeof(buf), fp))
		 {
			 ret = sscanf(buf, "%d\t%s", &region_id, region_name);
			 if(2>ret)
			 {
			 	fclose(fp);
				 MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_FATAL, "QUIC_READCONF", "Read error, Please check %s, region_line: %s", g_quic_regionname_conffile, buf);
				 return -1;
			 }
			 if(region_id>MAX_REGION_NUM)
			 {
			 	fclose(fp);
				 MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_FATAL, "QUIC_READCONF", "Read error, Please check %s, bigger than MAX_REGION_NUM, region_line: %s", g_quic_regionname_conffile, buf);
				 return -1;
			 }
			 
			 strncpy(g_quic_param.quic_conf_regionname[region_id], region_name, strlen(region_name));
			 g_quic_param.quic_region_cnt++;
			 memset(region_name, 0, sizeof(region_name));
		}
		 
		fclose(fp);
	}
	else
	{
		 MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_FATAL, "QUIC_READCONF", "Open %s error, Please check %s", g_quic_regionname_conffile, g_quic_regionname_conffile);
		 return -1;
	}

	gcry_init();

	return 0;
}/*QUICINIT*/

extern "C" void QUIC_DESTROY(void)
{
	return ;
}/*QUICDESTROY*/

extern "C" void QUIC_GETPLUGID(unsigned short plugid)
{
	g_quic_param.quic_plugid = plugid;
	MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_FATAL, "QUIC_GETPLUGID", "quic_plugid: %d", plugid);
}

extern "C" void QUIC_PROT_FUNSTAT(unsigned long long protflag)
{
	if(0==protflag){
		return;
	}
	g_quic_param.quic_interested_region_flag=protflag;
	MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_FATAL, "QUIC_PROT_FUNSTAT", "interested_region_flag: %llu", g_quic_param.quic_interested_region_flag);
	return;
}/*PROT_FUNSTAT*/

extern "C" unsigned long long quic_getRegionID(char *string, int str_len,const char g_string[MAX_REGION_NUM][REGION_NAME_LEN])
{
	unsigned long long i=0;
	for(i=0;i<g_quic_param.quic_region_cnt;i++)
	{
        if(0==strcasecmp(g_string[i], string))
		{
			return i;
		}
	}
	return 0;
}

extern "C" long long QUIC_FLAG_CHANGE(char* flag_str)
{
	if(flag_str==NULL) return -1;
	long long protflag = 0;
	long long region_id = 0;
	char *start_token = flag_str;
	char *end_token = flag_str;
	char *end_pos = flag_str+strlen(flag_str);
	char region_name[REGION_NAME_LEN] = {0};

	while (end_token < end_pos)
	{
		end_token = (char*)memchr(start_token, ',', end_pos-start_token);
		if(end_token!=NULL)
		{
			memcpy(region_name, start_token, end_token-start_token);
			start_token = end_token+1;
			end_token += 1;
		}
		else
		{
			memcpy(region_name, start_token, end_pos-start_token);
			end_token = end_pos;
		}
		region_id = quic_getRegionID(region_name, strlen(region_name), g_quic_param.quic_conf_regionname);
		if(-1==region_id)
		{
			MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_FATAL, "QUIC_FLAG_CHANGE", "Read %s error, flag_str: %d", region_name, flag_str);
			return -1;
		}
		protflag |= ((long long)1)<<region_id;
		memset(region_name, 0, REGION_NAME_LEN);
	}

	
	MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_FATAL, "QUIC_FLAG_CHANGE", "protflag: %llu", protflag);
	return protflag;
}

extern "C" char QUIC_ENTRY(struct streaminfo *pstream, void**pme, int thread_seq, void *a_packet)
{
	int ret=0;
	struct _quic_context *_context=(struct _quic_context *)*pme;

	if(g_quic_param.quic_interested_region_flag<QUIC_KEY)
	{
		return APP_STATE_DROPME;
	}

	if(!is_quic_port(pstream))
	{
		return APP_STATE_DROPME;
	}

	if(*pme==NULL)
	{
		quic_init_stream(pme, thread_seq);
		_context=(struct _quic_context *)*pme;
		_context->call_business=TRUE;
	}
	
	switch(pstream->opstate)
	{
		case OP_STATE_PENDING:
		case OP_STATE_DATA:
			ret=quic_process(pstream, _context, thread_seq, a_packet);
			break;
		case OP_STATE_CLOSE:
			ret=quic_process(pstream, _context, thread_seq, a_packet);
			quic_callPlugins(pstream, _context, NULL, 0, QUIC_INTEREST_KEY_MASK, a_packet);
			break;
		default:
			break;
	}
	
	if(ret&APP_STATE_DROPME || ret&APP_STATE_DROPPKT || pstream->opstate==OP_STATE_CLOSE)
	{
		quic_release_stream(pme, thread_seq);
		*pme=NULL;
		return ret;
	}

	return APP_STATE_GIVEME;
}