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-2025 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.5.4
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
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  dnsDeleteEntry(entry);
89  //Report an error
90  error = ERROR_FAILURE;
91  }
92  else
93  {
94  //Host name resolution is in progress
95  error = ERROR_IN_PROGRESS;
96  }
97  }
98  else
99  {
100  //If no entry exists, then create a new one
101  entry = dnsCreateEntry();
102 
103  //Record the host name whose IP address is unknown
104  osStrcpy(entry->name, name);
105 
106  //Initialize DNS cache entry
107  entry->type = type;
109  entry->interface = interface;
110 
111  //Select primary DNS server
112  entry->dnsServerIndex = 0;
113 
114  //Get an ephemeral port number
115  entry->port = udpGetDynamicPort();
116 
117  //An identifier is used by the DNS client to match replies with
118  //corresponding requests
119  entry->id = (uint16_t) netGenerateRand();
120 
121  //Callback function to be called when a DNS response is received
122  error = udpAttachRxCallback(interface, entry->port, dnsProcessResponse,
123  NULL);
124 
125  //Check status code
126  if(!error)
127  {
128  //Initialize retransmission counter
130  //Send DNS query
131  error = dnsSendQuery(entry);
132 
133  //DNS message successfully sent?
134  if(!error)
135  {
136  //Save the time at which the query message was sent
137  entry->timestamp = osGetSystemTime();
138  //Set timeout value
141  //Decrement retransmission counter
142  entry->retransmitCount--;
143 
144  //Switch state
145  entry->state = DNS_STATE_IN_PROGRESS;
146  //Host name resolution is in progress
147  error = ERROR_IN_PROGRESS;
148  }
149  else
150  {
151  //Unregister UDP callback function
152  udpDetachRxCallback(interface, entry->port);
153  }
154  }
155  }
156 
157  //Release exclusive access
159 
160 #if (NET_RTOS_SUPPORT == ENABLED)
161  //Set default polling interval
163 
164  //Wait the host name resolution to complete
165  while(error == ERROR_IN_PROGRESS)
166  {
167  //Wait until the next polling period
168  osDelayTask(delay);
169 
170  //Get exclusive access
172 
173  //Search the DNS cache for the specified host name
174  entry = dnsFindEntry(interface, name, type, HOST_NAME_RESOLVER_DNS);
175 
176  //Check whether a matching entry has been found
177  if(entry != NULL)
178  {
179  //Host name successfully resolved?
180  if(entry->state == DNS_STATE_RESOLVED)
181  {
182  //Return the corresponding IP address
183  *ipAddr = entry->ipAddr;
184  //Successful host name resolution
185  error = NO_ERROR;
186  }
187  else if(entry->state == DNS_STATE_FAILED)
188  {
189  //The entry should be deleted since name resolution has failed
190  dnsDeleteEntry(entry);
191  //Report an error
192  error = ERROR_FAILURE;
193  }
194  else
195  {
196  //Host name resolution is in progress
197  }
198  }
199  else
200  {
201  //Host name resolution failed
202  error = ERROR_FAILURE;
203  }
204 
205  //Release exclusive access
207 
208  //Backoff support for less aggressive polling
209  delay = MIN(delay * 2, DNS_CACHE_MAX_POLLING_INTERVAL);
210  }
211 
212  //Check status code
213  if(error)
214  {
215  //Failed to resolve host name
216  TRACE_INFO("Host name resolution failed!\r\n");
217  }
218  else
219  {
220  //Successful host name resolution
221  TRACE_INFO("Host name resolved to %s...\r\n", ipAddrToString(ipAddr, NULL));
222  }
223 #endif
224 
225  //Return status code
226  return error;
227 }
228 
229 
230 /**
231  * @brief Send a DNS query message
232  * @param[in] entry Pointer to a valid DNS cache entry
233  * @return Error code
234  **/
235 
237 {
238  error_t error;
239  size_t length;
240  size_t offset;
241  NetBuffer *buffer;
243  DnsQuestion *dnsQuestion;
245  NetTxAncillary ancillary;
246 
247 #if (IPV4_SUPPORT == ENABLED)
248  //An IPv4 address is expected?
249  if(entry->type == HOST_TYPE_IPV4)
250  {
251  //Point to the IPv4 context
252  Ipv4Context *ipv4Context = &entry->interface->ipv4Context;
253 
254  //Select the relevant DNS server
255  while(1)
256  {
257  //Out of range index?
259  return ERROR_NO_DNS_SERVER;
260 
261  //Copy the address of the DNS server
262  destIpAddr.length = sizeof(Ipv4Addr);
263  destIpAddr.ipv4Addr = ipv4Context->dnsServerList[entry->dnsServerIndex];
264 
265  //Make sure the IP address is valid
266  if(destIpAddr.ipv4Addr != IPV4_UNSPECIFIED_ADDR)
267  break;
268 
269  //Select the next DNS server in the list
270  entry->dnsServerIndex++;
271  }
272  }
273  else
274 #endif
275 #if (IPV6_SUPPORT == ENABLED)
276  //An IPv6 address is expected?
277  if(entry->type == HOST_TYPE_IPV6)
278  {
279  //Point to the IPv6 context
280  Ipv6Context *ipv6Context = &entry->interface->ipv6Context;
281 
282  //Select the relevant DNS server
283  while(1)
284  {
285  //Out of range index?
287  return ERROR_NO_DNS_SERVER;
288 
289  //Copy the address of the DNS server
290  destIpAddr.length = sizeof(Ipv6Addr);
291  destIpAddr.ipv6Addr = ipv6Context->dnsServerList[entry->dnsServerIndex];
292 
293  //Make sure the IP address is valid
295  break;
296 
297  //Select the next DNS server in the list
298  entry->dnsServerIndex++;
299  }
300  }
301  else
302 #endif
303  //Invalid host type?
304  {
305  //Report an error
307  }
308 
309  //Allocate a memory buffer to hold the DNS query message
310  buffer = udpAllocBuffer(DNS_MESSAGE_MAX_SIZE, &offset);
311  //Failed to allocate buffer?
312  if(buffer == NULL)
313  return ERROR_OUT_OF_MEMORY;
314 
315  //Point to the DNS header
316  message = netBufferAt(buffer, offset, 0);
317 
318  //Format DNS query message
319  message->id = htons(entry->id);
320  message->qr = 0;
321  message->opcode = DNS_OPCODE_QUERY;
322  message->aa = 0;
323  message->tc = 0;
324  message->rd = 1;
325  message->ra = 0;
326  message->z = 0;
327  message->rcode = DNS_RCODE_NOERROR;
328 
329  //The DNS query contains one question
330  message->qdcount = HTONS(1);
331  message->ancount = 0;
332  message->nscount = 0;
333  message->arcount = 0;
334 
335  //Length of the DNS query message
336  length = sizeof(DnsHeader);
337 
338  //Encode the host name using the DNS name notation
339  length += dnsEncodeName(entry->name, message->questions);
340 
341  //Point to the corresponding question structure
342  dnsQuestion = DNS_GET_QUESTION(message, length);
343 
344 #if (IPV4_SUPPORT == ENABLED)
345  //An IPv4 address is expected?
346  if(entry->type == HOST_TYPE_IPV4)
347  {
348  //Fill in question structure
349  dnsQuestion->qtype = HTONS(DNS_RR_TYPE_A);
350  dnsQuestion->qclass = HTONS(DNS_RR_CLASS_IN);
351  }
352 #endif
353 #if (IPV6_SUPPORT == ENABLED)
354  //An IPv6 address is expected?
355  if(entry->type == HOST_TYPE_IPV6)
356  {
357  //Fill in question structure
358  dnsQuestion->qtype = HTONS(DNS_RR_TYPE_AAAA);
359  dnsQuestion->qclass = HTONS(DNS_RR_CLASS_IN);
360  }
361 #endif
362 
363  //Update the length of the DNS query message
364  length += sizeof(DnsQuestion);
365 
366  //Adjust the length of the multi-part buffer
367  netBufferSetLength(buffer, offset + length);
368 
369  //Debug message
370  TRACE_INFO("Sending DNS message (%" PRIuSIZE " bytes)...\r\n", length);
371  //Dump message
373 
374  //Additional options can be passed to the stack along with the packet
375  ancillary = NET_DEFAULT_TX_ANCILLARY;
376 
377  //Send DNS query message
378  error = udpSendBuffer(entry->interface, NULL, entry->port, &destIpAddr,
379  DNS_PORT, buffer, offset, &ancillary);
380 
381  //Free previously allocated memory
382  netBufferFree(buffer);
383 
384  //Return status code
385  return error;
386 }
387 
388 
389 /**
390  * @brief Process incoming DNS response message
391  * @param[in] interface Underlying network interface
392  * @param[in] pseudoHeader UDP pseudo header
393  * @param[in] udpHeader UDP header
394  * @param[in] buffer Multi-part buffer containing the incoming DNS message
395  * @param[in] offset Offset to the first byte of the DNS message
396  * @param[in] ancillary Additional options passed to the stack along with
397  * the packet
398  * @param[in] param Callback function parameter (not used)
399  **/
400 
402  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
403  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
404  void *param)
405 {
406  uint_t i;
407  uint_t j;
408  size_t pos;
409  size_t length;
411  DnsQuestion *question;
412  DnsResourceRecord *record;
413  DnsCacheEntry *entry;
414 
415  //Retrieve the length of the DNS message
416  length = netBufferGetLength(buffer) - offset;
417 
418  //Malformed DNS message?
419  if(length < sizeof(DnsHeader))
420  return;
421 
422  //Point to the DNS message header
423  message = netBufferAt(buffer, offset, length);
424  //Sanity check
425  if(message == NULL)
426  return;
427 
428  //Debug message
429  TRACE_INFO("DNS message received (%" PRIuSIZE " bytes)...\r\n", length);
430  //Dump message
432 
433  //Check message type
434  if(!message->qr)
435  return;
436 
437  //The DNS message shall contain one question
438  if(ntohs(message->qdcount) != 1)
439  return;
440 
441  //Loop through DNS cache entries
442  for(i = 0; i < DNS_CACHE_SIZE; i++)
443  {
444  //Point to the current entry
445  entry = &dnsCache[i];
446 
447  //DNS name resolution in progress?
448  if(entry->state == DNS_STATE_IN_PROGRESS &&
450  {
451  //Check destination port number
452  if(entry->port == ntohs(udpHeader->destPort))
453  {
454  //Compare identifier against the expected one
455  if(ntohs(message->id) != entry->id)
456  break;
457 
458  //Point to the first question
459  pos = sizeof(DnsHeader);
460  //Parse domain name
461  pos = dnsParseName(message, length, pos, NULL, 0);
462 
463  //Invalid name?
464  if(!pos)
465  break;
466  //Malformed DNS message?
467  if((pos + sizeof(DnsQuestion)) > length)
468  break;
469 
470  //Compare domain name
471  if(dnsCompareName(message, length, sizeof(DnsHeader), entry->name, 0))
472  break;
473 
474  //Point to the corresponding entry
475  question = DNS_GET_QUESTION(message, pos);
476 
477  //Check the class of the query
478  if(ntohs(question->qclass) != DNS_RR_CLASS_IN)
479  break;
480 
481  //Check the type of the query
482  if(entry->type == HOST_TYPE_IPV4 &&
483  ntohs(question->qtype) != DNS_RR_TYPE_A)
484  {
485  break;
486  }
487 
488  if(entry->type == HOST_TYPE_IPV6 &&
489  ntohs(question->qtype) != DNS_RR_TYPE_AAAA)
490  {
491  break;
492  }
493 
494  //Check response code
495  if(message->rcode != DNS_RCODE_NOERROR)
496  {
497  //Select the next DNS server
498  dnsSelectNextServer(entry);
499  //Exit immediately
500  break;
501  }
502 
503  //Point to the first answer
504  pos += sizeof(DnsQuestion);
505 
506  //Parse answer resource records
507  for(j = 0; j < ntohs(message->ancount); j++)
508  {
509  //Parse domain name
510  pos = dnsParseName(message, length, pos, NULL, 0);
511  //Invalid name?
512  if(!pos)
513  break;
514 
515  //Point to the associated resource record
516  record = DNS_GET_RESOURCE_RECORD(message, pos);
517  //Point to the resource data
518  pos += sizeof(DnsResourceRecord);
519 
520  //Make sure the resource record is valid
521  if(pos > length)
522  break;
523  if((pos + ntohs(record->rdlength)) > length)
524  break;
525 
526 #if (IPV4_SUPPORT == ENABLED)
527  //IPv4 address expected?
528  if(entry->type == HOST_TYPE_IPV4)
529  {
530  //A resource record found?
531  if(ntohs(record->rtype) == DNS_RR_TYPE_A &&
532  ntohs(record->rdlength) == sizeof(Ipv4Addr))
533  {
534  //Copy the IPv4 address
535  entry->ipAddr.length = sizeof(Ipv4Addr);
536  ipv4CopyAddr(&entry->ipAddr.ipv4Addr, record->rdata);
537 
538  //Save current time
539  entry->timestamp = osGetSystemTime();
540  //Save TTL value
541  entry->timeout = ntohl(record->ttl) * 1000;
542 
543  //Limit the lifetime of the DNS cache entries
544  entry->timeout = MIN(entry->timeout, DNS_MAX_LIFETIME);
545  entry->timeout = MAX(entry->timeout, DNS_MIN_LIFETIME);
546 
547  //Unregister UDP callback function
548  udpDetachRxCallback(interface, entry->port);
549  //Host name successfully resolved
550  entry->state = DNS_STATE_RESOLVED;
551 
552  //Exit immediately
553  break;
554  }
555  }
556 #endif
557 #if (IPV6_SUPPORT == ENABLED)
558  //IPv6 address expected?
559  if(entry->type == HOST_TYPE_IPV6)
560  {
561  //AAAA resource record found?
562  if(ntohs(record->rtype) == DNS_RR_TYPE_AAAA &&
563  ntohs(record->rdlength) == sizeof(Ipv6Addr))
564  {
565  //Copy the IPv6 address
566  entry->ipAddr.length = sizeof(Ipv6Addr);
567  ipv6CopyAddr(&entry->ipAddr.ipv6Addr, record->rdata);
568 
569  //Save current time
570  entry->timestamp = osGetSystemTime();
571  //Save TTL value
572  entry->timeout = ntohl(record->ttl) * 1000;
573 
574  //Limit the lifetime of the DNS cache entries
575  entry->timeout = MIN(entry->timeout, DNS_MAX_LIFETIME);
576  entry->timeout = MAX(entry->timeout, DNS_MIN_LIFETIME);
577 
578  //Unregister UDP callback function
579  udpDetachRxCallback(interface, entry->port);
580  //Host name successfully resolved
581  entry->state = DNS_STATE_RESOLVED;
582 
583  //Exit immediately
584  break;
585  }
586  }
587 #endif
588  //Point to the next resource record
589  pos += ntohs(record->rdlength);
590  }
591 
592  //We are done
593  break;
594  }
595  }
596  }
597 }
598 
599 
600 /**
601  * @brief Select the next DNS server
602  * @param[in] entry Pointer to a valid DNS cache entry
603  **/
604 
606 {
607  error_t error;
608 
609 #if defined(DNS_SELECT_NEXT_SERVER_HOOK)
610  DNS_SELECT_NEXT_SERVER_HOOK(entry);
611 #endif
612 
613  //Select the next DNS server
614  entry->dnsServerIndex++;
615 
616  //An identifier is used by the DNS client to match replies with
617  //corresponding requests
618  entry->id = (uint16_t) netGenerateRand();
619 
620  //Initialize retransmission counter
622  //Send DNS query
623  error = dnsSendQuery(entry);
624 
625  //DNS message successfully sent?
626  if(!error)
627  {
628  //Save the time at which the query message was sent
629  entry->timestamp = osGetSystemTime();
630  //Set timeout value
632  //Decrement retransmission counter
633  entry->retransmitCount--;
634  }
635  else
636  {
637  //Unregister UDP callback function
638  udpDetachRxCallback(entry->interface, entry->port);
639  //Host name resolution failed
640  entry->state = DNS_STATE_FAILED;
641  }
642 }
643 
644 #endif
error_t dnsSendQuery(DnsCacheEntry *entry)
Send a DNS query message.
Definition: dns_client.c:236
#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:100
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:72
@ HOST_NAME_RESOLVER_DNS
Definition: socket.h:229
#define netMutex
Definition: net_legacy.h:195
Ipv4Addr dnsServerList[IPV4_DNS_SERVER_LIST_SIZE]
DNS servers.
Definition: ipv4.h:435
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
systime_t timeout
Retransmission timeout.
Definition: dns_cache.h:109
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:804
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:260
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:101
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:127
@ 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
IpAddr ipAddr
IP address.
Definition: dns_cache.h:107
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:298
IPv6 context.
Definition: ipv6.h:496
@ ERROR_NO_DNS_SERVER
Definition: error.h:255
IPv4 context.
Definition: ipv4.h:427
uint32_t netGenerateRand(void)
Generate a random 32-bit value.
Definition: net_misc.c:937
@ 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:105
@ 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:104
@ 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:106
systime_t timestamp
Timestamp to manage entry lifetime.
Definition: dns_cache.h:108
#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:507
#define IPV4_DNS_SERVER_LIST_SIZE
Definition: ipv4.h:76
error_t udpDetachRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1062
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
error_t udpAttachRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1021
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
error_t udpSendBuffer(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:658
DnsCacheEntry * dnsCreateEntry(void)
Create a new entry in the DNS cache.
Definition: dns_cache.c:103
#define NetTxAncillary
Definition: net_misc.h:36
systime_t maxTimeout
Maximum retransmission timeout.
Definition: dns_cache.h:110
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:66
#define IPV6_DNS_SERVER_LIST_SIZE
Definition: ipv6.h:93
#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
uint16_t udpGetDynamicPort(void)
Get an ephemeral port number.
Definition: udp.c:81
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
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:184
Ipv4Addr ipv4Addr
Definition: ip.h:95
DNS cache management.
#define ipv6CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv6.h:123
#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:102
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
#define DNS_CACHE_SIZE
Definition: dns_cache.h:47
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
@ 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:153
#define ipv4CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv4.h:155
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:51
DnsState state
Entry state.
Definition: dns_cache.h:99
void dnsSelectNextServer(DnsCacheEntry *entry)
Select the next DNS server.
Definition: dns_client.c:605
uint_t dnsServerIndex
This parameter selects between the primary and secondary DNS server.
Definition: dns_cache.h:103
Ipv6Addr ipv6Addr
Definition: ip.h:98
uint_t retransmitCount
Retransmission counter.
Definition: dns_cache.h:111
#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:401
#define ntohl(value)
Definition: cpu_endian.h:422
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:117
systime_t osGetSystemTime(void)
Retrieve system time.
Ipv4Addr destIpAddr
Definition: ipcp.h:80