-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.cpp
214 lines (170 loc) · 4.38 KB
/
entry.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <string>
#include <stdexcept>
#include <cctype>
#include "entry.hpp"
using namespace std;
Entry::Entry() {
this->setDefault();
}
Entry::Entry(string protocol, string line) {
this->setDefault();
this->fetchLine(protocol, line);
}
Entry::~Entry() {
}
void Entry::fetchLine(string protocol, string line) {
this->line = line;
this->protocol = protocol;
this->fetched = 1;
this->parseLine();
}
string Entry::getStateEnum(){
if (this->protocol == "udp") return "";
switch(this->state) {
case TCP_ESTABLISHED:
return "ESTABLISHED";
case TCP_SYN_SENT:
return "SYN_SENT";
case TCP_SYN_RECV:
return "SYN_RECV";
case TCP_FIN_WAIT1:
return "FIN_WAIT1";
case TCP_FIN_WAIT2:
return "FIN_WAIT2";
case TCP_TIME_WAIT:
return "TIME_WAIT";
case TCP_CLOSE:
return "CLOSE";
case TCP_CLOSE_WAIT:
return "CLOSE_WAIT";
case TCP_LAST_ACK:
return "LAST_ACK";
case TCP_LISTEN:
return "LISTEN";
case TCP_CLOSING:
return "CLOSING";
default:
return "";
}
}
void Entry::printLineDebug() {
cout << this->protocol << " : " << this->line << endl;
if (this->parsed) {
cout << "\t" << "selector: " << this->sl << endl;
cout << "\t" << "local_addr: " << this->local_addr << endl;
cout << "\t" << "local_port: " << this->local_port << endl;
cout << "\t" << "remote_addr: " << this->remote_addr << endl;
cout << "\t" << "remote_port: " << this->remote_port << endl;
cout << "\t" << "state: " << this->getStateEnum() << endl;
cout << "\t" << "inode: " << this->inode << endl;
}
}
#define IPV6_HEXLEN 32
#define IPV4_HEXLEN 8
void reverseIPHexString(string& str) {
for (size_t i = 0; i < str.length()/2; i += 2) {
char tmp = str[i];
str[i] = str[str.length()-1 - 1 - i];
str[str.length()-1 - 1 - i] = tmp;
tmp = str[i+1];
str[i+1] = str[str.length()-1 - i];
str[str.length()-1 - i] = tmp;
}
}
void ipv4HexaToDec(string hexa, string& ipdec) {
ipdec = "";
for (int i = 0; i < 4; i++){
string oct = hexa.substr(2*i, 2);
int dec = stoi(oct, nullptr, 16);
ipdec.append(to_string(dec) + ".");
}
ipdec.pop_back();
}
void formatIPV6(string ipv6, string& ip) {
ip = "";
for (size_t i = 0; i < ipv6.length(); i++) {
if (((i % 4) == 0) && i != 0) ip.push_back(':');
ip.push_back(ipv6[i]);
}
}
void Entry::parseLine(){
if (!this->fetched) throw runtime_error(string(__func__) + string(": already fetched"));
this->parsed = 1;
size_t pos = 0;
size_t end = 0;
string hexa = "";
this->sl = stoi(this->line, &pos);
pos += 2;
end = this->line.find_first_of(':', pos);
hexa = this->line.substr(pos, end - pos);
reverseIPHexString(hexa);
if (hexa.length() == IPV4_HEXLEN)
ipv4HexaToDec(hexa, this->local_addr);
else if (hexa.length() == IPV6_HEXLEN)
formatIPV6(hexa, this->local_addr);
else
runtime_error(string(__func__) + string(": bad ip"));
pos = end + 1;
this->local_port = stoi(this->line.substr(pos, 4), &end, 16);
pos += 5;
end = this->line.find_first_of(':', pos);
hexa = this->line.substr(pos, end - pos);
reverseIPHexString(hexa);
if (hexa.length() == IPV4_HEXLEN)
ipv4HexaToDec(hexa, this->remote_addr);
else if (hexa.length() == IPV6_HEXLEN)
formatIPV6(hexa, this->remote_addr);
else
throw runtime_error(string(__func__) + string(": bad ip"));
pos = end + 1;
this->remote_port = stoi(this->line.substr(pos, 4), &end, 16);
pos += 5;
this->state = stoi(this->line.substr(pos, 2), &end, 16);
pos += 57;
this->inode = stoi(this->line.substr(pos, 10), &end, 10);
}
string Entry::printLineFormat() {
string result;
result = this->protocol + " " + this->local_addr + " " +
to_string(this->local_port) + " " + this->remote_addr + " " +
to_string(this->remote_port) + " ";
return result;
}
long Entry::getInode() {
return this->inode;
}
int Entry::getState() {
return this->state;
}
string Entry::getProtocol() {
return this->protocol;
}
int Entry::wasPrinted() {
return this->print;
}
void Entry::printed() {
this->print = 1;
}
int Entry::equal(Entry &e) {
if (this->local_addr == e.local_addr &&
this->local_port == e.local_port &&
this->remote_addr == e.remote_addr &&
this->remote_port == e.remote_port) {
return 1;
}
return 0;
}
void Entry::setDefault(){
this->line = "";
this->fetched = 0;
this->parsed = 0;
this->sl = 0;
this->local_addr = "";
this->local_port = 0;
this->remote_addr = "";
this->remote_port = 0;
this->state = -1;
this->inode = 0;
this->print = 0;
}