diff --git a/Log.cpp b/Log.cpp index eb5267f..919733a 100644 --- a/Log.cpp +++ b/Log.cpp @@ -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(); diff --git a/Log.h b/Log.h index 785e3da..8e86969 100644 --- a/Log.h +++ b/Log.h @@ -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); diff --git a/Main.cpp b/Main.cpp index 4e85361..76cae29 100644 --- a/Main.cpp +++ b/Main.cpp @@ -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")) diff --git a/NATRouter.cpp b/NATRouter.cpp index fac0214..0c6892d 100644 --- a/NATRouter.cpp +++ b/NATRouter.cpp @@ -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(); diff --git a/Node.cpp b/Node.cpp index 3508ac6..29cc13b 100644 --- a/Node.cpp +++ b/Node.cpp @@ -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) diff --git a/Simulation.cpp b/Simulation.cpp index a70c43c..5f9fd3e 100644 --- a/Simulation.cpp +++ b/Simulation.cpp @@ -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); + } }