messagerenderer_python.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/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. self->outputbuffer = new OutputBuffer(4096);
  112. self->messagerenderer = new MessageRenderer(*self->outputbuffer);
  113. return 0;
  114. }
  115. static void
  116. MessageRenderer_destroy(s_MessageRenderer* self) {
  117. delete self->messagerenderer;
  118. delete self->outputbuffer;
  119. self->messagerenderer = NULL;
  120. Py_TYPE(self)->tp_free(self);
  121. }
  122. static PyObject*
  123. MessageRenderer_getData(s_MessageRenderer* self) {
  124. return Py_BuildValue("y#",
  125. self->messagerenderer->getData(),
  126. self->messagerenderer->getLength());
  127. }
  128. static PyObject*
  129. MessageRenderer_getLength(s_MessageRenderer* self) {
  130. return Py_BuildValue("I", self->messagerenderer->getLength());
  131. }
  132. static PyObject*
  133. MessageRenderer_isTruncated(s_MessageRenderer* self) {
  134. if (self->messagerenderer->isTruncated()) {
  135. Py_RETURN_TRUE;
  136. } else {
  137. Py_RETURN_FALSE;
  138. }
  139. }
  140. static PyObject*
  141. MessageRenderer_getLengthLimit(s_MessageRenderer* self) {
  142. return Py_BuildValue("I", self->messagerenderer->getLengthLimit());
  143. }
  144. static PyObject*
  145. MessageRenderer_setTruncated(s_MessageRenderer* self) {
  146. self->messagerenderer->setTruncated();
  147. Py_RETURN_NONE;
  148. }
  149. static PyObject*
  150. MessageRenderer_setLengthLimit(s_MessageRenderer* self,
  151. PyObject* args)
  152. {
  153. size_t lengthlimit;
  154. if (!PyArg_ParseTuple(args, "I", &lengthlimit)) {
  155. return NULL;
  156. }
  157. self->messagerenderer->setLengthLimit(lengthlimit);
  158. Py_RETURN_NONE;
  159. }
  160. // end of MessageRenderer
  161. // Module Initialization, all statics are initialized here
  162. bool
  163. initModulePart_MessageRenderer(PyObject* mod) {
  164. // Add the exceptions to the module
  165. // Add the enums to the module
  166. // Add the constants to the module
  167. // Add the classes to the module
  168. // We initialize the static description object with PyType_Ready(),
  169. // then add it to the module
  170. // NameComparisonResult
  171. if (PyType_Ready(&messagerenderer_type) < 0) {
  172. return false;
  173. }
  174. Py_INCREF(&messagerenderer_type);
  175. PyModule_AddObject(mod, "MessageRenderer",
  176. (PyObject*) &messagerenderer_type);
  177. return true;
  178. }