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.

272 lines
5.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**
  2. * @file NATRouter.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->lastUsedPort = 0;
  23. this->initalizeNatTable();
  24. }
  25. NATRouter::NATRouter(string hostname) : Node(hostname)
  26. {
  27. this->lastUsedPort = 0;
  28. this->initalizeNatTable();
  29. }
  30. NATRouter::NATRouter(string hostname, string ip, string mask) : Node(hostname, ip, mask)
  31. {
  32. this->lastUsedPort = 0;
  33. this->initalizeNatTable();
  34. }
  35. NATRouter::NATRouter(string hostname, string ip, string mask, string gatewayIp) : Node(hostname, ip, mask, gatewayIp)
  36. {
  37. this->lastUsedPort = 0;
  38. this->initalizeNatTable();
  39. }
  40. int NATRouter::getFreePort()
  41. {
  42. if (this->lastUsedPort == NAT_TABLE_LEN-1)
  43. this->lastUsedPort = 0;
  44. for (int i = this->lastUsedPort+1; i<NAT_TABLE_LEN; ++i)
  45. if (this->natTable[i].isFree())
  46. {
  47. this->lastUsedPort = i;
  48. return i;
  49. }
  50. for (int i = 1; i<this->lastUsedPort; ++i)
  51. if (this->natTable[i].isFree())
  52. {
  53. this->lastUsedPort = i;
  54. return i;
  55. }
  56. return -1; // nie ma żadnego wolnego portu
  57. }
  58. NATRouter::~NATRouter()
  59. {
  60. if (natTable)
  61. delete [] natTable;
  62. }
  63. void NATRouter::onRecv()
  64. {
  65. while (true)
  66. {
  67. Packet p = this->recv();
  68. stringstream s;
  69. if (p.getSrcPort() != 0)
  70. {
  71. // jezeli pakiet idzie z jednego wezla do drugiego w danej possieci to odeslij mu "jak switch"
  72. if (this->calculateNetwork(p.getDstIp(), this->getMask()) == this->getNetwork() && p.getDstIp() != this->getIp())
  73. {
  74. this->send(p, true);
  75. }
  76. else if (p.getDstIp() != this->getIp() && p.getDstIp() != this->getWanIp())
  77. {
  78. this->sNAT(&p);
  79. }
  80. else if (p.getDstIp() == this->getWanIp() && !this->natTable[p.getDstPort()].isFree())
  81. {
  82. this->dNAT(&p);
  83. }
  84. else
  85. {
  86. // TUTAJ ew. implementacja obslugi pakietu adresowanego do routera
  87. }
  88. this->delay();
  89. }
  90. else
  91. {
  92. #ifndef DEBUG
  93. this->print("onRecv() sleeping...");
  94. #endif
  95. }
  96. this->delay();
  97. }
  98. }
  99. void NATRouter::setWanIp(string wanIp)
  100. {
  101. this->netWanConf.ip = wanIp;
  102. this->setWanNetwork();
  103. }
  104. void NATRouter::setWanMask(string wanMask)
  105. {
  106. this->netWanConf.mask = wanMask;
  107. this->setWanNetwork();
  108. }
  109. void NATRouter::setWanGatewayIp(string wanGatewayIp)
  110. {
  111. this->netWanConf.gatewayIp = wanGatewayIp;
  112. }
  113. string NATRouter::getWanIp()
  114. {
  115. return this->netWanConf.ip;
  116. }
  117. string NATRouter::getWanMask()
  118. {
  119. return this->netWanConf.mask;
  120. }
  121. void NATRouter::sNAT(Packet *packet)
  122. {
  123. int port;
  124. NATItem *natItem;
  125. bool allocated = false;
  126. stringstream ss;
  127. // mozliwe, ze juz port zostal zaalokowany, to trzeba wykorzystac
  128. port = this->getAllocatedPort(packet->getSrcIp(), packet->getSrcPort());
  129. if (port == -1)
  130. while ((port = this->getFreePort()) == -1)
  131. {
  132. this->print("NAT table full, waiting 1s", true);
  133. this->delay(1);
  134. }
  135. else
  136. allocated = true;
  137. // wstawiam do tablicy NAT info ze port zarezerowany na odbior
  138. natItem = &this->natTable[port];
  139. natItem->setIp(packet->getSrcIp());
  140. natItem->setPort(packet->getSrcPort());
  141. if (allocated)
  142. natItem->increaseTimeout(15);
  143. else
  144. natItem->setTimeout(15);
  145. ss << packet->getSrcIp() << ":" << packet->getSrcPort();
  146. if (!allocated)
  147. this->natHelper.insert(pair<string, int>(ss.str(), port));
  148. ss.str("");
  149. ss << "SNAT " << packet->getSrcIp() << ":" << packet->getSrcPort() << " --> " << this->getWanIp() << ":" << port;
  150. packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
  151. packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
  152. this->print(ss.str(), true);
  153. this->delay();
  154. this->send(*packet, true); // wysylam dalej
  155. natItem = NULL;
  156. }
  157. void NATRouter::dNAT(Packet* packet)
  158. {
  159. NATItem *natItem = &this->natTable[packet->getDstPort()];
  160. stringstream ss;
  161. ss << "DNAT " << packet->getDstIp() << ":" << packet->getDstPort() << " --> " << natItem->getIp() << ":" << natItem->getPort();
  162. packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe
  163. packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla
  164. natItem->increaseTimeout(5); // podbijam o kolejne 5 sekund skoro transmisja trwa
  165. this->print(ss.str());
  166. this->delay();
  167. this->send(*packet, true); // wysylam dalej
  168. natItem = NULL;
  169. }
  170. void NATRouter::setWanNetwork()
  171. {
  172. this->netWanConf.network = this->calculateNetwork(this->getWanIp(), this->getWanMask());
  173. }
  174. string NATRouter::getWanNetwork()
  175. {
  176. return this->netWanConf.network;
  177. }
  178. int NATRouter::getAllocatedPort(string srcIp, int srcPort)
  179. {
  180. map<string, int>::iterator it;
  181. int pos = -1;
  182. stringstream ss;
  183. ss << srcIp << ":" << srcPort;
  184. it = this->natHelper.find(ss.str());
  185. if (it != this->natHelper.end())
  186. {
  187. // sprawdz czy wpis o podanym IP z nat nie zostal napisany (referencja nieaktualna)
  188. if (this->natTable[it->second].getIp() != srcIp || this->natTable[it->second].getPort() != srcPort)
  189. this->natHelper.erase(it);
  190. else
  191. pos = it->second;
  192. }
  193. return pos;
  194. }
  195. void NATRouter::freePorts()
  196. {
  197. stringstream ss;
  198. int i, ports, peak=NAT_TABLE_LEN-1;
  199. Log portsLog;
  200. string name = "NAT";
  201. portsLog.setLogParams(3, GREEN, "\t\t");
  202. portsLog.setObjectName(&name);
  203. portsLog.setDelay(100000);
  204. while(true)
  205. {
  206. ports = 0;
  207. ss.str("");
  208. for (i=1; i<NAT_TABLE_LEN; ++i)
  209. if (this->natTable[i].isFree())
  210. ++ports;
  211. if (ports < peak)
  212. peak = ports;
  213. ss << "Free/Reserved:\t" << ports << " (" << peak << ")/" << (NAT_TABLE_LEN-ports-1) << " (" << (NAT_TABLE_LEN-peak-1) << ")"; // poprawka na nieuzywany port 0
  214. portsLog.print(ss.str());
  215. portsLog.printProgressBar(4, 4, "Utilization", (float)(NAT_TABLE_LEN-ports-1)/(NAT_TABLE_LEN-1));
  216. portsLog.delay();
  217. }
  218. }
  219. string NATRouter::getWanGatewayIp()
  220. {
  221. return this->netWanConf.gatewayIp;
  222. }