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-2019 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 1.9.6
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 "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)
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  memset(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 used
86  if(entry->state != DNS_STATE_NONE)
87  {
88  //Delete DNS entries only for the given network interface
89  if(entry->interface == interface)
90  dnsDeleteEntry(entry);
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 used or not
121  if(entry->state == DNS_STATE_NONE)
122  {
123  //Erase contents
124  memset(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  memset(oldestEntry, 0, sizeof(DnsCacheEntry));
140  //Return a pointer to the DNS entry
141  return oldestEntry;
142 }
143 
144 
145 /**
146  * @brief Delete the specified DNS cache entry
147  * @param[in] entry Pointer to the DNS cache entry to be deleted
148  **/
149 
151 {
152  //Make sure the specified entry is valid
153  if(entry != NULL)
154  {
155 #if (DNS_CLIENT_SUPPORT == ENABLED)
156  //DNS resolver?
157  if(entry->protocol == HOST_NAME_RESOLVER_DNS)
158  {
159  //Name resolution in progress?
160  if(entry->state == DNS_STATE_IN_PROGRESS)
161  {
162  //Unregister user callback
163  udpDetachRxCallback(entry->interface, entry->port);
164  }
165  }
166 #endif
167  //Delete DNS cache entry
168  entry->state = DNS_STATE_NONE;
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 used
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 || !strcasecmp(entry->name, name))
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  //Unknown protocol?
278  {
279  error = ERROR_FAILURE;
280  }
281 
282  //Query message successfully sent?
283  if(!error)
284  {
285  //Save the time at which the query message was sent
286  entry->timestamp = time;
287  //The timeout value is doubled for each subsequent retransmission
288  entry->timeout = MIN(entry->timeout * 2, entry->maxTimeout);
289  //Decrement retransmission counter
290  entry->retransmitCount--;
291  }
292  else
293  {
294  //The entry should be deleted since name resolution has failed
295  dnsDeleteEntry(entry);
296  }
297  }
298 #if (DNS_CLIENT_SUPPORT == ENABLED)
299  //DNS resolver?
300  else if(entry->protocol == HOST_NAME_RESOLVER_DNS)
301  {
302  //Select the next DNS server
303  entry->dnsServerNum++;
304  //Initialize retransmission counter
306  //Send DNS query
307  error = dnsSendQuery(entry);
308 
309  //DNS message successfully sent?
310  if(!error)
311  {
312  //Save the time at which the query message was sent
313  entry->timestamp = time;
314  //Set timeout value
316  //Decrement retransmission counter
317  entry->retransmitCount--;
318  }
319  else
320  {
321  //The entry should be deleted since name resolution has failed
322  dnsDeleteEntry(entry);
323  }
324  }
325 #endif
326  else
327  {
328  //The maximum number of retransmissions has been exceeded
329  dnsDeleteEntry(entry);
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:217
HostType
Host types.
Definition: socket.h:171
HostType type
IPv4 or IPv6 host?
Definition: dns_cache.h:99
HostnameResolver
Name resolution protocols.
Definition: socket.h:183
@ HOST_NAME_RESOLVER_DNS
Definition: socket.h:186
@ HOST_TYPE_ANY
Definition: socket.h:173
systime_t timeout
Retransmission timeout.
Definition: dns_cache.h:108
HostnameResolver protocol
Name resolution protocol.
Definition: dns_cache.h:100
#define DNS_CLIENT_MAX_RETRIES
Definition: dns_client.h:49
char_t name[]
void dnsFlushCache(NetInterface *interface)
Flush DNS cache.
Definition: dns_cache.c:74
#define timeCompare(t1, t2)
Definition: os_port.h:42
error_t nbnsSendQuery(DnsCacheEntry *entry)
Send a NBNS query message.
Definition: nbns_client.c:193
uint16_t port
Port number used by the resolver.
Definition: dns_cache.h:103
uint_t dnsServerNum
This parameter selects between the primary and secondary DNS server.
Definition: dns_cache.h:102
char_t type
error_t
Error codes.
Definition: error.h:42
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
uint8_t protocol
@ HOST_NAME_RESOLVER_MDNS
Definition: socket.h:187
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:811
#define NetInterface
Definition: net.h:36
DNS cache entry.
Definition: dns_cache.h:96
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:109
#define MIN(a, b)
Definition: os_port.h:62
void dnsTick(void)
DNS timer handler.
Definition: dns_cache.c:225
DNS client (Domain Name System)
char char_t
Definition: compiler_port.h:43
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:185
@ 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:188
#define DNS_CLIENT_INIT_TIMEOUT
Definition: dns_client.h:56
#define strcasecmp
error_t mdnsClientSendQuery(DnsCacheEntry *entry)
Send a mDNS query message.
Definition: mdns_client.c:197
void dnsDeleteEntry(DnsCacheEntry *entry)
Delete the specified DNS cache entry.
Definition: dns_cache.c:150
DnsCacheEntry dnsCache[DNS_CACHE_SIZE]
Definition: dns_cache.c:51
DnsState state
Entry state.
Definition: dns_cache.h:98
uint_t retransmitCount
Retransmission counter.
Definition: dns_cache.h:110
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
error_t dnsInit(void)
DNS cache initialization.
Definition: dns_cache.c:59
@ DNS_STATE_NONE
Definition: dns_cache.h:85
uint32_t systime_t
Definition: compiler_port.h:46
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
systime_t osGetSystemTime(void)
Retrieve system time.
systime_t dnsTickCounter
Definition: dns_cache.c:49