rcode_python.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #include <exceptions/exceptions.h>
  16. #include <dns/rcode.h>
  17. #include <util/python/pycppwrapper_util.h>
  18. #include "pydnspp_common.h"
  19. #include "rcode_python.h"
  20. using namespace isc::dns;
  21. using namespace isc::dns::python;
  22. using namespace isc::util::python;
  23. namespace {
  24. // The s_* Class simply covers one instantiation of the object.
  25. //
  26. // We added a helper variable static_code here
  27. // Since we can create Rcodes dynamically with Rcode(int), but also
  28. // use the static globals (Rcode::NOERROR() etc), we use this
  29. // variable to see if the code came from one of the latter, in which
  30. // case Rcode_destroy should not free it (the other option is to
  31. // allocate new Rcodes for every use of the static ones, but this
  32. // seems more efficient).
  33. //
  34. // Follow-up note: we don't have to use the proxy function in the python lib;
  35. // we can just define class specific constants directly (see TSIGError).
  36. // We should make this cleanup later.
  37. class s_Rcode : public PyObject {
  38. public:
  39. s_Rcode() : cppobj(NULL), static_code(false) {};
  40. const Rcode* cppobj;
  41. bool static_code;
  42. };
  43. typedef CPPPyObjectContainer<s_Rcode, Rcode> RcodeContainer;
  44. int Rcode_init(s_Rcode* const self, PyObject* args);
  45. void Rcode_destroy(s_Rcode* const self);
  46. PyObject* Rcode_getCode(const s_Rcode* const self);
  47. PyObject* Rcode_getExtendedCode(const s_Rcode* const self);
  48. PyObject* Rcode_toText(const s_Rcode* const self);
  49. PyObject* Rcode_str(PyObject* self);
  50. PyObject* Rcode_NOERROR(const s_Rcode* self);
  51. PyObject* Rcode_FORMERR(const s_Rcode* self);
  52. PyObject* Rcode_SERVFAIL(const s_Rcode* self);
  53. PyObject* Rcode_NXDOMAIN(const s_Rcode* self);
  54. PyObject* Rcode_NOTIMP(const s_Rcode* self);
  55. PyObject* Rcode_REFUSED(const s_Rcode* self);
  56. PyObject* Rcode_YXDOMAIN(const s_Rcode* self);
  57. PyObject* Rcode_YXRRSET(const s_Rcode* self);
  58. PyObject* Rcode_NXRRSET(const s_Rcode* self);
  59. PyObject* Rcode_NOTAUTH(const s_Rcode* self);
  60. PyObject* Rcode_NOTZONE(const s_Rcode* self);
  61. PyObject* Rcode_RESERVED11(const s_Rcode* self);
  62. PyObject* Rcode_RESERVED12(const s_Rcode* self);
  63. PyObject* Rcode_RESERVED13(const s_Rcode* self);
  64. PyObject* Rcode_RESERVED14(const s_Rcode* self);
  65. PyObject* Rcode_RESERVED15(const s_Rcode* self);
  66. PyObject* Rcode_BADVERS(const s_Rcode* self);
  67. PyObject* Rcode_richcmp(const s_Rcode* const self,
  68. const s_Rcode* const other, int op);
  69. PyMethodDef Rcode_methods[] = {
  70. { "get_code", reinterpret_cast<PyCFunction>(Rcode_getCode), METH_NOARGS,
  71. "Returns the code value" },
  72. { "get_extended_code",
  73. reinterpret_cast<PyCFunction>(Rcode_getExtendedCode), METH_NOARGS,
  74. "Returns the upper 8-bit part of the extended code value" },
  75. { "to_text", reinterpret_cast<PyCFunction>(Rcode_toText), METH_NOARGS,
  76. "Returns the text representation" },
  77. { "NOERROR", reinterpret_cast<PyCFunction>(Rcode_NOERROR),
  78. METH_NOARGS | METH_STATIC, "Creates a NOERROR Rcode" },
  79. { "FORMERR", reinterpret_cast<PyCFunction>(Rcode_FORMERR),
  80. METH_NOARGS | METH_STATIC, "Creates a FORMERR Rcode" },
  81. { "SERVFAIL", reinterpret_cast<PyCFunction>(Rcode_SERVFAIL),
  82. METH_NOARGS | METH_STATIC, "Creates a SERVFAIL Rcode" },
  83. { "NXDOMAIN", reinterpret_cast<PyCFunction>(Rcode_NXDOMAIN),
  84. METH_NOARGS | METH_STATIC, "Creates a NXDOMAIN Rcode" },
  85. { "NOTIMP", reinterpret_cast<PyCFunction>(Rcode_NOTIMP),
  86. METH_NOARGS | METH_STATIC, "Creates a NOTIMP Rcode" },
  87. { "REFUSED", reinterpret_cast<PyCFunction>(Rcode_REFUSED),
  88. METH_NOARGS | METH_STATIC, "Creates a REFUSED Rcode" },
  89. { "YXDOMAIN", reinterpret_cast<PyCFunction>(Rcode_YXDOMAIN),
  90. METH_NOARGS | METH_STATIC, "Creates a YXDOMAIN Rcode" },
  91. { "YXRRSET", reinterpret_cast<PyCFunction>(Rcode_YXRRSET),
  92. METH_NOARGS | METH_STATIC, "Creates a YYRRSET Rcode" },
  93. { "NXRRSET", reinterpret_cast<PyCFunction>(Rcode_NXRRSET),
  94. METH_NOARGS | METH_STATIC, "Creates a NXRRSET Rcode" },
  95. { "NOTAUTH", reinterpret_cast<PyCFunction>(Rcode_NOTAUTH),
  96. METH_NOARGS | METH_STATIC, "Creates a NOTAUTH Rcode" },
  97. { "NOTZONE", reinterpret_cast<PyCFunction>(Rcode_NOTZONE),
  98. METH_NOARGS | METH_STATIC, "Creates a NOTZONE Rcode" },
  99. { "RESERVED11", reinterpret_cast<PyCFunction>(Rcode_RESERVED11),
  100. METH_NOARGS | METH_STATIC, "Creates a RESERVED11 Rcode" },
  101. { "RESERVED12", reinterpret_cast<PyCFunction>(Rcode_RESERVED12),
  102. METH_NOARGS | METH_STATIC, "Creates a RESERVED12 Rcode" },
  103. { "RESERVED13", reinterpret_cast<PyCFunction>(Rcode_RESERVED13),
  104. METH_NOARGS | METH_STATIC, "Creates a RESERVED13 Rcode" },
  105. { "RESERVED14", reinterpret_cast<PyCFunction>(Rcode_RESERVED14),
  106. METH_NOARGS | METH_STATIC, "Creates a RESERVED14 Rcode" },
  107. { "RESERVED15", reinterpret_cast<PyCFunction>(Rcode_RESERVED15),
  108. METH_NOARGS | METH_STATIC, "Creates a RESERVED15 Rcode" },
  109. { "BADVERS", reinterpret_cast<PyCFunction>(Rcode_BADVERS),
  110. METH_NOARGS | METH_STATIC, "Creates a BADVERS Rcode" },
  111. { NULL, NULL, 0, NULL }
  112. };
  113. int
  114. Rcode_init(s_Rcode* const self, PyObject* args) {
  115. long code = 0;
  116. int ext_code = 0;
  117. if (PyArg_ParseTuple(args, "l", &code)) {
  118. if (code < 0 || code > 0xffff) {
  119. PyErr_SetString(PyExc_ValueError, "Rcode out of range");
  120. return (-1);
  121. }
  122. ext_code = -1;
  123. } else if (PyArg_ParseTuple(args, "li", &code, &ext_code)) {
  124. if (code < 0 || code > 0xff || ext_code < 0 || ext_code > 0xff) {
  125. PyErr_SetString(PyExc_ValueError, "Rcode out of range");
  126. return (-1);
  127. }
  128. } else {
  129. PyErr_Clear();
  130. PyErr_SetString(PyExc_TypeError,
  131. "Invalid arguments to Rcode constructor");
  132. return (-1);
  133. }
  134. try {
  135. if (ext_code == -1) {
  136. self->cppobj = new Rcode(code);
  137. } else {
  138. self->cppobj = new Rcode(code, ext_code);
  139. }
  140. self->static_code = false;
  141. } catch (const isc::OutOfRange& ex) {
  142. PyErr_SetString(PyExc_OverflowError, ex.what());
  143. return (-1);
  144. } catch (...) {
  145. PyErr_SetString(po_IscException, "Unexpected exception");
  146. return (-1);
  147. }
  148. return (0);
  149. }
  150. void
  151. Rcode_destroy(s_Rcode* const self) {
  152. // Depending on whether we created the rcode or are referring
  153. // to a global one, we do or do not delete self->cppobj here
  154. if (!self->static_code) {
  155. delete self->cppobj;
  156. }
  157. self->cppobj = NULL;
  158. Py_TYPE(self)->tp_free(self);
  159. }
  160. PyObject*
  161. Rcode_getCode(const s_Rcode* const self) {
  162. return (Py_BuildValue("I", self->cppobj->getCode()));
  163. }
  164. PyObject*
  165. Rcode_getExtendedCode(const s_Rcode* const self) {
  166. return (Py_BuildValue("I", self->cppobj->getExtendedCode()));
  167. }
  168. PyObject*
  169. Rcode_toText(const s_Rcode* const self) {
  170. return (Py_BuildValue("s", self->cppobj->toText().c_str()));
  171. }
  172. PyObject*
  173. Rcode_str(PyObject* self) {
  174. // Simply call the to_text method we already defined
  175. return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
  176. const_cast<char*>("")));
  177. }
  178. PyObject*
  179. Rcode_createStatic(const Rcode& rcode) {
  180. s_Rcode* ret = PyObject_New(s_Rcode, &rcode_type);
  181. if (ret != NULL) {
  182. ret->cppobj = &rcode;
  183. ret->static_code = true;
  184. }
  185. return (ret);
  186. }
  187. PyObject*
  188. Rcode_NOERROR(const s_Rcode*) {
  189. return (Rcode_createStatic(Rcode::NOERROR()));
  190. }
  191. PyObject*
  192. Rcode_FORMERR(const s_Rcode*) {
  193. return (Rcode_createStatic(Rcode::FORMERR()));
  194. }
  195. PyObject*
  196. Rcode_SERVFAIL(const s_Rcode*) {
  197. return (Rcode_createStatic(Rcode::SERVFAIL()));
  198. }
  199. PyObject*
  200. Rcode_NXDOMAIN(const s_Rcode*) {
  201. return (Rcode_createStatic(Rcode::NXDOMAIN()));
  202. }
  203. PyObject*
  204. Rcode_NOTIMP(const s_Rcode*) {
  205. return (Rcode_createStatic(Rcode::NOTIMP()));
  206. }
  207. PyObject*
  208. Rcode_REFUSED(const s_Rcode*) {
  209. return (Rcode_createStatic(Rcode::REFUSED()));
  210. }
  211. PyObject*
  212. Rcode_YXDOMAIN(const s_Rcode*) {
  213. return (Rcode_createStatic(Rcode::YXDOMAIN()));
  214. }
  215. PyObject*
  216. Rcode_YXRRSET(const s_Rcode*) {
  217. return (Rcode_createStatic(Rcode::YXRRSET()));
  218. }
  219. PyObject*
  220. Rcode_NXRRSET(const s_Rcode*) {
  221. return (Rcode_createStatic(Rcode::NXRRSET()));
  222. }
  223. PyObject*
  224. Rcode_NOTAUTH(const s_Rcode*) {
  225. return (Rcode_createStatic(Rcode::NOTAUTH()));
  226. }
  227. PyObject*
  228. Rcode_NOTZONE(const s_Rcode*) {
  229. return (Rcode_createStatic(Rcode::NOTZONE()));
  230. }
  231. PyObject*
  232. Rcode_RESERVED11(const s_Rcode*) {
  233. return (Rcode_createStatic(Rcode::RESERVED11()));
  234. }
  235. PyObject*
  236. Rcode_RESERVED12(const s_Rcode*) {
  237. return (Rcode_createStatic(Rcode::RESERVED12()));
  238. }
  239. PyObject*
  240. Rcode_RESERVED13(const s_Rcode*) {
  241. return (Rcode_createStatic(Rcode::RESERVED13()));
  242. }
  243. PyObject*
  244. Rcode_RESERVED14(const s_Rcode*) {
  245. return (Rcode_createStatic(Rcode::RESERVED14()));
  246. }
  247. PyObject*
  248. Rcode_RESERVED15(const s_Rcode*) {
  249. return (Rcode_createStatic(Rcode::RESERVED15()));
  250. }
  251. PyObject*
  252. Rcode_BADVERS(const s_Rcode*) {
  253. return (Rcode_createStatic(Rcode::BADVERS()));
  254. }
  255. PyObject*
  256. Rcode_richcmp(const s_Rcode* const self, const s_Rcode* const other,
  257. const int op)
  258. {
  259. bool c = false;
  260. // Check for null and if the types match. If different type,
  261. // simply return False
  262. if (!other || (self->ob_type != other->ob_type)) {
  263. Py_RETURN_FALSE;
  264. }
  265. // Only equals and not equals here, unorderable type
  266. switch (op) {
  267. case Py_LT:
  268. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  269. return (NULL);
  270. case Py_LE:
  271. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  272. return (NULL);
  273. case Py_EQ:
  274. c = (*self->cppobj == *other->cppobj);
  275. break;
  276. case Py_NE:
  277. c = (*self->cppobj != *other->cppobj);
  278. break;
  279. case Py_GT:
  280. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  281. return (NULL);
  282. case Py_GE:
  283. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  284. return (NULL);
  285. }
  286. if (c)
  287. Py_RETURN_TRUE;
  288. else
  289. Py_RETURN_FALSE;
  290. }
  291. } // end of unnamed namespace
  292. namespace isc {
  293. namespace dns {
  294. namespace python {
  295. PyTypeObject rcode_type = {
  296. PyVarObject_HEAD_INIT(NULL, 0)
  297. "pydnspp.Rcode",
  298. sizeof(s_Rcode), // tp_basicsize
  299. 0, // tp_itemsize
  300. (destructor)Rcode_destroy, // tp_dealloc
  301. NULL, // tp_print
  302. NULL, // tp_getattr
  303. NULL, // tp_setattr
  304. NULL, // tp_reserved
  305. NULL, // tp_repr
  306. NULL, // tp_as_number
  307. NULL, // tp_as_sequence
  308. NULL, // tp_as_mapping
  309. NULL, // tp_hash
  310. NULL, // tp_call
  311. Rcode_str, // tp_str
  312. NULL, // tp_getattro
  313. NULL, // tp_setattro
  314. NULL, // tp_as_buffer
  315. Py_TPFLAGS_DEFAULT, // tp_flags
  316. "The Rcode class objects represent standard RCODEs"
  317. "of the header section of DNS messages.",
  318. NULL, // tp_traverse
  319. NULL, // tp_clear
  320. reinterpret_cast<richcmpfunc>(Rcode_richcmp), // tp_richcompare
  321. 0, // tp_weaklistoffset
  322. NULL, // tp_iter
  323. NULL, // tp_iternext
  324. Rcode_methods, // tp_methods
  325. NULL, // tp_members
  326. NULL, // tp_getset
  327. NULL, // tp_base
  328. NULL, // tp_dict
  329. NULL, // tp_descr_get
  330. NULL, // tp_descr_set
  331. 0, // tp_dictoffset
  332. (initproc)Rcode_init, // tp_init
  333. NULL, // tp_alloc
  334. PyType_GenericNew, // tp_new
  335. NULL, // tp_free
  336. NULL, // tp_is_gc
  337. NULL, // tp_bases
  338. NULL, // tp_mro
  339. NULL, // tp_cache
  340. NULL, // tp_subclasses
  341. NULL, // tp_weaklist
  342. NULL, // tp_del
  343. 0 // tp_version_tag
  344. };
  345. PyObject*
  346. createRcodeObject(const Rcode& source) {
  347. RcodeContainer container(PyObject_New(s_Rcode, &rcode_type));
  348. container.set(new Rcode(source));
  349. return (container.release());
  350. }
  351. bool
  352. PyRcode_Check(PyObject* obj) {
  353. if (obj == NULL) {
  354. isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
  355. }
  356. return (PyObject_TypeCheck(obj, &rcode_type));
  357. }
  358. const Rcode&
  359. PyRcode_ToRcode(const PyObject* rcode_obj) {
  360. if (rcode_obj == NULL) {
  361. isc_throw(PyCPPWrapperException,
  362. "obj argument NULL in Rcode PyObject conversion");
  363. }
  364. const s_Rcode* rcode = static_cast<const s_Rcode*>(rcode_obj);
  365. return (*rcode->cppobj);
  366. }
  367. } // namespace python
  368. } // namespace dns
  369. } // namespace isc