rdata_python.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/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. s_RRType* rrtype;
  129. s_RRClass* rrclass;
  130. const char* s;
  131. if (PyArg_ParseTuple(args, "O!O!s", &rrtype_type, &rrtype,
  132. &rrclass_type, &rrclass,
  133. &s, &s
  134. )) {
  135. const std::string str(s);
  136. self->rdata = createRdata(*rrtype->rrtype, *rrclass->rrclass, s);
  137. return 0;
  138. }
  139. return -1;
  140. }
  141. static void
  142. Rdata_destroy(s_Rdata* self) {
  143. // Clear the shared_ptr so that its reference count is zero
  144. // before we call tp_free() (there is no direct release())
  145. self->rdata.reset();
  146. Py_TYPE(self)->tp_free(self);
  147. }
  148. static PyObject*
  149. Rdata_toText(s_Rdata* self) {
  150. // Py_BuildValue makes python objects from native data
  151. return Py_BuildValue("s", self->rdata->toText().c_str());
  152. }
  153. static PyObject*
  154. Rdata_str(PyObject* self) {
  155. // Simply call the to_text method we already defined
  156. return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
  157. }
  158. static PyObject*
  159. Rdata_toWire(s_Rdata* self, PyObject* args) {
  160. PyObject* bytes;
  161. s_MessageRenderer* mr;
  162. if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  163. PyObject* bytes_o = bytes;
  164. OutputBuffer buffer(2);
  165. self->rdata->toWire(buffer);
  166. PyObject* n = PyBytes_FromStringAndSize((const char*) buffer.getData(), buffer.getLength());
  167. PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
  168. // We need to release the object we temporarily created here
  169. // to prevent memory leak
  170. Py_DECREF(n);
  171. return result;
  172. } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, (PyObject**) &mr)) {
  173. self->rdata->toWire(*mr->messagerenderer);
  174. // If we return NULL it is seen as an error, so use this for
  175. // None returns
  176. Py_RETURN_NONE;
  177. }
  178. PyErr_Clear();
  179. PyErr_SetString(PyExc_TypeError,
  180. "toWire argument must be a sequence object or a MessageRenderer");
  181. return NULL;
  182. }
  183. // end of Rdata
  184. // Module Initialization, all statics are initialized here
  185. bool
  186. initModulePart_Rdata(PyObject* mod) {
  187. // We initialize the static description object with PyType_Ready(),
  188. // then add it to the module. This is not just a check! (leaving
  189. // this out results in segmentation faults)
  190. if (PyType_Ready(&rdata_type) < 0) {
  191. return false;
  192. }
  193. Py_INCREF(&rdata_type);
  194. PyModule_AddObject(mod, "Rdata",
  195. (PyObject*) &rdata_type);
  196. // Add the exceptions to the class
  197. po_InvalidRdataLength = PyErr_NewException("libdns_python.InvalidRdataLength", NULL, NULL);
  198. PyModule_AddObject(mod, "InvalidRdataLength", po_InvalidRdataLength);
  199. po_InvalidRdataText = PyErr_NewException("libdns_python.InvalidRdataText", NULL, NULL);
  200. PyModule_AddObject(mod, "InvalidRdataText", po_InvalidRdataText);
  201. po_CharStringTooLong = PyErr_NewException("libdns_python.CharStringTooLong", NULL, NULL);
  202. PyModule_AddObject(mod, "CharStringTooLong", po_CharStringTooLong);
  203. return true;
  204. }