Browse Source

metoda watku pokazujaca ile jest wolnych i zajetych portow

master
Piotr Dergun 7 years ago
parent
commit
98f610e80a
5 changed files with 43 additions and 6 deletions
  1. +3
    -3
      Log.h
  2. +30
    -3
      NATRouter.cpp
  3. +5
    -0
      NATRouter.h
  4. +4
    -0
      Simulation.cpp
  5. +1
    -0
      Simulation.h

+ 3
- 3
Log.h View File

@ -29,9 +29,7 @@ class Log
string *objectName; string *objectName;
pthread_mutex_t *writeMutex; pthread_mutex_t *writeMutex;
int delayVal; int delayVal;
protected:
string* getObjectName() const;
void setObjectName(string* objectName);
public: public:
Log(); Log();
void print(string msg); void print(string msg);
@ -47,6 +45,8 @@ public:
void delay(int optional=-1); void delay(int optional=-1);
int getDelay() const; int getDelay() const;
void setDelay(int delay); void setDelay(int delay);
string* getObjectName() const;
void setObjectName(string* objectName);
}; };
#endif /* LOG_H_ */ #endif /* LOG_H_ */

+ 30
- 3
NATRouter.cpp View File

@ -11,7 +11,7 @@ void NATRouter::initalizeNatTable()
{ {
try try
{ {
natTable = new NATItem[65536];
natTable = new NATItem[NAT_TABLE_LEN];
} }
catch (bad_alloc &ba) catch (bad_alloc &ba)
{ {
@ -42,7 +42,7 @@ NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp)
int NATRouter::getFreePort() int NATRouter::getFreePort()
{ {
for (int i=1; i<65536; ++i)
for (int i=1; i<NAT_TABLE_LEN; ++i)
if (this->natTable[i].isFree()) if (this->natTable[i].isFree())
return i; return i;
@ -209,13 +209,40 @@ string NATRouter::getWanNetwork()
*/ */
int NATRouter::getAllocatedPort(string srcIp, int srcPort) int NATRouter::getAllocatedPort(string srcIp, int srcPort)
{ {
for (int i=1; i<65536; ++i)
for (int i=1; i<NAT_TABLE_LEN; ++i)
if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort) if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort)
return i; return i;
return -1; // nic nie znaleziono 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; i<NAT_TABLE_LEN; ++i)
if (this->natTable[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() string NATRouter::getWanGatewayIp()
{ {
return this->netWanConf.gatewayIp; return this->netWanConf.gatewayIp;

+ 5
- 0
NATRouter.h View File

@ -29,7 +29,9 @@ public:
NATRouter(string hostname, string ip, string mask); NATRouter(string hostname, string ip, string mask);
NATRouter(string hostname, string ip, string mask, string gatewayIp); NATRouter(string hostname, string ip, string mask, string gatewayIp);
~NATRouter(); ~NATRouter();
virtual void onRecv(); virtual void onRecv();
void freePorts();
// settery // settery
void setWanIp(string wanIp); void setWanIp(string wanIp);
@ -42,4 +44,7 @@ public:
string getWanNetwork(); string getWanNetwork();
string getWanGatewayIp(); string getWanGatewayIp();
}; };
#define NAT_TABLE_LEN 65536
#endif /* NATROUTER_H_ */ #endif /* NATROUTER_H_ */

+ 4
- 0
Simulation.cpp View File

@ -129,6 +129,9 @@ void * Simulation::threadWrapper(void * context)
case SIM_RESIZE: case SIM_RESIZE:
((Simulation *)params->context)->resizeWnd(); ((Simulation *)params->context)->resizeWnd();
break; break;
case NAT_FP:
((NATRouter *)params->context)->freePorts();
break;
} }
return 0; return 0;
} }
@ -239,6 +242,7 @@ void Simulation::p2pSimulation()
sleep(3); sleep(3);
this->createThread("p1", NODE_RECV, &seed); this->createThread("p1", NODE_RECV, &seed);
this->createThread("p2", NODE_RECV, &peer); this->createThread("p2", NODE_RECV, &peer);
this->createThread("nat", NAT_FP, &r1);
while(true); while(true);
} }

+ 1
- 0
Simulation.h View File

@ -16,6 +16,7 @@ enum THREAD_TYPE
NODE_RECV, NODE_RECV,
SIM_TIMER, SIM_TIMER,
SIM_RESIZE, SIM_RESIZE,
NAT_FP,
}; };
struct threadParams struct threadParams

Loading…
Cancel
Save