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-2024 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.4.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  //Return a pointer to the DNS entry
143  return oldestEntry;
144 }
145 
146 
147 /**
148  * @brief Delete the specified DNS cache entry
149  * @param[in] entry Pointer to the DNS cache entry to be deleted
150  **/
151 
153 {
154  //Make sure the specified entry is valid
155  if(entry != NULL)
156  {
157 #if (DNS_CLIENT_SUPPORT == ENABLED || LLMNR_CLIENT_SUPPORT == ENABLED)
158  //DNS or LLMNR resolver?
159  if(entry->protocol == HOST_NAME_RESOLVER_DNS ||
161  {
162  //Name resolution in progress?
163  if(entry->state == DNS_STATE_IN_PROGRESS)
164  {
165  //Unregister user callback
166  udpDetachRxCallback(entry->interface, entry->port);
167  }
168  }
169 #endif
170  //Delete DNS cache entry
171  entry->state = DNS_STATE_NONE;
172  }
173 }
174 
175 
176 /**
177  * @brief Search the DNS cache for a given domain name
178  * @param[in] interface Underlying network interface
179  * @param[in] name Domain name
180  * @param[in] type Host type (IPv4 or IPv6)
181  * @param[in] protocol Host name resolution protocol
182  * @return A pointer to the matching DNS entry is returned. NULL is returned
183  * if the specified domain name could not be found in the DNS cache
184  **/
185 
188 {
189  uint_t i;
190  DnsCacheEntry *entry;
191 
192  //Loop through DNS cache entries
193  for(i = 0; i < DNS_CACHE_SIZE; i++)
194  {
195  //Point to the current entry
196  entry = &dnsCache[i];
197 
198  //Make sure that the entry is currently in use
199  if(entry->state == DNS_STATE_NONE)
200  continue;
201 
202  //Filter out entries that do not match the specified criteria
203  if(entry->interface != interface)
204  continue;
205  if(entry->type != type && type != HOST_TYPE_ANY)
206  continue;
208  continue;
209 
210  //Does the entry match the specified domain name?
211  if(name == NULL || osStrcasecmp(entry->name, name) == 0)
212  return entry;
213  }
214 
215  //No matching entry in the DNS cache
216  return NULL;
217 }
218 
219 
220 /**
221  * @brief DNS timer handler
222  *
223  * This routine must be periodically called by the TCP/IP stack to
224  * manage DNS cache
225  *
226  **/
227 
228 void dnsTick(void)
229 {
230  error_t error;
231  uint_t i;
232  systime_t time;
233  DnsCacheEntry *entry;
234 
235  //Get current time
236  time = osGetSystemTime();
237 
238  //Go through DNS cache
239  for(i = 0; i < DNS_CACHE_SIZE; i++)
240  {
241  //Point to the current entry
242  entry = &dnsCache[i];
243 
244  //Name resolution in progress?
245  if(entry->state == DNS_STATE_IN_PROGRESS)
246  {
247  //The request timed out?
248  if(timeCompare(time, entry->timestamp + entry->timeout) >= 0)
249  {
250  //Check whether the maximum number of retransmissions has been exceeded
251  if(entry->retransmitCount > 0)
252  {
253 #if (DNS_CLIENT_SUPPORT == ENABLED)
254  //DNS resolver?
255  if(entry->protocol == HOST_NAME_RESOLVER_DNS)
256  {
257  //Retransmit DNS query
258  error = dnsSendQuery(entry);
259  }
260  else
261 #endif
262 #if (MDNS_CLIENT_SUPPORT == ENABLED)
263  //mDNS resolver?
264  if(entry->protocol == HOST_NAME_RESOLVER_MDNS)
265  {
266  //Retransmit mDNS query
267  error = mdnsClientSendQuery(entry);
268  }
269  else
270 #endif
271 #if (NBNS_CLIENT_SUPPORT == ENABLED && IPV4_SUPPORT == ENABLED)
272  //NetBIOS Name Service resolver?
273  if(entry->protocol == HOST_NAME_RESOLVER_NBNS)
274  {
275  //Retransmit NBNS query
276  error = nbnsSendQuery(entry);
277  }
278  else
279 #endif
280 #if (LLMNR_CLIENT_SUPPORT == ENABLED)
281  //LLMNR resolver?
282  if(entry->protocol == HOST_NAME_RESOLVER_LLMNR)
283  {
284  //Retransmit LLMNR query
285  error = llmnrSendQuery(entry);
286  }
287  else
288 #endif
289  //Unknown protocol?
290  {
291  error = ERROR_FAILURE;
292  }
293 
294  //Query message successfully sent?
295  if(!error)
296  {
297  //Save the time at which the query message was sent
298  entry->timestamp = time;
299  //The timeout value is doubled for each subsequent retransmission
300  entry->timeout = MIN(entry->timeout * 2, entry->maxTimeout);
301  //Decrement retransmission counter
302  entry->retransmitCount--;
303  }
304  else
305  {
306  //The entry should be deleted since name resolution has failed
307  dnsDeleteEntry(entry);
308  }
309  }
310 #if (DNS_CLIENT_SUPPORT == ENABLED)
311  //DNS resolver?
312  else if(entry->protocol == HOST_NAME_RESOLVER_DNS)
313  {
314  //Select the next DNS server
315  dnsSelectNextServer(entry);
316  }
317 #endif
318  else
319  {
320  //The maximum number of retransmissions has been exceeded
321  dnsDeleteEntry(entry);
322  }
323  }
324  }
325  //Name successfully resolved?
326  else if(entry->state == DNS_STATE_RESOLVED)
327  {
328  //Check the lifetime of the current DNS cache entry
329  if(timeCompare(time, entry->timestamp + entry->timeout) >= 0)
330  {
331  //Periodically time out DNS cache entries
332  dnsDeleteEntry(entry);
333  }
334  }
335  }
336 }
337 
338 #endif
error_t dnsSendQuery(DnsCacheEntry *entry)
Send a DNS query message.
Definition: dns_client.c:218
HostType
Host types.
Definition: socket.h:215
HostType type
IPv4 or IPv6 host?
Definition: dns_cache.h:99
HostnameResolver
Name resolution protocols.
Definition: socket.h:227
uint8_t protocol
Definition: ipv4.h:326
@ 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:214
systime_t timeout
Retransmission timeout.
Definition: dns_cache.h:108
@ 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:100
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:195
uint16_t port
Port number used by the resolver.
Definition: dns_cache.h:103
error_t
Error codes.
Definition: error.h:43
char_t name[DNS_MAX_NAME_LEN+1]
Domain name.
Definition: dns_cache.h:105
systime_t timestamp
Time stamp to manage entry lifetime.
Definition: dns_cache.h:107
@ HOST_NAME_RESOLVER_MDNS
Definition: socket.h:230
NBNS client (NetBIOS Name Service)
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
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:97
#define osStrcasecmp(s1, s2)
Definition: os_port.h:183
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:109
#define MIN(a, b)
Definition: os_port.h:63
void dnsTick(void)
DNS timer handler.
Definition: dns_cache.c:228
DNS client (Domain Name System)
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:48
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:186
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:101
#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:196
void dnsDeleteEntry(DnsCacheEntry *entry)
Delete the specified DNS cache entry.
Definition: dns_cache.c:152
DnsCacheEntry dnsCache[DNS_CACHE_SIZE]
Definition: dns_cache.c:51
DnsState state
Entry state.
Definition: dns_cache.h:98
void dnsSelectNextServer(DnsCacheEntry *entry)
Select the next DNS server.
Definition: dns_client.c:589
uint_t retransmitCount
Retransmission counter.
Definition: dns_cache.h:110
unsigned int uint_t
Definition: compiler_port.h:50
#define osMemset(p, value, length)
Definition: os_port.h:135
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