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.

124 lines
2.3 KiB

  1. /*
  2. * NATItem.cpp
  3. *
  4. * Created on: 10-01-2017
  5. * Author: Piotr Dergun
  6. */
  7. #include "NATRouter.h"
  8. NATRouter::NATRouter()
  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. int NATRouter::getFreePort()
  21. {
  22. for (int i=0; i<65536; ++i)
  23. if (this->natTable[i].isFree())
  24. return i;
  25. return -1; // nie ma żadnego wolnego portu
  26. }
  27. NATRouter::~NATRouter()
  28. {
  29. if (natTable)
  30. delete [] natTable;
  31. }
  32. void NATRouter::onRecv()
  33. {
  34. Packet p = this->recv();
  35. if (p.getSrcPort() != 0)
  36. {
  37. if (p.getDstIp() != this->getIp() && p.getDstIp() != this->getWanIp())
  38. {
  39. this->sNAT(&p);
  40. }
  41. else if (p.getDstIp() == this->getWanIp() && !this->natTable[p.getDstPort()].isFree())
  42. {
  43. this->dNAT(&p);
  44. }
  45. else
  46. {
  47. // TUTAJ ew. implementacja obslugi pakietu adresowanego do routera
  48. }
  49. }
  50. }
  51. void NATRouter::setWanIp(string wanIp)
  52. {
  53. this->netWanConf.ip = wanIp;
  54. }
  55. void NATRouter::setWanMask(string wanMask)
  56. {
  57. this->netWanConf.mask = wanMask;
  58. }
  59. void NATRouter::setWanGatewayIp(string wanGatewayIp)
  60. {
  61. this->netWanConf.gatewayIp = wanGatewayIp;
  62. }
  63. string NATRouter::getWanIp()
  64. {
  65. return this->netWanConf.ip;
  66. }
  67. string NATRouter::getWanMask()
  68. {
  69. return this->netWanConf.mask;
  70. }
  71. void NATRouter::sNAT(Packet *packet)
  72. {
  73. int port;
  74. NATItem *natItem;
  75. while ((port = this->getFreePort()) == -1)
  76. {
  77. //TESTOWO
  78. cout << "Brak wolnych portow, czekam 1s" << endl;
  79. sleep(1);
  80. }
  81. // wstawiam do tablicy NAT info ze port zarezerowany na odbior
  82. natItem = &this->natTable[port];
  83. natItem->setIp(packet->getSrcIp());
  84. natItem->setPort(packet->getSrcPort());
  85. packet->setSrcIp(this->getWanIp()); // zamieniam IP lokalne na WAN'owe
  86. packet->setSrcPort(port); // zamieniam port lokalny na WAN'owy - z tablicy NAT
  87. this->send(*packet, true); // wysylam dalej
  88. natItem = NULL;
  89. }
  90. void NATRouter::dNAT(Packet* packet)
  91. {
  92. NATItem *natItem = &this->natTable[packet->getDstPort()];
  93. packet->setDstIp(natItem->getIp()); // zamieniam IP lokalne na WAN'owe
  94. packet->setDstPort(natItem->getPort()); // zamieniam port NAT na lokalny wezla
  95. natItem->free(); // zwalniam port w routerze
  96. this->send(*packet, true); // wysylam dalej
  97. natItem = NULL;
  98. }
  99. string NATRouter::getWanGatewayIp()
  100. {
  101. return this->netWanConf.gatewayIp;
  102. }