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