|
@ -1,8 +1,8 @@ |
|
|
/*
|
|
|
|
|
|
* NATItem.cpp |
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @file NATRouter.cpp |
|
|
* |
|
|
* |
|
|
* Created on: 11-01-2017 |
|
|
|
|
|
* Author: Piotr Dergun |
|
|
|
|
|
|
|
|
* Created on: 11-01-2017 |
|
|
|
|
|
* @author Piotr Dergun |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
#include "NATRouter.h"
|
|
|
#include "NATRouter.h"
|
|
@ -22,21 +22,25 @@ void NATRouter::initalizeNatTable() |
|
|
|
|
|
|
|
|
NATRouter::NATRouter() |
|
|
NATRouter::NATRouter() |
|
|
{ |
|
|
{ |
|
|
|
|
|
this->lastUsedPort = 0; |
|
|
this->initalizeNatTable(); |
|
|
this->initalizeNatTable(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
NATRouter::NATRouter(string hostname) : Node(hostname) |
|
|
NATRouter::NATRouter(string hostname) : Node(hostname) |
|
|
{ |
|
|
{ |
|
|
|
|
|
this->lastUsedPort = 0; |
|
|
this->initalizeNatTable(); |
|
|
this->initalizeNatTable(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
NATRouter::NATRouter(string hostname, string ip, string mask) : Node(hostname, ip, mask) |
|
|
NATRouter::NATRouter(string hostname, string ip, string mask) : Node(hostname, ip, mask) |
|
|
{ |
|
|
{ |
|
|
|
|
|
this->lastUsedPort = 0; |
|
|
this->initalizeNatTable(); |
|
|
this->initalizeNatTable(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp) : Node(hostname, ip, mask, gatewayIp) |
|
|
NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp) : Node(hostname, ip, mask, gatewayIp) |
|
|
{ |
|
|
{ |
|
|
|
|
|
this->lastUsedPort = 0; |
|
|
this->initalizeNatTable(); |
|
|
this->initalizeNatTable(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -45,8 +49,6 @@ 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;
|
|
|
|
|
|
for (int i = this->lastUsedPort+1; i<NAT_TABLE_LEN; ++i) |
|
|
for (int i = this->lastUsedPort+1; i<NAT_TABLE_LEN; ++i) |
|
|
if (this->natTable[i].isFree()) |
|
|
if (this->natTable[i].isFree()) |
|
|
{ |
|
|
{ |
|
@ -60,11 +62,7 @@ int NATRouter::getFreePort() |
|
|
this->lastUsedPort = i; |
|
|
this->lastUsedPort = i; |
|
|
return i; |
|
|
return i; |
|
|
} |
|
|
} |
|
|
/*
|
|
|
|
|
|
for (int i=1; i<NAT_TABLE_LEN; ++i) |
|
|
|
|
|
if (this->natTable[i].isFree()) |
|
|
|
|
|
return i; |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
return -1; // nie ma żadnego wolnego portu
|
|
|
return -1; // nie ma żadnego wolnego portu
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -142,14 +140,6 @@ string NATRouter::getWanMask() |
|
|
return this->netWanConf.mask; |
|
|
return this->netWanConf.mask; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* funkcja SNAT zapisuje w tablicy NAT |
|
|
|
|
|
* numer portu zrodlowego komputera z LAN |
|
|
|
|
|
* oraz jego IP i przeksztalca pakiet, zmieniajac |
|
|
|
|
|
* zrodlowy IP na IP zewn. routera oraz wolny |
|
|
|
|
|
* port z tablicy NAT. Jezeli portu nie ma to funkcja |
|
|
|
|
|
* czeka, az sie zwolni |
|
|
|
|
|
*/ |
|
|
|
|
|
void NATRouter::sNAT(Packet *packet) |
|
|
void NATRouter::sNAT(Packet *packet) |
|
|
{ |
|
|
{ |
|
|
int port; |
|
|
int port; |
|
@ -162,7 +152,6 @@ void NATRouter::sNAT(Packet *packet) |
|
|
if (port == -1) |
|
|
if (port == -1) |
|
|
while ((port = this->getFreePort()) == -1) |
|
|
while ((port = this->getFreePort()) == -1) |
|
|
{ |
|
|
{ |
|
|
//TESTOWO
|
|
|
|
|
|
this->print("NAT table full, waiting 1s", true); |
|
|
this->print("NAT table full, waiting 1s", true); |
|
|
this->delay(1); |
|
|
this->delay(1); |
|
|
} |
|
|
} |
|
@ -196,12 +185,6 @@ void NATRouter::sNAT(Packet *packet) |
|
|
natItem = NULL; |
|
|
natItem = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* funkcja DNAT ma za zadanie znalezc w tablicy NAT |
|
|
|
|
|
* port i adres IP wezla na ktory ma wrocic odpowiedz |
|
|
|
|
|
* z sieci Internet (z zewnatrz), a takze przedluzyc |
|
|
|
|
|
* czas zycia tego portu |
|
|
|
|
|
*/ |
|
|
|
|
|
void NATRouter::dNAT(Packet* packet) |
|
|
void NATRouter::dNAT(Packet* packet) |
|
|
{ |
|
|
{ |
|
|
NATItem *natItem = &this->natTable[packet->getDstPort()]; |
|
|
NATItem *natItem = &this->natTable[packet->getDstPort()]; |
|
@ -211,7 +194,6 @@ 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->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa
|
|
|
natItem->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa
|
|
|
|
|
|
|
|
|
this->print(ss.str()); |
|
|
this->print(ss.str()); |
|
@ -231,18 +213,11 @@ 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) |
|
|
int NATRouter::getAllocatedPort(string srcIp, int srcPort) |
|
|
{ |
|
|
{ |
|
|
map<string, int>::iterator it; |
|
|
map<string, int>::iterator it; |
|
|
int pos = -1; |
|
|
int pos = -1; |
|
|
/*for (int i=1; i<NAT_TABLE_LEN; ++i)
|
|
|
|
|
|
if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort) |
|
|
|
|
|
return i; |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
stringstream ss; |
|
|
stringstream ss; |
|
|
ss << srcIp << ":" << srcPort; |
|
|
ss << srcIp << ":" << srcPort; |
|
|
|
|
|
|
|
|