Branch data Line data Source code
1 : : // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #include <Python.h>
16 : : #include <vector>
17 : :
18 : : #include <dns/rrtype.h>
19 : : #include <dns/messagerenderer.h>
20 : : #include <util/python/pycppwrapper_util.h>
21 : :
22 : : #include "rrtype_python.h"
23 : : #include "messagerenderer_python.h"
24 : : #include "pydnspp_common.h"
25 : :
26 : : using namespace std;
27 : : using namespace isc::dns;
28 : : using namespace isc::dns::python;
29 : : using namespace isc::util;
30 : : using namespace isc::util::python;
31 : :
32 : : namespace {
33 : : // The s_* Class simply covers one instantiation of the object
34 : : class s_RRType : public PyObject {
35 : : public:
36 : : const RRType* cppobj;
37 : : };
38 : :
39 : : // General creation and destruction
40 : : int RRType_init(s_RRType* self, PyObject* args);
41 : : void RRType_destroy(s_RRType* self);
42 : :
43 : : // These are the functions we export
44 : : PyObject*
45 : : RRType_toText(s_RRType* self);
46 : : // This is a second version of toText, we need one where the argument
47 : : // is a PyObject*, for the str() function in python.
48 : : PyObject* RRType_str(PyObject* self);
49 : : PyObject* RRType_toWire(s_RRType* self, PyObject* args);
50 : : PyObject* RRType_getCode(s_RRType* self);
51 : : PyObject* RRType_richcmp(s_RRType* self, s_RRType* other, int op);
52 : : PyObject* RRType_NSEC3PARAM(s_RRType *self);
53 : : PyObject* RRType_DNAME(s_RRType *self);
54 : : PyObject* RRType_PTR(s_RRType *self);
55 : : PyObject* RRType_MX(s_RRType *self);
56 : : PyObject* RRType_DNSKEY(s_RRType *self);
57 : : PyObject* RRType_TXT(s_RRType *self);
58 : : PyObject* RRType_RRSIG(s_RRType *self);
59 : : PyObject* RRType_NSEC(s_RRType *self);
60 : : PyObject* RRType_AAAA(s_RRType *self);
61 : : PyObject* RRType_DS(s_RRType *self);
62 : : PyObject* RRType_OPT(s_RRType *self);
63 : : PyObject* RRType_A(s_RRType *self);
64 : : PyObject* RRType_NS(s_RRType *self);
65 : : PyObject* RRType_CNAME(s_RRType *self);
66 : : PyObject* RRType_SOA(s_RRType *self);
67 : : PyObject* RRType_NSEC3(s_RRType *self);
68 : : PyObject* RRType_IXFR(s_RRType *self);
69 : : PyObject* RRType_AXFR(s_RRType *self);
70 : : PyObject* RRType_ANY(s_RRType *self);
71 : :
72 : : typedef CPPPyObjectContainer<s_RRType, RRType> RRTypeContainer;
73 : :
74 : : // This list contains the actual set of functions we have in
75 : : // python. Each entry has
76 : : // 1. Python method name
77 : : // 2. Our static function here
78 : : // 3. Argument type
79 : : // 4. Documentation
80 : : PyMethodDef RRType_methods[] = {
81 : : { "to_text", reinterpret_cast<PyCFunction>(RRType_toText), METH_NOARGS,
82 : : "Returns the string representation" },
83 : : { "to_wire", reinterpret_cast<PyCFunction>(RRType_toWire), METH_VARARGS,
84 : : "Converts the RRType object to wire format.\n"
85 : : "The argument can be either a MessageRenderer or an object that "
86 : : "implements the sequence interface. If the object is mutable "
87 : : "(for instance a bytearray()), the wire data is added in-place.\n"
88 : : "If it is not (for instance a bytes() object), a new object is "
89 : : "returned" },
90 : : { "get_code", reinterpret_cast<PyCFunction>(RRType_getCode), METH_NOARGS,
91 : : "Returns the type code as an integer" },
92 : : { "NSEC3PARAM", reinterpret_cast<PyCFunction>(RRType_NSEC3PARAM), METH_NOARGS | METH_STATIC, "Creates an NSEC3PARAM RRType" },
93 : : { "DNAME", reinterpret_cast<PyCFunction>(RRType_DNAME), METH_NOARGS | METH_STATIC, "Creates a DNAME RRType" },
94 : : { "PTR", reinterpret_cast<PyCFunction>(RRType_PTR), METH_NOARGS | METH_STATIC, "Creates a PTR RRType" },
95 : : { "MX", reinterpret_cast<PyCFunction>(RRType_MX), METH_NOARGS | METH_STATIC, "Creates an MX RRType" },
96 : : { "DNSKEY", reinterpret_cast<PyCFunction>(RRType_DNSKEY), METH_NOARGS | METH_STATIC, "Creates a DNSKEY RRType" },
97 : : { "TXT", reinterpret_cast<PyCFunction>(RRType_TXT), METH_NOARGS | METH_STATIC, "Creates a TXT RRType" },
98 : : { "RRSIG", reinterpret_cast<PyCFunction>(RRType_RRSIG), METH_NOARGS | METH_STATIC, "Creates a RRSIG RRType" },
99 : : { "NSEC", reinterpret_cast<PyCFunction>(RRType_NSEC), METH_NOARGS | METH_STATIC, "Creates a NSEC RRType" },
100 : : { "AAAA", reinterpret_cast<PyCFunction>(RRType_AAAA), METH_NOARGS | METH_STATIC, "Creates an AAAA RRType" },
101 : : { "DS", reinterpret_cast<PyCFunction>(RRType_DS), METH_NOARGS | METH_STATIC, "Creates a DS RRType" },
102 : : { "OPT", reinterpret_cast<PyCFunction>(RRType_OPT), METH_NOARGS | METH_STATIC, "Creates an OPT RRType" },
103 : : { "A", reinterpret_cast<PyCFunction>(RRType_A), METH_NOARGS | METH_STATIC, "Creates an A RRType" },
104 : : { "NS", reinterpret_cast<PyCFunction>(RRType_NS), METH_NOARGS | METH_STATIC, "Creates an NS RRType" },
105 : : { "CNAME", reinterpret_cast<PyCFunction>(RRType_CNAME), METH_NOARGS | METH_STATIC, "Creates a CNAME RRType" },
106 : : { "SOA", reinterpret_cast<PyCFunction>(RRType_SOA), METH_NOARGS | METH_STATIC, "Creates a SOA RRType" },
107 : : { "NSEC3", reinterpret_cast<PyCFunction>(RRType_NSEC3), METH_NOARGS | METH_STATIC, "Creates an NSEC3 RRType" },
108 : : { "IXFR", reinterpret_cast<PyCFunction>(RRType_IXFR), METH_NOARGS | METH_STATIC, "Creates an IXFR RRType" },
109 : : { "AXFR", reinterpret_cast<PyCFunction>(RRType_AXFR), METH_NOARGS | METH_STATIC, "Creates an AXFR RRType" },
110 : : { "ANY", reinterpret_cast<PyCFunction>(RRType_ANY), METH_NOARGS | METH_STATIC, "Creates an ANY RRType" },
111 : : { NULL, NULL, 0, NULL }
112 : : };
113 : :
114 : : int
115 : 593 : RRType_init(s_RRType* self, PyObject* args) {
116 : : const char* s;
117 : : long i;
118 : 593 : PyObject* bytes = NULL;
119 : : // The constructor argument can be a string ("A"), an integer (1),
120 : : // or a sequence of numbers between 0 and 65535 (wire code)
121 : :
122 : : // Note that PyArg_ParseType can set PyError, and we need to clear
123 : : // that if we try several like here. Otherwise the *next* python
124 : : // call will suddenly appear to throw an exception.
125 : : // (the way to do exceptions is to set PyErr and return -1)
126 : : try {
127 [ + - ][ + + ]: 593 : if (PyArg_ParseTuple(args, "s", &s)) {
128 [ + - ][ + - ]: 495 : self->cppobj = new RRType(s);
[ + + ]
129 : : return (0);
130 [ + - ][ + + ]: 98 : } else if (PyArg_ParseTuple(args, "l", &i)) {
131 [ + - ]: 94 : PyErr_Clear();
132 [ + + ]: 94 : if (i < 0 || i > 0xffff) {
133 [ + - ]: 1 : PyErr_SetString(PyExc_ValueError, "RR Type number out of range");
134 : : return (-1);
135 : : }
136 [ + - ]: 93 : self->cppobj = new RRType(i);
137 : 93 : return (0);
138 [ + - ][ + - ]: 4 : } else if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
[ + - ][ + + ]
[ + + ]
139 [ + - ]: 2 : Py_ssize_t size = PySequence_Size(bytes);
140 : 2 : vector<uint8_t> data(size);
141 [ + - ]: 2 : int result = readDataFromSequence(&data[0], size, bytes);
142 [ + - ]: 2 : if (result != 0) {
143 : : return (result);
144 : : }
145 : 2 : InputBuffer ib(&data[0], size);
146 [ + - ][ + + ]: 2 : self->cppobj = new RRType(ib);
147 [ + - ]: 1 : PyErr_Clear();
148 : : return (0);
149 : : }
150 : 2 : } catch (const IncompleteRRType& icc) {
151 : : // Ok so one of our functions has thrown a C++ exception.
152 : : // We need to translate that to a Python Exception
153 : : // First clear any existing error that was set
154 [ - + ]: 1 : PyErr_Clear();
155 : : // Now set our own exception
156 [ - + ]: 1 : PyErr_SetString(po_IncompleteRRType, icc.what());
157 : : // And return negative
158 : : return (-1);
159 [ - + + ]: 15 : } catch (const InvalidRRType& ic) {
160 [ - + ]: 7 : PyErr_Clear();
161 [ - + ]: 7 : PyErr_SetString(po_InvalidRRType, ic.what());
162 : : return (-1);
163 : : }
164 : 2 : PyErr_Clear();
165 : : PyErr_SetString(PyExc_TypeError,
166 : 2 : "no valid type in constructor argument");
167 : 593 : return (-1);
168 : : }
169 : :
170 : : void
171 : 6049 : RRType_destroy(s_RRType* self) {
172 : 6049 : delete self->cppobj;
173 : 6049 : self->cppobj = NULL;
174 : 6049 : Py_TYPE(self)->tp_free(self);
175 : 6049 : }
176 : :
177 : : PyObject*
178 : 56 : RRType_toText(s_RRType* self) {
179 : : // Py_BuildValue makes python objects from native data
180 [ + - ]: 56 : return (Py_BuildValue("s", self->cppobj->toText().c_str()));
181 : : }
182 : :
183 : : PyObject*
184 : 36 : RRType_str(PyObject* self) {
185 : : // Simply call the to_text method we already defined
186 : : return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
187 : 36 : const_cast<char*>("")));
188 : : }
189 : :
190 : : PyObject*
191 : 11 : RRType_toWire(s_RRType* self, PyObject* args) {
192 : : PyObject* bytes;
193 : : PyObject* mr;
194 : :
195 [ + - ][ + + ]: 11 : if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
[ + + ]
196 : 6 : PyObject* bytes_o = bytes;
197 : :
198 : 6 : OutputBuffer buffer(2);
199 [ + - ]: 6 : self->cppobj->toWire(buffer);
200 [ + - ]: 6 : PyObject* n = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()), buffer.getLength());
201 [ + - ]: 6 : PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
202 : : // We need to release the object we temporarily created here
203 : : // to prevent memory leak
204 [ + - ][ + - ]: 6 : Py_DECREF(n);
205 : : return (result);
206 [ + - ]: 5 : } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
207 : 5 : self->cppobj->toWire(PyMessageRenderer_ToMessageRenderer(mr));
208 : : // If we return NULL it is seen as an error, so use this for
209 : : // None returns
210 : 5 : Py_RETURN_NONE;
211 : : }
212 : 0 : PyErr_Clear();
213 : : PyErr_SetString(PyExc_TypeError,
214 : 0 : "toWire argument must be a sequence object or a MessageRenderer");
215 : 11 : return (NULL);
216 : : }
217 : :
218 : : PyObject*
219 : 4 : RRType_getCode(s_RRType* self) {
220 : 4 : return (Py_BuildValue("I", self->cppobj->getCode()));
221 : : }
222 : :
223 : : PyObject*
224 : 1818 : RRType_richcmp(s_RRType* self, s_RRType* other, int op) {
225 : : bool c;
226 : :
227 : : // Check for null and if the types match. If different type,
228 : : // simply return False
229 [ + - ][ + + ]: 1818 : if (!other || (self->ob_type != other->ob_type)) {
230 : 22 : Py_RETURN_FALSE;
231 : : }
232 : :
233 [ + + + + : 1796 : switch (op) {
+ + - ]
234 : : case Py_LT:
235 : 22 : c = *self->cppobj < *other->cppobj;
236 : 11 : break;
237 : : case Py_LE:
238 : 2 : c = *self->cppobj < *other->cppobj ||
239 [ - + ][ # # ]: 2 : *self->cppobj == *other->cppobj;
240 : 2 : break;
241 : : case Py_EQ:
242 : 3100 : c = *self->cppobj == *other->cppobj;
243 : 1550 : break;
244 : : case Py_NE:
245 : 456 : c = *self->cppobj != *other->cppobj;
246 : 228 : break;
247 : : case Py_GT:
248 : 6 : c = *other->cppobj < *self->cppobj;
249 : 3 : break;
250 : : case Py_GE:
251 : 2 : c = *other->cppobj < *self->cppobj ||
252 [ + - ][ + - ]: 2 : *self->cppobj == *other->cppobj;
253 : 2 : break;
254 : : default:
255 : : PyErr_SetString(PyExc_IndexError,
256 : 0 : "Unhandled rich comparison operator");
257 : 0 : return (NULL);
258 : : }
259 [ + + ]: 1796 : if (c)
260 : 1031 : Py_RETURN_TRUE;
261 : : else
262 : 1818 : Py_RETURN_FALSE;
263 : : }
264 : :
265 : : //
266 : : // Common function for RRType_A/NS/etc.
267 : : //
268 : 3645 : PyObject* RRType_createStatic(RRType stc) {
269 : 3645 : s_RRType* ret = PyObject_New(s_RRType, &rrtype_type);
270 [ + - ]: 3645 : if (ret != NULL) {
271 : 3645 : ret->cppobj = new RRType(stc);
272 : : }
273 : 3645 : return (ret);
274 : : }
275 : :
276 : : PyObject*
277 : 12 : RRType_NSEC3PARAM(s_RRType*) {
278 : 12 : return (RRType_createStatic(RRType::NSEC3PARAM()));
279 : : }
280 : :
281 : : PyObject*
282 : 1 : RRType_DNAME(s_RRType*) {
283 : 1 : return (RRType_createStatic(RRType::DNAME()));
284 : : }
285 : :
286 : : PyObject*
287 : 1 : RRType_PTR(s_RRType*) {
288 : 1 : return (RRType_createStatic(RRType::PTR()));
289 : : }
290 : :
291 : : PyObject*
292 : 1 : RRType_MX(s_RRType*) {
293 : 1 : return (RRType_createStatic(RRType::MX()));
294 : : }
295 : :
296 : : PyObject*
297 : 3 : RRType_DNSKEY(s_RRType*) {
298 : 3 : return (RRType_createStatic(RRType::DNSKEY()));
299 : : }
300 : :
301 : : PyObject*
302 : 3 : RRType_TXT(s_RRType*) {
303 : 3 : return (RRType_createStatic(RRType::TXT()));
304 : : }
305 : :
306 : : PyObject*
307 : 404 : RRType_RRSIG(s_RRType*) {
308 : 404 : return (RRType_createStatic(RRType::RRSIG()));
309 : : }
310 : :
311 : : PyObject*
312 : 3 : RRType_NSEC(s_RRType*) {
313 : 3 : return (RRType_createStatic(RRType::NSEC()));
314 : : }
315 : :
316 : : PyObject*
317 : 76 : RRType_AAAA(s_RRType*) {
318 : 76 : return (RRType_createStatic(RRType::AAAA()));
319 : : }
320 : :
321 : : PyObject*
322 : 1 : RRType_DS(s_RRType*) {
323 : 1 : return (RRType_createStatic(RRType::DS()));
324 : : }
325 : :
326 : : PyObject*
327 : 1 : RRType_OPT(s_RRType*) {
328 : 1 : return (RRType_createStatic(RRType::OPT()));
329 : : }
330 : :
331 : : PyObject*
332 : 260 : RRType_A(s_RRType*) {
333 : 260 : return (RRType_createStatic(RRType::A()));
334 : : }
335 : :
336 : : PyObject*
337 : 352 : RRType_NS(s_RRType*) {
338 : 352 : return (RRType_createStatic(RRType::NS()));
339 : : }
340 : :
341 : : PyObject*
342 : 17 : RRType_CNAME(s_RRType*) {
343 : 17 : return (RRType_createStatic(RRType::CNAME()));
344 : : }
345 : :
346 : : PyObject*
347 : 1867 : RRType_SOA(s_RRType*) {
348 : 1867 : return (RRType_createStatic(RRType::SOA()));
349 : : }
350 : :
351 : : PyObject*
352 : 10 : RRType_NSEC3(s_RRType*) {
353 : 10 : return (RRType_createStatic(RRType::NSEC3()));
354 : : }
355 : :
356 : : PyObject*
357 : 362 : RRType_IXFR(s_RRType*) {
358 : 362 : return (RRType_createStatic(RRType::IXFR()));
359 : : }
360 : :
361 : : PyObject*
362 : 270 : RRType_AXFR(s_RRType*) {
363 : 270 : return (RRType_createStatic(RRType::AXFR()));
364 : : }
365 : :
366 : : PyObject*
367 : 1 : RRType_ANY(s_RRType*) {
368 : 1 : return (RRType_createStatic(RRType::ANY()));
369 : : }
370 : :
371 : : } // end anonymous namespace
372 : :
373 : : namespace isc {
374 : : namespace dns {
375 : : namespace python {
376 : :
377 : : PyObject* po_InvalidRRType;
378 : : PyObject* po_IncompleteRRType;
379 : :
380 : : // This defines the complete type for reflection in python and
381 : : // parsing of PyObject* to s_RRType
382 : : // Most of the functions are not actually implemented and NULL here.
383 : : PyTypeObject rrtype_type = {
384 : : PyVarObject_HEAD_INIT(NULL, 0)
385 : : "pydnspp.RRType",
386 : : sizeof(s_RRType), // tp_basicsize
387 : : 0, // tp_itemsize
388 : : (destructor)RRType_destroy, // tp_dealloc
389 : : NULL, // tp_print
390 : : NULL, // tp_getattr
391 : : NULL, // tp_setattr
392 : : NULL, // tp_reserved
393 : : NULL, // tp_repr
394 : : NULL, // tp_as_number
395 : : NULL, // tp_as_sequence
396 : : NULL, // tp_as_mapping
397 : : NULL, // tp_hash
398 : : NULL, // tp_call
399 : : RRType_str, // tp_str
400 : : NULL, // tp_getattro
401 : : NULL, // tp_setattro
402 : : NULL, // tp_as_buffer
403 : : Py_TPFLAGS_DEFAULT, // tp_flags
404 : : "The RRType class encapsulates DNS resource record types.\n\n"
405 : : "This class manages the 16-bit integer type codes in quite a straightforward "
406 : : "way. The only non trivial task is to handle textual representations of "
407 : : "RR types, such as \"A\", \"AAAA\", or \"TYPE65534\".",
408 : : NULL, // tp_traverse
409 : : NULL, // tp_clear
410 : : (richcmpfunc)RRType_richcmp, // tp_richcompare
411 : : 0, // tp_weaklistoffset
412 : : NULL, // tp_iter
413 : : NULL, // tp_iternext
414 : : RRType_methods, // tp_methods
415 : : NULL, // tp_members
416 : : NULL, // tp_getset
417 : : NULL, // tp_base
418 : : NULL, // tp_dict
419 : : NULL, // tp_descr_get
420 : : NULL, // tp_descr_set
421 : : 0, // tp_dictoffset
422 : : (initproc)RRType_init, // tp_init
423 : : NULL, // tp_alloc
424 : : PyType_GenericNew, // tp_new
425 : : NULL, // tp_free
426 : : NULL, // tp_is_gc
427 : : NULL, // tp_bases
428 : : NULL, // tp_mro
429 : : NULL, // tp_cache
430 : : NULL, // tp_subclasses
431 : : NULL, // tp_weaklist
432 : : NULL, // tp_del
433 : : 0 // tp_version_tag
434 : : };
435 : :
436 : : PyObject*
437 : 1812 : createRRTypeObject(const RRType& source) {
438 : 1812 : RRTypeContainer container(PyObject_New(s_RRType, &rrtype_type));
439 [ + - ][ + - ]: 1812 : container.set(new RRType(source));
440 : 1812 : return (container.release());
441 : : }
442 : :
443 : : bool
444 : 0 : PyRRType_Check(PyObject* obj) {
445 [ # # ]: 0 : if (obj == NULL) {
446 [ # # ][ # # ]: 0 : isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
447 : : }
448 [ # # ][ # # ]: 0 : return (PyObject_TypeCheck(obj, &rrtype_type));
449 : : }
450 : :
451 : : const RRType&
452 : 2909 : PyRRType_ToRRType(const PyObject* rrtype_obj) {
453 [ - + ]: 2909 : if (rrtype_obj == NULL) {
454 [ # # ][ # # ]: 0 : isc_throw(PyCPPWrapperException,
455 : : "obj argument NULL in RRType PyObject conversion");
456 : : }
457 : 2909 : const s_RRType* rrtype = static_cast<const s_RRType*>(rrtype_obj);
458 : 2909 : return (*rrtype->cppobj);
459 : : }
460 : :
461 : :
462 : : } // end namespace python
463 : : } // end namespace dns
464 : 6 : } // end namespace isc
|