replace gethostbyname with getaddrinfo
This commit is contained in:
parent
5e9986d608
commit
7ba8c23212
|
@ -24,25 +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 hostent * record = gethostbyname(fqdn);
|
struct addrinfo hints = {0};
|
||||||
if(record == NULL) return -1;
|
struct addrinfo *result = NULL;
|
||||||
return ((struct in_addr *)record->h_addr)->s_addr;
|
|
||||||
|
getaddrinfo(fqdn, NULL, &hints, &result);
|
||||||
|
if (result) {
|
||||||
|
struct sockaddr *sa = result->ai_addr;
|
||||||
|
struct sockaddr_in *si = (struct sockaddr_in*)sa;
|
||||||
|
struct in_addr ia = si->sin_addr;
|
||||||
|
uint32_t ip = ia.s_addr;
|
||||||
|
freeaddrinfo(result);
|
||||||
|
return ip;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function converting an IP address string to an unsigned int.
|
// Function converting an IP address string to an unsigned int.
|
||||||
|
|
Loading…
Reference in New Issue