fix: clean way to handle ip address, takeover from celeron55 fork

This commit is contained in:
uhi22 2023-05-12 07:46:35 +02:00
parent 88b4ee978f
commit 4edad2658a
2 changed files with 10 additions and 17 deletions

2
.gitignore vendored
View file

@ -5,3 +5,5 @@
PevExiLog*.txt PevExiLog*.txt
*.ini *.ini
/log /log
/local
*.bak

View file

@ -7,6 +7,7 @@
import subprocess import subprocess
import os import os
import sys import sys
import ipaddress
from helpers import * # prettyMac etc from helpers import * # prettyMac etc
from configmodule import getConfigValue, getConfigValueBool from configmodule import getConfigValue, getConfigValueBool
@ -242,27 +243,17 @@ class addressManager():
# The caller wants a byte array. We need to convert a string like # 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%3" into a byte array of 16 bytes.
# "fe80::4c46:fea5:b6c9:25a9" # "fe80::4c46:fea5:b6c9:25a9"
ba = bytearray(16)
s = self.localIpv6Address
# print("[addressManager] converting self.localIpv6Address into bytearray") # print("[addressManager] converting self.localIpv6Address into bytearray")
# Step1: remove the % and all behind: # Step1: remove the % and all behind:
x = s.find("%") s = self.localIpv6Address
#print("percent found at " + str(x)) s = s.partition('%')[0]
#print(s) #print(s)
if (x>0):
s=s[0:x]
#print(s)
# Step 2: expand the ommited zeros # Step 2: expand the ommited zeros
x = s.find("::") # Step 3: Fill in leading zeroes for each 16 bit field
#print(":: found at " + str(x)) # Step 4: Remove all ":"
if (x>0): s = ipaddress.IPv6Address(s).exploded.replace(':', '')
# 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(":", "")
#print(s) #print(s)
ba = bytearray(s, 'utf-8')
if (len(s)!=32): if (len(s)!=32):
print("[addressManager] ERROR: invalid length of IPv6 string. Expected be 16 bytes, means 32 hex characters. Found " + str(len(s))) print("[addressManager] ERROR: invalid length of IPv6 string. Expected be 16 bytes, means 32 hex characters. Found " + str(len(s)))
else: else: