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