-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathnet.cpp
125 lines (102 loc) · 1.94 KB
/
net.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
#include "net.h"
namespace FeatureExtractor {
bool ether_header_t::is_ethernet2() const
{
return (ntohs(type_length) >= MIN_ETH2);
}
bool ether_header_t::is_type_ipv4() const
{
return (ntohs(type_length) == IPV4);
}
uint8_t* ether_header_t::get_eth2_sdu() const
{
return (((uint8_t *) this) + ETH2_HEADER_LENGTH);
}
uint8_t ip_header_t::ihl() const
{
return (ver_ihl & 0x0F);
}
size_t ip_header_t::header_length() const
{
return ihl() * sizeof(uint32_t);
}
uint8_t ip_header_t::flags() const
{
return (ntohs(flags_fo) >> 13) & 0x7;
}
bool ip_header_t::flag_eb() const
{
return ((flags() & 0x1) != 0);
}
bool ip_header_t::flag_df() const
{
return ((flags() & 0x2) != 0);
}
bool ip_header_t::flag_mf() const
{
return ((flags() & 0x4) != 0);
}
size_t ip_header_t::frag_offset() const
{
return (ntohs(flags_fo) & 0x01FFF) << 3; // 1 unit = 8 bytes
}
const char *ip_header_t::protocol_str() const
{
switch (protocol) {
case ICMP:
return "ICMP";
break;
case TCP:
return "TCP";
break;
case UDP:
return "UDP";
break;
default:
break;
}
return "other";
}
uint8_t* ip_header_t::get_sdu() const
{
return (((uint8_t *) this) + header_length());
}
tcp_field_flags_t::tcp_field_flags_t(uint8_t flags)
: flags(flags)
{}
tcp_field_flags_t::tcp_field_flags_t()
: flags(0)
{}
bool tcp_field_flags_t::fin() const
{
return ((flags & 0x01) != 0);
}
bool tcp_field_flags_t::syn() const
{
return ((flags & 0x02) != 0);
}
bool tcp_field_flags_t::rst() const
{
return ((flags & 0x04) != 0);
}
bool tcp_field_flags_t::psh() const
{
return ((flags & 0x08) != 0);
}
bool tcp_field_flags_t::ack() const
{
return ((flags & 0x10) != 0);
}
bool tcp_field_flags_t::urg() const
{
return ((flags & 0x20) != 0);
}
bool tcp_field_flags_t::ece() const
{
return ((flags & 0x40) != 0);
}
bool tcp_field_flags_t::cwr() const
{
return ((flags & 0x80) != 0);
}
}