Skip to content

Commit

Permalink
Merge pull request #41 from do3cc/add_dhcp_features
Browse files Browse the repository at this point in the history
Add dhcp features
  • Loading branch information
ties authored Apr 19, 2024
2 parents 58af1db + c5dfc63 commit d6bd4f6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ wifi.update_wifi_settings(new_settings)

print(wifi.wifi_settings)

# And/or DHCPSettings
# And/or Make all dhcp adresses static:

dhcp = DHCPSettings(modem)
lan_table = LanTable(modem)
for client in (*lan_table.get_lan(), *lan_table.get_wifi()):
dhcp.add_static_lease(
lease_ip=client["IPv4Addr"].split("/")[0], lease_mac=client["MACAddr"]
)


# If you want to go back to 'normal':
# modem.reboot() # or
Expand Down
43 changes: 35 additions & 8 deletions compal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Client for the Compal CH7465LG/Ziggo Connect box cable modem
"""

import inspect
import io
import itertools
Expand All @@ -11,8 +12,8 @@
from collections import OrderedDict
from datetime import timedelta
from enum import Enum
from xml.dom import minidom
from hashlib import sha256
from xml.dom import minidom

import requests
from lxml import etree
Expand All @@ -25,9 +26,9 @@
GuestNetworkEnabling,
GuestNetworkProperties,
GuestNetworkSettings,
NatMode,
IPv6FilterRule,
IPv6FilterRuleProto,
NatMode,
PortForward,
Proto,
RadioSettings,
Expand Down Expand Up @@ -681,19 +682,23 @@ def condGetOrDefault(direction, val, defaultValueIn, defaultValueOut):
int(
FilterIpRange.all
if src_addr == "::"
else FilterIpRange.range
if src_prefix != 128
else FilterIpRange.single
else (
FilterIpRange.range
if src_prefix != 128
else FilterIpRange.single
)
),
),
(
"dsIpRange",
int(
FilterIpRange.all
if dst_addr == "::"
else FilterIpRange.range
if dst_prefix != 128
else FilterIpRange.single
else (
FilterIpRange.range
if dst_prefix != 128
else FilterIpRange.single
)
),
),
("PortRange", "2"), # manual port selection
Expand Down Expand Up @@ -1283,6 +1288,7 @@ class DHCPSettings:

def __init__(self, modem):
self.modem = modem
self.parser = etree.XMLParser(recover=True)

def add_static_lease(self, lease_ip, lease_mac):
"""
Expand All @@ -1293,6 +1299,27 @@ def add_static_lease(self, lease_ip, lease_mac):
{"data": "ADD,{ip},{mac};".format(ip=lease_ip, mac=lease_mac)},
)

def del_static_lease(self, lease_ip, lease_mac):
"""
Delete a static DHCP lease
"""
return self.modem.xml_setter(
SetFunction.STATIC_DHCP_LEASE,
{"data": "DEL,{ip},{mac};".format(ip=lease_ip, mac=lease_mac)},
)

def get_static_leases(self):
"""
Get all static leases
"""
xml_content = self.modem.xml_getter(GetFunction.BASICDHCP, {}).content
tree = etree.fromstring(xml_content, parser=self.parser)
for host in tree.findall("ReserveIpadrr"):
yield {
"lease_ip": host.find("LeasedIP").text,
"lease_mac": host.find("MacAddress").text,
}

def set_upnp_status(self, enabled):
"""
Ensure that UPnP is set to the given value
Expand Down
3 changes: 2 additions & 1 deletion compal/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Objects used by CH7465LG"""

from dataclasses import dataclass
from enum import IntEnum
from typing import Optional, List
from typing import List, Optional


@dataclass
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Set up script """

import os

from setuptools import find_packages, setup
Expand Down

0 comments on commit d6bd4f6

Please sign in to comment.