-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCAPRecord.cpp
More file actions
63 lines (53 loc) · 1.77 KB
/
PCAPRecord.cpp
File metadata and controls
63 lines (53 loc) · 1.77 KB
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
#include "stdafx.h"
#include "Packet.h"
#include "PCAPHeader.h"
#include "PCAPRecord.h"
Yellow::PCAP::Record::Record(std::shared_ptr<Yellow::Packet> packet)
: _packet(packet)
{
std::chrono::high_resolution_clock::duration tp = _packet->getTimePoint().time_since_epoch();
_hdr.ts_sec = std::chrono::duration_cast<std::chrono::seconds>(tp).count();
tp -= std::chrono::duration_cast<std::chrono::seconds>(tp);
_hdr.ts_usec = std::chrono::duration_cast<std::chrono::microseconds>(tp).count();
_hdr.orig_len = _packet->getLenght();
_hdr.incl_len = 0;
}
Yellow::PCAP::Record::Record()
: _packet(nullptr)
{
}
Yellow::PCAP::Record::~Record()
{
}
const Yellow::PCAP::RecordHeader &Yellow::PCAP::Record::getHeader() const
{
return _hdr;
}
const std::shared_ptr<Yellow::Packet> Yellow::PCAP::Record::getPacket() const
{
return _packet;
}
std::ostream &Yellow::PCAP::operator<<(std::ostream &output, Yellow::PCAP::Record &user)
{
if (user._packet == nullptr)
return output;
user._hdr.incl_len = user._hdr.orig_len;
output.write(reinterpret_cast<char *>(&user._hdr), sizeof(user._hdr));
if (!output || output.eof())
return (output);
output.write(reinterpret_cast<const char *>((*user._packet)[0]), user._packet->getLenght());
return output;
}
std::istream &Yellow::PCAP::operator>>(std::istream &input, Yellow::PCAP::Record &user)
{
void *buff;
input.read(reinterpret_cast<char *>(&user._hdr), sizeof(user._hdr));
if (!input || input.eof())
return (input);
buff = new unsigned char[user._hdr.incl_len];
input.read(reinterpret_cast<char *>(buff), user._hdr.incl_len);
if (!input || input.eof())
return input;
user._packet = std::make_shared<Yellow::Packet>(buff, static_cast<unsigned int>(user._hdr.incl_len));
return input;
}