rcode_python.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_richcmp(const s_Rcode* const self,
  51. const s_Rcode* const other, int op);
  52. PyMethodDef Rcode_methods[] = {
  53. { "get_code", reinterpret_cast<PyCFunction>(Rcode_getCode), METH_NOARGS,
  54. "Returns the code value" },
  55. { "get_extended_code",
  56. reinterpret_cast<PyCFunction>(Rcode_getExtendedCode), METH_NOARGS,
  57. "Returns the upper 8-bit part of the extended code value" },
  58. { "to_text", reinterpret_cast<PyCFunction>(Rcode_toText), METH_NOARGS,
  59. "Returns the text representation" },
  60. { NULL, NULL, 0, NULL }
  61. };
  62. int
  63. Rcode_init(s_Rcode* const self, PyObject* args) {
  64. long code = 0;
  65. int ext_code = 0;
  66. if (PyArg_ParseTuple(args, "l", &code)) {
  67. if (code < 0 || code > 0xffff) {
  68. PyErr_SetString(PyExc_ValueError, "Rcode out of range");
  69. return (-1);
  70. }
  71. ext_code = -1;
  72. } else if (PyArg_ParseTuple(args, "li", &code, &ext_code)) {
  73. if (code < 0 || code > 0xff || ext_code < 0 || ext_code > 0xff) {
  74. PyErr_SetString(PyExc_ValueError, "Rcode out of range");
  75. return (-1);
  76. }
  77. } else {
  78. PyErr_Clear();
  79. PyErr_SetString(PyExc_TypeError,
  80. "Invalid arguments to Rcode constructor");
  81. return (-1);
  82. }
  83. try {
  84. if (ext_code == -1) {
  85. self->cppobj = new Rcode(code);
  86. } else {
  87. self->cppobj = new Rcode(code, ext_code);
  88. }
  89. self->static_code = false;
  90. } catch (const isc::OutOfRange& ex) {
  91. PyErr_SetString(PyExc_OverflowError, ex.what());
  92. return (-1);
  93. } catch (...) {
  94. PyErr_SetString(po_IscException, "Unexpected exception");
  95. return (-1);
  96. }
  97. return (0);
  98. }
  99. void
  100. Rcode_destroy(s_Rcode* const self) {
  101. // Depending on whether we created the rcode or are referring
  102. // to a global one, we do or do not delete self->cppobj here
  103. if (!self->static_code) {
  104. delete self->cppobj;
  105. }
  106. self->cppobj = NULL;
  107. Py_TYPE(self)->tp_free(self);
  108. }
  109. PyObject*
  110. Rcode_getCode(const s_Rcode* const self) {
  111. return (Py_BuildValue("I", self->cppobj->getCode()));
  112. }
  113. PyObject*
  114. Rcode_getExtendedCode(const s_Rcode* const self) {
  115. return (Py_BuildValue("I", self->cppobj->getExtendedCode()));
  116. }
  117. PyObject*
  118. Rcode_toText(const s_Rcode* const self) {
  119. return (Py_BuildValue("s", self->cppobj->toText().c_str()));
  120. }
  121. PyObject*
  122. Rcode_str(PyObject* self) {
  123. // Simply call the to_text method we already defined
  124. return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
  125. const_cast<char*>("")));
  126. }
  127. PyObject*
  128. Rcode_richcmp(const s_Rcode* const self, const s_Rcode* const other,
  129. const int op)
  130. {
  131. bool c = false;
  132. // Check for null and if the types match. If different type,
  133. // simply return False
  134. if (!other || (self->ob_type != other->ob_type)) {
  135. Py_RETURN_FALSE;
  136. }
  137. // Only equals and not equals here, unorderable type
  138. switch (op) {
  139. case Py_LT:
  140. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  141. return (NULL);
  142. case Py_LE:
  143. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  144. return (NULL);
  145. case Py_EQ:
  146. c = (*self->cppobj == *other->cppobj);
  147. break;
  148. case Py_NE:
  149. c = (*self->cppobj != *other->cppobj);
  150. break;
  151. case Py_GT:
  152. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  153. return (NULL);
  154. case Py_GE:
  155. PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
  156. return (NULL);
  157. }
  158. if (c)
  159. Py_RETURN_TRUE;
  160. else
  161. Py_RETURN_FALSE;
  162. }
  163. } // end of unnamed namespace
  164. namespace isc {
  165. namespace dns {
  166. namespace python {
  167. PyTypeObject rcode_type = {
  168. PyVarObject_HEAD_INIT(NULL, 0)
  169. "pydnspp.Rcode",
  170. sizeof(s_Rcode), // tp_basicsize
  171. 0, // tp_itemsize
  172. (destructor)Rcode_destroy, // tp_dealloc
  173. NULL, // tp_print
  174. NULL, // tp_getattr
  175. NULL, // tp_setattr
  176. NULL, // tp_reserved
  177. NULL, // tp_repr
  178. NULL, // tp_as_number
  179. NULL, // tp_as_sequence
  180. NULL, // tp_as_mapping
  181. NULL, // tp_hash
  182. NULL, // tp_call
  183. Rcode_str, // tp_str
  184. NULL, // tp_getattro
  185. NULL, // tp_setattro
  186. NULL, // tp_as_buffer
  187. Py_TPFLAGS_DEFAULT, // tp_flags
  188. "The Rcode class objects represent standard RCODEs"
  189. "of the header section of DNS messages.",
  190. NULL, // tp_traverse
  191. NULL, // tp_clear
  192. reinterpret_cast<richcmpfunc>(Rcode_richcmp), // tp_richcompare
  193. 0, // tp_weaklistoffset
  194. NULL, // tp_iter
  195. NULL, // tp_iternext
  196. Rcode_methods, // tp_methods
  197. NULL, // tp_members
  198. NULL, // tp_getset
  199. NULL, // tp_base
  200. NULL, // tp_dict
  201. NULL, // tp_descr_get
  202. NULL, // tp_descr_set
  203. 0, // tp_dictoffset
  204. (initproc)Rcode_init, // tp_init
  205. NULL, // tp_alloc
  206. PyType_GenericNew, // tp_new
  207. NULL, // tp_free
  208. NULL, // tp_is_gc
  209. NULL, // tp_bases
  210. NULL, // tp_mro
  211. NULL, // tp_cache
  212. NULL, // tp_subclasses
  213. NULL, // tp_weaklist
  214. NULL, // tp_del
  215. 0 // tp_version_tag
  216. };
  217. PyObject*
  218. createRcodeObject(const Rcode& source) {
  219. RcodeContainer container(PyObject_New(s_Rcode, &rcode_type));
  220. container.set(new Rcode(source));
  221. return (container.release());
  222. }
  223. bool
  224. PyRcode_Check(PyObject* obj) {
  225. if (obj == NULL) {
  226. isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
  227. }
  228. return (PyObject_TypeCheck(obj, &rcode_type));
  229. }
  230. const Rcode&
  231. PyRcode_ToRcode(const PyObject* rcode_obj) {
  232. if (rcode_obj == NULL) {
  233. isc_throw(PyCPPWrapperException,
  234. "obj argument NULL in Rcode PyObject conversion");
  235. }
  236. const s_Rcode* rcode = static_cast<const s_Rcode*>(rcode_obj);
  237. return (*rcode->cppobj);
  238. }
  239. } // namespace python
  240. } // namespace dns
  241. } // namespace isc