From d8f0c531c9b18ea19eab1519d0c9dd55049f9f22 Mon Sep 17 00:00:00 2001
From: Matthias Schiffer <mschiffer@universe-factory.net>
Date: Sat, 9 Aug 2014 14:29:24 +0200
Subject: [PATCH] gluon-wan-dnsmasq: allow configuring static DNS servers

---
 .../files/etc/config/gluon-wan-dnsmasq         |  2 ++
 .../files/etc/init.d/gluon-wan-dnsmasq         |  2 +-
 .../files/lib/gluon/wan-dnsmasq/update.lua     | 18 ++++++++++++++----
 3 files changed, 17 insertions(+), 5 deletions(-)
 create mode 100644 package/gluon-wan-dnsmasq/files/etc/config/gluon-wan-dnsmasq

diff --git a/package/gluon-wan-dnsmasq/files/etc/config/gluon-wan-dnsmasq b/package/gluon-wan-dnsmasq/files/etc/config/gluon-wan-dnsmasq
new file mode 100644
index 000000000..748eecbfd
--- /dev/null
+++ b/package/gluon-wan-dnsmasq/files/etc/config/gluon-wan-dnsmasq
@@ -0,0 +1,2 @@
+config 'static'
+       # list 'server' '192.168.0.1'		# Example
diff --git a/package/gluon-wan-dnsmasq/files/etc/init.d/gluon-wan-dnsmasq b/package/gluon-wan-dnsmasq/files/etc/init.d/gluon-wan-dnsmasq
index eae519276..22bed0392 100755
--- a/package/gluon-wan-dnsmasq/files/etc/init.d/gluon-wan-dnsmasq
+++ b/package/gluon-wan-dnsmasq/files/etc/init.d/gluon-wan-dnsmasq
@@ -16,7 +16,7 @@ RESOLV_CONF=$RESOLV_CONF_DIR/resolv.conf
 
 start() {
 	mkdir -p $RESOLV_CONF_DIR
-	touch $RESOLV_CONF
+	/lib/gluon/wan-dnsmasq/update.lua
 
 	LD_PRELOAD=libpacketmark.so LIBPACKETMARK_MARK=$PACKET_MARK service_start /usr/sbin/dnsmasq -x $SERVICE_PID_FILE -u root -i lo -p $PORT -h -r $RESOLV_CONF
 }
diff --git a/package/gluon-wan-dnsmasq/files/lib/gluon/wan-dnsmasq/update.lua b/package/gluon-wan-dnsmasq/files/lib/gluon/wan-dnsmasq/update.lua
index cf65e8d6c..0f97e0bdc 100755
--- a/package/gluon-wan-dnsmasq/files/lib/gluon/wan-dnsmasq/update.lua
+++ b/package/gluon-wan-dnsmasq/files/lib/gluon/wan-dnsmasq/update.lua
@@ -5,21 +5,31 @@ local RESOLV_CONF = RESOLV_CONF_DIR .. '/resolv.conf'
 
 
 local ubus = require('ubus').connect()
+local uci = require('luci.model.uci').cursor()
 local fs = require 'nixio.fs'
 
 
-local function write_servers(f, iface)
-  local servers = ubus:call('network.interface.' .. iface, 'status', {}).inactive['dns-server']
+local function write_servers(f, servers)
   for _, server in ipairs(servers) do
     f:write('nameserver ', server, '\n')
   end
 end
 
+local function write_interface_servers(f, iface)
+  write_servers(f, ubus:call('network.interface.' .. iface, 'status', {}).inactive['dns-server'])
+end
+
 
 fs.mkdirr(RESOLV_CONF_DIR)
 local f = io.open(RESOLV_CONF, 'w+')
 
-pcall(write_servers, f, 'wan6')
-pcall(write_servers, f, 'wan')
+local static = uci:get_first('gluon-wan-dnsmasq', 'static', 'server')
+
+if type(static) == 'table' and #static > 0 then
+  write_servers(f, static)
+else
+  pcall(write_interface_servers, f, 'wan6')
+  pcall(write_interface_servers, f, 'wan')
+end
 
 f:close()
-- 
GitLab