rdata_python.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (C) 2009 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/rdata.h>
  16. using namespace isc::dns;
  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. typedef struct {
  37. PyObject_HEAD
  38. RdataPtr rdata;
  39. } s_Rdata;
  40. //
  41. // We declare the functions here, the definitions are below
  42. // the type definition of the object, since both can use the other
  43. //
  44. // General creation and destruction
  45. static int Rdata_init(s_Rdata* self, PyObject* args);
  46. static void Rdata_destroy(s_Rdata* self);
  47. // These are the functions we export
  48. static PyObject* Rdata_toText(s_Rdata* self);
  49. // This is a second version of toText, we need one where the argument
  50. // is a PyObject*, for the str() function in python.
  51. static PyObject* Rdata_str(PyObject* self);
  52. static PyObject* Rdata_toWire(s_Rdata* self, PyObject* args);
  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 Rdata_methods[] = {
  60. { "to_text", (PyCFunction)Rdata_toText, METH_NOARGS,
  61. "Returns the string representation" },
  62. { "to_wire", (PyCFunction)Rdata_toWire, METH_VARARGS,
  63. "Converts the Rdata 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. { NULL, NULL, 0, NULL }
  70. };
  71. // This defines the complete type for reflection in python and
  72. // parsing of PyObject* to s_Rdata
  73. // Most of the functions are not actually implemented and NULL here.
  74. static PyTypeObject rdata_type = {
  75. PyVarObject_HEAD_INIT(NULL, 0)
  76. "libdns_python.Rdata",
  77. sizeof(s_Rdata), /* tp_basicsize */
  78. 0, /* tp_itemsize */
  79. (destructor)Rdata_destroy, /* tp_dealloc */
  80. NULL, /* tp_print */
  81. NULL, /* tp_getattr */
  82. NULL, /* tp_setattr */
  83. NULL, /* tp_reserved */
  84. NULL, /* tp_repr */
  85. NULL, /* tp_as_number */
  86. NULL, /* tp_as_sequence */
  87. NULL, /* tp_as_mapping */
  88. NULL, /* tp_hash */
  89. NULL, /* tp_call */
  90. Rdata_str, /* tp_str */
  91. NULL, /* tp_getattro */
  92. NULL, /* tp_setattro */
  93. NULL, /* tp_as_buffer */
  94. Py_TPFLAGS_DEFAULT, /* tp_flags */
  95. "The Rdata class is an abstract base class that provides "
  96. "a set of common interfaces to manipulate concrete RDATA objects.",
  97. NULL, /* tp_traverse */
  98. NULL, /* tp_clear */
  99. NULL, /* tp_richcompare */
  100. 0, /* tp_weaklistoffset */
  101. NULL, /* tp_iter */
  102. NULL, /* tp_iternext */
  103. Rdata_methods, /* tp_methods */
  104. NULL, /* tp_members */
  105. NULL, /* tp_getset */
  106. NULL, /* tp_base */
  107. NULL, /* tp_dict */
  108. NULL, /* tp_descr_get */
  109. NULL, /* tp_descr_set */
  110. 0, /* tp_dictoffset */
  111. (initproc)Rdata_init, /* tp_init */
  112. NULL, /* tp_alloc */
  113. PyType_GenericNew, /* tp_new */
  114. NULL, /* tp_free */
  115. NULL, /* tp_is_gc */
  116. NULL, /* tp_bases */
  117. NULL, /* tp_mro */
  118. NULL, /* tp_cache */
  119. NULL, /* tp_subclasses */
  120. NULL, /* tp_weaklist */
  121. // Note: not sure if the following are correct. Added them just to
  122. // make the compiler happy.
  123. NULL, /* tp_del */
  124. 0 /* tp_version_tag */
  125. };
  126. static int
  127. Rdata_init(s_Rdata* self, PyObject* args)
  128. {
  129. s_RRType* rrtype;
  130. s_RRClass* rrclass;
  131. const char* s;
  132. if (PyArg_ParseTuple(args, "O!O!s", &rrtype_type, &rrtype,
  133. &rrclass_type, &rrclass,
  134. &s, &s
  135. )) {
  136. const std::string str(s);
  137. self->rdata = createRdata(*rrtype->rrtype, *rrclass->rrclass, s);
  138. return 0;
  139. }
  140. return -1;
  141. }
  142. static void
  143. Rdata_destroy(s_Rdata* self)
  144. {
  145. // Clear the shared_ptr so that its reference count is zero
  146. // before we call tp_free() (there is no direct release())
  147. self->rdata.reset();
  148. Py_TYPE(self)->tp_free(self);
  149. }
  150. static PyObject*
  151. Rdata_toText(s_Rdata* self)
  152. {
  153. // Py_BuildValue makes python objects from native data
  154. return Py_BuildValue("s", self->rdata->toText().c_str());
  155. }
  156. static PyObject*
  157. Rdata_str(PyObject* self)
  158. {
  159. // Simply call the to_text method we already defined
  160. return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
  161. }
  162. static PyObject*
  163. Rdata_toWire(s_Rdata* self, PyObject* args)
  164. {
  165. PyObject* bytes;
  166. s_MessageRenderer* mr;
  167. if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  168. PyObject* bytes_o = bytes;
  169. OutputBuffer buffer(2);
  170. self->rdata->toWire(buffer);
  171. PyObject* n = PyBytes_FromStringAndSize((const char*) buffer.getData(), buffer.getLength());
  172. PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
  173. // We need to release the object we temporarily created here
  174. // to prevent memory leak
  175. Py_DECREF(n);
  176. return result;
  177. } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, (PyObject**) &mr)) {
  178. self->rdata->toWire(*mr->messagerenderer);
  179. // If we return NULL it is seen as an error, so use this for
  180. // None returns
  181. Py_RETURN_NONE;
  182. }
  183. PyErr_Clear();
  184. PyErr_SetString(PyExc_TypeError,
  185. "toWire argument must be a sequence object or a MessageRenderer");
  186. return NULL;
  187. }
  188. // end of Rdata
  189. // Module Initialization, all statics are initialized here
  190. bool
  191. initModulePart_Rdata(PyObject* mod)
  192. {
  193. // We initialize the static description object with PyType_Ready(),
  194. // then add it to the module. This is not just a check! (leaving
  195. // this out results in segmentation faults)
  196. if (PyType_Ready(&rdata_type) < 0) {
  197. return false;
  198. }
  199. Py_INCREF(&rdata_type);
  200. PyModule_AddObject(mod, "Rdata",
  201. (PyObject*) &rdata_type);
  202. // Add the exceptions to the class
  203. po_InvalidRdataLength = PyErr_NewException("libdns_python.InvalidRdataLength", NULL, NULL);
  204. PyModule_AddObject(mod, "InvalidRdataLength", po_InvalidRdataLength);
  205. po_InvalidRdataText = PyErr_NewException("libdns_python.InvalidRdataText", NULL, NULL);
  206. PyModule_AddObject(mod, "InvalidRdataText", po_InvalidRdataText);
  207. po_CharStringTooLong = PyErr_NewException("libdns_python.CharStringTooLong", NULL, NULL);
  208. PyModule_AddObject(mod, "CharStringTooLong", po_CharStringTooLong);
  209. return true;
  210. }