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 : : #include <Python.h>
15 : :
16 : : #include <dns/rrclass.h>
17 : : #include <dns/messagerenderer.h>
18 : : #include <util/buffer.h>
19 : : #include <util/python/pycppwrapper_util.h>
20 : :
21 : : #include "rrclass_python.h"
22 : : #include "messagerenderer_python.h"
23 : : #include "pydnspp_common.h"
24 : :
25 : :
26 : : using namespace isc::dns;
27 : : using namespace isc::dns::python;
28 : : using namespace isc::util;
29 : : using namespace isc::util::python;
30 : : namespace {
31 : : // The s_* Class simply covers one instantiation of the object
32 : : class s_RRClass : public PyObject {
33 : : public:
34 : : s_RRClass() : cppobj(NULL) {};
35 : : RRClass* cppobj;
36 : : };
37 : :
38 : : //
39 : : // We declare the functions here, the definitions are below
40 : : // the type definition of the object, since both can use the other
41 : : //
42 : :
43 : : // General creation and destruction
44 : : int RRClass_init(s_RRClass* self, PyObject* args);
45 : : void RRClass_destroy(s_RRClass* self);
46 : :
47 : : // These are the functions we export
48 : : PyObject* RRClass_toText(s_RRClass* self);
49 : : // This is a second version of toText, we need one where the argument
50 : : // is a PyObject*, for the str() function in python.
51 : : PyObject* RRClass_str(PyObject* self);
52 : : PyObject* RRClass_toWire(s_RRClass* self, PyObject* args);
53 : : PyObject* RRClass_getCode(s_RRClass* self);
54 : : PyObject* RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op);
55 : :
56 : : // Static function for direct class creation
57 : : PyObject* RRClass_IN(s_RRClass *self);
58 : : PyObject* RRClass_CH(s_RRClass *self);
59 : : PyObject* RRClass_HS(s_RRClass *self);
60 : : PyObject* RRClass_NONE(s_RRClass *self);
61 : : PyObject* RRClass_ANY(s_RRClass *self);
62 : :
63 : : typedef CPPPyObjectContainer<s_RRClass, RRClass> RRClassContainer;
64 : :
65 : : // This list contains the actual set of functions we have in
66 : : // python. Each entry has
67 : : // 1. Python method name
68 : : // 2. Our static function here
69 : : // 3. Argument type
70 : : // 4. Documentation
71 : : PyMethodDef RRClass_methods[] = {
72 : : { "to_text", reinterpret_cast<PyCFunction>(RRClass_toText), METH_NOARGS,
73 : : "Returns the string representation" },
74 : : { "to_wire", reinterpret_cast<PyCFunction>(RRClass_toWire), METH_VARARGS,
75 : : "Converts the RRClass object to wire format.\n"
76 : : "The argument can be either a MessageRenderer or an object that "
77 : : "implements the sequence interface. If the object is mutable "
78 : : "(for instance a bytearray()), the wire data is added in-place.\n"
79 : : "If it is not (for instance a bytes() object), a new object is "
80 : : "returned" },
81 : : { "get_code", reinterpret_cast<PyCFunction>(RRClass_getCode), METH_NOARGS,
82 : : "Returns the class code as an integer" },
83 : : { "IN", reinterpret_cast<PyCFunction>(RRClass_IN), METH_NOARGS | METH_STATIC, "Creates an IN RRClass" },
84 : : { "CH", reinterpret_cast<PyCFunction>(RRClass_CH), METH_NOARGS | METH_STATIC, "Creates a CH RRClass" },
85 : : { "HS", reinterpret_cast<PyCFunction>(RRClass_HS), METH_NOARGS | METH_STATIC, "Creates an HS RRClass" },
86 : : { "NONE", reinterpret_cast<PyCFunction>(RRClass_NONE), METH_NOARGS | METH_STATIC, "Creates a NONE RRClass" },
87 : : { "ANY", reinterpret_cast<PyCFunction>(RRClass_ANY), METH_NOARGS | METH_STATIC, "Creates an ANY RRClass" },
88 : : { NULL, NULL, 0, NULL }
89 : : };
90 : :
91 : : int
92 : 666 : RRClass_init(s_RRClass* self, PyObject* args) {
93 : : const char* s;
94 : : long i;
95 : 666 : PyObject* bytes = NULL;
96 : : // The constructor argument can be a string ("IN"), an integer (1),
97 : : // or a sequence of numbers between 0 and 65535 (wire code)
98 : :
99 : : // Note that PyArg_ParseType can set PyError, and we need to clear
100 : : // that if we try several like here. Otherwise the *next* python
101 : : // call will suddenly appear to throw an exception.
102 : : // (the way to do exceptions is to set PyErr and return -1)
103 : : try {
104 [ + - ][ + + ]: 666 : if (PyArg_ParseTuple(args, "s", &s)) {
105 [ + - ][ + - ]: 649 : self->cppobj = new RRClass(s);
[ + + ]
106 : : return (0);
107 [ + - ][ + + ]: 17 : } else if (PyArg_ParseTuple(args, "l", &i)) {
108 [ + + ]: 13 : if (i < 0 || i > 0xffff) {
109 [ + - ]: 1 : PyErr_Clear();
110 : : PyErr_SetString(PyExc_ValueError,
111 [ + - ]: 1 : "RR class number out of range");
112 : : return (-1);
113 : : }
114 [ + - ]: 12 : self->cppobj = new RRClass(i);
115 : 12 : return (0);
116 [ + - ][ + - ]: 4 : } else if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
[ + - ][ + + ]
[ + + ]
117 : : uint8_t data[2];
118 [ + - ]: 2 : int result = readDataFromSequence(data, 2, bytes);
119 [ + + ]: 2 : if (result != 0) {
120 : : return (result);
121 : : }
122 : : InputBuffer ib(data, 2);
123 [ + - ][ + - ]: 1 : self->cppobj = new RRClass(ib);
124 [ + - ]: 1 : PyErr_Clear();
125 : : return (0);
126 : : }
127 : : // Incomplete is never thrown, a type error would have already been raised
128 : : //when we try to read the 2 bytes above
129 [ - + ]: 14 : } catch (const InvalidRRClass& ic) {
130 [ - + ]: 7 : PyErr_Clear();
131 [ - + ]: 7 : PyErr_SetString(po_InvalidRRClass, ic.what());
132 : : return (-1);
133 : : }
134 : 2 : PyErr_Clear();
135 : : PyErr_SetString(PyExc_TypeError,
136 : 2 : "no valid type in constructor argument");
137 : 666 : return (-1);
138 : : }
139 : :
140 : : void
141 : 3103 : RRClass_destroy(s_RRClass* self) {
142 : 3103 : delete self->cppobj;
143 : 3103 : self->cppobj = NULL;
144 : 3103 : Py_TYPE(self)->tp_free(self);
145 : 3103 : }
146 : :
147 : : PyObject*
148 : 387 : RRClass_toText(s_RRClass* self) {
149 : : // Py_BuildValue makes python objects from native data
150 [ + - ]: 387 : return (Py_BuildValue("s", self->cppobj->toText().c_str()));
151 : : }
152 : :
153 : : PyObject*
154 : 182 : RRClass_str(PyObject* self) {
155 : : // Simply call the to_text method we already defined
156 : : return (PyObject_CallMethod(self,
157 : : const_cast<char*>("to_text"),
158 : 182 : const_cast<char*>("")));
159 : : }
160 : :
161 : : PyObject*
162 : 5 : RRClass_toWire(s_RRClass* self, PyObject* args) {
163 : : PyObject* bytes;
164 : : PyObject* mr;
165 : :
166 [ + - ][ + + ]: 5 : if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
[ + + ]
167 : 4 : PyObject* bytes_o = bytes;
168 : :
169 : 4 : OutputBuffer buffer(2);
170 [ + - ]: 4 : self->cppobj->toWire(buffer);
171 [ + - ]: 4 : PyObject* n = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()), buffer.getLength());
172 [ + - ]: 4 : PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
173 : : // We need to release the object we temporarily created here
174 : : // to prevent memory leak
175 [ + - ][ + - ]: 4 : Py_DECREF(n);
176 : : return (result);
177 [ + - ]: 1 : } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
178 : 1 : self->cppobj->toWire(PyMessageRenderer_ToMessageRenderer(mr));
179 : : // If we return NULL it is seen as an error, so use this for
180 : : // None returns
181 : 1 : Py_RETURN_NONE;
182 : : }
183 : 0 : PyErr_Clear();
184 : : PyErr_SetString(PyExc_TypeError,
185 : 0 : "toWire argument must be a sequence object or a MessageRenderer");
186 : 5 : return (NULL);
187 : : }
188 : :
189 : : PyObject*
190 : 4 : RRClass_getCode(s_RRClass* self) {
191 : 4 : return (Py_BuildValue("I", self->cppobj->getCode()));
192 : : }
193 : :
194 : : PyObject*
195 : 713 : RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op) {
196 : : bool c;
197 : :
198 : : // Check for null and if the types match. If different type,
199 : : // simply return False
200 [ + - ][ - + ]: 713 : if (!other || (self->ob_type != other->ob_type)) {
201 : 0 : Py_RETURN_FALSE;
202 : : }
203 : :
204 [ + + + + : 713 : switch (op) {
+ + - ]
205 : : case Py_LT:
206 : 4 : c = *self->cppobj < *other->cppobj;
207 : 2 : break;
208 : : case Py_LE:
209 : 2 : c = *self->cppobj < *other->cppobj ||
210 [ - + ][ # # ]: 2 : *self->cppobj == *other->cppobj;
211 : 2 : break;
212 : : case Py_EQ:
213 : 680 : c = *self->cppobj == *other->cppobj;
214 : 340 : break;
215 : : case Py_NE:
216 : 730 : c = *self->cppobj != *other->cppobj;
217 : 365 : break;
218 : : case Py_GT:
219 : 4 : c = *other->cppobj < *self->cppobj;
220 : 2 : break;
221 : : case Py_GE:
222 : 2 : c = *other->cppobj < *self->cppobj ||
223 [ + - ][ + - ]: 2 : *self->cppobj == *other->cppobj;
224 : 2 : break;
225 : : default:
226 : : PyErr_SetString(PyExc_IndexError,
227 : 0 : "Unhandled rich comparison operator");
228 : 0 : return (NULL);
229 : : }
230 [ + + ]: 713 : if (c)
231 : 349 : Py_RETURN_TRUE;
232 : : else
233 : 713 : Py_RETURN_FALSE;
234 : : }
235 : :
236 : : //
237 : : // Common function for RRClass_IN/CH/etc.
238 : : //
239 : 1272 : PyObject* RRClass_createStatic(RRClass stc) {
240 : 1272 : s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
241 [ + - ]: 1272 : if (ret != NULL) {
242 : 1272 : ret->cppobj = new RRClass(stc);
243 : : }
244 : 1272 : return (ret);
245 : : }
246 : :
247 : 1252 : PyObject* RRClass_IN(s_RRClass*) {
248 : 1252 : return (RRClass_createStatic(RRClass::IN()));
249 : : }
250 : :
251 : 17 : PyObject* RRClass_CH(s_RRClass*) {
252 : 17 : return (RRClass_createStatic(RRClass::CH()));
253 : : }
254 : :
255 : 1 : PyObject* RRClass_HS(s_RRClass*) {
256 : 1 : return (RRClass_createStatic(RRClass::HS()));
257 : : }
258 : :
259 : 1 : PyObject* RRClass_NONE(s_RRClass*) {
260 : 1 : return (RRClass_createStatic(RRClass::NONE()));
261 : : }
262 : :
263 : 1 : PyObject* RRClass_ANY(s_RRClass*) {
264 : 1 : return (RRClass_createStatic(RRClass::ANY()));
265 : : }
266 : :
267 : : } // end anonymous namespace
268 : :
269 : : namespace isc {
270 : : namespace dns {
271 : : namespace python {
272 : :
273 : : //
274 : : // Declaration of the custom exceptions
275 : : // Initialization and addition of these go in the initModulePart
276 : : // function in pydnspp.cc
277 : : //
278 : : PyObject* po_InvalidRRClass;
279 : : PyObject* po_IncompleteRRClass;
280 : :
281 : :
282 : : // This defines the complete type for reflection in python and
283 : : // parsing of PyObject* to s_RRClass
284 : : // Most of the functions are not actually implemented and NULL here.
285 : : PyTypeObject rrclass_type = {
286 : : PyVarObject_HEAD_INIT(NULL, 0)
287 : : "pydnspp.RRClass",
288 : : sizeof(s_RRClass), // tp_basicsize
289 : : 0, // tp_itemsize
290 : : (destructor)RRClass_destroy, // tp_dealloc
291 : : NULL, // tp_print
292 : : NULL, // tp_getattr
293 : : NULL, // tp_setattr
294 : : NULL, // tp_reserved
295 : : NULL, // tp_repr
296 : : NULL, // tp_as_number
297 : : NULL, // tp_as_sequence
298 : : NULL, // tp_as_mapping
299 : : NULL, // tp_hash
300 : : NULL, // tp_call
301 : : RRClass_str, // tp_str
302 : : NULL, // tp_getattro
303 : : NULL, // tp_setattro
304 : : NULL, // tp_as_buffer
305 : : Py_TPFLAGS_DEFAULT, // tp_flags
306 : : "The RRClass class encapsulates DNS resource record classes.\n"
307 : : "This class manages the 16-bit integer class codes in quite a straightforward"
308 : : "way. The only non trivial task is to handle textual representations of"
309 : : "RR classes, such as \"IN\", \"CH\", or \"CLASS65534\".",
310 : : NULL, // tp_traverse
311 : : NULL, // tp_clear
312 : : (richcmpfunc)RRClass_richcmp, // tp_richcompare
313 : : 0, // tp_weaklistoffset
314 : : NULL, // tp_iter
315 : : NULL, // tp_iternext
316 : : RRClass_methods, // tp_methods
317 : : NULL, // tp_members
318 : : NULL, // tp_getset
319 : : NULL, // tp_base
320 : : NULL, // tp_dict
321 : : NULL, // tp_descr_get
322 : : NULL, // tp_descr_set
323 : : 0, // tp_dictoffset
324 : : (initproc)RRClass_init, // tp_init
325 : : NULL, // tp_alloc
326 : : PyType_GenericNew, // tp_new
327 : : NULL, // tp_free
328 : : NULL, // tp_is_gc
329 : : NULL, // tp_bases
330 : : NULL, // tp_mro
331 : : NULL, // tp_cache
332 : : NULL, // tp_subclasses
333 : : NULL, // tp_weaklist
334 : : NULL, // tp_del
335 : : 0 // tp_version_tag
336 : : };
337 : :
338 : : PyObject*
339 : 1165 : createRRClassObject(const RRClass& source) {
340 : 1165 : RRClassContainer container(PyObject_New(s_RRClass, &rrclass_type));
341 [ + - ][ + - ]: 1165 : container.set(new RRClass(source));
342 : 1165 : return (container.release());
343 : : }
344 : :
345 : :
346 : : bool
347 : 0 : PyRRClass_Check(PyObject* obj) {
348 [ # # ]: 0 : if (obj == NULL) {
349 [ # # ][ # # ]: 0 : isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
350 : : }
351 [ # # ][ # # ]: 0 : return (PyObject_TypeCheck(obj, &rrclass_type));
352 : : }
353 : :
354 : : const RRClass&
355 : 2660 : PyRRClass_ToRRClass(const PyObject* rrclass_obj) {
356 [ - + ]: 2660 : if (rrclass_obj == NULL) {
357 [ # # ][ # # ]: 0 : isc_throw(PyCPPWrapperException,
358 : : "obj argument NULL in RRClass PyObject conversion");
359 : : }
360 : 2660 : const s_RRClass* rrclass = static_cast<const s_RRClass*>(rrclass_obj);
361 : 2660 : return (*rrclass->cppobj);
362 : : }
363 : :
364 : : } // end namespace python
365 : : } // end namespace dns
366 : 0 : } // end namespace isc
|