-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.cpp
136 lines (125 loc) · 2.93 KB
/
connection.cpp
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
#include "connection.h"
#include <stdio.h>
#include <string.h>
#include <iterator>
Connection::Connection()
{
buffer = NULL;
buffer_size = 0;
}
Connection::Connection(string session_id)
{
this->session_id = session_id;
buffer = new unsigned char [BUFFER_ALLOCATE];
buffer_size = 0;
}
Connection::Connection(const Connection & c)
{
buffer = new unsigned char [BUFFER_ALLOCATE];
session_id = c.session_id;
memcpy(buffer, c.buffer, c.buffer_size);
buffer_size = c.buffer_size;
}
Connection::~Connection()
{
if (buffer != NULL)
delete [] buffer;
map <uint, Packet>::iterator it1;
for (it1 = packet_map.begin(); it1 != packet_map.end(); it1++)
{
delete [] (it1->second.packet);
}
}
void Connection::addData(unsigned char * payload, uint size_payload, Packet packet)
{
if (BUFFER_ALLOCATE - buffer_size < size_payload)
{
fprintf(stderr, "Something went wrong, unable to add payload to Connection buffer.\n");
return;
}
memcpy(buffer + buffer_size, payload, size_payload);
uint start_pos = buffer_size;
buffer_size += size_payload;
uint packet_size = packet.header.caplen;
unsigned char * copy_packet = new unsigned char [packet_size];
memcpy(copy_packet, packet.packet, packet_size);
Packet copy_p(copy_packet, packet.header);
packet_map[start_pos] = copy_p;
}
uint Connection::getBufferSize()
{
return buffer_size;
}
unsigned char * Connection::getBuffer()
{
return buffer;
}
vector<Packet> Connection::getPackets()
{
vector<Packet> v;
for (auto it1 = packet_map.begin(); it1 != packet_map.end(); it1++)
{
v.push_back(it1->second);
}
return v;
}
void Connection::extractData(uint size)
{
if (size == 0)
return;
if (size > buffer_size)
size = buffer_size;
if (size == BUFFER_ANALYZE)
{
memmove(buffer, buffer + (size*2/3), buffer_size - (size*2/3));
buffer_size -= size*2/3;
size = size *2/3; //the actual number of bytes we removed from the buffer
}
else
{
buffer_size = 0;
}
map <uint, Packet> new_packet_map;
map <uint, Packet>::iterator it1, it2;
for (it1 = packet_map.begin(); it1 != packet_map.end(); it1++)
{
if (it1->first < size) //we extracted at least part of this packet
{
if (next(it1) != packet_map.end())
{
if ((next(it1)->first) <= size)
{
//remove package
delete [] (it1->second.packet);
}
else
{
new_packet_map[0] = it1->second;
for (it2 = next(it1); it2 != packet_map.end(); it2++)
new_packet_map[it2->first-size] = it2->second;
break;
}
}
if (next(it1) == packet_map.end())
{
if (buffer_size != 0)
new_packet_map[0] = it1->second;
else
delete [] (it1->second.packet);
}
}
else if (it1->first == size)
{
new_packet_map[0] = it1->second;
for (it2 = next(it1); it2 != packet_map.end(); it2++)
new_packet_map[it2->first-size] = it2->second;
break;
}
else
{
fprintf(stderr, "Connection::extractData: Something is wrong, shouldn't get there\n");
break;
}
}
packet_map = new_packet_map;
}