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