blob: 4f585fed26a5ba1fc28322dc7fba00f9ad97aefd (
plain)
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
|
"""Libknot dname interface wrapper."""
import ctypes
import libknot
class KnotDname(object):
"""Libknot dname."""
CAPACITY = 255
CAPACITY_TXT = 1004
DnameStorage = ctypes.c_char * CAPACITY
DnameTxtStorage = ctypes.c_char * CAPACITY_TXT
SIZE = None
TO_STR = None
FROM_STR = None
data = None
def __init__(self, dname: str = None) -> None:
"""Initializes a dname storage. Optionally initializes from a string."""
if not KnotDname.SIZE:
libknot.Knot()
KnotDname.SIZE = libknot.Knot.LIBKNOT.knot_dname_size
KnotDname.SIZE.restype = ctypes.c_size_t
KnotDname.SIZE.argtypes = [KnotDname.DnameStorage]
KnotDname.TO_STR = libknot.Knot.LIBKNOT.knot_dname_to_str
KnotDname.TO_STR.restype = ctypes.c_char_p
KnotDname.TO_STR.argtypes = [KnotDname.DnameTxtStorage, KnotDname.DnameStorage, ctypes.c_size_t]
KnotDname.FROM_STR = libknot.Knot.LIBKNOT.knot_dname_from_str
KnotDname.FROM_STR.restype = ctypes.c_char_p
KnotDname.FROM_STR.argtypes = [KnotDname.DnameStorage, ctypes.c_char_p, ctypes.c_size_t]
if dname:
self.data = KnotDname.DnameStorage()
if not KnotDname.FROM_STR(self.data, dname.encode('utf-8'), KnotDname.CAPACITY):
raise ValueError
def size(self):
"""Returns size of the stored dname."""
if self.data:
return KnotDname.SIZE(self.data)
else:
return 0
def str(self) -> str:
"""Prints the stored dname in textual format."""
if self.data:
data_txt = KnotDname.DnameTxtStorage()
if not KnotDname.TO_STR(data_txt, self.data, KnotDname.CAPACITY_TXT):
raise ValueError
return data_txt.value.decode("utf-8")
else:
return ""
def wire(self) -> bytes:
"""Returns the dname in wire format."""
if self.data:
return self.data.value + b'\x00'
else:
return bytes()
|