dns_client.c
Go to the documentation of this file.
1 /**
2  * @file dns_client.c
3  * @brief DNS client (Domain Name System)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL DNS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "dns/dns_cache.h"
37 #include "dns/dns_client.h"
38 #include "dns/dns_common.h"
39 #include "dns/dns_debug.h"
40 #include "debug.h"
41 
42 //Check TCP/IP stack configuration
43 #if (DNS_CLIENT_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Resolve a host name using DNS
48  * @param[in] interface Underlying network interface
49  * @param[in] name Name of the host to be resolved
50  * @param[in] type Host type (IPv4 or IPv6)
51  * @param[out] ipAddr IP address corresponding to the specified host name
52  **/
53 
56 {
57  error_t error;
58  DnsCacheEntry *entry;
59 
60 #if (NET_RTOS_SUPPORT == ENABLED)
61  systime_t delay;
62 
63  //Debug message
64  TRACE_INFO("Resolving host name %s (DNS resolver)...\r\n", name);
65 #endif
66 
67  //Get exclusive access
68  netLock(interface->netContext);
69 
70  //Search the DNS cache for the specified host name
71  entry = dnsFindEntry(interface, name, type, HOST_NAME_RESOLVER_DNS);
72 
73  //Check whether a matching entry has been found
74  if(entry != NULL)
75  {
76  //Host name already resolved?
77  if(entry->state == DNS_STATE_RESOLVED ||
78  entry->state == DNS_STATE_PERMANENT)
79  {
80  //Return the corresponding IP address
81  *ipAddr = entry->ipAddr;
82  //Successful host name resolution
83  error = NO_ERROR;
84  }
85  else if(entry->state == DNS_STATE_FAILED)
86  {
87  //The entry should be deleted since name resolution has failed
88  if(entry->refCount == 0)
89  {
90  //The entry should be deleted since name resolution has failed
91  dnsDeleteEntry(entry);
92  }
93 
94  //Report an error
95  error = ERROR_FAILURE;
96  }
97  else
98  {
99 #if (NET_RTOS_SUPPORT == ENABLED)
100  //Increment the reference count
101  entry->refCount++;
102 #endif
103  //Host name resolution is in progress
104  error = ERROR_IN_PROGRESS;
105  }
106  }
107  else
108  {
109  //If no entry exists, then create a new one
110  entry = dnsCreateEntry();
111 
112  //Record the host name whose IP address is unknown
113  osStrcpy(entry->name, name);
114 
115  //Initialize DNS cache entry
116  entry->type = type;
118  entry->interface = interface;
119 
120  //Select primary DNS server
121  entry->dnsServerIndex = 0;
122 
123  //Get an ephemeral port number
124  entry->port = udpGetDynamicPort(interface->netContext);
125 
126  //An identifier is used by the DNS client to match replies with
127  //corresponding requests
128  entry->id = (uint16_t) netGenerateRand(interface->netContext);
129 
130  //Callback function to be called when a DNS response is received
131  error = udpRegisterRxCallback(interface, entry->port, dnsProcessResponse,
132  NULL);
133 
134  //Check status code
135  if(!error)
136  {
137  //Initialize retransmission counter
139  //Send DNS query
140  error = dnsSendQuery(entry);
141 
142  //DNS message successfully sent?
143  if(!error)
144  {
145  //Save the time at which the query message was sent
146  entry->timestamp = osGetSystemTime();
147  //Set timeout value
150  //Decrement retransmission counter
151  entry->retransmitCount--;
152 
153  //Switch state
154  entry->state = DNS_STATE_IN_PROGRESS;
155 
156 #if (NET_RTOS_SUPPORT == ENABLED)
157  //Initialize the reference count
158  entry->refCount = 1;
159 #endif
160  //Host name resolution is in progress
161  error = ERROR_IN_PROGRESS;
162  }
163  else
164  {
165  //Unregister UDP callback function
166  udpUnregisterRxCallback(interface, entry->port);
167  }
168  }
169  }
170 
171  //Release exclusive access
172  netUnlock(interface->netContext);
173 
174 #if (NET_RTOS_SUPPORT == ENABLED)
175  //Set default polling interval
177 
178  //Wait the host name resolution to complete
179  while(error == ERROR_IN_PROGRESS)
180  {
181  //Wait until the next polling period
182  osDelayTask(delay);
183 
184  //Get exclusive access
185  netLock(interface->netContext);
186 
187  //Search the DNS cache for the specified host name
188  entry = dnsFindEntry(interface, name, type, HOST_NAME_RESOLVER_DNS);
189 
190  //Check whether a matching entry has been found
191  if(entry != NULL)
192  {
193  //Host name successfully resolved?
194  if(entry->state == DNS_STATE_RESOLVED)
195  {
196  //Return the corresponding IP address
197  *ipAddr = entry->ipAddr;
198  //Successful host name resolution
199  error = NO_ERROR;
200  }
201  else if(entry->state == DNS_STATE_FAILED)
202  {
203  //Decrement the reference count
204  if(entry->refCount > 0)
205  {
206  entry->refCount--;
207  }
208 
209  //The entry should be deleted since name resolution has failed
210  if(entry->refCount == 0)
211  {
212  dnsDeleteEntry(entry);
213  }
214 
215  //Report an error
216  error = ERROR_FAILURE;
217  }
218  else
219  {
220  //Host name resolution is in progress
221  }
222  }
223  else
224  {
225  //Host name resolution failed
226  error = ERROR_FAILURE;
227  }
228 
229  //Release exclusive access
230  netUnlock(interface->netContext);
231 
232  //Backoff support for less aggressive polling
233  delay = MIN(delay * 2, DNS_CACHE_MAX_POLLING_INTERVAL);
234  }
235 
236  //Check status code
237  if(error)
238  {
239  //Failed to resolve host name
240  TRACE_INFO("Host name resolution failed!\r\n");
241  }
242  else
243  {
244  //Successful host name resolution
245  TRACE_INFO("Host name resolved to %s...\r\n", ipAddrToString(ipAddr, NULL));
246  }
247 #endif
248 
249  //Return status code
250  return error;
251 }
252 
253 
254 /**
255  * @brief Send a DNS query message
256  * @param[in] entry Pointer to a valid DNS cache entry
257  * @return Error code
258  **/
259 
261 {
262  error_t error;
263  size_t length;
264  size_t offset;
265  NetBuffer *buffer;
267  DnsQuestion *dnsQuestion;
269  NetTxAncillary ancillary;
270 
271 #if (IPV4_SUPPORT == ENABLED)
272  //An IPv4 address is expected?
273  if(entry->type == HOST_TYPE_IPV4)
274  {
275  //Point to the IPv4 context
276  Ipv4Context *ipv4Context = &entry->interface->ipv4Context;
277 
278  //Select the relevant DNS server
279  while(1)
280  {
281  //Out of range index?
283  return ERROR_NO_DNS_SERVER;
284 
285  //Copy the address of the DNS server
286  destIpAddr.length = sizeof(Ipv4Addr);
287  destIpAddr.ipv4Addr = ipv4Context->dnsServerList[entry->dnsServerIndex];
288 
289  //Make sure the IP address is valid
290  if(destIpAddr.ipv4Addr != IPV4_UNSPECIFIED_ADDR)
291  break;
292 
293  //Select the next DNS server in the list
294  entry->dnsServerIndex++;
295  }
296  }
297  else
298 #endif
299 #if (IPV6_SUPPORT == ENABLED)
300  //An IPv6 address is expected?
301  if(entry->type == HOST_TYPE_IPV6)
302  {
303  //Point to the IPv6 context
304  Ipv6Context *ipv6Context = &entry->interface->ipv6Context;
305 
306  //Select the relevant DNS server
307  while(1)
308  {
309  //Out of range index?
311  return ERROR_NO_DNS_SERVER;
312 
313  //Copy the address of the DNS server
314  destIpAddr.length = sizeof(Ipv6Addr);
315  destIpAddr.ipv6Addr = ipv6Context->dnsServerList[entry->dnsServerIndex];
316 
317  //Make sure the IP address is valid
319  break;
320 
321  //Select the next DNS server in the list
322  entry->dnsServerIndex++;
323  }
324  }
325  else
326 #endif
327  //Invalid host type?
328  {
329  //Report an error
331  }
332 
333  //Allocate a memory buffer to hold the DNS query message
334  buffer = udpAllocBuffer(DNS_MESSAGE_MAX_SIZE, &offset);
335  //Failed to allocate buffer?
336  if(buffer == NULL)
337  return ERROR_OUT_OF_MEMORY;
338 
339  //Point to the DNS header
340  message = netBufferAt(buffer, offset, 0);
341 
342  //Format DNS query message
343  message->id = htons(entry->id);
344  message->qr = 0;
345  message->opcode = DNS_OPCODE_QUERY;
346  message->aa = 0;
347  message->tc = 0;
348  message->rd = 1;
349  message->ra = 0;
350  message->z = 0;
351  message->rcode = DNS_RCODE_NOERROR;
352 
353  //The DNS query contains one question
354  message->qdcount = HTONS(1);
355  message->ancount = 0;
356  message->nscount = 0;
357  message->arcount = 0;
358 
359  //Length of the DNS query message
360  length = sizeof(DnsHeader);
361 
362  //Encode the host name using the DNS name notation
363  length += dnsEncodeName(entry->name, message->questions);
364 
365  //Point to the corresponding question structure
366  dnsQuestion = DNS_GET_QUESTION(message, length);
367 
368 #if (IPV4_SUPPORT == ENABLED)
369  //An IPv4 address is expected?
370  if(entry->type == HOST_TYPE_IPV4)
371  {
372  //Fill in question structure
373  dnsQuestion->qtype = HTONS(DNS_RR_TYPE_A);
374  dnsQuestion->qclass = HTONS(DNS_RR_CLASS_IN);
375  }
376 #endif
377 #if (IPV6_SUPPORT == ENABLED)
378  //An IPv6 address is expected?
379  if(entry->type == HOST_TYPE_IPV6)
380  {
381  //Fill in question structure
382  dnsQuestion->qtype = HTONS(DNS_RR_TYPE_AAAA);
383  dnsQuestion->qclass = HTONS(DNS_RR_CLASS_IN);
384  }
385 #endif
386 
387  //Update the length of the DNS query message
388  length += sizeof(DnsQuestion);
389 
390  //Adjust the length of the multi-part buffer
391  netBufferSetLength(buffer, offset + length);
392 
393  //Debug message
394  TRACE_INFO("Sending DNS message (%" PRIuSIZE " bytes)...\r\n", length);
395  //Dump message
397 
398  //Additional options can be passed to the stack along with the packet
399  ancillary = NET_DEFAULT_TX_ANCILLARY;
400 
401  //Send DNS query message
402  error = udpSendBuffer(entry->interface->netContext, entry->interface, NULL,
403  entry->port, &destIpAddr, DNS_PORT, buffer, offset, &ancillary);
404 
405  //Free previously allocated memory
406  netBufferFree(buffer);
407 
408  //Return status code
409  return error;
410 }
411 
412 
413 /**
414  * @brief Process incoming DNS response message
415  * @param[in] interface Underlying network interface
416  * @param[in] pseudoHeader UDP pseudo header
417  * @param[in] udpHeader UDP header
418  * @param[in] buffer Multi-part buffer containing the incoming DNS message
419  * @param[in] offset Offset to the first byte of the DNS message
420  * @param[in] ancillary Additional options passed to the stack along with
421  * the packet
422  * @param[in] param Callback function parameter (not used)
423  **/
424 
426  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
427  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
428  void *param)
429 {
430  uint_t i;
431  uint_t j;
432  size_t pos;
433  size_t length;
435  DnsQuestion *question;
436  DnsResourceRecord *record;
437  DnsCacheEntry *entry;
438 
439  //Retrieve the length of the DNS message
440  length = netBufferGetLength(buffer) - offset;
441 
442  //Malformed DNS message?
443  if(length < sizeof(DnsHeader))
444  return;
445 
446  //Point to the DNS message header
447  message = netBufferAt(buffer, offset, length);
448  //Sanity check
449  if(message == NULL)
450  return;
451 
452  //Debug message
453  TRACE_INFO("DNS message received (%" PRIuSIZE " bytes)...\r\n", length);
454  //Dump message
456 
457  //Check message type
458  if(!message->qr)
459  return;
460 
461  //The DNS message shall contain one question
462  if(ntohs(message->qdcount) != 1)
463  return;
464 
465  //Loop through DNS cache entries
466  for(i = 0; i < DNS_CACHE_SIZE; i++)
467  {
468  //Point to the current entry
469  entry = &dnsCache[i];
470 
471  //DNS name resolution in progress?
472  if(entry->state == DNS_STATE_IN_PROGRESS &&
474  {
475  //Check destination port number
476  if(entry->port == ntohs(udpHeader->destPort))
477  {
478  //Compare identifier against the expected one
479  if(ntohs(message->id) != entry->id)
480  break;
481 
482  //Point to the first question
483  pos = sizeof(DnsHeader);
484  //Parse domain name
485  pos = dnsParseName(message, length, pos, NULL, 0);
486 
487  //Invalid name?
488  if(!pos)
489  break;
490  //Malformed DNS message?
491  if((pos + sizeof(DnsQuestion)) > length)
492  break;
493 
494  //Compare domain name
495  if(dnsCompareName(message, length, sizeof(DnsHeader), entry->name, 0))
496  break;
497 
498  //Point to the corresponding entry
499  question = DNS_GET_QUESTION(message, pos);
500 
501  //Check the class of the query
502  if(ntohs(question->qclass) != DNS_RR_CLASS_IN)
503  break;
504 
505  //Check the type of the query
506  if(entry->type == HOST_TYPE_IPV4 &&
507  ntohs(question->qtype) != DNS_RR_TYPE_A)
508  {
509  break;
510  }
511 
512  if(entry->type == HOST_TYPE_IPV6 &&
513  ntohs(question->qtype) != DNS_RR_TYPE_AAAA)
514  {
515  break;
516  }
517 
518  //Check response code
519  if(message->rcode != DNS_RCODE_NOERROR)
520  {
521  //Select the next DNS server
522  dnsSelectNextServer(entry);
523  //Exit immediately
524  break;
525  }
526 
527  //Point to the first answer
528  pos += sizeof(DnsQuestion);
529 
530  //Parse answer resource records
531  for(j = 0; j < ntohs(message->ancount); j++)
532  {
533  //Parse domain name
534  pos = dnsParseName(message, length, pos, NULL, 0);
535  //Invalid name?
536  if(!pos)
537  break;
538 
539  //Point to the associated resource record
540  record = DNS_GET_RESOURCE_RECORD(message, pos);
541  //Point to the resource data
542  pos += sizeof(DnsResourceRecord);
543 
544  //Make sure the resource record is valid
545  if(pos > length)
546  break;
547  if((pos + ntohs(record->rdlength)) > length)
548  break;
549 
550 #if (IPV4_SUPPORT == ENABLED)
551  //IPv4 address expected?
552  if(entry->type == HOST_TYPE_IPV4)
553  {
554  //A resource record found?
555  if(ntohs(record->rtype) == DNS_RR_TYPE_A &&
556  ntohs(record->rdlength) == sizeof(Ipv4Addr))
557  {
558  //Copy the IPv4 address
559  entry->ipAddr.length = sizeof(Ipv4Addr);
560  ipv4CopyAddr(&entry->ipAddr.ipv4Addr, record->rdata);
561 
562  //Save current time
563  entry->timestamp = osGetSystemTime();
564  //Save TTL value
565  entry->timeout = ntohl(record->ttl) * 1000;
566 
567  //Limit the lifetime of the DNS cache entries
568  entry->timeout = MIN(entry->timeout, DNS_MAX_LIFETIME);
569  entry->timeout = MAX(entry->timeout, DNS_MIN_LIFETIME);
570 
571  //Unregister UDP callback function
572  udpUnregisterRxCallback(interface, entry->port);
573  //Host name successfully resolved
574  entry->state = DNS_STATE_RESOLVED;
575 
576  //Exit immediately
577  break;
578  }
579  }
580 #endif
581 #if (IPV6_SUPPORT == ENABLED)
582  //IPv6 address expected?
583  if(entry->type == HOST_TYPE_IPV6)
584  {
585  //AAAA resource record found?
586  if(ntohs(record->rtype) == DNS_RR_TYPE_AAAA &&
587  ntohs(record->rdlength) == sizeof(Ipv6Addr))
588  {
589  //Copy the IPv6 address
590  entry->ipAddr.length = sizeof(Ipv6Addr);
591  ipv6CopyAddr(&entry->ipAddr.ipv6Addr, record->rdata);
592 
593  //Save current time
594  entry->timestamp = osGetSystemTime();
595  //Save TTL value
596  entry->timeout = ntohl(record->ttl) * 1000;
597 
598  //Limit the lifetime of the DNS cache entries
599  entry->timeout = MIN(entry->timeout, DNS_MAX_LIFETIME);
600  entry->timeout = MAX(entry->timeout, DNS_MIN_LIFETIME);
601 
602  //Unregister UDP callback function
603  udpUnregisterRxCallback(interface, entry->port);
604  //Host name successfully resolved
605  entry->state = DNS_STATE_RESOLVED;
606 
607  //Exit immediately
608  break;
609  }
610  }
611 #endif
612  //Point to the next resource record
613  pos += ntohs(record->rdlength);
614  }
615 
616  //We are done
617  break;
618  }
619  }
620  }
621 }
622 
623 
624 /**
625  * @brief Select the next DNS server
626  * @param[in] entry Pointer to a valid DNS cache entry
627  **/
628 
630 {
631  error_t error;
632  NetInterface *interface;
633 
634  //Point to the underlying network interface
635  interface = entry->interface;
636 
637 #if defined(DNS_SELECT_NEXT_SERVER_HOOK)
638  DNS_SELECT_NEXT_SERVER_HOOK(entry);
639 #endif
640 
641  //Select the next DNS server
642  entry->dnsServerIndex++;
643 
644  //An identifier is used by the DNS client to match replies with
645  //corresponding requests
646  entry->id = (uint16_t) netGenerateRand(interface->netContext);
647 
648  //Initialize retransmission counter
650  //Send DNS query
651  error = dnsSendQuery(entry);
652 
653  //DNS message successfully sent?
654  if(!error)
655  {
656  //Save the time at which the query message was sent
657  entry->timestamp = osGetSystemTime();
658  //Set timeout value
660  //Decrement retransmission counter
661  entry->retransmitCount--;
662  }
663  else
664  {
665  //Unregister UDP callback function
666  udpUnregisterRxCallback(interface, entry->port);
667  //Host name resolution failed
668  entry->state = DNS_STATE_FAILED;
669  }
670 }
671 
672 #endif
error_t dnsSendQuery(DnsCacheEntry *entry)
Send a DNS query message.
Definition: dns_client.c:260
#define htons(value)
Definition: cpu_endian.h:413
HostType
Host types.
Definition: socket.h:215
@ DNS_STATE_PERMANENT
Definition: dns_cache.h:89
#define DNS_MAX_LIFETIME
Definition: dns_client.h:77
HostType type
IPv4 or IPv6 host?
Definition: dns_cache.h:101
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:319
error_t udpUnregisterRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1062
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:70
@ HOST_NAME_RESOLVER_DNS
Definition: socket.h:229
Ipv4Addr dnsServerList[IPV4_DNS_SERVER_LIST_SIZE]
DNS servers.
Definition: ipv4.h:460
IP network address.
Definition: ip.h:90
@ DNS_OPCODE_QUERY
Query.
Definition: dns_common.h:81
#define DNS_MIN_LIFETIME
Definition: dns_client.h:70
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
uint32_t netGenerateRand(NetContext *context)
Generate a random 32-bit value.
Definition: net_misc.c:956
systime_t timeout
Retransmission timeout.
Definition: dns_cache.h:110
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:810
size_t dnsParseName(const DnsHeader *message, size_t length, size_t pos, char_t *dest, uint_t level)
Decode a domain name that uses the DNS name encoding.
Definition: dns_common.c:132
Ipv6Addr
Definition: ipv6.h:280
uint8_t type
Definition: coap_common.h:176
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
HostnameResolver protocol
Name resolution protocol.
Definition: dns_cache.h:102
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:134
@ DNS_RR_CLASS_IN
Internet.
Definition: dns_common.h:124
#define DNS_CLIENT_MAX_RETRIES
Definition: dns_client.h:49
char_t name[]
DnsHeader
Definition: dns_common.h:202
uint_t refCount
Reference count for the current entry.
Definition: dns_cache.h:100
IpAddr ipAddr
IP address.
Definition: dns_cache.h:108
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:323
IPv6 context.
Definition: ipv6.h:516
@ ERROR_NO_DNS_SERVER
Definition: error.h:255
IPv4 context.
Definition: ipv4.h:452
@ HOST_TYPE_IPV6
Definition: socket.h:218
IP pseudo header.
Definition: ip.h:110
uint16_t id
Identifier used to match queries and responses.
Definition: dns_cache.h:106
@ DNS_RCODE_NOERROR
No error.
Definition: dns_common.h:95
@ ERROR_IN_PROGRESS
Definition: error.h:214
uint16_t port
Port number used by the resolver.
Definition: dns_cache.h:105
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t dnsResolve(NetInterface *interface, const char_t *name, HostType type, IpAddr *ipAddr)
Resolve a host name using DNS.
Definition: dns_client.c:54
error_t
Error codes.
Definition: error.h:43
#define DNS_GET_QUESTION(message, offset)
Definition: dns_common.h:63
char_t name[DNS_MAX_NAME_LEN+1]
Domain name.
Definition: dns_cache.h:107
systime_t timestamp
Timestamp to manage entry lifetime.
Definition: dns_cache.h:109
#define DNS_GET_RESOURCE_RECORD(message, offset)
Definition: dns_common.h:64
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ DNS_STATE_FAILED
Definition: dns_cache.h:88
Ipv6Addr dnsServerList[IPV6_DNS_SERVER_LIST_SIZE]
DNS servers.
Definition: ipv6.h:527
#define IPV4_DNS_SERVER_LIST_SIZE
Definition: ipv4.h:87
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
void netBufferFree(NetBuffer *buffer)
Dispose a multi-part buffer.
Definition: net_mem.c:282
DNS cache entry.
Definition: dns_cache.h:98
@ DNS_RR_TYPE_A
Host address.
Definition: dns_common.h:137
DnsCacheEntry * dnsCreateEntry(void)
Create a new entry in the DNS cache.
Definition: dns_cache.c:101
#define NetTxAncillary
Definition: net_misc.h:36
systime_t maxTimeout
Maximum retransmission timeout.
Definition: dns_cache.h:111
error_t udpSendBuffer(NetContext *context, NetInterface *interface, const IpAddr *srcIpAddr, uint16_t srcPort, const IpAddr *destIpAddr, uint16_t destPort, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a UDP datagram.
Definition: udp.c:657
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:65
#define IPV6_DNS_SERVER_LIST_SIZE
Definition: ipv6.h:100
#define TRACE_INFO(...)
Definition: debug.h:105
#define DNS_PORT
Definition: dns_common.h:57
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
void dnsDumpMessage(const DnsHeader *message, size_t length)
Dump DNS message for debugging purpose.
Definition: dns_debug.c:52
#define MIN(a, b)
Definition: os_port.h:63
NetBuffer * udpAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold a UDP packet.
Definition: udp.c:948
size_t length
Definition: ip.h:91
@ HOST_TYPE_IPV4
Definition: socket.h:217
size_t dnsEncodeName(const char_t *src, uint8_t *dest)
Encode a domain name using the DNS name notation.
Definition: dns_common.c:58
DNS client (Domain Name System)
UdpHeader
Definition: udp.h:85
#define DNS_CACHE_INIT_POLLING_INTERVAL
Definition: dns_cache.h:61
uint16_t udpGetDynamicPort(NetContext *context)
Get an ephemeral port number.
Definition: udp.c:80
uint32_t systime_t
System time.
#define ntohs(value)
Definition: cpu_endian.h:421
#define MAX(a, b)
Definition: os_port.h:67
char char_t
Definition: compiler_port.h:55
DnsCacheEntry * dnsFindEntry(NetInterface *interface, const char_t *name, HostType type, HostnameResolver protocol)
Search the DNS cache for a given domain name.
Definition: dns_cache.c:183
Ipv4Addr ipv4Addr
Definition: ip.h:95
DNS cache management.
#define ipv6CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv6.h:130
#define HTONS(value)
Definition: cpu_endian.h:410
@ DNS_STATE_IN_PROGRESS
Definition: dns_cache.h:86
NetInterface * interface
Underlying network interface.
Definition: dns_cache.h:103
#define DNS_CACHE_SIZE
Definition: dns_cache.h:47
@ DNS_STATE_RESOLVED
Definition: dns_cache.h:87
#define DNS_MESSAGE_MAX_SIZE
Definition: dns_common.h:45
error_t netBufferSetLength(NetBuffer *buffer, size_t length)
Adjust the length of a multi-part buffer.
Definition: net_mem.c:322
DnsQuestion
Definition: dns_common.h:213
#define DNS_CLIENT_INIT_TIMEOUT
Definition: dns_client.h:56
#define DNS_CLIENT_MAX_TIMEOUT
Definition: dns_client.h:63
void dnsDeleteEntry(DnsCacheEntry *entry)
Delete the specified DNS cache entry.
Definition: dns_cache.c:151
#define ipv4CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv4.h:166
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
void osDelayTask(systime_t delay)
Delay routine.
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:418
Ipv4Addr ipAddr
Definition: ipcp.h:105
DnsCacheEntry dnsCache[DNS_CACHE_SIZE]
Definition: dns_cache.c:49
DnsState state
Entry state.
Definition: dns_cache.h:99
void dnsSelectNextServer(DnsCacheEntry *entry)
Select the next DNS server.
Definition: dns_client.c:629
error_t udpRegisterRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1021
uint_t dnsServerIndex
This parameter selects between the primary and secondary DNS server.
Definition: dns_cache.h:104
Ipv6Addr ipv6Addr
Definition: ip.h:98
uint_t retransmitCount
Retransmission counter.
Definition: dns_cache.h:112
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
DnsResourceRecord
Definition: dns_common.h:227
@ DNS_RR_TYPE_AAAA
IPv6 address.
Definition: dns_common.h:147
Data logging functions for debugging purpose (DNS)
Common DNS routines.
int_t dnsCompareName(const DnsHeader *message, size_t length, size_t pos, const char_t *name, uint_t level)
Compare domain names.
Definition: dns_common.c:242
#define osStrcpy(s1, s2)
Definition: os_port.h:210
#define DNS_CACHE_MAX_POLLING_INTERVAL
Definition: dns_cache.h:68
void dnsProcessResponse(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary, void *param)
Process incoming DNS response message.
Definition: dns_client.c:425
#define ntohl(value)
Definition: cpu_endian.h:422
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:128
systime_t osGetSystemTime(void)
Retrieve system time.
Ipv4Addr destIpAddr
Definition: ipcp.h:80