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 : :
17 : : #include <cassert>
18 : :
19 : : #include <dns/edns.h>
20 : : #include <dns/exceptions.h>
21 : : #include <dns/messagerenderer.h>
22 : : #include <util/python/pycppwrapper_util.h>
23 : :
24 : : #include "edns_python.h"
25 : : #include "name_python.h"
26 : : #include "rrclass_python.h"
27 : : #include "rrtype_python.h"
28 : : #include "rrttl_python.h"
29 : : #include "rdata_python.h"
30 : : #include "messagerenderer_python.h"
31 : : #include "pydnspp_common.h"
32 : :
33 : : using namespace isc::dns;
34 : : using namespace isc::dns::rdata;
35 : : using namespace isc::dns::python;
36 : : using namespace isc::util;
37 : : using namespace isc::util::python;
38 : :
39 : : namespace {
40 : :
41 : : class s_EDNS : public PyObject {
42 : : public:
43 : : EDNS* cppobj;
44 : : };
45 : :
46 : : typedef CPPPyObjectContainer<s_EDNS, EDNS> EDNSContainer;
47 : :
48 : : // General creation and destruction
49 : : int EDNS_init(s_EDNS* self, PyObject* args);
50 : : void EDNS_destroy(s_EDNS* self);
51 : :
52 : : // These are the functions we export
53 : : PyObject* EDNS_toText(const s_EDNS* self);
54 : : // This is a second version of toText, we need one where the argument
55 : : // is a PyObject*, for the str() function in python.
56 : : PyObject* EDNS_str(PyObject* self);
57 : : PyObject* EDNS_toWire(const s_EDNS* self, PyObject* args);
58 : : PyObject* EDNS_getVersion(const s_EDNS* self);
59 : : PyObject* EDNS_getDNSSECAwareness(const s_EDNS* self);
60 : : PyObject* EDNS_setDNSSECAwareness(s_EDNS* self, PyObject* args);
61 : : PyObject* EDNS_getUDPSize(const s_EDNS* self);
62 : : PyObject* EDNS_setUDPSize(s_EDNS* self, PyObject* args);
63 : : PyObject* EDNS_createFromRR(const s_EDNS* null_self, PyObject* args);
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 EDNS_methods[] = {
72 : : { "to_text", reinterpret_cast<PyCFunction>(EDNS_toText), METH_NOARGS,
73 : : "Returns the string representation" },
74 : : { "to_wire", reinterpret_cast<PyCFunction>(EDNS_toWire), METH_VARARGS,
75 : : "Converts the EDNS 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_version",
82 : : reinterpret_cast<PyCFunction>(EDNS_getVersion), METH_NOARGS,
83 : : "Returns the version of EDNS." },
84 : : { "get_dnssec_awareness",
85 : : reinterpret_cast<PyCFunction>(EDNS_getDNSSECAwareness), METH_NOARGS,
86 : : "Returns whether the message sender is DNSSEC aware." },
87 : : { "set_dnssec_awareness",
88 : : reinterpret_cast<PyCFunction>(EDNS_setDNSSECAwareness), METH_VARARGS,
89 : : "Specifies whether the sender of the message containing this "
90 : : "EDNS is DNSSEC aware." },
91 : : { "get_udp_size",
92 : : reinterpret_cast<PyCFunction>(EDNS_getUDPSize), METH_NOARGS,
93 : : "Return the maximum buffer size of UDP messages for the sender "
94 : : "of the message." },
95 : : { "set_udp_size",
96 : : reinterpret_cast<PyCFunction>(EDNS_setUDPSize), METH_VARARGS,
97 : : "Specify the maximum buffer size of UDP messages that use this EDNS." },
98 : : { "create_from_rr",
99 : : reinterpret_cast<PyCFunction>(EDNS_createFromRR),
100 : : METH_VARARGS | METH_STATIC,
101 : : "Create a new EDNS object from a set of RR parameters, also providing "
102 : : "the extended RCODE value." },
103 : : { NULL, NULL, 0, NULL }
104 : : };
105 : :
106 : : EDNS*
107 : 21 : createFromRR(const Name& name, const RRClass& rrclass, const RRType& rrtype,
108 : : const RRTTL& rrttl, const Rdata& rdata, uint8_t& extended_rcode)
109 : : {
110 : : try {
111 : : return (createEDNSFromRR(name, rrclass, rrtype, rrttl, rdata,
112 [ + + ]: 25 : extended_rcode));
113 : 2 : } catch (const isc::InvalidParameter& ex) {
114 [ - + ]: 1 : PyErr_SetString(po_InvalidParameter, ex.what());
115 : 2 : } catch (const DNSMessageFORMERR& ex) {
116 [ - + ]: 1 : PyErr_SetString(po_DNSMessageFORMERR, ex.what());
117 : 4 : } catch (const DNSMessageBADVERS& ex) {
118 [ - + ]: 2 : PyErr_SetString(po_DNSMessageBADVERS, ex.what());
119 [ + + + - ]: 4 : } catch (...) {
120 [ # # ]: 0 : PyErr_SetString(po_IscException, "Unexpected exception");
121 : : }
122 : :
123 : : return (NULL);
124 : : }
125 : : int
126 : 28 : EDNS_init(s_EDNS* self, PyObject* args) {
127 : 28 : uint8_t version = EDNS::SUPPORTED_VERSION;
128 : : const PyObject* name;
129 : : const PyObject* rrclass;
130 : : const PyObject* rrtype;
131 : : const PyObject* rrttl;
132 : : const PyObject* rdata;
133 : :
134 [ + + ]: 28 : if (PyArg_ParseTuple(args, "|b", &version)) {
135 : : try {
136 [ + - ][ + + ]: 8 : self->cppobj = new EDNS(version);
137 : 2 : } catch (const isc::InvalidParameter& ex) {
138 [ - + ]: 1 : PyErr_SetString(po_InvalidParameter, ex.what());
139 : : return (-1);
140 [ + - ]: 1 : } catch (...) {
141 [ # # ]: 0 : PyErr_SetString(po_IscException, "Unexpected exception");
142 : : return (-1);
143 : : }
144 : 7 : return (0);
145 [ + + ]: 20 : } else if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
146 : : &rrclass_type, &rrclass, &rrtype_type, &rrtype,
147 : 20 : &rrttl_type, &rrttl, &rdata_type, &rdata)) {
148 : : // We use createFromRR() even if we don't need to know extended_rcode
149 : : // in this context so that we can share the try-catch logic with
150 : : // EDNS_createFromRR() (see below).
151 : : uint8_t extended_rcode;
152 : 18 : self->cppobj = createFromRR(PyName_ToName(name),
153 : 18 : PyRRClass_ToRRClass(rrclass),
154 : 18 : PyRRType_ToRRType(rrtype),
155 : 18 : PyRRTTL_ToRRTTL(rrttl),
156 : 36 : PyRdata_ToRdata(rdata), extended_rcode);
157 [ + + ]: 18 : return (self->cppobj != NULL ? 0 : -1);
158 : : }
159 : :
160 : 2 : PyErr_Clear();
161 : 2 : PyErr_SetString(PyExc_TypeError, "Invalid arguments to EDNS constructor");
162 : :
163 : 28 : return (-1);
164 : : }
165 : :
166 : : void
167 : 31 : EDNS_destroy(s_EDNS* const self) {
168 : 31 : delete self->cppobj;
169 : 31 : self->cppobj = NULL;
170 : 31 : Py_TYPE(self)->tp_free(self);
171 : 31 : }
172 : :
173 : : PyObject*
174 : 5 : EDNS_toText(const s_EDNS* const self) {
175 : : // Py_BuildValue makes python objects from native data
176 [ + - ]: 5 : return (Py_BuildValue("s", self->cppobj->toText().c_str()));
177 : : }
178 : :
179 : : PyObject*
180 : 1 : EDNS_str(PyObject* self) {
181 : : // Simply call the to_text method we already defined
182 : : return (PyObject_CallMethod(self,
183 : : const_cast<char*>("to_text"),
184 : 1 : const_cast<char*>("")));
185 : : }
186 : :
187 : : PyObject*
188 : 7 : EDNS_toWire(const s_EDNS* const self, PyObject* args) {
189 : : PyObject* bytes;
190 : : uint8_t extended_rcode;
191 : : PyObject* renderer;
192 : :
193 [ + - + + ]: 14 : if (PyArg_ParseTuple(args, "Ob", &bytes, &extended_rcode) &&
[ + + ]
194 : 7 : PySequence_Check(bytes)) {
195 : 1 : PyObject* bytes_o = bytes;
196 : :
197 : 1 : OutputBuffer buffer(0);
198 [ + - ]: 1 : self->cppobj->toWire(buffer, extended_rcode);
199 : : PyObject* rd_bytes = PyBytes_FromStringAndSize(
200 [ + - ]: 1 : static_cast<const char*>(buffer.getData()), buffer.getLength());
201 [ + - ]: 1 : PyObject* result = PySequence_InPlaceConcat(bytes_o, rd_bytes);
202 : : // We need to release the object we temporarily created here
203 : : // to prevent memory leak
204 [ - + ][ # # ]: 1 : Py_DECREF(rd_bytes);
205 : : return (result);
206 [ + - ]: 6 : } else if (PyArg_ParseTuple(args, "O!b", &messagerenderer_type,
207 : 6 : &renderer, &extended_rcode)) {
208 : : const unsigned int n = self->cppobj->toWire(
209 : 6 : PyMessageRenderer_ToMessageRenderer(renderer), extended_rcode);
210 : :
211 : 6 : return (Py_BuildValue("I", n));
212 : : }
213 : 0 : PyErr_Clear();
214 : 0 : PyErr_SetString(PyExc_TypeError, "Incorrect arguments for EDNS.to_wire()");
215 : 7 : return (NULL);
216 : : }
217 : :
218 : : PyObject*
219 : 3 : EDNS_getVersion(const s_EDNS* const self) {
220 : 3 : return (Py_BuildValue("B", self->cppobj->getVersion()));
221 : : }
222 : :
223 : : PyObject*
224 : 7 : EDNS_getDNSSECAwareness(const s_EDNS* const self) {
225 [ + + ]: 7 : if (self->cppobj->getDNSSECAwareness()) {
226 : 4 : Py_RETURN_TRUE;
227 : : } else {
228 : 7 : Py_RETURN_FALSE;
229 : : }
230 : : }
231 : :
232 : : PyObject*
233 : 9 : EDNS_setDNSSECAwareness(s_EDNS* self, PyObject* args) {
234 : : const PyObject *b;
235 [ + + ]: 9 : if (!PyArg_ParseTuple(args, "O!", &PyBool_Type, &b)) {
236 : : return (NULL);
237 : : }
238 : 7 : self->cppobj->setDNSSECAwareness(b == Py_True);
239 : 9 : Py_RETURN_NONE;
240 : : }
241 : :
242 : : PyObject*
243 : 7 : EDNS_getUDPSize(const s_EDNS* const self) {
244 : 7 : return (Py_BuildValue("I", self->cppobj->getUDPSize()));
245 : : }
246 : :
247 : : PyObject*
248 : 10 : EDNS_setUDPSize(s_EDNS* self, PyObject* args) {
249 : : long size;
250 [ + + ]: 10 : if (!PyArg_ParseTuple(args, "l", &size)) {
251 : 1 : PyErr_Clear();
252 : : PyErr_SetString(PyExc_TypeError,
253 : 1 : "No valid type in set_udp_size argument");
254 : 1 : return (NULL);
255 : : }
256 [ + + ]: 9 : if (size < 0 || size > 0xffff) {
257 : : PyErr_SetString(PyExc_ValueError,
258 : 2 : "UDP size is not an unsigned 16-bit integer");
259 : 2 : return (NULL);
260 : : }
261 : 7 : self->cppobj->setUDPSize(size);
262 : 10 : Py_RETURN_NONE;
263 : : }
264 : :
265 : : PyObject*
266 : 3 : EDNS_createFromRR(const s_EDNS* null_self, PyObject* args) {
267 : : const PyObject* name;
268 : : const PyObject* rrclass;
269 : : const PyObject* rrtype;
270 : : const PyObject* rrttl;
271 : : const PyObject* rdata;
272 : 3 : s_EDNS* edns_obj = NULL;
273 : :
274 [ - + ]: 3 : assert(null_self == NULL);
275 : :
276 [ + - ]: 3 : if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
277 : : &rrclass_type, &rrclass, &rrtype_type, &rrtype,
278 : 3 : &rrttl_type, &rrttl, &rdata_type, &rdata)) {
279 : : uint8_t extended_rcode;
280 : 3 : edns_obj = PyObject_New(s_EDNS, &edns_type);
281 [ + - ]: 3 : if (edns_obj == NULL) {
282 : : return (NULL);
283 : : }
284 : :
285 : 3 : edns_obj->cppobj = createFromRR(PyName_ToName(name),
286 : 3 : PyRRClass_ToRRClass(rrclass),
287 : 3 : PyRRType_ToRRType(rrtype),
288 : 3 : PyRRTTL_ToRRTTL(rrttl),
289 : 3 : PyRdata_ToRdata(rdata),
290 : 3 : extended_rcode);
291 [ + + ]: 3 : if (edns_obj->cppobj != NULL) {
292 : 2 : PyObject* extrcode_obj = Py_BuildValue("B", extended_rcode);
293 : 2 : return (Py_BuildValue("OO", edns_obj, extrcode_obj));
294 : : }
295 : :
296 [ + - ]: 1 : Py_DECREF(edns_obj);
297 : : return (NULL);
298 : : }
299 : :
300 : 0 : PyErr_Clear();
301 : : PyErr_SetString(PyExc_TypeError,
302 : 0 : "Incorrect arguments for EDNS.create_from_rr()");
303 : 3 : return (NULL);
304 : : }
305 : :
306 : : } // end of anonymous namespace
307 : :
308 : : namespace isc {
309 : : namespace dns {
310 : : namespace python {
311 : :
312 : : // This defines the complete type for reflection in python and
313 : : // parsing of PyObject* to s_EDNS
314 : : // Most of the functions are not actually implemented and NULL here.
315 : : PyTypeObject edns_type = {
316 : : PyVarObject_HEAD_INIT(NULL, 0)
317 : : "pydnspp.EDNS",
318 : : sizeof(s_EDNS), // tp_basicsize
319 : : 0, // tp_itemsize
320 : : (destructor)EDNS_destroy, // tp_dealloc
321 : : NULL, // tp_print
322 : : NULL, // tp_getattr
323 : : NULL, // tp_setattr
324 : : NULL, // tp_reserved
325 : : NULL, // tp_repr
326 : : NULL, // tp_as_number
327 : : NULL, // tp_as_sequence
328 : : NULL, // tp_as_mapping
329 : : NULL, // tp_hash
330 : : NULL, // tp_call
331 : : EDNS_str, // tp_str
332 : : NULL, // tp_getattro
333 : : NULL, // tp_setattro
334 : : NULL, // tp_as_buffer
335 : : Py_TPFLAGS_DEFAULT, // tp_flags
336 : : "The EDNS class encapsulates DNS extensions "
337 : : "provided by the EDNSx protocol.",
338 : : NULL, // tp_traverse
339 : : NULL, // tp_clear
340 : : NULL, // tp_richcompare
341 : : 0, // tp_weaklistoffset
342 : : NULL, // tp_iter
343 : : NULL, // tp_iternext
344 : : EDNS_methods, // tp_methods
345 : : NULL, // tp_members
346 : : NULL, // tp_getset
347 : : NULL, // tp_base
348 : : NULL, // tp_dict
349 : : NULL, // tp_descr_get
350 : : NULL, // tp_descr_set
351 : : 0, // tp_dictoffset
352 : : (initproc)EDNS_init, // tp_init
353 : : NULL, // tp_alloc
354 : : PyType_GenericNew, // tp_new
355 : : NULL, // tp_free
356 : : NULL, // tp_is_gc
357 : : NULL, // tp_bases
358 : : NULL, // tp_mro
359 : : NULL, // tp_cache
360 : : NULL, // tp_subclasses
361 : : NULL, // tp_weaklist
362 : : NULL, // tp_del
363 : : 0 // tp_version_tag
364 : : };
365 : :
366 : : PyObject*
367 : 2 : createEDNSObject(const EDNS& source) {
368 : 2 : EDNSContainer container(PyObject_New(s_EDNS, &edns_type));
369 [ + - ][ + - ]: 2 : container.set(new EDNS(source));
370 : 2 : return (container.release());
371 : : }
372 : :
373 : : bool
374 : 0 : PyEDNS_Check(PyObject* obj) {
375 [ # # ]: 0 : if (obj == NULL) {
376 [ # # ][ # # ]: 0 : isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
377 : : }
378 [ # # ][ # # ]: 0 : return (PyObject_TypeCheck(obj, &edns_type));
379 : : }
380 : :
381 : : const EDNS&
382 : 3 : PyEDNS_ToEDNS(const PyObject* edns_obj) {
383 [ - + ]: 3 : if (edns_obj == NULL) {
384 [ # # ][ # # ]: 0 : isc_throw(PyCPPWrapperException,
385 : : "obj argument NULL in EDNS PyObject conversion");
386 : : }
387 : 3 : const s_EDNS* edns = static_cast<const s_EDNS*>(edns_obj);
388 : 3 : return (*edns->cppobj);
389 : : }
390 : :
391 : : } // end namespace python
392 : : } // end namespace dns
393 : 17 : } // end namespace isc
|