38 #if LWIP_DNS && LWIP_SOCKET
43 #include "lwip/ip_addr.h"
51 struct gethostbyname_r_helper {
58 #if LWIP_DNS_API_DECLARE_H_ERRNO
64 #ifndef LWIP_DNS_API_HOSTENT_STORAGE
65 #define LWIP_DNS_API_HOSTENT_STORAGE 0
69 #if LWIP_DNS_API_HOSTENT_STORAGE
70 #define HOSTENT_STORAGE
72 #define HOSTENT_STORAGE static
85 lwip_gethostbyname(
const char *name)
91 HOSTENT_STORAGE
struct hostent s_hostent;
92 HOSTENT_STORAGE
char *s_aliases;
94 HOSTENT_STORAGE
ip_addr_t *s_phostent_addr[2];
97 err = netconn_gethostbyname(name, &addr);
100 h_errno = HOST_NOT_FOUND;
105 s_hostent_addr = addr;
106 s_phostent_addr[0] = &s_hostent_addr;
107 s_phostent_addr[1] =
NULL;
108 s_hostent.h_name = (
char*)name;
109 s_hostent.h_aliases = &s_aliases;
110 s_hostent.h_addrtype = AF_INET;
112 s_hostent.h_addr_list = (
char**)&s_phostent_addr;
118 if (s_hostent.h_aliases !=
NULL) {
120 for ( idx=0; s_hostent.h_aliases[idx]; idx++) {
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) {
183 if ((name ==
NULL) || (ret ==
NULL) || (buf == 0)) {
189 namelen = strlen(name);
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 = ENSRNOTFOUND;
208 MEMCPY(hostname, name, namelen);
209 hostname[namelen] = 0;
212 h->addrs = &(h->addr);
214 ret->h_name = (
char*)hostname;
215 ret->h_aliases = &(h->aliases);
216 ret->h_addrtype = AF_INET;
218 ret->h_addr_list = (
char**)&(h->addrs);
235 lwip_freeaddrinfo(
struct addrinfo *ai)
237 struct addrinfo *next;
266 lwip_getaddrinfo(
const char *nodename,
const char *servname,
267 const struct addrinfo *hints,
struct addrinfo **res)
272 struct sockaddr_in *sa =
NULL;
281 if ((nodename ==
NULL) && (servname ==
NULL)) {
285 if (servname !=
NULL) {
288 port_nr = atoi(servname);
289 if ((port_nr <= 0) || (port_nr > 0xffff)) {
294 if (nodename !=
NULL) {
296 err = netconn_gethostbyname(nodename, &addr);
305 total_size =
sizeof(
struct addrinfo) + sizeof(struct sockaddr_in);
306 if (nodename !=
NULL) {
307 namelen = strlen(nodename);
309 total_size += namelen + 1;
312 LWIP_ASSERT(
"total_size <= NETDB_ELEM_SIZE: please report this!",
313 total_size <= NETDB_ELEM_SIZE);
318 memset(ai, 0, total_size);
319 sa = (
struct sockaddr_in*)((
u8_t*)ai +
sizeof(
struct addrinfo));
322 sa->sin_family = AF_INET;
323 sa->sin_len =
sizeof(
struct sockaddr_in);
327 ai->ai_family = AF_INET;
330 ai->ai_socktype = hints->ai_socktype;
331 ai->ai_protocol = hints->ai_protocol;
333 if (nodename !=
NULL) {
335 ai->ai_canonname = ((
char*)ai +
sizeof(
struct addrinfo) +
sizeof(
struct sockaddr_in));
336 MEMCPY(ai->ai_canonname, nodename, namelen);
337 ai->ai_canonname[namelen] = 0;
339 ai->ai_addrlen =
sizeof(
struct sockaddr_in);
340 ai->ai_addr = (
struct sockaddr*)sa;