main.cpp 3.2 KB

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