59 #if LWIP_DNS && LWIP_SOCKET 72 struct 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 106 lwip_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);
181 lwip_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;
255 lwip_freeaddrinfo(
struct addrinfo *ai)
257 struct addrinfo *next;
288 lwip_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) {
327 port_nr = atoi(servname);
328 if ((port_nr <= 0) || (port_nr > 0xffff)) {
333 if (nodename != NULL) {
335 if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
337 if (!ipaddr_aton(nodename, &addr)) {
340 #if LWIP_IPV4 && LWIP_IPV6 341 if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
342 (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
347 #if LWIP_IPV4 && LWIP_IPV6 349 u8_t type = NETCONN_DNS_IPV4_IPV6;
350 if (ai_family == AF_INET) {
351 type = NETCONN_DNS_IPV4;
352 }
else if (ai_family == AF_INET6) {
353 type = NETCONN_DNS_IPV6;
356 err = netconn_gethostbyname_addrtype(nodename, &addr, type);
363 if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
364 ip_addr_set_any(ai_family == AF_INET6, &addr);
366 ip_addr_set_loopback(ai_family == AF_INET6, &addr);
370 total_size =
sizeof(
struct addrinfo) + sizeof(struct sockaddr_storage);
371 if (nodename != NULL) {
372 namelen = strlen(nodename);
377 LWIP_ASSERT(
"namelen is too long", total_size + namelen + 1 > total_size);
378 total_size += namelen + 1;
381 LWIP_ASSERT(
"total_size <= NETDB_ELEM_SIZE: please report this!",
382 total_size <= NETDB_ELEM_SIZE);
387 memset(ai, 0, total_size);
389 sa = (
struct sockaddr_storage *)(
void*)((u8_t*)ai +
sizeof(
struct addrinfo));
390 if (IP_IS_V6_VAL(addr)) {
392 struct sockaddr_in6 *sa6 = (
struct sockaddr_in6*)sa;
394 inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
395 sa6->sin6_family = AF_INET6;
396 sa6->sin6_len =
sizeof(
struct sockaddr_in6);
397 sa6->sin6_port = lwip_htons((u16_t)port_nr);
398 ai->ai_family = AF_INET6;
402 struct sockaddr_in *sa4 = (
struct sockaddr_in*)sa;
404 inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
405 sa4->sin_family = AF_INET;
406 sa4->sin_len =
sizeof(
struct sockaddr_in);
407 sa4->sin_port = lwip_htons((u16_t)port_nr);
408 ai->ai_family = AF_INET;
415 ai->ai_socktype = hints->ai_socktype;
416 ai->ai_protocol = hints->ai_protocol;
418 if (nodename != NULL) {
420 ai->ai_canonname = ((
char*)ai +
sizeof(
struct addrinfo) +
sizeof(
struct sockaddr_storage));
421 MEMCPY(ai->ai_canonname, nodename, namelen);
422 ai->ai_canonname[namelen] = 0;
424 ai->ai_addrlen =
sizeof(
struct sockaddr_storage);
425 ai->ai_addr = (
struct sockaddr*)sa;
void memp_free(memp_t type, void *mem)
#define DNS_MAX_NAME_LENGTH
#define LWIP_DEBUGF(debug, message)
#define LWIP_MEM_ALIGN(addr)
void * memp_malloc(memp_t type)