如何修改PADDRINFOA结构的ip

时间:2017-05-27 17:15:23

标签: c++

我挂了 getaddrinfo 函数,我想修改 ppResult 的ip。

f_getaddrinfo(PCSTR pNodeName, PCSTR pServiceName, const ADDRINFOA  *pHints, PADDRINFOA *ppResult) {

    // REAL FUNCTION
    r_getaddrinfo(pNodeName,pServiceName,pHints,ppResult);

    // FAIL
    sockaddr_in addrIn;
    inet_pton(AF_INET, "127.0.0.1", &addrIn.sin_addr);
    const_cast<PADDRINFOA>(*ppResult)->ai_addr = (struct sockaddr*)&addrIn.sin_addr;

    // FAIL
    inet_pton(AF_INET, "127.0.0.1", const_cast<PADDRINFOA>(*ppResult)->ai_addr);
}

非常感谢

1 个答案:

答案 0 :(得分:0)

首先不要使用PADDRINFOA* !!! PADDRINFOA已经是ADDRINFOA的指针,你现在实际上有双指针。实际上坚持可移植的struct addrinfo*

无论如何试试这个:

PADDRINFOA result = *ppResult;  // Should not do that, ppResult should be PADDRINFOA
struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(result->ai_addr);
inet_pton(AF_INET, "127.0.0.1", &(addr->sin_addr));