edns_python.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. // $Id$
  15. #include <cassert>
  16. #include <dns/edns.h>
  17. using namespace isc::dns;
  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_isDNSSECSupported(const s_EDNS* self);
  49. PyObject* EDNS_setDNSSECSupported(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. { "is_dnssec_supported",
  73. reinterpret_cast<PyCFunction>(EDNS_isDNSSECSupported), METH_NOARGS,
  74. "Returns True if the message sender indicates DNSSEC is supported. "
  75. "If EDNS is included, this corresponds to the value of the DO bit. "
  76. "Otherwise, DNSSEC is considered not supported." },
  77. { "set_dnssec_supported",
  78. reinterpret_cast<PyCFunction>(EDNS_setDNSSECSupported), METH_VARARGS,
  79. "Specify whether DNSSEC is supported in the message." },
  80. { "get_udp_size",
  81. reinterpret_cast<PyCFunction>(EDNS_getUDPSize), METH_NOARGS,
  82. "Return the maximum buffer size of UDP messages for the sender "
  83. "of the message." },
  84. { "set_udp_size",
  85. reinterpret_cast<PyCFunction>(EDNS_setUDPSize), METH_VARARGS,
  86. "Specify the maximum buffer size of UDP messages that use this EDNS." },
  87. { "create_from_rr",
  88. reinterpret_cast<PyCFunction>(EDNS_createFromRR),
  89. METH_VARARGS | METH_STATIC,
  90. "Create a new EDNS object from a set of RR parameters, also providing "
  91. "the extended RCODE value." },
  92. { NULL, NULL, 0, NULL }
  93. };
  94. // This defines the complete type for reflection in python and
  95. // parsing of PyObject* to s_EDNS
  96. // Most of the functions are not actually implemented and NULL here.
  97. PyTypeObject edns_type = {
  98. PyVarObject_HEAD_INIT(NULL, 0)
  99. "libdns_python.EDNS",
  100. sizeof(s_EDNS), // tp_basicsize
  101. 0, // tp_itemsize
  102. (destructor)EDNS_destroy, // tp_dealloc
  103. NULL, // tp_print
  104. NULL, // tp_getattr
  105. NULL, // tp_setattr
  106. NULL, // tp_reserved
  107. NULL, // tp_repr
  108. NULL, // tp_as_number
  109. NULL, // tp_as_sequence
  110. NULL, // tp_as_mapping
  111. NULL, // tp_hash
  112. NULL, // tp_call
  113. EDNS_str, // tp_str
  114. NULL, // tp_getattro
  115. NULL, // tp_setattro
  116. NULL, // tp_as_buffer
  117. Py_TPFLAGS_DEFAULT, // tp_flags
  118. "The EDNS class encapsulates DNS extensions "
  119. "provided by the EDNSx protocol.",
  120. NULL, // tp_traverse
  121. NULL, // tp_clear
  122. NULL, // tp_richcompare
  123. 0, // tp_weaklistoffset
  124. NULL, // tp_iter
  125. NULL, // tp_iternext
  126. EDNS_methods, // tp_methods
  127. NULL, // tp_members
  128. NULL, // tp_getset
  129. NULL, // tp_base
  130. NULL, // tp_dict
  131. NULL, // tp_descr_get
  132. NULL, // tp_descr_set
  133. 0, // tp_dictoffset
  134. (initproc)EDNS_init, // tp_init
  135. NULL, // tp_alloc
  136. PyType_GenericNew, // tp_new
  137. NULL, // tp_free
  138. NULL, // tp_is_gc
  139. NULL, // tp_bases
  140. NULL, // tp_mro
  141. NULL, // tp_cache
  142. NULL, // tp_subclasses
  143. NULL, // tp_weaklist
  144. NULL, // tp_del
  145. 0 // tp_version_tag
  146. };
  147. EDNS*
  148. createFromRR(const Name& name, const RRClass& rrclass, const RRType& rrtype,
  149. const RRTTL& rrttl, const Rdata& rdata, uint8_t& extended_rcode)
  150. {
  151. try {
  152. return (createEDNSFromRR(name, rrclass, rrtype, rrttl, rdata,
  153. extended_rcode));
  154. } catch (const isc::InvalidParameter& ex) {
  155. PyErr_SetString(po_InvalidParameter, ex.what());
  156. } catch (const DNSMessageFORMERR& ex) {
  157. PyErr_SetString(po_DNSMessageFORMERR, ex.what());
  158. } catch (const DNSMessageBADVERS& ex) {
  159. PyErr_SetString(po_DNSMessageBADVERS, ex.what());
  160. } catch (...) {
  161. PyErr_SetString(po_IscException, "Unexpected exception");
  162. }
  163. return (NULL);
  164. }
  165. int
  166. EDNS_init(s_EDNS* self, PyObject* args) {
  167. uint8_t version = EDNS::SUPPORTED_VERSION;
  168. const s_Name* name;
  169. const s_RRClass* rrclass;
  170. const s_RRType* rrtype;
  171. const s_RRTTL* rrttl;
  172. const s_Rdata* rdata;
  173. if (PyArg_ParseTuple(args, "|b", &version)) {
  174. try {
  175. self->edns = new EDNS(version);
  176. } catch (const isc::InvalidParameter& ex) {
  177. PyErr_SetString(po_InvalidParameter, ex.what());
  178. return (-1);
  179. } catch (...) {
  180. PyErr_SetString(po_IscException, "Unexpected exception");
  181. return (-1);
  182. }
  183. return (0);
  184. } else if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
  185. &rrclass_type, &rrclass, &rrtype_type, &rrtype,
  186. &rrttl_type, &rrttl, &rdata_type, &rdata)) {
  187. // We use createFromRR() even if we don't need to know extended_rcode
  188. // in this context so that we can share the try-catch logic with
  189. // EDNS_createFromRR() (see below).
  190. uint8_t extended_rcode;
  191. self->edns = createFromRR(*name->name, *rrclass->rrclass,
  192. *rrtype->rrtype, *rrttl->rrttl,
  193. *rdata->rdata, extended_rcode);
  194. return (self->edns != NULL ? 0 : -1);
  195. }
  196. PyErr_Clear();
  197. PyErr_SetString(PyExc_TypeError, "Invalid arguments to EDNS constructor");
  198. return (-1);
  199. }
  200. void
  201. EDNS_destroy(s_EDNS* const self) {
  202. delete self->edns;
  203. self->edns = NULL;
  204. Py_TYPE(self)->tp_free(self);
  205. }
  206. PyObject*
  207. EDNS_toText(const s_EDNS* const self) {
  208. // Py_BuildValue makes python objects from native data
  209. return (Py_BuildValue("s", self->edns->toText().c_str()));
  210. }
  211. PyObject*
  212. EDNS_str(PyObject* const self) {
  213. // Simply call the to_text method we already defined
  214. return (PyObject_CallMethod(self,
  215. const_cast<char*>("to_text"),
  216. const_cast<char*>("")));
  217. }
  218. PyObject*
  219. EDNS_toWire(const s_EDNS* const self, PyObject* args) {
  220. PyObject* bytes;
  221. uint8_t extended_rcode;
  222. s_MessageRenderer* renderer;
  223. if (PyArg_ParseTuple(args, "Ob", &bytes, &extended_rcode) &&
  224. PySequence_Check(bytes)) {
  225. PyObject* bytes_o = bytes;
  226. OutputBuffer buffer(0);
  227. self->edns->toWire(buffer, extended_rcode);
  228. PyObject* rd_bytes = PyBytes_FromStringAndSize(
  229. static_cast<const char*>(buffer.getData()), buffer.getLength());
  230. PyObject* result = PySequence_InPlaceConcat(bytes_o, rd_bytes);
  231. // We need to release the object we temporarily created here
  232. // to prevent memory leak
  233. Py_DECREF(rd_bytes);
  234. return (result);
  235. } else if (PyArg_ParseTuple(args, "O!b", &messagerenderer_type,
  236. &renderer, &extended_rcode)) {
  237. const unsigned int n = self->edns->toWire(*renderer->messagerenderer,
  238. extended_rcode);
  239. return (Py_BuildValue("I", n));
  240. }
  241. PyErr_Clear();
  242. PyErr_SetString(PyExc_TypeError, "Incorrect arguments for EDNS.to_wire()");
  243. return (NULL);
  244. }
  245. PyObject*
  246. EDNS_getVersion(const s_EDNS* const self) {
  247. return (Py_BuildValue("B", self->edns->getVersion()));
  248. }
  249. PyObject*
  250. EDNS_isDNSSECSupported(const s_EDNS* const self) {
  251. if (self->edns->isDNSSECSupported()) {
  252. Py_RETURN_TRUE;
  253. } else {
  254. Py_RETURN_FALSE;
  255. }
  256. }
  257. PyObject*
  258. EDNS_setDNSSECSupported(s_EDNS* self, PyObject* args) {
  259. const PyObject *b;
  260. if (!PyArg_ParseTuple(args, "O!", &PyBool_Type, &b)) {
  261. return (NULL);
  262. }
  263. self->edns->setDNSSECSupported(b == Py_True);
  264. Py_RETURN_NONE;
  265. }
  266. PyObject*
  267. EDNS_getUDPSize(const s_EDNS* const self) {
  268. return (Py_BuildValue("I", self->edns->getUDPSize()));
  269. }
  270. PyObject*
  271. EDNS_setUDPSize(s_EDNS* self, PyObject* args) {
  272. unsigned int size;
  273. if (!PyArg_ParseTuple(args, "I", &size)) {
  274. return (NULL);
  275. }
  276. if (size > 65535) {
  277. PyErr_SetString(PyExc_OverflowError,
  278. "UDP size is not an unsigned 16-bit integer");
  279. return (NULL);
  280. }
  281. self->edns->setUDPSize(size);
  282. Py_RETURN_NONE;
  283. }
  284. PyObject*
  285. EDNS_createFromRR(const s_EDNS* null_self, PyObject* args) {
  286. const s_Name* name;
  287. const s_RRClass* rrclass;
  288. const s_RRType* rrtype;
  289. const s_RRTTL* rrttl;
  290. const s_Rdata* rdata;
  291. s_EDNS* edns_obj = NULL;
  292. assert(null_self == NULL);
  293. if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
  294. &rrclass_type, &rrclass, &rrtype_type, &rrtype,
  295. &rrttl_type, &rrttl, &rdata_type, &rdata)) {
  296. uint8_t extended_rcode;
  297. edns_obj = PyObject_New(s_EDNS, &edns_type);
  298. if (edns_obj == NULL) {
  299. return (NULL);
  300. }
  301. edns_obj->edns = createFromRR(*name->name, *rrclass->rrclass,
  302. *rrtype->rrtype, *rrttl->rrttl,
  303. *rdata->rdata, extended_rcode);
  304. if (edns_obj->edns != NULL) {
  305. PyObject* extrcode_obj = Py_BuildValue("B", extended_rcode);
  306. return (Py_BuildValue("OO", edns_obj, extrcode_obj));
  307. }
  308. Py_DECREF(edns_obj);
  309. return (NULL);
  310. }
  311. PyErr_Clear();
  312. PyErr_SetString(PyExc_TypeError,
  313. "Incorrect arguments for EDNS.create_from_rr()");
  314. return (NULL);
  315. }
  316. } // end of anonymous namespace
  317. // end of EDNS
  318. // Module Initialization, all statics are initialized here
  319. bool
  320. initModulePart_EDNS(PyObject* mod) {
  321. // We initialize the static description object with PyType_Ready(),
  322. // then add it to the module. This is not just a check! (leaving
  323. // this out results in segmentation faults)
  324. if (PyType_Ready(&edns_type) < 0) {
  325. return (false);
  326. }
  327. Py_INCREF(&edns_type);
  328. void* p = &edns_type;
  329. PyModule_AddObject(mod, "EDNS", static_cast<PyObject*>(p));
  330. addClassVariable(edns_type, "SUPPORTED_VERSION",
  331. Py_BuildValue("B", EDNS::SUPPORTED_VERSION));
  332. return (true);
  333. }