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.

298 lines
6.5 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. // TO
  140. port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort());
  141. if (port == -1)
  142. while ((port = this->getFreePort()) == -1)
  143. {
  144. //TESTOWO
  145. this->print("NAT table full, waiting 1s", true);
  146. this->delay(1);
  147. }
  148. else
  149. allocated = true;
  150. // wstawiam do tablicy NAT info ze port zarezerowany na odbior
  151. natItem = &this->natTable[port];
  152. natItem->setIp(packet->getSrcIp());
  153. natItem->setPort(packet->getSrcPort());
  154. if (allocated)
  155. natItem->increaseTimeout(15);
  156. else
  157. natItem->setTimeout(15);
  158. ss << packet->getSrcIp() << ":" << packet->getSrcPort();
  159. if (!allocated)
  160. this->natHelper.insert(pair<string, int>(ss.str(), port));
  161. ss.str("");
  162. ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port;
  163. packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
  164. packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
  165. this->print(ss.str(), true);
  166. this->delay();
  167. this->send(*packet, true); // wysylam dalej
  168. natItem = NULL;
  169. }
  170. /*
  171. * funkcja DNAT ma za zadanie znalezc w tablicy NAT
  172. * port i adres IP wezla na ktory ma wrocic odpowiedz
  173. * z sieci Internet (z zewnatrz), a takze przedluzyc
  174. * czas zycia tego portu
  175. */
  176. void NATRouter::dNAT(Packet* packet)
  177. {
  178. NATItem *natItem = &this->natTable[packet->getDstPort()];
  179. stringstream ss;
  180. ss << "DNAT " << packet->getDstIp() << ":" << packet->getDstPort() << " --> " << natItem->getIp() << ":" << natItem->getPort();
  181. packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe
  182. packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla
  183. //natItem->free(); // zwalniam port w routerze
  184. natItem->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa
  185. this->print(ss.str());
  186. this->delay();
  187. this->send(*packet, true); // wysylam dalej
  188. natItem = NULL;
  189. }
  190. void NATRouter::setWanNetwork()
  191. {
  192. this->netWanConf.network = this->calculateNetwork(this->getWanIp(), this->getWanMask());
  193. }
  194. string NATRouter::getWanNetwork()
  195. {
  196. return this->netWanConf.network;
  197. }
  198. /*
  199. * funkcja szuka aktywnego zaalokowanego portu (aktywna
  200. * transmisja) w tablicy NAT dla pary (srcIp:srcPort)
  201. */
  202. int NATRouter::getAllocatedPort(string srcIp, int srcPort)
  203. {
  204. map<string, int>::iterator it;
  205. int pos = -1;
  206. /*for (int i=1; i<NAT_TABLE_LEN; ++i)
  207. if (this->natTable[i].getIp() == srcIp && this->natTable[i].getPort() == srcPort)
  208. return i;
  209. */
  210. stringstream ss;
  211. ss << srcIp << ":" << srcPort;
  212. it = this->natHelper.find(ss.str());
  213. if (it != this->natHelper.end())
  214. {
  215. // sprawdz czy wpis o podanym IP z nat nie zostal napisany (referencja nieaktualna)
  216. if (this->natTable[it->second].getIp() != srcIp || this->natTable[it->second].getPort() != srcPort)
  217. this->natHelper.erase(it);
  218. else
  219. pos = it->second;
  220. }
  221. return pos;
  222. }
  223. void NATRouter::freePorts()
  224. {
  225. stringstream ss;
  226. int i, ports, peak=NAT_TABLE_LEN-1;
  227. Log portsLog;
  228. string name = "NAT";
  229. portsLog.setLogParams(3, GREEN, "\t\t");
  230. portsLog.setObjectName(&name);
  231. portsLog.setDelay(100000);
  232. while(true)
  233. {
  234. ports = 0;
  235. ss.str("");
  236. for (i=1; i<NAT_TABLE_LEN; ++i)
  237. if (this->natTable[i].isFree())
  238. ++ports;
  239. if (ports < peak)
  240. peak = ports;
  241. ss << "Free/Reserved:\t" << ports << " (" << peak << ")/" << (NAT_TABLE_LEN-ports-1); // poprawka na nieuzywany port 0
  242. portsLog.print(ss.str());
  243. portsLog.printProgressBar(4, 4, "Utilization", (float)(NAT_TABLE_LEN-ports-1)/(NAT_TABLE_LEN-1));
  244. portsLog.delay();
  245. }
  246. }
  247. string NATRouter::getWanGatewayIp()
  248. {
  249. return this->netWanConf.gatewayIp;
  250. }