-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNAT.cpp
43 lines (35 loc) · 1003 Bytes
/
NAT.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
#include "NAT.h"
string NAT::lookup(string addr)
{
// Look for the address in our NAT table
auto it = nat.find(addr);
if (it != nat.end())
return it->second;
// Look for a match on the ip (ie a NAT entry with port = *)
// by looking up ip.*
string addr_ip = addr.substr(0, 1 + addr.find(':')) + '*';
it = nat.find(addr_ip);
if (it != nat.end())
return it->second;
// Look for a match on the port (ie a NAT entry with ip = *)
// by looking up *:port
string addr_port = '*' + addr.substr(addr.find(':'));
it = nat.find(addr_port);
if (it != nat.end())
return it->second;
return "";
}
void NAT::init(const string& fname)
{
ifstream natfile(fname);
if (!natfile)
cerr << "Error: failed to open nat file for reading" << endl;
string entry, addr, new_addr;
while (getline(natfile, entry))
{
addr = entry.substr(0, entry.find(','));
new_addr = entry.substr(1 + entry.find(','));
nat[addr] = new_addr;
}
natfile.close();
}