Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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