未验证 提交 54e947e3 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #2170 from freemine/with-getaddrinfo

replace gethostbyname with getaddrinfo
...@@ -24,43 +24,37 @@ int taosGetFqdn(char *fqdn) { ...@@ -24,43 +24,37 @@ int taosGetFqdn(char *fqdn) {
hostname[1023] = '\0'; hostname[1023] = '\0';
gethostname(hostname, 1023); gethostname(hostname, 1023);
struct hostent* h; struct addrinfo hints = {0};
h = gethostbyname(hostname); struct addrinfo *result = NULL;
if (h != NULL) {
strcpy(fqdn, h->h_name); hints.ai_flags = AI_CANONNAME;
getaddrinfo(hostname, NULL, &hints, &result);
if (result) {
strcpy(fqdn, result->ai_canonname);
freeaddrinfo(result);
} else { } else {
uError("failed to get host name(%s)", strerror(errno));
code = -1; code = -1;
} }
// to do: free the resources
// free(h);
return code; return code;
} }
uint32_t taosGetIpFromFqdn(const char *fqdn) { uint32_t taosGetIpFromFqdn(const char *fqdn) {
struct addrinfo hints, *servinfo, *p; struct addrinfo hints = {0};
struct sockaddr_in *h; struct addrinfo *result = NULL;
uint32_t ip = -1;
getaddrinfo(fqdn, NULL, &hints, &result);
memset(&hints, 0, sizeof hints); if (result) {
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 struct sockaddr *sa = result->ai_addr;
hints.ai_socktype = SOCK_STREAM; struct sockaddr_in *si = (struct sockaddr_in*)sa;
struct in_addr ia = si->sin_addr;
if (getaddrinfo(fqdn, "http", &hints, &servinfo) != 0) { uint32_t ip = ia.s_addr;
uError("failed to get IP from %s(%s)", fqdn, strerror(errno)); freeaddrinfo(result);
return ip;
} else {
return -1; return -1;
} }
// to do: loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
h = (struct sockaddr_in *) p->ai_addr;
ip = h->sin_addr.s_addr;
}
freeaddrinfo(servinfo); // all done with this structure
return ip;
} }
// Function converting an IP address string to an unsigned int. // Function converting an IP address string to an unsigned int.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册