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