Symulacja NAT na przedmiot Symulacje Komputerowe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
5.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * NATItem.cpp
  3. *
  4. * Created on: 11-01-2017
  5. * Author: Piotr Dergun
  6. */
  7. #include "NATRouter.h"
  8. void NATRouter::initalizeNatTable()
  9. {
  10. try
  11. {
  12. natTable = new NATItem[NAT_TABLE_LEN];
  13. }
  14. catch (bad_alloc &ba)
  15. {
  16. cerr << "Nie mozna zaalokowac pamieci dla tablicy NAT!" << endl;
  17. exit(1);
  18. }
  19. }
  20. NATRouter::NATRouter()
  21. {
  22. this->initalizeNatTable();
  23. }
  24. NATRouter::NATRouter(string hostname) : Node(hostname)
  25. {
  26. this->initalizeNatTable();
  27. }
  28. NATRouter::NATRouter(string hostname, string ip, string mask) : Node(hostname, ip, mask)
  29. {
  30. this->initalizeNatTable();
  31. }
  32. NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp) : Node(hostname, ip, mask, gatewayIp)
  33. {
  34. this->initalizeNatTable();
  35. }
  36. int NATRouter::getFreePort()
  37. {
  38. if (this->lastUsedPort == NAT_TABLE_LEN-1)
  39. this->lastUsedPort = 0;
  40. if (this->natTable[this->lastUsedPort+1].isFree())
  41. return ++this->lastUsedPort;
  42. for (int i = 1; i<this->lastUsedPort; ++i)
  43. if (this->natTable[i].isFree())
  44. {
  45. this->lastUsedPort = i;
  46. return i;
  47. }
  48. /*
  49. for (int i=1; i<NAT_TABLE_LEN; ++i)
  50. if (this->natTable[i].isFree())
  51. return i;
  52. */
  53. return -1; // nie ma żadnego wolnego portu
  54. }
  55. NATRouter::~NATRouter()
  56. {
  57. if (natTable)
  58. delete [] natTable;
  59. }
  60. void NATRouter::onRecv()
  61. {
  62. while (true)
  63. {
  64. Packet p = this->recv();
  65. stringstream s;
  66. if (p.getSrcPort() != 0)
  67. {
  68. // jezeli pakiet idzie z jednego wezla do drugiego w danej possieci to odeslij mu "jak switch"
  69. if (this->calculateNetwork(p.getDstIp(), this->getMask()) == this->getNetwork() && p.getDstIp() != this->getIp())
  70. {
  71. this->send(p, true);
  72. }
  73. else if (p.getDstIp() != this->getIp() && p.getDstIp() != this->getWanIp())
  74. {
  75. this->sNAT(&p);
  76. }
  77. else if (p.getDstIp() == this->getWanIp() && !this->natTable[p.getDstPort()].isFree())
  78. {
  79. this->dNAT(&p);
  80. }
  81. else
  82. {
  83. // TUTAJ ew. implementacja obslugi pakietu adresowanego do routera
  84. }
  85. this->delay();
  86. }
  87. else
  88. {
  89. #ifndef DEBUG
  90. this->print("onRecv() sleeping...");
  91. #endif
  92. }
  93. this->delay();
  94. }
  95. }
  96. void NATRouter::setWanIp(string wanIp)
  97. {
  98. this->netWanConf.ip = wanIp;
  99. this->setWanNetwork();
  100. }
  101. void NATRouter::setWanMask(string wanMask)
  102. {
  103. this->netWanConf.mask = wanMask;
  104. this->setWanNetwork();
  105. }
  106. void NATRouter::setWanGatewayIp(string wanGatewayIp)
  107. {
  108. this->netWanConf.gatewayIp = wanGatewayIp;
  109. }
  110. string NATRouter::getWanIp()
  111. {
  112. return this->netWanConf.ip;
  113. }
  114. string NATRouter::getWanMask()
  115. {
  116. return this->netWanConf.mask;
  117. }
  118. /*
  119. * funkcja SNAT zapisuje w tablicy NAT
  120. * numer portu zrodlowego komputera z LAN
  121. * oraz jego IP i przeksztalca pakiet, zmieniajac
  122. * zrodlowy IP na IP zewn. routera oraz wolny
  123. * port z tablicy NAT. Jezeli portu nie ma to funkcja
  124. * czeka, az sie zwolni
  125. */
  126. void NATRouter::sNAT(Packet *packet)
  127. {
  128. int port;
  129. NATItem *natItem;
  130. // mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
  131. // TODO getAllocatedPort - STRASZNIE zmula caly symulator
  132. port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort());
  133. if (port == -1)
  134. while ((port = this->getFreePort()) == -1)
  135. {
  136. //TESTOWO
  137. this->print("NAT table full, waiting 1s", true);
  138. this->delay(1);
  139. }
  140. // wstawiam do tablicy NAT info ze port zarezerowany na odbior
  141. natItem = &this->natTable[port];
  142. natItem->setIp(packet->getSrcIp());
  143. natItem->setPort(packet->getSrcPort());
  144. natItem->setTimeout(15);
  145. stringstream ss;
  146. ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port;
  147. packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
  148. packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
  149. this->print(ss.str(), true);
  150. this->delay();
  151. this->send(*packet, true); // wysylam dalej
  152. natItem = NULL;
  153. }
  154. /*
  155. * funkcja DNAT ma za zadanie znalezc w tablicy NAT
  156. * port i adres IP wezla na ktory ma wrocic odpowiedz
  157. * z sieci Internet (z zewnatrz), a takze przedluzyc
  158. * czas zycia tego portu
  159. */
  160. void NATRouter::dNAT(Packet* packet)
  161. {
  162. NATItem *natItem = &this->natTable[packet->getDstPort()];
  163. stringstream ss;
  164. ss << "DNAT " << packet->getDstIp() << ":" << packet->getDstPort() << " --> " << natItem->getIp() << ":" << natItem->getPort();
  165. packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe
  166. packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla
  167. //natItem->free(); // zwalniam port w routerze
  168. natItem->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa
  169. this->print(ss.str());
  170. this->delay();
  171. this->send(*packet, true); // wysylam dalej
  172. natItem = NULL;
  173. }
  174. void NATRouter::setWanNetwork()
  175. {
  176. this->netWanConf.network = this->calculateNetwork(this->getWanIp(), this->getWanMask());
  177. }
  178. string NATRouter::getWanNetwork()
  179. {
  180. return this->netWanConf.network;
  181. }
  182. /*
  183. * funkcja szuka aktywnego zaalokowanego portu (aktywna
  184. * transmisja) w tablicy NAT dla pary (srcIp:srcPort)
  185. */
  186. int NATRouter::getAllocatedPort(string srcIp, int srcPort)
  187. {
  188. for (int i=1; i<NAT_TABLE_LEN; ++i)
  189. if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort)
  190. return i;
  191. return -1; // nic nie znaleziono
  192. }
  193. void NATRouter::freePorts()
  194. {
  195. stringstream ss;
  196. int i, ports, peak=NAT_TABLE_LEN-1;
  197. Log portsLog;
  198. string name = "NAT";
  199. portsLog.setLogParams(3, GREEN, "\t\t");
  200. portsLog.setObjectName(&name);
  201. portsLog.setDelay(100000);
  202. while(true)
  203. {
  204. ports = 0;
  205. ss.str("");
  206. for (i=1; i<NAT_TABLE_LEN; ++i)
  207. if (this->natTable[i].isFree())
  208. ++ports;
  209. if (ports < peak)
  210. peak = ports;
  211. ss << "Free/Reserved:\t" << ports << " (" << peak << ")/" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0
  212. portsLog.print(ss.str());
  213. portsLog.printProgressBar(4, 4, "Utilization", (float)(NAT_TABLE_LEN-ports-1)/(NAT_TABLE_LEN-1));
  214. portsLog.delay();
  215. }
  216. }
  217. string NATRouter::getWanGatewayIp()
  218. {
  219. return this->netWanConf.gatewayIp;
  220. }