+# add dns entries if they are not in resolv.conf yet
+add_dns() {
+ local cfg="$1"; shift
+
+ remove_dns "$cfg"
+
+ # We may be called by pppd's ip-up which has a nonstandard umask set.
+ # Create an empty file here and force its permission to 0644, otherwise
+ # dnsmasq will not be able to re-read the resolv.conf.auto .
+ [ ! -f /tmp/resolv.conf.auto ] && {
+ touch /tmp/resolv.conf.auto
+ chmod 0644 /tmp/resolv.conf.auto
+ }
+
+ local dns
+ local add
+ for dns in "$@"; do
+ grep -qsE "^nameserver ${dns//./\\.}$" /tmp/resolv.conf.auto || {
+ add="${add:+$add }$dns"
+ echo "nameserver $dns" >> /tmp/resolv.conf.auto
+ }
+ done
+
+ [ -n "$cfg" ] && {
+ uci_toggle_state network "$cfg" dns "$add"
+ uci_toggle_state network "$cfg" resolv_dns "$add"
+ }
+}
+
+# remove dns entries of the given iface
+remove_dns() {
+ local cfg="$1"
+
+ [ -n "$cfg" ] && {
+ [ -f /tmp/resolv.conf.auto ] && {
+ local dns=$(uci_get_state network "$cfg" resolv_dns)
+ for dns in $dns; do
+ sed -i -e "/^nameserver ${dns//./\\.}$/d" /tmp/resolv.conf.auto
+ done
+ }
+
+ uci_revert_state network "$cfg" dns
+ uci_revert_state network "$cfg" resolv_dns
+ }
+}
+