opcode_python.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/opcode.h>
  16. using namespace isc::dns;
  17. //
  18. // Declaration of the custom exceptions (None for this class)
  19. //
  20. // Definition of the classes
  21. //
  22. // For each class, we need a struct, a helper functions (init, destroy,
  23. // and static wrappers around the methods we export), a list of methods,
  24. // and a type description
  25. namespace {
  26. //
  27. // Opcode
  28. //
  29. class s_Opcode : public PyObject {
  30. public:
  31. s_Opcode() : opcode(NULL), static_code(false) {}
  32. const Opcode* opcode;
  33. bool static_code;
  34. };
  35. int Opcode_init(s_Opcode* const self, PyObject* args);
  36. void Opcode_destroy(s_Opcode* const self);
  37. PyObject* Opcode_getCode(const s_Opcode* const self);
  38. PyObject* Opcode_toText(const s_Opcode* const self);
  39. PyObject* Opcode_str(PyObject* const self);
  40. PyObject* Opcode_QUERY(const s_Opcode* self);
  41. PyObject* Opcode_IQUERY(const s_Opcode* self);
  42. PyObject* Opcode_STATUS(const s_Opcode* self);
  43. PyObject* Opcode_RESERVED3(const s_Opcode* self);
  44. PyObject* Opcode_NOTIFY(const s_Opcode* self);
  45. PyObject* Opcode_UPDATE(const s_Opcode* self);
  46. PyObject* Opcode_RESERVED6(const s_Opcode* self);
  47. PyObject* Opcode_RESERVED7(const s_Opcode* self);
  48. PyObject* Opcode_RESERVED8(const s_Opcode* self);
  49. PyObject* Opcode_RESERVED9(const s_Opcode* self);
  50. PyObject* Opcode_RESERVED10(const s_Opcode* self);
  51. PyObject* Opcode_RESERVED11(const s_Opcode* self);
  52. PyObject* Opcode_RESERVED12(const s_Opcode* self);
  53. PyObject* Opcode_RESERVED13(const s_Opcode* self);
  54. PyObject* Opcode_RESERVED14(const s_Opcode* self);
  55. PyObject* Opcode_RESERVED15(const s_Opcode* self);
  56. PyObject* Opcode_richcmp(const s_Opcode* const self,
  57. const s_Opcode* const other, int op);
  58. PyMethodDef Opcode_methods[] = {
  59. { "get_code", reinterpret_cast<PyCFunction>(Opcode_getCode), METH_NOARGS,
  60. "Returns the code value" },
  61. { "to_text", reinterpret_cast<PyCFunction>(Opcode_toText), METH_NOARGS,
  62. "Returns the text representation" },
  63. { "QUERY", reinterpret_cast<PyCFunction>(Opcode_QUERY),
  64. METH_NOARGS | METH_STATIC, "Creates a QUERY Opcode" },
  65. { "IQUERY", reinterpret_cast<PyCFunction>(Opcode_IQUERY),
  66. METH_NOARGS | METH_STATIC, "Creates a IQUERY Opcode" },
  67. { "STATUS", reinterpret_cast<PyCFunction>(Opcode_STATUS),
  68. METH_NOARGS | METH_STATIC, "Creates a STATUS Opcode" },
  69. { "RESERVED3", reinterpret_cast<PyCFunction>(Opcode_RESERVED3),
  70. METH_NOARGS | METH_STATIC, "Creates a RESERVED3 Opcode" },
  71. { "NOTIFY", reinterpret_cast<PyCFunction>(Opcode_NOTIFY),
  72. METH_NOARGS | METH_STATIC, "Creates a NOTIFY Opcode" },
  73. { "UPDATE", reinterpret_cast<PyCFunction>(Opcode_UPDATE),
  74. METH_NOARGS | METH_STATIC, "Creates a UPDATE Opcode" },
  75. { "RESERVED6", reinterpret_cast<PyCFunction>(Opcode_RESERVED6),
  76. METH_NOARGS | METH_STATIC, "Creates a RESERVED6 Opcode" },
  77. { "RESERVED7", reinterpret_cast<PyCFunction>(Opcode_RESERVED7),
  78. METH_NOARGS | METH_STATIC, "Creates a RESERVED7 Opcode" },
  79. { "RESERVED8", reinterpret_cast<PyCFunction>(Opcode_RESERVED8),
  80. METH_NOARGS | METH_STATIC, "Creates a RESERVED8 Opcode" },
  81. { "RESERVED9", reinterpret_cast<PyCFunction>(Opcode_RESERVED9),
  82. METH_NOARGS | METH_STATIC, "Creates a RESERVED9 Opcode" },
  83. { "RESERVED10", reinterpret_cast<PyCFunction>(Opcode_RESERVED10),
  84. METH_NOARGS | METH_STATIC, "Creates a RESERVED10 Opcode" },
  85. { "RESERVED11", reinterpret_cast<PyCFunction>(Opcode_RESERVED11),
  86. METH_NOARGS | METH_STATIC, "Creates a RESERVED11 Opcode" },
  87. { "RESERVED12", reinterpret_cast<PyCFunction>(Opcode_RESERVED12),
  88. METH_NOARGS | METH_STATIC, "Creates a RESERVED12 Opcode" },
  89. { "RESERVED13", reinterpret_cast<PyCFunction>(Opcode_RESERVED13),
  90. METH_NOARGS | METH_STATIC, "Creates a RESERVED13 Opcode" },
  91. { "RESERVED14", reinterpret_cast<PyCFunction>(Opcode_RESERVED14),
  92. METH_NOARGS | METH_STATIC, "Creates a RESERVED14 Opcode" },
  93. { "RESERVED15", reinterpret_cast<PyCFunction>(Opcode_RESERVED15),
  94. METH_NOARGS | METH_STATIC, "Creates a RESERVED15 Opcode" },
  95. { NULL, NULL, 0, NULL }
  96. };
  97. PyTypeObject opcode_type = {
  98. PyVarObject_HEAD_INIT(NULL, 0)
  99. "pydnspp.Opcode",
  100. sizeof(s_Opcode), // tp_basicsize
  101. 0, // tp_itemsize
  102. (destructor)Opcode_destroy, // tp_dealloc
  103. NULL, // tp_print
  104. NULL, // tp_getattr
  105. NULL, // tp_setattr
  106. NULL, // tp_reserved
  107. NULL, // tp_repr
  108. NULL, // tp_as_number
  109. NULL, // tp_as_sequence
  110. NULL, // tp_as_mapping
  111. NULL, // tp_hash
  112. NULL, // tp_call
  113. Opcode_str, // tp_str
  114. NULL, // tp_getattro
  115. NULL, // tp_setattro
  116. NULL, // tp_as_buffer
  117. Py_TPFLAGS_DEFAULT, // tp_flags
  118. "The Opcode class objects represent standard OPCODEs "
  119. "of the header section of DNS messages.",
  120. NULL, // tp_traverse
  121. NULL, // tp_clear
  122. (richcmpfunc)Opcode_richcmp, // tp_richcompare
  123. 0, // tp_weaklistoffset
  124. NULL, // tp_iter
  125. NULL, // tp_iternext
  126. Opcode_methods, // tp_methods
  127. NULL, // tp_members
  128. NULL, // tp_getset
  129. NULL, // tp_base
  130. NULL, // tp_dict
  131. NULL, // tp_descr_get
  132. NULL, // tp_descr_set
  133. 0, // tp_dictoffset
  134. (initproc)Opcode_init, // tp_init
  135. NULL, // tp_alloc
  136. PyType_GenericNew, // tp_new
  137. NULL, // tp_free
  138. NULL, // tp_is_gc
  139. NULL, // tp_bases
  140. NULL, // tp_mro
  141. NULL, // tp_cache
  142. NULL, // tp_subclasses
  143. NULL, // tp_weaklist
  144. NULL, // tp_del
  145. 0 // tp_version_tag
  146. };
  147. int
  148. Opcode_init(s_Opcode* const self, PyObject* args) {
  149. uint8_t code = 0;
  150. if (PyArg_ParseTuple(args, "b", &code)) {
  151. try {
  152. self->opcode = new Opcode(code);
  153. self->static_code = false;
  154. } catch (const isc::OutOfRange& ex) {
  155. PyErr_SetString(PyExc_OverflowError, ex.what());
  156. return (-1);
  157. } catch (...) {
  158. PyErr_SetString(po_IscException, "Unexpected exception");
  159. return (-1);
  160. }
  161. return (0);
  162. }
  163. PyErr_Clear();
  164. PyErr_SetString(PyExc_TypeError, "Invalid arguments to Opcode constructor");
  165. return (-1);
  166. }
  167. void
  168. Opcode_destroy(s_Opcode* const self) {
  169. // Depending on whether we created the rcode or are referring
  170. // to a global static one, we do or do not delete self->opcode here
  171. if (!self->static_code) {
  172. delete self->opcode;
  173. }
  174. self->opcode = NULL;
  175. Py_TYPE(self)->tp_free(self);
  176. }
  177. PyObject*
  178. Opcode_getCode(const s_Opcode* const self) {
  179. return (Py_BuildValue("I", self->opcode->getCode()));
  180. }
  181. PyObject*
  182. Opcode_toText(const s_Opcode* const self) {
  183. return (Py_BuildValue("s", self->opcode->toText().c_str()));
  184. }
  185. PyObject*
  186. Opcode_str(PyObject* const self) {
  187. // Simply call the to_text method we already defined
  188. return (PyObject_CallMethod(self,
  189. const_cast<char*>("to_text"),
  190. const_cast<char*>("")));
  191. }
  192. PyObject*
  193. Opcode_createStatic(const Opcode& opcode) {
  194. s_Opcode* ret = PyObject_New(s_Opcode, &opcode_type);
  195. if (ret != NULL) {
  196. ret->opcode = &opcode;
  197. ret->static_code = true;
  198. }
  199. return (ret);
  200. }
  201. PyObject*
  202. Opcode_QUERY(const s_Opcode* self UNUSED_PARAM) {
  203. return (Opcode_createStatic(Opcode::QUERY()));
  204. }
  205. PyObject*
  206. Opcode_IQUERY(const s_Opcode* self UNUSED_PARAM) {
  207. return (Opcode_createStatic(Opcode::IQUERY()));
  208. }
  209. PyObject*
  210. Opcode_STATUS(const s_Opcode* self UNUSED_PARAM) {
  211. return (Opcode_createStatic(Opcode::STATUS()));
  212. }
  213. PyObject*
  214. Opcode_RESERVED3(const s_Opcode* self UNUSED_PARAM) {
  215. return (Opcode_createStatic(Opcode::RESERVED3()));
  216. }
  217. PyObject*
  218. Opcode_NOTIFY(const s_Opcode* self UNUSED_PARAM) {
  219. return (Opcode_createStatic(Opcode::NOTIFY()));
  220. }
  221. PyObject*
  222. Opcode_UPDATE(const s_Opcode* self UNUSED_PARAM) {
  223. return (Opcode_createStatic(Opcode::UPDATE()));
  224. }
  225. PyObject*
  226. Opcode_RESERVED6(const s_Opcode* self UNUSED_PARAM) {
  227. return (Opcode_createStatic(Opcode::RESERVED6()));
  228. }
  229. PyObject*
  230. Opcode_RESERVED7(const s_Opcode* self UNUSED_PARAM) {
  231. return (Opcode_createStatic(Opcode::RESERVED7()));
  232. }
  233. PyObject*
  234. Opcode_RESERVED8(const s_Opcode* self UNUSED_PARAM) {
  235. return (Opcode_createStatic(Opcode::RESERVED8()));
  236. }
  237. PyObject*
  238. Opcode_RESERVED9(const s_Opcode* self UNUSED_PARAM) {
  239. return (Opcode_createStatic(Opcode::RESERVED9()));
  240. }
  241. PyObject*
  242. Opcode_RESERVED10(const s_Opcode* self UNUSED_PARAM) {
  243. return (Opcode_createStatic(Opcode::RESERVED10()));
  244. }
  245. PyObject*
  246. Opcode_RESERVED11(const s_Opcode* self UNUSED_PARAM) {
  247. return (Opcode_createStatic(Opcode::RESERVED11()));
  248. }
  249. PyObject*
  250. Opcode_RESERVED12(const s_Opcode* self UNUSED_PARAM) {
  251. return (Opcode_createStatic(Opcode::RESERVED12()));
  252. }
  253. PyObject*
  254. Opcode_RESERVED13(const s_Opcode* self UNUSED_PARAM) {
  255. return (Opcode_createStatic(Opcode::RESERVED13()));
  256. }
  257. PyObject*
  258. Opcode_RESERVED14(const s_Opcode* self UNUSED_PARAM) {
  259. return (Opcode_createStatic(Opcode::RESERVED14()));
  260. }
  261. PyObject*
  262. Opcode_RESERVED15(const s_Opcode* self UNUSED_PARAM) {
  263. return (Opcode_createStatic(Opcode::RESERVED15()));
  264. }
  265. PyObject*
  266. Opcode_richcmp(const s_Opcode* const self, const s_Opcode* const other,
  267. const int op)
  268. {
  269. bool c = false;
  270. // Check for null and if the types match. If different type,
  271. // simply return False
  272. if (!other || (self->ob_type != other->ob_type)) {
  273. Py_RETURN_FALSE;
  274. }
  275. // Only equals and not equals here, unorderable type
  276. switch (op) {
  277. case Py_LT:
  278. PyErr_SetString(PyExc_TypeError, "Unorderable type; Opcode");
  279. return (NULL);
  280. case Py_LE:
  281. PyErr_SetString(PyExc_TypeError, "Unorderable type; Opcode");
  282. return (NULL);
  283. case Py_EQ:
  284. c = (*self->opcode == *other->opcode);
  285. break;
  286. case Py_NE:
  287. c = (*self->opcode != *other->opcode);
  288. break;
  289. case Py_GT:
  290. PyErr_SetString(PyExc_TypeError, "Unorderable type; Opcode");
  291. return (NULL);
  292. case Py_GE:
  293. PyErr_SetString(PyExc_TypeError, "Unorderable type; Opcode");
  294. return (NULL);
  295. }
  296. if (c)
  297. Py_RETURN_TRUE;
  298. else
  299. Py_RETURN_FALSE;
  300. }
  301. // Module Initialization, all statics are initialized here
  302. bool
  303. initModulePart_Opcode(PyObject* mod) {
  304. // We initialize the static description object with PyType_Ready(),
  305. // then add it to the module. This is not just a check! (leaving
  306. // this out results in segmentation faults)
  307. if (PyType_Ready(&opcode_type) < 0) {
  308. return (false);
  309. }
  310. Py_INCREF(&opcode_type);
  311. void* p = &opcode_type;
  312. if (PyModule_AddObject(mod, "Opcode", static_cast<PyObject*>(p)) != 0) {
  313. Py_DECREF(&opcode_type);
  314. return (false);
  315. }
  316. addClassVariable(opcode_type, "QUERY_CODE",
  317. Py_BuildValue("h", Opcode::QUERY_CODE));
  318. addClassVariable(opcode_type, "IQUERY_CODE",
  319. Py_BuildValue("h", Opcode::IQUERY_CODE));
  320. addClassVariable(opcode_type, "STATUS_CODE",
  321. Py_BuildValue("h", Opcode::STATUS_CODE));
  322. addClassVariable(opcode_type, "RESERVED3_CODE",
  323. Py_BuildValue("h", Opcode::RESERVED3_CODE));
  324. addClassVariable(opcode_type, "NOTIFY_CODE",
  325. Py_BuildValue("h", Opcode::NOTIFY_CODE));
  326. addClassVariable(opcode_type, "UPDATE_CODE",
  327. Py_BuildValue("h", Opcode::UPDATE_CODE));
  328. addClassVariable(opcode_type, "RESERVED6_CODE",
  329. Py_BuildValue("h", Opcode::RESERVED6_CODE));
  330. addClassVariable(opcode_type, "RESERVED7_CODE",
  331. Py_BuildValue("h", Opcode::RESERVED7_CODE));
  332. addClassVariable(opcode_type, "RESERVED8_CODE",
  333. Py_BuildValue("h", Opcode::RESERVED8_CODE));
  334. addClassVariable(opcode_type, "RESERVED9_CODE",
  335. Py_BuildValue("h", Opcode::RESERVED9_CODE));
  336. addClassVariable(opcode_type, "RESERVED10_CODE",
  337. Py_BuildValue("h", Opcode::RESERVED10_CODE));
  338. addClassVariable(opcode_type, "RESERVED11_CODE",
  339. Py_BuildValue("h", Opcode::RESERVED11_CODE));
  340. addClassVariable(opcode_type, "RESERVED12_CODE",
  341. Py_BuildValue("h", Opcode::RESERVED12_CODE));
  342. addClassVariable(opcode_type, "RESERVED13_CODE",
  343. Py_BuildValue("h", Opcode::RESERVED13_CODE));
  344. addClassVariable(opcode_type, "RESERVED14_CODE",
  345. Py_BuildValue("h", Opcode::RESERVED14_CODE));
  346. addClassVariable(opcode_type, "RESERVED15_CODE",
  347. Py_BuildValue("h", Opcode::RESERVED15_CODE));
  348. return (true);
  349. }
  350. } // end of unnamed namespace