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