dns_cache.c
Go to the documentation of this file.
1 /**
2  * @file dns_cache.c
3  * @brief DNS cache management
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 "mdns/mdns_client.h"
39 #include "netbios/nbns_client.h"
40 #include "llmnr/llmnr_client.h"
41 #include "core/udp.h"
42 #include "debug.h"
43 
44 //Check TCP/IP stack configuration
45 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
46  NBNS_CLIENT_SUPPORT == ENABLED || LLMNR_CLIENT_SUPPORT == ENABLED)
47 
48 //Tick counter to handle periodic operations
50 //DNS cache
52 
53 
54 /**
55  * @brief DNS cache initialization
56  * @return Error code
57  **/
58 
60 {
61  //Initialize DNS cache
62  osMemset(dnsCache, 0, sizeof(dnsCache));
63 
64  //Successful initialization
65  return NO_ERROR;
66 }
67 
68 
69 /**
70  * @brief Flush DNS cache
71  * @param[in] interface Underlying network interface
72  **/
73 
74 void dnsFlushCache(NetInterface *interface)
75 {
76  uint_t i;
77  DnsCacheEntry *entry;
78 
79  //Go through DNS cache
80  for(i = 0; i < DNS_CACHE_SIZE; i++)
81  {
82  //Point to the current entry
83  entry = &dnsCache[i];
84 
85  //Check whether the entry is currently in use
86  if(entry->state != DNS_STATE_NONE)
87  {
88  //Delete DNS entries only for the given network interface
89  if(entry->interface == interface)
90  {
91  dnsDeleteEntry(entry);
92  }
93  }
94  }
95 }
96 
97 
98 /**
99  * @brief Create a new entry in the DNS cache
100  * @return Pointer to the newly created entry
101  **/
102 
104 {
105  uint_t i;
106  systime_t time;
107  DnsCacheEntry *entry;
108  DnsCacheEntry *oldestEntry;
109 
110  //Get current time
111  time = osGetSystemTime();
112 
113  //Keep track of the oldest entry
114  oldestEntry = &dnsCache[0];
115 
116  //Loop through DNS cache entries
117  for(i = 0; i < DNS_CACHE_SIZE; i++)
118  {
119  //Point to the current entry
120  entry = &dnsCache[i];
121 
122  //Check whether the entry is currently in use or not
123  if(entry->state == DNS_STATE_NONE)
124  {
125  //Erase contents
126  osMemset(entry, 0, sizeof(DnsCacheEntry));
127  //Return a pointer to the DNS entry
128  return entry;
129  }
130 
131  //Keep track of the oldest entry in the table
132  if((time - entry->timestamp) > (time - oldestEntry->timestamp))
133  {
134  oldestEntry = entry;
135  }
136  }
137 
138  //The oldest entry is removed whenever the table runs out of space
139  dnsDeleteEntry(oldestEntry);
140  //Erase contents
141  osMemset(oldestEntry, 0, sizeof(DnsCacheEntry));
142 
143  //Return a pointer to the DNS entry
144  return oldestEntry;
145 }
146 
147 
148 /**
149  * @brief Delete the specified DNS cache entry
150  * @param[in] entry Pointer to the DNS cache entry to be deleted
151  **/
152 
154 {
155  //Make sure the specified entry is valid
156  if(entry != NULL)
157  {
158  //Name resolution in progress?
159  if(entry->state == DNS_STATE_IN_PROGRESS)
160  {
161  //Unregister UDP callback function
162  if(entry->port != 0)
163  {
164  udpDetachRxCallback(entry->interface, entry->port);
165  }
166  }
167 
168  //Delete DNS cache entry
169  entry->state = DNS_STATE_NONE;
170  }
171 }
172 
173 
174 /**
175  * @brief Search the DNS cache for a given domain name
176  * @param[in] interface Underlying network interface
177  * @param[in] name Domain name
178  * @param[in] type Host type (IPv4 or IPv6)
179  * @param[in] protocol Host name resolution protocol
180  * @return A pointer to the matching DNS entry is returned. NULL is returned
181  * if the specified domain name could not be found in the DNS cache
182  **/
183 
186 {
187  uint_t i;
188  DnsCacheEntry *entry;
189 
190  //Loop through DNS cache entries
191  for(i = 0; i < DNS_CACHE_SIZE; i++)
192  {
193  //Point to the current entry
194  entry = &dnsCache[i];
195 
196  //Make sure that the entry is currently in use
197  if(entry->state == DNS_STATE_NONE)
198  continue;
199 
200  //Filter out entries that do not match the specified criteria
201  if(entry->interface != interface)
202  continue;
203  if(entry->type != type && type != HOST_TYPE_ANY)
204  continue;
206  continue;
207 
208  //Does the entry match the specified domain name?
209  if(name == NULL || osStrcasecmp(entry->name, name) == 0)
210  return entry;
211  }
212 
213  //No matching entry in the DNS cache
214  return NULL;
215 }
216 
217 
218 /**
219  * @brief DNS timer handler
220  *
221  * This routine must be periodically called by the TCP/IP stack to
222  * manage DNS cache
223  *
224  **/
225 
226 void dnsTick(void)
227 {
228  error_t error;
229  uint_t i;
230  systime_t time;
231  DnsCacheEntry *entry;
232 
233  //Get current time
234  time = osGetSystemTime();
235 
236  //Go through DNS cache
237  for(i = 0; i < DNS_CACHE_SIZE; i++)
238  {
239  //Point to the current entry
240  entry = &dnsCache[i];
241 
242  //Name resolution in progress?
243  if(entry->state == DNS_STATE_IN_PROGRESS)
244  {
245  //The request timed out?
246  if(timeCompare(time, entry->timestamp + entry->timeout) >= 0)
247  {
248  //Check whether the maximum number of retransmissions has been exceeded
249  if(entry->retransmitCount > 0)
250  {
251 #if (DNS_CLIENT_SUPPORT == ENABLED)
252  //DNS resolver?
253  if(entry->protocol == HOST_NAME_RESOLVER_DNS)
254  {
255  //Retransmit DNS query
256  error = dnsSendQuery(entry);
257  }
258  else
259 #endif
260 #if (MDNS_CLIENT_SUPPORT == ENABLED)
261  //mDNS resolver?
262  if(entry->protocol == HOST_NAME_RESOLVER_MDNS)
263  {
264  //Retransmit mDNS query
265  error = mdnsClientSendQuery(entry);
266  }
267  else
268 #endif
269 #if (NBNS_CLIENT_SUPPORT == ENABLED && IPV4_SUPPORT == ENABLED)
270  //NetBIOS Name Service resolver?
271  if(entry->protocol == HOST_NAME_RESOLVER_NBNS)
272  {
273  //Retransmit NBNS query
274  error = nbnsSendQuery(entry);
275  }
276  else
277 #endif
278 #if (LLMNR_CLIENT_SUPPORT == ENABLED)
279  //LLMNR resolver?
280  if(entry->protocol == HOST_NAME_RESOLVER_LLMNR)
281  {
282  //Retransmit LLMNR query
283  error = llmnrSendQuery(entry);
284  }
285  else
286 #endif
287  //Unknown protocol?
288  {
289  error = ERROR_FAILURE;
290  }
291 
292  //Query message successfully sent?
293  if(!error)
294  {
295  //Save the time at which the query message was sent
296  entry->timestamp = time;
297  //The timeout value is doubled for each subsequent retransmission
298  entry->timeout = MIN(entry->timeout * 2, entry->maxTimeout);
299  //Decrement retransmission counter
300  entry->retransmitCount--;
301  }
302  else
303  {
304  //Unregister UDP callback function
305  if(entry->port != 0)
306  {
307  udpDetachRxCallback(entry->interface, entry->port);
308  }
309 
310  //Host name resolution failed
311  entry->state = DNS_STATE_FAILED;
312  }
313  }
314 #if (DNS_CLIENT_SUPPORT == ENABLED)
315  //DNS resolver?
316  else if(entry->protocol == HOST_NAME_RESOLVER_DNS)
317  {
318  //Select the next DNS server
319  dnsSelectNextServer(entry);
320  }
321 #endif
322  else
323  {
324  //Unregister UDP callback function
325  if(entry->port != 0)
326  {
327  udpDetachRxCallback(entry->interface, entry->port);
328  }
329 
330  //Host name resolution failed
331  entry->state = DNS_STATE_FAILED;
332  }
333  }
334  }
335  //Name successfully resolved?
336  else if(entry->state == DNS_STATE_RESOLVED)
337  {
338  //Check the lifetime of the current DNS cache entry
339  if(timeCompare(time, entry->timestamp + entry->timeout) >= 0)
340  {
341  //Periodically time out DNS cache entries
342  dnsDeleteEntry(entry);
343  }
344  }
345  }
346 }
347 
348 #endif
error_t dnsSendQuery(DnsCacheEntry *entry)
Send a DNS query message.
Definition: dns_client.c:236
HostType
Host types.
Definition: socket.h:215
HostType type
IPv4 or IPv6 host?
Definition: dns_cache.h:100
HostnameResolver
Name resolution protocols.
Definition: socket.h:227
uint8_t protocol
Definition: ipv4.h:327
@ HOST_NAME_RESOLVER_DNS
Definition: socket.h:229
@ HOST_TYPE_ANY
Definition: socket.h:216
error_t llmnrSendQuery(DnsCacheEntry *entry)
Send a LLMNR query message.
Definition: llmnr_client.c:232
systime_t timeout
Retransmission timeout.
Definition: dns_cache.h:109
@ HOST_NAME_RESOLVER_LLMNR
Definition: socket.h:232
uint8_t type
Definition: coap_common.h:176
HostnameResolver protocol
Name resolution protocol.
Definition: dns_cache.h:101
char_t name[]
void dnsFlushCache(NetInterface *interface)
Flush DNS cache.
Definition: dns_cache.c:74
#define timeCompare(t1, t2)
Definition: os_port.h:40
error_t nbnsSendQuery(DnsCacheEntry *entry)
Send a NBNS query message.
Definition: nbns_client.c:213
uint16_t port
Port number used by the resolver.
Definition: dns_cache.h:104
error_t
Error codes.
Definition: error.h:43
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
@ HOST_NAME_RESOLVER_MDNS
Definition: socket.h:230
NBNS client (NetBIOS Name Service)
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ DNS_STATE_FAILED
Definition: dns_cache.h:88
error_t udpDetachRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1062
#define NetInterface
Definition: net.h:36
DNS cache entry.
Definition: dns_cache.h:98
#define osStrcasecmp(s1, s2)
Definition: os_port.h:186
DnsCacheEntry * dnsCreateEntry(void)
Create a new entry in the DNS cache.
Definition: dns_cache.c:103
mDNS client (Multicast DNS)
systime_t maxTimeout
Maximum retransmission timeout.
Definition: dns_cache.h:110
#define MIN(a, b)
Definition: os_port.h:63
void dnsTick(void)
DNS timer handler.
Definition: dns_cache.c:226
DNS client (Domain Name System)
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:55
LLMNR client (Link-Local Multicast Name Resolution)
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
DNS cache management.
uint32_t time
@ HOST_NAME_RESOLVER_ANY
Definition: socket.h:228
@ DNS_STATE_IN_PROGRESS
Definition: dns_cache.h:86
NetInterface * interface
Underlying network interface.
Definition: dns_cache.h:102
#define DNS_CACHE_SIZE
Definition: dns_cache.h:47
@ DNS_STATE_RESOLVED
Definition: dns_cache.h:87
UDP (User Datagram Protocol)
@ HOST_NAME_RESOLVER_NBNS
Definition: socket.h:231
error_t mdnsClientSendQuery(DnsCacheEntry *entry)
Send a mDNS query message.
Definition: mdns_client.c:214
void dnsDeleteEntry(DnsCacheEntry *entry)
Delete the specified DNS cache entry.
Definition: dns_cache.c:153
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 retransmitCount
Retransmission counter.
Definition: dns_cache.h:111
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
error_t dnsInit(void)
DNS cache initialization.
Definition: dns_cache.c:59
@ DNS_STATE_NONE
Definition: dns_cache.h:85
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
systime_t osGetSystemTime(void)
Retrieve system time.
systime_t dnsTickCounter
Definition: dns_cache.c:49