rdata_python.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <dns/rdata.h>
  15. using namespace isc::dns;
  16. using namespace isc::util;
  17. using namespace isc::dns::rdata;
  18. //
  19. // Declaration of the custom exceptions
  20. // Initialization and addition of these go in the initModulePart
  21. // function at the end of this file
  22. //
  23. static PyObject* po_InvalidRdataLength;
  24. static PyObject* po_InvalidRdataText;
  25. static PyObject* po_CharStringTooLong;
  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. // Rdata
  34. //
  35. // The s_* Class simply coverst one instantiation of the object
  36. // Using a shared_ptr here should not really be necessary (PyObject
  37. // is already reference-counted), however internally on the cpp side,
  38. // not doing so might result in problems, since we can't copy construct
  39. // rdata field, adding them to rrsets results in a problem when the
  40. // rrset is destroyed later
  41. class s_Rdata : public PyObject {
  42. public:
  43. RdataPtr rdata;
  44. };
  45. //
  46. // We declare the functions here, the definitions are below
  47. // the type definition of the object, since both can use the other
  48. //
  49. // General creation and destruction
  50. static int Rdata_init(s_Rdata* self, PyObject* args);
  51. static void Rdata_destroy(s_Rdata* self);
  52. // These are the functions we export
  53. static PyObject* Rdata_toText(s_Rdata* self);
  54. // This is a second version of toText, we need one where the argument
  55. // is a PyObject*, for the str() function in python.
  56. static PyObject* Rdata_str(PyObject* self);
  57. static PyObject* Rdata_toWire(s_Rdata* self, PyObject* args);
  58. static PyObject* RData_richcmp(s_Rdata* self, s_Rdata* other, int op);
  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. static PyMethodDef Rdata_methods[] = {
  66. { "to_text", reinterpret_cast<PyCFunction>(Rdata_toText), METH_NOARGS,
  67. "Returns the string representation" },
  68. { "to_wire", reinterpret_cast<PyCFunction>(Rdata_toWire), METH_VARARGS,
  69. "Converts the Rdata object to wire format.\n"
  70. "The argument can be either a MessageRenderer or an object that "
  71. "implements the sequence interface. If the object is mutable "
  72. "(for instance a bytearray()), the wire data is added in-place.\n"
  73. "If it is not (for instance a bytes() object), a new object is "
  74. "returned" },
  75. { NULL, NULL, 0, NULL }
  76. };
  77. // This defines the complete type for reflection in python and
  78. // parsing of PyObject* to s_Rdata
  79. // Most of the functions are not actually implemented and NULL here.
  80. static PyTypeObject rdata_type = {
  81. PyVarObject_HEAD_INIT(NULL, 0)
  82. "pydnspp.Rdata",
  83. sizeof(s_Rdata), // tp_basicsize
  84. 0, // tp_itemsize
  85. (destructor)Rdata_destroy, // tp_dealloc
  86. NULL, // tp_print
  87. NULL, // tp_getattr
  88. NULL, // tp_setattr
  89. NULL, // tp_reserved
  90. NULL, // tp_repr
  91. NULL, // tp_as_number
  92. NULL, // tp_as_sequence
  93. NULL, // tp_as_mapping
  94. NULL, // tp_hash
  95. NULL, // tp_call
  96. Rdata_str, // tp_str
  97. NULL, // tp_getattro
  98. NULL, // tp_setattro
  99. NULL, // tp_as_buffer
  100. Py_TPFLAGS_DEFAULT, // tp_flags
  101. "The Rdata class is an abstract base class that provides "
  102. "a set of common interfaces to manipulate concrete RDATA objects.",
  103. NULL, // tp_traverse
  104. NULL, // tp_clear
  105. (richcmpfunc)RData_richcmp, // tp_richcompare
  106. 0, // tp_weaklistoffset
  107. NULL, // tp_iter
  108. NULL, // tp_iternext
  109. Rdata_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)Rdata_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. NULL, // tp_del
  128. 0 // tp_version_tag
  129. };
  130. static int
  131. Rdata_init(s_Rdata* self, PyObject* args) {
  132. s_RRType* rrtype;
  133. s_RRClass* rrclass;
  134. const char* s;
  135. const char* data;
  136. Py_ssize_t len;
  137. // Create from string
  138. if (PyArg_ParseTuple(args, "O!O!s", &rrtype_type, &rrtype,
  139. &rrclass_type, &rrclass,
  140. &s)) {
  141. self->rdata = createRdata(*rrtype->rrtype, *rrclass->rrclass, s);
  142. return (0);
  143. } else if (PyArg_ParseTuple(args, "O!O!y#", &rrtype_type, &rrtype,
  144. &rrclass_type, &rrclass, &data, &len)) {
  145. InputBuffer input_buffer(data, len);
  146. self->rdata = createRdata(*rrtype->rrtype, *rrclass->rrclass,
  147. input_buffer, len);
  148. return (0);
  149. }
  150. return (-1);
  151. }
  152. static void
  153. Rdata_destroy(s_Rdata* self) {
  154. // Clear the shared_ptr so that its reference count is zero
  155. // before we call tp_free() (there is no direct release())
  156. self->rdata.reset();
  157. Py_TYPE(self)->tp_free(self);
  158. }
  159. static PyObject*
  160. Rdata_toText(s_Rdata* self) {
  161. // Py_BuildValue makes python objects from native data
  162. return (Py_BuildValue("s", self->rdata->toText().c_str()));
  163. }
  164. static PyObject*
  165. Rdata_str(PyObject* self) {
  166. // Simply call the to_text method we already defined
  167. return (PyObject_CallMethod(self,
  168. const_cast<char*>("to_text"),
  169. const_cast<char*>("")));
  170. }
  171. static PyObject*
  172. Rdata_toWire(s_Rdata* self, PyObject* args) {
  173. PyObject* bytes;
  174. s_MessageRenderer* mr;
  175. if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  176. PyObject* bytes_o = bytes;
  177. OutputBuffer buffer(4);
  178. self->rdata->toWire(buffer);
  179. PyObject* rd_bytes = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()), buffer.getLength());
  180. PyObject* result = PySequence_InPlaceConcat(bytes_o, rd_bytes);
  181. // We need to release the object we temporarily created here
  182. // to prevent memory leak
  183. Py_DECREF(rd_bytes);
  184. return (result);
  185. } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
  186. self->rdata->toWire(*mr->messagerenderer);
  187. // If we return NULL it is seen as an error, so use this for
  188. // None returns
  189. Py_RETURN_NONE;
  190. }
  191. PyErr_Clear();
  192. PyErr_SetString(PyExc_TypeError,
  193. "toWire argument must be a sequence object or a MessageRenderer");
  194. return (NULL);
  195. }
  196. static PyObject*
  197. RData_richcmp(s_Rdata* self, s_Rdata* other, int op) {
  198. bool c;
  199. // Check for null and if the types match. If different type,
  200. // simply return False
  201. if (!other || (self->ob_type != other->ob_type)) {
  202. Py_RETURN_FALSE;
  203. }
  204. switch (op) {
  205. case Py_LT:
  206. c = self->rdata->compare(*other->rdata) < 0;
  207. break;
  208. case Py_LE:
  209. c = self->rdata->compare(*other->rdata) < 0 ||
  210. self->rdata->compare(*other->rdata) == 0;
  211. break;
  212. case Py_EQ:
  213. c = self->rdata->compare(*other->rdata) == 0;
  214. break;
  215. case Py_NE:
  216. c = self->rdata->compare(*other->rdata) != 0;
  217. break;
  218. case Py_GT:
  219. c = self->rdata->compare(*other->rdata) > 0;
  220. break;
  221. case Py_GE:
  222. c = self->rdata->compare(*other->rdata) > 0 ||
  223. self->rdata->compare(*other->rdata) == 0;
  224. break;
  225. default:
  226. PyErr_SetString(PyExc_IndexError,
  227. "Unhandled rich comparison operator");
  228. return (NULL);
  229. }
  230. if (c)
  231. Py_RETURN_TRUE;
  232. else
  233. Py_RETURN_FALSE;
  234. }
  235. // end of Rdata
  236. // Module Initialization, all statics are initialized here
  237. bool
  238. initModulePart_Rdata(PyObject* mod) {
  239. // We initialize the static description object with PyType_Ready(),
  240. // then add it to the module. This is not just a check! (leaving
  241. // this out results in segmentation faults)
  242. if (PyType_Ready(&rdata_type) < 0) {
  243. return (false);
  244. }
  245. Py_INCREF(&rdata_type);
  246. PyModule_AddObject(mod, "Rdata",
  247. reinterpret_cast<PyObject*>(&rdata_type));
  248. // Add the exceptions to the class
  249. po_InvalidRdataLength = PyErr_NewException("pydnspp.InvalidRdataLength", NULL, NULL);
  250. PyModule_AddObject(mod, "InvalidRdataLength", po_InvalidRdataLength);
  251. po_InvalidRdataText = PyErr_NewException("pydnspp.InvalidRdataText", NULL, NULL);
  252. PyModule_AddObject(mod, "InvalidRdataText", po_InvalidRdataText);
  253. po_CharStringTooLong = PyErr_NewException("pydnspp.CharStringTooLong", NULL, NULL);
  254. PyModule_AddObject(mod, "CharStringTooLong", po_CharStringTooLong);
  255. return (true);
  256. }