EthernetNetIf Compatibility.
Dependents: XBeeWiFi_SPI_example
Fork of NetServicesSource by
lwip/core/dns.c@2:a4f97773c90f, 2010-06-18 (annotated)
- Committer:
- donatien
- Date:
- Fri Jun 18 09:22:54 2010 +0000
- Revision:
- 2:a4f97773c90f
- Parent:
- 0:632c9925f013
- Child:
- 5:dd63a1e02b1b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 2:a4f97773c90f | 1 | /** |
donatien | 2:a4f97773c90f | 2 | * @file |
donatien | 2:a4f97773c90f | 3 | * DNS - host name to IP address resolver. |
donatien | 2:a4f97773c90f | 4 | * |
donatien | 2:a4f97773c90f | 5 | */ |
donatien | 2:a4f97773c90f | 6 | |
donatien | 2:a4f97773c90f | 7 | /** |
donatien | 2:a4f97773c90f | 8 | |
donatien | 2:a4f97773c90f | 9 | * This file implements a DNS host name to IP address resolver. |
donatien | 2:a4f97773c90f | 10 | |
donatien | 2:a4f97773c90f | 11 | * Port to lwIP from uIP |
donatien | 2:a4f97773c90f | 12 | * by Jim Pettinato April 2007 |
donatien | 2:a4f97773c90f | 13 | |
donatien | 2:a4f97773c90f | 14 | * uIP version Copyright (c) 2002-2003, Adam Dunkels. |
donatien | 2:a4f97773c90f | 15 | * All rights reserved. |
donatien | 2:a4f97773c90f | 16 | * |
donatien | 2:a4f97773c90f | 17 | * Redistribution and use in source and binary forms, with or without |
donatien | 2:a4f97773c90f | 18 | * modification, are permitted provided that the following conditions |
donatien | 2:a4f97773c90f | 19 | * are met: |
donatien | 2:a4f97773c90f | 20 | * 1. Redistributions of source code must retain the above copyright |
donatien | 2:a4f97773c90f | 21 | * notice, this list of conditions and the following disclaimer. |
donatien | 2:a4f97773c90f | 22 | * 2. Redistributions in binary form must reproduce the above copyright |
donatien | 2:a4f97773c90f | 23 | * notice, this list of conditions and the following disclaimer in the |
donatien | 2:a4f97773c90f | 24 | * documentation and/or other materials provided with the distribution. |
donatien | 2:a4f97773c90f | 25 | * 3. The name of the author may not be used to endorse or promote |
donatien | 2:a4f97773c90f | 26 | * products derived from this software without specific prior |
donatien | 2:a4f97773c90f | 27 | * written permission. |
donatien | 2:a4f97773c90f | 28 | * |
donatien | 2:a4f97773c90f | 29 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
donatien | 2:a4f97773c90f | 30 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
donatien | 2:a4f97773c90f | 31 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
donatien | 2:a4f97773c90f | 32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
donatien | 2:a4f97773c90f | 33 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
donatien | 2:a4f97773c90f | 34 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
donatien | 2:a4f97773c90f | 35 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
donatien | 2:a4f97773c90f | 36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
donatien | 2:a4f97773c90f | 37 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
donatien | 2:a4f97773c90f | 38 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
donatien | 2:a4f97773c90f | 39 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
donatien | 2:a4f97773c90f | 40 | * |
donatien | 2:a4f97773c90f | 41 | * |
donatien | 2:a4f97773c90f | 42 | * DNS.C |
donatien | 2:a4f97773c90f | 43 | * |
donatien | 2:a4f97773c90f | 44 | * The lwIP DNS resolver functions are used to lookup a host name and |
donatien | 2:a4f97773c90f | 45 | * map it to a numerical IP address. It maintains a list of resolved |
donatien | 2:a4f97773c90f | 46 | * hostnames that can be queried with the dns_lookup() function. |
donatien | 2:a4f97773c90f | 47 | * New hostnames can be resolved using the dns_query() function. |
donatien | 2:a4f97773c90f | 48 | * |
donatien | 2:a4f97773c90f | 49 | * The lwIP version of the resolver also adds a non-blocking version of |
donatien | 2:a4f97773c90f | 50 | * gethostbyname() that will work with a raw API application. This function |
donatien | 2:a4f97773c90f | 51 | * checks for an IP address string first and converts it if it is valid. |
donatien | 2:a4f97773c90f | 52 | * gethostbyname() then does a dns_lookup() to see if the name is |
donatien | 2:a4f97773c90f | 53 | * already in the table. If so, the IP is returned. If not, a query is |
donatien | 2:a4f97773c90f | 54 | * issued and the function returns with a ERR_INPROGRESS status. The app |
donatien | 2:a4f97773c90f | 55 | * using the dns client must then go into a waiting state. |
donatien | 2:a4f97773c90f | 56 | * |
donatien | 2:a4f97773c90f | 57 | * Once a hostname has been resolved (or found to be non-existent), |
donatien | 2:a4f97773c90f | 58 | * the resolver code calls a specified callback function (which |
donatien | 2:a4f97773c90f | 59 | * must be implemented by the module that uses the resolver). |
donatien | 2:a4f97773c90f | 60 | */ |
donatien | 2:a4f97773c90f | 61 | |
donatien | 2:a4f97773c90f | 62 | /*----------------------------------------------------------------------------- |
donatien | 2:a4f97773c90f | 63 | * RFC 1035 - Domain names - implementation and specification |
donatien | 2:a4f97773c90f | 64 | * RFC 2181 - Clarifications to the DNS Specification |
donatien | 2:a4f97773c90f | 65 | *----------------------------------------------------------------------------*/ |
donatien | 2:a4f97773c90f | 66 | |
donatien | 2:a4f97773c90f | 67 | /** @todo: define good default values (rfc compliance) */ |
donatien | 2:a4f97773c90f | 68 | /** @todo: improve answer parsing, more checkings... */ |
donatien | 2:a4f97773c90f | 69 | /** @todo: check RFC1035 - 7.3. Processing responses */ |
donatien | 2:a4f97773c90f | 70 | |
donatien | 2:a4f97773c90f | 71 | /*----------------------------------------------------------------------------- |
donatien | 2:a4f97773c90f | 72 | * Includes |
donatien | 2:a4f97773c90f | 73 | *----------------------------------------------------------------------------*/ |
donatien | 2:a4f97773c90f | 74 | |
donatien | 2:a4f97773c90f | 75 | #include "lwip/opt.h" |
donatien | 2:a4f97773c90f | 76 | |
donatien | 2:a4f97773c90f | 77 | #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ |
donatien | 2:a4f97773c90f | 78 | |
donatien | 2:a4f97773c90f | 79 | #include "lwip/udp.h" |
donatien | 2:a4f97773c90f | 80 | #include "lwip/mem.h" |
donatien | 2:a4f97773c90f | 81 | #include "lwip/memp.h" |
donatien | 2:a4f97773c90f | 82 | #include "lwip/dns.h" |
donatien | 2:a4f97773c90f | 83 | #include "lwip/debug.h" |
donatien | 2:a4f97773c90f | 84 | |
donatien | 2:a4f97773c90f | 85 | #include <string.h> |
donatien | 2:a4f97773c90f | 86 | |
donatien | 2:a4f97773c90f | 87 | /** DNS server IP address */ |
donatien | 2:a4f97773c90f | 88 | #ifndef DNS_SERVER_ADDRESS |
donatien | 2:a4f97773c90f | 89 | #define DNS_SERVER_ADDRESS(ipaddr) (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */ |
donatien | 2:a4f97773c90f | 90 | #endif |
donatien | 2:a4f97773c90f | 91 | |
donatien | 2:a4f97773c90f | 92 | /** DNS server port address */ |
donatien | 2:a4f97773c90f | 93 | #ifndef DNS_SERVER_PORT |
donatien | 2:a4f97773c90f | 94 | #define DNS_SERVER_PORT 53 |
donatien | 2:a4f97773c90f | 95 | #endif |
donatien | 2:a4f97773c90f | 96 | |
donatien | 2:a4f97773c90f | 97 | /** DNS maximum number of retries when asking for a name, before "timeout". */ |
donatien | 2:a4f97773c90f | 98 | #ifndef DNS_MAX_RETRIES |
donatien | 2:a4f97773c90f | 99 | #define DNS_MAX_RETRIES 4 |
donatien | 2:a4f97773c90f | 100 | #endif |
donatien | 2:a4f97773c90f | 101 | |
donatien | 2:a4f97773c90f | 102 | /** DNS resource record max. TTL (one week as default) */ |
donatien | 2:a4f97773c90f | 103 | #ifndef DNS_MAX_TTL |
donatien | 2:a4f97773c90f | 104 | #define DNS_MAX_TTL 604800 |
donatien | 2:a4f97773c90f | 105 | #endif |
donatien | 2:a4f97773c90f | 106 | |
donatien | 2:a4f97773c90f | 107 | /* DNS protocol flags */ |
donatien | 2:a4f97773c90f | 108 | #define DNS_FLAG1_RESPONSE 0x80 |
donatien | 2:a4f97773c90f | 109 | #define DNS_FLAG1_OPCODE_STATUS 0x10 |
donatien | 2:a4f97773c90f | 110 | #define DNS_FLAG1_OPCODE_INVERSE 0x08 |
donatien | 2:a4f97773c90f | 111 | #define DNS_FLAG1_OPCODE_STANDARD 0x00 |
donatien | 2:a4f97773c90f | 112 | #define DNS_FLAG1_AUTHORATIVE 0x04 |
donatien | 2:a4f97773c90f | 113 | #define DNS_FLAG1_TRUNC 0x02 |
donatien | 2:a4f97773c90f | 114 | #define DNS_FLAG1_RD 0x01 |
donatien | 2:a4f97773c90f | 115 | #define DNS_FLAG2_RA 0x80 |
donatien | 2:a4f97773c90f | 116 | #define DNS_FLAG2_ERR_MASK 0x0f |
donatien | 2:a4f97773c90f | 117 | #define DNS_FLAG2_ERR_NONE 0x00 |
donatien | 2:a4f97773c90f | 118 | #define DNS_FLAG2_ERR_NAME 0x03 |
donatien | 2:a4f97773c90f | 119 | |
donatien | 2:a4f97773c90f | 120 | /* DNS protocol states */ |
donatien | 2:a4f97773c90f | 121 | #define DNS_STATE_UNUSED 0 |
donatien | 2:a4f97773c90f | 122 | #define DNS_STATE_NEW 1 |
donatien | 2:a4f97773c90f | 123 | #define DNS_STATE_ASKING 2 |
donatien | 2:a4f97773c90f | 124 | #define DNS_STATE_DONE 3 |
donatien | 2:a4f97773c90f | 125 | |
donatien | 2:a4f97773c90f | 126 | #ifdef PACK_STRUCT_USE_INCLUDES |
donatien | 2:a4f97773c90f | 127 | # include "arch/bpstruct.h" |
donatien | 2:a4f97773c90f | 128 | #endif |
donatien | 2:a4f97773c90f | 129 | PACK_STRUCT_BEGIN |
donatien | 2:a4f97773c90f | 130 | /** DNS message header */ |
donatien | 2:a4f97773c90f | 131 | struct dns_hdr { |
donatien | 2:a4f97773c90f | 132 | PACK_STRUCT_FIELD(u16_t id); |
donatien | 2:a4f97773c90f | 133 | PACK_STRUCT_FIELD(u8_t flags1); |
donatien | 2:a4f97773c90f | 134 | PACK_STRUCT_FIELD(u8_t flags2); |
donatien | 2:a4f97773c90f | 135 | PACK_STRUCT_FIELD(u16_t numquestions); |
donatien | 2:a4f97773c90f | 136 | PACK_STRUCT_FIELD(u16_t numanswers); |
donatien | 2:a4f97773c90f | 137 | PACK_STRUCT_FIELD(u16_t numauthrr); |
donatien | 2:a4f97773c90f | 138 | PACK_STRUCT_FIELD(u16_t numextrarr); |
donatien | 2:a4f97773c90f | 139 | } PACK_STRUCT_STRUCT; |
donatien | 2:a4f97773c90f | 140 | PACK_STRUCT_END |
donatien | 2:a4f97773c90f | 141 | #ifdef PACK_STRUCT_USE_INCLUDES |
donatien | 2:a4f97773c90f | 142 | # include "arch/epstruct.h" |
donatien | 2:a4f97773c90f | 143 | #endif |
donatien | 2:a4f97773c90f | 144 | #define SIZEOF_DNS_HDR 12 |
donatien | 2:a4f97773c90f | 145 | |
donatien | 2:a4f97773c90f | 146 | /** DNS query message structure. |
donatien | 2:a4f97773c90f | 147 | No packing needed: only used locally on the stack. */ |
donatien | 2:a4f97773c90f | 148 | struct dns_query { |
donatien | 2:a4f97773c90f | 149 | /* DNS query record starts with either a domain name or a pointer |
donatien | 2:a4f97773c90f | 150 | to a name already present somewhere in the packet. */ |
donatien | 2:a4f97773c90f | 151 | u16_t type; |
donatien | 2:a4f97773c90f | 152 | u16_t cls; |
donatien | 2:a4f97773c90f | 153 | }; |
donatien | 2:a4f97773c90f | 154 | #define SIZEOF_DNS_QUERY 4 |
donatien | 2:a4f97773c90f | 155 | |
donatien | 2:a4f97773c90f | 156 | /** DNS answer message structure. |
donatien | 2:a4f97773c90f | 157 | No packing needed: only used locally on the stack. */ |
donatien | 2:a4f97773c90f | 158 | struct dns_answer { |
donatien | 2:a4f97773c90f | 159 | /* DNS answer record starts with either a domain name or a pointer |
donatien | 2:a4f97773c90f | 160 | to a name already present somewhere in the packet. */ |
donatien | 2:a4f97773c90f | 161 | u16_t type; |
donatien | 2:a4f97773c90f | 162 | u16_t cls; |
donatien | 2:a4f97773c90f | 163 | u32_t ttl; |
donatien | 2:a4f97773c90f | 164 | u16_t len; |
donatien | 2:a4f97773c90f | 165 | }; |
donatien | 2:a4f97773c90f | 166 | #define SIZEOF_DNS_ANSWER 10 |
donatien | 2:a4f97773c90f | 167 | |
donatien | 2:a4f97773c90f | 168 | /** DNS table entry */ |
donatien | 2:a4f97773c90f | 169 | struct dns_table_entry { |
donatien | 2:a4f97773c90f | 170 | u8_t state; |
donatien | 2:a4f97773c90f | 171 | u8_t numdns; |
donatien | 2:a4f97773c90f | 172 | u8_t tmr; |
donatien | 2:a4f97773c90f | 173 | u8_t retries; |
donatien | 2:a4f97773c90f | 174 | u8_t seqno; |
donatien | 2:a4f97773c90f | 175 | u8_t err; |
donatien | 2:a4f97773c90f | 176 | u32_t ttl; |
donatien | 2:a4f97773c90f | 177 | char name[DNS_MAX_NAME_LENGTH]; |
donatien | 2:a4f97773c90f | 178 | ip_addr_t ipaddr; |
donatien | 2:a4f97773c90f | 179 | /* pointer to callback on DNS query done */ |
donatien | 2:a4f97773c90f | 180 | dns_found_callback found; |
donatien | 2:a4f97773c90f | 181 | void *arg; |
donatien | 2:a4f97773c90f | 182 | }; |
donatien | 2:a4f97773c90f | 183 | |
donatien | 2:a4f97773c90f | 184 | #if DNS_LOCAL_HOSTLIST |
donatien | 2:a4f97773c90f | 185 | |
donatien | 2:a4f97773c90f | 186 | #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC |
donatien | 2:a4f97773c90f | 187 | /** Local host-list. For hostnames in this list, no |
donatien | 2:a4f97773c90f | 188 | * external name resolution is performed */ |
donatien | 2:a4f97773c90f | 189 | static struct local_hostlist_entry *local_hostlist_dynamic; |
donatien | 2:a4f97773c90f | 190 | #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ |
donatien | 2:a4f97773c90f | 191 | |
donatien | 2:a4f97773c90f | 192 | /** Defining this allows the local_hostlist_static to be placed in a different |
donatien | 2:a4f97773c90f | 193 | * linker section (e.g. FLASH) */ |
donatien | 2:a4f97773c90f | 194 | #ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE |
donatien | 2:a4f97773c90f | 195 | #define DNS_LOCAL_HOSTLIST_STORAGE_PRE static |
donatien | 2:a4f97773c90f | 196 | #endif /* DNS_LOCAL_HOSTLIST_STORAGE_PRE */ |
donatien | 2:a4f97773c90f | 197 | /** Defining this allows the local_hostlist_static to be placed in a different |
donatien | 2:a4f97773c90f | 198 | * linker section (e.g. FLASH) */ |
donatien | 2:a4f97773c90f | 199 | #ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST |
donatien | 2:a4f97773c90f | 200 | #define DNS_LOCAL_HOSTLIST_STORAGE_POST |
donatien | 2:a4f97773c90f | 201 | #endif /* DNS_LOCAL_HOSTLIST_STORAGE_POST */ |
donatien | 2:a4f97773c90f | 202 | DNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[] |
donatien | 2:a4f97773c90f | 203 | DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT; |
donatien | 2:a4f97773c90f | 204 | |
donatien | 2:a4f97773c90f | 205 | #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ |
donatien | 2:a4f97773c90f | 206 | |
donatien | 2:a4f97773c90f | 207 | static void dns_init_local(); |
donatien | 2:a4f97773c90f | 208 | #endif /* DNS_LOCAL_HOSTLIST */ |
donatien | 2:a4f97773c90f | 209 | |
donatien | 2:a4f97773c90f | 210 | |
donatien | 2:a4f97773c90f | 211 | /* forward declarations */ |
donatien | 2:a4f97773c90f | 212 | static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port); |
donatien | 2:a4f97773c90f | 213 | static void dns_check_entries(void); |
donatien | 2:a4f97773c90f | 214 | |
donatien | 2:a4f97773c90f | 215 | /*----------------------------------------------------------------------------- |
donatien | 2:a4f97773c90f | 216 | * Globales |
donatien | 2:a4f97773c90f | 217 | *----------------------------------------------------------------------------*/ |
donatien | 2:a4f97773c90f | 218 | |
donatien | 2:a4f97773c90f | 219 | /* DNS variables */ |
donatien | 2:a4f97773c90f | 220 | static struct udp_pcb *dns_pcb; |
donatien | 2:a4f97773c90f | 221 | static u8_t dns_seqno; |
donatien | 2:a4f97773c90f | 222 | static struct dns_table_entry dns_table[DNS_TABLE_SIZE]; |
donatien | 2:a4f97773c90f | 223 | static ip_addr_t dns_servers[DNS_MAX_SERVERS]; |
donatien | 2:a4f97773c90f | 224 | /** Contiguous buffer for processing responses */ |
donatien | 2:a4f97773c90f | 225 | static u8_t dns_payload_buffer[LWIP_MEM_ALIGN_BUFFER(DNS_MSG_SIZE)]; |
donatien | 2:a4f97773c90f | 226 | static u8_t* dns_payload; |
donatien | 2:a4f97773c90f | 227 | |
donatien | 2:a4f97773c90f | 228 | /** |
donatien | 2:a4f97773c90f | 229 | * Initialize the resolver: set up the UDP pcb and configure the default server |
donatien | 2:a4f97773c90f | 230 | * (DNS_SERVER_ADDRESS). |
donatien | 2:a4f97773c90f | 231 | */ |
donatien | 2:a4f97773c90f | 232 | void |
donatien | 2:a4f97773c90f | 233 | dns_init() |
donatien | 2:a4f97773c90f | 234 | { |
donatien | 2:a4f97773c90f | 235 | ip_addr_t dnsserver; |
donatien | 2:a4f97773c90f | 236 | |
donatien | 2:a4f97773c90f | 237 | dns_payload = (u8_t *)LWIP_MEM_ALIGN(dns_payload_buffer); |
donatien | 2:a4f97773c90f | 238 | |
donatien | 2:a4f97773c90f | 239 | /* initialize default DNS server address */ |
donatien | 2:a4f97773c90f | 240 | DNS_SERVER_ADDRESS(&dnsserver); |
donatien | 2:a4f97773c90f | 241 | |
donatien | 2:a4f97773c90f | 242 | LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n")); |
donatien | 2:a4f97773c90f | 243 | |
donatien | 2:a4f97773c90f | 244 | /* if dns client not yet initialized... */ |
donatien | 2:a4f97773c90f | 245 | if (dns_pcb == NULL) { |
donatien | 2:a4f97773c90f | 246 | dns_pcb = udp_new(); |
donatien | 2:a4f97773c90f | 247 | |
donatien | 2:a4f97773c90f | 248 | if (dns_pcb != NULL) { |
donatien | 2:a4f97773c90f | 249 | /* initialize DNS table not needed (initialized to zero since it is a |
donatien | 2:a4f97773c90f | 250 | * global variable) */ |
donatien | 2:a4f97773c90f | 251 | LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0", |
donatien | 2:a4f97773c90f | 252 | DNS_STATE_UNUSED == 0); |
donatien | 2:a4f97773c90f | 253 | |
donatien | 2:a4f97773c90f | 254 | /* initialize DNS client */ |
donatien | 2:a4f97773c90f | 255 | udp_bind(dns_pcb, IP_ADDR_ANY, 0); |
donatien | 2:a4f97773c90f | 256 | udp_recv(dns_pcb, dns_recv, NULL); |
donatien | 2:a4f97773c90f | 257 | |
donatien | 2:a4f97773c90f | 258 | /* initialize default DNS primary server */ |
donatien | 2:a4f97773c90f | 259 | dns_setserver(0, &dnsserver); |
donatien | 2:a4f97773c90f | 260 | } |
donatien | 2:a4f97773c90f | 261 | } |
donatien | 2:a4f97773c90f | 262 | #if DNS_LOCAL_HOSTLIST |
donatien | 2:a4f97773c90f | 263 | dns_init_local(); |
donatien | 2:a4f97773c90f | 264 | #endif |
donatien | 2:a4f97773c90f | 265 | } |
donatien | 2:a4f97773c90f | 266 | |
donatien | 2:a4f97773c90f | 267 | /** |
donatien | 2:a4f97773c90f | 268 | * Initialize one of the DNS servers. |
donatien | 2:a4f97773c90f | 269 | * |
donatien | 2:a4f97773c90f | 270 | * @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS |
donatien | 2:a4f97773c90f | 271 | * @param dnsserver IP address of the DNS server to set |
donatien | 2:a4f97773c90f | 272 | */ |
donatien | 2:a4f97773c90f | 273 | void |
donatien | 2:a4f97773c90f | 274 | dns_setserver(u8_t numdns, ip_addr_t *dnsserver) |
donatien | 2:a4f97773c90f | 275 | { |
donatien | 2:a4f97773c90f | 276 | if ((numdns < DNS_MAX_SERVERS) && (dns_pcb != NULL) && |
donatien | 2:a4f97773c90f | 277 | (dnsserver != NULL) && !ip_addr_isany(dnsserver)) { |
donatien | 2:a4f97773c90f | 278 | dns_servers[numdns] = (*dnsserver); |
donatien | 2:a4f97773c90f | 279 | } |
donatien | 2:a4f97773c90f | 280 | } |
donatien | 2:a4f97773c90f | 281 | |
donatien | 2:a4f97773c90f | 282 | /** |
donatien | 2:a4f97773c90f | 283 | * Obtain one of the currently configured DNS server. |
donatien | 2:a4f97773c90f | 284 | * |
donatien | 2:a4f97773c90f | 285 | * @param numdns the index of the DNS server |
donatien | 2:a4f97773c90f | 286 | * @return IP address of the indexed DNS server or "ip_addr_any" if the DNS |
donatien | 2:a4f97773c90f | 287 | * server has not been configured. |
donatien | 2:a4f97773c90f | 288 | */ |
donatien | 2:a4f97773c90f | 289 | ip_addr_t |
donatien | 2:a4f97773c90f | 290 | dns_getserver(u8_t numdns) |
donatien | 2:a4f97773c90f | 291 | { |
donatien | 2:a4f97773c90f | 292 | if (numdns < DNS_MAX_SERVERS) { |
donatien | 2:a4f97773c90f | 293 | return dns_servers[numdns]; |
donatien | 2:a4f97773c90f | 294 | } else { |
donatien | 2:a4f97773c90f | 295 | return *IP_ADDR_ANY; |
donatien | 2:a4f97773c90f | 296 | } |
donatien | 2:a4f97773c90f | 297 | } |
donatien | 2:a4f97773c90f | 298 | |
donatien | 2:a4f97773c90f | 299 | /** |
donatien | 2:a4f97773c90f | 300 | * The DNS resolver client timer - handle retries and timeouts and should |
donatien | 2:a4f97773c90f | 301 | * be called every DNS_TMR_INTERVAL milliseconds (every second by default). |
donatien | 2:a4f97773c90f | 302 | */ |
donatien | 2:a4f97773c90f | 303 | void |
donatien | 2:a4f97773c90f | 304 | dns_tmr(void) |
donatien | 2:a4f97773c90f | 305 | { |
donatien | 2:a4f97773c90f | 306 | if (dns_pcb != NULL) { |
donatien | 2:a4f97773c90f | 307 | LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n")); |
donatien | 2:a4f97773c90f | 308 | dns_check_entries(); |
donatien | 2:a4f97773c90f | 309 | } |
donatien | 2:a4f97773c90f | 310 | } |
donatien | 2:a4f97773c90f | 311 | |
donatien | 2:a4f97773c90f | 312 | #if DNS_LOCAL_HOSTLIST |
donatien | 2:a4f97773c90f | 313 | static void |
donatien | 2:a4f97773c90f | 314 | dns_init_local() |
donatien | 2:a4f97773c90f | 315 | { |
donatien | 2:a4f97773c90f | 316 | #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) |
donatien | 2:a4f97773c90f | 317 | int i; |
donatien | 2:a4f97773c90f | 318 | struct local_hostlist_entry *entry; |
donatien | 2:a4f97773c90f | 319 | /* Dynamic: copy entries from DNS_LOCAL_HOSTLIST_INIT to list */ |
donatien | 2:a4f97773c90f | 320 | struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT; |
donatien | 2:a4f97773c90f | 321 | size_t namelen; |
donatien | 2:a4f97773c90f | 322 | for (i = 0; i < sizeof(local_hostlist_init) / sizeof(struct local_hostlist_entry); i++) { |
donatien | 2:a4f97773c90f | 323 | struct local_hostlist_entry *init_entry = &local_hostlist_init[i]; |
donatien | 2:a4f97773c90f | 324 | LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL); |
donatien | 2:a4f97773c90f | 325 | namelen = strlen(init_entry->name); |
donatien | 2:a4f97773c90f | 326 | LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); |
donatien | 2:a4f97773c90f | 327 | entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); |
donatien | 2:a4f97773c90f | 328 | LWIP_ASSERT("mem-error in dns_init_local", entry != NULL); |
donatien | 2:a4f97773c90f | 329 | if (entry != NULL) { |
donatien | 2:a4f97773c90f | 330 | entry->name = (char*)entry + sizeof(struct local_hostlist_entry); |
donatien | 2:a4f97773c90f | 331 | MEMCPY((char*)entry->name, init_entry->name, namelen); |
donatien | 2:a4f97773c90f | 332 | ((char*)entry->name)[namelen] = 0; |
donatien | 2:a4f97773c90f | 333 | entry->addr = init_entry->addr; |
donatien | 2:a4f97773c90f | 334 | entry->next = local_hostlist_dynamic; |
donatien | 2:a4f97773c90f | 335 | local_hostlist_dynamic = entry; |
donatien | 2:a4f97773c90f | 336 | } |
donatien | 2:a4f97773c90f | 337 | } |
donatien | 2:a4f97773c90f | 338 | #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) */ |
donatien | 2:a4f97773c90f | 339 | } |
donatien | 2:a4f97773c90f | 340 | |
donatien | 2:a4f97773c90f | 341 | /** |
donatien | 2:a4f97773c90f | 342 | * Scans the local host-list for a hostname. |
donatien | 2:a4f97773c90f | 343 | * |
donatien | 2:a4f97773c90f | 344 | * @param hostname Hostname to look for in the local host-list |
donatien | 2:a4f97773c90f | 345 | * @return The first IP address for the hostname in the local host-list or |
donatien | 2:a4f97773c90f | 346 | * IPADDR_NONE if not found. |
donatien | 2:a4f97773c90f | 347 | */ |
donatien | 2:a4f97773c90f | 348 | static u32_t |
donatien | 2:a4f97773c90f | 349 | dns_lookup_local(const char *hostname) |
donatien | 2:a4f97773c90f | 350 | { |
donatien | 2:a4f97773c90f | 351 | #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC |
donatien | 2:a4f97773c90f | 352 | struct local_hostlist_entry *entry = local_hostlist_dynamic; |
donatien | 2:a4f97773c90f | 353 | while(entry != NULL) { |
donatien | 2:a4f97773c90f | 354 | if(strcmp(entry->name, hostname) == 0) { |
donatien | 2:a4f97773c90f | 355 | return ip4_addr_get_u32(&entry->addr); |
donatien | 2:a4f97773c90f | 356 | } |
donatien | 2:a4f97773c90f | 357 | entry = entry->next; |
donatien | 2:a4f97773c90f | 358 | } |
donatien | 2:a4f97773c90f | 359 | #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ |
donatien | 2:a4f97773c90f | 360 | int i; |
donatien | 2:a4f97773c90f | 361 | for (i = 0; i < sizeof(local_hostlist_static) / sizeof(struct local_hostlist_entry); i++) { |
donatien | 2:a4f97773c90f | 362 | if(strcmp(local_hostlist_static[i].name, hostname) == 0) { |
donatien | 2:a4f97773c90f | 363 | return ip4_addr_get_u32(&local_hostlist_static[i].addr); |
donatien | 2:a4f97773c90f | 364 | } |
donatien | 2:a4f97773c90f | 365 | } |
donatien | 2:a4f97773c90f | 366 | #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ |
donatien | 2:a4f97773c90f | 367 | return IPADDR_NONE; |
donatien | 2:a4f97773c90f | 368 | } |
donatien | 2:a4f97773c90f | 369 | |
donatien | 2:a4f97773c90f | 370 | #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC |
donatien | 2:a4f97773c90f | 371 | /** Remove all entries from the local host-list for a specific hostname |
donatien | 2:a4f97773c90f | 372 | * and/or IP addess |
donatien | 2:a4f97773c90f | 373 | * |
donatien | 2:a4f97773c90f | 374 | * @param hostname hostname for which entries shall be removed from the local |
donatien | 2:a4f97773c90f | 375 | * host-list |
donatien | 2:a4f97773c90f | 376 | * @param addr address for which entries shall be removed from the local host-list |
donatien | 2:a4f97773c90f | 377 | * @return the number of removed entries |
donatien | 2:a4f97773c90f | 378 | */ |
donatien | 2:a4f97773c90f | 379 | int |
donatien | 2:a4f97773c90f | 380 | dns_local_removehost(const char *hostname, const ip_addr_t *addr) |
donatien | 2:a4f97773c90f | 381 | { |
donatien | 2:a4f97773c90f | 382 | int removed = 0; |
donatien | 2:a4f97773c90f | 383 | struct local_hostlist_entry *entry = local_hostlist_dynamic; |
donatien | 2:a4f97773c90f | 384 | struct local_hostlist_entry *last_entry = NULL; |
donatien | 2:a4f97773c90f | 385 | while (entry != NULL) { |
donatien | 2:a4f97773c90f | 386 | if (((hostname == NULL) || !strcmp(entry->name, hostname)) && |
donatien | 2:a4f97773c90f | 387 | ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) { |
donatien | 2:a4f97773c90f | 388 | struct local_hostlist_entry *free_entry; |
donatien | 2:a4f97773c90f | 389 | if (last_entry != NULL) { |
donatien | 2:a4f97773c90f | 390 | last_entry->next = entry->next; |
donatien | 2:a4f97773c90f | 391 | } else { |
donatien | 2:a4f97773c90f | 392 | local_hostlist_dynamic = entry->next; |
donatien | 2:a4f97773c90f | 393 | } |
donatien | 2:a4f97773c90f | 394 | free_entry = entry; |
donatien | 2:a4f97773c90f | 395 | entry = entry->next; |
donatien | 2:a4f97773c90f | 396 | memp_free(MEMP_LOCALHOSTLIST, free_entry); |
donatien | 2:a4f97773c90f | 397 | removed++; |
donatien | 2:a4f97773c90f | 398 | } else { |
donatien | 2:a4f97773c90f | 399 | last_entry = entry; |
donatien | 2:a4f97773c90f | 400 | entry = entry->next; |
donatien | 2:a4f97773c90f | 401 | } |
donatien | 2:a4f97773c90f | 402 | } |
donatien | 2:a4f97773c90f | 403 | return removed; |
donatien | 2:a4f97773c90f | 404 | } |
donatien | 2:a4f97773c90f | 405 | |
donatien | 2:a4f97773c90f | 406 | /** |
donatien | 2:a4f97773c90f | 407 | * Add a hostname/IP address pair to the local host-list. |
donatien | 2:a4f97773c90f | 408 | * Duplicates are not checked. |
donatien | 2:a4f97773c90f | 409 | * |
donatien | 2:a4f97773c90f | 410 | * @param hostname hostname of the new entry |
donatien | 2:a4f97773c90f | 411 | * @param addr IP address of the new entry |
donatien | 2:a4f97773c90f | 412 | * @return ERR_OK if succeeded or ERR_MEM on memory error |
donatien | 2:a4f97773c90f | 413 | */ |
donatien | 2:a4f97773c90f | 414 | err_t |
donatien | 2:a4f97773c90f | 415 | dns_local_addhost(const char *hostname, const ip_addr_t *addr) |
donatien | 2:a4f97773c90f | 416 | { |
donatien | 2:a4f97773c90f | 417 | struct local_hostlist_entry *entry; |
donatien | 2:a4f97773c90f | 418 | size_t namelen; |
donatien | 2:a4f97773c90f | 419 | LWIP_ASSERT("invalid host name (NULL)", hostname != NULL); |
donatien | 2:a4f97773c90f | 420 | namelen = strlen(hostname); |
donatien | 2:a4f97773c90f | 421 | LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); |
donatien | 2:a4f97773c90f | 422 | entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); |
donatien | 2:a4f97773c90f | 423 | if (entry == NULL) { |
donatien | 2:a4f97773c90f | 424 | return ERR_MEM; |
donatien | 2:a4f97773c90f | 425 | } |
donatien | 2:a4f97773c90f | 426 | entry->name = (char*)entry + sizeof(struct local_hostlist_entry); |
donatien | 2:a4f97773c90f | 427 | MEMCPY((char*)entry->name, hostname, namelen); |
donatien | 2:a4f97773c90f | 428 | ((char*)entry->name)[namelen] = 0; |
donatien | 2:a4f97773c90f | 429 | ip_addr_copy(entry->addr, *addr); |
donatien | 2:a4f97773c90f | 430 | entry->next = local_hostlist_dynamic; |
donatien | 2:a4f97773c90f | 431 | local_hostlist_dynamic = entry; |
donatien | 2:a4f97773c90f | 432 | return ERR_OK; |
donatien | 2:a4f97773c90f | 433 | } |
donatien | 2:a4f97773c90f | 434 | #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC*/ |
donatien | 2:a4f97773c90f | 435 | #endif /* DNS_LOCAL_HOSTLIST */ |
donatien | 2:a4f97773c90f | 436 | |
donatien | 2:a4f97773c90f | 437 | /** |
donatien | 2:a4f97773c90f | 438 | * Look up a hostname in the array of known hostnames. |
donatien | 2:a4f97773c90f | 439 | * |
donatien | 2:a4f97773c90f | 440 | * @note This function only looks in the internal array of known |
donatien | 2:a4f97773c90f | 441 | * hostnames, it does not send out a query for the hostname if none |
donatien | 2:a4f97773c90f | 442 | * was found. The function dns_enqueue() can be used to send a query |
donatien | 2:a4f97773c90f | 443 | * for a hostname. |
donatien | 2:a4f97773c90f | 444 | * |
donatien | 2:a4f97773c90f | 445 | * @param name the hostname to look up |
donatien | 2:a4f97773c90f | 446 | * @return the hostname's IP address, as u32_t (instead of ip_addr_t to |
donatien | 2:a4f97773c90f | 447 | * better check for failure: != IPADDR_NONE) or IPADDR_NONE if the hostname |
donatien | 2:a4f97773c90f | 448 | * was not found in the cached dns_table. |
donatien | 2:a4f97773c90f | 449 | */ |
donatien | 2:a4f97773c90f | 450 | static u32_t |
donatien | 2:a4f97773c90f | 451 | dns_lookup(const char *name) |
donatien | 2:a4f97773c90f | 452 | { |
donatien | 2:a4f97773c90f | 453 | u8_t i; |
donatien | 2:a4f97773c90f | 454 | #if DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) |
donatien | 2:a4f97773c90f | 455 | u32_t addr; |
donatien | 2:a4f97773c90f | 456 | #endif /* DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) */ |
donatien | 2:a4f97773c90f | 457 | #if DNS_LOCAL_HOSTLIST |
donatien | 2:a4f97773c90f | 458 | if ((addr = dns_lookup_local(name)) != IPADDR_NONE) { |
donatien | 2:a4f97773c90f | 459 | return addr; |
donatien | 2:a4f97773c90f | 460 | } |
donatien | 2:a4f97773c90f | 461 | #endif /* DNS_LOCAL_HOSTLIST */ |
donatien | 2:a4f97773c90f | 462 | #ifdef DNS_LOOKUP_LOCAL_EXTERN |
donatien | 2:a4f97773c90f | 463 | if((addr = DNS_LOOKUP_LOCAL_EXTERN(name)) != IPADDR_NONE) { |
donatien | 2:a4f97773c90f | 464 | return addr; |
donatien | 2:a4f97773c90f | 465 | } |
donatien | 2:a4f97773c90f | 466 | #endif /* DNS_LOOKUP_LOCAL_EXTERN */ |
donatien | 2:a4f97773c90f | 467 | |
donatien | 2:a4f97773c90f | 468 | /* Walk through name list, return entry if found. If not, return NULL. */ |
donatien | 2:a4f97773c90f | 469 | for (i = 0; i < DNS_TABLE_SIZE; ++i) { |
donatien | 2:a4f97773c90f | 470 | if ((dns_table[i].state == DNS_STATE_DONE) && |
donatien | 2:a4f97773c90f | 471 | (strcmp(name, dns_table[i].name) == 0)) { |
donatien | 2:a4f97773c90f | 472 | LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name)); |
donatien | 2:a4f97773c90f | 473 | ip_addr_debug_print(DNS_DEBUG, &(dns_table[i].ipaddr)); |
donatien | 2:a4f97773c90f | 474 | LWIP_DEBUGF(DNS_DEBUG, ("\n")); |
donatien | 2:a4f97773c90f | 475 | return ip4_addr_get_u32(&dns_table[i].ipaddr); |
donatien | 2:a4f97773c90f | 476 | } |
donatien | 2:a4f97773c90f | 477 | } |
donatien | 2:a4f97773c90f | 478 | |
donatien | 2:a4f97773c90f | 479 | return IPADDR_NONE; |
donatien | 2:a4f97773c90f | 480 | } |
donatien | 2:a4f97773c90f | 481 | |
donatien | 2:a4f97773c90f | 482 | #if DNS_DOES_NAME_CHECK |
donatien | 2:a4f97773c90f | 483 | /** |
donatien | 2:a4f97773c90f | 484 | * Compare the "dotted" name "query" with the encoded name "response" |
donatien | 2:a4f97773c90f | 485 | * to make sure an answer from the DNS server matches the current dns_table |
donatien | 2:a4f97773c90f | 486 | * entry (otherwise, answers might arrive late for hostname not on the list |
donatien | 2:a4f97773c90f | 487 | * any more). |
donatien | 2:a4f97773c90f | 488 | * |
donatien | 2:a4f97773c90f | 489 | * @param query hostname (not encoded) from the dns_table |
donatien | 2:a4f97773c90f | 490 | * @param response encoded hostname in the DNS response |
donatien | 2:a4f97773c90f | 491 | * @return 0: names equal; 1: names differ |
donatien | 2:a4f97773c90f | 492 | */ |
donatien | 2:a4f97773c90f | 493 | static u8_t |
donatien | 2:a4f97773c90f | 494 | dns_compare_name(unsigned char *query, unsigned char *response) |
donatien | 2:a4f97773c90f | 495 | { |
donatien | 2:a4f97773c90f | 496 | unsigned char n; |
donatien | 2:a4f97773c90f | 497 | |
donatien | 2:a4f97773c90f | 498 | do { |
donatien | 2:a4f97773c90f | 499 | n = *response++; |
donatien | 2:a4f97773c90f | 500 | /** @see RFC 1035 - 4.1.4. Message compression */ |
donatien | 2:a4f97773c90f | 501 | if ((n & 0xc0) == 0xc0) { |
donatien | 2:a4f97773c90f | 502 | /* Compressed name */ |
donatien | 2:a4f97773c90f | 503 | break; |
donatien | 2:a4f97773c90f | 504 | } else { |
donatien | 2:a4f97773c90f | 505 | /* Not compressed name */ |
donatien | 2:a4f97773c90f | 506 | while (n > 0) { |
donatien | 2:a4f97773c90f | 507 | if ((*query) != (*response)) { |
donatien | 2:a4f97773c90f | 508 | return 1; |
donatien | 2:a4f97773c90f | 509 | } |
donatien | 2:a4f97773c90f | 510 | ++response; |
donatien | 2:a4f97773c90f | 511 | ++query; |
donatien | 2:a4f97773c90f | 512 | --n; |
donatien | 2:a4f97773c90f | 513 | }; |
donatien | 2:a4f97773c90f | 514 | ++query; |
donatien | 2:a4f97773c90f | 515 | } |
donatien | 2:a4f97773c90f | 516 | } while (*response != 0); |
donatien | 2:a4f97773c90f | 517 | |
donatien | 2:a4f97773c90f | 518 | return 0; |
donatien | 2:a4f97773c90f | 519 | } |
donatien | 2:a4f97773c90f | 520 | #endif /* DNS_DOES_NAME_CHECK */ |
donatien | 2:a4f97773c90f | 521 | |
donatien | 2:a4f97773c90f | 522 | /** |
donatien | 2:a4f97773c90f | 523 | * Walk through a compact encoded DNS name and return the end of the name. |
donatien | 2:a4f97773c90f | 524 | * |
donatien | 2:a4f97773c90f | 525 | * @param query encoded DNS name in the DNS server response |
donatien | 2:a4f97773c90f | 526 | * @return end of the name |
donatien | 2:a4f97773c90f | 527 | */ |
donatien | 2:a4f97773c90f | 528 | static unsigned char * |
donatien | 2:a4f97773c90f | 529 | dns_parse_name(unsigned char *query) |
donatien | 2:a4f97773c90f | 530 | { |
donatien | 2:a4f97773c90f | 531 | unsigned char n; |
donatien | 2:a4f97773c90f | 532 | |
donatien | 2:a4f97773c90f | 533 | do { |
donatien | 2:a4f97773c90f | 534 | n = *query++; |
donatien | 2:a4f97773c90f | 535 | /** @see RFC 1035 - 4.1.4. Message compression */ |
donatien | 2:a4f97773c90f | 536 | if ((n & 0xc0) == 0xc0) { |
donatien | 2:a4f97773c90f | 537 | /* Compressed name */ |
donatien | 2:a4f97773c90f | 538 | break; |
donatien | 2:a4f97773c90f | 539 | } else { |
donatien | 2:a4f97773c90f | 540 | /* Not compressed name */ |
donatien | 2:a4f97773c90f | 541 | while (n > 0) { |
donatien | 2:a4f97773c90f | 542 | ++query; |
donatien | 2:a4f97773c90f | 543 | --n; |
donatien | 2:a4f97773c90f | 544 | }; |
donatien | 2:a4f97773c90f | 545 | } |
donatien | 2:a4f97773c90f | 546 | } while (*query != 0); |
donatien | 2:a4f97773c90f | 547 | |
donatien | 2:a4f97773c90f | 548 | return query + 1; |
donatien | 2:a4f97773c90f | 549 | } |
donatien | 2:a4f97773c90f | 550 | |
donatien | 2:a4f97773c90f | 551 | /** |
donatien | 2:a4f97773c90f | 552 | * Send a DNS query packet. |
donatien | 2:a4f97773c90f | 553 | * |
donatien | 2:a4f97773c90f | 554 | * @param numdns index of the DNS server in the dns_servers table |
donatien | 2:a4f97773c90f | 555 | * @param name hostname to query |
donatien | 2:a4f97773c90f | 556 | * @param id index of the hostname in dns_table, used as transaction ID in the |
donatien | 2:a4f97773c90f | 557 | * DNS query packet |
donatien | 2:a4f97773c90f | 558 | * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise |
donatien | 2:a4f97773c90f | 559 | */ |
donatien | 2:a4f97773c90f | 560 | static err_t |
donatien | 2:a4f97773c90f | 561 | dns_send(u8_t numdns, const char* name, u8_t id) |
donatien | 2:a4f97773c90f | 562 | { |
donatien | 2:a4f97773c90f | 563 | err_t err; |
donatien | 2:a4f97773c90f | 564 | struct dns_hdr *hdr; |
donatien | 2:a4f97773c90f | 565 | struct dns_query qry; |
donatien | 2:a4f97773c90f | 566 | struct pbuf *p; |
donatien | 2:a4f97773c90f | 567 | char *query, *nptr; |
donatien | 2:a4f97773c90f | 568 | const char *pHostname; |
donatien | 2:a4f97773c90f | 569 | u8_t n; |
donatien | 2:a4f97773c90f | 570 | |
donatien | 2:a4f97773c90f | 571 | LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n", |
donatien | 2:a4f97773c90f | 572 | (u16_t)(numdns), name)); |
donatien | 2:a4f97773c90f | 573 | LWIP_ASSERT("dns server out of array", numdns < DNS_MAX_SERVERS); |
donatien | 2:a4f97773c90f | 574 | LWIP_ASSERT("dns server has no IP address set", !ip_addr_isany(&dns_servers[numdns])); |
donatien | 2:a4f97773c90f | 575 | |
donatien | 2:a4f97773c90f | 576 | /* if here, we have either a new query or a retry on a previous query to process */ |
donatien | 2:a4f97773c90f | 577 | p = pbuf_alloc(PBUF_TRANSPORT, SIZEOF_DNS_HDR + DNS_MAX_NAME_LENGTH + |
donatien | 2:a4f97773c90f | 578 | SIZEOF_DNS_QUERY, PBUF_RAM); |
donatien | 2:a4f97773c90f | 579 | if (p != NULL) { |
donatien | 2:a4f97773c90f | 580 | LWIP_ASSERT("pbuf must be in one piece", p->next == NULL); |
donatien | 2:a4f97773c90f | 581 | /* fill dns header */ |
donatien | 2:a4f97773c90f | 582 | hdr = (struct dns_hdr*)p->payload; |
donatien | 2:a4f97773c90f | 583 | memset(hdr, 0, SIZEOF_DNS_HDR); |
donatien | 2:a4f97773c90f | 584 | hdr->id = htons(id); |
donatien | 2:a4f97773c90f | 585 | hdr->flags1 = DNS_FLAG1_RD; |
donatien | 2:a4f97773c90f | 586 | hdr->numquestions = PP_HTONS(1); |
donatien | 2:a4f97773c90f | 587 | query = (char*)hdr + SIZEOF_DNS_HDR; |
donatien | 2:a4f97773c90f | 588 | pHostname = name; |
donatien | 2:a4f97773c90f | 589 | --pHostname; |
donatien | 2:a4f97773c90f | 590 | |
donatien | 2:a4f97773c90f | 591 | /* convert hostname into suitable query format. */ |
donatien | 2:a4f97773c90f | 592 | do { |
donatien | 2:a4f97773c90f | 593 | ++pHostname; |
donatien | 2:a4f97773c90f | 594 | nptr = query; |
donatien | 2:a4f97773c90f | 595 | ++query; |
donatien | 2:a4f97773c90f | 596 | for(n = 0; *pHostname != '.' && *pHostname != 0; ++pHostname) { |
donatien | 2:a4f97773c90f | 597 | *query = *pHostname; |
donatien | 2:a4f97773c90f | 598 | ++query; |
donatien | 2:a4f97773c90f | 599 | ++n; |
donatien | 2:a4f97773c90f | 600 | } |
donatien | 2:a4f97773c90f | 601 | *nptr = n; |
donatien | 2:a4f97773c90f | 602 | } while(*pHostname != 0); |
donatien | 2:a4f97773c90f | 603 | *query++='\0'; |
donatien | 2:a4f97773c90f | 604 | |
donatien | 2:a4f97773c90f | 605 | /* fill dns query */ |
donatien | 2:a4f97773c90f | 606 | qry.type = PP_HTONS(DNS_RRTYPE_A); |
donatien | 2:a4f97773c90f | 607 | qry.cls = PP_HTONS(DNS_RRCLASS_IN); |
donatien | 2:a4f97773c90f | 608 | SMEMCPY(query, &qry, SIZEOF_DNS_QUERY); |
donatien | 2:a4f97773c90f | 609 | |
donatien | 2:a4f97773c90f | 610 | /* resize pbuf to the exact dns query */ |
donatien | 2:a4f97773c90f | 611 | pbuf_realloc(p, (u16_t)((query + SIZEOF_DNS_QUERY) - ((char*)(p->payload)))); |
donatien | 2:a4f97773c90f | 612 | |
donatien | 2:a4f97773c90f | 613 | /* connect to the server for faster receiving */ |
donatien | 2:a4f97773c90f | 614 | udp_connect(dns_pcb, &dns_servers[numdns], DNS_SERVER_PORT); |
donatien | 2:a4f97773c90f | 615 | /* send dns packet */ |
donatien | 2:a4f97773c90f | 616 | err = udp_sendto(dns_pcb, p, &dns_servers[numdns], DNS_SERVER_PORT); |
donatien | 2:a4f97773c90f | 617 | |
donatien | 2:a4f97773c90f | 618 | /* free pbuf */ |
donatien | 2:a4f97773c90f | 619 | pbuf_free(p); |
donatien | 2:a4f97773c90f | 620 | } else { |
donatien | 2:a4f97773c90f | 621 | err = ERR_MEM; |
donatien | 2:a4f97773c90f | 622 | } |
donatien | 2:a4f97773c90f | 623 | |
donatien | 2:a4f97773c90f | 624 | return err; |
donatien | 2:a4f97773c90f | 625 | } |
donatien | 2:a4f97773c90f | 626 | |
donatien | 2:a4f97773c90f | 627 | /** |
donatien | 2:a4f97773c90f | 628 | * dns_check_entry() - see if pEntry has not yet been queried and, if so, sends out a query. |
donatien | 2:a4f97773c90f | 629 | * Check an entry in the dns_table: |
donatien | 2:a4f97773c90f | 630 | * - send out query for new entries |
donatien | 2:a4f97773c90f | 631 | * - retry old pending entries on timeout (also with different servers) |
donatien | 2:a4f97773c90f | 632 | * - remove completed entries from the table if their TTL has expired |
donatien | 2:a4f97773c90f | 633 | * |
donatien | 2:a4f97773c90f | 634 | * @param i index of the dns_table entry to check |
donatien | 2:a4f97773c90f | 635 | */ |
donatien | 2:a4f97773c90f | 636 | static void |
donatien | 2:a4f97773c90f | 637 | dns_check_entry(u8_t i) |
donatien | 2:a4f97773c90f | 638 | { |
donatien | 2:a4f97773c90f | 639 | err_t err; |
donatien | 2:a4f97773c90f | 640 | struct dns_table_entry *pEntry = &dns_table[i]; |
donatien | 2:a4f97773c90f | 641 | |
donatien | 2:a4f97773c90f | 642 | LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE); |
donatien | 2:a4f97773c90f | 643 | |
donatien | 2:a4f97773c90f | 644 | switch(pEntry->state) { |
donatien | 2:a4f97773c90f | 645 | |
donatien | 2:a4f97773c90f | 646 | case DNS_STATE_NEW: { |
donatien | 2:a4f97773c90f | 647 | /* initialize new entry */ |
donatien | 2:a4f97773c90f | 648 | pEntry->state = DNS_STATE_ASKING; |
donatien | 2:a4f97773c90f | 649 | pEntry->numdns = 0; |
donatien | 2:a4f97773c90f | 650 | pEntry->tmr = 1; |
donatien | 2:a4f97773c90f | 651 | pEntry->retries = 0; |
donatien | 2:a4f97773c90f | 652 | |
donatien | 2:a4f97773c90f | 653 | /* send DNS packet for this entry */ |
donatien | 2:a4f97773c90f | 654 | err = dns_send(pEntry->numdns, pEntry->name, i); |
donatien | 2:a4f97773c90f | 655 | if (err != ERR_OK) { |
donatien | 2:a4f97773c90f | 656 | LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, |
donatien | 2:a4f97773c90f | 657 | ("dns_send returned error: %d\n", err)); |
donatien | 2:a4f97773c90f | 658 | } |
donatien | 2:a4f97773c90f | 659 | break; |
donatien | 2:a4f97773c90f | 660 | } |
donatien | 2:a4f97773c90f | 661 | |
donatien | 2:a4f97773c90f | 662 | case DNS_STATE_ASKING: { |
donatien | 2:a4f97773c90f | 663 | if (--pEntry->tmr == 0) { |
donatien | 2:a4f97773c90f | 664 | if (++pEntry->retries == DNS_MAX_RETRIES) { |
donatien | 2:a4f97773c90f | 665 | if ((pEntry->numdns+1<DNS_MAX_SERVERS) && !ip_addr_isany(&dns_servers[pEntry->numdns+1])) { |
donatien | 2:a4f97773c90f | 666 | /* change of server */ |
donatien | 2:a4f97773c90f | 667 | pEntry->numdns++; |
donatien | 2:a4f97773c90f | 668 | pEntry->tmr = 1; |
donatien | 2:a4f97773c90f | 669 | pEntry->retries = 0; |
donatien | 2:a4f97773c90f | 670 | break; |
donatien | 2:a4f97773c90f | 671 | } else { |
donatien | 2:a4f97773c90f | 672 | LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", pEntry->name)); |
donatien | 2:a4f97773c90f | 673 | /* call specified callback function if provided */ |
donatien | 2:a4f97773c90f | 674 | if (pEntry->found) |
donatien | 2:a4f97773c90f | 675 | (*pEntry->found)(pEntry->name, NULL, pEntry->arg); |
donatien | 2:a4f97773c90f | 676 | /* flush this entry */ |
donatien | 2:a4f97773c90f | 677 | pEntry->state = DNS_STATE_UNUSED; |
donatien | 2:a4f97773c90f | 678 | pEntry->found = NULL; |
donatien | 2:a4f97773c90f | 679 | break; |
donatien | 2:a4f97773c90f | 680 | } |
donatien | 2:a4f97773c90f | 681 | } |
donatien | 2:a4f97773c90f | 682 | |
donatien | 2:a4f97773c90f | 683 | /* wait longer for the next retry */ |
donatien | 2:a4f97773c90f | 684 | pEntry->tmr = pEntry->retries; |
donatien | 2:a4f97773c90f | 685 | |
donatien | 2:a4f97773c90f | 686 | /* send DNS packet for this entry */ |
donatien | 2:a4f97773c90f | 687 | err = dns_send(pEntry->numdns, pEntry->name, i); |
donatien | 2:a4f97773c90f | 688 | if (err != ERR_OK) { |
donatien | 2:a4f97773c90f | 689 | LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, |
donatien | 2:a4f97773c90f | 690 | ("dns_send returned error: %d\n", err)); |
donatien | 2:a4f97773c90f | 691 | } |
donatien | 2:a4f97773c90f | 692 | } |
donatien | 2:a4f97773c90f | 693 | break; |
donatien | 2:a4f97773c90f | 694 | } |
donatien | 2:a4f97773c90f | 695 | |
donatien | 2:a4f97773c90f | 696 | case DNS_STATE_DONE: { |
donatien | 2:a4f97773c90f | 697 | /* if the time to live is nul */ |
donatien | 2:a4f97773c90f | 698 | if (--pEntry->ttl == 0) { |
donatien | 2:a4f97773c90f | 699 | LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name)); |
donatien | 2:a4f97773c90f | 700 | /* flush this entry */ |
donatien | 2:a4f97773c90f | 701 | pEntry->state = DNS_STATE_UNUSED; |
donatien | 2:a4f97773c90f | 702 | pEntry->found = NULL; |
donatien | 2:a4f97773c90f | 703 | } |
donatien | 2:a4f97773c90f | 704 | break; |
donatien | 2:a4f97773c90f | 705 | } |
donatien | 2:a4f97773c90f | 706 | case DNS_STATE_UNUSED: |
donatien | 2:a4f97773c90f | 707 | /* nothing to do */ |
donatien | 2:a4f97773c90f | 708 | break; |
donatien | 2:a4f97773c90f | 709 | default: |
donatien | 2:a4f97773c90f | 710 | LWIP_ASSERT("unknown dns_table entry state:", 0); |
donatien | 2:a4f97773c90f | 711 | break; |
donatien | 2:a4f97773c90f | 712 | } |
donatien | 2:a4f97773c90f | 713 | } |
donatien | 2:a4f97773c90f | 714 | |
donatien | 2:a4f97773c90f | 715 | /** |
donatien | 2:a4f97773c90f | 716 | * Call dns_check_entry for each entry in dns_table - check all entries. |
donatien | 2:a4f97773c90f | 717 | */ |
donatien | 2:a4f97773c90f | 718 | static void |
donatien | 2:a4f97773c90f | 719 | dns_check_entries(void) |
donatien | 2:a4f97773c90f | 720 | { |
donatien | 2:a4f97773c90f | 721 | u8_t i; |
donatien | 2:a4f97773c90f | 722 | |
donatien | 2:a4f97773c90f | 723 | for (i = 0; i < DNS_TABLE_SIZE; ++i) { |
donatien | 2:a4f97773c90f | 724 | dns_check_entry(i); |
donatien | 2:a4f97773c90f | 725 | } |
donatien | 2:a4f97773c90f | 726 | } |
donatien | 2:a4f97773c90f | 727 | |
donatien | 2:a4f97773c90f | 728 | /** |
donatien | 2:a4f97773c90f | 729 | * Receive input function for DNS response packets arriving for the dns UDP pcb. |
donatien | 2:a4f97773c90f | 730 | * |
donatien | 2:a4f97773c90f | 731 | * @params see udp.h |
donatien | 2:a4f97773c90f | 732 | */ |
donatien | 2:a4f97773c90f | 733 | static void |
donatien | 2:a4f97773c90f | 734 | dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port) |
donatien | 2:a4f97773c90f | 735 | { |
donatien | 2:a4f97773c90f | 736 | u16_t i; |
donatien | 2:a4f97773c90f | 737 | char *pHostname; |
donatien | 2:a4f97773c90f | 738 | struct dns_hdr *hdr; |
donatien | 2:a4f97773c90f | 739 | struct dns_answer ans; |
donatien | 2:a4f97773c90f | 740 | struct dns_table_entry *pEntry; |
donatien | 2:a4f97773c90f | 741 | u16_t nquestions, nanswers; |
donatien | 2:a4f97773c90f | 742 | |
donatien | 2:a4f97773c90f | 743 | LWIP_UNUSED_ARG(arg); |
donatien | 2:a4f97773c90f | 744 | LWIP_UNUSED_ARG(pcb); |
donatien | 2:a4f97773c90f | 745 | LWIP_UNUSED_ARG(addr); |
donatien | 2:a4f97773c90f | 746 | LWIP_UNUSED_ARG(port); |
donatien | 2:a4f97773c90f | 747 | |
donatien | 2:a4f97773c90f | 748 | /* is the dns message too big ? */ |
donatien | 2:a4f97773c90f | 749 | if (p->tot_len > DNS_MSG_SIZE) { |
donatien | 2:a4f97773c90f | 750 | LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too big\n")); |
donatien | 2:a4f97773c90f | 751 | /* free pbuf and return */ |
donatien | 2:a4f97773c90f | 752 | goto memerr; |
donatien | 2:a4f97773c90f | 753 | } |
donatien | 2:a4f97773c90f | 754 | |
donatien | 2:a4f97773c90f | 755 | /* is the dns message big enough ? */ |
donatien | 2:a4f97773c90f | 756 | if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY + SIZEOF_DNS_ANSWER)) { |
donatien | 2:a4f97773c90f | 757 | LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n")); |
donatien | 2:a4f97773c90f | 758 | /* free pbuf and return */ |
donatien | 2:a4f97773c90f | 759 | goto memerr; |
donatien | 2:a4f97773c90f | 760 | } |
donatien | 2:a4f97773c90f | 761 | |
donatien | 2:a4f97773c90f | 762 | /* copy dns payload inside static buffer for processing */ |
donatien | 2:a4f97773c90f | 763 | if (pbuf_copy_partial(p, dns_payload, p->tot_len, 0) == p->tot_len) { |
donatien | 2:a4f97773c90f | 764 | /* The ID in the DNS header should be our entry into the name table. */ |
donatien | 2:a4f97773c90f | 765 | hdr = (struct dns_hdr*)dns_payload; |
donatien | 2:a4f97773c90f | 766 | i = htons(hdr->id); |
donatien | 2:a4f97773c90f | 767 | if (i < DNS_TABLE_SIZE) { |
donatien | 2:a4f97773c90f | 768 | pEntry = &dns_table[i]; |
donatien | 2:a4f97773c90f | 769 | if(pEntry->state == DNS_STATE_ASKING) { |
donatien | 2:a4f97773c90f | 770 | /* This entry is now completed. */ |
donatien | 2:a4f97773c90f | 771 | pEntry->state = DNS_STATE_DONE; |
donatien | 2:a4f97773c90f | 772 | pEntry->err = hdr->flags2 & DNS_FLAG2_ERR_MASK; |
donatien | 2:a4f97773c90f | 773 | |
donatien | 2:a4f97773c90f | 774 | /* We only care about the question(s) and the answers. The authrr |
donatien | 2:a4f97773c90f | 775 | and the extrarr are simply discarded. */ |
donatien | 2:a4f97773c90f | 776 | nquestions = htons(hdr->numquestions); |
donatien | 2:a4f97773c90f | 777 | nanswers = htons(hdr->numanswers); |
donatien | 2:a4f97773c90f | 778 | |
donatien | 2:a4f97773c90f | 779 | /* Check for error. If so, call callback to inform. */ |
donatien | 2:a4f97773c90f | 780 | if (((hdr->flags1 & DNS_FLAG1_RESPONSE) == 0) || (pEntry->err != 0) || (nquestions != 1)) { |
donatien | 2:a4f97773c90f | 781 | LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", pEntry->name)); |
donatien | 2:a4f97773c90f | 782 | /* call callback to indicate error, clean up memory and return */ |
donatien | 2:a4f97773c90f | 783 | goto responseerr; |
donatien | 2:a4f97773c90f | 784 | } |
donatien | 2:a4f97773c90f | 785 | |
donatien | 2:a4f97773c90f | 786 | #if DNS_DOES_NAME_CHECK |
donatien | 2:a4f97773c90f | 787 | /* Check if the name in the "question" part match with the name in the entry. */ |
donatien | 2:a4f97773c90f | 788 | if (dns_compare_name((unsigned char *)(pEntry->name), (unsigned char *)dns_payload + SIZEOF_DNS_HDR) != 0) { |
donatien | 2:a4f97773c90f | 789 | LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", pEntry->name)); |
donatien | 2:a4f97773c90f | 790 | /* call callback to indicate error, clean up memory and return */ |
donatien | 2:a4f97773c90f | 791 | goto responseerr; |
donatien | 2:a4f97773c90f | 792 | } |
donatien | 2:a4f97773c90f | 793 | #endif /* DNS_DOES_NAME_CHECK */ |
donatien | 2:a4f97773c90f | 794 | |
donatien | 2:a4f97773c90f | 795 | /* Skip the name in the "question" part */ |
donatien | 2:a4f97773c90f | 796 | pHostname = (char *) dns_parse_name((unsigned char *)dns_payload + SIZEOF_DNS_HDR) + SIZEOF_DNS_QUERY; |
donatien | 2:a4f97773c90f | 797 | |
donatien | 2:a4f97773c90f | 798 | while (nanswers > 0) { |
donatien | 2:a4f97773c90f | 799 | /* skip answer resource record's host name */ |
donatien | 2:a4f97773c90f | 800 | pHostname = (char *) dns_parse_name((unsigned char *)pHostname); |
donatien | 2:a4f97773c90f | 801 | |
donatien | 2:a4f97773c90f | 802 | /* Check for IP address type and Internet class. Others are discarded. */ |
donatien | 2:a4f97773c90f | 803 | SMEMCPY(&ans, pHostname, SIZEOF_DNS_ANSWER); |
donatien | 2:a4f97773c90f | 804 | if((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) && |
donatien | 2:a4f97773c90f | 805 | (ans.len == PP_HTONS(sizeof(ip_addr_t))) ) { |
donatien | 2:a4f97773c90f | 806 | /* read the answer resource record's TTL, and maximize it if needed */ |
donatien | 2:a4f97773c90f | 807 | pEntry->ttl = ntohl(ans.ttl); |
donatien | 2:a4f97773c90f | 808 | if (pEntry->ttl > DNS_MAX_TTL) { |
donatien | 2:a4f97773c90f | 809 | pEntry->ttl = DNS_MAX_TTL; |
donatien | 2:a4f97773c90f | 810 | } |
donatien | 2:a4f97773c90f | 811 | /* read the IP address after answer resource record's header */ |
donatien | 2:a4f97773c90f | 812 | SMEMCPY(&(pEntry->ipaddr), (pHostname+SIZEOF_DNS_ANSWER), sizeof(ip_addr_t)); |
donatien | 2:a4f97773c90f | 813 | LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", pEntry->name)); |
donatien | 2:a4f97773c90f | 814 | ip_addr_debug_print(DNS_DEBUG, (&(pEntry->ipaddr))); |
donatien | 2:a4f97773c90f | 815 | LWIP_DEBUGF(DNS_DEBUG, ("\n")); |
donatien | 2:a4f97773c90f | 816 | /* call specified callback function if provided */ |
donatien | 2:a4f97773c90f | 817 | if (pEntry->found) { |
donatien | 2:a4f97773c90f | 818 | (*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg); |
donatien | 2:a4f97773c90f | 819 | } |
donatien | 2:a4f97773c90f | 820 | /* deallocate memory and return */ |
donatien | 2:a4f97773c90f | 821 | goto memerr; |
donatien | 2:a4f97773c90f | 822 | } else { |
donatien | 2:a4f97773c90f | 823 | pHostname = pHostname + SIZEOF_DNS_ANSWER + htons(ans.len); |
donatien | 2:a4f97773c90f | 824 | } |
donatien | 2:a4f97773c90f | 825 | --nanswers; |
donatien | 2:a4f97773c90f | 826 | } |
donatien | 2:a4f97773c90f | 827 | LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", pEntry->name)); |
donatien | 2:a4f97773c90f | 828 | /* call callback to indicate error, clean up memory and return */ |
donatien | 2:a4f97773c90f | 829 | goto responseerr; |
donatien | 2:a4f97773c90f | 830 | } |
donatien | 2:a4f97773c90f | 831 | } |
donatien | 2:a4f97773c90f | 832 | } |
donatien | 2:a4f97773c90f | 833 | |
donatien | 2:a4f97773c90f | 834 | /* deallocate memory and return */ |
donatien | 2:a4f97773c90f | 835 | goto memerr; |
donatien | 2:a4f97773c90f | 836 | |
donatien | 2:a4f97773c90f | 837 | responseerr: |
donatien | 2:a4f97773c90f | 838 | /* ERROR: call specified callback function with NULL as name to indicate an error */ |
donatien | 2:a4f97773c90f | 839 | if (pEntry->found) { |
donatien | 2:a4f97773c90f | 840 | (*pEntry->found)(pEntry->name, NULL, pEntry->arg); |
donatien | 2:a4f97773c90f | 841 | } |
donatien | 2:a4f97773c90f | 842 | /* flush this entry */ |
donatien | 2:a4f97773c90f | 843 | pEntry->state = DNS_STATE_UNUSED; |
donatien | 2:a4f97773c90f | 844 | pEntry->found = NULL; |
donatien | 2:a4f97773c90f | 845 | |
donatien | 2:a4f97773c90f | 846 | memerr: |
donatien | 2:a4f97773c90f | 847 | /* free pbuf */ |
donatien | 2:a4f97773c90f | 848 | pbuf_free(p); |
donatien | 2:a4f97773c90f | 849 | return; |
donatien | 2:a4f97773c90f | 850 | } |
donatien | 2:a4f97773c90f | 851 | |
donatien | 2:a4f97773c90f | 852 | /** |
donatien | 2:a4f97773c90f | 853 | * Queues a new hostname to resolve and sends out a DNS query for that hostname |
donatien | 2:a4f97773c90f | 854 | * |
donatien | 2:a4f97773c90f | 855 | * @param name the hostname that is to be queried |
donatien | 2:a4f97773c90f | 856 | * @param found a callback founction to be called on success, failure or timeout |
donatien | 2:a4f97773c90f | 857 | * @param callback_arg argument to pass to the callback function |
donatien | 2:a4f97773c90f | 858 | * @return @return a err_t return code. |
donatien | 2:a4f97773c90f | 859 | */ |
donatien | 2:a4f97773c90f | 860 | static err_t |
donatien | 2:a4f97773c90f | 861 | dns_enqueue(const char *name, dns_found_callback found, void *callback_arg) |
donatien | 2:a4f97773c90f | 862 | { |
donatien | 2:a4f97773c90f | 863 | u8_t i; |
donatien | 2:a4f97773c90f | 864 | u8_t lseq, lseqi; |
donatien | 2:a4f97773c90f | 865 | struct dns_table_entry *pEntry = NULL; |
donatien | 2:a4f97773c90f | 866 | size_t namelen; |
donatien | 2:a4f97773c90f | 867 | |
donatien | 2:a4f97773c90f | 868 | /* search an unused entry, or the oldest one */ |
donatien | 2:a4f97773c90f | 869 | lseq = lseqi = 0; |
donatien | 2:a4f97773c90f | 870 | for (i = 0; i < DNS_TABLE_SIZE; ++i) { |
donatien | 2:a4f97773c90f | 871 | pEntry = &dns_table[i]; |
donatien | 2:a4f97773c90f | 872 | /* is it an unused entry ? */ |
donatien | 2:a4f97773c90f | 873 | if (pEntry->state == DNS_STATE_UNUSED) |
donatien | 2:a4f97773c90f | 874 | break; |
donatien | 2:a4f97773c90f | 875 | |
donatien | 2:a4f97773c90f | 876 | /* check if this is the oldest completed entry */ |
donatien | 2:a4f97773c90f | 877 | if (pEntry->state == DNS_STATE_DONE) { |
donatien | 2:a4f97773c90f | 878 | if ((dns_seqno - pEntry->seqno) > lseq) { |
donatien | 2:a4f97773c90f | 879 | lseq = dns_seqno - pEntry->seqno; |
donatien | 2:a4f97773c90f | 880 | lseqi = i; |
donatien | 2:a4f97773c90f | 881 | } |
donatien | 2:a4f97773c90f | 882 | } |
donatien | 2:a4f97773c90f | 883 | } |
donatien | 2:a4f97773c90f | 884 | |
donatien | 2:a4f97773c90f | 885 | /* if we don't have found an unused entry, use the oldest completed one */ |
donatien | 2:a4f97773c90f | 886 | if (i == DNS_TABLE_SIZE) { |
donatien | 2:a4f97773c90f | 887 | if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) { |
donatien | 2:a4f97773c90f | 888 | /* no entry can't be used now, table is full */ |
donatien | 2:a4f97773c90f | 889 | LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name)); |
donatien | 2:a4f97773c90f | 890 | return ERR_MEM; |
donatien | 2:a4f97773c90f | 891 | } else { |
donatien | 2:a4f97773c90f | 892 | /* use the oldest completed one */ |
donatien | 2:a4f97773c90f | 893 | i = lseqi; |
donatien | 2:a4f97773c90f | 894 | pEntry = &dns_table[i]; |
donatien | 2:a4f97773c90f | 895 | } |
donatien | 2:a4f97773c90f | 896 | } |
donatien | 2:a4f97773c90f | 897 | |
donatien | 2:a4f97773c90f | 898 | /* use this entry */ |
donatien | 2:a4f97773c90f | 899 | LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i))); |
donatien | 2:a4f97773c90f | 900 | |
donatien | 2:a4f97773c90f | 901 | /* fill the entry */ |
donatien | 2:a4f97773c90f | 902 | pEntry->state = DNS_STATE_NEW; |
donatien | 2:a4f97773c90f | 903 | pEntry->seqno = dns_seqno++; |
donatien | 2:a4f97773c90f | 904 | pEntry->found = found; |
donatien | 2:a4f97773c90f | 905 | pEntry->arg = callback_arg; |
donatien | 2:a4f97773c90f | 906 | namelen = LWIP_MIN(strlen(name), DNS_MAX_NAME_LENGTH-1); |
donatien | 2:a4f97773c90f | 907 | MEMCPY(pEntry->name, name, namelen); |
donatien | 2:a4f97773c90f | 908 | pEntry->name[namelen] = 0; |
donatien | 2:a4f97773c90f | 909 | |
donatien | 2:a4f97773c90f | 910 | /* force to send query without waiting timer */ |
donatien | 2:a4f97773c90f | 911 | dns_check_entry(i); |
donatien | 2:a4f97773c90f | 912 | |
donatien | 2:a4f97773c90f | 913 | /* dns query is enqueued */ |
donatien | 2:a4f97773c90f | 914 | return ERR_INPROGRESS; |
donatien | 2:a4f97773c90f | 915 | } |
donatien | 2:a4f97773c90f | 916 | |
donatien | 2:a4f97773c90f | 917 | /** |
donatien | 2:a4f97773c90f | 918 | * Resolve a hostname (string) into an IP address. |
donatien | 2:a4f97773c90f | 919 | * NON-BLOCKING callback version for use with raw API!!! |
donatien | 2:a4f97773c90f | 920 | * |
donatien | 2:a4f97773c90f | 921 | * Returns immediately with one of err_t return codes: |
donatien | 2:a4f97773c90f | 922 | * - ERR_OK if hostname is a valid IP address string or the host |
donatien | 2:a4f97773c90f | 923 | * name is already in the local names table. |
donatien | 2:a4f97773c90f | 924 | * - ERR_INPROGRESS enqueue a request to be sent to the DNS server |
donatien | 2:a4f97773c90f | 925 | * for resolution if no errors are present. |
donatien | 2:a4f97773c90f | 926 | * |
donatien | 2:a4f97773c90f | 927 | * @param hostname the hostname that is to be queried |
donatien | 2:a4f97773c90f | 928 | * @param addr pointer to a ip_addr_t where to store the address if it is already |
donatien | 2:a4f97773c90f | 929 | * cached in the dns_table (only valid if ERR_OK is returned!) |
donatien | 2:a4f97773c90f | 930 | * @param found a callback function to be called on success, failure or timeout (only if |
donatien | 2:a4f97773c90f | 931 | * ERR_INPROGRESS is returned!) |
donatien | 2:a4f97773c90f | 932 | * @param callback_arg argument to pass to the callback function |
donatien | 2:a4f97773c90f | 933 | * @return a err_t return code. |
donatien | 2:a4f97773c90f | 934 | */ |
donatien | 2:a4f97773c90f | 935 | err_t |
donatien | 2:a4f97773c90f | 936 | dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, |
donatien | 2:a4f97773c90f | 937 | void *callback_arg) |
donatien | 2:a4f97773c90f | 938 | { |
donatien | 2:a4f97773c90f | 939 | u32_t ipaddr; |
donatien | 2:a4f97773c90f | 940 | /* not initialized or no valid server yet, or invalid addr pointer |
donatien | 2:a4f97773c90f | 941 | * or invalid hostname or invalid hostname length */ |
donatien | 2:a4f97773c90f | 942 | if ((dns_pcb == NULL) || (addr == NULL) || |
donatien | 2:a4f97773c90f | 943 | (!hostname) || (!hostname[0]) || |
donatien | 2:a4f97773c90f | 944 | (strlen(hostname) >= DNS_MAX_NAME_LENGTH)) { |
donatien | 2:a4f97773c90f | 945 | return ERR_VAL; |
donatien | 2:a4f97773c90f | 946 | } |
donatien | 2:a4f97773c90f | 947 | |
donatien | 2:a4f97773c90f | 948 | #if LWIP_HAVE_LOOPIF |
donatien | 2:a4f97773c90f | 949 | if (strcmp(hostname, "localhost")==0) { |
donatien | 2:a4f97773c90f | 950 | ip_addr_set_loopback(addr); |
donatien | 2:a4f97773c90f | 951 | return ERR_OK; |
donatien | 2:a4f97773c90f | 952 | } |
donatien | 2:a4f97773c90f | 953 | #endif /* LWIP_HAVE_LOOPIF */ |
donatien | 2:a4f97773c90f | 954 | |
donatien | 2:a4f97773c90f | 955 | /* host name already in octet notation? set ip addr and return ERR_OK */ |
donatien | 2:a4f97773c90f | 956 | ipaddr = ipaddr_addr(hostname); |
donatien | 2:a4f97773c90f | 957 | if (ipaddr == IPADDR_NONE) { |
donatien | 2:a4f97773c90f | 958 | /* already have this address cached? */ |
donatien | 2:a4f97773c90f | 959 | ipaddr = dns_lookup(hostname); |
donatien | 2:a4f97773c90f | 960 | } |
donatien | 2:a4f97773c90f | 961 | if (ipaddr != IPADDR_NONE) { |
donatien | 2:a4f97773c90f | 962 | ip4_addr_set_u32(addr, ipaddr); |
donatien | 2:a4f97773c90f | 963 | return ERR_OK; |
donatien | 2:a4f97773c90f | 964 | } |
donatien | 2:a4f97773c90f | 965 | |
donatien | 2:a4f97773c90f | 966 | /* queue query with specified callback */ |
donatien | 2:a4f97773c90f | 967 | return dns_enqueue(hostname, found, callback_arg); |
donatien | 2:a4f97773c90f | 968 | } |
donatien | 2:a4f97773c90f | 969 | |
donatien | 2:a4f97773c90f | 970 | #endif /* LWIP_DNS */ |