rcode_python.cc 15 KB

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