1#!/usr/bin/env python3
2#
3#  Copyright (c) 2022, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28
29import ipaddress
30import sys
31import time
32from zeroconf import IPVersion, ServiceBrowser, ServiceStateChange, Zeroconf, DNSAddress, DNSService, DNSText
33
34
35def on_service_state_change(zeroconf, service_type, name, state_change):
36    if state_change is ServiceStateChange.Added:
37        zeroconf.get_service_info(service_type, name)
38
39
40class BorderAgent(object):
41    alias = None
42    server_name = None
43    addr = None
44    port = None
45    thread_status = None
46
47    def __init__(self, alias):
48        self.alias = alias
49
50    def __repr__(self):
51        return str([self.alias, self.addr, self.port, self.thread_status])
52
53
54def get_ipaddr_priority(addr: ipaddress.IPv6Address):
55    # calculate the priority of IPv6 addresses in order: Global > non Global > Link local
56    if addr.is_link_local:
57        return 0
58
59    if not addr.is_global:
60        return 1
61
62    return 2
63
64
65def parse_cache(cache):
66    border_agents = []
67
68    # Find all border routers
69    for ptr in cache.get('_meshcop._udp.local.', []):
70        border_agents.append(BorderAgent(ptr.alias))
71
72    # Find server name, port and Thread Interface status for each border router
73    for ba in border_agents:
74        for record in cache.get(ba.alias.lower(), []):
75            if isinstance(record, DNSService):
76                ba.server_name = record.server
77                ba.port = record.port
78            elif isinstance(record, DNSText):
79                text = bytearray(record.text)
80                sb = text.split(b'sb=')[1][0:4]
81                ba.thread_status = (sb[3] & 0x18) >> 3
82
83    # Find IPv6 address for each border router
84    for ba in border_agents:
85        for record in cache.get(ba.server_name.lower(), []):
86            if isinstance(record, DNSAddress):
87                addr = ipaddress.ip_address(record.address)
88                if not isinstance(addr, ipaddress.IPv6Address) or addr.is_multicast or addr.is_loopback:
89                    continue
90
91                if not ba.addr or get_ipaddr_priority(addr) > get_ipaddr_priority(ipaddress.IPv6Address(ba.addr)):
92                    ba.addr = str(addr)
93
94    return border_agents
95
96
97def main():
98    # Browse border agents
99    zeroconf = Zeroconf(ip_version=IPVersion.V6Only)
100    ServiceBrowser(zeroconf, "_meshcop._udp.local.", handlers=[on_service_state_change])
101    time.sleep(2)
102    cache = zeroconf.cache.cache
103    zeroconf.close()
104
105    border_agents = parse_cache(cache)
106    for ba in border_agents:
107        print(ba)
108
109
110if __name__ == '__main__':
111    main()
112