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.6
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(interface->netContext);
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;
434  NetContext *context;
436  DnsQuestion *question;
437  DnsResourceRecord *record;
438  DnsCacheEntry *entry;
439 
440  //Point to the TCP/IP stack context
441  context = interface->netContext;
442 
443  //Retrieve the length of the DNS message
444  length = netBufferGetLength(buffer) - offset;
445 
446  //Malformed DNS message?
447  if(length < sizeof(DnsHeader))
448  return;
449 
450  //Point to the DNS message header
451  message = netBufferAt(buffer, offset, length);
452  //Malformed DNS message?
453  if(message == NULL)
454  return;
455 
456  //Debug message
457  TRACE_INFO("DNS message received (%" PRIuSIZE " bytes)...\r\n", length);
458  //Dump message
460 
461  //Check message type
462  if(!message->qr)
463  return;
464 
465  //The DNS message shall contain one question
466  if(ntohs(message->qdcount) != 1)
467  return;
468 
469  //Loop through DNS cache entries
470  for(i = 0; i < DNS_CACHE_SIZE; i++)
471  {
472  //Point to the current entry
473  entry = &context->dnsCache[i];
474 
475  //DNS name resolution in progress?
476  if(entry->state == DNS_STATE_IN_PROGRESS &&
478  {
479  //Check destination port number
480  if(entry->port == ntohs(udpHeader->destPort))
481  {
482  //Compare identifier against the expected one
483  if(ntohs(message->id) != entry->id)
484  break;
485 
486  //Point to the first question
487  pos = sizeof(DnsHeader);
488  //Parse domain name
489  pos = dnsParseName(message, length, pos, 0);
490 
491  //Invalid name?
492  if(pos == 0)
493  break;
494  //Malformed DNS message?
495  if((pos + sizeof(DnsQuestion)) > length)
496  break;
497 
498  //Compare domain name
499  if(dnsCompareName(message, length, sizeof(DnsHeader), entry->name, 0))
500  break;
501 
502  //Point to the corresponding entry
503  question = DNS_GET_QUESTION(message, pos);
504 
505  //Check the class of the query
506  if(ntohs(question->qclass) != DNS_RR_CLASS_IN)
507  break;
508 
509  //Check the type of the query
510  if(entry->type == HOST_TYPE_IPV4 &&
511  ntohs(question->qtype) != DNS_RR_TYPE_A)
512  {
513  break;
514  }
515 
516  if(entry->type == HOST_TYPE_IPV6 &&
517  ntohs(question->qtype) != DNS_RR_TYPE_AAAA)
518  {
519  break;
520  }
521 
522  //Check response code
523  if(message->rcode != DNS_RCODE_NOERROR)
524  {
525  //Select the next DNS server
526  dnsSelectNextServer(entry);
527  //Exit immediately
528  break;
529  }
530 
531  //Point to the first answer
532  pos += sizeof(DnsQuestion);
533 
534  //Parse answer resource records
535  for(j = 0; j < ntohs(message->ancount); j++)
536  {
537  //Parse domain name
538  pos = dnsParseName(message, length, pos, 0);
539  //Invalid name?
540  if(pos == 0)
541  break;
542 
543  //Point to the associated resource record
544  record = DNS_GET_RESOURCE_RECORD(message, pos);
545  //Point to the resource data
546  pos += sizeof(DnsResourceRecord);
547 
548  //Make sure the resource record is valid
549  if(pos > length)
550  break;
551  if((pos + ntohs(record->rdlength)) > length)
552  break;
553 
554 #if (IPV4_SUPPORT == ENABLED)
555  //IPv4 address expected?
556  if(entry->type == HOST_TYPE_IPV4)
557  {
558  //A resource record found?
559  if(ntohs(record->rtype) == DNS_RR_TYPE_A &&
560  ntohs(record->rdlength) == sizeof(Ipv4Addr))
561  {
562  //Copy the IPv4 address
563  entry->ipAddr.length = sizeof(Ipv4Addr);
564  ipv4CopyAddr(&entry->ipAddr.ipv4Addr, record->rdata);
565 
566  //Save current time
567  entry->timestamp = osGetSystemTime();
568  //Save TTL value
569  entry->timeout = ntohl(record->ttl) * 1000;
570 
571  //Limit the lifetime of the DNS cache entries
572  entry->timeout = MIN(entry->timeout, DNS_MAX_LIFETIME);
573  entry->timeout = MAX(entry->timeout, DNS_MIN_LIFETIME);
574 
575  //Unregister UDP callback function
576  udpUnregisterRxCallback(interface, entry->port);
577  //Host name successfully resolved
578  entry->state = DNS_STATE_RESOLVED;
579 
580  //Exit immediately
581  break;
582  }
583  }
584 #endif
585 #if (IPV6_SUPPORT == ENABLED)
586  //IPv6 address expected?
587  if(entry->type == HOST_TYPE_IPV6)
588  {
589  //AAAA resource record found?
590  if(ntohs(record->rtype) == DNS_RR_TYPE_AAAA &&
591  ntohs(record->rdlength) == sizeof(Ipv6Addr))
592  {
593  //Copy the IPv6 address
594  entry->ipAddr.length = sizeof(Ipv6Addr);
595  ipv6CopyAddr(&entry->ipAddr.ipv6Addr, record->rdata);
596 
597  //Save current time
598  entry->timestamp = osGetSystemTime();
599  //Save TTL value
600  entry->timeout = ntohl(record->ttl) * 1000;
601 
602  //Limit the lifetime of the DNS cache entries
603  entry->timeout = MIN(entry->timeout, DNS_MAX_LIFETIME);
604  entry->timeout = MAX(entry->timeout, DNS_MIN_LIFETIME);
605 
606  //Unregister UDP callback function
607  udpUnregisterRxCallback(interface, entry->port);
608  //Host name successfully resolved
609  entry->state = DNS_STATE_RESOLVED;
610 
611  //Exit immediately
612  break;
613  }
614  }
615 #endif
616  //Point to the next resource record
617  pos += ntohs(record->rdlength);
618  }
619 
620  //We are done
621  break;
622  }
623  }
624  }
625 }
626 
627 
628 /**
629  * @brief Select the next DNS server
630  * @param[in] entry Pointer to a valid DNS cache entry
631  **/
632 
634 {
635  error_t error;
636  NetInterface *interface;
637 
638  //Point to the underlying network interface
639  interface = entry->interface;
640 
641 #if defined(DNS_SELECT_NEXT_SERVER_HOOK)
642  DNS_SELECT_NEXT_SERVER_HOOK(entry);
643 #endif
644 
645  //Select the next DNS server
646  entry->dnsServerIndex++;
647 
648  //An identifier is used by the DNS client to match replies with
649  //corresponding requests
650  entry->id = (uint16_t) netGenerateRand(interface->netContext);
651 
652  //Initialize retransmission counter
654  //Send DNS query
655  error = dnsSendQuery(entry);
656 
657  //DNS message successfully sent?
658  if(!error)
659  {
660  //Save the time at which the query message was sent
661  entry->timestamp = osGetSystemTime();
662  //Set timeout value
664  //Decrement retransmission counter
665  entry->retransmitCount--;
666  }
667  else
668  {
669  //Unregister UDP callback function
670  udpUnregisterRxCallback(interface, entry->port);
671  //Host name resolution failed
672  entry->state = DNS_STATE_FAILED;
673  }
674 }
675 
676 #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
#define NetContext
Definition: net.h:36
error_t udpUnregisterRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1080
size_t dnsParseName(const DnsHeader *message, size_t length, size_t pos, uint_t level)
Decode a domain name that uses the DNS name encoding.
Definition: dns_common.c:131
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:461
IP network address.
Definition: ip.h:94
@ DNS_OPCODE_QUERY
Query.
Definition: dns_common.h:83
#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:962
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
Ipv6Addr
Definition: ipv6.h:282
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:136
@ DNS_RR_CLASS_IN
Internet.
Definition: dns_common.h:126
#define DNS_CLIENT_MAX_RETRIES
Definition: dns_client.h:49
char_t name[]
DnsHeader
Definition: dns_common.h:204
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:324
IPv6 context.
Definition: ipv6.h:518
@ ERROR_NO_DNS_SERVER
Definition: error.h:255
IPv4 context.
Definition: ipv4.h:453
@ HOST_TYPE_IPV6
Definition: socket.h:218
uint16_t id
Identifier used to match queries and responses.
Definition: dns_cache.h:106
@ DNS_RCODE_NOERROR
No error.
Definition: dns_common.h:97
@ 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:65
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:66
@ 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:529
#define IPV4_DNS_SERVER_LIST_SIZE
Definition: ipv4.h:88
#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:139
#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:665
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:65
#define IPV6_DNS_SERVER_LIST_SIZE
Definition: ipv6.h:102
#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:958
size_t length
Definition: ip.h:95
@ 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
DnsCacheEntry * dnsCreateEntry(NetContext *context)
Create a new entry in the DNS cache.
Definition: dns_cache.c:104
#define DNS_CACHE_INIT_POLLING_INTERVAL
Definition: dns_cache.h:61
uint16_t udpGetDynamicPort(NetContext *context)
Get an ephemeral port number.
Definition: udp.c:78
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:186
Ipv4Addr ipv4Addr
Definition: ip.h:99
DNS cache management.
#define ipv6CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv6.h:132
#define HTONS(value)
Definition: cpu_endian.h:410
#define IpPseudoHeader
Definition: ip.h:36
@ 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:215
#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:154
#define ipv4CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv4.h:167
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
DnsState state
Entry state.
Definition: dns_cache.h:99
void dnsSelectNextServer(DnsCacheEntry *entry)
Select the next DNS server.
Definition: dns_client.c:633
error_t udpRegisterRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1031
uint_t dnsServerIndex
This parameter selects between the primary and secondary DNS server.
Definition: dns_cache.h:104
Ipv6Addr ipv6Addr
Definition: ip.h:102
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:229
@ DNS_RR_TYPE_AAAA
IPv6 address.
Definition: dns_common.h:149
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:213
#define osStrcpy(s1, s2)
Definition: os_port.h:213
#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:129
systime_t osGetSystemTime(void)
Retrieve system time.
Ipv4Addr destIpAddr
Definition: ipcp.h:80