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