|
|
@ -42,10 +42,23 @@ NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp) |
|
|
|
|
|
|
|
int NATRouter::getFreePort() |
|
|
|
{ |
|
|
|
if (this->lastUsedPort == NAT_TABLE_LEN-1) |
|
|
|
this->lastUsedPort = 0; |
|
|
|
|
|
|
|
if (this->natTable[this->lastUsedPort+1].isFree()) |
|
|
|
return ++this->lastUsedPort; |
|
|
|
|
|
|
|
for (int i = 1; i<this->lastUsedPort; ++i) |
|
|
|
if (this->natTable[i].isFree()) |
|
|
|
{ |
|
|
|
this->lastUsedPort = i; |
|
|
|
return i; |
|
|
|
} |
|
|
|
/*
|
|
|
|
for (int i=1; i<NAT_TABLE_LEN; ++i) |
|
|
|
if (this->natTable[i].isFree()) |
|
|
|
return i; |
|
|
|
|
|
|
|
*/ |
|
|
|
return -1; // nie ma żadnego wolnego portu
|
|
|
|
} |
|
|
|
|
|
|
@ -62,10 +75,6 @@ void NATRouter::onRecv() |
|
|
|
Packet p = this->recv(); |
|
|
|
stringstream s; |
|
|
|
|
|
|
|
/*if (this->getHostname() == "Router 2") {
|
|
|
|
s << "src: " << p.getSrcIp() << ":" << p.getSrcPort() << "dst: " << p.getDstIp() << ":" << p.getDstPort(); |
|
|
|
this->print(s.str()); |
|
|
|
}*/ |
|
|
|
if (p.getSrcPort() != 0) |
|
|
|
{ |
|
|
|
// jezeli pakiet idzie z jednego wezla do drugiego w danej possieci to odeslij mu "jak switch"
|
|
|
@ -141,12 +150,13 @@ void NATRouter::sNAT(Packet *packet) |
|
|
|
NATItem *natItem; |
|
|
|
|
|
|
|
// mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
|
|
|
|
// TODO getAllocatedPort - STRASZNIE zmula caly symulator
|
|
|
|
port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort()); |
|
|
|
if (port == -1) |
|
|
|
while ((port = this->getFreePort()) == -1) |
|
|
|
{ |
|
|
|
//TESTOWO
|
|
|
|
this->print("NAT table full, waiting 1s"); |
|
|
|
this->print("NAT table full, waiting 1s", true); |
|
|
|
this->delay(1); |
|
|
|
} |
|
|
|
|
|
|
@ -154,7 +164,7 @@ void NATRouter::sNAT(Packet *packet) |
|
|
|
natItem = &this->natTable[port]; |
|
|
|
natItem->setIp(packet->getSrcIp()); |
|
|
|
natItem->setPort(packet->getSrcPort()); |
|
|
|
natItem->setTimeout(/*5*/15); |
|
|
|
natItem->setTimeout(15); |
|
|
|
|
|
|
|
stringstream ss; |
|
|
|
ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port; |
|
|
@ -162,7 +172,7 @@ void NATRouter::sNAT(Packet *packet) |
|
|
|
packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
|
|
|
|
packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
|
|
|
|
|
|
|
|
this->print(ss.str()); |
|
|
|
this->print(ss.str(), true); |
|
|
|
this->delay(); |
|
|
|
this->send(*packet, true); // wysylam dalej
|
|
|
|
natItem = NULL; |
|
|
@ -223,8 +233,9 @@ void NATRouter::freePorts() |
|
|
|
|
|
|
|
Log portsLog; |
|
|
|
string name = "NAT"; |
|
|
|
portsLog.setLogParams(10, BLUE, "\t\t"); |
|
|
|
portsLog.setLogParams(3, GREEN, "\t\t"); |
|
|
|
portsLog.setObjectName(&name); |
|
|
|
portsLog.setDelay(100000); |
|
|
|
|
|
|
|
while(true) |
|
|
|
{ |
|
|
@ -238,9 +249,10 @@ void NATRouter::freePorts() |
|
|
|
if (ports < peak) |
|
|
|
peak = ports; |
|
|
|
|
|
|
|
ss << "Free:\t" << ports << " (" << peak << ")\t\tReserved:\t" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0
|
|
|
|
ss << "Free/Reserved:\t" << ports << " (" << peak << ")/" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0
|
|
|
|
|
|
|
|
portsLog.print(ss.str()); |
|
|
|
portsLog.printProgressBar(4, 4, "Utilization", (float)(NAT_TABLE_LEN-ports-1)/(NAT_TABLE_LEN-1)); |
|
|
|
portsLog.delay(); |
|
|
|
} |
|
|
|
|
|
|
|