question_python.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/question.h>
  15. using namespace isc::dns;
  16. //
  17. // Question
  18. //
  19. // The s_* Class simply coverst one instantiation of the object
  20. class s_Question : public PyObject {
  21. public:
  22. QuestionPtr question;
  23. };
  24. //
  25. // We declare the functions here, the definitions are below
  26. // the type definition of the object, since both can use the other
  27. //
  28. // General creation and destruction
  29. static int Question_init(s_Question* self, PyObject* args);
  30. static void Question_destroy(s_Question* self);
  31. // These are the functions we export
  32. static PyObject* Question_getName(s_Question* self);
  33. static PyObject* Question_getType(s_Question* self);
  34. static PyObject* Question_getClass(s_Question* self);
  35. static PyObject* Question_toText(s_Question* self);
  36. // This is a second version of toText, we need one where the argument
  37. // is a PyObject*, for the str() function in python.
  38. static PyObject* Question_str(PyObject* self);
  39. static PyObject* Question_toWire(s_Question* self, PyObject* args);
  40. // This list contains the actual set of functions we have in
  41. // python. Each entry has
  42. // 1. Python method name
  43. // 2. Our static function here
  44. // 3. Argument type
  45. // 4. Documentation
  46. static PyMethodDef Question_methods[] = {
  47. { "get_name", reinterpret_cast<PyCFunction>(Question_getName), METH_NOARGS,
  48. "Returns the Name" },
  49. { "get_type", reinterpret_cast<PyCFunction>(Question_getType), METH_NOARGS,
  50. "Returns the RRType" },
  51. { "get_class", reinterpret_cast<PyCFunction>(Question_getClass), METH_NOARGS,
  52. "Returns the RRClass" },
  53. { "to_text", reinterpret_cast<PyCFunction>(Question_toText), METH_NOARGS,
  54. "Returns the string representation" },
  55. { "to_wire", reinterpret_cast<PyCFunction>(Question_toWire), METH_VARARGS,
  56. "Converts the Question object to wire format.\n"
  57. "The argument can be either a MessageRenderer or an object that "
  58. "implements the sequence interface. If the object is mutable "
  59. "(for instance a bytearray()), the wire data is added in-place.\n"
  60. "If it is not (for instance a bytes() object), a new object is "
  61. "returned" },
  62. { NULL, NULL, 0, NULL }
  63. };
  64. // This defines the complete type for reflection in python and
  65. // parsing of PyObject* to s_Question
  66. // Most of the functions are not actually implemented and NULL here.
  67. static PyTypeObject question_type = {
  68. PyVarObject_HEAD_INIT(NULL, 0)
  69. "pydnspp.Question",
  70. sizeof(s_Question), // tp_basicsize
  71. 0, // tp_itemsize
  72. (destructor)Question_destroy, // tp_dealloc
  73. NULL, // tp_print
  74. NULL, // tp_getattr
  75. NULL, // tp_setattr
  76. NULL, // tp_reserved
  77. NULL, // tp_repr
  78. NULL, // tp_as_number
  79. NULL, // tp_as_sequence
  80. NULL, // tp_as_mapping
  81. NULL, // tp_hash
  82. NULL, // tp_call
  83. Question_str, // tp_str
  84. NULL, // tp_getattro
  85. NULL, // tp_setattro
  86. NULL, // tp_as_buffer
  87. Py_TPFLAGS_DEFAULT, // tp_flags
  88. "The Question class encapsulates the common search key of DNS"
  89. "lookup, consisting of owner name, RR type and RR class.",
  90. NULL, // tp_traverse
  91. NULL, // tp_clear
  92. NULL, // tp_richcompare
  93. 0, // tp_weaklistoffset
  94. NULL, // tp_iter
  95. NULL, // tp_iternext
  96. Question_methods, // tp_methods
  97. NULL, // tp_members
  98. NULL, // tp_getset
  99. NULL, // tp_base
  100. NULL, // tp_dict
  101. NULL, // tp_descr_get
  102. NULL, // tp_descr_set
  103. 0, // tp_dictoffset
  104. (initproc)Question_init, // tp_init
  105. NULL, // tp_alloc
  106. PyType_GenericNew, // tp_new
  107. NULL, // tp_free
  108. NULL, // tp_is_gc
  109. NULL, // tp_bases
  110. NULL, // tp_mro
  111. NULL, // tp_cache
  112. NULL, // tp_subclasses
  113. NULL, // tp_weaklist
  114. NULL, // tp_del
  115. 0 // tp_version_tag
  116. };
  117. static int
  118. Question_init(s_Question* self, PyObject* args) {
  119. // Try out the various combinations of arguments to call the
  120. // correct cpp constructor.
  121. // Note that PyArg_ParseType can set PyError, and we need to clear
  122. // that if we try several like here. Otherwise the *next* python
  123. // call will suddenly appear to throw an exception.
  124. // (the way to do exceptions is to set PyErr and return -1)
  125. s_Name* name;
  126. s_RRClass* rrclass;
  127. s_RRType* rrtype;
  128. const char* b;
  129. Py_ssize_t len;
  130. unsigned int position = 0;
  131. try {
  132. if (PyArg_ParseTuple(args, "O!O!O!", &name_type, &name,
  133. &rrclass_type, &rrclass,
  134. &rrtype_type, &rrtype
  135. )) {
  136. self->question = QuestionPtr(new Question(*name->cppobj, *rrclass->rrclass,
  137. *rrtype->rrtype));
  138. return (0);
  139. } else if (PyArg_ParseTuple(args, "y#|I", &b, &len, &position)) {
  140. PyErr_Clear();
  141. InputBuffer inbuf(b, len);
  142. inbuf.setPosition(position);
  143. self->question = QuestionPtr(new Question(inbuf));
  144. return (0);
  145. }
  146. } catch (const DNSMessageFORMERR& dmfe) {
  147. PyErr_Clear();
  148. PyErr_SetString(po_DNSMessageFORMERR, dmfe.what());
  149. return (-1);
  150. } catch (const IncompleteRRClass& irc) {
  151. PyErr_Clear();
  152. PyErr_SetString(po_IncompleteRRClass, irc.what());
  153. return (-1);
  154. } catch (const IncompleteRRType& irt) {
  155. PyErr_Clear();
  156. PyErr_SetString(po_IncompleteRRType, irt.what());
  157. return (-1);
  158. }
  159. self->question = QuestionPtr();
  160. PyErr_Clear();
  161. PyErr_SetString(PyExc_TypeError,
  162. "no valid type in constructor argument");
  163. return (-1);
  164. }
  165. static void
  166. Question_destroy(s_Question* self) {
  167. self->question.reset();
  168. Py_TYPE(self)->tp_free(self);
  169. }
  170. static PyObject*
  171. Question_getName(s_Question* self) {
  172. s_Name* name;
  173. // is this the best way to do this?
  174. name = static_cast<s_Name*>(name_type.tp_alloc(&name_type, 0));
  175. if (name != NULL) {
  176. name->cppobj = new Name(self->question->getName());
  177. }
  178. return (name);
  179. }
  180. static PyObject*
  181. Question_getType(s_Question* self) {
  182. s_RRType* rrtype;
  183. rrtype = static_cast<s_RRType*>(rrtype_type.tp_alloc(&rrtype_type, 0));
  184. if (rrtype != NULL) {
  185. rrtype->rrtype = new RRType(self->question->getType());
  186. }
  187. return (rrtype);
  188. }
  189. static PyObject*
  190. Question_getClass(s_Question* self) {
  191. s_RRClass* rrclass;
  192. rrclass = static_cast<s_RRClass*>(rrclass_type.tp_alloc(&rrclass_type, 0));
  193. if (rrclass != NULL) {
  194. rrclass->rrclass = new RRClass(self->question->getClass());
  195. }
  196. return (rrclass);
  197. }
  198. static PyObject*
  199. Question_toText(s_Question* self) {
  200. // Py_BuildValue makes python objects from native data
  201. return (Py_BuildValue("s", self->question->toText().c_str()));
  202. }
  203. static PyObject*
  204. Question_str(PyObject* self) {
  205. // Simply call the to_text method we already defined
  206. return (PyObject_CallMethod(self,
  207. const_cast<char*>("to_text"),
  208. const_cast<char*>("")));
  209. }
  210. static PyObject*
  211. Question_toWire(s_Question* self, PyObject* args) {
  212. PyObject* bytes;
  213. s_MessageRenderer* mr;
  214. if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  215. PyObject* bytes_o = bytes;
  216. // Max length is Name::MAX_WIRE + rrclass (2) + rrtype (2)
  217. OutputBuffer buffer(Name::MAX_WIRE + 4);
  218. self->question->toWire(buffer);
  219. PyObject* n = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()),
  220. buffer.getLength());
  221. PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
  222. // We need to release the object we temporarily created here
  223. // to prevent memory leak
  224. Py_DECREF(n);
  225. return (result);
  226. } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
  227. self->question->toWire(*mr->messagerenderer);
  228. // If we return NULL it is seen as an error, so use this for
  229. // None returns
  230. Py_RETURN_NONE;
  231. }
  232. PyErr_Clear();
  233. PyErr_SetString(PyExc_TypeError,
  234. "toWire argument must be a sequence object or a MessageRenderer");
  235. return (NULL);
  236. }
  237. // end of Question
  238. // Module Initialization, all statics are initialized here
  239. bool
  240. initModulePart_Question(PyObject* mod) {
  241. // Add the exceptions to the module
  242. // We initialize the static description object with PyType_Ready(),
  243. // then add it to the module. This is not just a check! (leaving
  244. // this out results in segmentation faults)
  245. if (PyType_Ready(&question_type) < 0) {
  246. return (false);
  247. }
  248. Py_INCREF(&question_type);
  249. PyModule_AddObject(mod, "Question",
  250. reinterpret_cast<PyObject*>(&question_type));
  251. return (true);
  252. }