1
0
ithewei 6 жил өмнө
parent
commit
1f1ccfab74
5 өөрчлөгдсөн 75 нэмэгдсэн , 8 устгасан
  1. 36 0
      base/hsocket.c
  2. 5 1
      base/hsocket.h
  3. 1 1
      event/nmap.cpp
  4. 2 0
      event/nmap.h
  5. 31 6
      examples/nmap.cpp

+ 36 - 0
base/hsocket.c

@@ -2,6 +2,10 @@
 #include "htime.h"
 #include "netinet.h"
 
+#ifdef OS_UNIX
+#include <sys/select.h>
+#endif
+
 char *socket_strerror(int err) {
 #ifdef OS_WIN
     static char buffer[128];
@@ -106,6 +110,38 @@ error:
     return socket_errno() > 0 ? -socket_errno() : -1;
 }
 
+int ConnectNonblock(const char* host, int port) {
+    return Connect(host, port, 1);
+}
+
+int ConnectTimeout(const char* host, int port, int ms) {
+    int connfd = Connect(host, port, 1);
+    if (connfd < 0) return connfd;
+    int err;
+    socklen_t optlen = sizeof(err);
+    struct timeval tv = {ms/1000, (ms%1000)*1000};
+    fd_set writefds;
+    FD_ZERO(&writefds);
+    FD_SET(connfd, &writefds);
+    int ret = select(connfd, 0, &writefds, 0, &tv);
+    if (ret < 0) {
+        perror("select");
+        goto error;
+    }
+    if (ret == 0) {
+        errno = ETIMEDOUT;
+        goto error;
+    }
+    if (getsockopt(connfd, SOL_SOCKET, SO_ERROR, (char*)&err, &optlen) < 0 || err != 0) {
+        goto error;
+    }
+    blocking(connfd);
+    return connfd;
+error:
+    closesocket(connfd);
+    return socket_errno() > 0 ? -socket_errno() : -1;
+}
+
 #define PING_TIMEOUT    1000 // ms
 int Ping(const char* host, int cnt) {
     static uint16_t seq = 0;

+ 5 - 1
base/hsocket.h

@@ -32,9 +32,13 @@ int Listen(int port);
 // @retval 0:succeed
 int Resolver(const char* host, struct sockaddr* addr);
 
-// Resolver -> socket -> nonblocking -> connect
 // @return sockfd
+// Resolver -> socket -> nonblocking -> connect
 int Connect(const char* host, int port, int nonblock DEFAULT(0));
+// Connect(host, port, 1)
+int ConnectNonblock(const char* host, int port);
+// Connect(host, port, 1) -> select -> blocking
+int ConnectTimeout(const char* host, int port, int ms);
 
 // @param cnt: ping count
 // @return: ok count

+ 1 - 1
event/nmap.cpp

@@ -154,7 +154,7 @@ int host_discovery(const char* segment24, Nmap* nmap) {
     p[1] = atoi(strlist[1].c_str());
     p[2] = atoi(strlist[2].c_str());
     printd("Nmap scan %u.%u.%u.x...\n", p[0], p[1], p[2]);
-    // 0,256 reserved
+    // 0,255 reserved
     nmap->clear();
     for (int i = 1; i < 255; ++i) {
         p[3] = i;

+ 2 - 0
event/nmap.h

@@ -3,6 +3,8 @@
 
 #include <map>
 #include "hsocket.h"
+
+// addr => 0:down 1:up
 typedef std::map<uint32_t, int> Nmap;
 
 // ip = segment + host

+ 31 - 6
examples/nmap.cpp

@@ -4,10 +4,12 @@
 #include "nmap.h"
 #include "hthreadpool.h"
 
+/*
 int host_discovery_task(std::string segment, void* nmap) {
     Nmap* hosts= (Nmap*)nmap;
     return host_discovery(segment.c_str(), hosts);
 }
+*/
 
 int main(int argc, char* argv[]) {
     if (argc < 2) {
@@ -31,17 +33,38 @@ int main(int argc, char* argv[]) {
 
     if (n == 24) {
         Nmap nmap;
-        int ups = host_discovery(segment, &nmap);
-        return 0;
+        return host_discovery(segment, &nmap);
     }
 
     char ip[INET_ADDRSTRLEN];
     if (n == 16) {
-        Nmap nmap;
-        int up_nsegs = segment_discovery(segment, &nmap);
+        Nmap segs;
+        int up_nsegs = segment_discovery(segment, &segs);
         if (up_nsegs == 0) return 0;
+        Nmap hosts;
+        for (auto& pair : segs) {
+            if (pair.second == 1) {
+                uint32_t addr = pair.first;
+                uint8_t* p = (uint8_t*)&addr;
+                // 0,255 reserved
+                for (int i = 1; i < 255; ++i) {
+                    p[3] = i;
+                    hosts[addr] = 0;
+                }
+            }
+        }
+        nmap_discovery(&hosts);
+        // filter up hosts
+        std::vector<uint32_t> up_hosts;
+        for (auto& pair : hosts) {
+            if (pair.second == 1) {
+                up_hosts.push_back(pair.first);
+            }
+        }
+        // ThreadPool + host_discovery
+        /*
         if (up_nsegs == 1) {
-            for (auto& pair : nmap) {
+            for (auto& pair : segs) {
                 if (pair.second == 1) {
                     inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
                     Nmap hosts;
@@ -78,12 +101,14 @@ int main(int argc, char* argv[]) {
             }
         }
         delete[] hosts;
+        */
         // print up hosts
-        printf("Up hosts %d:\n", nhosts);
+        printf("Up hosts %lu:\n", up_hosts.size());
         for (auto& host : up_hosts) {
             inet_ntop(AF_INET, (void*)&host, ip, sizeof(ip));
             printf("%s\n", ip);
         }
+        return up_hosts.size();
     }
 
     return 0;