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