tsigrecord_python.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/tsigrecord.h>
  19. #include "pydnspp_common.h"
  20. #include "pydnspp_towire.h"
  21. #include "name_python.h"
  22. #include "tsig_rdata_python.h"
  23. #include "tsigrecord_python.h"
  24. using namespace std;
  25. using namespace isc::util::python;
  26. using namespace isc::dns;
  27. using namespace isc::dns::python;
  28. //
  29. // Definition of the classes
  30. //
  31. // For each class, we need a struct, a helper functions (init, destroy,
  32. // and static wrappers around the methods we export), a list of methods,
  33. // and a type description
  34. //
  35. // TSIGRecord
  36. //
  37. // Trivial constructor.
  38. s_TSIGRecord::s_TSIGRecord() : cppobj(NULL) {
  39. }
  40. namespace {
  41. // Shortcut type which would be convenient for adding class variables safely.
  42. typedef CPPPyObjectContainer<s_TSIGRecord, TSIGRecord> TSIGRecordContainer;
  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 TSIGRecord_init(s_TSIGRecord* self, PyObject* args);
  49. void TSIGRecord_destroy(s_TSIGRecord* self);
  50. PyObject* TSIGRecord_toText(const s_TSIGRecord* const self);
  51. PyObject* TSIGRecord_str(PyObject* self);
  52. PyObject* TSIGRecord_toWire(const s_TSIGRecord* self, PyObject* args);
  53. PyObject* TSIGRecord_getName(const s_TSIGRecord* self);
  54. PyObject* TSIGRecord_getLength(const s_TSIGRecord* self);
  55. PyObject* TSIGRecord_getRdata(const s_TSIGRecord* self);
  56. // These are the functions we export
  57. // For a minimal support, we don't need them.
  58. // This list contains the actual set of functions we have in
  59. // python. Each entry has
  60. // 1. Python method name
  61. // 2. Our static function here
  62. // 3. Argument type
  63. // 4. Documentation
  64. PyMethodDef TSIGRecord_methods[] = {
  65. { "get_name", reinterpret_cast<PyCFunction>(TSIGRecord_getName),
  66. METH_NOARGS,
  67. "Return the owner name of the TSIG RR, which is the TSIG key name" },
  68. { "get_length", reinterpret_cast<PyCFunction>(TSIGRecord_getLength),
  69. METH_NOARGS,
  70. "Return the length of the TSIG record" },
  71. { "get_rdata", reinterpret_cast<PyCFunction>(TSIGRecord_getRdata),
  72. METH_NOARGS,
  73. "Return the RDATA of the TSIG RR" },
  74. { "to_text", reinterpret_cast<PyCFunction>(TSIGRecord_toText), METH_NOARGS,
  75. "Returns the text representation" },
  76. { "to_wire", reinterpret_cast<PyCFunction>(TSIGRecord_toWire),
  77. METH_VARARGS,
  78. "Converts the TSIGRecord object to wire format.\n"
  79. "The argument can be either a MessageRenderer or an object that "
  80. "implements the sequence interface. If the object is mutable "
  81. "(for instance a bytearray()), the wire data is added in-place.\n"
  82. "If it is not (for instance a bytes() object), a new object is "
  83. "returned" },
  84. { NULL, NULL, 0, NULL }
  85. };
  86. int
  87. TSIGRecord_init(s_TSIGRecord* self, PyObject* args) {
  88. try {
  89. const s_Name* py_name;
  90. const s_TSIG* py_tsig;
  91. if (PyArg_ParseTuple(args, "O!O!", &name_type, &py_name,
  92. &tsig_type, &py_tsig)) {
  93. self->cppobj = new TSIGRecord(*py_name->cppobj, *py_tsig->cppobj);
  94. return (0);
  95. }
  96. } catch (const exception& ex) {
  97. const string ex_what = "Failed to construct TSIGRecord object: " +
  98. string(ex.what());
  99. PyErr_SetString(po_IscException, ex_what.c_str());
  100. return (-1);
  101. } catch (...) {
  102. PyErr_SetString(po_IscException,
  103. "Unexpected exception in constructing TSIGRecord");
  104. return (-1);
  105. }
  106. PyErr_SetString(PyExc_TypeError,
  107. "Invalid arguments to TSIGRecord constructor");
  108. return (-1);
  109. }
  110. // This is a template of typical code logic of python object destructor.
  111. // In many cases you can use it without modification, but check that carefully.
  112. void
  113. TSIGRecord_destroy(s_TSIGRecord* const self) {
  114. delete self->cppobj;
  115. self->cppobj = NULL;
  116. Py_TYPE(self)->tp_free(self);
  117. }
  118. // This should be able to be used without modification as long as the
  119. // underlying C++ class has toText().
  120. PyObject*
  121. TSIGRecord_toText(const s_TSIGRecord* const self) {
  122. try {
  123. // toText() could throw, so we need to catch any exceptions below.
  124. return (Py_BuildValue("s", self->cppobj->toText().c_str()));
  125. } catch (const exception& ex) {
  126. const string ex_what =
  127. "Failed to convert TSIGRecord object to text: " +
  128. string(ex.what());
  129. PyErr_SetString(po_IscException, ex_what.c_str());
  130. } catch (...) {
  131. PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
  132. "converting TSIGRecord object to text");
  133. }
  134. return (NULL);
  135. }
  136. PyObject*
  137. TSIGRecord_str(PyObject* self) {
  138. // Simply call the to_text method we already defined
  139. return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
  140. const_cast<char*>("")));
  141. }
  142. PyObject*
  143. TSIGRecord_toWire(const s_TSIGRecord* const self, PyObject* args) {
  144. typedef ToWireCallInt<const TSIGRecord> ToWireCall;
  145. PyObject* (*towire_fn)(const s_TSIGRecord* const, PyObject*) =
  146. toWireWrapper<s_TSIGRecord, TSIGRecord, ToWireCall>;
  147. return (towire_fn(self, args));
  148. }
  149. PyObject*
  150. TSIGRecord_getName(const s_TSIGRecord* const self) {
  151. try {
  152. return (createNameObject(self->cppobj->getName()));
  153. } catch (const exception& ex) {
  154. const string ex_what =
  155. "Failed to get TSIGRecord name: " + string(ex.what());
  156. PyErr_SetString(po_IscException, ex_what.c_str());
  157. } catch (...) {
  158. PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
  159. "getting TSIGRecord name");
  160. }
  161. return (NULL);
  162. }
  163. PyObject*
  164. TSIGRecord_getLength(const s_TSIGRecord* const self) {
  165. return (Py_BuildValue("H", self->cppobj->getLength()));
  166. }
  167. PyObject*
  168. TSIGRecord_getRdata(const s_TSIGRecord* const self) {
  169. try {
  170. return (createTSIGObject(self->cppobj->getRdata()));
  171. } catch (const exception& ex) {
  172. const string ex_what =
  173. "Failed to get TSIGRecord RDATA: " + string(ex.what());
  174. PyErr_SetString(po_IscException, ex_what.c_str());
  175. } catch (...) {
  176. PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
  177. "getting TSIGRecord RDATA");
  178. }
  179. return (NULL);
  180. }
  181. } // end of unnamed namespace
  182. namespace isc {
  183. namespace dns {
  184. namespace python {
  185. // This defines the complete type for reflection in python and
  186. // parsing of PyObject* to s_TSIGRecord
  187. // Most of the functions are not actually implemented and NULL here.
  188. PyTypeObject tsigrecord_type = {
  189. PyVarObject_HEAD_INIT(NULL, 0)
  190. "pydnspp.TSIGRecord",
  191. sizeof(s_TSIGRecord), // tp_basicsize
  192. 0, // tp_itemsize
  193. reinterpret_cast<destructor>(TSIGRecord_destroy), // tp_dealloc
  194. NULL, // tp_print
  195. NULL, // tp_getattr
  196. NULL, // tp_setattr
  197. NULL, // tp_reserved
  198. NULL, // tp_repr
  199. NULL, // tp_as_number
  200. NULL, // tp_as_sequence
  201. NULL, // tp_as_mapping
  202. NULL, // tp_hash
  203. NULL, // tp_call
  204. TSIGRecord_str, // tp_str
  205. NULL, // tp_getattro
  206. NULL, // tp_setattro
  207. NULL, // tp_as_buffer
  208. Py_TPFLAGS_DEFAULT, // tp_flags
  209. "The TSIGRecord class objects is...(COMPLETE THIS)",
  210. NULL, // tp_traverse
  211. NULL, // tp_clear
  212. NULL, // tp_richcompare
  213. 0, // tp_weaklistoffset
  214. NULL, // tp_iter
  215. NULL, // tp_iternext
  216. TSIGRecord_methods, // tp_methods
  217. NULL, // tp_members
  218. NULL, // tp_getset
  219. NULL, // tp_base
  220. NULL, // tp_dict
  221. NULL, // tp_descr_get
  222. NULL, // tp_descr_set
  223. 0, // tp_dictoffset
  224. reinterpret_cast<initproc>(TSIGRecord_init), // tp_init
  225. NULL, // tp_alloc
  226. PyType_GenericNew, // tp_new
  227. NULL, // tp_free
  228. NULL, // tp_is_gc
  229. NULL, // tp_bases
  230. NULL, // tp_mro
  231. NULL, // tp_cache
  232. NULL, // tp_subclasses
  233. NULL, // tp_weaklist
  234. NULL, // tp_del
  235. 0 // tp_version_tag
  236. };
  237. // Module Initialization, all statics are initialized here
  238. bool
  239. initModulePart_TSIGRecord(PyObject* mod) {
  240. // We initialize the static description object with PyType_Ready(),
  241. // then add it to the module. This is not just a check! (leaving
  242. // this out results in segmentation faults)
  243. if (PyType_Ready(&tsigrecord_type) < 0) {
  244. return (false);
  245. }
  246. void* p = &tsigrecord_type;
  247. if (PyModule_AddObject(mod, "TSIGRecord", static_cast<PyObject*>(p)) < 0) {
  248. return (false);
  249. }
  250. Py_INCREF(&tsigrecord_type);
  251. // The following template is the typical procedure for installing class
  252. // variables. If the class doesn't have a class variable, remove the
  253. // entire try-catch clauses.
  254. try {
  255. // Constant class variables
  256. installClassVariable(tsigrecord_type, "TSIG_TTL",
  257. Py_BuildValue("I", 0));
  258. } catch (const exception& ex) {
  259. const string ex_what =
  260. "Unexpected failure in TSIGRecord initialization: " +
  261. string(ex.what());
  262. PyErr_SetString(po_IscException, ex_what.c_str());
  263. return (false);
  264. } catch (...) {
  265. PyErr_SetString(PyExc_SystemError,
  266. "Unexpected failure in TSIGRecord initialization");
  267. return (false);
  268. }
  269. return (true);
  270. }
  271. PyObject*
  272. createTSIGRecordObject(const TSIGRecord& source) {
  273. TSIGRecordContainer container = PyObject_New(s_TSIGRecord,
  274. &tsigrecord_type);
  275. container.set(new TSIGRecord(source));
  276. return (container.release());
  277. }
  278. } // namespace python
  279. } // namespace dns
  280. } // namespace isc