Browse Source

jakas podstawa przeciazenia NAT

master
Piotr Dergun 7 years ago
parent
commit
d1afb8cde2
6 changed files with 91 additions and 8 deletions
  1. +4
    -1
      Log.cpp
  2. +1
    -1
      Log.h
  3. +1
    -1
      Main.cpp
  4. +5
    -2
      NATRouter.cpp
  5. +3
    -1
      Node.cpp
  6. +77
    -2
      Simulation.cpp

+ 4
- 1
Log.cpp View File

@ -58,12 +58,15 @@ void Log::setObjectName(string* objectName)
this->objectName = objectName;
}
void Log::print(string msg)
void Log::print(string msg, bool force)
{
#ifndef DEBUG
if (this->getObjectName() == NULL)
return;
if (this->getDelay() == 0 && !force)
return;
pthread_mutex_lock(this->writeMutex);
move(this->getLineNumber(), 0);
clrtoeol();

+ 1
- 1
Log.h View File

@ -32,7 +32,7 @@ class Log
public:
Log();
void print(string msg);
void print(string msg, bool force=false);
LOG_COLOR getColor() const;
void setColor(LOG_COLOR color);

+ 1
- 1
Main.cpp View File

@ -47,7 +47,7 @@ int main(int argc, char *argv[])
}
Simulation sim;
sim.createThread("timer", SIM_TIMER, &sim);
//sim.createThread("timer", SIM_TIMER, &sim);
sim.natOverflowSimulation(nodesCount);
}
else if (!strcmp(argv[1], "-v"))

+ 5
- 2
NATRouter.cpp View File

@ -219,7 +219,7 @@ int NATRouter::getAllocatedPort(string srcIp, int srcPort)
void NATRouter::freePorts()
{
stringstream ss;
int i, ports;
int i, ports, peak=NAT_TABLE_LEN-1;
Log portsLog;
string name = "NAT";
@ -235,7 +235,10 @@ void NATRouter::freePorts()
if (this->natTable[i].isFree())
++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.delay();

+ 3
- 1
Node.cpp View File

@ -173,7 +173,6 @@ int Node::send(Packet packet, bool isRouter)
if (packet.getSrcPort() == 0)
{
// ustaw port zrodlowy na port losowy z zakresu [32768,61000] - zob. empheral port (port emferyczny)
packet.setSrcPort(rand() % 32768 + 28233);
}
}
@ -239,6 +238,9 @@ string Node::calculateNetwork(string ip, string mask)
return ipToString(stringToIp(ip) & stringToIp(mask));
}
/*
* domyslnie onRecv w watku sciaga pakiet z kolejki (aby nie zjadalo pamieci RAM)
*/
void Node::onRecv()
{
while(true)

+ 77
- 2
Simulation.cpp View File

@ -249,9 +249,84 @@ void Simulation::p2pSimulation()
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];
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);
}
}

Loading…
Cancel
Save