mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-31 19:24:33 +02:00
optimization DNS get function
This commit is contained in:
@@ -34,18 +34,20 @@ typedef struct {
|
|||||||
|
|
||||||
static int resolve_dns(const char *host, struct sockaddr_in *ip) {
|
static int resolve_dns(const char *host, struct sockaddr_in *ip) {
|
||||||
|
|
||||||
struct hostent *he;
|
const struct addrinfo hints = {
|
||||||
struct in_addr **addr_list;
|
.ai_family = AF_INET,
|
||||||
he = gethostbyname(host);
|
.ai_socktype = SOCK_STREAM,
|
||||||
if (he == NULL) {
|
};
|
||||||
return ESP_FAIL;
|
struct addrinfo *res;
|
||||||
}
|
|
||||||
addr_list = (struct in_addr **)he->h_addr_list;
|
int err = getaddrinfo(host, NULL, &hints, &res);
|
||||||
if (addr_list[0] == NULL) {
|
if(err != 0 || res == NULL) {
|
||||||
|
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
ip->sin_family = AF_INET;
|
ip->sin_family = AF_INET;
|
||||||
memcpy(&ip->sin_addr, addr_list[0], sizeof(ip->sin_addr));
|
memcpy(&ip->sin_addr, &((struct sockaddr_in *)(res->ai_addr))->sin_addr, sizeof(ip->sin_addr));
|
||||||
|
freeaddrinfo(res);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user