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.

297 lines
6.6 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 = this->lastUsedPort+1; i<NAT_TABLE_LEN; ++i)
  43. if (this->natTable[i].isFree())
  44. {
  45. this->lastUsedPort = i;
  46. return i;
  47. }
  48. for (int i = 1; i<this->lastUsedPort; ++i)
  49. if (this->natTable[i].isFree())
  50. {
  51. this->lastUsedPort = i;
  52. return i;
  53. }
  54. /*
  55. for (int i=1; i<NAT_TABLE_LEN; ++i)
  56. if (this->natTable[i].isFree())
  57. return i;
  58. */
  59. return -1; // nie ma żadnego wolnego portu
  60. }
  61. NATRouter::~NATRouter()
  62. {
  63. if (natTable)
  64. delete [] natTable;
  65. }
  66. void NATRouter::onRecv()
  67. {
  68. while (true)
  69. {
  70. Packet p = this->recv();
  71. stringstream s;
  72. if (p.getSrcPort() != 0)
  73. {
  74. // jezeli pakiet idzie z jednego wezla do drugiego w danej possieci to odeslij mu "jak switch"
  75. if (this->calculateNetwork(p.getDstIp(), this->getMask()) == this->getNetwork() && p.getDstIp() != this->getIp())
  76. {
  77. this->send(p, true);
  78. }
  79. else if (p.getDstIp() != this->getIp() && p.getDstIp() != this->getWanIp())
  80. {
  81. this->sNAT(&p);
  82. }
  83. else if (p.getDstIp() == this->getWanIp() && !this->natTable[p.getDstPort()].isFree())
  84. {
  85. this->dNAT(&p);
  86. }
  87. else
  88. {
  89. // TUTAJ ew. implementacja obslugi pakietu adresowanego do routera
  90. }
  91. this->delay();
  92. }
  93. else
  94. {
  95. #ifndef DEBUG
  96. this->print("onRecv() sleeping...");
  97. #endif
  98. }
  99. this->delay();
  100. }
  101. }
  102. void NATRouter::setWanIp(string wanIp)
  103. {
  104. this->netWanConf.ip = wanIp;
  105. this->setWanNetwork();
  106. }
  107. void NATRouter::setWanMask(string wanMask)
  108. {
  109. this->netWanConf.mask = wanMask;
  110. this->setWanNetwork();
  111. }
  112. void NATRouter::setWanGatewayIp(string wanGatewayIp)
  113. {
  114. this->netWanConf.gatewayIp = wanGatewayIp;
  115. }
  116. string NATRouter::getWanIp()
  117. {
  118. return this->netWanConf.ip;
  119. }
  120. string NATRouter::getWanMask()
  121. {
  122. return this->netWanConf.mask;
  123. }
  124. /*
  125. * funkcja SNAT zapisuje w tablicy NAT
  126. * numer portu zrodlowego komputera z LAN
  127. * oraz jego IP i przeksztalca pakiet, zmieniajac
  128. * zrodlowy IP na IP zewn. routera oraz wolny
  129. * port z tablicy NAT. Jezeli portu nie ma to funkcja
  130. * czeka, az sie zwolni
  131. */
  132. void NATRouter::sNAT(Packet *packet)
  133. {
  134. int port;
  135. NATItem *natItem;
  136. bool allocated = false;
  137. stringstream ss;
  138. // mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
  139. port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort());
  140. if (port == -1)
  141. while ((port = this->getFreePort()) == -1)
  142. {
  143. //TESTOWO
  144. this->print("NAT table full, waiting 1s", true);
  145. this->delay(1);
  146. }
  147. else
  148. allocated = true;
  149. // wstawiam do tablicy NAT info ze port zarezerowany na odbior
  150. natItem = &this->natTable[port];
  151. natItem->setIp(packet->getSrcIp());
  152. natItem->setPort(packet->getSrcPort());
  153. if (allocated)
  154. natItem->increaseTimeout(15);
  155. else
  156. natItem->setTimeout(15);
  157. ss << packet->getSrcIp() << ":" << packet->getSrcPort();
  158. if (!allocated)
  159. this->natHelper.insert(pair<string, int>(ss.str(), port));
  160. ss.str("");
  161. ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port;
  162. packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
  163. packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
  164. this->print(ss.str(), true);
  165. this->delay();
  166. this->send(*packet, true); // wysylam dalej
  167. natItem = NULL;
  168. }
  169. /*
  170. * funkcja DNAT ma za zadanie znalezc w tablicy NAT
  171. * port i adres IP wezla na ktory ma wrocic odpowiedz
  172. * z sieci Internet (z zewnatrz), a takze przedluzyc
  173. * czas zycia tego portu
  174. */
  175. void NATRouter::dNAT(Packet* packet)
  176. {
  177. NATItem *natItem = &this->natTable[packet->getDstPort()];
  178. stringstream ss;
  179. ss << "DNAT " << packet->getDstIp() << ":" << packet->getDstPort() << " --> " << natItem->getIp() << ":" << natItem->getPort();
  180. packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe
  181. packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla
  182. //natItem->free(); // zwalniam port w routerze
  183. natItem->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa
  184. this->print(ss.str());
  185. this->delay();
  186. this->send(*packet, true); // wysylam dalej
  187. natItem = NULL;
  188. }
  189. void NATRouter::setWanNetwork()
  190. {
  191. this->netWanConf.network = this->calculateNetwork(this->getWanIp(), this->getWanMask());
  192. }
  193. string NATRouter::getWanNetwork()
  194. {
  195. return this->netWanConf.network;
  196. }
  197. /*
  198. * funkcja szuka aktywnego zaalokowanego portu (aktywna
  199. * transmisja) w tablicy NAT dla pary (srcIp:srcPort)
  200. */
  201. int NATRouter::getAllocatedPort(string srcIp, int srcPort)
  202. {
  203. map<string, int>::iterator it;
  204. int pos = -1;
  205. /*for (int i=1; i<NAT_TABLE_LEN; ++i)
  206. if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort)
  207. return i;
  208. */
  209. stringstream ss;
  210. ss << srcIp << ":" << srcPort;
  211. it = this->natHelper.find(ss.str());
  212. if (it != this->natHelper.end())
  213. {
  214. // sprawdz czy wpis o podanym IP z nat nie zostal napisany (referencja nieaktualna)
  215. if (this->natTable[it->second].getIp() != srcIp || this->natTable[it->second].getPort() != srcPort)
  216. this->natHelper.erase(it);
  217. else
  218. pos = it->second;
  219. }
  220. return pos;
  221. }
  222. void NATRouter::freePorts()
  223. {
  224. stringstream ss;
  225. int i, ports, peak=NAT_TABLE_LEN-1;
  226. Log portsLog;
  227. string name = "NAT";
  228. portsLog.setLogParams(3, GREEN, "\t\t");
  229. portsLog.setObjectName(&name);
  230. portsLog.setDelay(100000);
  231. while(true)
  232. {
  233. ports = 0;
  234. ss.str("");
  235. for (i=1; i<NAT_TABLE_LEN; ++i)
  236. if (this->natTable[i].isFree())
  237. ++ports;
  238. if (ports < peak)
  239. peak = ports;
  240. ss << "Free/Reserved:\t" << ports << " (" << peak << ")/" << (NAT_TABLE_LEN-ports-1) << " (" << (NAT_TABLE_LEN-peak-1) << ")"; // poprawka na nieuzywany port 0
  241. portsLog.print(ss.str());
  242. portsLog.printProgressBar(4, 4, "Utilization", (float)(NAT_TABLE_LEN-ports-1)/(NAT_TABLE_LEN-1));
  243. portsLog.delay();
  244. }
  245. }
  246. string NATRouter::getWanGatewayIp()
  247. {
  248. return this->netWanConf.gatewayIp;
  249. }