-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidlist.py
executable file
·77 lines (59 loc) · 1.44 KB
/
sidlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
sidlist
implements a list of IPv6 addresses
my_sidlist = Sidlist()
my_sidlist.add('ff00::1')
my_sidlist.add('ff00::2')
print(my_sidlist.len())
print(my_sidlist.get())
print(my_sidlist.to_hex())
my_sidlist.set(['cafe::1','cafe::2','cafe::2'])
"""
import math
import ipaddress
from hex_types import to_hex
class Sidlist:
def __init__(self):
self.list = []
def __call__(self):
return self.get()
def set(self,data):
"""
take a list as input
each element of the list can be an ipaddress.IPv6Address
or a string representation of an IPv6 address
"""
if type(data) != type([]):
raise TypeError("List is needed")
else:
self.list = []
for i in data:
self.add(i)
def add(self, ipv6_addr):
"""
add an IPv6 address to a Sidlist
the IPv6 address can be an ipaddress.IPv6Address
or a string representation of an IPv6 address
"""
if type(ipv6_addr) == type(ipaddress.IPv6Address('::1')):
self.list.append(ipv6_addr)
else:
self.list.append(ipaddress.IPv6Address(ipv6_addr))
def get(self):
"""
return the Sidlist as a list of ipaddress.IPv6Address
"""
return self.list
def len(self):
"""
return the length of the Sidlist
"""
return len(self.list)
def to_hex(self):
"""
return the Sidlist as an array of hex in network order
"""
tmp = []
for i in self.list:
tmp.extend(to_hex(i))
return tmp