Browse Source

obsluga adresu sieci interfejsu WAN na NATRouter

master
Piotr Dergun 7 years ago
parent
commit
d176521abe
4 changed files with 29 additions and 5 deletions
  1. +26
    -1
      NATRouter.cpp
  2. +2
    -0
      NATRouter.h
  3. +1
    -3
      Node.cpp
  4. +0
    -1
      Node.h

+ 26
- 1
NATRouter.cpp View File

@ -59,7 +59,6 @@ void NATRouter::onRecv()
{ {
Packet p = this->recv(); Packet p = this->recv();
// TODO: jako ze router jest tez switchem, zaimplementowac wysylanie wiadomosci po sieci lokalnej
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"
@ -85,11 +84,13 @@ void NATRouter::onRecv()
void NATRouter::setWanIp(string wanIp) void NATRouter::setWanIp(string wanIp)
{ {
this->netWanConf.ip = wanIp; this->netWanConf.ip = wanIp;
this->setWanNetwork();
} }
void NATRouter::setWanMask(string wanMask) void NATRouter::setWanMask(string wanMask)
{ {
this->netWanConf.mask = wanMask; this->netWanConf.mask = wanMask;
this->setWanNetwork();
} }
void NATRouter::setWanGatewayIp(string wanGatewayIp) void NATRouter::setWanGatewayIp(string wanGatewayIp)
@ -107,6 +108,14 @@ string NATRouter::getWanMask()
return this->netWanConf.mask; return this->netWanConf.mask;
} }
/*
* funkcja SNAT zapisuje w tablicy NAT
* numer portu zrodlowego komputera z LAN
* oraz jego IP i przeksztalca pakiet, zmieniajac
* zrodlowy IP na IP zewn. routera oraz wolny
* port z tablicy NAT. Jezeli portu nie ma to funkcja
* czeka, az sie zwolni
*/
void NATRouter::sNAT(Packet *packet) void NATRouter::sNAT(Packet *packet)
{ {
int port; int port;
@ -133,6 +142,12 @@ void NATRouter::sNAT(Packet *packet)
natItem = NULL; natItem = NULL;
} }
/*
* funkcja DNAT ma za zadanie znalezc w tablicy NAT
* port i adres IP wezla na ktory ma wrocic odpowiedz
* z sieci Internet (z zewnatrz), a takze zwolnic ten
* port
*/
void NATRouter::dNAT(Packet* packet) void NATRouter::dNAT(Packet* packet)
{ {
NATItem *natItem = &this->natTable[packet->getDstPort()]; NATItem *natItem = &this->natTable[packet->getDstPort()];
@ -146,6 +161,16 @@ void NATRouter::dNAT(Packet* packet)
natItem = NULL; natItem = NULL;
} }
void NATRouter::setWanNetwork()
{
this->netWanConf.network = this->calculateNetwork(this->getWanIp(), this->getWanMask());
}
string NATRouter::getWanNetwork()
{
return this->netWanConf.network;
}
string NATRouter::getWanGatewayIp() string NATRouter::getWanGatewayIp()
{ {
return this->netWanConf.gatewayIp; return this->netWanConf.gatewayIp;

+ 2
- 0
NATRouter.h View File

@ -20,6 +20,7 @@ class NATRouter : public Node
int getFreePort(); int getFreePort();
void sNAT(Packet *packet); void sNAT(Packet *packet);
void dNAT(Packet *packet); void dNAT(Packet *packet);
void setWanNetwork();
public: public:
NATRouter(); NATRouter();
@ -37,6 +38,7 @@ public:
// gettery // gettery
string getWanIp(); string getWanIp();
string getWanMask(); string getWanMask();
string getWanNetwork();
string getWanGatewayIp(); string getWanGatewayIp();
}; };
#endif /* NATROUTER_H_ */ #endif /* NATROUTER_H_ */

+ 1
- 3
Node.cpp View File

@ -137,7 +137,6 @@ bool Node::connectNode(Node *node, bool firstConnected)
/* /*
* funkcja wysyla, czyli przekazuje kopie pakietu kolejnemu wezlowi - docelowemu lub bramie domyslnej * funkcja wysyla, czyli przekazuje kopie pakietu kolejnemu wezlowi - docelowemu lub bramie domyslnej
* o ile jest skonfigurowana i istnieje do niej sciezka * o ile jest skonfigurowana i istnieje do niej sciezka
* TODO zaimplementowac porownywanie z maska - jezeli dstIp z maska nie gra, wysylac na ruter
*/ */
bool Node::send(Packet packet, bool isRouter) bool Node::send(Packet packet, bool isRouter)
{ {
@ -182,7 +181,6 @@ void Node::setHostname(string hostname)
void Node::setIp(string ip) void Node::setIp(string ip)
{ {
this->netConf.ip = ip; this->netConf.ip = ip;
this->setNetwork(); this->setNetwork();
} }
@ -240,7 +238,7 @@ void Node::onRecv()
void Node::setNetwork() void Node::setNetwork()
{ {
this->netConf.network = this->calculateNetwork(this->netConf.ip, this->netConf.mask);
this->netConf.network = this->calculateNetwork(this->getIp(), this->getMask());
} }
string Node::getNetwork() string Node::getNetwork()

+ 0
- 1
Node.h View File

@ -30,7 +30,6 @@ protected:
Packet recv(); Packet recv();
string ipToString(unsigned int ip); string ipToString(unsigned int ip);
unsigned int stringToIp(string ip); unsigned int stringToIp(string ip);
public:
string calculateNetwork(string ip, string mask); string calculateNetwork(string ip, string mask);
public: public:

Loading…
Cancel
Save