|
@@ -20,7 +20,10 @@
|
|
|
#define DNS_MAX_ANSWERS 4
|
|
#define DNS_MAX_ANSWERS 4
|
|
|
#define DNS_RETRY_MS 1000
|
|
#define DNS_RETRY_MS 1000
|
|
|
#define DNS_RETRIES 3
|
|
#define DNS_RETRIES 3
|
|
|
-#define DNS_WAIT_MS 5000
|
|
|
|
|
|
|
+#define DNS_WAIT_MS 15000
|
|
|
|
|
+#define DNS_CACHE_MAX 8
|
|
|
|
|
+#define DNS_CACHE_TTL_MS 300000
|
|
|
|
|
+#define DNS_CACHE_NAME 96
|
|
|
|
|
|
|
|
/* Query buffer: 12-byte header + a 255-byte name + qtype/qclass. */
|
|
/* Query buffer: 12-byte header + a 255-byte name + qtype/qclass. */
|
|
|
static unsigned char query_buf[12 + 260 + 4];
|
|
static unsigned char query_buf[12 + 260 + 4];
|
|
@@ -37,6 +40,16 @@ static unsigned char dns_pending_extra[DNS_MAX_ANSWERS][4];
|
|
|
static unsigned int dns_pending_count;
|
|
static unsigned int dns_pending_count;
|
|
|
static int dns_pending_result; /* 0 none, 1 answer, -1 failure */
|
|
static int dns_pending_result; /* 0 none, 1 answer, -1 failure */
|
|
|
|
|
|
|
|
|
|
+/* Small A-record cache so parallel browser workers that ask for the same
|
|
|
|
|
+ * host do not queue a separate query each. */
|
|
|
|
|
+struct dns_cache_entry {
|
|
|
|
|
+ char name[DNS_CACHE_NAME];
|
|
|
|
|
+ unsigned char ip[4];
|
|
|
|
|
+ unsigned int age_ms;
|
|
|
|
|
+ int used;
|
|
|
|
|
+};
|
|
|
|
|
+static struct dns_cache_entry dns_cache[DNS_CACHE_MAX];
|
|
|
|
|
+
|
|
|
static void put_dec(unsigned int v)
|
|
static void put_dec(unsigned int v)
|
|
|
{
|
|
{
|
|
|
char b[12];
|
|
char b[12];
|
|
@@ -256,6 +269,77 @@ static void dns_print_answer(const char *name, const unsigned char *ip,
|
|
|
io_putchar('\n');
|
|
io_putchar('\n');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static void dns_print_cached(const char *name, const unsigned char *ip)
|
|
|
|
|
+{
|
|
|
|
|
+ io_putstr("dns: ");
|
|
|
|
|
+ io_putstr(name);
|
|
|
|
|
+ io_putstr(" -> ");
|
|
|
|
|
+ put_ip(ip);
|
|
|
|
|
+ io_putchar('\n');
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int dns_name_eq(const char *a, const char *b)
|
|
|
|
|
+{
|
|
|
|
|
+ while (*a && *b && *a == *b)
|
|
|
|
|
+ a++, b++;
|
|
|
|
|
+ return *a == *b;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int dns_cache_lookup(const char *name, unsigned char ip[4])
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int i, now = get_ticks_ms();
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < DNS_CACHE_MAX; i++) {
|
|
|
|
|
+ if (!dns_cache[i].used ||
|
|
|
|
|
+ !dns_name_eq(dns_cache[i].name, name))
|
|
|
|
|
+ continue;
|
|
|
|
|
+ if (now - dns_cache[i].age_ms > DNS_CACHE_TTL_MS) {
|
|
|
|
|
+ dns_cache[i].used = 0;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ ip[0] = dns_cache[i].ip[0];
|
|
|
|
|
+ ip[1] = dns_cache[i].ip[1];
|
|
|
|
|
+ ip[2] = dns_cache[i].ip[2];
|
|
|
|
|
+ ip[3] = dns_cache[i].ip[3];
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void dns_cache_put(const char *name, const unsigned char ip[4])
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int i, n = 0, slot = 0;
|
|
|
|
|
+
|
|
|
|
|
+ while (name[n] && n + 1 < DNS_CACHE_NAME)
|
|
|
|
|
+ n++;
|
|
|
|
|
+ if (name[n]) /* too long: do not truncate */
|
|
|
|
|
+ return;
|
|
|
|
|
+ for (i = 0; i < DNS_CACHE_MAX; i++) {
|
|
|
|
|
+ if (dns_cache[i].used && dns_name_eq(dns_cache[i].name, name)) {
|
|
|
|
|
+ slot = i;
|
|
|
|
|
+ goto store;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ for (i = 0; i < DNS_CACHE_MAX; i++) {
|
|
|
|
|
+ if (!dns_cache[i].used) {
|
|
|
|
|
+ slot = i;
|
|
|
|
|
+ goto store;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dns_cache[i].age_ms < dns_cache[slot].age_ms)
|
|
|
|
|
+ slot = i;
|
|
|
|
|
+ }
|
|
|
|
|
+store:
|
|
|
|
|
+ for (i = 0; i < n; i++)
|
|
|
|
|
+ dns_cache[slot].name[i] = name[i];
|
|
|
|
|
+ dns_cache[slot].name[n] = '\0';
|
|
|
|
|
+ dns_cache[slot].ip[0] = ip[0];
|
|
|
|
|
+ dns_cache[slot].ip[1] = ip[1];
|
|
|
|
|
+ dns_cache[slot].ip[2] = ip[2];
|
|
|
|
|
+ dns_cache[slot].ip[3] = ip[3];
|
|
|
|
|
+ dns_cache[slot].age_ms = get_ticks_ms();
|
|
|
|
|
+ dns_cache[slot].used = 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
int dns_resolve(const char *name, unsigned char ip[4])
|
|
int dns_resolve(const char *name, unsigned char ip[4])
|
|
|
{
|
|
{
|
|
|
unsigned char server[4], hop[4], server_mac[6];
|
|
unsigned char server[4], hop[4], server_mac[6];
|
|
@@ -271,6 +355,10 @@ int dns_resolve(const char *name, unsigned char ip[4])
|
|
|
io_putstr("dns: no NIC found\n");
|
|
io_putstr("dns: no NIC found\n");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
+ if (dns_cache_lookup(name, ip)) {
|
|
|
|
|
+ dns_print_cached(name, ip);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
raw = pid && netdev_owner() == pid;
|
|
raw = pid && netdev_owner() == pid;
|
|
|
if (!raw)
|
|
if (!raw)
|
|
@@ -294,6 +382,12 @@ int dns_resolve(const char *name, unsigned char ip[4])
|
|
|
dns_pending_result = 0;
|
|
dns_pending_result = 0;
|
|
|
dns_pending_count = 0;
|
|
dns_pending_count = 0;
|
|
|
|
|
|
|
|
|
|
+ /* The waiter we just queued behind may have filled the cache. */
|
|
|
|
|
+ if (dns_cache_lookup(name, ip)) {
|
|
|
|
|
+ dns_print_cached(name, ip);
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
net_get_dns(server);
|
|
net_get_dns(server);
|
|
|
if (!(server[0] || server[1] || server[2] || server[3])) {
|
|
if (!(server[0] || server[1] || server[2] || server[3])) {
|
|
|
/* Home routers usually proxy DNS for their clients. */
|
|
/* Home routers usually proxy DNS for their clients. */
|
|
@@ -370,6 +464,7 @@ int dns_resolve(const char *name, unsigned char ip[4])
|
|
|
goto out;
|
|
goto out;
|
|
|
if (r == 0)
|
|
if (r == 0)
|
|
|
continue;
|
|
continue;
|
|
|
|
|
+ dns_cache_put(name, ip);
|
|
|
dns_print_answer(name, ip, extra, count);
|
|
dns_print_answer(name, ip, extra, count);
|
|
|
r = 0;
|
|
r = 0;
|
|
|
goto out;
|
|
goto out;
|
|
@@ -386,6 +481,7 @@ int dns_resolve(const char *name, unsigned char ip[4])
|
|
|
dns_pending_extra[i][j];
|
|
dns_pending_extra[i][j];
|
|
|
}
|
|
}
|
|
|
count = dns_pending_count;
|
|
count = dns_pending_count;
|
|
|
|
|
+ dns_cache_put(name, ip);
|
|
|
dns_print_answer(name, ip, extra, count);
|
|
dns_print_answer(name, ip, extra, count);
|
|
|
r = 0;
|
|
r = 0;
|
|
|
goto out;
|
|
goto out;
|