Mdns » Historial » Versió 4
Pau Escrich, 24-10-2013 15:23
| 1 | 3 | Pau Escrich | h1. Mdns: mesh DNS system |
|---|---|---|---|
| 2 | 1 | Pau Escrich | |
| 3 | This is an example of bmx6 SMS plugin utilization. It is a distributed DNS system where every node publish the domains it is managing. |
||
| 4 | The current implementation is done for working in OpenWRT with the Dnsmasq daemon. |
||
| 5 | |||
| 6 | 4 | Pau Escrich | h3. Usage |
| 7 | 1 | Pau Escrich | |
| 8 | * /etc/init.d/mdns start |
||
| 9 | * /etc/init.d/mdns start |
||
| 10 | * /etc/init.d/mdns reload |
||
| 11 | |||
| 12 | 4 | Pau Escrich | h3. Files |
| 13 | 1 | Pau Escrich | |
| 14 | * /etc/mdns/public contains the list of domains we wanto to announce in the network |
||
| 15 | * /etc/mdns/hosts contains a list of "IP -> domain" which have been obtained from the network (dnsmasq looks on it) |
||
| 16 | 3 | Pau Escrich | |
| 17 | 4 | Pau Escrich | h3. Executation |
| 18 | 3 | Pau Escrich | |
| 19 | By default, even if there are not domains to publish in /etc/mdns/public file, the mdns script publish its own hostname.[ALL_ACCEPTED_TLDs] |
||
| 20 | |||
| 21 | The accepted TLDs can be configured in the variables DOMAINS4 and DOMAINS6, for IPv4 and IPv6 respectivelly. |
||
| 22 | So if someone publish "google.com" but your mdns instance is not accepting the ".com" TLD it will be discarted. |
||
| 23 | |||
| 24 | <pre> |
||
| 25 | root@UPClab104-1677:~# /etc/init.d/mdns start |
||
| 26 | -> preparing bmx6 |
||
| 27 | INFO uci_create_section: bmx6.cfg125cb5=syncSms |
||
| 28 | INFO uci_save_option: bmx6.cfg125cb5.syncSms=mdns |
||
| 29 | INFO uci_save_option: bmx6.cfg125cb5.syncSms=mdns |
||
| 30 | INFO : --syncSms mdns |
||
| 31 | -> adding addn-host entry to dnsmasq config file |
||
| 32 | -> restarting dnsmasq daemon |
||
| 33 | -> cleaning files |
||
| 34 | -> publishing own IPv4 domains: UPClab104-1677.qmp |
||
| 35 | -> publishing own IPv6 domains: UPClab104-1677.qm6 |
||
| 36 | -> added remote domains google.qmp pau.qmp tita.qmp SApalafolls-ebb2.qmp = 10.228.206.65 |
||
| 37 | -> added remote domains SApalafolls-ebb2.qm6 = 2012:0:0:ebb2::1 |
||
| 38 | </pre> |
||
| 39 | 1 | Pau Escrich | |
| 40 | 3 | Pau Escrich | |
| 41 | 4 | Pau Escrich | h3. Code |
| 42 | 2 | Pau Escrich | |
| 43 | 4 | Pau Escrich | <pre> |
| 44 | 1 | Pau Escrich | #!/bin/sh |
| 45 | # Copyright (C) 2013 Pau Escrich <p4u@dabax.net> |
||
| 46 | # |
||
| 47 | # This program is free software; you can redistribute it and/or modify |
||
| 48 | # it under the terms of the GNU General Public License as published by |
||
| 49 | # the Free Software Foundation; either version 2 of the License, or |
||
| 50 | # (at your option) any later version. |
||
| 51 | # |
||
| 52 | # This program is distributed in the hope that it will be useful, |
||
| 53 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 54 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 55 | # GNU General Public License for more details. |
||
| 56 | # |
||
| 57 | # You should have received a copy of the GNU General Public License along |
||
| 58 | # with this program; if not, write to the Free Software Foundation, Inc., |
||
| 59 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 60 | # |
||
| 61 | # The full GNU General Public License is included in this distribution in |
||
| 62 | # the file called "COPYING". |
||
| 63 | |||
| 64 | # Configuration variables |
||
| 65 | MDNS_DIR="/etc/mdns" |
||
| 66 | HOSTF="$MDNS_DIR/hosts" |
||
| 67 | MY_DOMAINS="$MDNS_DIR/public" |
||
| 68 | DOMAINS4="qmp" |
||
| 69 | DOMAINS6="qm6" |
||
| 70 | SMSF="/var/run/bmx6/sms/sendSms/mdns" |
||
| 71 | SMSR="/var/run/bmx6/sms/rcvdSms" |
||
| 72 | DNSMASQ="/etc/dnsmasq.conf" |
||
| 73 | DNSMASQ_PID="/var/run/dnsmasq.pid" |
||
| 74 | |||
| 75 | # Optional |
||
| 76 | IPV4="" |
||
| 77 | IPV6="" |
||
| 78 | |||
| 79 | start() { |
||
| 80 | if ! cat /etc/crontabs/root | grep mdns > /dev/null; then |
||
| 81 | echo "/10 * * * * /etc/init.d/mdns reload > /tmp/log/mdns.log" >> /etc/crontabs/root |
||
| 82 | /etc/init.d/cron enable |
||
| 83 | /etc/init.d/cron start |
||
| 84 | fi |
||
| 85 | /etc/init.d/mdns reload |
||
| 86 | } |
||
| 87 | |||
| 88 | stop() { |
||
| 89 | sed -i -e '/.*\/etc\/init\.d\/mdns reload > \/tmp\/log\/mdns\.log$/d' /etc/crontabs/root |
||
| 90 | /etc/init.d/cron restart |
||
| 91 | } |
||
| 92 | |||
| 93 | reload() { |
||
| 94 | init |
||
| 95 | publish_my_domains |
||
| 96 | get_domains |
||
| 97 | finish |
||
| 98 | } |
||
| 99 | |||
| 100 | refresh() { |
||
| 101 | reload |
||
| 102 | } |
||
| 103 | |||
| 104 | # Code |
||
| 105 | |||
| 106 | error() { |
||
| 107 | echo "ERROR: $@" |
||
| 108 | exit 1 |
||
| 109 | } |
||
| 110 | |||
| 111 | log() { |
||
| 112 | echo -e "-> $@" |
||
| 113 | } |
||
| 114 | |||
| 115 | init() { |
||
| 116 | log "preparing bmx6" |
||
| 117 | bmx6 -c --syncSms mdns |
||
| 118 | [ $? -ne 0 ] && error "cannot configure bmx6 daemon" |
||
| 119 | |||
| 120 | cat $DNSMASQ | egrep "^addn-hosts=$HOSTF" -q || { |
||
| 121 | log "adding addn-host entry to dnsmasq config file" |
||
| 122 | echo "addn-hosts=$HOSTF" >> $DNSMASQ |
||
| 123 | log "restarting dnsmasq daemon" |
||
| 124 | /etc/init.d/dnsmasq restart |
||
| 125 | } |
||
| 126 | ps | grep dnsmasq -q || error "dnsmasq not running" |
||
| 127 | |||
| 128 | log "cleaning files" |
||
| 129 | [ -f $SMSF ] && rm -f $SMSF |
||
| 130 | touch $SMSF |
||
| 131 | |||
| 132 | [ -f $HOSTF ] && rm -f $HOSTF |
||
| 133 | touch $HOSTF |
||
| 134 | |||
| 135 | [ ! -d $MDNS_DIR ] && mkdir -p $MDNS_DIR |
||
| 136 | touch $MY_DOMAINS |
||
| 137 | } |
||
| 138 | |||
| 139 | check_domain() { |
||
| 140 | local v="$1" |
||
| 141 | local d="$2" |
||
| 142 | local valid=1 |
||
| 143 | local domains="" |
||
| 144 | |||
| 145 | [ $v == "4" ] && domains="$DOMAINS4" |
||
| 146 | [ $v == "6" ] && domains="$DOMAINS6" |
||
| 147 | [ $v == "all" ] && domains="$DOMAINS4 $DOMAINS6" |
||
| 148 | |||
| 149 | for D in $domains; do |
||
| 150 | echo "$d" | egrep "\.$D"$ -q && valid=0 && break |
||
| 151 | done |
||
| 152 | |||
| 153 | return $valid |
||
| 154 | } |
||
| 155 | |||
| 156 | get_my_domains() { |
||
| 157 | local v=${1:-4} |
||
| 158 | local my_domains="" |
||
| 159 | local domains="" |
||
| 160 | local d |
||
| 161 | |||
| 162 | for d in $(cat $MY_DOMAINS); do |
||
| 163 | check_domain $v $d && my_domains="$d $my_domains" |
||
| 164 | done |
||
| 165 | |||
| 166 | [ "$v" == "4" ] && domains="$DOMAINS4" |
||
| 167 | [ "$v" == "6" ] && domains="$DOMAINS6" |
||
| 168 | |||
| 169 | for d in $domains; do |
||
| 170 | my_domains="$(cat /proc/sys/kernel/hostname).$d $my_domains" |
||
| 171 | done |
||
| 172 | |||
| 173 | echo $my_domains |
||
| 174 | } |
||
| 175 | |||
| 176 | get_my_ip4() { |
||
| 177 | [ -z $IPV4 ] && \ |
||
| 178 | bmx6 -cp | grep tun4Address | awk '{print $2}' | awk -F / '{print $1}' || \ |
||
| 179 | echo "$IPV4" |
||
| 180 | } |
||
| 181 | |||
| 182 | get_my_ip6() { |
||
| 183 | [ -z $IPV4 ] && \ |
||
| 184 | bmx6 -cp | grep tun6Address | awk '{print $2}' | awk -F / '{print $1}' || \ |
||
| 185 | echo "$IPV6" |
||
| 186 | } |
||
| 187 | |||
| 188 | publish_my_domains() { |
||
| 189 | local d domains |
||
| 190 | |||
| 191 | local ip4="$(get_my_ip4)" |
||
| 192 | local ip6="$(get_my_ip6)" |
||
| 193 | |||
| 194 | [ -z "$ip4$ip6" ] && error "cannot get ip address" |
||
| 195 | |||
| 196 | [ -n "$ip4" ] && { |
||
| 197 | domains="$(get_my_domains 4)" |
||
| 198 | log "publishing own IPv4 domains: $domains" |
||
| 199 | echo "$ip4 $domains" >> $SMSF |
||
| 200 | echo "$ip4 $domains" >> $HOSTF |
||
| 201 | } |
||
| 202 | |||
| 203 | [ -n "$ip6" ] && { |
||
| 204 | domains="$(get_my_domains 6)" |
||
| 205 | log "publishing own IPv6 domains: $domains" |
||
| 206 | echo "$ip6 $domains" >> $SMSF |
||
| 207 | echo "$ip6 $domains" >> $HOSTF |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | get_domains() { |
||
| 212 | local f d domline ip line n i |
||
| 213 | |||
| 214 | ls $SMSR/*:mdns 2>/dev/null >/dev/null && { |
||
| 215 | |||
| 216 | # for each received file |
||
| 217 | for f in $SMSR/*:mdns; do |
||
| 218 | n=$(cat $f | wc -l) |
||
| 219 | |||
| 220 | # for each line inside the file |
||
| 221 | for i in $(seq $n); do |
||
| 222 | line=$(cat $f | awk "NR==$i") |
||
| 223 | |||
| 224 | ip="$(echo $line | awk '{print $1}')" |
||
| 225 | [ -z $ip ] && continue |
||
| 226 | |||
| 227 | domains="" |
||
| 228 | # for each domain (first is IP) |
||
| 229 | for d in $(echo "$line" | awk '{$1=""; print $0}'); do |
||
| 230 | check_domain all $d && domains="$d $domains" |
||
| 231 | done |
||
| 232 | |||
| 233 | [ -n "$domains" ] && echo "$ip $domains" >> $HOSTF |
||
| 234 | log "added remote domains $domains= $ip" |
||
| 235 | done |
||
| 236 | done |
||
| 237 | |||
| 238 | } || log "not published domains found in the network" |
||
| 239 | } |
||
| 240 | |||
| 241 | finish() { |
||
| 242 | kill -SIGHUP $(cat $DNSMASQ_PID) |
||
| 243 | } |
||
| 244 | |||
| 245 | $@ |
||
| 246 | |||
| 247 | 2 | Pau Escrich | </pre> |