rrclass_python.cc 13 KB

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