-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNAT.cpp
More file actions
91 lines (85 loc) · 1.96 KB
/
NAT.cpp
File metadata and controls
91 lines (85 loc) · 1.96 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
bool check(string a, string b) {// check if there's matching address
int i = 0, j = b.size();
if (a[0] == '*') i = b.size() - a.size() + 1;// When IP is *
if (a[a.size() - 1] == '*') j = a.size() - 1 + i;// When port is *
for (int k = i; k < j; k ++) {
if (i == 0 && a[k] != b[k]) return false;
else if (i != 0 && a[k - i + 1] != b[k]) return false;
}
return true;
}
int main() {
ifstream nat, flow;
nat.open("NAT.txt");
string res = "", tmp = "", t1 = "", t2 = "";
vector<pair<string, string> > list;
char c;
while (nat.get(c)) {// Build a list of NAT matching
if (c == ',') {
t1 = tmp;
tmp = "";
}
else if (c == '\n') {
t2 = tmp;
if (t1 != "" && t2 != "") {
pair<string, string> p;
p.first = t1;
p.second = t2;
list.push_back(p);
}
tmp = "";
t1 = "";
t2 = "";
}
else tmp += c;
}
// for the last line of the file
t2 = tmp;
if (t1 != "" && t2 != "") {
pair<string, string> p;
p.first = t1;
p.second = t2;
list.push_back(p);
}
tmp = "";
t1 = "";
t2 = "";
nat.close();
flow.open("FLOW.txt");
while (flow.get(c)) {// Check the input flow
if (c == '\n') {
bool isChecked = false;
for (int i = 0; i < list.size(); i ++) {
if (check(list[i].first, tmp)) {
res += tmp + " -> " + list[i].second + '\n';
isChecked = true;
break;
}
}
if (!isChecked) res += "No nat match for " + tmp + '\n';
tmp = "";
}
else tmp += c;
}
// for the last line of the file
bool isChecked = false;
for (int i = 0; i < list.size(); i ++) {
if (check(list[i].first, tmp)) {
res += tmp + " -> " + list[i].second;
isChecked = true;
break;
}
}
if (!isChecked) res += "No nat match for " + tmp;
flow.close();
cout << res;
ofstream fout("OUTPUT.txt");
fout << res;
fout.close();
return 0;
}