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