messagerenderer_python.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/messagerenderer.h>
  16. // For each class, we need a struct, a helper functions (init, destroy,
  17. // and static wrappers around the methods we export), a list of methods,
  18. // and a type description
  19. using namespace isc::dns;
  20. // MessageRenderer
  21. // since we don't use *Buffer in the python version (but work with
  22. // the already existing bytearray type where we use these custom buffers
  23. // in c++, we need to keep track of one here.
  24. typedef struct {
  25. PyObject_HEAD
  26. OutputBuffer* outputbuffer;
  27. MessageRenderer* messagerenderer;
  28. } s_MessageRenderer;
  29. static int MessageRenderer_init(s_MessageRenderer* self);
  30. static void MessageRenderer_destroy(s_MessageRenderer* self);
  31. static PyObject* MessageRenderer_getData(s_MessageRenderer* self);
  32. static PyObject* MessageRenderer_getLength(s_MessageRenderer* self);
  33. static PyObject* MessageRenderer_isTruncated(s_MessageRenderer* self);
  34. static PyObject* MessageRenderer_getLengthLimit(s_MessageRenderer* self);
  35. // TODO: set/get compressmode
  36. static PyObject* MessageRenderer_setTruncated(s_MessageRenderer* self);
  37. static PyObject* MessageRenderer_setLengthLimit(s_MessageRenderer* self, PyObject* args);
  38. static PyMethodDef MessageRenderer_methods[] = {
  39. { "get_data", (PyCFunction)MessageRenderer_getData, METH_NOARGS,
  40. "Returns the data as a bytes() object" },
  41. { "get_length", (PyCFunction)MessageRenderer_getLength, METH_NOARGS,
  42. "Returns the length of the data" },
  43. { "is_truncated", (PyCFunction)MessageRenderer_isTruncated, METH_NOARGS,
  44. "Returns True if the data is truncated" },
  45. { "get_length_limit", (PyCFunction)MessageRenderer_getLengthLimit, METH_NOARGS,
  46. "Returns the length limit of the data" },
  47. { "set_truncated", (PyCFunction)MessageRenderer_setTruncated, METH_NOARGS,
  48. "Sets truncated to true" },
  49. { "set_length_limit", (PyCFunction)MessageRenderer_setLengthLimit, METH_VARARGS,
  50. "Sets the length limit of the data to the given number" },
  51. { NULL, NULL, 0, NULL }
  52. };
  53. static PyTypeObject messagerenderer_type = {
  54. PyVarObject_HEAD_INIT(NULL, 0)
  55. "libdns_python.MessageRenderer",
  56. sizeof(s_MessageRenderer), /* tp_basicsize */
  57. 0, /* tp_itemsize */
  58. (destructor)MessageRenderer_destroy, /* tp_dealloc */
  59. NULL, /* tp_print */
  60. NULL, /* tp_getattr */
  61. NULL, /* tp_setattr */
  62. NULL, /* tp_reserved */
  63. NULL, /* tp_repr */
  64. NULL, /* tp_as_number */
  65. NULL, /* tp_as_sequence */
  66. NULL, /* tp_as_mapping */
  67. NULL, /* tp_hash */
  68. NULL, /* tp_call */
  69. NULL, /* tp_str */
  70. NULL, /* tp_getattro */
  71. NULL, /* tp_setattro */
  72. NULL, /* tp_as_buffer */
  73. Py_TPFLAGS_DEFAULT, /* tp_flags */
  74. "The MessageRenderer class encapsulates implementation details "
  75. "of rendering a DNS message into a buffer in wire format. "
  76. "In effect, it's simply responsible for name compression at least in the "
  77. "current implementation. A MessageRenderer class object manages the "
  78. "positions of names rendered in a buffer and uses that information to render "
  79. "subsequent names with compression.",
  80. NULL, /* tp_traverse */
  81. NULL, /* tp_clear */
  82. NULL, /* tp_richcompare */
  83. 0, /* tp_weaklistoffset */
  84. NULL, /* tp_iter */
  85. NULL, /* tp_iternext */
  86. MessageRenderer_methods, /* tp_methods */
  87. NULL, /* tp_members */
  88. NULL, /* tp_getset */
  89. NULL, /* tp_base */
  90. NULL, /* tp_dict */
  91. NULL, /* tp_descr_get */
  92. NULL, /* tp_descr_set */
  93. 0, /* tp_dictoffset */
  94. (initproc)MessageRenderer_init, /* tp_init */
  95. NULL, /* tp_alloc */
  96. PyType_GenericNew, /* tp_new */
  97. NULL, /* tp_free */
  98. NULL, /* tp_is_gc */
  99. NULL, /* tp_bases */
  100. NULL, /* tp_mro */
  101. NULL, /* tp_cache */
  102. NULL, /* tp_subclasses */
  103. NULL, /* tp_weaklist */
  104. // Note: not sure if the following are correct. Added them just to
  105. // make the compiler happy.
  106. NULL, /* tp_del */
  107. 0 /* tp_version_tag */
  108. };
  109. static int
  110. MessageRenderer_init(s_MessageRenderer* self)
  111. {
  112. self->outputbuffer = new OutputBuffer(4096);
  113. self->messagerenderer = new MessageRenderer(*self->outputbuffer);
  114. return 0;
  115. }
  116. static void
  117. MessageRenderer_destroy(s_MessageRenderer* self)
  118. {
  119. delete self->messagerenderer;
  120. delete self->outputbuffer;
  121. self->messagerenderer = NULL;
  122. Py_TYPE(self)->tp_free(self);
  123. }
  124. static PyObject*
  125. MessageRenderer_getData(s_MessageRenderer* self)
  126. {
  127. return Py_BuildValue("y#", self->messagerenderer->getData(), self->messagerenderer->getLength());
  128. }
  129. static PyObject*
  130. MessageRenderer_getLength(s_MessageRenderer* self)
  131. {
  132. return Py_BuildValue("I", self->messagerenderer->getLength());
  133. }
  134. static PyObject*
  135. MessageRenderer_isTruncated(s_MessageRenderer* self)
  136. {
  137. if (self->messagerenderer->isTruncated()) {
  138. Py_RETURN_TRUE;
  139. } else {
  140. Py_RETURN_FALSE;
  141. }
  142. }
  143. static PyObject*
  144. MessageRenderer_getLengthLimit(s_MessageRenderer* self)
  145. {
  146. return Py_BuildValue("I", self->messagerenderer->getLengthLimit());
  147. }
  148. static PyObject*
  149. MessageRenderer_setTruncated(s_MessageRenderer* self)
  150. {
  151. self->messagerenderer->setTruncated();
  152. Py_RETURN_NONE;
  153. }
  154. static PyObject*
  155. MessageRenderer_setLengthLimit(s_MessageRenderer* self, PyObject* args)
  156. {
  157. size_t lengthlimit;
  158. if (!PyArg_ParseTuple(args, "I", &lengthlimit)) {
  159. return NULL;
  160. }
  161. self->messagerenderer->setLengthLimit(lengthlimit);
  162. Py_RETURN_NONE;
  163. }
  164. // end of MessageRenderer
  165. // Module Initialization, all statics are initialized here
  166. bool
  167. initModulePart_MessageRenderer(PyObject* mod)
  168. {
  169. // Add the exceptions to the module
  170. // Add the enums to the module
  171. // Add the constants to the module
  172. // Add the classes to the module
  173. // We initialize the static description object with PyType_Ready(),
  174. // then add it to the module
  175. // NameComparisonResult
  176. if (PyType_Ready(&messagerenderer_type) < 0) {
  177. return false;
  178. }
  179. Py_INCREF(&messagerenderer_type);
  180. PyModule_AddObject(mod, "MessageRenderer",
  181. (PyObject*) &messagerenderer_type);
  182. return true;
  183. }