From 4edad2658ab2558b720f01f8d508a4ae9bde6bf5 Mon Sep 17 00:00:00 2001 From: uhi22 Date: Fri, 12 May 2023 07:46:35 +0200 Subject: [PATCH] fix: clean way to handle ip address, takeover from celeron55 fork --- .gitignore | 2 ++ addressManager.py | 25 ++++++++----------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 546fe56..ce297e5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ PevExiLog*.txt *.ini /log +/local +*.bak diff --git a/addressManager.py b/addressManager.py index 2cdbe2b..7e43fe0 100644 --- a/addressManager.py +++ b/addressManager.py @@ -7,6 +7,7 @@ import subprocess import os import sys +import ipaddress from helpers import * # prettyMac etc from configmodule import getConfigValue, getConfigValueBool @@ -242,27 +243,17 @@ class addressManager(): # The caller wants a byte array. We need to convert a string like # "fe80::4c46:fea5:b6c9:25a9%3" into a byte array of 16 bytes. # "fe80::4c46:fea5:b6c9:25a9" - ba = bytearray(16) - s = self.localIpv6Address # print("[addressManager] converting self.localIpv6Address into bytearray") # Step1: remove the % and all behind: - x = s.find("%") - #print("percent found at " + str(x)) + s = self.localIpv6Address + s = s.partition('%')[0] #print(s) - if (x>0): - s=s[0:x] - #print(s) # Step 2: expand the ommited zeros - x = s.find("::") - #print(":: found at " + str(x)) - if (x>0): - # a :: means four bytes which are 0x00 each. - # Todo: but we need two bytes more?!? - s = s.replace("::", ":0000:0000:0000:") - #print(s) - # Step 3: Remove all ":" - s = s.replace(":", "") + # Step 3: Fill in leading zeroes for each 16 bit field + # Step 4: Remove all ":" + s = ipaddress.IPv6Address(s).exploded.replace(':', '') #print(s) + ba = bytearray(s, 'utf-8') if (len(s)!=32): print("[addressManager] ERROR: invalid length of IPv6 string. Expected be 16 bytes, means 32 hex characters. Found " + str(len(s))) else: @@ -281,4 +272,4 @@ if __name__ == "__main__": am.setPevIp(bytearray([0xfe, 0x80, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF])) print("Test result: LinkLocalIpv6=" + am.getLinkLocalIpv6Address()) print("same as byte array: " + str(am.getLinkLocalIpv6Address(resulttype="bytearray"))) - \ No newline at end of file +