summaryrefslogtreecommitdiff
path: root/src/frag_app.c
blob: 2c33e74966ca5d675efa17e5de88f257889714d0 (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
/*
* APP
*/

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <math.h>
#include <net/if.h>
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h>
#include <time.h>
#include <openssl/md5.h>

#include "MESA_handle_logger.h"

#include "app_detect.h"

#include "AV_interface.h"

#include "frag_app.h"
#include "frag_proc.h"
#include "log.h"

extern frag_rssb_parameter_t 		g_frag_run;
extern frag_rssb_configure_t 		g_frag_cfg;
extern frag_rssb_status_t 			g_frag_stat;
extern frag_reassembly_t 			frag_rssb;

void app_change_pid(frag_unit_t* frg_unit)
{	
	char				pid_buf[1024] = {0};	
	char 				serverIP[64] = {0};
	uint64_t			pid_before = frg_unit->pid;
	/*APP��PID�޸�ΪserverIP+len, ����tuple7*/
	get_serverIP(frg_unit->opt[MEDIA_OPT_ADDR]->opt_value, frg_unit->opt[MEDIA_OPT_ADDR]->opt_len, serverIP);
	snprintf(pid_buf, sizeof(pid_buf), "APP_%s->%" PRIu64 "",  serverIP, frg_unit->content_length);
	frg_unit->pid = make_mid(pid_buf, strlen(pid_buf), PID_TYPE_IPLEN);	
	frg_unit->mid = frg_unit->pid;
	frag_write_to_log(APP_CHANGE_PID, pid_before, frg_unit, NULL, 0);			
}

static int url_indicated_app(const char *url)
{
	const char *pfilename = NULL, *p1 = NULL, *p2 = NULL, *p3 = NULL, *p = NULL;
	unsigned int len=0;

	if(url == NULL)
		return 0;

	pfilename = strrchr(url, '/');
	if(pfilename == NULL || *(pfilename + 1) == '\0')
		return 0;

	pfilename = pfilename + 1;
	len = strlen(pfilename);

	p1 = strchr(pfilename, ';');
	p2 = strchr(pfilename, '?');
	p3 = strchr(pfilename, '#');

	if(p1>p2)
	{
		p = p1; p1 = p2; p2 = p;
	}
	if(p1>p3)
	{
		p = p3; p1 = p3; p3 = p;
	}
	if(p2>p3)
	{
		p = p2; p2 = p3; p3 = p;
	}
	if(p1 != NULL)
		len = p1 - pfilename;
	else if(p2 != NULL)
		len = p2 - pfilename;
	else if(p3 != NULL)
		len = p3 - pfilename;
	
	if(!strncasecmp(pfilename + len - 4, ".apk", 4))
		return FILE_ANDRIOD;
	if(!strncasecmp(pfilename + len - 4, ".ipa", 4))
		return FILE_IOS;
	return 0;
}

void free_appdtc_detail(appdtc_detail_in_t*	 detail)
{
	if(NULL!=detail)
	{
		if(NULL!=detail->app_data)
		{
			free(detail->app_data);
		}
		if(NULL!=detail->tuple4)
		{
			free(detail->tuple4);
		}
		if(NULL!=detail->url)
		{
			free(detail->url);
		}
		if(NULL!=detail->user_agent)
		{
			free(detail->user_agent);
		}
		if(NULL!=detail->app_frg_lq)
		{
			MESA_lqueue_destroy(detail->app_frg_lq, NULL, NULL);
			detail->app_frg_lq = NULL;
		}
		free(detail);
	}
}

void proc_app_data(media_t* mdi)
{	
	appdtc_detail_in_t*	detail = (appdtc_detail_in_t*)calloc(1, sizeof(appdtc_detail_in_t));
	char*				p_url = NULL;
	
	/*appid*/
	detail->appid = mdi->mid;

	/*app_type*/
	if(mdi->media_type==FILE_APP)
	{
		if(NULL!=mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index])
		{
			p_url = (char*)malloc(mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_len+1);
			memset(p_url, 0 , mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_len+1);
			memcpy(p_url, mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_value, mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_len);
			mdi->media_type = url_indicated_app(p_url);
			if(NULL!=p_url)
			{
				free(p_url);
			}
		}
	}

	switch(mdi->media_type)
	{	
		case FILE_IOS:			
			detail->app_type = APP_TYPE_IOS;
			break;
		case FILE_ANDRIOD:			
			detail->app_type = APP_TYPE_ANDROID;
			break;
		default:			
			detail->app_type = APP_TYPE_UNKNOWN;
			break;
	}
	
	/*data*/
	detail->app_frg_lq = mdi->app_frg_lq;
	mdi->app_frg_lq = NULL;	

	/*tuple4*/
	if(NULL!=mdi->addrlist)
	{
		detail->tuple4 = (char*)malloc(mdi->addrlist_len);
		memcpy(detail->tuple4, mdi->addrlist, mdi->addrlist_len);
		detail->tuple4_len = mdi->addrlist_len;
	}

	/*url : app_http_session urls are same, so save one*/
	if(NULL!=mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index])
	{
		detail->url = (char*)malloc(mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_len);
		memcpy(detail->url, mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_value, mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_len);
		detail->url_len = mdi->opt[MEDIA_OPT_URL][mdi->url_opt_index]->opt_len;		
	}	
	
	/*ua*/
	if(NULL!=mdi->opt[MEDIA_OPT_ADDR][mdi->url_opt_index])
	{
		detail->user_agent = (char*)malloc(mdi->opt[MEDIA_OPT_UA][mdi->url_opt_index]->opt_len);
		memcpy(detail->user_agent, mdi->opt[MEDIA_OPT_UA][mdi->url_opt_index]->opt_value, mdi->opt[MEDIA_OPT_UA][mdi->url_opt_index]->opt_len);
		detail->ua_len = mdi->opt[MEDIA_OPT_UA][mdi->url_opt_index]->opt_len;	
	}	
	atomic_inc(&g_frag_stat.sysinfo_stat[APP_QUEUE][QUEUE_IN]);
	MESA_lqueue_join_tail(g_frag_run.app_lq, &detail, sizeof(detail));		
}

void* thread_send_app_data(void *param)
{
	appdtc_detail_in_t*	detail = NULL;
	long 				detail_len = sizeof(detail);	
	frag_in_t*			frg = NULL;
	long				frglen = sizeof(frg);
	int 				rec_detail = 0;
	int 				rec_frg = 0;
	
	while(1)
	{
		rec_detail = MESA_lqueue_try_get_head(g_frag_run.app_lq, &detail, &detail_len);
		if (rec_detail<0)
		{		
			usleep(10);
		}	
		else
		{		
			rec_frg = MESA_lqueue_try_get_head(detail->app_frg_lq, &frg, &frglen);
			while(0==rec_frg)
			{	
				atomic_inc(&frag_rssb.sysinfo_stat[RSSB_WAIT_QUEUE][QUEUE_OUT]); 
				MESA_handle_runtime_log(frag_rssb.logger, RLOG_LV_FATAL, FRAG_REASSEMBLY_MODULE_NAME, 
										"{%s:%d} get app_frg_lq: [PID: %llu, offset: %llu, datalen:%u]", 
										__FILE__,__LINE__, frg->pid, frg->offset, frg->datalen);
				if(detail->data_len > frg->offset+frg->datalen)
				{
					memcpy((char*)detail->app_data+frg->offset, (void*)frg->data, frg->datalen);
				}
				else
				{
					detail->app_data = (char*)realloc(detail->app_data, frg->offset+frg->datalen);			
					memcpy((char*)detail->app_data+frg->offset, (void*)frg->data, frg->datalen);
					detail->data_len = frg->offset+frg->datalen;			
				}						
				free_frag_in(frg,0,NULL);
				rec_frg = MESA_lqueue_try_get_head(detail->app_frg_lq, &frg, &frglen);
			}			
			atomic_inc(&g_frag_stat.sysinfo_stat[APP_QUEUE][QUEUE_OUT]);
			atomic_inc(&g_frag_stat.stat_info[TO_SEND][TOTAL_PKTS]);
			atomic_add(&g_frag_stat.stat_info[TO_SEND][TOTAL_BYTES], detail->data_len);
#if APP_FUNC
			APPDTC_PLUG_ENTRY(g_frag_run.appdtc_handle, (appdtc_detail_t*)detail, 0);	
#endif
			free_appdtc_detail(detail);
			detail = NULL;			
		}
	}	
	return NULL;
}