tsigerror_python.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // Import pydoc text
  36. #include "tsigerror_python_inc.cc"
  37. namespace {
  38. // The s_* Class simply covers one instantiation of the object
  39. class s_TSIGError : public PyObject {
  40. public:
  41. s_TSIGError() : cppobj(NULL) {};
  42. const TSIGError* cppobj;
  43. };
  44. // Shortcut type which would be convenient for adding class variables safely.
  45. typedef CPPPyObjectContainer<s_TSIGError, TSIGError> TSIGErrorContainer;
  46. //
  47. // We declare the functions here, the definitions are below
  48. // the type definition of the object, since both can use the other
  49. //
  50. // General creation and destruction
  51. int TSIGError_init(s_TSIGError* self, PyObject* args);
  52. void TSIGError_destroy(s_TSIGError* self);
  53. // These are the functions we export
  54. PyObject* TSIGError_getCode(const s_TSIGError* const self);
  55. PyObject* TSIGError_toText(const s_TSIGError* const self);
  56. PyObject* TSIGError_toRcode(const s_TSIGError* const self);
  57. PyObject* TSIGError_str(PyObject* self);
  58. PyObject* TSIGError_richcmp(const s_TSIGError* const self,
  59. const s_TSIGError* const other, int op);
  60. // These are the functions we export
  61. // For a minimal support, we don't need them.
  62. // This list contains the actual set of functions we have in
  63. // python. Each entry has
  64. // 1. Python method name
  65. // 2. Our static function here
  66. // 3. Argument type
  67. // 4. Documentation
  68. PyMethodDef TSIGError_methods[] = {
  69. { "get_code", reinterpret_cast<PyCFunction>(TSIGError_getCode),
  70. METH_NOARGS,
  71. TSIGError_getCode_doc },
  72. { "to_text", reinterpret_cast<PyCFunction>(TSIGError_toText), METH_NOARGS,
  73. TSIGError_toText_doc },
  74. { "to_rcode", reinterpret_cast<PyCFunction>(TSIGError_toRcode),
  75. METH_NOARGS,
  76. TSIGError_toRcode_doc },
  77. { NULL, NULL, 0, NULL }
  78. };
  79. int
  80. TSIGError_init(s_TSIGError* self, PyObject* args) {
  81. try {
  82. // Constructor from the code value
  83. long code = 0;
  84. if (PyArg_ParseTuple(args, "l", &code)) {
  85. if (code < 0 || code > 0xffff) {
  86. PyErr_SetString(PyExc_ValueError, "TSIG error out of range");
  87. return (-1);
  88. }
  89. self->cppobj = new TSIGError(code);
  90. return (0);
  91. }
  92. // Constructor from Rcode
  93. PyErr_Clear();
  94. PyObject* py_rcode;
  95. if (PyArg_ParseTuple(args, "O!", &rcode_type, &py_rcode)) {
  96. self->cppobj = new TSIGError(PyRcode_ToRcode(py_rcode));
  97. return (0);
  98. }
  99. } catch (const isc::OutOfRange& ex) {
  100. const string ex_what = "Failed to construct TSIGError object: " +
  101. string(ex.what());
  102. PyErr_SetString(PyExc_ValueError, ex_what.c_str());
  103. return (-1);
  104. } catch (const exception& ex) {
  105. const string ex_what = "Failed to construct TSIGError object: " +
  106. string(ex.what());
  107. PyErr_SetString(po_IscException, ex_what.c_str());
  108. return (-1);
  109. } catch (...) {
  110. PyErr_SetString(po_IscException,
  111. "Unexpected exception in constructing TSIGError");
  112. return (-1);
  113. }
  114. PyErr_SetString(PyExc_TypeError,
  115. "Invalid arguments to TSIGError constructor");
  116. return (-1);
  117. }
  118. void
  119. TSIGError_destroy(s_TSIGError* const self) {
  120. delete self->cppobj;
  121. self->cppobj = NULL;
  122. Py_TYPE(self)->tp_free(self);
  123. }
  124. PyObject*
  125. TSIGError_getCode(const s_TSIGError* const self) {
  126. return (Py_BuildValue("I", self->cppobj->getCode()));
  127. }
  128. PyObject*
  129. TSIGError_toText(const s_TSIGError* const self) {
  130. try {
  131. // toText() could throw, so we need to catch any exceptions below.
  132. return (Py_BuildValue("s", self->cppobj->toText().c_str()));
  133. } catch (const exception& ex) {
  134. const string ex_what =
  135. "Failed to convert TSIGError object to text: " +
  136. string(ex.what());
  137. PyErr_SetString(po_IscException, ex_what.c_str());
  138. } catch (...) {
  139. PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
  140. "converting TSIGError object to text");
  141. }
  142. return (NULL);
  143. }
  144. PyObject*
  145. TSIGError_str(PyObject* self) {
  146. // Simply call the to_text method we already defined
  147. return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
  148. const_cast<char*>("")));
  149. }
  150. PyObject*
  151. TSIGError_toRcode(const s_TSIGError* const self) {
  152. try {
  153. return (createRcodeObject(self->cppobj->toRcode()));
  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. "pydnspp.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. namespace internal {
  270. bool
  271. initModulePart_TSIGError(PyObject* mod) {
  272. // We initialize the static description object with PyType_Ready(),
  273. // then add it to the module. This is not just a check! (leaving
  274. // this out results in segmentation faults)
  275. if (PyType_Ready(&tsigerror_type) < 0) {
  276. return (false);
  277. }
  278. void* p = &tsigerror_type;
  279. if (PyModule_AddObject(mod, "TSIGError", static_cast<PyObject*>(p)) < 0) {
  280. return (false);
  281. }
  282. Py_INCREF(&tsigerror_type);
  283. try {
  284. // Constant class variables
  285. // Error codes (bare values)
  286. installClassVariable(tsigerror_type, "BAD_SIG_CODE",
  287. Py_BuildValue("H", TSIGError::BAD_SIG_CODE));
  288. installClassVariable(tsigerror_type, "BAD_KEY_CODE",
  289. Py_BuildValue("H", TSIGError::BAD_KEY_CODE));
  290. installClassVariable(tsigerror_type, "BAD_TIME_CODE",
  291. Py_BuildValue("H", TSIGError::BAD_TIME_CODE));
  292. // Error codes (constant objects)
  293. installTSIGErrorConstant("NOERROR", TSIGError::NOERROR());
  294. installTSIGErrorConstant("FORMERR", TSIGError::FORMERR());
  295. installTSIGErrorConstant("SERVFAIL", TSIGError::SERVFAIL());
  296. installTSIGErrorConstant("NXDOMAIN", TSIGError::NXDOMAIN());
  297. installTSIGErrorConstant("NOTIMP", TSIGError::NOTIMP());
  298. installTSIGErrorConstant("REFUSED", TSIGError::REFUSED());
  299. installTSIGErrorConstant("YXDOMAIN", TSIGError::YXDOMAIN());
  300. installTSIGErrorConstant("YXRRSET", TSIGError::YXRRSET());
  301. installTSIGErrorConstant("NXRRSET", TSIGError::NXRRSET());
  302. installTSIGErrorConstant("NOTAUTH", TSIGError::NOTAUTH());
  303. installTSIGErrorConstant("NOTZONE", TSIGError::NOTZONE());
  304. installTSIGErrorConstant("RESERVED11", TSIGError::RESERVED11());
  305. installTSIGErrorConstant("RESERVED12", TSIGError::RESERVED12());
  306. installTSIGErrorConstant("RESERVED13", TSIGError::RESERVED13());
  307. installTSIGErrorConstant("RESERVED14", TSIGError::RESERVED14());
  308. installTSIGErrorConstant("RESERVED15", TSIGError::RESERVED15());
  309. installTSIGErrorConstant("BAD_SIG", TSIGError::BAD_SIG());
  310. installTSIGErrorConstant("BAD_KEY", TSIGError::BAD_KEY());
  311. installTSIGErrorConstant("BAD_TIME", TSIGError::BAD_TIME());
  312. } catch (const exception& ex) {
  313. const string ex_what =
  314. "Unexpected failure in TSIGError initialization: " +
  315. string(ex.what());
  316. PyErr_SetString(po_IscException, ex_what.c_str());
  317. return (false);
  318. } catch (...) {
  319. PyErr_SetString(PyExc_SystemError,
  320. "Unexpected failure in TSIGError initialization");
  321. return (false);
  322. }
  323. return (true);
  324. }
  325. } // end namespace internal
  326. PyObject*
  327. createTSIGErrorObject(const TSIGError& source) {
  328. TSIGErrorContainer container = PyObject_New(s_TSIGError, &tsigerror_type);
  329. container.set(new TSIGError(source));
  330. return (container.release());
  331. }
  332. } // namespace python
  333. } // namespace dns
  334. } // namespace isc