diff --git a/NATRouter.cpp b/NATRouter.cpp index 4909652..199371e 100644 --- a/NATRouter.cpp +++ b/NATRouter.cpp @@ -45,8 +45,14 @@ int NATRouter::getFreePort() if (this->lastUsedPort == NAT_TABLE_LEN-1) 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; inatTable[i].isFree()) + { + this->lastUsedPort = i; + return i; + } for (int i = 1; ilastUsedPort; ++i) if (this->natTable[i].isFree()) @@ -148,9 +154,11 @@ void NATRouter::sNAT(Packet *packet) { int port; NATItem *natItem; + bool allocated = false; + stringstream ss; // mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac - // TODO getAllocatedPort - STRASZNIE zmula caly symulator + // TO port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort()); if (port == -1) while ((port = this->getFreePort()) == -1) @@ -159,14 +167,25 @@ void NATRouter::sNAT(Packet *packet) this->print("NAT table full, waiting 1s", true); this->delay(1); } + else + allocated = true; // wstawiam do tablicy NAT info ze port zarezerowany na odbior natItem = &this->natTable[port]; natItem->setIp(packet->getSrcIp()); 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(ss.str(), port)); + + ss.str(""); ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port; packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe @@ -219,11 +238,26 @@ string NATRouter::getWanNetwork() */ int NATRouter::getAllocatedPort(string srcIp, int srcPort) { - for (int i=1; i::iterator it; + int pos = -1; + /*for (int i=1; inatTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort) 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() diff --git a/NATRouter.h b/NATRouter.h index d551c00..6dd60de 100644 --- a/NATRouter.h +++ b/NATRouter.h @@ -15,6 +15,7 @@ class NATRouter : public Node { NATItem *natTable = NULL; // tablica z zaalokowanymi portami na zewnÄ…trz + map natHelper; // mapa pomocnicza pozwalajaca szybko znalezc wpis NetConf netWanConf; // konfiguracja sieciowa na zewnÄ…trz void initalizeNatTable(); int getFreePort(); diff --git a/Simulation.cpp b/Simulation.cpp index f6ab76d..6924b41 100644 --- a/Simulation.cpp +++ b/Simulation.cpp @@ -324,8 +324,8 @@ void Simulation::natOverflowSimulation(int nNodes) if (currentSrcPort[i] == 65535) currentSrcPort[i] = 1; - p.setSrcPort(currentSrcPort[i]++); - + //p.setSrcPort(currentSrcPort[i]++); + //p.setSrcPort(0); client[i].send(p); this->delay(150);