NAT - DNAT podbija timeout (zamiast usuwac z tablicy NAT), SNAT szuka czy port juz byl uprzednio zaalokowany

This commit is contained in:
2017-01-11 21:50:12 +01:00
parent d176521abe
commit e45685b4f1
5 changed files with 40 additions and 13 deletions

View File

@@ -57,3 +57,8 @@ void NATItem::free()
{ {
this->setTimeout(0); this->setTimeout(0);
} }
void NATItem::increaseTimeout(int timeout)
{
this->setTimeout(this->getTimeout() + timeout);
}

View File

@@ -20,6 +20,7 @@ public:
NATItem(); NATItem();
bool isFree(); bool isFree();
void increaseTimeout(int timeout);
const string& getIp() const; const string& getIp() const;
void setIp(const string& ip); void setIp(const string& ip);

View File

@@ -1,7 +1,7 @@
/* /*
* NATItem.cpp * NATItem.cpp
* *
* Created on: 10-01-2017 * Created on: 11-01-2017
* Author: Piotr Dergun * Author: Piotr Dergun
*/ */
@@ -121,12 +121,15 @@ void NATRouter::sNAT(Packet *packet)
int port; int port;
NATItem *natItem; NATItem *natItem;
while ((port = this->getFreePort()) == -1) // mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
{ port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort());
//TESTOWO if (port == -1)
cout << "Brak wolnych portow, czekam 1s" << endl; while ((port = this->getFreePort()) == -1)
sleep(1); {
} //TESTOWO
cout << "Brak wolnych portow, czekam 1s" << endl;
sleep(1);
}
// 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];
@@ -145,8 +148,8 @@ void NATRouter::sNAT(Packet *packet)
/* /*
* funkcja DNAT ma za zadanie znalezc w tablicy NAT * funkcja DNAT ma za zadanie znalezc w tablicy NAT
* port i adres IP wezla na ktory ma wrocic odpowiedz * port i adres IP wezla na ktory ma wrocic odpowiedz
* z sieci Internet (z zewnatrz), a takze zwolnic ten * z sieci Internet (z zewnatrz), a takze przedluzyc
* port * czas zycia tego portu
*/ */
void NATRouter::dNAT(Packet* packet) void NATRouter::dNAT(Packet* packet)
{ {
@@ -154,7 +157,8 @@ void NATRouter::dNAT(Packet* packet)
packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe
packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla
natItem->free(); // zwalniam port w routerze //natItem->free(); // zwalniam port w routerze
natItem->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa
this->send(*packet, true); // wysylam dalej this->send(*packet, true); // wysylam dalej
@@ -171,6 +175,19 @@ string NATRouter::getWanNetwork()
return this->netWanConf.network; return this->netWanConf.network;
} }
/*
* funkcja szuka aktywnego zaalokowanego portu (aktywna
* transmisja) w tablicy NAT dla pary (srcIp:srcPort)
*/
int NATRouter::getAllocatedPort(string srcIp, int srcPort)
{
for (int i=1; i<65536; ++i)
if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort)
return i;
return -1; // nic nie znaleziono
}
string NATRouter::getWanGatewayIp() string NATRouter::getWanGatewayIp()
{ {
return this->netWanConf.gatewayIp; return this->netWanConf.gatewayIp;

View File

@@ -18,6 +18,7 @@ class NATRouter : public Node
NetConf netWanConf; // konfiguracja sieciowa na zewnątrz NetConf netWanConf; // konfiguracja sieciowa na zewnątrz
void initalizeNatTable(); void initalizeNatTable();
int getFreePort(); int getFreePort();
int getAllocatedPort(string srcIp, int srcPort);
void sNAT(Packet *packet); void sNAT(Packet *packet);
void dNAT(Packet *packet); void dNAT(Packet *packet);
void setWanNetwork(); void setWanNetwork();

View File

@@ -155,9 +155,12 @@ bool Node::send(Packet packet, bool isRouter)
if (!isRouter) if (!isRouter)
{ {
packet.setSrcIp(this->getIp()); packet.setSrcIp(this->getIp());
// ustaw port zrodlowy na port losowy z zakresu [32768,61000] - zob. empheral port (port emferyczny) if (packet.getSrcPort() == 0)
srand(time(NULL)); {
packet.setSrcPort(rand() % 32768 + 28233); // ustaw port zrodlowy na port losowy z zakresu [32768,61000] - zob. empheral port (port emferyczny)
srand(time(NULL));
packet.setSrcPort(rand() % 32768 + 28233);
}
} }
node->putPacket(packet); node->putPacket(packet);