59#if LWIP_DNS && LWIP_SOCKET
72struct gethostbyname_r_helper {
73 ip_addr_t *addr_list[2];
79#if LWIP_DNS_API_DECLARE_H_ERRNO
85#ifndef LWIP_DNS_API_HOSTENT_STORAGE
86#define LWIP_DNS_API_HOSTENT_STORAGE 0
90#if LWIP_DNS_API_HOSTENT_STORAGE
91#define HOSTENT_STORAGE
93#define HOSTENT_STORAGE static
106lwip_gethostbyname(
const char *name)
112 HOSTENT_STORAGE
struct hostent s_hostent;
113 HOSTENT_STORAGE
char *s_aliases;
114 HOSTENT_STORAGE ip_addr_t s_hostent_addr;
115 HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
119 err = netconn_gethostbyname(name, &addr);
122 h_errno = HOST_NOT_FOUND;
127 s_hostent_addr = addr;
128 s_phostent_addr[0] = &s_hostent_addr;
129 s_phostent_addr[1] = NULL;
132 s_hostent.h_name = s_hostname;
134 s_hostent.h_aliases = &s_aliases;
135 s_hostent.h_addrtype = AF_INET;
136 s_hostent.h_length =
sizeof(ip_addr_t);
137 s_hostent.h_addr_list = (
char**)&s_phostent_addr;
147 if (s_hostent.h_addr_list != NULL) {
149 for (idx=0; s_hostent.h_addr_list[idx]; idx++) {
151 LWIP_DEBUGF(
DNS_DEBUG, (
"hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t*)s_hostent.h_addr_list[idx])));
156#if LWIP_DNS_API_HOSTENT_STORAGE
158 return sys_thread_hostent(&s_hostent);
181lwip_gethostbyname_r(
const char *name,
struct hostent *ret,
char *buf,
182 size_t buflen,
struct hostent **result,
int *h_errnop)
185 struct gethostbyname_r_helper *h;
190 if (h_errnop == NULL) {
192 h_errnop = &lh_errno;
195 if (result == NULL) {
202 if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
208 namelen = strlen(name);
209 if (buflen < (
sizeof(
struct gethostbyname_r_helper) + namelen + 1 + (MEM_ALIGNMENT - 1))) {
216 hostname = ((
char*)h) +
sizeof(
struct gethostbyname_r_helper);
219 err = netconn_gethostbyname(name, &h->addr);
222 *h_errnop = HOST_NOT_FOUND;
227 MEMCPY(hostname, name, namelen);
228 hostname[namelen] = 0;
231 h->addr_list[0] = &h->addr;
232 h->addr_list[1] = NULL;
234 ret->h_name = hostname;
235 ret->h_aliases = &h->aliases;
236 ret->h_addrtype = AF_INET;
237 ret->h_length =
sizeof(ip_addr_t);
238 ret->h_addr_list = (
char**)&h->addr_list;
255lwip_freeaddrinfo(
struct addrinfo *ai)
257 struct addrinfo *next;
288lwip_getaddrinfo(
const char *nodename,
const char *servname,
289 const struct addrinfo *hints,
struct addrinfo **res)
294 struct sockaddr_storage *sa = NULL;
304 if ((nodename == NULL) && (servname == NULL)) {
309 ai_family = hints->ai_family;
310 if ((ai_family != AF_UNSPEC)
312 && (ai_family != AF_INET)
315 && (ai_family != AF_INET6)
321 ai_family = AF_UNSPEC;
324 if (servname != NULL) {
328 unsigned long parsed_port = strtoul(servname, &end, 10);
329 if ((end == servname) || (*end !=
'\0') || (parsed_port == 0) || (parsed_port > 0xffff)) {
332 port_nr = (int)parsed_port;
335 if (nodename != NULL) {
337 if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
339 if (!ipaddr_aton(nodename, &addr)) {
342#if LWIP_IPV4 && LWIP_IPV6
343 if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
344 (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
349#if LWIP_IPV4 && LWIP_IPV6
351 u8_t type = NETCONN_DNS_IPV4_IPV6;
352 if (ai_family == AF_INET) {
353 type = NETCONN_DNS_IPV4;
354 }
else if (ai_family == AF_INET6) {
355 type = NETCONN_DNS_IPV6;
358 err = netconn_gethostbyname_addrtype(nodename, &addr, type);
365 if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
366 ip_addr_set_any(ai_family == AF_INET6, &addr);
368 ip_addr_set_loopback(ai_family == AF_INET6, &addr);
372 total_size =
sizeof(
struct addrinfo) + sizeof(struct sockaddr_storage);
373 if (nodename != NULL) {
374 namelen = strlen(nodename);
379 LWIP_ASSERT(
"namelen is too long", total_size + namelen + 1 > total_size);
380 total_size += namelen + 1;
383 LWIP_ASSERT(
"total_size <= NETDB_ELEM_SIZE: please report this!",
384 total_size <= NETDB_ELEM_SIZE);
389 memset(ai, 0, total_size);
391 sa = (
struct sockaddr_storage *)(
void*)((u8_t*)ai +
sizeof(
struct addrinfo));
392 if (IP_IS_V6_VAL(addr)) {
394 struct sockaddr_in6 *sa6 = (
struct sockaddr_in6*)sa;
396 inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
397 sa6->sin6_family = AF_INET6;
398 sa6->sin6_len =
sizeof(
struct sockaddr_in6);
399 sa6->sin6_port = lwip_htons((u16_t)port_nr);
400 ai->ai_family = AF_INET6;
404 struct sockaddr_in *sa4 = (
struct sockaddr_in*)sa;
406 inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
407 sa4->sin_family = AF_INET;
408 sa4->sin_len =
sizeof(
struct sockaddr_in);
409 sa4->sin_port = lwip_htons((u16_t)port_nr);
410 ai->ai_family = AF_INET;
417 ai->ai_socktype = hints->ai_socktype;
418 ai->ai_protocol = hints->ai_protocol;
420 if (nodename != NULL) {
422 ai->ai_canonname = ((
char*)ai +
sizeof(
struct addrinfo) +
sizeof(
struct sockaddr_storage));
423 MEMCPY(ai->ai_canonname, nodename, namelen);
424 ai->ai_canonname[namelen] = 0;
426 ai->ai_addrlen =
sizeof(
struct sockaddr_storage);
427 ai->ai_addr = (
struct sockaddr*)sa;
#define LWIP_DEBUGF(debug, message)
#define LWIP_MEM_ALIGN(addr)
#define DNS_MAX_NAME_LENGTH
void * memp_malloc(memp_t type)
void memp_free(memp_t type, void *mem)