-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.c.orig
executable file
·167 lines (149 loc) · 4.24 KB
/
loop.c.orig
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
#include "loop.h"
int init_sqlite()
{
char* dbpath="shutter.db";
char *zErrMsg = 0;
int rc;
//open the database file.If the file is not exist,it will create a file.
rc = sqlite3_open(dbpath, &db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 0;
}
return 1;
}
void process_data_head(struct data_head * dh)
{
/*--Description--
get mutex lock, then get all data_segment (up) in data_head,
split out every http request,
and send it to process_http()
*/
//printf(" |---Node---test=%d| ",dh->test);
/*--step 1, get mutex lock--*/
if(pthread_mutex_trylock(&(dh->tcp_stream_lock_up))==0)
{
if(NULL == dh->a_tcp)
return;
char * buffer;
uint32_t buffer_length;
int request_number = 0;
uint32_t offset_temp = dh->offset_up;// temp offset, for putting ptr to offset
uint32_t offset_new = 0; //data processed this round
uint32_t this_length = 0;
struct data_segment * ds = NULL;
struct data_segment * start = NULL;
//pthread_mutex_lock(&(dh->tcp_stream_lock_up));
/*--step 2, find the data_segment that "offset" points to--*/
ds = dh->first_up;
while(ds != dh->last_up->next) //find the segment contains offset
{
if(ds->len > offset_temp)
//offset is in this data_segment
{
break;
}
else
// havn't reach segment
{
offset_temp = offset_temp - ds->len;
}
ds = ds->next;
}
//store this segment to "start" ,and offset in this segment to "offset_temp"
start = ds;
/*--step 3, calculate length from offset to end--*/
this_length = ds->len - offset_temp;
ds = ds->next;
while(ds != dh->last_up->next)
{
this_length += ds->len;
ds = ds->next;
}
printf("\ndata length:%d\n",this_length);
/*--step 4, get all data from offset to end--*/
buffer = (char*)malloc(1 + this_length*sizeof(char));
buffer_length = this_length;
*(buffer+buffer_length)='\0';
this_length = start->len - offset_temp;
memcpy(buffer, start->data+offset_temp, this_length);
ds = start->next;
while(ds != dh->last_up->next)
{
memcpy(buffer+this_length,ds->data,ds->len);
this_length += ds->len;
ds = ds->next;
}
//printf("buffer:%s\n",buffer);
/*--step 5, get all request head pointer offset into an array--*/
request_number = get_request_number(buffer);
int request[request_number];
get_request_offset(buffer,request);
//printf("request number:%d\n",request_number);
/*--step 6, put every request into processhttp(char*,int)--*/
int i=0;
//check every requests except the last one.
for(; i<request_number-1 ; i++)
{
//printf(" one request:%s|length:%d\n",buffer+request[i],request[i+1]-request[i]);
if( !processhttp(buffer+request[i],request[i+1]-request[i]) )
{
//zhongy , checking is same in net_ntil.c
//if illegal request, do:
// dh->a_tcp->gk_state = GK_STATE_BLOCK
//else legal request, do:
// dh->a_tcp->gk_state = GK_STATE_CHECKED
}
}
//check the last request. because we have to identify whether it is complete
//printf("last request:%s|length:%d\n",buffer+request[request_number-1],buffer_length-request[request_number-1]);
if(!processhttp(buffer+request[request_number-1],buffer_length-request[request_number-1]))
{
//zhongy,check
//if illegal request, do:
// dh->a_tcp->gk_state = GK_STATE_BLOCK
//else legal request, do:
// dh->a_tcp->gk_state = GK_STATE_CHECKED
dh->offset_up += buffer_length;
}
else
{
dh->offset_up += request[request_number-1];
}
/*--step 7, release mutex lock--*/
pthread_mutex_unlock(&(dh->tcp_stream_lock_up));
}
//printf("Done\n");
return;
}
int loop()
{
int loopcount = 0;
for(;loopcount<DATA_CACHE_TABLE_SIZE;)
{
//printf("%d--->",loopcount);
//poll the linked-list in one data_cache_table, send each data_head to process_data_head()
struct data_head * dh_pos = data_cache_table[loopcount].next;
while(dh_pos!=NULL)
{
// if(dh_pos->a_tcp->gk_state == GK_STATE_UNCHECKED)
process_data_head(dh_pos);
dh_pos = dh_pos->next;
}
//printf("\n");
//following lines for endless loop
if ( loopcount == DATA_CACHE_TABLE_SIZE-1 )
{
loopcount = 0;
continue;
}
loopcount++;
}
}
void init_loop()
{
init_ac();
init_sqlite();
}