summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/python/messagerenderer_python.cc
blob: 5561c12c346c975829b616f8715beef7be85a3e3 (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
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <Python.h>

#include <util/buffer.h>

#include <dns/messagerenderer.h>
#include <util/python/pycppwrapper_util.h>

#include "pydnspp_common.h"
#include "messagerenderer_python.h"

using namespace isc::dns;
using namespace isc::dns::python;
using namespace isc::util;
using namespace isc::util::python;

namespace {
// The s_* Class simply covers one instantiation of the object.
//
// since we don't use *Buffer in the python version (but work with
// the already existing bytearray type where we use these custom buffers
// in C++, we need to keep track of one here.
class s_MessageRenderer : public PyObject {
public:
    s_MessageRenderer();
    MessageRenderer* cppobj;
};

int MessageRenderer_init(s_MessageRenderer* self);
void MessageRenderer_destroy(s_MessageRenderer* self);

PyObject* MessageRenderer_getData(s_MessageRenderer* self);
PyObject* MessageRenderer_getLength(s_MessageRenderer* self);
PyObject* MessageRenderer_isTruncated(s_MessageRenderer* self);
PyObject* MessageRenderer_getLengthLimit(s_MessageRenderer* self);
PyObject* MessageRenderer_getCompressMode(s_MessageRenderer* self);
PyObject* MessageRenderer_setTruncated(s_MessageRenderer* self);
PyObject* MessageRenderer_setLengthLimit(s_MessageRenderer* self, PyObject* args);
PyObject* MessageRenderer_setCompressMode(s_MessageRenderer* self, PyObject* args);
PyObject* MessageRenderer_clear(s_MessageRenderer* self);

PyMethodDef MessageRenderer_methods[] = {
    { "get_data", reinterpret_cast<PyCFunction>(MessageRenderer_getData), METH_NOARGS,
      "Returns the data as a bytes() object" },
    { "get_length", reinterpret_cast<PyCFunction>(MessageRenderer_getLength), METH_NOARGS,
      "Returns the length of the data" },
    { "is_truncated", reinterpret_cast<PyCFunction>(MessageRenderer_isTruncated), METH_NOARGS,
      "Returns True if the data is truncated" },
    { "get_length_limit", reinterpret_cast<PyCFunction>(MessageRenderer_getLengthLimit), METH_NOARGS,
      "Returns the length limit of the data" },
    { "get_compress_mode", reinterpret_cast<PyCFunction>(MessageRenderer_getCompressMode), METH_NOARGS,
      "Returns the current compression mode" },
    { "set_truncated", reinterpret_cast<PyCFunction>(MessageRenderer_setTruncated), METH_NOARGS,
      "Sets truncated to true" },
    { "set_length_limit", reinterpret_cast<PyCFunction>(MessageRenderer_setLengthLimit), METH_VARARGS,
      "Sets the length limit of the data to the given number" },
    { "set_compress_mode", reinterpret_cast<PyCFunction>(MessageRenderer_setCompressMode), METH_VARARGS,
      "Sets the compression mode of the MessageRenderer" },
    { "clear", reinterpret_cast<PyCFunction>(MessageRenderer_clear),
      METH_NOARGS,
      "Clear the internal buffer and other internal resources." },
    { NULL, NULL, 0, NULL }
};

int
MessageRenderer_init(s_MessageRenderer* self) {
    self->cppobj = new MessageRenderer;
    return (0);
}

void
MessageRenderer_destroy(s_MessageRenderer* self) {
    delete self->cppobj;
    self->cppobj = NULL;
    Py_TYPE(self)->tp_free(self);
}

PyObject*
MessageRenderer_getData(s_MessageRenderer* self) {
    return (Py_BuildValue("y#",
                          self->cppobj->getData(),
                          self->cppobj->getLength()));
}

PyObject*
MessageRenderer_getLength(s_MessageRenderer* self) {
    return (Py_BuildValue("I", self->cppobj->getLength()));
}

PyObject*
MessageRenderer_isTruncated(s_MessageRenderer* self) {
    if (self->cppobj->isTruncated()) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}

PyObject*
MessageRenderer_getLengthLimit(s_MessageRenderer* self) {
    return (Py_BuildValue("I", self->cppobj->getLengthLimit()));
}

PyObject*
MessageRenderer_getCompressMode(s_MessageRenderer* self) {
    return (Py_BuildValue("I", self->cppobj->getCompressMode()));
}

PyObject*
MessageRenderer_setTruncated(s_MessageRenderer* self) {
    self->cppobj->setTruncated();
    Py_RETURN_NONE;
}

PyObject*
MessageRenderer_setLengthLimit(s_MessageRenderer* self,
                               PyObject* args)
{
    long lengthlimit;
    if (!PyArg_ParseTuple(args, "l", &lengthlimit)) {
        PyErr_Clear();
        PyErr_SetString(PyExc_TypeError,
                        "No valid type in set_length_limit argument");
        return (NULL);
    }
    if (lengthlimit < 0) {
        PyErr_SetString(PyExc_ValueError,
                        "MessageRenderer length limit out of range");
        return (NULL);
    }
    self->cppobj->setLengthLimit(lengthlimit);
    Py_RETURN_NONE;
}

PyObject*
MessageRenderer_setCompressMode(s_MessageRenderer* self,
                               PyObject* args)
{
    int mode;
    if (!PyArg_ParseTuple(args, "i", &mode)) {
        return (NULL);
    }

    if (mode == MessageRenderer::CASE_INSENSITIVE) {
        self->cppobj->setCompressMode(MessageRenderer::CASE_INSENSITIVE);
        // If we return NULL it is seen as an error, so use this for
        // None returns, it also applies to CASE_SENSITIVE.
        Py_RETURN_NONE;
    } else if (mode == MessageRenderer::CASE_SENSITIVE) {
        self->cppobj->setCompressMode(MessageRenderer::CASE_SENSITIVE);
        Py_RETURN_NONE;
    } else {
        PyErr_SetString(PyExc_TypeError,
                        "MessageRenderer compress mode must be MessageRenderer.CASE_INSENSITIVE"
                        "or MessageRenderer.CASE_SENSITIVE");
        return (NULL);
    }
}

PyObject*
MessageRenderer_clear(s_MessageRenderer* self) {
    self->cppobj->clear();
    Py_RETURN_NONE;
}
} // end of unnamed namespace

namespace isc {
namespace dns {
namespace python {
PyTypeObject messagerenderer_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "pydnspp.MessageRenderer",
    sizeof(s_MessageRenderer),          // tp_basicsize
    0,                                  // tp_itemsize
    (destructor)MessageRenderer_destroy,// tp_dealloc
    NULL,                               // tp_print
    NULL,                               // tp_getattr
    NULL,                               // tp_setattr
    NULL,                               // tp_reserved
    NULL,                               // tp_repr
    NULL,                               // tp_as_number
    NULL,                               // tp_as_sequence
    NULL,                               // tp_as_mapping
    NULL,                               // tp_hash
    NULL,                               // tp_call
    NULL,                               // tp_str
    NULL,                               // tp_getattro
    NULL,                               // tp_setattro
    NULL,                               // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
    "The MessageRenderer class encapsulates implementation details "
    "of rendering a DNS message into a buffer in wire format. "
    "In effect, it's simply responsible for name compression at least in the "
    "current implementation. A MessageRenderer class object manages the "
    "positions of names rendered in a buffer and uses that information to render "
    "subsequent names with compression.",
    NULL,                               // tp_traverse
    NULL,                               // tp_clear
    NULL,                               // tp_richcompare
    0,                                  // tp_weaklistoffset
    NULL,                               // tp_iter
    NULL,                               // tp_iternext
    MessageRenderer_methods,            // tp_methods
    NULL,                               // tp_members
    NULL,                               // tp_getset
    NULL,                               // tp_base
    NULL,                               // tp_dict
    NULL,                               // tp_descr_get
    NULL,                               // tp_descr_set
    0,                                  // tp_dictoffset
    (initproc)MessageRenderer_init,     // tp_init
    NULL,                               // tp_alloc
    PyType_GenericNew,                  // tp_new
    NULL,                               // tp_free
    NULL,                               // tp_is_gc
    NULL,                               // tp_bases
    NULL,                               // tp_mro
    NULL,                               // tp_cache
    NULL,                               // tp_subclasses
    NULL,                               // tp_weaklist
    NULL,                               // tp_del
    0                                   // tp_version_tag
};

// If we need a createMessageRendererObject(), should we copy? can we?
// copy the existing buffer into a new one, then create a new renderer with
// that buffer?

bool
PyMessageRenderer_Check(PyObject* obj) {
    if (obj == NULL) {
        isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
    }
    return (PyObject_TypeCheck(obj, &messagerenderer_type));
}

MessageRenderer&
PyMessageRenderer_ToMessageRenderer(PyObject* messagerenderer_obj) {
    if (messagerenderer_obj == NULL) {
        isc_throw(PyCPPWrapperException,
                  "obj argument NULL in MessageRenderer PyObject conversion");
    }
    s_MessageRenderer* messagerenderer = static_cast<s_MessageRenderer*>(messagerenderer_obj);
    return (*messagerenderer->cppobj);
}


} // namespace python
} // namespace dns
} // namespace isc