rrclass_python.cc 13 KB

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