首页 > 代码库 > DNS IPV6化——inet_ntop、inet_pton
DNS IPV6化——inet_ntop、inet_pton
我就奇了怪了,在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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。