From e2e910fb41679427f73b4cbf7ff85ca51ed7e6e3 Mon Sep 17 00:00:00 2001
From: Christof Schulze <christof.schulze@gmx.net>
Date: Fri, 30 Dec 2016 17:22:22 +0100
Subject: [PATCH] gluon-core: add configuration that enables the next_node to
 be used as dns cache

---
 docs/features/dns-cache.rst                   | 30 ++++++++++++++
 package/gluon-core/check_site.lua             |  9 +++++
 .../luasrc/lib/gluon/upgrade/820-dns-config   | 40 +++++++++++++++++++
 .../330-gluon-mesh-batman-adv-core-local-node |  4 ++
 4 files changed, 83 insertions(+)
 create mode 100644 docs/features/dns-cache.rst
 create mode 100755 package/gluon-core/luasrc/lib/gluon/upgrade/820-dns-config

diff --git a/docs/features/dns-cache.rst b/docs/features/dns-cache.rst
new file mode 100644
index 000000000..f40cde017
--- /dev/null
+++ b/docs/features/dns-cache.rst
@@ -0,0 +1,30 @@
+DNS-Caching
+===========
+User experience may be greatly improved when dns is accelerated. Also it
+seems like a good idea to keep the number if packages being exchanged
+between node and gateway as small as possible. In order to do this, a
+dns-cache may be used on a node. The dnsmasq instance listening on port
+53 in the node will be re-configured to answer requests, use a list of
+upstream servers and a specific cache size if the below options are
+added to site.conf All settings are optional, though if no dns server is
+set, the configuration will not be altered by gluon-core.
+
+Besides caching dns requests from clients, the next_node-addresses are set to
+resolve to a configurable name that may optionally be placed in next_node.name.
+
+::
+
+  dns = {
+    cacheentries = 5000,
+    servers = { '2001:db8::1', },
+  },
+  
+  next_node = {
+    name = 'nextnode',
+    ip6 = '2001:db8:8::1',
+    ip4 = '198.51.100.1',
+  }
+
+
+The cache will be initialized during startup. Each cache entry will use roughly
+90 Bytes of main memory.
diff --git a/package/gluon-core/check_site.lua b/package/gluon-core/check_site.lua
index b6c5c6848..367c1eebd 100644
--- a/package/gluon-core/check_site.lua
+++ b/package/gluon-core/check_site.lua
@@ -39,3 +39,12 @@ for _, config in ipairs({'wifi24', 'wifi5'}) do
 end
 
 need_boolean('poe_passthrough', false)
+if need_table('dns', nil, false) then
+	need_number('dns.cacheentries', false)
+	need_string_array('dns.servers', false)
+end
+
+if need_table('next_node', nil, false) then
+	need_string_match('next_node.ip6', '^[%x:]+$', false)
+	need_string_match('next_node.ip4', '^%d+.%d+.%d+.%d+$', false)
+end
diff --git a/package/gluon-core/luasrc/lib/gluon/upgrade/820-dns-config b/package/gluon-core/luasrc/lib/gluon/upgrade/820-dns-config
new file mode 100755
index 000000000..e95a53a1a
--- /dev/null
+++ b/package/gluon-core/luasrc/lib/gluon/upgrade/820-dns-config
@@ -0,0 +1,40 @@
+#!/usr/bin/lua
+local site = require 'gluon.site_config'
+local uci = require('luci.model.uci').cursor()
+
+dnsmasq=uci:get_first("dhcp", "dnsmasq")
+
+uci:set('dhcp', dnsmasq, 'localise_queries', '1')
+uci:set('dhcp', dnsmasq, 'localservice', '0')
+
+if site.dns and site.dns.servers then
+	uci:set('dhcp', dnsmasq, 'server', site.dns.servers)
+else
+	uci:delete('dhcp', dnsmasq, 'server')
+end
+
+if site.dns and site.dns.cacheentries then
+	uci:set('dhcp', dnsmasq, 'cachesize', site.dns.cacheentries)
+else
+	uci:delete('dhcp', dnsmasq, 'cachesize')
+end
+
+if site.next_node and site.next_node.name and site.next_node.ip4 then
+	uci:section('dhcp','domain','nextnode4',{
+		name=site.next_node.name,
+		ip=site.next_node.ip4,
+	})
+else
+	uci:delete('dhcp', 'domain', 'nextnode4')
+end
+
+if site.next_node and site.next_node.name and site.next_node.ip6 then
+	uci:section('dhcp','domain','nextnode6',{
+		name=site.next_node.name,
+		ip=site.next_node.ip6,
+	})
+else
+	uci:delete('dhcp', 'domain', 'nextnode6')
+end
+uci:save('dhcp')
+
diff --git a/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/330-gluon-mesh-batman-adv-core-local-node b/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/330-gluon-mesh-batman-adv-core-local-node
index ae20d61c6..ea3a29d0f 100755
--- a/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/330-gluon-mesh-batman-adv-core-local-node
+++ b/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/330-gluon-mesh-batman-adv-core-local-node
@@ -29,6 +29,10 @@ uci:section('network', 'interface', 'local_node',
 	}
 )
 
+if site.dns and site.dns.servers then
+	uci:set('network', 'local-node', 'peerdns','0')
+end
+
 uci:delete('network', 'local_node_route6')
 uci:section('network', 'route6', 'local_node_route6',
 	{
-- 
GitLab