1
0

nmap.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "nmap.h"
  4. #include "hthreadpool.h"
  5. int host_discover_task(std::string segment, void* nmap) {
  6. Nmap* hosts= (Nmap*)nmap;
  7. return host_discover(segment.c_str(), hosts);
  8. }
  9. int main(int argc, char* argv[]) {
  10. if (argc < 2) {
  11. printf("Usage: nmap segment\n");
  12. printf("Examples: nmap 192.168.1.123\n");
  13. printf(" nmap 192.168.1.x/24\n");
  14. printf(" nmap 192.168.x.x/16\n");
  15. return -1;
  16. }
  17. char* segment = argv[1];
  18. char* split = strchr(segment, '/');
  19. int n = 24;
  20. if (split) {
  21. *split = '\0';
  22. n = atoi(split+1);
  23. if (n != 24 && n != 16) {
  24. return -2;
  25. }
  26. }
  27. Nmap hosts;
  28. char ip[INET_ADDRSTRLEN];
  29. if (n == 24) {
  30. host_discover(segment, &hosts);
  31. }
  32. else if (n == 16) {
  33. Nmap segs;
  34. int up_nsegs = segment_discover(segment, &segs);
  35. if (up_nsegs == 0) return 0;
  36. #if 1
  37. for (auto& pair : segs) {
  38. if (pair.second == 1) {
  39. uint32_t addr = pair.first;
  40. uint8_t* p = (uint8_t*)&addr;
  41. // 0,255 reserved
  42. for (int i = 1; i < 255; ++i) {
  43. p[3] = i;
  44. hosts[addr] = 0;
  45. }
  46. }
  47. }
  48. nmap_discover(&hosts);
  49. #else
  50. if (up_nsegs == 1) {
  51. for (auto& pair : segs) {
  52. if (pair.second == 1) {
  53. inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
  54. host_discover(ip, &hosts);
  55. }
  56. }
  57. }
  58. else {
  59. // ThreadPool + host_discover
  60. Nmap* hosts = new Nmap[up_nsegs];
  61. // use ThreadPool
  62. HThreadPool tp(4);
  63. tp.start();
  64. std::vector<std::future<int>> futures;
  65. int i = 0;
  66. for (auto& pair : segs) {
  67. if (pair.second == 1) {
  68. inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
  69. auto future = tp.commit(host_discover_task, std::string(ip), &hosts[i++]);
  70. futures.push_back(std::move(future));
  71. }
  72. }
  73. // wait all task done
  74. int nhosts = 0;
  75. for (auto& future : futures) {
  76. nhosts += future.get();
  77. }
  78. // filter up hosts
  79. std::vector<uint32_t> up_hosts;
  80. for (int i = 0; i < up_nsegs; ++i) {
  81. Nmap& nmap = hosts[i];
  82. for (auto& host : nmap) {
  83. if (host.second == 1) {
  84. up_hosts.push_back(host.first);
  85. }
  86. }
  87. }
  88. delete[] hosts;
  89. }
  90. #endif
  91. }
  92. // filter up hosts
  93. std::vector<uint32_t> up_hosts;
  94. for (auto& pair : hosts) {
  95. if (pair.second == 1) {
  96. up_hosts.push_back(pair.first);
  97. }
  98. }
  99. // print up hosts
  100. printf("Up hosts %lu:\n", up_hosts.size());
  101. for (auto& host : up_hosts) {
  102. inet_ntop(AF_INET, (void*)&host, ip, sizeof(ip));
  103. printf("%s\n", ip);
  104. }
  105. return 0;
  106. }