|
@ -45,8 +45,14 @@ int NATRouter::getFreePort() |
|
|
if (this->lastUsedPort == NAT_TABLE_LEN-1) |
|
|
if (this->lastUsedPort == NAT_TABLE_LEN-1) |
|
|
this->lastUsedPort = 0; |
|
|
this->lastUsedPort = 0; |
|
|
|
|
|
|
|
|
if (this->natTable[this->lastUsedPort+1].isFree()) |
|
|
|
|
|
return ++this->lastUsedPort; |
|
|
|
|
|
|
|
|
//if (this->natTable[this->lastUsedPort+1].isFree())
|
|
|
|
|
|
// return ++this->lastUsedPort;
|
|
|
|
|
|
for (int i = this->lastUsedPort+1; i<NAT_TABLE_LEN; ++i) |
|
|
|
|
|
if (this->natTable[i].isFree()) |
|
|
|
|
|
{ |
|
|
|
|
|
this->lastUsedPort = i; |
|
|
|
|
|
return i; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for (int i = 1; i<this->lastUsedPort; ++i) |
|
|
for (int i = 1; i<this->lastUsedPort; ++i) |
|
|
if (this->natTable[i].isFree()) |
|
|
if (this->natTable[i].isFree()) |
|
@ -148,9 +154,11 @@ void NATRouter::sNAT(Packet *packet) |
|
|
{ |
|
|
{ |
|
|
int port; |
|
|
int port; |
|
|
NATItem *natItem; |
|
|
NATItem *natItem; |
|
|
|
|
|
bool allocated = false; |
|
|
|
|
|
stringstream ss; |
|
|
|
|
|
|
|
|
// mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
|
|
|
// mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
|
|
|
// TODO getAllocatedPort - STRASZNIE zmula caly symulator
|
|
|
|
|
|
|
|
|
// TO
|
|
|
port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort()); |
|
|
port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort()); |
|
|
if (port == -1) |
|
|
if (port == -1) |
|
|
while ((port = this->getFreePort()) == -1) |
|
|
while ((port = this->getFreePort()) == -1) |
|
@ -159,14 +167,25 @@ void NATRouter::sNAT(Packet *packet) |
|
|
this->print("NAT table full, waiting 1s", true); |
|
|
this->print("NAT table full, waiting 1s", true); |
|
|
this->delay(1); |
|
|
this->delay(1); |
|
|
} |
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
allocated = true; |
|
|
|
|
|
|
|
|
// wstawiam do tablicy NAT info ze port zarezerowany na odbior
|
|
|
// wstawiam do tablicy NAT info ze port zarezerowany na odbior
|
|
|
natItem = &this->natTable[port]; |
|
|
natItem = &this->natTable[port]; |
|
|
natItem->setIp(packet->getSrcIp()); |
|
|
natItem->setIp(packet->getSrcIp()); |
|
|
natItem->setPort(packet->getSrcPort()); |
|
|
natItem->setPort(packet->getSrcPort()); |
|
|
natItem->setTimeout(15); |
|
|
|
|
|
|
|
|
|
|
|
stringstream ss; |
|
|
|
|
|
|
|
|
if (allocated) |
|
|
|
|
|
natItem->increaseTimeout(15); |
|
|
|
|
|
else |
|
|
|
|
|
natItem->setTimeout(15); |
|
|
|
|
|
|
|
|
|
|
|
ss << packet->getSrcIp() << ":" << packet->getSrcPort(); |
|
|
|
|
|
|
|
|
|
|
|
if (!allocated) |
|
|
|
|
|
this->natHelper.insert(pair<string, int>(ss.str(), port)); |
|
|
|
|
|
|
|
|
|
|
|
ss.str(""); |
|
|
ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port; |
|
|
ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port; |
|
|
|
|
|
|
|
|
packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
|
|
|
packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
|
|
@ -219,11 +238,26 @@ string NATRouter::getWanNetwork() |
|
|
*/ |
|
|
*/ |
|
|
int NATRouter::getAllocatedPort(string srcIp, int srcPort) |
|
|
int NATRouter::getAllocatedPort(string srcIp, int srcPort) |
|
|
{ |
|
|
{ |
|
|
for (int i=1; i<NAT_TABLE_LEN; ++i) |
|
|
|
|
|
|
|
|
map<string, int>::iterator it; |
|
|
|
|
|
int pos = -1; |
|
|
|
|
|
/*for (int i=1; i<NAT_TABLE_LEN; ++i)
|
|
|
if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort) |
|
|
if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort) |
|
|
return i; |
|
|
return i; |
|
|
|
|
|
*/ |
|
|
|
|
|
stringstream ss; |
|
|
|
|
|
ss << srcIp << ":" << srcPort; |
|
|
|
|
|
|
|
|
|
|
|
it = this->natHelper.find(ss.str()); |
|
|
|
|
|
if (it != this->natHelper.end()) |
|
|
|
|
|
{ |
|
|
|
|
|
// sprawdz czy wpis o podanym IP z nat nie zostal napisany (referencja nieaktualna)
|
|
|
|
|
|
if (this->natTable[it->second].getIp() != srcIp || this->natTable[it->second].getPort() != srcPort) |
|
|
|
|
|
this->natHelper.erase(it); |
|
|
|
|
|
else |
|
|
|
|
|
pos = it->second; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return -1; // nic nie znaleziono
|
|
|
|
|
|
|
|
|
return pos; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void NATRouter::freePorts() |
|
|
void NATRouter::freePorts() |
|
|