rcode_python.cc 15 KB

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