import time
from struct import pack

import kibra.database as db
import kitools
from kibra.ktask import Ktask
from kibra.shell import bash

DHCP_CONFIG = '/etc/dibbler/server.conf'
DHCP_DAEMON = 'dibbler-server'


def ntp_server_opt(addr):
    '''Given a server address return the HEX formatted payload of DHCPv6 toption
       56 as RFC5908'''
    b_pload = pack('>H', 1)
    b_pload += pack('>H', 16)
    b_pload += kitools.kicmds.s2b(kitools.kicmds.TYP.ADDR, addr)
    return ':'.join([hex(byte).replace('0x', '').zfill(2) for byte in b_pload])


def dhcp_server_start():
    # Don't start if DHCP is not to be used
    if not db.get('prefix_dhcp'):
        return

    # Stop DHCP daemon
    bash(DHCP_DAEMON + ' stop')
    # Remove previous configuration for this NCP
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'), '\n}\n')
    # Add new configuration
    with open(DHCP_CONFIG, 'w') as file_:
        file_.write('\n')
        file_.write('iface ' + db.get('interior_ifname') + ' {\n')
        file_.write('\tclient-max-lease 1\n')
        file_.write('\tunicast ' + db.get('ncp_rloc') + '\n')
        file_.write('\trapid-commit yes\n')
        file_.write('\toption ntp-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\toption dns-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\tpreference 255\n')
        file_.write('\tclass {\n')
        file_.write('\t\tT1 0\n')
        file_.write('\t\tT2 0\n')
        file_.write('\t\tpreferred-lifetime 86400\n')
        file_.write('\t\tvalid-lifetime 86400\n')
        file_.write('\t\tpool ' + db.get('prefix') + '\n')
        file_.write('\t}\n')
        file_.write('}\n')
    # Allow for the file to be stored
    time.sleep(0.2)
    # Start DHCP daemon
    bash(DHCP_DAEMON + ' start')


def dhcp_server_stop():
    # Don't stop if DHCP is not to be used
    if not db.get('prefix_dhcp'):
        return
        
    # Stop DHCP daemon
    bash(DHCP_DAEMON + ' stop')
    # Remove previous configuration for this NCP
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'), '\n}\n')
    # Allow for the file to be stored
    time.sleep(0.2)

    # Start DHCP daemon
    bash(DHCP_DAEMON + ' start')


class DHCP(Ktask):
    def __init__(self):
        Ktask.__init__(
            self,
            name='dhcp',
            start_keys=['prefix', 'interior_ifname', 'ncp_rloc', 'interior_mac'],
            stop_keys=['interior_ifname'],
            start_tasks=['network', 'serial'],
            period=2,
        )

    def kstart(self):
        dhcp_server_start()

    def kstop(self):
        dhcp_server_stop()

75d345d1f6d573b08655a3383c411063e2f12cf5
<mask token>


class DHCP(Ktask):

    def __init__(self):
        Ktask.__init__(self, name='dhcp', start_keys=['prefix',
            'interior_ifname', 'ncp_rloc', 'interior_mac'], stop_keys=[
            'interior_ifname'], start_tasks=['network', 'serial'], period=2)

    def kstart(self):
        dhcp_server_start()

    def kstop(self):
        dhcp_server_stop()

<mask token>


def ntp_server_opt(addr):
    """Given a server address return the HEX formatted payload of DHCPv6 toption
       56 as RFC5908"""
    b_pload = pack('>H', 1)
    b_pload += pack('>H', 16)
    b_pload += kitools.kicmds.s2b(kitools.kicmds.TYP.ADDR, addr)
    return ':'.join([hex(byte).replace('0x', '').zfill(2) for byte in b_pload])


<mask token>


def dhcp_server_stop():
    if not db.get('prefix_dhcp'):
        return
    bash(DHCP_DAEMON + ' stop')
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'),
        '\n}\n')
    time.sleep(0.2)
    bash(DHCP_DAEMON + ' start')


class DHCP(Ktask):

    def __init__(self):
        Ktask.__init__(self, name='dhcp', start_keys=['prefix',
            'interior_ifname', 'ncp_rloc', 'interior_mac'], stop_keys=[
            'interior_ifname'], start_tasks=['network', 'serial'], period=2)

    def kstart(self):
        dhcp_server_start()

    def kstop(self):
        dhcp_server_stop()

<mask token>


def ntp_server_opt(addr):
    """Given a server address return the HEX formatted payload of DHCPv6 toption
       56 as RFC5908"""
    b_pload = pack('>H', 1)
    b_pload += pack('>H', 16)
    b_pload += kitools.kicmds.s2b(kitools.kicmds.TYP.ADDR, addr)
    return ':'.join([hex(byte).replace('0x', '').zfill(2) for byte in b_pload])


def dhcp_server_start():
    if not db.get('prefix_dhcp'):
        return
    bash(DHCP_DAEMON + ' stop')
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'),
        '\n}\n')
    with open(DHCP_CONFIG, 'w') as file_:
        file_.write('\n')
        file_.write('iface ' + db.get('interior_ifname') + ' {\n')
        file_.write('\tclient-max-lease 1\n')
        file_.write('\tunicast ' + db.get('ncp_rloc') + '\n')
        file_.write('\trapid-commit yes\n')
        file_.write('\toption ntp-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\toption dns-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\tpreference 255\n')
        file_.write('\tclass {\n')
        file_.write('\t\tT1 0\n')
        file_.write('\t\tT2 0\n')
        file_.write('\t\tpreferred-lifetime 86400\n')
        file_.write('\t\tvalid-lifetime 86400\n')
        file_.write('\t\tpool ' + db.get('prefix') + '\n')
        file_.write('\t}\n')
        file_.write('}\n')
    time.sleep(0.2)
    bash(DHCP_DAEMON + ' start')


def dhcp_server_stop():
    if not db.get('prefix_dhcp'):
        return
    bash(DHCP_DAEMON + ' stop')
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'),
        '\n}\n')
    time.sleep(0.2)
    bash(DHCP_DAEMON + ' start')


class DHCP(Ktask):

    def __init__(self):
        Ktask.__init__(self, name='dhcp', start_keys=['prefix',
            'interior_ifname', 'ncp_rloc', 'interior_mac'], stop_keys=[
            'interior_ifname'], start_tasks=['network', 'serial'], period=2)

    def kstart(self):
        dhcp_server_start()

    def kstop(self):
        dhcp_server_stop()

import time
from struct import pack
import kibra.database as db
import kitools
from kibra.ktask import Ktask
from kibra.shell import bash
DHCP_CONFIG = '/etc/dibbler/server.conf'
DHCP_DAEMON = 'dibbler-server'


def ntp_server_opt(addr):
    """Given a server address return the HEX formatted payload of DHCPv6 toption
       56 as RFC5908"""
    b_pload = pack('>H', 1)
    b_pload += pack('>H', 16)
    b_pload += kitools.kicmds.s2b(kitools.kicmds.TYP.ADDR, addr)
    return ':'.join([hex(byte).replace('0x', '').zfill(2) for byte in b_pload])


def dhcp_server_start():
    if not db.get('prefix_dhcp'):
        return
    bash(DHCP_DAEMON + ' stop')
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'),
        '\n}\n')
    with open(DHCP_CONFIG, 'w') as file_:
        file_.write('\n')
        file_.write('iface ' + db.get('interior_ifname') + ' {\n')
        file_.write('\tclient-max-lease 1\n')
        file_.write('\tunicast ' + db.get('ncp_rloc') + '\n')
        file_.write('\trapid-commit yes\n')
        file_.write('\toption ntp-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\toption dns-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\tpreference 255\n')
        file_.write('\tclass {\n')
        file_.write('\t\tT1 0\n')
        file_.write('\t\tT2 0\n')
        file_.write('\t\tpreferred-lifetime 86400\n')
        file_.write('\t\tvalid-lifetime 86400\n')
        file_.write('\t\tpool ' + db.get('prefix') + '\n')
        file_.write('\t}\n')
        file_.write('}\n')
    time.sleep(0.2)
    bash(DHCP_DAEMON + ' start')


def dhcp_server_stop():
    if not db.get('prefix_dhcp'):
        return
    bash(DHCP_DAEMON + ' stop')
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'),
        '\n}\n')
    time.sleep(0.2)
    bash(DHCP_DAEMON + ' start')


class DHCP(Ktask):

    def __init__(self):
        Ktask.__init__(self, name='dhcp', start_keys=['prefix',
            'interior_ifname', 'ncp_rloc', 'interior_mac'], stop_keys=[
            'interior_ifname'], start_tasks=['network', 'serial'], period=2)

    def kstart(self):
        dhcp_server_start()

    def kstop(self):
        dhcp_server_stop()

import time
from struct import pack

import kibra.database as db
import kitools
from kibra.ktask import Ktask
from kibra.shell import bash

DHCP_CONFIG = '/etc/dibbler/server.conf'
DHCP_DAEMON = 'dibbler-server'


def ntp_server_opt(addr):
    '''Given a server address return the HEX formatted payload of DHCPv6 toption
       56 as RFC5908'''
    b_pload = pack('>H', 1)
    b_pload += pack('>H', 16)
    b_pload += kitools.kicmds.s2b(kitools.kicmds.TYP.ADDR, addr)
    return ':'.join([hex(byte).replace('0x', '').zfill(2) for byte in b_pload])


def dhcp_server_start():
    # Don't start if DHCP is not to be used
    if not db.get('prefix_dhcp'):
        return

    # Stop DHCP daemon
    bash(DHCP_DAEMON + ' stop')
    # Remove previous configuration for this NCP
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'), '\n}\n')
    # Add new configuration
    with open(DHCP_CONFIG, 'w') as file_:
        file_.write('\n')
        file_.write('iface ' + db.get('interior_ifname') + ' {\n')
        file_.write('\tclient-max-lease 1\n')
        file_.write('\tunicast ' + db.get('ncp_rloc') + '\n')
        file_.write('\trapid-commit yes\n')
        file_.write('\toption ntp-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\toption dns-server ' + db.get('ncp_mleid') + '\n')
        file_.write('\tpreference 255\n')
        file_.write('\tclass {\n')
        file_.write('\t\tT1 0\n')
        file_.write('\t\tT2 0\n')
        file_.write('\t\tpreferred-lifetime 86400\n')
        file_.write('\t\tvalid-lifetime 86400\n')
        file_.write('\t\tpool ' + db.get('prefix') + '\n')
        file_.write('\t}\n')
        file_.write('}\n')
    # Allow for the file to be stored
    time.sleep(0.2)
    # Start DHCP daemon
    bash(DHCP_DAEMON + ' start')


def dhcp_server_stop():
    # Don't stop if DHCP is not to be used
    if not db.get('prefix_dhcp'):
        return
        
    # Stop DHCP daemon
    bash(DHCP_DAEMON + ' stop')
    # Remove previous configuration for this NCP
    db.del_from_file(DHCP_CONFIG, '\niface %s' % db.get('interior_ifname'), '\n}\n')
    # Allow for the file to be stored
    time.sleep(0.2)

    # Start DHCP daemon
    bash(DHCP_DAEMON + ' start')


class DHCP(Ktask):
    def __init__(self):
        Ktask.__init__(
            self,
            name='dhcp',
            start_keys=['prefix', 'interior_ifname', 'ncp_rloc', 'interior_mac'],
            stop_keys=['interior_ifname'],
            start_tasks=['network', 'serial'],
            period=2,
        )

    def kstart(self):
        dhcp_server_start()

    def kstop(self):
        dhcp_server_stop()
