polaczenia do sieci jako wewnetrzne/zewnetrzne, przeniesione do mapy
This commit is contained in:
27
Main.cpp
27
Main.cpp
@@ -11,6 +11,31 @@
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* test puszczania sciezki external/internal
|
||||||
|
|
||||||
|
Node k1("K1", "10.0.0.5", "255.255.255.0", "10.0.0.2"), k2("K2", "10.0.0.2", "255.255.255.0", "10.0.0.1");
|
||||||
|
NATRouter n1("N1", "10.0.0.2", "255.255.255.0"), n2("N2", "10.0.0.1", "255.255.255.0");
|
||||||
|
n1.setWanIp("8.8.8.8");
|
||||||
|
n1.setWanMask("255.255.255.255");
|
||||||
|
n2.setWanIp("8.8.4.4");
|
||||||
|
n2.setWanMask("255.255.255.255");
|
||||||
|
n1.connectNode(&k1);
|
||||||
|
n2.connectNode(&k2);
|
||||||
|
n2.connectNode(&n1, true);
|
||||||
|
|
||||||
|
Packet pkt("TEST");
|
||||||
|
pkt.setDstIp("10.0.0.2");
|
||||||
|
pkt.setDstPort(1000);
|
||||||
|
cout << "N2 Send: " << n2.send(pkt) << endl;
|
||||||
|
cout << "Wywoluje onRecv() na N1" << endl;
|
||||||
|
n1.onRecv();
|
||||||
|
cout << "Wywoluje onRecv() na K2" << endl;
|
||||||
|
k2.onRecv();
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
*/
|
||||||
|
|
||||||
cout << "Obiekt PC1" << endl;
|
cout << "Obiekt PC1" << endl;
|
||||||
Node pc1("PC1", "10.0.0.2", "255.0.0.0", "10.0.0.1");
|
Node pc1("PC1", "10.0.0.2", "255.0.0.0", "10.0.0.1");
|
||||||
|
|
||||||
@@ -28,7 +53,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
cout << "Obiekt S" << endl;
|
cout << "Obiekt S" << endl;
|
||||||
Node s("Serwer", "8.8.8.8", "255.255.255.255");
|
Node s("Serwer", "8.8.8.8", "255.255.255.255");
|
||||||
s.connectNode(&r1);
|
s.connectNode(&r1, true);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* zastapilem to polaczenie bezposrednie
|
* zastapilem to polaczenie bezposrednie
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ void NATRouter::onRecv()
|
|||||||
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"
|
||||||
if (this->calculateNetwork(p.getDstIp(), this->getMask()) == this->getNetwork())
|
if (this->calculateNetwork(p.getDstIp(), this->getMask()) == this->getNetwork() && p.getDstIp() != this->getIp())
|
||||||
{
|
{
|
||||||
this->send(p, true);
|
this->send(p, true);
|
||||||
}
|
}
|
||||||
|
|||||||
38
Node.cpp
38
Node.cpp
@@ -10,27 +10,39 @@
|
|||||||
|
|
||||||
Node * Node::findConnection(string ip)
|
Node * Node::findConnection(string ip)
|
||||||
{
|
{
|
||||||
NATRouter *ptr;
|
NATRouter *r;
|
||||||
|
Node *ptr = NULL;
|
||||||
if (ip == "0.0.0.0")
|
if (ip == "0.0.0.0")
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
vector<Node*>::iterator it = this->connectedNodes.begin();
|
// najpierw szukam tras lokalnych
|
||||||
|
map<Node*, bool>::iterator it = this->connectedNodes.begin();
|
||||||
for (; it != this->connectedNodes.end(); ++it)
|
for (; it != this->connectedNodes.end(); ++it)
|
||||||
{
|
{
|
||||||
if ((*it)->getIp() == ip)
|
//if ((*it)->getIp() == ip)
|
||||||
return *it;
|
if ((*it).first->getIp() == ip)
|
||||||
|
{
|
||||||
|
ptr = (*it).first;
|
||||||
|
if ((*it).second == false)
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* jezeli wezel ma polaczenie do NAT-routera
|
* jezeli wezel ma polaczenie do NAT-routera
|
||||||
* (i istnieje jego interfejs WAN to sprawdz
|
* (i istnieje jego interfejs WAN to sprawdz
|
||||||
* czy nie ma z nim bezposredniego polaczenia)
|
* czy nie ma z nim bezposredniego polaczenia)
|
||||||
*/
|
*/
|
||||||
ptr = dynamic_cast<NATRouter*>(*it);
|
r = dynamic_cast<NATRouter*>((*it).first);
|
||||||
|
|
||||||
if (ptr != NULL)
|
if (r != NULL)
|
||||||
if (ptr->getWanIp() == ip)
|
if (r->getWanIp() == ip)
|
||||||
return *it;
|
return (*it).first;
|
||||||
}
|
}
|
||||||
|
/* jezeli takich nie ma, a jest zewnetrzna
|
||||||
|
* to musze wskazac do niej
|
||||||
|
*/
|
||||||
|
if (ptr)
|
||||||
|
return ptr;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -112,24 +124,24 @@ Node::Node(string hostname, string ip, string mask, string gatewayIp) : Node(hos
|
|||||||
* czy polaczenie juz gdzies zostalo przypadkowo nawiazane, nawiazujemy polaczenie, a nastepnie
|
* czy polaczenie juz gdzies zostalo przypadkowo nawiazane, nawiazujemy polaczenie, a nastepnie
|
||||||
* przekazujemy sterowanie drugiemu wezlowi zeby zrobil to samo)
|
* przekazujemy sterowanie drugiemu wezlowi zeby zrobil to samo)
|
||||||
*/
|
*/
|
||||||
bool Node::connectNode(Node *node, bool firstConnected)
|
bool Node::connectNode(Node *node, bool isExternal, bool firstConnected)
|
||||||
{
|
{
|
||||||
if (node->getIp() == "0.0.0.0") // jezeli wezel nie ma skonfigurowanej sieci, nie mozna go przylaczyc
|
if (node->getIp() == "0.0.0.0") // jezeli wezel nie ma skonfigurowanej sieci, nie mozna go przylaczyc
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!firstConnected)
|
if (!firstConnected)
|
||||||
{
|
{
|
||||||
vector<Node*>::iterator it = this->connectedNodes.begin();
|
map<Node*, bool>::iterator it = this->connectedNodes.begin();
|
||||||
for (; it != this->connectedNodes.end(); ++it) // sprawdzamy, czy połączenia już przypadkiem nie ma
|
for (; it != this->connectedNodes.end(); ++it) // sprawdzamy, czy połączenia już przypadkiem nie ma
|
||||||
{
|
{
|
||||||
if (*it == node)
|
if ((*it).first == node)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->connectedNodes.push_back(node); // podłączamy drugi węzeł
|
this->connectedNodes.insert(pair<Node*, bool>(node, isExternal));//this->connectedNodes.push_back(node); // podłączamy drugi węzeł
|
||||||
|
|
||||||
if (!firstConnected)
|
if (!firstConnected)
|
||||||
node->connectNode(this, true); // to samo w drugą stronę
|
node->connectNode(this, isExternal, true); // to samo w drugą stronę
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
4
Node.h
4
Node.h
@@ -21,7 +21,7 @@ class Node
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
queue<Packet>rcvBuffer; // kolejka z pakietami do przetworzenia
|
queue<Packet>rcvBuffer; // kolejka z pakietami do przetworzenia
|
||||||
vector<Node*> connectedNodes; // referencje do węzłów, z którymi jest podłączony
|
map<Node*, bool> connectedNodes; // referencje do węzłów, z którymi jest podłączony
|
||||||
NetConf netConf; // konfiguracja sieciowa
|
NetConf netConf; // konfiguracja sieciowa
|
||||||
string hostname; // nazwa węzła
|
string hostname; // nazwa węzła
|
||||||
void setNetwork();
|
void setNetwork();
|
||||||
@@ -38,7 +38,7 @@ public:
|
|||||||
Node(string hostname, string ip, string mask);
|
Node(string hostname, string ip, string mask);
|
||||||
Node(string hostname, string ip, string mask, string gatewayIp);
|
Node(string hostname, string ip, string mask, string gatewayIp);
|
||||||
virtual ~Node();
|
virtual ~Node();
|
||||||
bool connectNode(Node *node, bool firstConnected = false);
|
bool connectNode(Node *node, bool isExternal = false, bool firstConnected = false);
|
||||||
|
|
||||||
bool send(Packet packet, bool isRouter = false);
|
bool send(Packet packet, bool isRouter = false);
|
||||||
void putPacket(Packet packet);
|
void putPacket(Packet packet);
|
||||||
|
|||||||
Reference in New Issue
Block a user