Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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