tsigrecord_python.cc 12 KB

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