rrclass_python.cc 13 KB

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