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