【问题标题】:InetPton() converts any IP to 1.0.0.0InetPton() 将任何 IP 转换为 1.0.0.0
【发布时间】:2014-09-14 14:08:51
【问题描述】:

我正在尝试通过ICMP 向某个 IP 发送 ping,但 InetPton() 函数(假设将字符串 IP 转换为二进制形式)总是返回相同的 ip:"1.0.0.0 ".

我的代码如下所示:

short ip[4] = { 192, 168, 1, 2 };

bool checkIP() {
    HANDLE hIcmpFile;
    unsigned long ipaddr = INADDR_NONE;
    DWORD dwRetVal = 0;
    char SendData[32] = "Data Buffer";
    LPVOID ReplyBuffer = NULL;
    DWORD ReplySize = 0;

    std::wostringstream strIP;
    strIP << ip[0] << "." << ip[1] << "." << ip[2] << "." << ip[3];

    in_addr ipAddress;
    ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
    if (ipaddr != 1) 
    {
        SendMessage((HWND)StatusBar, (UINT)SB_SETTEXT, (WPARAM)(INT)1 | 0, (LPARAM)(LPSTR)TEXT("Invalid IP format!"));
        return false;
    }

    hIcmpFile = IcmpCreateFile();
    if (hIcmpFile == INVALID_HANDLE_VALUE) 
    {
        SendMessage((HWND)StatusBar, (UINT)SB_SETTEXT, (WPARAM)(INT)1 | 0, (LPARAM)(LPSTR)TEXT("Unable to open ICMP handle!"));
        return false;
    }

    ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
    ReplyBuffer = (VOID*)malloc(ReplySize);
    if (ReplyBuffer == NULL) 
    {
        SendMessage((HWND)StatusBar, (UINT)SB_SETTEXT, (WPARAM)(INT)1 | 0, (LPARAM)(LPSTR)TEXT("Unable to allocate memory!"));
        return false;
    }

    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 2000);
    if (dwRetVal != 0) 
    {
        PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
        struct in_addr ReplyAddr;
        ReplyAddr.S_un.S_addr = pEchoReply->Address;
        IcmpCloseHandle(hIcmpFile);
        return true;
    }
    else {
        IcmpCloseHandle(hIcmpFile);
        return false;
    }
    return true;
}

所以,我正在使用WireShark 分析网络,我可以看到 PING 总是发送到 1.0.0.0。我想问题出在InetPton()函数上,但不明白在哪里。

【问题讨论】:

    标签: c++ ip-address ping data-conversion icmp


    【解决方案1】:

    你的代码在这里似乎有点不对;

    in_addr* ipAddress;
    ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
    

    ipAddress 在这里是一个未初始化的指向 in_addr 的指针,你将一个指针传递给 InetPton。 InetPton 真正想要的是一个指向它可以填充的实际结构/缓冲区的指针;

    in_addr ipAddress;
    ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
    

    【讨论】:

    • 谢谢。现在,我可以看到 ipAddress 变量已正确填充,但看起来 PING 仍然发送到 1.0.0.0
    • @ali ipaddrInetPton的状态返回值,并不是实际的ip地址。你应该传递给IcmpSendEcho 的是ipAddress,而不是有点容易混淆的ipaddr
    • 是的。现在可以了。但仅限于本地网络。我有一个路由器,我想这就是原因。即使关闭了防火墙,我仍然无法从 Internet 上 PING IP
    • 您是否正在尝试 ping 您实际使用普通 ping 工具成功 ping 的 IP?
    【解决方案2】:
    IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 2000);
    

    应该是:

    IcmpSendEcho(hIcmpFile, ipAddress, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 2000);
    

    你的ipaddr是1,这可能是1.0.0.0地址的来源。

    【讨论】:

    • 当然,这就是问题所在。 ipAddress 也应该是IPAddr 类型。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2013-03-19
    • 2018-05-26
    • 2017-08-23
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多