ソースの整理中ですが、利用はできます。

Dependencies:   EthernetInterface HttpServer TextLCD mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
yueee_yt
Date:
Wed Mar 12 04:39:15 2014 +0000
Revision:
2:14b689a85306
Parent:
0:7766f6712673
bug fix

Who changed what in which revision?

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