summaryrefslogtreecommitdiff
path: root/src/http_count.c
blob: d1952b1b4e398c9bb3a1147f2775f1846b277bf5 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../../../opt/MESA/include/MESA/stream.h"
#include "../../../opt/MESA/include/MESA/MESA_prof_load.h"
#include "../../../opt/MESA/include/MESA/MESA_handle_logger.h"
#include "../../../opt/MESA/include/MESA/http.h"

#define MAX_STR_LEN				256
#define PERSIST_HASH_SIZE		512
#define DOUBLE_DIRECTION		0x03
#define HTTP_CHECK_CONFIG		"./plug/business/http_count/http_count.conf"
#define BASIC_DIR 				"./plug/business/http_count/"
#define HTTP_COUNT_PLUGNAME 	"http_count.so"
#define JHASH_GOLDEN_RATIO		0x9e3779b9
#define __jhash_mix(a, b, c)\
{\
	a -= b; a -= c; a ^= (c>>13);\
	b -= c; b -= a; b ^= (a<<8);\
	c -= a; c -= b; c ^= (b>>13);\
	a -= b; a -= c; a ^= (c>>12);\
	b -= c; b -= a; b ^= (a<<16);\
	c -= a; c -= b; c ^= (b>>5);\
	a -= b; a -= c; a ^= (c>>3);\
	b -= c; b -= a; b ^= (a<<10);\
	c -= a; c -= b; c ^= (b>>15);\
}

int g_topn = 0, g_log_level = 0, g_type = 0;
char g_statistics_type[MAX_STR_LEN]="";
char g_http_count_log_path[MAX_STR_LEN]="";
void *g_http_count_log_handler = NULL;
struct http_data_htable *g_http_data_htable = NULL;
enum statistics_type
{
	HTTP_TYPE,
	USER_AGENT_TYPE,
	TYPE_ERROR,
};
	
struct http_count_node
{
	struct http_count_node *next;
	//char data[MAX_STR_LEN];
	uint64_t count;//统计次数
	char *data;//存储host/useragent字段
};

struct http_data_htable
{
	struct http_count_node *hlist_head;
	uint64_t sum;//所有hash链总节点数统计
};

int add_hlist_node(uint32_t key, char *buf, uint32_t buflen)
{
	struct http_count_node *node = NULL;

	node = malloc(sizeof(struct http_count_node));
	if(!node)
	{
		printf("http_check.so: malloc node failed\n");
		MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc node failed!");
		return -1;
	}
	node->data = malloc(buflen+1);
	if(!node->data)
	{
		printf("http_check.so: malloc node->data failed\n");
		MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc node->data failed!");
		return -1;
	}

	strncpy(node->data, buf, buflen);
	node->data[buflen] = 0;
	node->count = 1;//命中次数
	node->next = g_http_data_htable->hlist_head[key].next;
	g_http_data_htable->hlist_head[key].next = node;
	g_http_data_htable->hlist_head[key].count++;//哈希链节点数
	g_http_data_htable->sum++;//所有hash链总节点数
	
	return 0;
}

struct http_count_node * find_hlist_node(uint32_t key, char *buf, uint32_t buflen)
{
	struct http_count_node *node = NULL;
	
	node = g_http_data_htable->hlist_head[key].next;
	for(; node != NULL; node = node->next)
	{
		if(!strncmp(node->data, buf, buflen))
		{
			return node;
		}		
	}
	
	return NULL;
}

uint32_t jhash (void *key, uint32_t length, uint32_t initval)
{
	uint32_t a, b, c, len;
  	uint8_t *k = key;

  	len = length;
  	a = b = JHASH_GOLDEN_RATIO;
  	c = initval;

 	while (len >= 12)
    {
    	a += (k[0] + ((uint32_t) k[1] << 8) + ((uint32_t) k[2] << 16) + ((uint32_t) k[3] << 24));
      	b += (k[4] + ((uint32_t) k[5] << 8) + ((uint32_t) k[6] << 16) + ((uint32_t) k[7] << 24));
      	c += (k[8] + ((uint32_t) k[9] << 8) + ((uint32_t) k[10] << 16) + ((uint32_t) k[11] << 24));
      	__jhash_mix (a, b, c);
      	k += 12;
      	len -= 12;
    }

  	c += length;
  	switch (len)
    {
	    case 11:
	    	c += ((uint32_t) k[10] << 24);
	    case 10:
	      	c += ((uint32_t) k[9] << 16);
	    case 9:
	      	c += ((uint32_t) k[8] << 8);
	    case 8:
	      	b += ((uint32_t) k[7] << 24);
	    case 7:
	      	b += ((uint32_t) k[6] << 16);
	    case 6:
	      	b += ((uint32_t) k[5] << 8);
	    case 5:
	      	b += k[4];
	    case 4:
	      	a += ((uint32_t) k[3] << 24);
	    case 3:
	      	a += ((uint32_t) k[2] << 16);
	    case 2:
	      	a += ((uint32_t) k[1] << 8);
	    case 1:
	      	a += k[0];
    };

  	__jhash_mix (a, b, c);

  	return c;
}

uint32_t get_hash_key(void *buf, uint32_t buflen)
{
	return (jhash(buf, buflen, 0) % PERSIST_HASH_SIZE);
}

void http_count_statistics(char *buf, uint32_t buflen)
{
	uint32_t key = 0;
	struct http_count_node *node = NULL;
	
	key = get_hash_key(buf, buflen);
	node = find_hlist_node(key, buf, buflen);
	if(node)
	{
		node->count++;
	}
	else
	{
		add_hlist_node(key, buf, buflen);
	}

	return;
}

char http_count_entry(stSessionInfo* session_info, void **param, int thread_seq, struct streaminfo *a_tcp, void *a_packet)
{	
	if(a_tcp->dir != DOUBLE_DIRECTION)
		return PROT_STATE_GIVEME;
	
    switch(session_info->prot_flag)
    {
    	case HTTP_HOST:
			if(g_type == HTTP_TYPE)
				http_count_statistics(session_info->buf, session_info->buflen);
            break;
        case HTTP_USER_AGENT:
			if(g_type == USER_AGENT_TYPE)
          		http_count_statistics(session_info->buf, session_info->buflen);
            break;
        default:
            break;
    }
	
	return PROT_STATE_GIVEME;
}

int http_check_readconf(const char* filename)
{
	MESA_load_profile_int_def(filename, "HTTP", "TOPN", (int *)(&g_topn), 5);
    MESA_load_profile_string_nodef(filename, "HTTP", "TYPE", g_statistics_type, MAX_STR_LEN);
	MESA_load_profile_int_def(filename, "HTTP", "LOG_LEVEL", (int *)(&g_log_level), 30);
    MESA_load_profile_string_nodef(filename, "HTTP", "HTTP_COUNT_LOG_PATH", g_http_count_log_path, MAX_STR_LEN);

	g_http_count_log_handler = MESA_create_runtime_log_handle(g_http_count_log_path, g_log_level);
	if(!g_http_count_log_handler)
	{
		printf("http_check.so: get log handle error!\n");
		MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "get log handle error!");	
		return -1;
	}
	if(!strncmp(g_statistics_type, "Host", 4))
		g_type = HTTP_TYPE;
	else if(!strncmp(g_statistics_type, "User-Agent", 10))
		g_type = USER_AGENT_TYPE;
	else
	{
		g_type = TYPE_ERROR;
		printf("http_check.so: get statistics type error!\n");
		MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "get statistics type error!");	
		return -1;
	}
    //printf("***v1***conf:topn:%d, key:%s(%d), log_level:%d, log_path:%s\n",g_topn, g_statistics_type, g_type, g_log_level, g_http_count_log_path);
	MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_DEBUG, HTTP_COUNT_PLUGNAME, 
		"read_conf:topn:%d, key:%s(%d), log_level:%d, log_path:%s\n", g_topn, g_statistics_type, g_type, g_log_level, g_http_count_log_path);

	return 0;
}

int http_htable_init(void)
{
	uint32_t i = 0;
	
	g_http_data_htable = malloc(sizeof(struct http_data_htable));
	if(!g_http_data_htable)
	{
		printf("http_check.so: malloc g_http_data_htable failed!\n");
		MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc g_http_data_htable failed!");	
		return -1;
	}
	memset(g_http_data_htable, 0, sizeof(struct http_data_htable));
	g_http_data_htable->hlist_head = malloc(sizeof(struct http_count_node) * PERSIST_HASH_SIZE);
	if(!g_http_data_htable->hlist_head)
	{
		printf("http_check.so: malloc g_http_data_htable->hlist_head failed!\n");
		MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc g_http_data_htable->hlist_head failed!");	
		return -1;
	}
	for(i=0; i < PERSIST_HASH_SIZE; i++)
		memset(&g_http_data_htable->hlist_head[i], 0, sizeof(struct http_count_node));

	return 0;
}

int http_count_init(void)
{
	int ret = 0;
	
	ret = http_check_readconf(HTTP_CHECK_CONFIG);
	if(ret)
	{
		goto OUT;
	}
	ret = http_htable_init();
	if(ret)
	{
		goto OUT;
	}

OUT:
	return ret;
}

void free_all(void)
{
	uint32_t i = 0;
	struct http_count_node *free_node = NULL;
	struct http_count_node *head = NULL;
	
	for(i = 0; i < PERSIST_HASH_SIZE; i++)
	{
		head = &g_http_data_htable->hlist_head[i];
		free_node = head->next;
		for(; free_node != NULL; free_node = head->next)
		{
			head->next = free_node->next;
			free_node->next = NULL;
			free(free_node->data);
			free_node->data = NULL;
			free(free_node);
			free_node = NULL;
		}
	}
	free(g_http_data_htable->hlist_head);
	g_http_data_htable->hlist_head = NULL;
	free(g_http_data_htable);
	g_http_data_htable = NULL;

	return;
}

void http_count_destory(void)
{
	uint32_t i = 0;
	uint32_t j = 0;
	uint64_t max_count;
	uint8_t flag = 0;
	struct http_count_node *node = NULL;
	struct http_count_node *max_node = NULL;
	char buff[MAX_STR_LEN] = "";
	FILE *fp = NULL;

	sprintf(buff, "%s%s.txt", BASIC_DIR, g_statistics_type);
	fp = fopen(buff, "w+");
	if(!fp)
	{
		printf("http_check.so: open file error!\n");
		MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "open file %s error!", buff);	
		goto OUT;
	}
	
	if(g_type == HTTP_TYPE)
		fprintf(fp, "%-100s	%s\n","Host", "Count");
	else if(g_type == USER_AGENT_TYPE)
		fprintf(fp, "%-180s	%s\n","User-Agent", "Count");
#if 0
	for(i = 0; i < PERSIST_HASH_SIZE; i++)//print_test
	{
		node = g_http_data_htable->hlist_head[i].next;
		for(; node != NULL; node = node->next)
		{
			printf("hash_print:key:%d, data:%s, count:%"PRIu64", hash_node:%"PRIu64", all_node:%"PRIu64"\n",
				i, node->data, node->count, g_http_data_htable->hlist_head[i].count, g_http_data_htable->sum);	
		}
	}
#endif

	//printf("***************************top%d***************************\n", g_topn);
	for(j = 0; j < g_topn; j++)
	{
		flag = 0;
		for(i = 0; i < PERSIST_HASH_SIZE; i++)
		{
			node = g_http_data_htable->hlist_head[i].next;
			for(; node != NULL; node = node->next)
			{
				if (!flag)
				{
					max_count = node->count;
					max_node = node;
					flag = 1;
				}
				if(max_count < node->count)
				{
					max_count = node->count;
					max_node = node;
				}
			}	
		}
		if(max_node && max_node->count)
		{
			//printf("top%d: %s, count:%"PRIu64"\n",g_topn, max_node->data, max_node->count);
			MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_DEBUG, HTTP_COUNT_PLUGNAME, 
				"top%d: %s, count:%d\n", g_topn, max_node->data, max_node->count);
			if(g_type == HTTP_TYPE)
				fprintf(fp, "%-100s	%"PRIu64"\n",  max_node->data, max_node->count);
			else if(g_type == USER_AGENT_TYPE)
				fprintf(fp, "%-180s	%"PRIu64"\n",  max_node->data, max_node->count);
			max_node->count = 0;
		}
	}

OUT:
	if(fp)
		fclose(fp);
	if(g_http_count_log_handler)
		MESA_destroy_runtime_log_handle(g_http_count_log_handler);
	free_all();
		
    return;
}