1
0

nmap.cpp 3.1 KB

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