Browse Source

poprawka losowania wolnego portu, poprawka liczenia timeoutu dla SNAT (+15 zamiast sztywno 15)

master
Piotr Dergun 7 years ago
parent
commit
4f7736de3b
3 changed files with 44 additions and 9 deletions
  1. +41
    -7
      NATRouter.cpp
  2. +1
    -0
      NATRouter.h
  3. +2
    -2
      Simulation.cpp

+ 41
- 7
NATRouter.cpp View File

@ -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()

+ 1
- 0
NATRouter.h View File

@ -15,6 +15,7 @@
class NATRouter : public Node class NATRouter : public Node
{ {
NATItem *natTable = NULL; // tablica z zaalokowanymi portami na zewnątrz NATItem *natTable = NULL; // tablica z zaalokowanymi portami na zewnątrz
map<string, int> natHelper; // mapa pomocnicza pozwalajaca szybko znalezc wpis
NetConf netWanConf; // konfiguracja sieciowa na zewnątrz NetConf netWanConf; // konfiguracja sieciowa na zewnątrz
void initalizeNatTable(); void initalizeNatTable();
int getFreePort(); int getFreePort();

+ 2
- 2
Simulation.cpp View File

@ -324,8 +324,8 @@ void Simulation::natOverflowSimulation(int nNodes)
if (currentSrcPort[i] == 65535) if (currentSrcPort[i] == 65535)
currentSrcPort[i] = 1; currentSrcPort[i] = 1;
p.setSrcPort(currentSrcPort[i]++);
//p.setSrcPort(currentSrcPort[i]++);
//p.setSrcPort(0);
client[i].send(p); client[i].send(p);
this->delay(150); this->delay(150);

Loading…
Cancel
Save