From cb3fb84b8d5a63c525fab6518252b97bc1782c6e Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Sun, 30 Sep 2018 15:53:54 -0400 Subject: [PATCH] Support anding start and end addresses to DHCP config --- router-daemon/pvcrd/DHCPServer.py | 25 +++++++++++------------ router-daemon/pvcrd/VXNetworkInstance.py | 26 ++++++++++++++++++------ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/router-daemon/pvcrd/DHCPServer.py b/router-daemon/pvcrd/DHCPServer.py index 81cd0a52..cb356c33 100644 --- a/router-daemon/pvcrd/DHCPServer.py +++ b/router-daemon/pvcrd/DHCPServer.py @@ -504,7 +504,7 @@ class Transaction(object): class DHCPServerConfiguration(object): - def __init__(self, zk_conn, ipaddr, iface, vni, network, router, dns_servers): + def __init__(self, zk_conn, ipaddr, iface, vni, network, router, dns_servers, start_addr, end_addr): self.dhcp_offer_after_seconds = 1 self.dhcp_acknowledge_after_seconds = 1 self.length_of_transaction = 60 @@ -515,36 +515,35 @@ class DHCPServerConfiguration(object): self.iface = iface self.vni = vni - network_cidr = ipaddress.IPv4Network(network, False) - self.network = str(network_cidr.network_address) - self.broadcast_address = str(network_cidr.broadcast_address) - self.subnet_mask = str(network_cidr.netmask) + self.network_cidr = ipaddress.IPv4Network(network, False) + self.network = str(self.network_cidr.network_address) + self.broadcast_address = str(self.network_cidr.broadcast_address) + self.subnet_mask = str(self.network_cidr.netmask) self.router = router self.domain_name_server = dns_servers + self.start_addr = start_addr + self.end_addr = end_addr + # 1 day is 86400 self.ip_address_lease_time = 300 # seconds - self.host_file = 'hosts.csv' - self.debug = lambda *args, **kw: None def all_ip_addresses(self): - ips = ip_addresses(self.network, self.subnet_mask) - for i in range(5): - next(ips) + ips = ip_addresses(self.network, self.subnet_mask, self.start_addr, self.end_addr) return ips def network_filter(self): return NETWORK(self.network, self.subnet_mask) -def ip_addresses(network, subnet_mask): +def ip_addresses(network, subnet_mask, start_addr, end_addr): subnet_mask = struct.unpack('>I', inet_aton(subnet_mask))[0] network = struct.unpack('>I', inet_aton(network))[0] network = network & subnet_mask - start = network + 1 - end = (network | (~subnet_mask & 0xffffffff)) + start = struct.unpack('>I', inet_aton(start_addr))[0] + end = struct.unpack('>I', inet_aton(end_addr))[0] return (inet_ntoa(struct.pack('>I', i)) for i in range(start, end)) class ALL(object): diff --git a/router-daemon/pvcrd/VXNetworkInstance.py b/router-daemon/pvcrd/VXNetworkInstance.py index 7bc57fda..18a453dd 100644 --- a/router-daemon/pvcrd/VXNetworkInstance.py +++ b/router-daemon/pvcrd/VXNetworkInstance.py @@ -44,6 +44,8 @@ class VXNetworkInstance(): self.ip_network = None self.ip_cidrnetmask = None self.dhcp_flag = None + self.dhcp_start = None + self.dhcp_end = None self.vxlan_nic = 'vxlan{}'.format(self.vni) self.bridge_nic = 'br{}'.format(self.vni) @@ -57,26 +59,26 @@ class VXNetworkInstance(): # Zookeper handlers for changed states @zk_conn.DataWatch('/networks/{}'.format(self.vni)) def watch_network_description(data, stat, event=''): - if data != None and self.description != data.decode('ascii'): + if data and self.description != data.decode('ascii'): self.old_description = self.description self.description = data.decode('ascii') @zk_conn.DataWatch('/networks/{}/domain'.format(self.vni)) def watch_network_domain(data, stat, event=''): - if data != None and self.domain != data.decode('ascii'): + if data and self.domain != data.decode('ascii'): domain = data.decode('ascii') self.domain = domain @zk_conn.DataWatch('/networks/{}/ip_network'.format(self.vni)) def watch_network_ip_network(data, stat, event=''): - if data != None and self.ip_network != data.decode('ascii'): + if data and self.ip_network != data.decode('ascii'): ip_network = data.decode('ascii') self.ip_network = ip_network self.ip_cidrnetmask = ip_network.split('/')[-1] @zk_conn.DataWatch('/networks/{}/ip_gateway'.format(self.vni)) def watch_network_gateway(data, stat, event=''): - if data != None and self.ip_gateway != data.decode('ascii'): + if data and self.ip_gateway != data.decode('ascii'): if self.this_router.network_state == 'primary': if self.ip_gateway: self.removeGatewayAddress() @@ -85,13 +87,23 @@ class VXNetworkInstance(): @zk_conn.DataWatch('/networks/{}/dhcp_flag'.format(self.vni)) def watch_network_dhcp_status(data, stat, event=''): - if data != None and self.dhcp_flag != data.decode('ascii'): + if data and self.dhcp_flag != data.decode('ascii'): self.dhcp_flag = ( data.decode('ascii') == 'True' ) if self.dhcp_flag and self.this_router.network_state == 'primary': self.startDHCPServer() elif self.this_router.network_state == 'primary': self.stopDHCPServer() + @zk_conn.DataWatch('/networks/{}/dhcp_start'.format(self.vni)) + def watch_network_dhcp_start(data, stat, event=''): + if data and self.dhcp_start != data.decode('ascii'): + self.dhcp_start = data.decode('ascii') + + @zk_conn.DataWatch('/networks/{}/dhcp_end'.format(self.vni)) + def watch_network_dhcp_end(data, stat, event=''): + if data and self.dhcp_end != data.decode('ascii'): + self.dhcp_end = data.decode('ascii') + def getvni(self): return self.vni @@ -176,7 +188,9 @@ class VXNetworkInstance(): vni=self.vni, network=self.ip_network, router=[self.ip_gateway], - dns_servers=[self.ip_gateway] + dns_servers=[self.ip_gateway], + start_addr=self.dhcp_start, + end_addr=self.dhcp_end ) dhcp_configuration.debug = print self.dhcp_server = DHCPServer.DHCPServer(dhcp_configuration)