xref: /btstack/3rd-party/lwip/core/src/api/netdb.c (revision 97dc5e692c7d94a280158af58036a0efee5b0e56)
1*97dc5e69SMatthias Ringwald /**
2*97dc5e69SMatthias Ringwald  * @file
3*97dc5e69SMatthias Ringwald  * API functions for name resolving
4*97dc5e69SMatthias Ringwald  *
5*97dc5e69SMatthias Ringwald  * @defgroup netdbapi NETDB API
6*97dc5e69SMatthias Ringwald  * @ingroup socket
7*97dc5e69SMatthias Ringwald  */
8*97dc5e69SMatthias Ringwald 
9*97dc5e69SMatthias Ringwald /*
10*97dc5e69SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without modification,
11*97dc5e69SMatthias Ringwald  * are permitted provided that the following conditions are met:
12*97dc5e69SMatthias Ringwald  *
13*97dc5e69SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright notice,
14*97dc5e69SMatthias Ringwald  *    this list of conditions and the following disclaimer.
15*97dc5e69SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright notice,
16*97dc5e69SMatthias Ringwald  *    this list of conditions and the following disclaimer in the documentation
17*97dc5e69SMatthias Ringwald  *    and/or other materials provided with the distribution.
18*97dc5e69SMatthias Ringwald  * 3. The name of the author may not be used to endorse or promote products
19*97dc5e69SMatthias Ringwald  *    derived from this software without specific prior written permission.
20*97dc5e69SMatthias Ringwald  *
21*97dc5e69SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22*97dc5e69SMatthias Ringwald  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23*97dc5e69SMatthias Ringwald  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24*97dc5e69SMatthias Ringwald  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25*97dc5e69SMatthias Ringwald  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26*97dc5e69SMatthias Ringwald  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*97dc5e69SMatthias Ringwald  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*97dc5e69SMatthias Ringwald  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29*97dc5e69SMatthias Ringwald  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30*97dc5e69SMatthias Ringwald  * OF SUCH DAMAGE.
31*97dc5e69SMatthias Ringwald  *
32*97dc5e69SMatthias Ringwald  * This file is part of the lwIP TCP/IP stack.
33*97dc5e69SMatthias Ringwald  *
34*97dc5e69SMatthias Ringwald  * Author: Simon Goldschmidt
35*97dc5e69SMatthias Ringwald  *
36*97dc5e69SMatthias Ringwald  */
37*97dc5e69SMatthias Ringwald 
38*97dc5e69SMatthias Ringwald #include "lwip/netdb.h"
39*97dc5e69SMatthias Ringwald 
40*97dc5e69SMatthias Ringwald #if LWIP_DNS && LWIP_SOCKET
41*97dc5e69SMatthias Ringwald 
42*97dc5e69SMatthias Ringwald #include "lwip/err.h"
43*97dc5e69SMatthias Ringwald #include "lwip/mem.h"
44*97dc5e69SMatthias Ringwald #include "lwip/memp.h"
45*97dc5e69SMatthias Ringwald #include "lwip/ip_addr.h"
46*97dc5e69SMatthias Ringwald #include "lwip/api.h"
47*97dc5e69SMatthias Ringwald #include "lwip/dns.h"
48*97dc5e69SMatthias Ringwald 
49*97dc5e69SMatthias Ringwald #include <string.h> /* memset */
50*97dc5e69SMatthias Ringwald #include <stdlib.h> /* atoi */
51*97dc5e69SMatthias Ringwald 
52*97dc5e69SMatthias Ringwald /** helper struct for gethostbyname_r to access the char* buffer */
53*97dc5e69SMatthias Ringwald struct gethostbyname_r_helper {
54*97dc5e69SMatthias Ringwald   ip_addr_t *addr_list[2];
55*97dc5e69SMatthias Ringwald   ip_addr_t addr;
56*97dc5e69SMatthias Ringwald   char *aliases;
57*97dc5e69SMatthias Ringwald };
58*97dc5e69SMatthias Ringwald 
59*97dc5e69SMatthias Ringwald /** h_errno is exported in netdb.h for access by applications. */
60*97dc5e69SMatthias Ringwald #if LWIP_DNS_API_DECLARE_H_ERRNO
61*97dc5e69SMatthias Ringwald int h_errno;
62*97dc5e69SMatthias Ringwald #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
63*97dc5e69SMatthias Ringwald 
64*97dc5e69SMatthias Ringwald /** define "hostent" variables storage: 0 if we use a static (but unprotected)
65*97dc5e69SMatthias Ringwald  * set of variables for lwip_gethostbyname, 1 if we use a local storage */
66*97dc5e69SMatthias Ringwald #ifndef LWIP_DNS_API_HOSTENT_STORAGE
67*97dc5e69SMatthias Ringwald #define LWIP_DNS_API_HOSTENT_STORAGE 0
68*97dc5e69SMatthias Ringwald #endif
69*97dc5e69SMatthias Ringwald 
70*97dc5e69SMatthias Ringwald /** define "hostent" variables storage */
71*97dc5e69SMatthias Ringwald #if LWIP_DNS_API_HOSTENT_STORAGE
72*97dc5e69SMatthias Ringwald #define HOSTENT_STORAGE
73*97dc5e69SMatthias Ringwald #else
74*97dc5e69SMatthias Ringwald #define HOSTENT_STORAGE static
75*97dc5e69SMatthias Ringwald #endif /* LWIP_DNS_API_STATIC_HOSTENT */
76*97dc5e69SMatthias Ringwald 
77*97dc5e69SMatthias Ringwald /**
78*97dc5e69SMatthias Ringwald  * Returns an entry containing addresses of address family AF_INET
79*97dc5e69SMatthias Ringwald  * for the host with name name.
80*97dc5e69SMatthias Ringwald  * Due to dns_gethostbyname limitations, only one address is returned.
81*97dc5e69SMatthias Ringwald  *
82*97dc5e69SMatthias Ringwald  * @param name the hostname to resolve
83*97dc5e69SMatthias Ringwald  * @return an entry containing addresses of address family AF_INET
84*97dc5e69SMatthias Ringwald  *         for the host with name name
85*97dc5e69SMatthias Ringwald  */
86*97dc5e69SMatthias Ringwald struct hostent *
lwip_gethostbyname(const char * name)87*97dc5e69SMatthias Ringwald lwip_gethostbyname(const char *name)
88*97dc5e69SMatthias Ringwald {
89*97dc5e69SMatthias Ringwald   err_t err;
90*97dc5e69SMatthias Ringwald   ip_addr_t addr;
91*97dc5e69SMatthias Ringwald 
92*97dc5e69SMatthias Ringwald   /* buffer variables for lwip_gethostbyname() */
93*97dc5e69SMatthias Ringwald   HOSTENT_STORAGE struct hostent s_hostent;
94*97dc5e69SMatthias Ringwald   HOSTENT_STORAGE char *s_aliases;
95*97dc5e69SMatthias Ringwald   HOSTENT_STORAGE ip_addr_t s_hostent_addr;
96*97dc5e69SMatthias Ringwald   HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
97*97dc5e69SMatthias Ringwald   HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
98*97dc5e69SMatthias Ringwald 
99*97dc5e69SMatthias Ringwald   /* query host IP address */
100*97dc5e69SMatthias Ringwald   err = netconn_gethostbyname(name, &addr);
101*97dc5e69SMatthias Ringwald   if (err != ERR_OK) {
102*97dc5e69SMatthias Ringwald     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
103*97dc5e69SMatthias Ringwald     h_errno = HOST_NOT_FOUND;
104*97dc5e69SMatthias Ringwald     return NULL;
105*97dc5e69SMatthias Ringwald   }
106*97dc5e69SMatthias Ringwald 
107*97dc5e69SMatthias Ringwald   /* fill hostent */
108*97dc5e69SMatthias Ringwald   s_hostent_addr = addr;
109*97dc5e69SMatthias Ringwald   s_phostent_addr[0] = &s_hostent_addr;
110*97dc5e69SMatthias Ringwald   s_phostent_addr[1] = NULL;
111*97dc5e69SMatthias Ringwald   strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
112*97dc5e69SMatthias Ringwald   s_hostname[DNS_MAX_NAME_LENGTH] = 0;
113*97dc5e69SMatthias Ringwald   s_hostent.h_name = s_hostname;
114*97dc5e69SMatthias Ringwald   s_aliases = NULL;
115*97dc5e69SMatthias Ringwald   s_hostent.h_aliases = &s_aliases;
116*97dc5e69SMatthias Ringwald   s_hostent.h_addrtype = AF_INET;
117*97dc5e69SMatthias Ringwald   s_hostent.h_length = sizeof(ip_addr_t);
118*97dc5e69SMatthias Ringwald   s_hostent.h_addr_list = (char **)&s_phostent_addr;
119*97dc5e69SMatthias Ringwald 
120*97dc5e69SMatthias Ringwald #if DNS_DEBUG
121*97dc5e69SMatthias Ringwald   /* dump hostent */
122*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name           == %s\n", s_hostent.h_name));
123*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases        == %p\n", (void *)s_hostent.h_aliases));
124*97dc5e69SMatthias Ringwald   /* h_aliases are always empty */
125*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype       == %d\n", s_hostent.h_addrtype));
126*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length         == %d\n", s_hostent.h_length));
127*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list      == %p\n", (void *)s_hostent.h_addr_list));
128*97dc5e69SMatthias Ringwald   if (s_hostent.h_addr_list != NULL) {
129*97dc5e69SMatthias Ringwald     u8_t idx;
130*97dc5e69SMatthias Ringwald     for (idx = 0; s_hostent.h_addr_list[idx]; idx++) {
131*97dc5e69SMatthias Ringwald       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]   == %p\n", idx, s_hostent.h_addr_list[idx]));
132*97dc5e69SMatthias Ringwald       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t *)s_hostent.h_addr_list[idx])));
133*97dc5e69SMatthias Ringwald     }
134*97dc5e69SMatthias Ringwald   }
135*97dc5e69SMatthias Ringwald #endif /* DNS_DEBUG */
136*97dc5e69SMatthias Ringwald 
137*97dc5e69SMatthias Ringwald #if LWIP_DNS_API_HOSTENT_STORAGE
138*97dc5e69SMatthias Ringwald   /* this function should return the "per-thread" hostent after copy from s_hostent */
139*97dc5e69SMatthias Ringwald   return sys_thread_hostent(&s_hostent);
140*97dc5e69SMatthias Ringwald #else
141*97dc5e69SMatthias Ringwald   return &s_hostent;
142*97dc5e69SMatthias Ringwald #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
143*97dc5e69SMatthias Ringwald }
144*97dc5e69SMatthias Ringwald 
145*97dc5e69SMatthias Ringwald /**
146*97dc5e69SMatthias Ringwald  * Thread-safe variant of lwip_gethostbyname: instead of using a static
147*97dc5e69SMatthias Ringwald  * buffer, this function takes buffer and errno pointers as arguments
148*97dc5e69SMatthias Ringwald  * and uses these for the result.
149*97dc5e69SMatthias Ringwald  *
150*97dc5e69SMatthias Ringwald  * @param name the hostname to resolve
151*97dc5e69SMatthias Ringwald  * @param ret pre-allocated struct where to store the result
152*97dc5e69SMatthias Ringwald  * @param buf pre-allocated buffer where to store additional data
153*97dc5e69SMatthias Ringwald  * @param buflen the size of buf
154*97dc5e69SMatthias Ringwald  * @param result pointer to a hostent pointer that is set to ret on success
155*97dc5e69SMatthias Ringwald  *               and set to zero on error
156*97dc5e69SMatthias Ringwald  * @param h_errnop pointer to an int where to store errors (instead of modifying
157*97dc5e69SMatthias Ringwald  *                 the global h_errno)
158*97dc5e69SMatthias Ringwald  * @return 0 on success, non-zero on error, additional error information
159*97dc5e69SMatthias Ringwald  *         is stored in *h_errnop instead of h_errno to be thread-safe
160*97dc5e69SMatthias Ringwald  */
161*97dc5e69SMatthias Ringwald int
lwip_gethostbyname_r(const char * name,struct hostent * ret,char * buf,size_t buflen,struct hostent ** result,int * h_errnop)162*97dc5e69SMatthias Ringwald lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
163*97dc5e69SMatthias Ringwald                      size_t buflen, struct hostent **result, int *h_errnop)
164*97dc5e69SMatthias Ringwald {
165*97dc5e69SMatthias Ringwald   err_t err;
166*97dc5e69SMatthias Ringwald   struct gethostbyname_r_helper *h;
167*97dc5e69SMatthias Ringwald   char *hostname;
168*97dc5e69SMatthias Ringwald   size_t namelen;
169*97dc5e69SMatthias Ringwald   int lh_errno;
170*97dc5e69SMatthias Ringwald 
171*97dc5e69SMatthias Ringwald   if (h_errnop == NULL) {
172*97dc5e69SMatthias Ringwald     /* ensure h_errnop is never NULL */
173*97dc5e69SMatthias Ringwald     h_errnop = &lh_errno;
174*97dc5e69SMatthias Ringwald   }
175*97dc5e69SMatthias Ringwald 
176*97dc5e69SMatthias Ringwald   if (result == NULL) {
177*97dc5e69SMatthias Ringwald     /* not all arguments given */
178*97dc5e69SMatthias Ringwald     *h_errnop = EINVAL;
179*97dc5e69SMatthias Ringwald     return -1;
180*97dc5e69SMatthias Ringwald   }
181*97dc5e69SMatthias Ringwald   /* first thing to do: set *result to nothing */
182*97dc5e69SMatthias Ringwald   *result = NULL;
183*97dc5e69SMatthias Ringwald   if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
184*97dc5e69SMatthias Ringwald     /* not all arguments given */
185*97dc5e69SMatthias Ringwald     *h_errnop = EINVAL;
186*97dc5e69SMatthias Ringwald     return -1;
187*97dc5e69SMatthias Ringwald   }
188*97dc5e69SMatthias Ringwald 
189*97dc5e69SMatthias Ringwald   namelen = strlen(name);
190*97dc5e69SMatthias Ringwald   if (buflen < (sizeof(struct gethostbyname_r_helper) + LWIP_MEM_ALIGN_BUFFER(namelen + 1))) {
191*97dc5e69SMatthias Ringwald     /* buf can't hold the data needed + a copy of name */
192*97dc5e69SMatthias Ringwald     *h_errnop = ERANGE;
193*97dc5e69SMatthias Ringwald     return -1;
194*97dc5e69SMatthias Ringwald   }
195*97dc5e69SMatthias Ringwald 
196*97dc5e69SMatthias Ringwald   h = (struct gethostbyname_r_helper *)LWIP_MEM_ALIGN(buf);
197*97dc5e69SMatthias Ringwald   hostname = ((char *)h) + sizeof(struct gethostbyname_r_helper);
198*97dc5e69SMatthias Ringwald 
199*97dc5e69SMatthias Ringwald   /* query host IP address */
200*97dc5e69SMatthias Ringwald   err = netconn_gethostbyname(name, &h->addr);
201*97dc5e69SMatthias Ringwald   if (err != ERR_OK) {
202*97dc5e69SMatthias Ringwald     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
203*97dc5e69SMatthias Ringwald     *h_errnop = HOST_NOT_FOUND;
204*97dc5e69SMatthias Ringwald     return -1;
205*97dc5e69SMatthias Ringwald   }
206*97dc5e69SMatthias Ringwald 
207*97dc5e69SMatthias Ringwald   /* copy the hostname into buf */
208*97dc5e69SMatthias Ringwald   MEMCPY(hostname, name, namelen);
209*97dc5e69SMatthias Ringwald   hostname[namelen] = 0;
210*97dc5e69SMatthias Ringwald 
211*97dc5e69SMatthias Ringwald   /* fill hostent */
212*97dc5e69SMatthias Ringwald   h->addr_list[0] = &h->addr;
213*97dc5e69SMatthias Ringwald   h->addr_list[1] = NULL;
214*97dc5e69SMatthias Ringwald   h->aliases = NULL;
215*97dc5e69SMatthias Ringwald   ret->h_name = hostname;
216*97dc5e69SMatthias Ringwald   ret->h_aliases = &h->aliases;
217*97dc5e69SMatthias Ringwald   ret->h_addrtype = AF_INET;
218*97dc5e69SMatthias Ringwald   ret->h_length = sizeof(ip_addr_t);
219*97dc5e69SMatthias Ringwald   ret->h_addr_list = (char **)&h->addr_list;
220*97dc5e69SMatthias Ringwald 
221*97dc5e69SMatthias Ringwald   /* set result != NULL */
222*97dc5e69SMatthias Ringwald   *result = ret;
223*97dc5e69SMatthias Ringwald 
224*97dc5e69SMatthias Ringwald   /* return success */
225*97dc5e69SMatthias Ringwald   return 0;
226*97dc5e69SMatthias Ringwald }
227*97dc5e69SMatthias Ringwald 
228*97dc5e69SMatthias Ringwald /**
229*97dc5e69SMatthias Ringwald  * Frees one or more addrinfo structures returned by getaddrinfo(), along with
230*97dc5e69SMatthias Ringwald  * any additional storage associated with those structures. If the ai_next field
231*97dc5e69SMatthias Ringwald  * of the structure is not null, the entire list of structures is freed.
232*97dc5e69SMatthias Ringwald  *
233*97dc5e69SMatthias Ringwald  * @param ai struct addrinfo to free
234*97dc5e69SMatthias Ringwald  */
235*97dc5e69SMatthias Ringwald void
lwip_freeaddrinfo(struct addrinfo * ai)236*97dc5e69SMatthias Ringwald lwip_freeaddrinfo(struct addrinfo *ai)
237*97dc5e69SMatthias Ringwald {
238*97dc5e69SMatthias Ringwald   struct addrinfo *next;
239*97dc5e69SMatthias Ringwald 
240*97dc5e69SMatthias Ringwald   while (ai != NULL) {
241*97dc5e69SMatthias Ringwald     next = ai->ai_next;
242*97dc5e69SMatthias Ringwald     memp_free(MEMP_NETDB, ai);
243*97dc5e69SMatthias Ringwald     ai = next;
244*97dc5e69SMatthias Ringwald   }
245*97dc5e69SMatthias Ringwald }
246*97dc5e69SMatthias Ringwald 
247*97dc5e69SMatthias Ringwald /**
248*97dc5e69SMatthias Ringwald  * Translates the name of a service location (for example, a host name) and/or
249*97dc5e69SMatthias Ringwald  * a service name and returns a set of socket addresses and associated
250*97dc5e69SMatthias Ringwald  * information to be used in creating a socket with which to address the
251*97dc5e69SMatthias Ringwald  * specified service.
252*97dc5e69SMatthias Ringwald  * Memory for the result is allocated internally and must be freed by calling
253*97dc5e69SMatthias Ringwald  * lwip_freeaddrinfo()!
254*97dc5e69SMatthias Ringwald  *
255*97dc5e69SMatthias Ringwald  * Due to a limitation in dns_gethostbyname, only the first address of a
256*97dc5e69SMatthias Ringwald  * host is returned.
257*97dc5e69SMatthias Ringwald  * Also, service names are not supported (only port numbers)!
258*97dc5e69SMatthias Ringwald  *
259*97dc5e69SMatthias Ringwald  * @param nodename descriptive name or address string of the host
260*97dc5e69SMatthias Ringwald  *                 (may be NULL -> local address)
261*97dc5e69SMatthias Ringwald  * @param servname port number as string of NULL
262*97dc5e69SMatthias Ringwald  * @param hints structure containing input values that set socktype and protocol
263*97dc5e69SMatthias Ringwald  * @param res pointer to a pointer where to store the result (set to NULL on failure)
264*97dc5e69SMatthias Ringwald  * @return 0 on success, non-zero on failure
265*97dc5e69SMatthias Ringwald  *
266*97dc5e69SMatthias Ringwald  * @todo: implement AI_V4MAPPED, AI_ADDRCONFIG
267*97dc5e69SMatthias Ringwald  */
268*97dc5e69SMatthias Ringwald int
lwip_getaddrinfo(const char * nodename,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)269*97dc5e69SMatthias Ringwald lwip_getaddrinfo(const char *nodename, const char *servname,
270*97dc5e69SMatthias Ringwald                  const struct addrinfo *hints, struct addrinfo **res)
271*97dc5e69SMatthias Ringwald {
272*97dc5e69SMatthias Ringwald   err_t err;
273*97dc5e69SMatthias Ringwald   ip_addr_t addr;
274*97dc5e69SMatthias Ringwald   struct addrinfo *ai;
275*97dc5e69SMatthias Ringwald   struct sockaddr_storage *sa = NULL;
276*97dc5e69SMatthias Ringwald   int port_nr = 0;
277*97dc5e69SMatthias Ringwald   size_t total_size;
278*97dc5e69SMatthias Ringwald   size_t namelen = 0;
279*97dc5e69SMatthias Ringwald   int ai_family;
280*97dc5e69SMatthias Ringwald 
281*97dc5e69SMatthias Ringwald   if (res == NULL) {
282*97dc5e69SMatthias Ringwald     return EAI_FAIL;
283*97dc5e69SMatthias Ringwald   }
284*97dc5e69SMatthias Ringwald   *res = NULL;
285*97dc5e69SMatthias Ringwald   if ((nodename == NULL) && (servname == NULL)) {
286*97dc5e69SMatthias Ringwald     return EAI_NONAME;
287*97dc5e69SMatthias Ringwald   }
288*97dc5e69SMatthias Ringwald 
289*97dc5e69SMatthias Ringwald   if (hints != NULL) {
290*97dc5e69SMatthias Ringwald     ai_family = hints->ai_family;
291*97dc5e69SMatthias Ringwald     if ((ai_family != AF_UNSPEC)
292*97dc5e69SMatthias Ringwald #if LWIP_IPV4
293*97dc5e69SMatthias Ringwald         && (ai_family != AF_INET)
294*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV4 */
295*97dc5e69SMatthias Ringwald #if LWIP_IPV6
296*97dc5e69SMatthias Ringwald         && (ai_family != AF_INET6)
297*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV6 */
298*97dc5e69SMatthias Ringwald        ) {
299*97dc5e69SMatthias Ringwald       return EAI_FAMILY;
300*97dc5e69SMatthias Ringwald     }
301*97dc5e69SMatthias Ringwald   } else {
302*97dc5e69SMatthias Ringwald     ai_family = AF_UNSPEC;
303*97dc5e69SMatthias Ringwald   }
304*97dc5e69SMatthias Ringwald 
305*97dc5e69SMatthias Ringwald   if (servname != NULL) {
306*97dc5e69SMatthias Ringwald     /* service name specified: convert to port number
307*97dc5e69SMatthias Ringwald      * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
308*97dc5e69SMatthias Ringwald     port_nr = atoi(servname);
309*97dc5e69SMatthias Ringwald     if ((port_nr <= 0) || (port_nr > 0xffff)) {
310*97dc5e69SMatthias Ringwald       return EAI_SERVICE;
311*97dc5e69SMatthias Ringwald     }
312*97dc5e69SMatthias Ringwald   }
313*97dc5e69SMatthias Ringwald 
314*97dc5e69SMatthias Ringwald   if (nodename != NULL) {
315*97dc5e69SMatthias Ringwald     /* service location specified, try to resolve */
316*97dc5e69SMatthias Ringwald     if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
317*97dc5e69SMatthias Ringwald       /* no DNS lookup, just parse for an address string */
318*97dc5e69SMatthias Ringwald       if (!ipaddr_aton(nodename, &addr)) {
319*97dc5e69SMatthias Ringwald         return EAI_NONAME;
320*97dc5e69SMatthias Ringwald       }
321*97dc5e69SMatthias Ringwald #if LWIP_IPV4 && LWIP_IPV6
322*97dc5e69SMatthias Ringwald       if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
323*97dc5e69SMatthias Ringwald           (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
324*97dc5e69SMatthias Ringwald         return EAI_NONAME;
325*97dc5e69SMatthias Ringwald       }
326*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV4 && LWIP_IPV6 */
327*97dc5e69SMatthias Ringwald     } else {
328*97dc5e69SMatthias Ringwald #if LWIP_IPV4 && LWIP_IPV6
329*97dc5e69SMatthias Ringwald       /* AF_UNSPEC: prefer IPv4 */
330*97dc5e69SMatthias Ringwald       u8_t type = NETCONN_DNS_IPV4_IPV6;
331*97dc5e69SMatthias Ringwald       if (ai_family == AF_INET) {
332*97dc5e69SMatthias Ringwald         type = NETCONN_DNS_IPV4;
333*97dc5e69SMatthias Ringwald       } else if (ai_family == AF_INET6) {
334*97dc5e69SMatthias Ringwald         type = NETCONN_DNS_IPV6;
335*97dc5e69SMatthias Ringwald       }
336*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV4 && LWIP_IPV6 */
337*97dc5e69SMatthias Ringwald       err = netconn_gethostbyname_addrtype(nodename, &addr, type);
338*97dc5e69SMatthias Ringwald       if (err != ERR_OK) {
339*97dc5e69SMatthias Ringwald         return EAI_FAIL;
340*97dc5e69SMatthias Ringwald       }
341*97dc5e69SMatthias Ringwald     }
342*97dc5e69SMatthias Ringwald   } else {
343*97dc5e69SMatthias Ringwald     /* service location specified, use loopback address */
344*97dc5e69SMatthias Ringwald     if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
345*97dc5e69SMatthias Ringwald       ip_addr_set_any_val(ai_family == AF_INET6, addr);
346*97dc5e69SMatthias Ringwald     } else {
347*97dc5e69SMatthias Ringwald       ip_addr_set_loopback_val(ai_family == AF_INET6, addr);
348*97dc5e69SMatthias Ringwald     }
349*97dc5e69SMatthias Ringwald   }
350*97dc5e69SMatthias Ringwald 
351*97dc5e69SMatthias Ringwald   total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
352*97dc5e69SMatthias Ringwald   if (nodename != NULL) {
353*97dc5e69SMatthias Ringwald     namelen = strlen(nodename);
354*97dc5e69SMatthias Ringwald     if (namelen > DNS_MAX_NAME_LENGTH) {
355*97dc5e69SMatthias Ringwald       /* invalid name length */
356*97dc5e69SMatthias Ringwald       return EAI_FAIL;
357*97dc5e69SMatthias Ringwald     }
358*97dc5e69SMatthias Ringwald     LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
359*97dc5e69SMatthias Ringwald     total_size += namelen + 1;
360*97dc5e69SMatthias Ringwald   }
361*97dc5e69SMatthias Ringwald   /* If this fails, please report to lwip-devel! :-) */
362*97dc5e69SMatthias Ringwald   LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
363*97dc5e69SMatthias Ringwald               total_size <= NETDB_ELEM_SIZE);
364*97dc5e69SMatthias Ringwald   ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
365*97dc5e69SMatthias Ringwald   if (ai == NULL) {
366*97dc5e69SMatthias Ringwald     return EAI_MEMORY;
367*97dc5e69SMatthias Ringwald   }
368*97dc5e69SMatthias Ringwald   memset(ai, 0, total_size);
369*97dc5e69SMatthias Ringwald   /* cast through void* to get rid of alignment warnings */
370*97dc5e69SMatthias Ringwald   sa = (struct sockaddr_storage *)(void *)((u8_t *)ai + sizeof(struct addrinfo));
371*97dc5e69SMatthias Ringwald   if (IP_IS_V6_VAL(addr)) {
372*97dc5e69SMatthias Ringwald #if LWIP_IPV6
373*97dc5e69SMatthias Ringwald     struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
374*97dc5e69SMatthias Ringwald     /* set up sockaddr */
375*97dc5e69SMatthias Ringwald     inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
376*97dc5e69SMatthias Ringwald     sa6->sin6_family = AF_INET6;
377*97dc5e69SMatthias Ringwald     sa6->sin6_len = sizeof(struct sockaddr_in6);
378*97dc5e69SMatthias Ringwald     sa6->sin6_port = lwip_htons((u16_t)port_nr);
379*97dc5e69SMatthias Ringwald     sa6->sin6_scope_id = ip6_addr_zone(ip_2_ip6(&addr));
380*97dc5e69SMatthias Ringwald     ai->ai_family = AF_INET6;
381*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV6 */
382*97dc5e69SMatthias Ringwald   } else {
383*97dc5e69SMatthias Ringwald #if LWIP_IPV4
384*97dc5e69SMatthias Ringwald     struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
385*97dc5e69SMatthias Ringwald     /* set up sockaddr */
386*97dc5e69SMatthias Ringwald     inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
387*97dc5e69SMatthias Ringwald     sa4->sin_family = AF_INET;
388*97dc5e69SMatthias Ringwald     sa4->sin_len = sizeof(struct sockaddr_in);
389*97dc5e69SMatthias Ringwald     sa4->sin_port = lwip_htons((u16_t)port_nr);
390*97dc5e69SMatthias Ringwald     ai->ai_family = AF_INET;
391*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV4 */
392*97dc5e69SMatthias Ringwald   }
393*97dc5e69SMatthias Ringwald 
394*97dc5e69SMatthias Ringwald   /* set up addrinfo */
395*97dc5e69SMatthias Ringwald   if (hints != NULL) {
396*97dc5e69SMatthias Ringwald     /* copy socktype & protocol from hints if specified */
397*97dc5e69SMatthias Ringwald     ai->ai_socktype = hints->ai_socktype;
398*97dc5e69SMatthias Ringwald     ai->ai_protocol = hints->ai_protocol;
399*97dc5e69SMatthias Ringwald   }
400*97dc5e69SMatthias Ringwald   if (nodename != NULL) {
401*97dc5e69SMatthias Ringwald     /* copy nodename to canonname if specified */
402*97dc5e69SMatthias Ringwald     ai->ai_canonname = ((char *)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
403*97dc5e69SMatthias Ringwald     MEMCPY(ai->ai_canonname, nodename, namelen);
404*97dc5e69SMatthias Ringwald     ai->ai_canonname[namelen] = 0;
405*97dc5e69SMatthias Ringwald   }
406*97dc5e69SMatthias Ringwald   ai->ai_addrlen = sizeof(struct sockaddr_storage);
407*97dc5e69SMatthias Ringwald   ai->ai_addr = (struct sockaddr *)sa;
408*97dc5e69SMatthias Ringwald 
409*97dc5e69SMatthias Ringwald   *res = ai;
410*97dc5e69SMatthias Ringwald 
411*97dc5e69SMatthias Ringwald   return 0;
412*97dc5e69SMatthias Ringwald }
413*97dc5e69SMatthias Ringwald 
414*97dc5e69SMatthias Ringwald #endif /* LWIP_DNS && LWIP_SOCKET */
415