38 #include "lwip/netdb.h"
40 #if LWIP_DNS && LWIP_SOCKET
44 #include "lwip/memp.h"
45 #include "lwip/ip_addr.h"
53 struct gethostbyname_r_helper {
60 #if LWIP_DNS_API_DECLARE_H_ERRNO
66 #ifndef LWIP_DNS_API_HOSTENT_STORAGE
67 #define LWIP_DNS_API_HOSTENT_STORAGE 0
71 #if LWIP_DNS_API_HOSTENT_STORAGE
72 #define HOSTENT_STORAGE
74 #define HOSTENT_STORAGE static
87 lwip_gethostbyname(
const char *
name)
93 HOSTENT_STORAGE
struct hostent s_hostent;
94 HOSTENT_STORAGE
char *s_aliases;
96 HOSTENT_STORAGE
ip_addr_t *s_phostent_addr[2];
100 err = netconn_gethostbyname(
name, &addr);
103 h_errno = HOST_NOT_FOUND;
108 s_hostent_addr = addr;
109 s_phostent_addr[0] = &s_hostent_addr;
110 s_phostent_addr[1] =
NULL;
113 s_hostent.h_name = s_hostname;
115 s_hostent.h_aliases = &s_aliases;
116 s_hostent.h_addrtype = AF_INET;
118 s_hostent.h_addr_list = (
char**)&s_phostent_addr;
128 if (s_hostent.h_addr_list !=
NULL) {
130 for (idx=0; s_hostent.h_addr_list[idx]; idx++) {
137 #if LWIP_DNS_API_HOSTENT_STORAGE
139 return sys_thread_hostent(&s_hostent);
162 lwip_gethostbyname_r(
const char *
name,
struct hostent *ret,
char *
buf,
163 size_t buflen,
struct hostent **result,
int *h_errnop)
166 struct gethostbyname_r_helper *h;
171 if (h_errnop ==
NULL) {
173 h_errnop = &lh_errno;
176 if (result ==
NULL) {
190 if (buflen < (
sizeof(
struct gethostbyname_r_helper) + namelen + 1 + (
MEM_ALIGNMENT - 1))) {
197 hostname = ((
char*)h) +
sizeof(
struct gethostbyname_r_helper);
200 err = netconn_gethostbyname(
name, &h->addr);
203 *h_errnop = HOST_NOT_FOUND;
209 hostname[namelen] = 0;
212 h->addr_list[0] = &h->addr;
213 h->addr_list[1] =
NULL;
215 ret->h_name = hostname;
216 ret->h_aliases = &h->aliases;
217 ret->h_addrtype = AF_INET;
219 ret->h_addr_list = (
char**)&h->addr_list;
236 lwip_freeaddrinfo(
struct addrinfo *ai)
238 struct addrinfo *next;
269 lwip_getaddrinfo(
const char *nodename,
const char *servname,
270 const struct addrinfo *hints,
struct addrinfo **res)
275 struct sockaddr_storage *sa =
NULL;
285 if ((nodename ==
NULL) && (servname ==
NULL)) {
290 ai_family = hints->ai_family;
291 if ((ai_family != AF_UNSPEC)
293 && (ai_family != AF_INET)
296 && (ai_family != AF_INET6)
302 ai_family = AF_UNSPEC;
305 if (servname !=
NULL) {
308 port_nr = atoi(servname);
309 if ((port_nr <= 0) || (port_nr > 0xffff)) {
314 if (nodename !=
NULL) {
316 if ((hints !=
NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
321 #if LWIP_IPV4 && LWIP_IPV6
328 #if LWIP_IPV4 && LWIP_IPV6
330 u8_t type = NETCONN_DNS_IPV4_IPV6;
331 if (ai_family == AF_INET) {
332 type = NETCONN_DNS_IPV4;
333 }
else if (ai_family == AF_INET6) {
334 type = NETCONN_DNS_IPV6;
337 err = netconn_gethostbyname_addrtype(nodename, &addr, type);
344 if ((hints !=
NULL) && (hints->ai_flags & AI_PASSIVE)) {
351 total_size =
sizeof(
struct addrinfo) + sizeof(struct sockaddr_storage);
352 if (nodename !=
NULL) {
353 namelen =
strlen(nodename);
358 LWIP_ASSERT(
"namelen is too long", total_size + namelen + 1 > total_size);
359 total_size += namelen + 1;
362 LWIP_ASSERT(
"total_size <= NETDB_ELEM_SIZE: please report this!",
363 total_size <= NETDB_ELEM_SIZE);
368 memset(ai, 0, total_size);
370 sa = (
struct sockaddr_storage *)(
void*)((
u8_t*)ai +
sizeof(
struct addrinfo));
373 struct sockaddr_in6 *sa6 = (
struct sockaddr_in6*)sa;
375 inet6_addr_from_ip6addr(&sa6->sin6_addr,
ip_2_ip6(&addr));
376 sa6->sin6_family = AF_INET6;
377 sa6->sin6_len =
sizeof(
struct sockaddr_in6);
379 ai->ai_family = AF_INET6;
383 struct sockaddr_in *sa4 = (
struct sockaddr_in*)sa;
385 inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
386 sa4->sin_family = AF_INET;
387 sa4->sin_len =
sizeof(
struct sockaddr_in);
389 ai->ai_family = AF_INET;
396 ai->ai_socktype = hints->ai_socktype;
397 ai->ai_protocol = hints->ai_protocol;
399 if (nodename !=
NULL) {
401 ai->ai_canonname = ((
char*)ai +
sizeof(
struct addrinfo) +
sizeof(
struct sockaddr_storage));
402 MEMCPY(ai->ai_canonname, nodename, namelen);
403 ai->ai_canonname[namelen] = 0;
405 ai->ai_addrlen =
sizeof(
struct sockaddr_storage);
406 ai->ai_addr = (
struct sockaddr*)sa;