rrclass_python.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <Python.h>
  15. #include <dns/rrclass.h>
  16. #include <dns/messagerenderer.h>
  17. #include <util/buffer.h>
  18. #include <util/python/pycppwrapper_util.h>
  19. #include "rrclass_python.h"
  20. #include "messagerenderer_python.h"
  21. #include "pydnspp_common.h"
  22. using namespace isc::dns;
  23. using namespace isc::dns::python;
  24. using namespace isc::util;
  25. using namespace isc::util::python;
  26. namespace {
  27. //
  28. // We declare the functions here, the definitions are below
  29. // the type definition of the object, since both can use the other
  30. //
  31. // General creation and destruction
  32. int RRClass_init(s_RRClass* self, PyObject* args);
  33. void RRClass_destroy(s_RRClass* self);
  34. // These are the functions we export
  35. PyObject* RRClass_toText(s_RRClass* 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. PyObject* RRClass_str(PyObject* self);
  39. PyObject* RRClass_toWire(s_RRClass* self, PyObject* args);
  40. PyObject* RRClass_getCode(s_RRClass* self);
  41. PyObject* RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op);
  42. // Static function for direct class creation
  43. PyObject* RRClass_IN(s_RRClass *self);
  44. PyObject* RRClass_CH(s_RRClass *self);
  45. PyObject* RRClass_HS(s_RRClass *self);
  46. PyObject* RRClass_NONE(s_RRClass *self);
  47. PyObject* RRClass_ANY(s_RRClass *self);
  48. typedef CPPPyObjectContainer<s_RRClass, RRClass> RRClassContainer;
  49. // This list contains the actual set of functions we have in
  50. // python. Each entry has
  51. // 1. Python method name
  52. // 2. Our static function here
  53. // 3. Argument type
  54. // 4. Documentation
  55. PyMethodDef RRClass_methods[] = {
  56. { "to_text", reinterpret_cast<PyCFunction>(RRClass_toText), METH_NOARGS,
  57. "Returns the string representation" },
  58. { "to_wire", reinterpret_cast<PyCFunction>(RRClass_toWire), METH_VARARGS,
  59. "Converts the RRClass object to wire format.\n"
  60. "The argument can be either a MessageRenderer or an object that "
  61. "implements the sequence interface. If the object is mutable "
  62. "(for instance a bytearray()), the wire data is added in-place.\n"
  63. "If it is not (for instance a bytes() object), a new object is "
  64. "returned" },
  65. { "get_code", reinterpret_cast<PyCFunction>(RRClass_getCode), METH_NOARGS,
  66. "Returns the class code as an integer" },
  67. { "IN", reinterpret_cast<PyCFunction>(RRClass_IN), METH_NOARGS | METH_STATIC, "Creates an IN RRClass" },
  68. { "CH", reinterpret_cast<PyCFunction>(RRClass_CH), METH_NOARGS | METH_STATIC, "Creates a CH RRClass" },
  69. { "HS", reinterpret_cast<PyCFunction>(RRClass_HS), METH_NOARGS | METH_STATIC, "Creates an HS RRClass" },
  70. { "NONE", reinterpret_cast<PyCFunction>(RRClass_NONE), METH_NOARGS | METH_STATIC, "Creates a NONE RRClass" },
  71. { "ANY", reinterpret_cast<PyCFunction>(RRClass_ANY), METH_NOARGS | METH_STATIC, "Creates an ANY RRClass" },
  72. { NULL, NULL, 0, NULL }
  73. };
  74. int
  75. RRClass_init(s_RRClass* self, PyObject* args) {
  76. const char* s;
  77. long i;
  78. PyObject* bytes = NULL;
  79. // The constructor argument can be a string ("IN"), an integer (1),
  80. // or a sequence of numbers between 0 and 65535 (wire code)
  81. // Note that PyArg_ParseType can set PyError, and we need to clear
  82. // that if we try several like here. Otherwise the *next* python
  83. // call will suddenly appear to throw an exception.
  84. // (the way to do exceptions is to set PyErr and return -1)
  85. try {
  86. if (PyArg_ParseTuple(args, "s", &s)) {
  87. self->cppobj = new RRClass(s);
  88. return (0);
  89. } else if (PyArg_ParseTuple(args, "l", &i)) {
  90. if (i < 0 || i > 0xffff) {
  91. PyErr_Clear();
  92. PyErr_SetString(PyExc_ValueError,
  93. "RR class number out of range");
  94. return (-1);
  95. }
  96. self->cppobj = new RRClass(i);
  97. return (0);
  98. } else if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  99. uint8_t data[2];
  100. int result = readDataFromSequence(data, 2, bytes);
  101. if (result != 0) {
  102. return (result);
  103. }
  104. InputBuffer ib(data, 2);
  105. self->cppobj = new RRClass(ib);
  106. PyErr_Clear();
  107. return (0);
  108. }
  109. // Incomplete is never thrown, a type error would have already been raised
  110. //when we try to read the 2 bytes above
  111. } catch (const InvalidRRClass& ic) {
  112. PyErr_Clear();
  113. PyErr_SetString(po_InvalidRRClass, ic.what());
  114. return (-1);
  115. }
  116. PyErr_Clear();
  117. PyErr_SetString(PyExc_TypeError,
  118. "no valid type in constructor argument");
  119. return (-1);
  120. }
  121. void
  122. RRClass_destroy(s_RRClass* self) {
  123. delete self->cppobj;
  124. self->cppobj = NULL;
  125. Py_TYPE(self)->tp_free(self);
  126. }
  127. PyObject*
  128. RRClass_toText(s_RRClass* self) {
  129. // Py_BuildValue makes python objects from native data
  130. return (Py_BuildValue("s", self->cppobj->toText().c_str()));
  131. }
  132. PyObject*
  133. RRClass_str(PyObject* self) {
  134. // Simply call the to_text method we already defined
  135. return (PyObject_CallMethod(self,
  136. const_cast<char*>("to_text"),
  137. const_cast<char*>("")));
  138. }
  139. PyObject*
  140. RRClass_toWire(s_RRClass* self, PyObject* args) {
  141. PyObject* bytes;
  142. s_MessageRenderer* mr;
  143. if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
  144. PyObject* bytes_o = bytes;
  145. OutputBuffer buffer(2);
  146. self->cppobj->toWire(buffer);
  147. PyObject* n = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()), buffer.getLength());
  148. PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
  149. // We need to release the object we temporarily created here
  150. // to prevent memory leak
  151. Py_DECREF(n);
  152. return (result);
  153. } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
  154. self->cppobj->toWire(*mr->messagerenderer);
  155. // If we return NULL it is seen as an error, so use this for
  156. // None returns
  157. Py_RETURN_NONE;
  158. }
  159. PyErr_Clear();
  160. PyErr_SetString(PyExc_TypeError,
  161. "toWire argument must be a sequence object or a MessageRenderer");
  162. return (NULL);
  163. }
  164. PyObject*
  165. RRClass_getCode(s_RRClass* self) {
  166. return (Py_BuildValue("I", self->cppobj->getCode()));
  167. }
  168. PyObject*
  169. RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op) {
  170. bool c;
  171. // Check for null and if the types match. If different type,
  172. // simply return False
  173. if (!other || (self->ob_type != other->ob_type)) {
  174. Py_RETURN_FALSE;
  175. }
  176. switch (op) {
  177. case Py_LT:
  178. c = *self->cppobj < *other->cppobj;
  179. break;
  180. case Py_LE:
  181. c = *self->cppobj < *other->cppobj ||
  182. *self->cppobj == *other->cppobj;
  183. break;
  184. case Py_EQ:
  185. c = *self->cppobj == *other->cppobj;
  186. break;
  187. case Py_NE:
  188. c = *self->cppobj != *other->cppobj;
  189. break;
  190. case Py_GT:
  191. c = *other->cppobj < *self->cppobj;
  192. break;
  193. case Py_GE:
  194. c = *other->cppobj < *self->cppobj ||
  195. *self->cppobj == *other->cppobj;
  196. break;
  197. default:
  198. PyErr_SetString(PyExc_IndexError,
  199. "Unhandled rich comparison operator");
  200. return (NULL);
  201. }
  202. if (c)
  203. Py_RETURN_TRUE;
  204. else
  205. Py_RETURN_FALSE;
  206. }
  207. //
  208. // Common function for RRClass_IN/CH/etc.
  209. //
  210. PyObject* RRClass_createStatic(RRClass stc) {
  211. s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
  212. if (ret != NULL) {
  213. ret->cppobj = new RRClass(stc);
  214. }
  215. return (ret);
  216. }
  217. PyObject* RRClass_IN(s_RRClass*) {
  218. return (RRClass_createStatic(RRClass::IN()));
  219. }
  220. PyObject* RRClass_CH(s_RRClass*) {
  221. return (RRClass_createStatic(RRClass::CH()));
  222. }
  223. PyObject* RRClass_HS(s_RRClass*) {
  224. return (RRClass_createStatic(RRClass::HS()));
  225. }
  226. PyObject* RRClass_NONE(s_RRClass*) {
  227. return (RRClass_createStatic(RRClass::NONE()));
  228. }
  229. PyObject* RRClass_ANY(s_RRClass*) {
  230. return (RRClass_createStatic(RRClass::ANY()));
  231. }
  232. } // end anonymous namespace
  233. namespace isc {
  234. namespace dns {
  235. namespace python {
  236. //
  237. // Declaration of the custom exceptions
  238. // Initialization and addition of these go in the initModulePart
  239. // function at the end of this file
  240. //
  241. PyObject* po_InvalidRRClass;
  242. PyObject* po_IncompleteRRClass;
  243. // This defines the complete type for reflection in python and
  244. // parsing of PyObject* to s_RRClass
  245. // Most of the functions are not actually implemented and NULL here.
  246. PyTypeObject rrclass_type = {
  247. PyVarObject_HEAD_INIT(NULL, 0)
  248. "pydnspp.RRClass",
  249. sizeof(s_RRClass), // tp_basicsize
  250. 0, // tp_itemsize
  251. (destructor)RRClass_destroy, // tp_dealloc
  252. NULL, // tp_print
  253. NULL, // tp_getattr
  254. NULL, // tp_setattr
  255. NULL, // tp_reserved
  256. NULL, // tp_repr
  257. NULL, // tp_as_number
  258. NULL, // tp_as_sequence
  259. NULL, // tp_as_mapping
  260. NULL, // tp_hash
  261. NULL, // tp_call
  262. RRClass_str, // tp_str
  263. NULL, // tp_getattro
  264. NULL, // tp_setattro
  265. NULL, // tp_as_buffer
  266. Py_TPFLAGS_DEFAULT, // tp_flags
  267. "The RRClass class encapsulates DNS resource record classes.\n"
  268. "This class manages the 16-bit integer class codes in quite a straightforward"
  269. "way. The only non trivial task is to handle textual representations of"
  270. "RR classes, such as \"IN\", \"CH\", or \"CLASS65534\".",
  271. NULL, // tp_traverse
  272. NULL, // tp_clear
  273. (richcmpfunc)RRClass_richcmp, // tp_richcompare
  274. 0, // tp_weaklistoffset
  275. NULL, // tp_iter
  276. NULL, // tp_iternext
  277. RRClass_methods, // tp_methods
  278. NULL, // tp_members
  279. NULL, // tp_getset
  280. NULL, // tp_base
  281. NULL, // tp_dict
  282. NULL, // tp_descr_get
  283. NULL, // tp_descr_set
  284. 0, // tp_dictoffset
  285. (initproc)RRClass_init, // tp_init
  286. NULL, // tp_alloc
  287. PyType_GenericNew, // tp_new
  288. NULL, // tp_free
  289. NULL, // tp_is_gc
  290. NULL, // tp_bases
  291. NULL, // tp_mro
  292. NULL, // tp_cache
  293. NULL, // tp_subclasses
  294. NULL, // tp_weaklist
  295. NULL, // tp_del
  296. 0 // tp_version_tag
  297. };
  298. // Module Initialization, all statics are initialized here
  299. bool
  300. initModulePart_RRClass(PyObject* mod) {
  301. // Add the exceptions to the module
  302. po_InvalidRRClass = PyErr_NewException("pydnspp.InvalidRRClass", NULL, NULL);
  303. Py_INCREF(po_InvalidRRClass);
  304. PyModule_AddObject(mod, "InvalidRRClass", po_InvalidRRClass);
  305. po_IncompleteRRClass = PyErr_NewException("pydnspp.IncompleteRRClass", NULL, NULL);
  306. Py_INCREF(po_IncompleteRRClass);
  307. PyModule_AddObject(mod, "IncompleteRRClass", po_IncompleteRRClass);
  308. // We initialize the static description object with PyType_Ready(),
  309. // then add it to the module. This is not just a check! (leaving
  310. // this out results in segmentation faults)
  311. if (PyType_Ready(&rrclass_type) < 0) {
  312. return (false);
  313. }
  314. Py_INCREF(&rrclass_type);
  315. PyModule_AddObject(mod, "RRClass",
  316. reinterpret_cast<PyObject*>(&rrclass_type));
  317. return (true);
  318. }
  319. PyObject*
  320. createRRClassObject(const RRClass& source) {
  321. RRClassContainer container = PyObject_New(s_RRClass, &rrclass_type);
  322. container.set(new RRClass(source));
  323. return (container.release());
  324. }
  325. bool
  326. PyRRClass_Check(PyObject* obj) {
  327. return (PyObject_TypeCheck(obj, &rrclass_type));
  328. }
  329. RRClass&
  330. PyRRClass_ToRRClassPtr(PyObject* rrclass_obj) {
  331. s_RRClass* rrclass = static_cast<s_RRClass*>(rrclass_obj);
  332. return (*rrclass->cppobj);
  333. }
  334. } // end namespace python
  335. } // end namespace dns
  336. } // end namespace isc