summaryrefslogtreecommitdiffstats
path: root/python/libknot/libknot/control.py
blob: 659aba8aa36dcd2696f328953b2e319207ad86dd (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""Libknot server control interface wrapper."""

import ctypes
import enum
import warnings
import libknot


def load_lib(path: str = None) -> None:
    """Compatibility wrapper."""

    libknot.Knot(path)
    warnings.warn("libknot.control.load_lib() is deprecated, use libknot.Knot() instead", \
                  category=Warning, stacklevel=2)


class KnotCtlType(enum.IntEnum):
    """Libknot server control data unit types."""

    END = 0
    DATA = 1
    EXTRA = 2
    BLOCK = 3


class KnotCtlDataIdx(enum.IntEnum):
    """Libknot server control data unit indices."""

    COMMAND = 0
    FLAGS = 1
    ERROR = 2
    SECTION = 3
    ITEM = 4
    ID = 5
    ZONE = 6
    OWNER = 7
    TTL = 8
    TYPE = 9
    DATA = 10
    FILTER = 11


class KnotCtlData(object):
    """Libknot server control data unit."""

    DataArray = ctypes.c_char_p * len(KnotCtlDataIdx)

    def __init__(self) -> None:
        self.data = self.DataArray()

    def __str__(self) -> str:
        """Returns data unit in text form."""

        string = str()

        for idx in KnotCtlDataIdx:
            if self.data[idx]:
                if string:
                    string += ", "
                string += "%s = '%s'" % (idx.name, self.data[idx].decode())

        return string

    def __getitem__(self, index: KnotCtlDataIdx) -> str:
        """Data unit item getter."""

        value = self.data[index]
        return value.decode() if value else str()

    def __setitem__(self, index: KnotCtlDataIdx, value: str) -> None:
        """Data unit item setter."""

        self.data[index] = ctypes.c_char_p(value.encode()) if value != None else ctypes.c_char_p()


class KnotCtlError(Exception):
    """Libknot server control error."""

    def __init__(self, message: str, data: KnotCtlData = None) -> None:
        super().__init__()
        self.message = message
        self.data = data

    def __str__(self) -> str:
        out = "%s" % self.message
        if self.data:
            out += " (%s)" % self.data
        return out


class KnotCtlErrorConnect(KnotCtlError):
    """Control connection error."""


class KnotCtlErrorSend(KnotCtlError):
    """Control data send error."""


class KnotCtlErrorReceive(KnotCtlError):
    """Control data receive error."""


class KnotCtlErrorRemote(KnotCtlError):
    """Control error on the remote (server) side."""


class KnotCtl(object):
    """Libknot server control interface."""

    ALLOC = None
    FREE = None
    SET_TIMEOUT = None
    CONNECT = None
    CLOSE = None
    SEND = None
    RECEIVE = None

    def __init__(self) -> None:
        """Initializes a control interface instance."""

        if not KnotCtl.ALLOC:
            libknot.Knot()

            KnotCtl.ALLOC = libknot.Knot.LIBKNOT.knot_ctl_alloc
            KnotCtl.ALLOC.restype = ctypes.c_void_p

            KnotCtl.FREE = libknot.Knot.LIBKNOT.knot_ctl_free
            KnotCtl.FREE.argtypes = [ctypes.c_void_p]

            KnotCtl.SET_TIMEOUT = libknot.Knot.LIBKNOT.knot_ctl_set_timeout
            KnotCtl.SET_TIMEOUT.argtypes = [ctypes.c_void_p, ctypes.c_int]

            KnotCtl.CONNECT = libknot.Knot.LIBKNOT.knot_ctl_connect
            KnotCtl.CONNECT.restype = ctypes.c_int
            KnotCtl.CONNECT.argtypes = [ctypes.c_void_p, ctypes.c_char_p]

            KnotCtl.CLOSE = libknot.Knot.LIBKNOT.knot_ctl_close
            KnotCtl.CLOSE.argtypes = [ctypes.c_void_p]

            KnotCtl.SEND = libknot.Knot.LIBKNOT.knot_ctl_send
            KnotCtl.SEND.restype = ctypes.c_int
            KnotCtl.SEND.argtypes = [ctypes.c_void_p, ctypes.c_uint, ctypes.c_void_p]

            KnotCtl.RECEIVE = libknot.Knot.LIBKNOT.knot_ctl_receive
            KnotCtl.RECEIVE.restype = ctypes.c_int
            KnotCtl.RECEIVE.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]

        self.obj = KnotCtl.ALLOC()

    def __del__(self) -> None:
        """Deallocates control interface instance."""

        KnotCtl.FREE(self.obj)

    def set_timeout(self, timeout: int) -> None:
        """Sets control socket operations timeout in seconds."""

        KnotCtl.SET_TIMEOUT(self.obj, timeout * 1000)

    def connect(self, path: str) -> None:
        """Connect to a specified control UNIX socket."""

        ret = KnotCtl.CONNECT(self.obj, path.encode())
        if ret != 0:
            err = libknot.Knot.STRERROR(ret)
            raise KnotCtlErrorConnect(err.decode())

    def close(self) -> None:
        """Disconnects from the current control socket."""

        KnotCtl.CLOSE(self.obj)

    def send(self, data_type: KnotCtlType, data: KnotCtlData = None) -> None:
        """Sends a data unit to the connected control socket."""

        ret = KnotCtl.SEND(self.obj, data_type,
                           data.data if data else ctypes.c_char_p())
        if ret != 0:
            err = libknot.Knot.STRERROR(ret)
            raise KnotCtlErrorSend(err.decode())

    def receive(self, data: KnotCtlData = None) -> KnotCtlType:
        """Receives a data unit from the connected control socket."""

        data_type = ctypes.c_uint()
        ret = KnotCtl.RECEIVE(self.obj, ctypes.byref(data_type),
                              data.data if data else ctypes.c_char_p())
        if ret != 0:
            err = libknot.Knot.STRERROR(ret)
            raise KnotCtlErrorReceive(err.decode())
        return KnotCtlType(data_type.value)

    def send_block(self, cmd: str, section: str = None, item: str = None,
                   identifier: str = None, zone: str = None, owner: str = None,
                   ttl: str = None, rtype: str = None, data: str = None,
                   flags: str = None, filters: str = None) -> None:
        """Sends a control query block."""

        query = KnotCtlData()
        query[KnotCtlDataIdx.COMMAND] = cmd
        query[KnotCtlDataIdx.SECTION] = section
        query[KnotCtlDataIdx.ITEM] = item
        query[KnotCtlDataIdx.ID] = identifier
        query[KnotCtlDataIdx.ZONE] = zone
        query[KnotCtlDataIdx.OWNER] = owner
        query[KnotCtlDataIdx.TTL] = ttl
        query[KnotCtlDataIdx.TYPE] = rtype
        query[KnotCtlDataIdx.DATA] = data
        query[KnotCtlDataIdx.FLAGS] = flags
        query[KnotCtlDataIdx.FILTER] = filters

        self.send(KnotCtlType.DATA, query)
        self.send(KnotCtlType.BLOCK)

    def _receive_conf(self, out, reply):

        section = reply[KnotCtlDataIdx.SECTION]
        ident = reply[KnotCtlDataIdx.ID]
        item = reply[KnotCtlDataIdx.ITEM]
        data = reply[KnotCtlDataIdx.DATA]

        # Add the section if not exists.
        if section not in out:
            out[section] = dict()

        # Add the identifier if not exists.
        if ident and ident not in out[section]:
            out[section][ident] = dict()

        # Return if no item/value.
        if not item:
            return

        item_level = out[section][ident] if ident else out[section]

        # Treat alone identifier item differently.
        if item in ["id", "domain", "target"]:
            if data not in out[section]:
                out[section][data] = dict()
        else:
            if item not in item_level:
                item_level[item] = list()

            if data:
                item_level[item].append(data)

    def _receive_zone_status(self, out, reply):

        zone = reply[KnotCtlDataIdx.ZONE]
        rtype = reply[KnotCtlDataIdx.TYPE]
        data = reply[KnotCtlDataIdx.DATA]

        # Add the zone if not exists.
        if zone not in out:
            out[zone] = dict()

        out[zone][rtype] = data

    def _receive_zone(self, out, reply):

        zone = reply[KnotCtlDataIdx.ZONE]
        owner = reply[KnotCtlDataIdx.OWNER]
        ttl = reply[KnotCtlDataIdx.TTL]
        rtype = reply[KnotCtlDataIdx.TYPE]
        data = reply[KnotCtlDataIdx.DATA]

        # Add the zone if not exists.
        if zone not in out:
            out[zone] = dict()

        if owner not in out[zone]:
            out[zone][owner] = dict()

        if rtype not in out[zone][owner]:
            out[zone][owner][rtype] = dict()

        # Add the key/value.
        out[zone][owner][rtype]["ttl"] = ttl

        if not "data" in out[zone][owner][rtype]:
            out[zone][owner][rtype]["data"] = [data]
        else:
            out[zone][owner][rtype]["data"].append(data)

    def _receive_stats(self, out, reply):

        zone = reply[KnotCtlDataIdx.ZONE]
        section = reply[KnotCtlDataIdx.SECTION]
        item = reply[KnotCtlDataIdx.ITEM]
        idx = reply[KnotCtlDataIdx.ID]
        data = int(reply[KnotCtlDataIdx.DATA])

        # Add the zone if not exists.
        if zone:
            if "zone" not in out:
                out["zone"] = dict()

            if zone not in out["zone"]:
                out["zone"][zone] = dict()

        section_level = out["zone"][zone] if zone else out

        if section not in section_level:
            section_level[section] = dict()

        if idx:
            if item not in section_level[section]:
                section_level[section][item] = dict()

            section_level[section][item][idx] = data
        else:
            section_level[section][item] = data

    def receive_stats(self) -> dict:
        """Receives statistics answer and returns it as a structured dictionary."""

        out = dict()
        err_reply = None

        while True:
            reply = KnotCtlData()
            reply_type = self.receive(reply)

            # Stop if not data type.
            if reply_type not in [KnotCtlType.DATA, KnotCtlType.EXTRA]:
                break

            # Check for an error.
            if reply[KnotCtlDataIdx.ERROR]:
                err_reply = reply
                continue

            self._receive_stats(out, reply)

        if err_reply:
            raise KnotCtlErrorRemote(err_reply[KnotCtlDataIdx.ERROR], err_reply)

        return out

    def receive_block(self) -> dict:
        """Receives a control answer and returns it as a structured dictionary."""

        out = dict()
        err_reply = None

        while True:
            reply = KnotCtlData()
            reply_type = self.receive(reply)

            # Stop if not data type.
            if reply_type not in [KnotCtlType.DATA, KnotCtlType.EXTRA]:
                break

            # Check for an error.
            if reply[KnotCtlDataIdx.ERROR]:
                err_reply = reply
                continue

            # Check for config data.
            if reply[KnotCtlDataIdx.SECTION]:
                self._receive_conf(out, reply)
            # Check for zone data.
            elif reply[KnotCtlDataIdx.ZONE]:
                if reply[KnotCtlDataIdx.OWNER]:
                    self._receive_zone(out, reply)
                else:
                    self._receive_zone_status(out, reply)
            else:
                continue

        if err_reply:
            raise KnotCtlErrorRemote(err_reply[KnotCtlDataIdx.ERROR], err_reply)

        return out