Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

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