InetPton()可以将任何IP转换为1.0.0.0 [英] InetPton() converts any IP to 1.0.0.0

查看:158
本文介绍了InetPton()可以将任何IP转换为1.0.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 ICMP ,但是InetPton()函数(假定将字符串IP转换为二进制形式)始终返回相同的ip:"1.0.0.0" .

I am trying to send a ping to some IP through ICMP but the InetPton() function, which is suppose to convert string IPs into a binary form, is always returning the same 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()函数,但是不知道在哪里.

So, I am analyzing the network with WireShark and I can see that the PING is always sent to 1.0.0.0. I suppose that the problem lies in the InetPton() function, but don't understand where.

推荐答案

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地址的来源.

Your ipaddr is 1, probably this is the source of 1.0.0.0 address.

这篇关于InetPton()可以将任何IP转换为1.0.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆