mirror of
https://github.com/uhi22/pyPLC.git
synced 2024-11-20 01:13:58 +00:00
fix: clean way to handle ip address, takeover from celeron55 fork
This commit is contained in:
parent
88b4ee978f
commit
4edad2658a
2 changed files with 10 additions and 17 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -5,3 +5,5 @@
|
||||||
PevExiLog*.txt
|
PevExiLog*.txt
|
||||||
*.ini
|
*.ini
|
||||||
/log
|
/log
|
||||||
|
/local
|
||||||
|
*.bak
|
||||||
|
|
|
@ -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:
|
||||||
|
@ -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]))
|
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("Test result: LinkLocalIpv6=" + am.getLinkLocalIpv6Address())
|
||||||
print("same as byte array: " + str(am.getLinkLocalIpv6Address(resulttype="bytearray")))
|
print("same as byte array: " + str(am.getLinkLocalIpv6Address(resulttype="bytearray")))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue