Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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