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