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