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.0
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  }
169 }
170 
171 
172 /**
173  * @brief Search the DNS cache for a given domain name
174  * @param[in] interface Underlying network interface
175  * @param[in] name Domain name
176  * @param[in] type Host type (IPv4 or IPv6)
177  * @param[in] protocol Host name resolution protocol
178  * @return A pointer to the matching DNS entry is returned. NULL is returned
179  * if the specified domain name could not be found in the DNS cache
180  **/
181 
184 {
185  uint_t i;
186  DnsCacheEntry *entry;
187 
188  //Loop through DNS cache entries
189  for(i = 0; i < DNS_CACHE_SIZE; i++)
190  {
191  //Point to the current entry
192  entry = &dnsCache[i];
193 
194  //Make sure that the entry is currently in use
195  if(entry->state == DNS_STATE_NONE)
196  continue;
197 
198  //Filter out entries that do not match the specified criteria
199  if(entry->interface != interface)
200  continue;
201  if(entry->type != type && type != HOST_TYPE_ANY)
202  continue;
204  continue;
205 
206  //Does the entry match the specified domain name?
207  if(name == NULL || osStrcasecmp(entry->name, name) == 0)
208  return entry;
209  }
210 
211  //No matching entry in the DNS cache
212  return NULL;
213 }
214 
215 
216 /**
217  * @brief DNS timer handler
218  *
219  * This routine must be periodically called by the TCP/IP stack to
220  * manage DNS cache
221  *
222  **/
223 
224 void dnsTick(void)
225 {
226  error_t error;
227  uint_t i;
228  systime_t time;
229  DnsCacheEntry *entry;
230 
231  //Get current time
232  time = osGetSystemTime();
233 
234  //Go through DNS cache
235  for(i = 0; i < DNS_CACHE_SIZE; i++)
236  {
237  //Point to the current entry
238  entry = &dnsCache[i];
239 
240  //Name resolution in progress?
241  if(entry->state == DNS_STATE_IN_PROGRESS)
242  {
243  //The request timed out?
244  if(timeCompare(time, entry->timestamp + entry->timeout) >= 0)
245  {
246  //Check whether the maximum number of retransmissions has been exceeded
247  if(entry->retransmitCount > 0)
248  {
249 #if (DNS_CLIENT_SUPPORT == ENABLED)
250  //DNS resolver?
251  if(entry->protocol == HOST_NAME_RESOLVER_DNS)
252  {
253  //Retransmit DNS query
254  error = dnsSendQuery(entry);
255  }
256  else
257 #endif
258 #if (MDNS_CLIENT_SUPPORT == ENABLED)
259  //mDNS resolver?
260  if(entry->protocol == HOST_NAME_RESOLVER_MDNS)
261  {
262  //Retransmit mDNS query
263  error = mdnsClientSendQuery(entry);
264  }
265  else
266 #endif
267 #if (NBNS_CLIENT_SUPPORT == ENABLED && IPV4_SUPPORT == ENABLED)
268  //NetBIOS Name Service resolver?
269  if(entry->protocol == HOST_NAME_RESOLVER_NBNS)
270  {
271  //Retransmit NBNS query
272  error = nbnsSendQuery(entry);
273  }
274  else
275 #endif
276 #if (LLMNR_CLIENT_SUPPORT == ENABLED)
277  //LLMNR resolver?
278  if(entry->protocol == HOST_NAME_RESOLVER_LLMNR)
279  {
280  //Retransmit LLMNR query
281  error = llmnrSendQuery(entry);
282  }
283  else
284 #endif
285  //Unknown protocol?
286  {
287  error = ERROR_FAILURE;
288  }
289 
290  //Query message successfully sent?
291  if(!error)
292  {
293  //Save the time at which the query message was sent
294  entry->timestamp = time;
295  //The timeout value is doubled for each subsequent retransmission
296  entry->timeout = MIN(entry->timeout * 2, entry->maxTimeout);
297  //Decrement retransmission counter
298  entry->retransmitCount--;
299  }
300  else
301  {
302  //Unregister UDP callback function
303  if(entry->port != 0)
304  {
305  udpUnregisterRxCallback(entry->interface, entry->port);
306  }
307 
308  //Host name resolution failed
309  entry->state = DNS_STATE_FAILED;
310  }
311  }
312 #if (DNS_CLIENT_SUPPORT == ENABLED)
313  //DNS resolver?
314  else if(entry->protocol == HOST_NAME_RESOLVER_DNS)
315  {
316  //Select the next DNS server
317  dnsSelectNextServer(entry);
318  }
319 #endif
320  else
321  {
322  //Unregister UDP callback function
323  if(entry->port != 0)
324  {
325  udpUnregisterRxCallback(entry->interface, entry->port);
326  }
327 
328  //Host name resolution failed
329  entry->state = DNS_STATE_FAILED;
330  }
331  }
332  }
333  //Name successfully resolved?
334  else if(entry->state == DNS_STATE_RESOLVED)
335  {
336  //Check the lifetime of the current DNS cache entry
337  if(timeCompare(time, entry->timestamp + entry->timeout) >= 0)
338  {
339  //Periodically time out DNS cache entries
340  dnsDeleteEntry(entry);
341  }
342  }
343  }
344 }
345 
346 #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
error_t udpUnregisterRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1062
uint8_t protocol
Definition: ipv4.h:351
@ 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:72
#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
#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:110
#define MIN(a, b)
Definition: os_port.h:63
void dnsTick(void)
DNS timer handler.
Definition: dns_cache.c:224
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:182
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: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: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: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.