jakas podstawa przeciazenia NAT
This commit is contained in:
5
Log.cpp
5
Log.cpp
@@ -58,12 +58,15 @@ void Log::setObjectName(string* objectName)
|
|||||||
this->objectName = objectName;
|
this->objectName = objectName;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::print(string msg)
|
void Log::print(string msg, bool force)
|
||||||
{
|
{
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
if (this->getObjectName() == NULL)
|
if (this->getObjectName() == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (this->getDelay() == 0 && !force)
|
||||||
|
return;
|
||||||
|
|
||||||
pthread_mutex_lock(this->writeMutex);
|
pthread_mutex_lock(this->writeMutex);
|
||||||
move(this->getLineNumber(), 0);
|
move(this->getLineNumber(), 0);
|
||||||
clrtoeol();
|
clrtoeol();
|
||||||
|
|||||||
2
Log.h
2
Log.h
@@ -32,7 +32,7 @@ class Log
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Log();
|
Log();
|
||||||
void print(string msg);
|
void print(string msg, bool force=false);
|
||||||
|
|
||||||
LOG_COLOR getColor() const;
|
LOG_COLOR getColor() const;
|
||||||
void setColor(LOG_COLOR color);
|
void setColor(LOG_COLOR color);
|
||||||
|
|||||||
2
Main.cpp
2
Main.cpp
@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
Simulation sim;
|
Simulation sim;
|
||||||
sim.createThread("timer", SIM_TIMER, &sim);
|
//sim.createThread("timer", SIM_TIMER, &sim);
|
||||||
sim.natOverflowSimulation(nodesCount);
|
sim.natOverflowSimulation(nodesCount);
|
||||||
}
|
}
|
||||||
else if (!strcmp(argv[1], "-v"))
|
else if (!strcmp(argv[1], "-v"))
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ int NATRouter::getAllocatedPort(string srcIp, int srcPort)
|
|||||||
void NATRouter::freePorts()
|
void NATRouter::freePorts()
|
||||||
{
|
{
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
int i, ports;
|
int i, ports, peak=NAT_TABLE_LEN-1;
|
||||||
|
|
||||||
Log portsLog;
|
Log portsLog;
|
||||||
string name = "NAT";
|
string name = "NAT";
|
||||||
@@ -235,7 +235,10 @@ void NATRouter::freePorts()
|
|||||||
if (this->natTable[i].isFree())
|
if (this->natTable[i].isFree())
|
||||||
++ports;
|
++ports;
|
||||||
|
|
||||||
ss << "Free:\t" << ports << "\t\tReserved:\t" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0
|
if (ports < peak)
|
||||||
|
peak = ports;
|
||||||
|
|
||||||
|
ss << "Free:\t" << ports << " (" << peak << ")\t\tReserved:\t" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0
|
||||||
|
|
||||||
portsLog.print(ss.str());
|
portsLog.print(ss.str());
|
||||||
portsLog.delay();
|
portsLog.delay();
|
||||||
|
|||||||
4
Node.cpp
4
Node.cpp
@@ -173,7 +173,6 @@ int Node::send(Packet packet, bool isRouter)
|
|||||||
if (packet.getSrcPort() == 0)
|
if (packet.getSrcPort() == 0)
|
||||||
{
|
{
|
||||||
// ustaw port zrodlowy na port losowy z zakresu [32768,61000] - zob. empheral port (port emferyczny)
|
// ustaw port zrodlowy na port losowy z zakresu [32768,61000] - zob. empheral port (port emferyczny)
|
||||||
|
|
||||||
packet.setSrcPort(rand() % 32768 + 28233);
|
packet.setSrcPort(rand() % 32768 + 28233);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -239,6 +238,9 @@ string Node::calculateNetwork(string ip, string mask)
|
|||||||
return ipToString(stringToIp(ip) & stringToIp(mask));
|
return ipToString(stringToIp(ip) & stringToIp(mask));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* domyslnie onRecv w watku sciaga pakiet z kolejki (aby nie zjadalo pamieci RAM)
|
||||||
|
*/
|
||||||
void Node::onRecv()
|
void Node::onRecv()
|
||||||
{
|
{
|
||||||
while(true)
|
while(true)
|
||||||
|
|||||||
@@ -249,9 +249,84 @@ void Simulation::p2pSimulation()
|
|||||||
|
|
||||||
void Simulation::natOverflowSimulation(int nNodes)
|
void Simulation::natOverflowSimulation(int nNodes)
|
||||||
{
|
{
|
||||||
int i=0;
|
int i = 0, srcPort;
|
||||||
|
stringstream ss;
|
||||||
|
|
||||||
|
this->print("Creating NAT device");
|
||||||
|
NATRouter router("Router", "10.13.12.254", "255.255.255.0");
|
||||||
|
router.setLogParams(1, YELLOW, "\t\t");
|
||||||
|
router.setDelay(0);
|
||||||
|
router.print("IP 10.13.12.254/24", true);
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
this->print("Setting WAN configuration");
|
||||||
|
router.setWanIp("80.55.33.12");
|
||||||
|
router.setWanMask("255.255.255.255");
|
||||||
|
router.print("WAN IP 80.55.33.12/32", true);
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
this->print("Creating server");
|
||||||
|
Node server("Server", "93.92.91.90", "255.255.255.255");
|
||||||
|
server.setLogParams(2, BLUE, "\t\t");
|
||||||
|
server.print("IP 93.92.91.90/32");
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
this->print("Creating and setting clients");
|
||||||
Node *client = new Node[nNodes];
|
Node *client = new Node[nNodes];
|
||||||
|
int *currentSrcPort = new int[nNodes];
|
||||||
|
for (i = 0; i < nNodes; ++i)
|
||||||
|
{
|
||||||
|
currentSrcPort[i] = 0;
|
||||||
|
client[i].setLogParams(0, CYAN, "\t");
|
||||||
|
ss.str("");
|
||||||
|
ss << "Client " << (i + 1);
|
||||||
|
client[i].setHostname(ss.str());
|
||||||
|
ss.str("");
|
||||||
|
ss << "10.13.12." << (i + 1);
|
||||||
|
client[i].setIp(ss.str());
|
||||||
|
client[i].setMask("255.255.255.0");
|
||||||
|
client[i].setGatewayIp("10.13.12.254");
|
||||||
|
ss.str("");
|
||||||
|
ss << "IP 10.13.12." << (i + 1) << "/24, Gateway: 10.0.0.1";
|
||||||
|
client[i].print(ss.str());
|
||||||
|
usleep(12500);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < nNodes; ++i)
|
||||||
|
{
|
||||||
|
ss.str("");
|
||||||
|
ss << "Plugging Client " << (i + 1) << " <---> Router";
|
||||||
|
this->print(ss.str());
|
||||||
|
router.connectNode(&client[i]);
|
||||||
|
usleep(12500);
|
||||||
|
}
|
||||||
|
|
||||||
while(true);
|
this->print("Plugging Server <---> Router");
|
||||||
|
server.connectNode(&router);
|
||||||
|
usleep(600000);
|
||||||
|
|
||||||
|
this->print("Starting simulation...");
|
||||||
|
sleep(2);
|
||||||
|
this->createThread("router", NODE_RECV, &router);
|
||||||
|
this->createThread("server", NODE_RECV, &server);
|
||||||
|
this->createThread("nat", NAT_FP, &router);
|
||||||
|
|
||||||
|
Packet p("test packet");
|
||||||
|
p.setDstIp("93.92.91.90");
|
||||||
|
p.setDstPort(80);
|
||||||
|
i=0;
|
||||||
|
struct timespec ts, ts2;
|
||||||
|
ts.tv_sec = 0;
|
||||||
|
ts.tv_nsec = 100;
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
if (i==65535)
|
||||||
|
i=1;
|
||||||
|
p.setSrcPort(i++);
|
||||||
|
client[0].send(p);
|
||||||
|
//this->delay(10);
|
||||||
|
//usleep(1);
|
||||||
|
nanosleep(&ts, &ts2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user