From 618b504aa1ae964e4e25082731302b402e26a866 Mon Sep 17 00:00:00 2001 From: PioDer Date: Thu, 19 Jan 2017 04:59:38 +0100 Subject: [PATCH] przeciazenie natu wzglednie dziala --- Log.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++- Log.h | 1 + Main.cpp | 2 +- NATRouter.cpp | 32 +++++++++++++++-------- NATRouter.h | 1 + Simulation.cpp | 42 +++++++++++++++--------------- 6 files changed, 116 insertions(+), 32 deletions(-) diff --git a/Log.cpp b/Log.cpp index 919733a..b1d1436 100644 --- a/Log.cpp +++ b/Log.cpp @@ -53,6 +53,74 @@ string* Log::getObjectName() const return objectName; } +void Log::printProgressBar(int lineNumber, int offset, string msg, + float percent) +{ + int cols, rows, bar_width, pools; + stringstream ss; +#ifndef DEBUG + if (this->getObjectName() == NULL) + return; + + if (this->getDelay() == 0) + return; + + getmaxyx(stdscr, rows, cols); + + pthread_mutex_lock(this->writeMutex); + move(lineNumber, 0); + clrtoeol(); + ss << msg << ":" << (int) (percent * 100) << "%"; + bar_width = cols - ss.str().size() - offset; + pools = (int) (percent * bar_width); + + if (has_colors()) + attron(COLOR_PAIR(this->getColor())); + printw("%s:", msg.c_str()); + if (has_colors()) + attroff(COLOR_PAIR(this->getColor())); + + for (int i = 0; i < offset; ++i) + printw(" "); + + if (has_colors()) + { + if (percent < 0.66) + attron(COLOR_PAIR(GREEN)); + else if (percent < 0.90) + attron(COLOR_PAIR(YELLOW)); + else + attron(COLOR_PAIR(RED)); + } + attron(A_BOLD); + + for (int i = 0; i < pools; ++i) + printw("#"); + ss.str(""); + ss << " " << (int) (percent * 100) << "%"; + move(lineNumber, cols - ss.str().size()); + printw(" %d%%", (int) (percent * 100)); + + attroff(A_BOLD); + + if (has_colors()) + { + if (percent < 0.66) + attroff(COLOR_PAIR(GREEN)); + else if (percent < 0.90) + attroff(COLOR_PAIR(YELLOW)); + else + attroff(COLOR_PAIR(RED)); + } + + move(0, 0); + pthread_mutex_unlock(this->writeMutex); + +#else + cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl; +#endif +} + void Log::setObjectName(string* objectName) { this->objectName = objectName; @@ -74,8 +142,8 @@ void Log::print(string msg, bool force) if(has_colors()) attron(COLOR_PAIR(this->getColor())); - //printw("%s:%s%s", this->getObjectName()->c_str(), this->getDelimiter().c_str(), msg.c_str()); printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str()); + if(has_colors()) attron(A_BOLD); printw("%s", msg.c_str()); attroff(A_BOLD); diff --git a/Log.h b/Log.h index 8e86969..5b9f85a 100644 --- a/Log.h +++ b/Log.h @@ -33,6 +33,7 @@ class Log public: Log(); void print(string msg, bool force=false); + void printProgressBar(int lineNumber, int offset, string msg, float percent); LOG_COLOR getColor() const; void setColor(LOG_COLOR color); diff --git a/Main.cpp b/Main.cpp index 76cae29..4e85361 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 0c6892d..4909652 100644 --- a/NATRouter.cpp +++ b/NATRouter.cpp @@ -42,10 +42,23 @@ NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp) int NATRouter::getFreePort() { + if (this->lastUsedPort == NAT_TABLE_LEN-1) + this->lastUsedPort = 0; + + if (this->natTable[this->lastUsedPort+1].isFree()) + return ++this->lastUsedPort; + + for (int i = 1; ilastUsedPort; ++i) + if (this->natTable[i].isFree()) + { + this->lastUsedPort = i; + return i; + } +/* for (int i=1; inatTable[i].isFree()) return i; - +*/ return -1; // nie ma żadnego wolnego portu } @@ -62,10 +75,6 @@ void NATRouter::onRecv() Packet p = this->recv(); stringstream s; - /*if (this->getHostname() == "Router 2") { - s << "src: " << p.getSrcIp() << ":" << p.getSrcPort() << "dst: " << p.getDstIp() << ":" << p.getDstPort(); - this->print(s.str()); - }*/ if (p.getSrcPort() != 0) { // jezeli pakiet idzie z jednego wezla do drugiego w danej possieci to odeslij mu "jak switch" @@ -141,12 +150,13 @@ void NATRouter::sNAT(Packet *packet) NATItem *natItem; // mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac + // TODO getAllocatedPort - STRASZNIE zmula caly symulator port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort()); if (port == -1) while ((port = this->getFreePort()) == -1) { //TESTOWO - this->print("NAT table full, waiting 1s"); + this->print("NAT table full, waiting 1s", true); this->delay(1); } @@ -154,7 +164,7 @@ void NATRouter::sNAT(Packet *packet) natItem = &this->natTable[port]; natItem->setIp(packet->getSrcIp()); natItem->setPort(packet->getSrcPort()); - natItem->setTimeout(/*5*/15); + natItem->setTimeout(15); stringstream ss; ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port; @@ -162,7 +172,7 @@ void NATRouter::sNAT(Packet *packet) packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT - this->print(ss.str()); + this->print(ss.str(), true); this->delay(); this->send(*packet, true); // wysylam dalej natItem = NULL; @@ -223,8 +233,9 @@ void NATRouter::freePorts() Log portsLog; string name = "NAT"; - portsLog.setLogParams(10, BLUE, "\t\t"); + portsLog.setLogParams(3, GREEN, "\t\t"); portsLog.setObjectName(&name); + portsLog.setDelay(100000); while(true) { @@ -238,9 +249,10 @@ void NATRouter::freePorts() if (ports < peak) peak = ports; - ss << "Free:\t" << ports << " (" << peak << ")\t\tReserved:\t" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0 + ss << "Free/Reserved:\t" << ports << " (" << peak << ")/" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0 portsLog.print(ss.str()); + portsLog.printProgressBar(4, 4, "Utilization", (float)(NAT_TABLE_LEN-ports-1)/(NAT_TABLE_LEN-1)); portsLog.delay(); } diff --git a/NATRouter.h b/NATRouter.h index 1865869..d551c00 100644 --- a/NATRouter.h +++ b/NATRouter.h @@ -18,6 +18,7 @@ class NATRouter : public Node NetConf netWanConf; // konfiguracja sieciowa na zewnątrz void initalizeNatTable(); int getFreePort(); + int lastUsedPort = 0; int getAllocatedPort(string srcIp, int srcPort); void sNAT(Packet *packet); void dNAT(Packet *packet); diff --git a/Simulation.cpp b/Simulation.cpp index 5f9fd3e..f6ab76d 100644 --- a/Simulation.cpp +++ b/Simulation.cpp @@ -155,6 +155,7 @@ void Simulation::timer() char str[10]; int h,m, s; unsigned long timeElapsed; + pthread_mutex_t *mutex; #endif while(true) { @@ -164,10 +165,12 @@ void Simulation::timer() h = timeElapsed / 3600; m = (timeElapsed % 3600) / 60; s = (timeElapsed % 3600) % 60; - //refresh(); + mutex = Log::getMutex(); + pthread_mutex_lock(mutex); sprintf(str, "%02d:%02d:%02d", h, m, s); move(0, this->cols-strlen(str)); printw(str); + pthread_mutex_unlock(mutex); #endif usleep(100000); // 100 ms //sleep(1); @@ -242,16 +245,21 @@ void Simulation::p2pSimulation() sleep(3); this->createThread("p1", NODE_RECV, &seed); this->createThread("p2", NODE_RECV, &peer); - this->createThread("nat", NAT_FP, &r1); while(true); } void Simulation::natOverflowSimulation(int nNodes) { - int i = 0, srcPort; + int i = 0;//, srcPort; stringstream ss; + this->print("Creating server"); + Node server("Server", "93.92.91.90", "255.255.255.255"); + server.setLogParams(0, BLUE, "\t\t"); + server.print("IP 93.92.91.90/32"); + sleep(1); + this->print("Creating NAT device"); NATRouter router("Router", "10.13.12.254", "255.255.255.0"); router.setLogParams(1, YELLOW, "\t\t"); @@ -265,19 +273,13 @@ void Simulation::natOverflowSimulation(int nNodes) 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"); + currentSrcPort[i] = 1; + client[i].setLogParams(2, CYAN, "\t"); ss.str(""); ss << "Client " << (i + 1); client[i].setHostname(ss.str()); @@ -315,18 +317,18 @@ void Simulation::natOverflowSimulation(int nNodes) 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); + i = (nNodes>1) ? rand()%nNodes : 0; + if (currentSrcPort[i] == 65535) + currentSrcPort[i] = 1; + + p.setSrcPort(currentSrcPort[i]++); + + client[i].send(p); + + this->delay(150); //usleep(1); - nanosleep(&ts, &ts2); } }