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.

334 lines
7.6 KiB

  1. /*
  2. * Simulation.cpp
  3. *
  4. * Created on: 16.01.2017
  5. * Author: piotrek
  6. */
  7. #include "Simulation.h"
  8. #include "Node.h"
  9. #include "NATRouter.h"
  10. #include "Peer.h"
  11. #include "P2PServer.h"
  12. Simulation::Simulation()
  13. {
  14. srand(time(NULL));
  15. this->rows = 0;
  16. this->cols = 0;
  17. #ifndef DEBUG
  18. // inicjalizacja ncurses
  19. initscr();
  20. getmaxyx(stdscr, this->rows, this->cols);
  21. if (has_colors())
  22. {
  23. start_color();
  24. init_pair(0, COLOR_BLACK, COLOR_WHITE);
  25. init_pair(1, COLOR_RED, COLOR_BLACK);
  26. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  27. init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  28. init_pair(4, COLOR_BLUE, COLOR_BLACK);
  29. init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
  30. init_pair(6, COLOR_CYAN, COLOR_BLACK);
  31. init_pair(7, COLOR_WHITE, COLOR_BLACK);
  32. }
  33. #endif
  34. this->name = "Simulation";
  35. this->setLineNumber(rows -1);
  36. this->setDelimiter("\t");
  37. this->setObjectName(&this->name);
  38. this->setColor(WHITE);
  39. this->startTime = time(NULL);
  40. #ifndef DEBUG
  41. this->createThread("resizeWnd", SIM_RESIZE, this);
  42. #endif
  43. }
  44. Simulation::~Simulation()
  45. {
  46. //niestety nie dziala...
  47. endwin();
  48. }
  49. void Simulation::createThread(string name, THREAD_TYPE type, void* context)
  50. {
  51. struct threadParams params;
  52. params.type = type;
  53. params.context = context;
  54. this->threads.insert(pair<string, struct threadParams>(name, params));
  55. map<string, struct threadParams>::iterator it = threads.find(name);
  56. if (pthread_create(&it->second.thread_id, NULL, Simulation::threadWrapper, &it->second) == -1)
  57. {
  58. cerr << "pthread_create() failed" << endl;
  59. exit(1);
  60. }
  61. this->threads.insert(pair<string, struct threadParams>(name, params));
  62. usleep(3000); // 3ms
  63. }
  64. /*
  65. * przy niszczeniu watku niszczy sie ncurses :(
  66. * TODO poszukac rozwiazania w miare mozliwosci
  67. */
  68. void Simulation::destroyThread(string name)
  69. {
  70. void * state;
  71. map<string, struct threadParams>::iterator it = threads.find(name);
  72. if (it == threads.end())
  73. {
  74. cerr << "Thread: " << name << " does not exist!" << endl;
  75. exit(1);
  76. }
  77. if (pthread_cancel(it->second.thread_id) != 0)
  78. {
  79. cerr << "pthread_cancel() failed" << endl;
  80. exit(1);
  81. }
  82. if (pthread_join(it->second.thread_id, &state) == -1)
  83. {
  84. cerr << "pthread_cancel() failed" << endl;
  85. exit(1);
  86. }
  87. if (pthread_detach(it->second.thread_id) == -1)
  88. {
  89. cerr << "pthread_detach() failed" << endl;
  90. exit(1);
  91. }
  92. threads.erase(it);
  93. }
  94. /*
  95. * wrapper, w ktory trzeba opakowac metody do zastosowania w watku
  96. */
  97. void * Simulation::threadWrapper(void * context)
  98. {
  99. struct threadParams *params = (struct threadParams*)context;
  100. switch(params->type)
  101. {
  102. case NODE_RECV:
  103. ((Node *)params->context)->onRecv();
  104. break;
  105. case SIM_TIMER:
  106. ((Simulation *)params->context)->timer();
  107. break;
  108. case SIM_RESIZE:
  109. ((Simulation *)params->context)->resizeWnd();
  110. break;
  111. case NAT_FP:
  112. ((NATRouter *)params->context)->freePorts();
  113. break;
  114. }
  115. return 0;
  116. }
  117. void Simulation::resizeWnd()
  118. {
  119. while(true)
  120. {
  121. #ifndef DEBUG
  122. getmaxyx(stdscr, this->rows, this->cols);
  123. if (this->getLineNumber() != this->rows-1)
  124. this->setLineNumber(rows -1);
  125. //refresh();
  126. #endif
  127. }
  128. }
  129. void Simulation::timer()
  130. {
  131. #ifndef DEBUG
  132. char str[10];
  133. int h,m, s;
  134. unsigned long timeElapsed;
  135. pthread_mutex_t *mutex;
  136. #endif
  137. while(true)
  138. {
  139. #ifndef DEBUG
  140. refresh();
  141. timeElapsed = time(NULL) - this->startTime;
  142. h = timeElapsed / 3600;
  143. m = (timeElapsed % 3600) / 60;
  144. s = (timeElapsed % 3600) % 60;
  145. mutex = Log::getMutex();
  146. pthread_mutex_lock(mutex);
  147. sprintf(str, "%02d:%02d:%02d", h, m, s);
  148. move(0, this->cols-strlen(str));
  149. printw(str);
  150. pthread_mutex_unlock(mutex);
  151. #endif
  152. usleep(100000); // 100 ms
  153. //sleep(1);
  154. }
  155. }
  156. void Simulation::p2pSimulation()
  157. {
  158. this->print("Creating P2P peers");
  159. Peer seed("Seed", "192.168.1.2", "255.255.255.0", "192.168.1.1");
  160. seed.setSender(true);
  161. seed.setSim(this);
  162. seed.setLogParams(0, GREEN, "\t\t");
  163. Peer peer("Peer", "10.0.0.2", "255.255.255.0", "10.0.0.1");
  164. peer.setSim(this);
  165. peer.setLogParams(4, CYAN, "\t\t");
  166. seed.print("IP 192.168.1.2/24, Gateway: 192.168.1.1");
  167. peer.print("IP 10.0.0.2/24, Gateway: 10.0.0.1");
  168. sleep(1);
  169. this->print("Creating NAT devices");
  170. NATRouter r1("Router 1", "192.168.1.1", "255.255.255.0");
  171. NATRouter r2("Router 2", "10.0.0.1", "255.255.255.0");
  172. r1.setLogParams(1, RED, "\t");
  173. r2.setLogParams(3, MAGENTA, "\t");
  174. r1.print("IP 192.168.1.1/24");
  175. r2.print("IP 10.0.0.1/24");
  176. sleep(1);
  177. this->print("Setting WAN configuration");
  178. r1.setWanIp("41.42.43.44");
  179. r1.setWanMask("255.255.255.255");
  180. r2.setWanIp("45.46.47.48");
  181. r2.setWanMask("255.255.255.255");
  182. r1.print("WAN IP 41.42.43.44/32");
  183. r2.print("WAN IP 45.46.47.48/32");
  184. sleep(1);
  185. this->print("Creating P2P server");
  186. P2PServer server("Server", "80.80.90.91", "255.255.255.255");
  187. server.setSim(this);
  188. server.setLogParams(2, YELLOW, "\t\t");
  189. server.print("IP 80.80.90.91/32");
  190. sleep(1);
  191. this->print("Plugging Seed <---> Router 1");
  192. r1.connectNode(&seed);
  193. usleep(600000);
  194. this->print("Plugging Peer <---> Router 2");
  195. r2.connectNode(&peer);
  196. usleep(600000);
  197. this->print("Plugging Router 1 <---> Router 2");
  198. r1.connectNode(&r2, true);
  199. usleep(600000);
  200. this->print("Plugging Server <---> Router 1");
  201. server.connectNode(&r1);
  202. usleep(600000);
  203. this->print("Plugging Server <---> Router 2");
  204. server.connectNode(&r2);
  205. usleep(600000);
  206. this->print("Starting simulation...");
  207. sleep(1);
  208. this->createThread("r1", NODE_RECV, &r1);
  209. this->createThread("r2", NODE_RECV, &r2);
  210. this->createThread("s1", NODE_RECV, &server);
  211. this->print("Connecting peers to P2P server");
  212. seed.connectToServer("80.80.90.91", 6565);
  213. peer.connectToServer("80.80.90.91", 6565);
  214. sleep(3);
  215. this->createThread("p1", NODE_RECV, &seed);
  216. this->createThread("p2", NODE_RECV, &peer);
  217. while(true);
  218. }
  219. void Simulation::natOverflowSimulation(int nNodes)
  220. {
  221. int i = 0;//, srcPort;
  222. stringstream ss;
  223. this->print("Creating server");
  224. Node server("Server", "93.92.91.90", "255.255.255.255");
  225. server.setLogParams(0, BLUE, "\t\t");
  226. server.print("IP 93.92.91.90/32");
  227. sleep(1);
  228. this->print("Creating NAT device");
  229. NATRouter router("Router", "10.13.12.254", "255.255.255.0");
  230. router.setLogParams(1, YELLOW, "\t\t");
  231. router.setDelay(0);
  232. router.print("IP 10.13.12.254/24", true);
  233. sleep(1);
  234. this->print("Setting WAN configuration");
  235. router.setWanIp("80.55.33.12");
  236. router.setWanMask("255.255.255.255");
  237. router.print("WAN IP 80.55.33.12/32", true);
  238. sleep(1);
  239. this->print("Creating and setting clients");
  240. Node *client = new Node[nNodes];
  241. int *currentSrcPort = new int[nNodes];
  242. for (i = 0; i < nNodes; ++i)
  243. {
  244. currentSrcPort[i] = 1;
  245. client[i].setLogParams(2, CYAN, "\t");
  246. ss.str("");
  247. ss << "Client " << (i + 1);
  248. client[i].setHostname(ss.str());
  249. ss.str("");
  250. ss << "10.13.12." << (i + 1);
  251. client[i].setIp(ss.str());
  252. client[i].setMask("255.255.255.0");
  253. client[i].setGatewayIp("10.13.12.254");
  254. ss.str("");
  255. ss << "IP 10.13.12." << (i + 1) << "/24, Gateway: 10.0.0.1";
  256. client[i].print(ss.str());
  257. usleep(12500);
  258. }
  259. for (i = 0; i < nNodes; ++i)
  260. {
  261. ss.str("");
  262. ss << "Plugging Client " << (i + 1) << " <---> Router";
  263. this->print(ss.str());
  264. router.connectNode(&client[i]);
  265. usleep(12500);
  266. }
  267. this->print("Plugging Server <---> Router");
  268. server.connectNode(&router);
  269. usleep(600000);
  270. this->print("Starting simulation...");
  271. sleep(2);
  272. this->createThread("router", NODE_RECV, &router);
  273. this->createThread("server", NODE_RECV, &server);
  274. this->createThread("nat", NAT_FP, &router);
  275. Packet p("test packet");
  276. p.setDstIp("93.92.91.90");
  277. p.setDstPort(80);
  278. i=0;
  279. while(true)
  280. {
  281. i = (nNodes>1) ? rand()%nNodes : 0;
  282. if (currentSrcPort[i] == 65535)
  283. currentSrcPort[i] = 1;
  284. //p.setSrcPort(currentSrcPort[i]++);
  285. p.setSrcPort(0);
  286. client[i].send(p);
  287. this->delay(150);
  288. //usleep(1);
  289. }
  290. }