diff --git a/Log.h b/Log.h index fa4c160..785e3da 100644 --- a/Log.h +++ b/Log.h @@ -29,9 +29,7 @@ class Log string *objectName; pthread_mutex_t *writeMutex; int delayVal; -protected: - string* getObjectName() const; - void setObjectName(string* objectName); + public: Log(); void print(string msg); @@ -47,6 +45,8 @@ public: void delay(int optional=-1); int getDelay() const; void setDelay(int delay); + string* getObjectName() const; + void setObjectName(string* objectName); }; #endif /* LOG_H_ */ diff --git a/NATRouter.cpp b/NATRouter.cpp index 2eb5927..fac0214 100644 --- a/NATRouter.cpp +++ b/NATRouter.cpp @@ -11,7 +11,7 @@ void NATRouter::initalizeNatTable() { try { - natTable = new NATItem[65536]; + natTable = new NATItem[NAT_TABLE_LEN]; } catch (bad_alloc &ba) { @@ -42,7 +42,7 @@ NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp) int NATRouter::getFreePort() { - for (int i=1; i<65536; ++i) + for (int i=1; inatTable[i].isFree()) return i; @@ -209,13 +209,40 @@ string NATRouter::getWanNetwork() */ int NATRouter::getAllocatedPort(string srcIp, int srcPort) { - for (int i=1; i<65536; ++i) + for (int i=1; inatTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort) return i; return -1; // nic nie znaleziono } +void NATRouter::freePorts() +{ + stringstream ss; + int i, ports; + + Log portsLog; + string name = "NAT"; + portsLog.setLogParams(10, BLUE, "\t\t"); + portsLog.setObjectName(&name); + + while(true) + { + ports = 0; + ss.str(""); + + for (i=1; inatTable[i].isFree()) + ++ports; + + ss << "Free:\t" << ports << "\t\tReserved:\t" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0 + + portsLog.print(ss.str()); + portsLog.delay(); + } + +} + string NATRouter::getWanGatewayIp() { return this->netWanConf.gatewayIp; diff --git a/NATRouter.h b/NATRouter.h index 6f02544..1865869 100644 --- a/NATRouter.h +++ b/NATRouter.h @@ -29,7 +29,9 @@ public: NATRouter(string hostname, string ip, string mask); NATRouter(string hostname, string ip, string mask, string gatewayIp); ~NATRouter(); + virtual void onRecv(); + void freePorts(); // settery void setWanIp(string wanIp); @@ -42,4 +44,7 @@ public: string getWanNetwork(); string getWanGatewayIp(); }; + +#define NAT_TABLE_LEN 65536 + #endif /* NATROUTER_H_ */ diff --git a/Simulation.cpp b/Simulation.cpp index def2726..a70c43c 100644 --- a/Simulation.cpp +++ b/Simulation.cpp @@ -129,6 +129,9 @@ void * Simulation::threadWrapper(void * context) case SIM_RESIZE: ((Simulation *)params->context)->resizeWnd(); break; + case NAT_FP: + ((NATRouter *)params->context)->freePorts(); + break; } return 0; } @@ -239,6 +242,7 @@ 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); } diff --git a/Simulation.h b/Simulation.h index 142e18a..df050b3 100644 --- a/Simulation.h +++ b/Simulation.h @@ -16,6 +16,7 @@ enum THREAD_TYPE NODE_RECV, SIM_TIMER, SIM_RESIZE, + NAT_FP, }; struct threadParams