-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathIpReassemblyBuffer.cpp
66 lines (52 loc) · 1.77 KB
/
IpReassemblyBuffer.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
#include "IpReassemblyBuffer.h"
#include <assert.h>
namespace FeatureExtractor {
using namespace std;
IpReassemblyBuffer::IpReassemblyBuffer()
: datagram(nullptr), first_frag_ts(), last_frag_ts()
, frame_count(0), total_length(0)
{
}
IpReassemblyBuffer::~IpReassemblyBuffer()
{
// Datagram is returned by calling method add_fragment() or& must be deallocated by caller
}
Timestamp IpReassemblyBuffer::get_last_fragment_ts() const
{
return last_frag_ts;
}
IpDatagram *IpReassemblyBuffer::add_fragment(const IpFragment *frag)
{
// If first fragment (by order in datagram) received for the first time,
// create datagram with its values
if (!datagram && frag->get_ip_frag_offset() == 0) {
datagram = new IpDatagram(*frag);
}
// Timestamps, fragment/frame count & total_length of datagram
if (frame_count == 0)
first_frag_ts = frag->get_start_ts();
last_frag_ts = frag->get_start_ts();
frame_count++;
total_length += frag->get_length();
// Flag MF = 0 only if last fragment
bool is_last_frag = !frag->get_ip_flag_mf();
size_t frag_start = frag->get_ip_frag_offset();
size_t frag_end = frag_start + frag->get_ip_payload_length() - 1;
// Fill holes with new fragment
hole_list.add_fragment(frag_start, frag_end, is_last_frag);
// If no hole left IP datagram is reassembled
if (hole_list.is_empty()) {
assert(datagram != nullptr && "IP reassebly failed: NULL datagram");
// Update timestamps, frame count & length
datagram->set_start_ts(first_frag_ts);
datagram->set_end_ts(last_frag_ts);
datagram->set_frame_count(frame_count);
datagram->set_length(total_length);
// Caller should take care of destroying the datagram object
IpDatagram *ret = datagram;
datagram = nullptr;
return ret;
}
return nullptr;
}
}