rrttl_python.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // $Id$
  15. #include <dns/rrttl.h>
  16. using namespace isc::dns;
  17. //
  18. // Declaration of the custom exceptions
  19. // Initialization and addition of these go in the initModulePart
  20. // function at the end of this file
  21. //
  22. static PyObject* po_InvalidRRTTL;
  23. static PyObject* po_IncompleteRRTTL;
  24. //
  25. // Definition of the classes
  26. //
  27. // For each class, we need a struct, a helper functions (init, destroy,
  28. // and static wrappers around the methods we export), a list of methods,
  29. // and a type description
  30. //
  31. // RRTTL
  32. //
  33. // The s_* Class simply covers one instantiation of the object
  34. typedef struct {
  35. PyObject_HEAD
  36. RRTTL* rrttl;
  37. } s_RRTTL;
  38. //
  39. // We declare the functions here, the definitions are below
  40. // the type definition of the object, since both can use the other
  41. //
  42. // General creation and destruction
  43. static int RRTTL_init(s_RRTTL* self, PyObject* args);
  44. static void RRTTL_destroy(s_RRTTL* self);
  45. // These are the functions we export
  46. static PyObject* RRTTL_toText(s_RRTTL* self);
  47. // This is a second version of toText, we need one where the argument
  48. // is a PyObject*, for the str() function in python.
  49. static PyObject* RRTTL_str(PyObject* self);
  50. static PyObject* RRTTL_toWire(s_RRTTL* self, PyObject* args);
  51. static PyObject* RRTTL_getValue(s_RRTTL* self);
  52. static PyObject* RRTTL_richcmp(s_RRTTL* self, s_RRTTL* other, int op);
  53. // This list contains the actual set of functions we have in
  54. // python. Each entry has
  55. // 1. Python method name
  56. // 2. Our static function here
  57. // 3. Argument type
  58. // 4. Documentation
  59. static PyMethodDef RRTTL_methods[] = {
  60. { "to_text", (PyCFunction)RRTTL_toText, METH_NOARGS,
  61. "Returns the string representation" },
  62. { "to_wire", (PyCFunction)RRTTL_toWire, METH_VARARGS,
  63. "Converts the RRTTL object to wire format.\n"
  64. "The argument can be either a MessageRenderer or an object that "
  65. "implements the sequence interface. If the object is mutable "
  66. "(for instance a bytearray()), the wire data is added in-place.\n"
  67. "If it is not (for instance a bytes() object), a new object is "
  68. "returned" },
  69. { "get_value", (PyCFunction)RRTTL_getValue, METH_NOARGS,
  70. "Returns the TTL as an integer" },
  71. { NULL, NULL, 0, NULL }
  72. };
  73. // This defines the complete type for reflection in python and
  74. // parsing of PyObject* to s_RRTTL
  75. // Most of the functions are not actually implemented and NULL here.
  76. static PyTypeObject rrttl_type = {
  77. PyVarObject_HEAD_INIT(NULL, 0)
  78. "libdns_python.RRTTL",
  79. sizeof(s_RRTTL), /* tp_basicsize */
  80. 0, /* tp_itemsize */
  81. (destructor)RRTTL_destroy, /* tp_dealloc */
  82. NULL, /* tp_print */
  83. NULL, /* tp_getattr */
  84. NULL, /* tp_setattr */
  85. NULL, /* tp_reserved */
  86. NULL, /* tp_repr */
  87. NULL, /* tp_as_number */
  88. NULL, /* tp_as_sequence */
  89. NULL, /* tp_as_mapping */
  90. NULL, /* tp_hash */
  91. NULL, /* tp_call */
  92. RRTTL_str, /* tp_str */
  93. NULL, /* tp_getattro */
  94. NULL, /* tp_setattro */
  95. NULL, /* tp_as_buffer */
  96. Py_TPFLAGS_DEFAULT, /* tp_flags */
  97. "The RRTTL class encapsulates TTLs used in DNS resource records.\n\n"
  98. "This is a straightforward class; an RRTTL object simply maintains a "
  99. "32-bit unsigned integer corresponding to the TTL value. The main purpose "
  100. "of this class is to provide convenient interfaces to convert a textual "
  101. "representation into the integer TTL value and vice versa, and to handle "
  102. "wire-format representations.",
  103. NULL, /* tp_traverse */
  104. NULL, /* tp_clear */
  105. (richcmpfunc)RRTTL_richcmp, /* tp_richcompare */
  106. 0, /* tp_weaklistoffset */
  107. NULL, /* tp_iter */
  108. NULL, /* tp_iternext */
  109. RRTTL_methods, /* tp_methods */
  110. NULL, /* tp_members */
  111. NULL, /* tp_getset */
  112. NULL, /* tp_base */
  113. NULL, /* tp_dict */
  114. NULL, /* tp_descr_get */
  115. NULL, /* tp_descr_set */
  116. 0, /* tp_dictoffset */
  117. (initproc)RRTTL_init, /* tp_init */
  118. NULL, /* tp_alloc */
  119. PyType_GenericNew, /* tp_new */
  120. NULL, /* tp_free */
  121. NULL, /* tp_is_gc */
  122. NULL, /* tp_bases */
  123. NULL, /* tp_mro */
  124. NULL, /* tp_cache */
  125. NULL, /* tp_subclasses */
  126. NULL, /* tp_weaklist */
  127. // Note: not sure if the following are correct. Added them just to
  128. // make the compiler happy.
  129. NULL, /* tp_del */
  130. 0 /* tp_version_tag */
  131. };
  132. static int
  133. RRTTL_init(s_RRTTL* self, PyObject* args) {
  134. const char* s;
  135. unsigned long i;
  136. PyObject* bytes = NULL;
  137. // The constructor argument can be a string ("IN"), an integer (1),
  138. // or a sequence of numbers between 0 and 255 (wire code)
  139. // Note that PyArg_ParseType can set PyError, and we need to clear
  140. // that if we try several like here. Otherwise the *next* python
  141. // call will suddenly appear to throw an exception.
  142. // (the way to do exceptions is to set PyErr and return -1)
  143. try {
  144. if (PyArg_ParseTuple(args, "s", &s)) {
  145. self->rrttl = new RRTTL(s);
  146. return 0;
  147. } else if (PyArg_ParseTuple(args, "I", &i)) {
  148. PyErr_Clear();
  149. self->rrttl = new RRTTL(i);
  150. return 0;
  151. } else if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  152. Py_ssize_t size = PySequence_Size(bytes);
  153. uint8_t data[size];
  154. int result = readDataFromSequence(data, size, bytes);
  155. if (result != 0) {
  156. return result;
  157. }
  158. InputBuffer ib(data, size);
  159. self->rrttl = new RRTTL(ib);
  160. PyErr_Clear();
  161. return 0;
  162. }
  163. } catch (IncompleteRRTTL icc) {
  164. // Ok so one of our functions has thrown a C++ exception.
  165. // We need to translate that to a Python Exception
  166. // First clear any existing error that was set
  167. PyErr_Clear();
  168. // Now set our own exception
  169. PyErr_SetString(po_IncompleteRRTTL, icc.what());
  170. // And return negative
  171. return -1;
  172. } catch (InvalidRRTTL ic) {
  173. PyErr_Clear();
  174. PyErr_SetString(po_InvalidRRTTL, ic.what());
  175. return -1;
  176. }
  177. PyErr_Clear();
  178. PyErr_SetString(PyExc_TypeError,
  179. "no valid type in constructor argument");
  180. return -1;
  181. }
  182. static void
  183. RRTTL_destroy(s_RRTTL* self) {
  184. if (self->rrttl != NULL)
  185. delete self->rrttl;
  186. self->rrttl = NULL;
  187. Py_TYPE(self)->tp_free(self);
  188. }
  189. static PyObject*
  190. RRTTL_toText(s_RRTTL* self) {
  191. // Py_BuildValue makes python objects from native data
  192. return Py_BuildValue("s", self->rrttl->toText().c_str());
  193. }
  194. static PyObject*
  195. RRTTL_str(PyObject* self) {
  196. // Simply call the to_text method we already defined
  197. return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
  198. }
  199. static PyObject*
  200. RRTTL_toWire(s_RRTTL* self, PyObject* args) {
  201. PyObject* bytes;
  202. s_MessageRenderer* mr;
  203. if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  204. PyObject* bytes_o = bytes;
  205. OutputBuffer buffer(2);
  206. self->rrttl->toWire(buffer);
  207. PyObject* n = PyBytes_FromStringAndSize((const char*) buffer.getData(), buffer.getLength());
  208. PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
  209. // We need to release the object we temporarily created here
  210. // to prevent memory leak
  211. Py_DECREF(n);
  212. return result;
  213. } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, (PyObject**) &mr)) {
  214. self->rrttl->toWire(*mr->messagerenderer);
  215. // If we return NULL it is seen as an error, so use this for
  216. // None returns
  217. Py_RETURN_NONE;
  218. }
  219. PyErr_Clear();
  220. PyErr_SetString(PyExc_TypeError,
  221. "toWire argument must be a sequence object or a MessageRenderer");
  222. return NULL;
  223. }
  224. static PyObject*
  225. RRTTL_getValue(s_RRTTL* self) {
  226. return Py_BuildValue("I", self->rrttl->getValue());
  227. }
  228. static PyObject*
  229. RRTTL_richcmp(s_RRTTL* self, s_RRTTL* other, int op) {
  230. bool c = false;
  231. // Check for null and if the types match. If different type,
  232. // simply return False
  233. if (!other ||
  234. ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
  235. ) {
  236. Py_RETURN_FALSE;
  237. }
  238. switch (op) {
  239. case Py_LT:
  240. c = *self->rrttl < *other->rrttl;
  241. break;
  242. case Py_LE:
  243. c = *self->rrttl < *other->rrttl ||
  244. *self->rrttl == *other->rrttl;
  245. break;
  246. case Py_EQ:
  247. c = self->rrttl->equals(*other->rrttl);
  248. break;
  249. case Py_NE:
  250. c = self->rrttl->nequals(*other->rrttl);
  251. break;
  252. case Py_GT:
  253. c = *other->rrttl < *self->rrttl;
  254. break;
  255. case Py_GE:
  256. c = *other->rrttl < *self->rrttl ||
  257. *self->rrttl == *other->rrttl;
  258. break;
  259. }
  260. if (c)
  261. Py_RETURN_TRUE;
  262. else
  263. Py_RETURN_FALSE;
  264. }
  265. // end of RRTTL
  266. // Module Initialization, all statics are initialized here
  267. bool
  268. initModulePart_RRTTL(PyObject* mod) {
  269. // Add the exceptions to the module
  270. po_InvalidRRTTL = PyErr_NewException("libdns_python.InvalidRRTTL", NULL, NULL);
  271. PyModule_AddObject(mod, "InvalidRRTTL", po_InvalidRRTTL);
  272. po_IncompleteRRTTL = PyErr_NewException("libdns_python.IncompleteRRTTL", NULL, NULL);
  273. PyModule_AddObject(mod, "IncompleteRRTTL", po_IncompleteRRTTL);
  274. // We initialize the static description object with PyType_Ready(),
  275. // then add it to the module. This is not just a check! (leaving
  276. // this out results in segmentation faults)
  277. if (PyType_Ready(&rrttl_type) < 0) {
  278. return false;
  279. }
  280. Py_INCREF(&rrttl_type);
  281. PyModule_AddObject(mod, "RRTTL",
  282. (PyObject*) &rrttl_type);
  283. return true;
  284. }