przeciazenie natu wzglednie dziala
This commit is contained in:
70
Log.cpp
70
Log.cpp
@@ -53,6 +53,74 @@ string* Log::getObjectName() const
|
|||||||
return objectName;
|
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)
|
void Log::setObjectName(string* objectName)
|
||||||
{
|
{
|
||||||
this->objectName = objectName;
|
this->objectName = objectName;
|
||||||
@@ -74,8 +142,8 @@ void Log::print(string msg, bool force)
|
|||||||
if(has_colors())
|
if(has_colors())
|
||||||
attron(COLOR_PAIR(this->getColor()));
|
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());
|
printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str());
|
||||||
|
if(has_colors())
|
||||||
attron(A_BOLD);
|
attron(A_BOLD);
|
||||||
printw("%s", msg.c_str());
|
printw("%s", msg.c_str());
|
||||||
attroff(A_BOLD);
|
attroff(A_BOLD);
|
||||||
|
|||||||
1
Log.h
1
Log.h
@@ -33,6 +33,7 @@ class Log
|
|||||||
public:
|
public:
|
||||||
Log();
|
Log();
|
||||||
void print(string msg, bool force=false);
|
void print(string msg, bool force=false);
|
||||||
|
void printProgressBar(int lineNumber, int offset, string msg, float percent);
|
||||||
|
|
||||||
LOG_COLOR getColor() const;
|
LOG_COLOR getColor() const;
|
||||||
void setColor(LOG_COLOR color);
|
void setColor(LOG_COLOR color);
|
||||||
|
|||||||
2
Main.cpp
2
Main.cpp
@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
Simulation sim;
|
Simulation sim;
|
||||||
//sim.createThread("timer", SIM_TIMER, &sim);
|
sim.createThread("timer", SIM_TIMER, &sim);
|
||||||
sim.natOverflowSimulation(nodesCount);
|
sim.natOverflowSimulation(nodesCount);
|
||||||
}
|
}
|
||||||
else if (!strcmp(argv[1], "-v"))
|
else if (!strcmp(argv[1], "-v"))
|
||||||
|
|||||||
@@ -42,10 +42,23 @@ NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp)
|
|||||||
|
|
||||||
int NATRouter::getFreePort()
|
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; i<this->lastUsedPort; ++i)
|
||||||
|
if (this->natTable[i].isFree())
|
||||||
|
{
|
||||||
|
this->lastUsedPort = i;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/*
|
||||||
for (int i=1; i<NAT_TABLE_LEN; ++i)
|
for (int i=1; i<NAT_TABLE_LEN; ++i)
|
||||||
if (this->natTable[i].isFree())
|
if (this->natTable[i].isFree())
|
||||||
return i;
|
return i;
|
||||||
|
*/
|
||||||
return -1; // nie ma żadnego wolnego portu
|
return -1; // nie ma żadnego wolnego portu
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,10 +75,6 @@ void NATRouter::onRecv()
|
|||||||
Packet p = this->recv();
|
Packet p = this->recv();
|
||||||
stringstream s;
|
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)
|
if (p.getSrcPort() != 0)
|
||||||
{
|
{
|
||||||
// jezeli pakiet idzie z jednego wezla do drugiego w danej possieci to odeslij mu "jak switch"
|
// 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;
|
NATItem *natItem;
|
||||||
|
|
||||||
// mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
|
// mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
|
||||||
|
// TODO getAllocatedPort - STRASZNIE zmula caly symulator
|
||||||
port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort());
|
port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort());
|
||||||
if (port == -1)
|
if (port == -1)
|
||||||
while ((port = this->getFreePort()) == -1)
|
while ((port = this->getFreePort()) == -1)
|
||||||
{
|
{
|
||||||
//TESTOWO
|
//TESTOWO
|
||||||
this->print("NAT table full, waiting 1s");
|
this->print("NAT table full, waiting 1s", true);
|
||||||
this->delay(1);
|
this->delay(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +164,7 @@ void NATRouter::sNAT(Packet *packet)
|
|||||||
natItem = &this->natTable[port];
|
natItem = &this->natTable[port];
|
||||||
natItem->setIp(packet->getSrcIp());
|
natItem->setIp(packet->getSrcIp());
|
||||||
natItem->setPort(packet->getSrcPort());
|
natItem->setPort(packet->getSrcPort());
|
||||||
natItem->setTimeout(/*5*/15);
|
natItem->setTimeout(15);
|
||||||
|
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port;
|
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->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
|
||||||
packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
|
packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
|
||||||
|
|
||||||
this->print(ss.str());
|
this->print(ss.str(), true);
|
||||||
this->delay();
|
this->delay();
|
||||||
this->send(*packet, true); // wysylam dalej
|
this->send(*packet, true); // wysylam dalej
|
||||||
natItem = NULL;
|
natItem = NULL;
|
||||||
@@ -223,8 +233,9 @@ void NATRouter::freePorts()
|
|||||||
|
|
||||||
Log portsLog;
|
Log portsLog;
|
||||||
string name = "NAT";
|
string name = "NAT";
|
||||||
portsLog.setLogParams(10, BLUE, "\t\t");
|
portsLog.setLogParams(3, GREEN, "\t\t");
|
||||||
portsLog.setObjectName(&name);
|
portsLog.setObjectName(&name);
|
||||||
|
portsLog.setDelay(100000);
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
@@ -238,9 +249,10 @@ void NATRouter::freePorts()
|
|||||||
if (ports < peak)
|
if (ports < peak)
|
||||||
peak = ports;
|
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.print(ss.str());
|
||||||
|
portsLog.printProgressBar(4, 4, "Utilization", (float)(NAT_TABLE_LEN-ports-1)/(NAT_TABLE_LEN-1));
|
||||||
portsLog.delay();
|
portsLog.delay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class NATRouter : public Node
|
|||||||
NetConf netWanConf; // konfiguracja sieciowa na zewnątrz
|
NetConf netWanConf; // konfiguracja sieciowa na zewnątrz
|
||||||
void initalizeNatTable();
|
void initalizeNatTable();
|
||||||
int getFreePort();
|
int getFreePort();
|
||||||
|
int lastUsedPort = 0;
|
||||||
int getAllocatedPort(string srcIp, int srcPort);
|
int getAllocatedPort(string srcIp, int srcPort);
|
||||||
void sNAT(Packet *packet);
|
void sNAT(Packet *packet);
|
||||||
void dNAT(Packet *packet);
|
void dNAT(Packet *packet);
|
||||||
|
|||||||
@@ -155,6 +155,7 @@ void Simulation::timer()
|
|||||||
char str[10];
|
char str[10];
|
||||||
int h,m, s;
|
int h,m, s;
|
||||||
unsigned long timeElapsed;
|
unsigned long timeElapsed;
|
||||||
|
pthread_mutex_t *mutex;
|
||||||
#endif
|
#endif
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
@@ -164,10 +165,12 @@ void Simulation::timer()
|
|||||||
h = timeElapsed / 3600;
|
h = timeElapsed / 3600;
|
||||||
m = (timeElapsed % 3600) / 60;
|
m = (timeElapsed % 3600) / 60;
|
||||||
s = (timeElapsed % 3600) % 60;
|
s = (timeElapsed % 3600) % 60;
|
||||||
//refresh();
|
mutex = Log::getMutex();
|
||||||
|
pthread_mutex_lock(mutex);
|
||||||
sprintf(str, "%02d:%02d:%02d", h, m, s);
|
sprintf(str, "%02d:%02d:%02d", h, m, s);
|
||||||
move(0, this->cols-strlen(str));
|
move(0, this->cols-strlen(str));
|
||||||
printw(str);
|
printw(str);
|
||||||
|
pthread_mutex_unlock(mutex);
|
||||||
#endif
|
#endif
|
||||||
usleep(100000); // 100 ms
|
usleep(100000); // 100 ms
|
||||||
//sleep(1);
|
//sleep(1);
|
||||||
@@ -242,16 +245,21 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Simulation::natOverflowSimulation(int nNodes)
|
void Simulation::natOverflowSimulation(int nNodes)
|
||||||
{
|
{
|
||||||
int i = 0, srcPort;
|
int i = 0;//, srcPort;
|
||||||
stringstream ss;
|
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");
|
this->print("Creating NAT device");
|
||||||
NATRouter router("Router", "10.13.12.254", "255.255.255.0");
|
NATRouter router("Router", "10.13.12.254", "255.255.255.0");
|
||||||
router.setLogParams(1, YELLOW, "\t\t");
|
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);
|
router.print("WAN IP 80.55.33.12/32", true);
|
||||||
sleep(1);
|
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");
|
this->print("Creating and setting clients");
|
||||||
Node *client = new Node[nNodes];
|
Node *client = new Node[nNodes];
|
||||||
int *currentSrcPort = new int[nNodes];
|
int *currentSrcPort = new int[nNodes];
|
||||||
for (i = 0; i < nNodes; ++i)
|
for (i = 0; i < nNodes; ++i)
|
||||||
{
|
{
|
||||||
currentSrcPort[i] = 0;
|
currentSrcPort[i] = 1;
|
||||||
client[i].setLogParams(0, CYAN, "\t");
|
client[i].setLogParams(2, CYAN, "\t");
|
||||||
ss.str("");
|
ss.str("");
|
||||||
ss << "Client " << (i + 1);
|
ss << "Client " << (i + 1);
|
||||||
client[i].setHostname(ss.str());
|
client[i].setHostname(ss.str());
|
||||||
@@ -315,18 +317,18 @@ void Simulation::natOverflowSimulation(int nNodes)
|
|||||||
p.setDstIp("93.92.91.90");
|
p.setDstIp("93.92.91.90");
|
||||||
p.setDstPort(80);
|
p.setDstPort(80);
|
||||||
i=0;
|
i=0;
|
||||||
struct timespec ts, ts2;
|
|
||||||
ts.tv_sec = 0;
|
|
||||||
ts.tv_nsec = 100;
|
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
if (i==65535)
|
i = (nNodes>1) ? rand()%nNodes : 0;
|
||||||
i=1;
|
if (currentSrcPort[i] == 65535)
|
||||||
p.setSrcPort(i++);
|
currentSrcPort[i] = 1;
|
||||||
client[0].send(p);
|
|
||||||
//this->delay(10);
|
p.setSrcPort(currentSrcPort[i]++);
|
||||||
|
|
||||||
|
client[i].send(p);
|
||||||
|
|
||||||
|
this->delay(150);
|
||||||
//usleep(1);
|
//usleep(1);
|
||||||
nanosleep(&ts, &ts2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user