-
Notifications
You must be signed in to change notification settings - Fork 108
/
header_packet.h
211 lines (198 loc) · 6.25 KB
/
header_packet.h
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
#ifndef HEADER_PACKET_H
#define HEADER_PACKET_H
#include "rqueue.h"
#include "md5.h"
#include "packet_dupl_check.h"
#define VLAN_UNSET 0xFFFF
#define VLAN_NOSET 0xFFFE
#define VLAN_IS_SET(vlan) ((vlan & 0xFFF) == vlan)
struct sPacketInfoData {
u_int16_t vlan;
u_int8_t flags;
inline void clear() {
vlan = VLAN_UNSET;
flags = 0;
}
};
struct sHeaderPacket {
void *stack;
u_int32_t packet_alloc_size;
u_int8_t detect_headers;
u_int16_t header_ip_encaps_offset;
u_int16_t header_ip_offset;
u_int16_t eth_protocol;
sPacketInfoData pid;
sPacketDuplCheck dc;
#if DEDUPLICATE_COLLISION_TEST
sPacketDuplCheck dc_ct_md5;
#endif
pcap_pkthdr header;
u_char packet[1];
inline void clearPcapProcessData() {
detect_headers = 0;
dc.clear();
#if DEDUPLICATE_COLLISION_TEST
dc_ct_md5.clear();
#endif
}
};
#define HPH(hp) (&((hp)->header))
#define HPP(hp) ((hp)->packet)
#define HEADER_PACKET_STACK_POOL_SIZE 100
#define HEADER_PACKET_STACK_PUSH_QUEUE_MAX 10
class cHeaderPacketStack {
private:
struct sHeaderPacketPool {
sHeaderPacket *pool[HEADER_PACKET_STACK_POOL_SIZE];
};
public:
cHeaderPacketStack(u_int32_t size_max, u_int32_t packet_alloc_size) {
this->packet_alloc_size = packet_alloc_size;
pop_queue_size = 0;
for(int i = 0; i < HEADER_PACKET_STACK_PUSH_QUEUE_MAX; i++) {
push_queues_size[i] = 0;
}
pop_queue_pool_prepared = false;
_sync_prepare = 0;
this->stack = new FILE_LINE(9001) rqueue_quick<sHeaderPacketPool>(size_max / HEADER_PACKET_STACK_POOL_SIZE, 0, 0, NULL, false);
}
~cHeaderPacketStack() {
for(int ipush = 0; ipush < HEADER_PACKET_STACK_PUSH_QUEUE_MAX; ipush++) {
for(u_int i = 0; i < push_queues_size[ipush]; i++) {
delete [] push_queues[ipush].pool[i];
}
}
sHeaderPacket *headerPacket;
while(pop(&headerPacket) != 2) {
delete [] headerPacket;
}
delete [] headerPacket;
delete this->stack;
}
inline int push(sHeaderPacket *headerPacket, u_int16_t push_queue_index) {
if(!headerPacket) {
return(0);
}
extern int opt_block_alloc_stack;
if(opt_block_alloc_stack || !headerPacket->stack) {
delete [] headerPacket;
return(2);
}
*(u_char*)&headerPacket->header = 0;
*headerPacket->packet = 0;
if(this->push_queues_size[push_queue_index] == HEADER_PACKET_STACK_POOL_SIZE) {
if(stack->push(&this->push_queues[push_queue_index], false, true)) {
this->push_queues_size[push_queue_index] = 0;
//cout << "+" << flush;
} else {
delete [] headerPacket;
return(2);
}
}
if(this->push_queues_size[push_queue_index] < HEADER_PACKET_STACK_POOL_SIZE) {
this->push_queues[push_queue_index].pool[this->push_queues_size[push_queue_index]] = headerPacket;
++this->push_queues_size[push_queue_index];
}
return(1);
}
inline int pop(sHeaderPacket **headerPacket) {
if(this->pop_queue_size > 0) {
*headerPacket = this->pop_queue.pool[HEADER_PACKET_STACK_POOL_SIZE - this->pop_queue_size];
--this->pop_queue_size;
} else {
if(stack->popq(&this->pop_queue)) {
*headerPacket = this->pop_queue.pool[0];
this->pop_queue_size = HEADER_PACKET_STACK_POOL_SIZE - 1;
//cout << "P" << flush;
} else {
*headerPacket = (sHeaderPacket*)new FILE_LINE(9002) u_char[sizeof(sHeaderPacket) + packet_alloc_size];
(*headerPacket)->stack = this;
(*headerPacket)->packet_alloc_size = packet_alloc_size;
(*headerPacket)->clearPcapProcessData();
return(2);
}
}
/*
if(*(u_char*)(&(*headerPacket)->header) ||
*(*headerPacket)->packet) {
printf("bad headerPacket from allocation stack\n");
abort();
}
*/
(*headerPacket)->clearPcapProcessData();
return(1);
}
inline int pop_queue_prepare() {
if(!pop_queue_pool_prepared) {
int rslt = 0;
__SYNC_LOCK(_sync_prepare);
if(stack->popq(&this->pop_queue_pool_prepare)) {
for(int i = 0; i < HEADER_PACKET_STACK_POOL_SIZE; i++) {
memset(this->pop_queue_pool_prepare.pool[i], 0, 100);
this->pop_queue_pool_prepare.pool[i]->stack = this;
this->pop_queue_pool_prepare.pool[i]->packet_alloc_size = packet_alloc_size;
this->pop_queue_pool_prepare.pool[i]->clearPcapProcessData();
}
pop_queue_pool_prepared = true;
rslt = 1;
}
__SYNC_UNLOCK(_sync_prepare);
return(rslt);
}
return(0);
}
inline int pop_prepared(sHeaderPacket **headerPacket) {
if(this->pop_queue_size > 0) {
*headerPacket = this->pop_queue.pool[HEADER_PACKET_STACK_POOL_SIZE - this->pop_queue_size];
--this->pop_queue_size;
} else {
__SYNC_LOCK(_sync_prepare);
if(pop_queue_pool_prepared) {
this->pop_queue = this->pop_queue_pool_prepare;
pop_queue_pool_prepared = false;
__SYNC_UNLOCK(_sync_prepare);
*headerPacket = this->pop_queue.pool[0];
this->pop_queue_size = HEADER_PACKET_STACK_POOL_SIZE - 1;
//cout << "P" << flush;
} else {
__SYNC_UNLOCK(_sync_prepare);
*headerPacket = (sHeaderPacket*)new FILE_LINE(9003) u_char[sizeof(sHeaderPacket) + packet_alloc_size];
(*headerPacket)->stack = this;
(*headerPacket)->packet_alloc_size = packet_alloc_size;
(*headerPacket)->clearPcapProcessData();
return(2);
}
}
return(1);
}
public:
u_int32_t packet_alloc_size;
sHeaderPacketPool pop_queue;
uint16_t pop_queue_size;
sHeaderPacketPool push_queues[HEADER_PACKET_STACK_PUSH_QUEUE_MAX];
u_int16_t push_queues_size[HEADER_PACKET_STACK_PUSH_QUEUE_MAX];
rqueue_quick<sHeaderPacketPool> *stack;
volatile bool pop_queue_pool_prepared;
sHeaderPacketPool pop_queue_pool_prepare;
volatile int _sync_prepare;
};
inline sHeaderPacket *CREATE_HP(u_int32_t packet_alloc_size) {
sHeaderPacket *header_packet = (sHeaderPacket*)new FILE_LINE(9004) u_char[sizeof(sHeaderPacket) + packet_alloc_size];
header_packet->stack = NULL;
header_packet->packet_alloc_size = packet_alloc_size;
header_packet->clearPcapProcessData();
return(header_packet);
}
inline void DESTROY_HP(sHeaderPacket **header_packet) {
delete [] (u_char*)(*header_packet);
*header_packet = NULL;
}
inline void PUSH_HP(sHeaderPacket **header_packet, int16_t push_queue_index) {
if(push_queue_index >= 0 && (*header_packet)->stack) {
((cHeaderPacketStack*)((*header_packet)->stack))->push(*header_packet, push_queue_index);
*header_packet = NULL;
} else {
DESTROY_HP(header_packet);
}
}
#endif