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.

152 lines
3.0 KiB

  1. /*
  2. * NATItem.cpp
  3. *
  4. * Created on: 10-01-2017
  5. * Author: Piotr Dergun
  6. */
  7. #include "NATRouter.h"
  8. void NATRouter::initalizeNatTable()
  9. {
  10. try
  11. {
  12. natTable = new NATItem[65536];
  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. for (int i=1; i<65536; ++i)
  39. if (this->natTable[i].isFree())
  40. return i;
  41. return -1; // nie ma żadnego wolnego portu
  42. }
  43. NATRouter::~NATRouter()
  44. {
  45. if (natTable)
  46. delete [] natTable;
  47. }
  48. void NATRouter::onRecv()
  49. {
  50. Packet p = this->recv();
  51. // TODO: jako ze router jest tez switchem, zaimplementowac wysylanie wiadomosci po sieci lokalnej
  52. if (p.getSrcPort() != 0)
  53. {
  54. // jezeli pakiet idzie z jednego wezla do drugiego w danej possieci to odeslij mu "jak switch"
  55. if (this->calculateNetwork(p.getDstIp(), this->getMask()) == this->getNetwork())
  56. {
  57. this->send(p, true);
  58. }
  59. else if (p.getDstIp() != this->getIp() && p.getDstIp() != this->getWanIp())
  60. {
  61. this->sNAT(&p);
  62. }
  63. else if (p.getDstIp() == this->getWanIp() && !this->natTable[p.getDstPort()].isFree())
  64. {
  65. this->dNAT(&p);
  66. }
  67. else
  68. {
  69. // TUTAJ ew. implementacja obslugi pakietu adresowanego do routera
  70. }
  71. }
  72. }
  73. void NATRouter::setWanIp(string wanIp)
  74. {
  75. this->netWanConf.ip = wanIp;
  76. }
  77. void NATRouter::setWanMask(string wanMask)
  78. {
  79. this->netWanConf.mask = wanMask;
  80. }
  81. void NATRouter::setWanGatewayIp(string wanGatewayIp)
  82. {
  83. this->netWanConf.gatewayIp = wanGatewayIp;
  84. }
  85. string NATRouter::getWanIp()
  86. {
  87. return this->netWanConf.ip;
  88. }
  89. string NATRouter::getWanMask()
  90. {
  91. return this->netWanConf.mask;
  92. }
  93. void NATRouter::sNAT(Packet *packet)
  94. {
  95. int port;
  96. NATItem *natItem;
  97. while ((port = this->getFreePort()) == -1)
  98. {
  99. //TESTOWO
  100. cout << "Brak wolnych portow, czekam 1s" << endl;
  101. sleep(1);
  102. }
  103. // wstawiam do tablicy NAT info ze port zarezerowany na odbior
  104. natItem = &this->natTable[port];
  105. natItem->setIp(packet->getSrcIp());
  106. natItem->setPort(packet->getSrcPort());
  107. natItem->setTimeout(5);
  108. packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
  109. packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
  110. this->send(*packet, true); // wysylam dalej
  111. natItem = NULL;
  112. }
  113. void NATRouter::dNAT(Packet* packet)
  114. {
  115. NATItem *natItem = &this->natTable[packet->getDstPort()];
  116. packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe
  117. packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla
  118. natItem->free(); // zwalniam port w routerze
  119. this->send(*packet, true); // wysylam dalej
  120. natItem = NULL;
  121. }
  122. string NATRouter::getWanGatewayIp()
  123. {
  124. return this->netWanConf.gatewayIp;
  125. }