From 2df22d5dcb138846defa68d526bd3d2055154e96 Mon Sep 17 00:00:00 2001 From: linuswck Date: Fri, 19 Apr 2024 15:36:27 +0800 Subject: [PATCH] driver: Use inet_aton to check IPV4 address format --- pykirdy/driver/kirdy_async.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pykirdy/driver/kirdy_async.py b/pykirdy/driver/kirdy_async.py index 4065f98..d63e6ff 100644 --- a/pykirdy/driver/kirdy_async.py +++ b/pykirdy/driver/kirdy_async.py @@ -93,15 +93,18 @@ class Device: self._read_response = read_response self._cmd_lock = cmd_lock - async def set_ip_settings(self, addr=[192, 168, 1, 128], port=1337, prefix_len=24, gateway=[192, 168, 1, 1]): + async def set_ip_settings(self, addr="192.168.1.128", port=1337, prefix_len=24, gateway="192.168.1.1"): """ After calling this fn, the ip settings are immediately saved into flash and will be effective on next reboot. """ - if not(isinstance(addr, list) and isinstance(gateway, list)): - raise InvalidDataType - - if not(len(addr) == 4 and len(gateway) == 4): + try: + socket.inet_aton(addr) + socket.inet_aton(gateway) + except OSError: raise InvalidDataType + + addr = addr.split(".") + gateway = gateway.split(".") if not(isinstance(port, int) and isinstance(prefix_len, int)): raise InvalidDataType