码迷,mamicode.com
首页 > Web开发 > 详细

DNS IPV6化——inet_ntop、inet_pton

时间:2014-12-26 11:16:44      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:网络编程   ipv4   ipv6   windows   

      我就奇了怪了,在Win32工程里导入#include <WS2tcpip.h>后Inet_ntop()函数的使用毫无压力,但在DLL工程里却报错:     IntelliSense: identifier "inet_ntop" is undefined,实在是搞不懂,有懂的朋友可以告诉我哈。现在急着用,只有用自己的函数实现。


Inet_ntop():converts an IPv4 or IPv6 Internet network address into a string in Internet standard format.

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
    if (af == AF_INET)
    {
        struct sockaddr_in in;
        memset(&in, 0, sizeof(in));
        in.sin_family = AF_INET;
        memcpy(&in.sin_addr, src, sizeof(struct in_addr));
        getnameinfo((struct sockaddr *)&in, sizeof(struct
            sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
        return dst;
    }
    else if (af == AF_INET6)
    {
        struct sockaddr_in6 in;
        memset(&in, 0, sizeof(in));
        in.sin6_family = AF_INET6;
        memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
        getnameinfo((struct sockaddr *)&in, sizeof(struct
            sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
        return dst;
    }
    return NULL;
}


Inet_pton():converts an IPv4 or IPv6 Internet network address in its standard text presentation form into its numeric binary form.

int inet_pton(int af, const char *src, void *dst)
{
    struct addrinfo hints, *res, *ressave;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = af;

    if (getaddrinfo(src, NULL, &hints, &res) != 0)
    {
// Commented out because dolog and LOG_ERR are not defined...
//        dolog(LOG_ERR, "Couldn't resolve host %s\n", src);
        return -1;
    }

    ressave = res;

    while (res)
    {
        memcpy(dst, res->ai_addr, res->ai_addrlen);
        res = res->ai_next;
    }

    freeaddrinfo(ressave);
    return 0;
}

未完待续!

DNS IPV6化——inet_ntop、inet_pton

标签:网络编程   ipv4   ipv6   windows   

原文地址:http://blog.csdn.net/a491057947/article/details/42167655

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!