V1

Dependents:   EthernetInterface

Fork of lwip by mbed official

Committer:
mbed_official
Date:
Fri Jun 22 09:25:39 2012 +0000
Revision:
0:51ac1d130fd4
Initial import from lwip-1.4.0: http://download.savannah.gnu.org/releases/lwip/lwip-1.4.0.zip

Who changed what in which revision?

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