#!/usr/bin/env python3
#
#  Copyright (c) 2021, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#
import ctypes
import ctypes.util
import json
import os
import socket
import struct
import logging

PORT = 12345
IFNAME = 'eth0'
GROUP = 'ff02::114'

libc = ctypes.CDLL(ctypes.util.find_library('c'))

logging.basicConfig(level=logging.INFO)


def if_nametoindex(name):
    if not isinstance(name, str):
        raise TypeError('name must be a string.')
    ret = libc.if_nametoindex(name.encode('ascii'))
    if not ret:
        raise RuntimeError("Invalid Name")
    return ret


def get_ipaddr():
    for line in os.popen(f'ip addr list dev {IFNAME} | grep inet6 | grep \'scope link\' '):
        addr = line.strip().split()[1]
        return addr.split('/')[0]


def advertise_bbr(s: socket.socket, src):
    bbr_info = {
        'ven': 'OpenThread_BR',
        'mod': 'OpenThread_BR',
        'ver': '1.0',
        'add': get_ipaddr(),
        'por': 22,
    }

    logging.info("Advertise: %r", bbr_info)
    s.sendto(json.dumps(bbr_info).encode('utf8'), src)


def main():
    # Look up multicast group address in name server and find out IP version
    addrinfo = socket.getaddrinfo(GROUP, None)[0]
    assert addrinfo[0] == socket.AF_INET6

    # Create a socket
    s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, (IFNAME + '\0').encode('ascii'))

    # Bind it to the port
    s.bind(('', PORT))

    group_bin = socket.inet_pton(addrinfo[0], addrinfo[4][0])
    # Join group
    interface_index = if_nametoindex(IFNAME)
    mreq = group_bin + struct.pack('@I', interface_index)
    s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)

    logging.info("Advertising BBR on interface %s group %s ...", IFNAME, GROUP)

    # Loop, printing any data we receive
    while True:
        data, src = s.recvfrom(100)
        if data == b'BBR':
            logging.info('Received BBR query, advertising')
            advertise_bbr(s, src)
        else:
            logging.warn('Received %r, but ignored', data)


if __name__ == '__main__':
    main()
