mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
LookupServer: Cache successful DNS lookup responses for 1 minute
This reduces DNS traffic spam during web browsing. We can definitely do a lot better here, this is just a very low-hanging fruit.
This commit is contained in:
parent
005b416639
commit
3a4da5aa05
1 changed files with 17 additions and 0 deletions
|
@ -16,6 +16,7 @@
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define T_A 1
|
#define T_A 1
|
||||||
|
@ -27,6 +28,13 @@
|
||||||
|
|
||||||
#define C_IN 1
|
#define C_IN 1
|
||||||
|
|
||||||
|
struct CachedLookup {
|
||||||
|
time_t timestamp { 0 };
|
||||||
|
unsigned short record_type { 0 };
|
||||||
|
Vector<String> responses;
|
||||||
|
};
|
||||||
|
|
||||||
|
static HashMap<String, CachedLookup> lookup_cache;
|
||||||
static HashMap<String, String> dns_custom_hostnames;
|
static HashMap<String, String> dns_custom_hostnames;
|
||||||
|
|
||||||
static Vector<String> lookup(const String& hostname, bool& did_timeout, const String& DNS_IP, unsigned short record_type);
|
static Vector<String> lookup(const String& hostname, bool& did_timeout, const String& DNS_IP, unsigned short record_type);
|
||||||
|
@ -207,6 +215,14 @@ static u16 get_next_id()
|
||||||
|
|
||||||
Vector<String> lookup(const String& hostname, bool& did_timeout, const String& DNS_IP, unsigned short record_type)
|
Vector<String> lookup(const String& hostname, bool& did_timeout, const String& DNS_IP, unsigned short record_type)
|
||||||
{
|
{
|
||||||
|
if (auto it = lookup_cache.find(hostname); it != lookup_cache.end()) {
|
||||||
|
auto& cached_lookup = it->value;
|
||||||
|
if (cached_lookup.record_type == record_type && cached_lookup.timestamp < (time(nullptr) + 60)) {
|
||||||
|
return it->value.responses;
|
||||||
|
}
|
||||||
|
lookup_cache.remove(it);
|
||||||
|
}
|
||||||
|
|
||||||
DNSPacket request_header;
|
DNSPacket request_header;
|
||||||
request_header.set_id(get_next_id());
|
request_header.set_id(get_next_id());
|
||||||
request_header.set_is_query();
|
request_header.set_is_query();
|
||||||
|
@ -335,6 +351,7 @@ Vector<String> lookup(const String& hostname, bool& did_timeout, const String& D
|
||||||
// FIXME: Parse some other record types perhaps?
|
// FIXME: Parse some other record types perhaps?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lookup_cache.set(hostname, { time(nullptr), record_type, addresses });
|
||||||
return addresses;
|
return addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue