edns_python.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <cassert>
  15. #include <dns/edns.h>
  16. using namespace isc::dns;
  17. using namespace isc::util;
  18. using namespace isc::dns::rdata;
  19. //
  20. // Definition of the classes
  21. //
  22. // For each class, we need a struct, a helper functions (init, destroy,
  23. // and static wrappers around the methods we export), a list of methods,
  24. // and a type description
  25. namespace {
  26. //
  27. // EDNS
  28. //
  29. // The s_* Class simply covers one instantiation of the object
  30. class s_EDNS : public PyObject {
  31. public:
  32. EDNS* edns;
  33. };
  34. //
  35. // We declare the functions here, the definitions are below
  36. // the type definition of the object, since both can use the other
  37. //
  38. // General creation and destruction
  39. int EDNS_init(s_EDNS* self, PyObject* args);
  40. void EDNS_destroy(s_EDNS* self);
  41. // These are the functions we export
  42. PyObject* EDNS_toText(const s_EDNS* self);
  43. // This is a second version of toText, we need one where the argument
  44. // is a PyObject*, for the str() function in python.
  45. PyObject* EDNS_str(PyObject* self);
  46. PyObject* EDNS_toWire(const s_EDNS* self, PyObject* args);
  47. PyObject* EDNS_getVersion(const s_EDNS* self);
  48. PyObject* EDNS_getDNSSECAwareness(const s_EDNS* self);
  49. PyObject* EDNS_setDNSSECAwareness(s_EDNS* self, PyObject* args);
  50. PyObject* EDNS_getUDPSize(const s_EDNS* self);
  51. PyObject* EDNS_setUDPSize(s_EDNS* self, PyObject* args);
  52. PyObject* EDNS_createFromRR(const s_EDNS* null_self, PyObject* args);
  53. // This list contains the actual set of functions we have in
  54. // python. Each entry has
  55. // 1. Python method name
  56. // 2. Our static function here
  57. // 3. Argument type
  58. // 4. Documentation
  59. PyMethodDef EDNS_methods[] = {
  60. { "to_text", reinterpret_cast<PyCFunction>(EDNS_toText), METH_NOARGS,
  61. "Returns the string representation" },
  62. { "to_wire", reinterpret_cast<PyCFunction>(EDNS_toWire), METH_VARARGS,
  63. "Converts the EDNS object to wire format.\n"
  64. "The argument can be either a MessageRenderer or an object that "
  65. "implements the sequence interface. If the object is mutable "
  66. "(for instance a bytearray()), the wire data is added in-place.\n"
  67. "If it is not (for instance a bytes() object), a new object is "
  68. "returned" },
  69. { "get_version",
  70. reinterpret_cast<PyCFunction>(EDNS_getVersion), METH_NOARGS,
  71. "Returns the version of EDNS." },
  72. { "get_dnssec_awareness",
  73. reinterpret_cast<PyCFunction>(EDNS_getDNSSECAwareness), METH_NOARGS,
  74. "Returns whether the message sender is DNSSEC aware." },
  75. { "set_dnssec_awareness",
  76. reinterpret_cast<PyCFunction>(EDNS_setDNSSECAwareness), METH_VARARGS,
  77. "Specifies whether the sender of the message containing this "
  78. "EDNS is DNSSEC aware." },
  79. { "get_udp_size",
  80. reinterpret_cast<PyCFunction>(EDNS_getUDPSize), METH_NOARGS,
  81. "Return the maximum buffer size of UDP messages for the sender "
  82. "of the message." },
  83. { "set_udp_size",
  84. reinterpret_cast<PyCFunction>(EDNS_setUDPSize), METH_VARARGS,
  85. "Specify the maximum buffer size of UDP messages that use this EDNS." },
  86. { "create_from_rr",
  87. reinterpret_cast<PyCFunction>(EDNS_createFromRR),
  88. METH_VARARGS | METH_STATIC,
  89. "Create a new EDNS object from a set of RR parameters, also providing "
  90. "the extended RCODE value." },
  91. { NULL, NULL, 0, NULL }
  92. };
  93. // This defines the complete type for reflection in python and
  94. // parsing of PyObject* to s_EDNS
  95. // Most of the functions are not actually implemented and NULL here.
  96. PyTypeObject edns_type = {
  97. PyVarObject_HEAD_INIT(NULL, 0)
  98. "pydnspp.EDNS",
  99. sizeof(s_EDNS), // tp_basicsize
  100. 0, // tp_itemsize
  101. (destructor)EDNS_destroy, // tp_dealloc
  102. NULL, // tp_print
  103. NULL, // tp_getattr
  104. NULL, // tp_setattr
  105. NULL, // tp_reserved
  106. NULL, // tp_repr
  107. NULL, // tp_as_number
  108. NULL, // tp_as_sequence
  109. NULL, // tp_as_mapping
  110. NULL, // tp_hash
  111. NULL, // tp_call
  112. EDNS_str, // tp_str
  113. NULL, // tp_getattro
  114. NULL, // tp_setattro
  115. NULL, // tp_as_buffer
  116. Py_TPFLAGS_DEFAULT, // tp_flags
  117. "The EDNS class encapsulates DNS extensions "
  118. "provided by the EDNSx protocol.",
  119. NULL, // tp_traverse
  120. NULL, // tp_clear
  121. NULL, // tp_richcompare
  122. 0, // tp_weaklistoffset
  123. NULL, // tp_iter
  124. NULL, // tp_iternext
  125. EDNS_methods, // tp_methods
  126. NULL, // tp_members
  127. NULL, // tp_getset
  128. NULL, // tp_base
  129. NULL, // tp_dict
  130. NULL, // tp_descr_get
  131. NULL, // tp_descr_set
  132. 0, // tp_dictoffset
  133. (initproc)EDNS_init, // tp_init
  134. NULL, // tp_alloc
  135. PyType_GenericNew, // tp_new
  136. NULL, // tp_free
  137. NULL, // tp_is_gc
  138. NULL, // tp_bases
  139. NULL, // tp_mro
  140. NULL, // tp_cache
  141. NULL, // tp_subclasses
  142. NULL, // tp_weaklist
  143. NULL, // tp_del
  144. 0 // tp_version_tag
  145. };
  146. EDNS*
  147. createFromRR(const Name& name, const RRClass& rrclass, const RRType& rrtype,
  148. const RRTTL& rrttl, const Rdata& rdata, uint8_t& extended_rcode)
  149. {
  150. try {
  151. return (createEDNSFromRR(name, rrclass, rrtype, rrttl, rdata,
  152. extended_rcode));
  153. } catch (const isc::InvalidParameter& ex) {
  154. PyErr_SetString(po_InvalidParameter, ex.what());
  155. } catch (const DNSMessageFORMERR& ex) {
  156. PyErr_SetString(po_DNSMessageFORMERR, ex.what());
  157. } catch (const DNSMessageBADVERS& ex) {
  158. PyErr_SetString(po_DNSMessageBADVERS, ex.what());
  159. } catch (...) {
  160. PyErr_SetString(po_IscException, "Unexpected exception");
  161. }
  162. return (NULL);
  163. }
  164. int
  165. EDNS_init(s_EDNS* self, PyObject* args) {
  166. uint8_t version = EDNS::SUPPORTED_VERSION;
  167. const s_Name* name;
  168. const s_RRClass* rrclass;
  169. const s_RRType* rrtype;
  170. const s_RRTTL* rrttl;
  171. const s_Rdata* rdata;
  172. if (PyArg_ParseTuple(args, "|b", &version)) {
  173. try {
  174. self->edns = new EDNS(version);
  175. } catch (const isc::InvalidParameter& ex) {
  176. PyErr_SetString(po_InvalidParameter, ex.what());
  177. return (-1);
  178. } catch (...) {
  179. PyErr_SetString(po_IscException, "Unexpected exception");
  180. return (-1);
  181. }
  182. return (0);
  183. } else if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
  184. &rrclass_type, &rrclass, &rrtype_type, &rrtype,
  185. &rrttl_type, &rrttl, &rdata_type, &rdata)) {
  186. // We use createFromRR() even if we don't need to know extended_rcode
  187. // in this context so that we can share the try-catch logic with
  188. // EDNS_createFromRR() (see below).
  189. uint8_t extended_rcode;
  190. self->edns = createFromRR(*name->cppobj, *rrclass->rrclass,
  191. *rrtype->rrtype, *rrttl->rrttl,
  192. *rdata->rdata, extended_rcode);
  193. return (self->edns != NULL ? 0 : -1);
  194. }
  195. PyErr_Clear();
  196. PyErr_SetString(PyExc_TypeError, "Invalid arguments to EDNS constructor");
  197. return (-1);
  198. }
  199. void
  200. EDNS_destroy(s_EDNS* const self) {
  201. delete self->edns;
  202. self->edns = NULL;
  203. Py_TYPE(self)->tp_free(self);
  204. }
  205. PyObject*
  206. EDNS_toText(const s_EDNS* const self) {
  207. // Py_BuildValue makes python objects from native data
  208. return (Py_BuildValue("s", self->edns->toText().c_str()));
  209. }
  210. PyObject*
  211. EDNS_str(PyObject* const self) {
  212. // Simply call the to_text method we already defined
  213. return (PyObject_CallMethod(self,
  214. const_cast<char*>("to_text"),
  215. const_cast<char*>("")));
  216. }
  217. PyObject*
  218. EDNS_toWire(const s_EDNS* const self, PyObject* args) {
  219. PyObject* bytes;
  220. uint8_t extended_rcode;
  221. s_MessageRenderer* renderer;
  222. if (PyArg_ParseTuple(args, "Ob", &bytes, &extended_rcode) &&
  223. PySequence_Check(bytes)) {
  224. PyObject* bytes_o = bytes;
  225. OutputBuffer buffer(0);
  226. self->edns->toWire(buffer, extended_rcode);
  227. PyObject* rd_bytes = PyBytes_FromStringAndSize(
  228. static_cast<const char*>(buffer.getData()), buffer.getLength());
  229. PyObject* result = PySequence_InPlaceConcat(bytes_o, rd_bytes);
  230. // We need to release the object we temporarily created here
  231. // to prevent memory leak
  232. Py_DECREF(rd_bytes);
  233. return (result);
  234. } else if (PyArg_ParseTuple(args, "O!b", &messagerenderer_type,
  235. &renderer, &extended_rcode)) {
  236. const unsigned int n = self->edns->toWire(*renderer->messagerenderer,
  237. extended_rcode);
  238. return (Py_BuildValue("I", n));
  239. }
  240. PyErr_Clear();
  241. PyErr_SetString(PyExc_TypeError, "Incorrect arguments for EDNS.to_wire()");
  242. return (NULL);
  243. }
  244. PyObject*
  245. EDNS_getVersion(const s_EDNS* const self) {
  246. return (Py_BuildValue("B", self->edns->getVersion()));
  247. }
  248. PyObject*
  249. EDNS_getDNSSECAwareness(const s_EDNS* const self) {
  250. if (self->edns->getDNSSECAwareness()) {
  251. Py_RETURN_TRUE;
  252. } else {
  253. Py_RETURN_FALSE;
  254. }
  255. }
  256. PyObject*
  257. EDNS_setDNSSECAwareness(s_EDNS* self, PyObject* args) {
  258. const PyObject *b;
  259. if (!PyArg_ParseTuple(args, "O!", &PyBool_Type, &b)) {
  260. return (NULL);
  261. }
  262. self->edns->setDNSSECAwareness(b == Py_True);
  263. Py_RETURN_NONE;
  264. }
  265. PyObject*
  266. EDNS_getUDPSize(const s_EDNS* const self) {
  267. return (Py_BuildValue("I", self->edns->getUDPSize()));
  268. }
  269. PyObject*
  270. EDNS_setUDPSize(s_EDNS* self, PyObject* args) {
  271. long size;
  272. if (!PyArg_ParseTuple(args, "l", &size)) {
  273. PyErr_Clear();
  274. PyErr_SetString(PyExc_TypeError,
  275. "No valid type in set_udp_size argument");
  276. return (NULL);
  277. }
  278. if (size < 0 || size > 0xffff) {
  279. PyErr_SetString(PyExc_ValueError,
  280. "UDP size is not an unsigned 16-bit integer");
  281. return (NULL);
  282. }
  283. self->edns->setUDPSize(size);
  284. Py_RETURN_NONE;
  285. }
  286. PyObject*
  287. EDNS_createFromRR(const s_EDNS* null_self, PyObject* args) {
  288. const s_Name* name;
  289. const s_RRClass* rrclass;
  290. const s_RRType* rrtype;
  291. const s_RRTTL* rrttl;
  292. const s_Rdata* rdata;
  293. s_EDNS* edns_obj = NULL;
  294. assert(null_self == NULL);
  295. if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
  296. &rrclass_type, &rrclass, &rrtype_type, &rrtype,
  297. &rrttl_type, &rrttl, &rdata_type, &rdata)) {
  298. uint8_t extended_rcode;
  299. edns_obj = PyObject_New(s_EDNS, &edns_type);
  300. if (edns_obj == NULL) {
  301. return (NULL);
  302. }
  303. edns_obj->edns = createFromRR(*name->cppobj, *rrclass->rrclass,
  304. *rrtype->rrtype, *rrttl->rrttl,
  305. *rdata->rdata, extended_rcode);
  306. if (edns_obj->edns != NULL) {
  307. PyObject* extrcode_obj = Py_BuildValue("B", extended_rcode);
  308. return (Py_BuildValue("OO", edns_obj, extrcode_obj));
  309. }
  310. Py_DECREF(edns_obj);
  311. return (NULL);
  312. }
  313. PyErr_Clear();
  314. PyErr_SetString(PyExc_TypeError,
  315. "Incorrect arguments for EDNS.create_from_rr()");
  316. return (NULL);
  317. }
  318. } // end of anonymous namespace
  319. // end of EDNS
  320. // Module Initialization, all statics are initialized here
  321. bool
  322. initModulePart_EDNS(PyObject* mod) {
  323. // We initialize the static description object with PyType_Ready(),
  324. // then add it to the module. This is not just a check! (leaving
  325. // this out results in segmentation faults)
  326. if (PyType_Ready(&edns_type) < 0) {
  327. return (false);
  328. }
  329. Py_INCREF(&edns_type);
  330. void* p = &edns_type;
  331. PyModule_AddObject(mod, "EDNS", static_cast<PyObject*>(p));
  332. addClassVariable(edns_type, "SUPPORTED_VERSION",
  333. Py_BuildValue("B", EDNS::SUPPORTED_VERSION));
  334. return (true);
  335. }