C++ 端口扫描器

标签 c++ sockets

我正在尝试用 C++ 做一个端口扫描器,这样我就可以从我网络中打开了某个端口的某些设备获取 IP 地址。我实现了超时,因为当我测试我网络中的每个 IP 地址时,如果我在一段时间内没有收到响应,它会自动关闭连接。

如果我将此超时设置为大约 30 秒,它只会检测到所有设备已关闭,如果我设置更大的值,它会挂起并且永远不会完成。

#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string>

using namespace std;    

static bool port_is_open(string ip, int port){

    struct sockaddr_in address;  /* the libc network address data structure */
    short int sock = -1;         /* file descriptor for the network socket */
    fd_set fdset;
    struct timeval tv;

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr(ip.c_str()); /* assign the address */
    address.sin_port = htons(port);

    /* translate int2port num */
    sock = socket(AF_INET, SOCK_STREAM, 0);
    fcntl(sock, F_SETFL, O_NONBLOCK);

    connect(sock, (struct sockaddr *)&address, sizeof(address));

    FD_ZERO(&fdset);
    FD_SET(sock, &fdset);
    tv.tv_sec = 0;             /* timeout */
    tv.tv_usec = 50;

    if (select(sock + 1, NULL, &fdset, NULL, &tv) == 1)
    {
        int so_error;
        socklen_t len = sizeof so_error;

        getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);

        if (so_error == 0){
            close(sock);
            return true;
        }else{
            close(sock);
            return false;
        }
    }        
    return false;
}


int main(int argc, char **argv){    

    int i=1;        
    int port = 22;        
    while (i<255) {            
        string ip = "10.0.60.";                        
        std::string host = std::to_string(i);
        ip.append(host);            
        if (port_is_open(ip, port)){                
            printf("%s:%d is open\n", ip.c_str(), port);                
        }            
        i++;
    }           
    return 0;        
}

最佳答案

您可以将您的逻辑包装到异步调用中并以合理的超时并行启动(例如 10 秒,因为 30us 在标准条件下毫无意义)。线程会将您的程序加速大约 255 倍,并且在最坏的情况下,它会在发生此超时后立即完成:

...
#include <iostream>
#include <thread>
#include <vector>
#include <sstream>
...

void task(std::string ip, int port){
    if (port_is_open(ip, port))
        cout << ip << ":" << port << " is open\n";
}

int main(int argc, char **argv){        
    const std::string ip_prefix = "10.0.60.";
    const int port = 22;
    std::vector<std::thread *> tasks;

    for (int i=0; i<255; i++){      
        std::ostringstream ip;
        ip << ip_prefix << i;
        tasks.push_back(new std::thread(task, ip.str(), port));
    }
    for (int i=0; i<255; i++){
        tasks[i]->join();
        delete tasks[i];
    }
    return 0;
}

您可能希望像这样编译它:g++ -std=c++11g++ -std=c++0x -pthread(对于较旧的 GCC) .

关于C++ 端口扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636558/

相关文章:

c - Linux 原始套接字 - ip header 中的字节顺序

c++ - 调用者保存的寄存器和被调用者保存的寄存器有什么区别?

c++ - a 到 z 的键盘字母在 Qt 的 keyPressEvent 中不起作用

c++ - sizeof 运算符在运行时发生变化?

c++ - 测试 IPv6 应用程序

.net - .net使用ReceiveAsync检测断开连接

c++ - 是什么导致我的 OpenCV 内存泄漏

python - 使用 boost::numpy 时出现 LNK2001 错误

c++ - OpenSSL 套接字 : Select always returns 0

java - FTP客户端/服务器登录验证