-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_storage.cpp
More file actions
102 lines (91 loc) · 2.17 KB
/
shared_storage.cpp
File metadata and controls
102 lines (91 loc) · 2.17 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
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
#include "shared_storage.h"
#include "settings.h"
#include <cstdint>
#include <random>
Packet::Packet(vector<uint8_t> && data)
: data(std::move(data)),
expiration(DEFAULT_SENT_PACKET_TIMEOUT)
{
}
Packet::Packet(Tins::PDU & pdu)
: Packet(std::move(pdu.serialize()))
{
}
Packet::Packet(Tins::PDU *pdu)
: Packet(std::move(pdu->serialize()))
{
}
bool Packet::operator==(const Packet & other) const
{
return this->data == other.data;
}
std::size_t Packet::Hash::operator()(const Packet & packet) const noexcept
{
std::size_t hash = packet.data.size();
for (int i = 0; i < packet.data.size(); i++)
{
auto byte = packet.data[i];
hash ^= byte + 73 + (hash >> 1) + (hash << 3);
}
return hash;
}
string protocolToString(Protocol protocol)
{
switch (protocol)
{
case Protocol::EthernetII:
return "EthernetII";
case Protocol::ARP:
return "ARP";
case Protocol::IP:
return "IP";
case Protocol::TCP:
return "TCP";
case Protocol::UDP:
return "UDP";
case Protocol::ICMP:
return "ICMP";
case Protocol::HTTP:
return "HTTP";
}
}
InterfaceEntry & SharedStorage::getInterface(mac_address address)
{
for (auto & interface : interfaces)
{
if (interface.first.hw_address() == address)
{
return interface.second;
}
}
throw std::runtime_error("No interface with the following MAC: " + address.to_string());
}
Session::Session()
: expiration(DEFAULT_SESSION_TIMEOUT),
token()
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distribution(0, 64);
for (int i = 0; i < TOKEN_LENGTH; i++)
{
auto result = distribution(rng);
if (result < 10)
{
this->token[i] = result + '0';
}
else if (result < 36)
{
this->token[i] = result - 10 + 'a';
}
else if (result < 62)
{
this->token[i] = result - 36 + 'A';
}
else
{
this->token[i] = result % 2 == 0 ? '-' : '_';
}
}
this->token[TOKEN_LENGTH] = '\0';
}