|
@ -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) |
|
|
|
|
|
{ |
|
|
|
|
|
//TESTOWO
|
|
|
|
|
|
cout << "Brak wolnych portow, czekam 1s" << endl; |
|
|
|
|
|
sleep(1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
|
|
|
|
|
|
port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort()); |
|
|
|
|
|
if (port == -1) |
|
|
|
|
|
while ((port = this->getFreePort()) == -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 |
|
|
|
|
|
* port |
|
|
|
|
|
|
|
|
* z sieci Internet (z zewnatrz), a takze przedluzyc |
|
|
|
|
|
* 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; |
|
|