nmap.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "nmap.h"
  4. #include "hthreadpool.h"
  5. /*
  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. */
  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. if (n == 24) {
  30. Nmap nmap;
  31. return host_discover(segment, &nmap);
  32. }
  33. char ip[INET_ADDRSTRLEN];
  34. if (n == 16) {
  35. Nmap segs;
  36. int up_nsegs = segment_discover(segment, &segs);
  37. if (up_nsegs == 0) return 0;
  38. Nmap hosts;
  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. // filter up hosts
  52. std::vector<uint32_t> up_hosts;
  53. for (auto& pair : hosts) {
  54. if (pair.second == 1) {
  55. up_hosts.push_back(pair.first);
  56. }
  57. }
  58. // ThreadPool + host_discover
  59. /*
  60. if (up_nsegs == 1) {
  61. for (auto& pair : segs) {
  62. if (pair.second == 1) {
  63. inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
  64. Nmap hosts;
  65. return host_discover(ip, &hosts);
  66. }
  67. }
  68. }
  69. Nmap* hosts = new Nmap[up_nsegs];
  70. // use ThreadPool
  71. HThreadPool tp(4);
  72. tp.start();
  73. std::vector<std::future<int>> futures;
  74. int i = 0;
  75. for (auto& pair : nmap) {
  76. if (pair.second == 1) {
  77. inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
  78. auto future = tp.commit(host_discover_task, std::string(ip), &hosts[i++]);
  79. futures.push_back(std::move(future));
  80. }
  81. }
  82. // wait all task done
  83. int nhosts = 0;
  84. for (auto& future : futures) {
  85. nhosts += future.get();
  86. }
  87. // filter up hosts
  88. std::vector<uint32_t> up_hosts;
  89. for (int i = 0; i < up_nsegs; ++i) {
  90. Nmap& nmap = hosts[i];
  91. for (auto& host : nmap) {
  92. if (host.second == 1) {
  93. up_hosts.push_back(host.first);
  94. }
  95. }
  96. }
  97. delete[] hosts;
  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 up_hosts.size();
  106. }
  107. return 0;
  108. }