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