Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

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