tsigerror_python.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Copyright (C) 2011 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 <string>
  16. #include <stdexcept>
  17. #include <util/python/pycppwrapper_util.h>
  18. #include <dns/tsigerror.h>
  19. #include "pydnspp_common.h"
  20. #include "rcode_python.h"
  21. #include "tsigerror_python.h"
  22. using namespace std;
  23. using namespace isc::util::python;
  24. using namespace isc::dns;
  25. using namespace isc::dns::python;
  26. //
  27. // Definition of the classes
  28. //
  29. // For each class, we need a struct, a helper functions (init, destroy,
  30. // and static wrappers around the methods we export), a list of methods,
  31. // and a type description
  32. //
  33. // TSIGError
  34. //
  35. // Trivial constructor.
  36. s_TSIGError::s_TSIGError() : cppobj(NULL) {
  37. }
  38. // Import pydoc text
  39. #include "tsigerror_python_inc.cc"
  40. namespace {
  41. // Shortcut type which would be convenient for adding class variables safely.
  42. typedef CPPPyObjectContainer<s_TSIGError, TSIGError> TSIGErrorContainer;
  43. //
  44. // We declare the functions here, the definitions are below
  45. // the type definition of the object, since both can use the other
  46. //
  47. // General creation and destruction
  48. int TSIGError_init(s_TSIGError* self, PyObject* args);
  49. void TSIGError_destroy(s_TSIGError* self);
  50. // These are the functions we export
  51. PyObject* TSIGError_getCode(const s_TSIGError* const self);
  52. PyObject* TSIGError_toText(const s_TSIGError* const self);
  53. PyObject* TSIGError_toRcode(const s_TSIGError* const self);
  54. PyObject* TSIGError_str(PyObject* self);
  55. PyObject* TSIGError_richcmp(const s_TSIGError* const self,
  56. const s_TSIGError* const other, int op);
  57. // These are the functions we export
  58. // For a minimal support, we don't need them.
  59. // This list contains the actual set of functions we have in
  60. // python. Each entry has
  61. // 1. Python method name
  62. // 2. Our static function here
  63. // 3. Argument type
  64. // 4. Documentation
  65. PyMethodDef TSIGError_methods[] = {
  66. { "get_code", reinterpret_cast<PyCFunction>(TSIGError_getCode),
  67. METH_NOARGS,
  68. TSIGError_getCode_doc },
  69. { "to_text", reinterpret_cast<PyCFunction>(TSIGError_toText), METH_NOARGS,
  70. TSIGError_toText_doc },
  71. { "to_rcode", reinterpret_cast<PyCFunction>(TSIGError_toRcode),
  72. METH_NOARGS,
  73. TSIGError_toRcode_doc },
  74. { NULL, NULL, 0, NULL }
  75. };
  76. int
  77. TSIGError_init(s_TSIGError* self, PyObject* args) {
  78. try {
  79. // Constructor from the code value
  80. long code = 0;
  81. if (PyArg_ParseTuple(args, "l", &code)) {
  82. if (code < 0 || code > 0xffff) {
  83. PyErr_SetString(PyExc_ValueError, "TSIG error out of range");
  84. return (-1);
  85. }
  86. self->cppobj = new TSIGError(code);
  87. return (0);
  88. }
  89. // Constructor from Rcode
  90. s_Rcode* py_rcode;
  91. if (PyArg_ParseTuple(args, "O!", &rcode_type, &py_rcode)) {
  92. self->cppobj = new TSIGError(*py_rcode->cppobj);
  93. return (0);
  94. }
  95. } catch (const isc::OutOfRange& ex) {
  96. const string ex_what = "Failed to construct TSIGError object: " +
  97. string(ex.what());
  98. PyErr_SetString(PyExc_ValueError, ex_what.c_str());
  99. return (-1);
  100. } catch (const exception& ex) {
  101. const string ex_what = "Failed to construct TSIGError object: " +
  102. string(ex.what());
  103. PyErr_SetString(po_IscException, ex_what.c_str());
  104. return (-1);
  105. } catch (...) {
  106. PyErr_SetString(po_IscException,
  107. "Unexpected exception in constructing TSIGError");
  108. return (-1);
  109. }
  110. PyErr_SetString(PyExc_TypeError,
  111. "Invalid arguments to TSIGError constructor");
  112. return (-1);
  113. }
  114. void
  115. TSIGError_destroy(s_TSIGError* const self) {
  116. delete self->cppobj;
  117. self->cppobj = NULL;
  118. Py_TYPE(self)->tp_free(self);
  119. }
  120. PyObject*
  121. TSIGError_getCode(const s_TSIGError* const self) {
  122. return (Py_BuildValue("I", self->cppobj->getCode()));
  123. }
  124. PyObject*
  125. TSIGError_toText(const s_TSIGError* const self) {
  126. try {
  127. // toText() could throw, so we need to catch any exceptions below.
  128. return (Py_BuildValue("s", self->cppobj->toText().c_str()));
  129. } catch (const exception& ex) {
  130. const string ex_what =
  131. "Failed to convert TSIGError object to text: " +
  132. string(ex.what());
  133. PyErr_SetString(po_IscException, ex_what.c_str());
  134. } catch (...) {
  135. PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
  136. "converting TSIGError object to text");
  137. }
  138. return (NULL);
  139. }
  140. PyObject*
  141. TSIGError_str(PyObject* self) {
  142. // Simply call the to_text method we already defined
  143. return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
  144. const_cast<char*>("")));
  145. }
  146. PyObject*
  147. TSIGError_toRcode(const s_TSIGError* const self) {
  148. typedef CPPPyObjectContainer<s_Rcode, Rcode> RcodePyObjectContainer;
  149. try {
  150. RcodePyObjectContainer rcode_container(PyObject_New(s_Rcode,
  151. &rcode_type));
  152. rcode_container.set(new Rcode(self->cppobj->toRcode()));
  153. return (rcode_container.release());
  154. } catch (const exception& ex) {
  155. const string ex_what =
  156. "Failed to convert TSIGError to Rcode: " + string(ex.what());
  157. PyErr_SetString(po_IscException, ex_what.c_str());
  158. } catch (...) {
  159. PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
  160. "converting TSIGError to Rcode");
  161. }
  162. return (NULL);
  163. }
  164. PyObject*
  165. TSIGError_richcmp(const s_TSIGError* const self,
  166. const s_TSIGError* const other,
  167. const int op)
  168. {
  169. bool c = false;
  170. // Check for null and if the types match. If different type,
  171. // simply return False
  172. if (other == NULL || (self->ob_type != other->ob_type)) {
  173. Py_RETURN_FALSE;
  174. }
  175. // Only equals and not equals here, unorderable type
  176. switch (op) {
  177. case Py_LT:
  178. PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
  179. return (NULL);
  180. case Py_LE:
  181. PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
  182. return (NULL);
  183. case Py_EQ:
  184. c = (*self->cppobj == *other->cppobj);
  185. break;
  186. case Py_NE:
  187. c = (*self->cppobj != *other->cppobj);
  188. break;
  189. case Py_GT:
  190. PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
  191. return (NULL);
  192. case Py_GE:
  193. PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
  194. return (NULL);
  195. }
  196. if (c) {
  197. Py_RETURN_TRUE;
  198. } else {
  199. Py_RETURN_FALSE;
  200. }
  201. }
  202. } // end of unnamed namespace
  203. namespace isc {
  204. namespace dns {
  205. namespace python {
  206. // This defines the complete type for reflection in python and
  207. // parsing of PyObject* to s_TSIGError
  208. // Most of the functions are not actually implemented and NULL here.
  209. PyTypeObject tsigerror_type = {
  210. PyVarObject_HEAD_INIT(NULL, 0)
  211. "libdns_python.TSIGError",
  212. sizeof(s_TSIGError), // tp_basicsize
  213. 0, // tp_itemsize
  214. reinterpret_cast<destructor>(TSIGError_destroy), // tp_dealloc
  215. NULL, // tp_print
  216. NULL, // tp_getattr
  217. NULL, // tp_setattr
  218. NULL, // tp_reserved
  219. NULL, // tp_repr
  220. NULL, // tp_as_number
  221. NULL, // tp_as_sequence
  222. NULL, // tp_as_mapping
  223. NULL, // tp_hash
  224. NULL, // tp_call
  225. // THIS MAY HAVE TO BE CHANGED TO NULL:
  226. TSIGError_str, // tp_str
  227. NULL, // tp_getattro
  228. NULL, // tp_setattro
  229. NULL, // tp_as_buffer
  230. Py_TPFLAGS_DEFAULT, // tp_flags
  231. TSIGError_doc,
  232. NULL, // tp_traverse
  233. NULL, // tp_clear
  234. // THIS MAY HAVE TO BE CHANGED TO NULL:
  235. reinterpret_cast<richcmpfunc>(TSIGError_richcmp), // tp_richcompare
  236. 0, // tp_weaklistoffset
  237. NULL, // tp_iter
  238. NULL, // tp_iternext
  239. TSIGError_methods, // tp_methods
  240. NULL, // tp_members
  241. NULL, // tp_getset
  242. NULL, // tp_base
  243. NULL, // tp_dict
  244. NULL, // tp_descr_get
  245. NULL, // tp_descr_set
  246. 0, // tp_dictoffset
  247. reinterpret_cast<initproc>(TSIGError_init), // tp_init
  248. NULL, // tp_alloc
  249. PyType_GenericNew, // tp_new
  250. NULL, // tp_free
  251. NULL, // tp_is_gc
  252. NULL, // tp_bases
  253. NULL, // tp_mro
  254. NULL, // tp_cache
  255. NULL, // tp_subclasses
  256. NULL, // tp_weaklist
  257. NULL, // tp_del
  258. 0 // tp_version_tag
  259. };
  260. namespace {
  261. // Trivial shortcut to create and install TSIGError constants.
  262. inline void
  263. installTSIGErrorConstant(const char* name, const TSIGError& val) {
  264. TSIGErrorContainer container(PyObject_New(s_TSIGError, &tsigerror_type));
  265. container.installAsClassVariable(tsigerror_type, name, new TSIGError(val));
  266. }
  267. }
  268. // Module Initialization, all statics are initialized here
  269. bool
  270. initModulePart_TSIGError(PyObject* mod) {
  271. // We initialize the static description object with PyType_Ready(),
  272. // then add it to the module. This is not just a check! (leaving
  273. // this out results in segmentation faults)
  274. if (PyType_Ready(&tsigerror_type) < 0) {
  275. return (false);
  276. }
  277. void* p = &tsigerror_type;
  278. if (PyModule_AddObject(mod, "TSIGError", static_cast<PyObject*>(p)) < 0) {
  279. return (false);
  280. }
  281. Py_INCREF(&tsigerror_type);
  282. try {
  283. // Constant class variables
  284. // Error codes (bare values)
  285. installClassVariable(tsigerror_type, "BAD_SIG_CODE",
  286. Py_BuildValue("H", TSIGError::BAD_SIG_CODE));
  287. installClassVariable(tsigerror_type, "BAD_KEY_CODE",
  288. Py_BuildValue("H", TSIGError::BAD_KEY_CODE));
  289. installClassVariable(tsigerror_type, "BAD_TIME_CODE",
  290. Py_BuildValue("H", TSIGError::BAD_TIME_CODE));
  291. // Error codes (constant objects)
  292. installTSIGErrorConstant("NOERROR", TSIGError::NOERROR());
  293. installTSIGErrorConstant("FORMERR", TSIGError::FORMERR());
  294. installTSIGErrorConstant("SERVFAIL", TSIGError::SERVFAIL());
  295. installTSIGErrorConstant("NXDOMAIN", TSIGError::NXDOMAIN());
  296. installTSIGErrorConstant("NOTIMP", TSIGError::NOTIMP());
  297. installTSIGErrorConstant("REFUSED", TSIGError::REFUSED());
  298. installTSIGErrorConstant("YXDOMAIN", TSIGError::YXDOMAIN());
  299. installTSIGErrorConstant("YXRRSET", TSIGError::YXRRSET());
  300. installTSIGErrorConstant("NXRRSET", TSIGError::NXRRSET());
  301. installTSIGErrorConstant("NOTAUTH", TSIGError::NOTAUTH());
  302. installTSIGErrorConstant("NOTZONE", TSIGError::NOTZONE());
  303. installTSIGErrorConstant("RESERVED11", TSIGError::RESERVED11());
  304. installTSIGErrorConstant("RESERVED12", TSIGError::RESERVED12());
  305. installTSIGErrorConstant("RESERVED13", TSIGError::RESERVED13());
  306. installTSIGErrorConstant("RESERVED14", TSIGError::RESERVED14());
  307. installTSIGErrorConstant("RESERVED15", TSIGError::RESERVED15());
  308. installTSIGErrorConstant("BAD_SIG", TSIGError::BAD_SIG());
  309. installTSIGErrorConstant("BAD_KEY", TSIGError::BAD_KEY());
  310. installTSIGErrorConstant("BAD_TIME", TSIGError::BAD_TIME());
  311. } catch (const exception& ex) {
  312. const string ex_what =
  313. "Unexpected failure in TSIGError initialization: " +
  314. string(ex.what());
  315. PyErr_SetString(po_IscException, ex_what.c_str());
  316. return (false);
  317. } catch (...) {
  318. PyErr_SetString(PyExc_SystemError,
  319. "Unexpected failure in TSIGError initialization");
  320. return (false);
  321. }
  322. return (true);
  323. }
  324. } // namespace python
  325. } // namespace dns
  326. } // namespace isc