pydnspp.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. // We want a lot of different parts of the DNS API in the python
  15. // module, but not one big 10000-line file.
  16. // So we split it up in several 'mini-modules'
  17. // These would be the same as a single module, except for
  18. // the init function, which has to be modified to a unique
  19. // name initModulePart_<name>, and return true/false instead of
  20. // NULL/*mod
  21. //
  22. // The big init function is split up into a separate initModulePart function
  23. // for each class we add.
  24. #define PY_SSIZE_T_CLEAN
  25. #include <Python.h>
  26. #include <structmember.h>
  27. #include <util/python/pycppwrapper_util.h>
  28. #include "pydnspp_common.h"
  29. #include "edns_python.h"
  30. #include "message_python.h"
  31. #include "messagerenderer_python.h"
  32. #include "name_python.h"
  33. #include "opcode_python.h"
  34. #include "pydnspp_common.h"
  35. #include "pydnspp_towire.h"
  36. #include "question_python.h"
  37. #include "rcode_python.h"
  38. #include "rdata_python.h"
  39. #include "rrclass_python.h"
  40. #include "rrset_python.h"
  41. #include "rrttl_python.h"
  42. #include "rrtype_python.h"
  43. #include "tsigerror_python.h"
  44. #include "tsigkey_python.h"
  45. #include "tsig_python.h"
  46. #include "tsig_rdata_python.h"
  47. #include "tsigrecord_python.h"
  48. using namespace isc::dns;
  49. using namespace isc::dns::python;
  50. using namespace isc::util::python;
  51. namespace {
  52. bool
  53. initModulePart_EDNS(PyObject* mod) {
  54. // We initialize the static description object with PyType_Ready(),
  55. // then add it to the module. This is not just a check! (leaving
  56. // this out results in segmentation faults)
  57. //
  58. // After the type has been initialized, we initialize any exceptions
  59. // that are defined in the wrapper for this class, and add constants
  60. // to the type, if any
  61. if (PyType_Ready(&edns_type) < 0) {
  62. return (false);
  63. }
  64. Py_INCREF(&edns_type);
  65. void* p = &edns_type;
  66. PyModule_AddObject(mod, "EDNS", static_cast<PyObject*>(p));
  67. addClassVariable(edns_type, "SUPPORTED_VERSION",
  68. Py_BuildValue("B", EDNS::SUPPORTED_VERSION));
  69. return (true);
  70. }
  71. bool
  72. initModulePart_Message(PyObject* mod) {
  73. if (PyType_Ready(&message_type) < 0) {
  74. return (false);
  75. }
  76. Py_INCREF(&message_type);
  77. // Class variables
  78. // These are added to the tp_dict of the type object
  79. //
  80. addClassVariable(message_type, "PARSE",
  81. Py_BuildValue("I", Message::PARSE));
  82. addClassVariable(message_type, "RENDER",
  83. Py_BuildValue("I", Message::RENDER));
  84. addClassVariable(message_type, "HEADERFLAG_QR",
  85. Py_BuildValue("I", Message::HEADERFLAG_QR));
  86. addClassVariable(message_type, "HEADERFLAG_AA",
  87. Py_BuildValue("I", Message::HEADERFLAG_AA));
  88. addClassVariable(message_type, "HEADERFLAG_TC",
  89. Py_BuildValue("I", Message::HEADERFLAG_TC));
  90. addClassVariable(message_type, "HEADERFLAG_RD",
  91. Py_BuildValue("I", Message::HEADERFLAG_RD));
  92. addClassVariable(message_type, "HEADERFLAG_RA",
  93. Py_BuildValue("I", Message::HEADERFLAG_RA));
  94. addClassVariable(message_type, "HEADERFLAG_AD",
  95. Py_BuildValue("I", Message::HEADERFLAG_AD));
  96. addClassVariable(message_type, "HEADERFLAG_CD",
  97. Py_BuildValue("I", Message::HEADERFLAG_CD));
  98. addClassVariable(message_type, "SECTION_QUESTION",
  99. Py_BuildValue("I", Message::SECTION_QUESTION));
  100. addClassVariable(message_type, "SECTION_ANSWER",
  101. Py_BuildValue("I", Message::SECTION_ANSWER));
  102. addClassVariable(message_type, "SECTION_AUTHORITY",
  103. Py_BuildValue("I", Message::SECTION_AUTHORITY));
  104. addClassVariable(message_type, "SECTION_ADDITIONAL",
  105. Py_BuildValue("I", Message::SECTION_ADDITIONAL));
  106. addClassVariable(message_type, "DEFAULT_MAX_UDPSIZE",
  107. Py_BuildValue("I", Message::DEFAULT_MAX_UDPSIZE));
  108. /* Class-specific exceptions */
  109. po_MessageTooShort = PyErr_NewException("pydnspp.MessageTooShort", NULL,
  110. NULL);
  111. PyModule_AddObject(mod, "MessageTooShort", po_MessageTooShort);
  112. po_InvalidMessageSection =
  113. PyErr_NewException("pydnspp.InvalidMessageSection", NULL, NULL);
  114. PyModule_AddObject(mod, "InvalidMessageSection", po_InvalidMessageSection);
  115. po_InvalidMessageOperation =
  116. PyErr_NewException("pydnspp.InvalidMessageOperation", NULL, NULL);
  117. PyModule_AddObject(mod, "InvalidMessageOperation",
  118. po_InvalidMessageOperation);
  119. po_InvalidMessageUDPSize =
  120. PyErr_NewException("pydnspp.InvalidMessageUDPSize", NULL, NULL);
  121. PyModule_AddObject(mod, "InvalidMessageUDPSize", po_InvalidMessageUDPSize);
  122. po_DNSMessageBADVERS = PyErr_NewException("pydnspp.DNSMessageBADVERS",
  123. NULL, NULL);
  124. PyModule_AddObject(mod, "DNSMessageBADVERS", po_DNSMessageBADVERS);
  125. PyModule_AddObject(mod, "Message",
  126. reinterpret_cast<PyObject*>(&message_type));
  127. return (true);
  128. }
  129. bool
  130. initModulePart_MessageRenderer(PyObject* mod) {
  131. if (PyType_Ready(&messagerenderer_type) < 0) {
  132. return (false);
  133. }
  134. Py_INCREF(&messagerenderer_type);
  135. addClassVariable(messagerenderer_type, "CASE_INSENSITIVE",
  136. Py_BuildValue("I", MessageRenderer::CASE_INSENSITIVE));
  137. addClassVariable(messagerenderer_type, "CASE_SENSITIVE",
  138. Py_BuildValue("I", MessageRenderer::CASE_SENSITIVE));
  139. PyModule_AddObject(mod, "MessageRenderer",
  140. reinterpret_cast<PyObject*>(&messagerenderer_type));
  141. return (true);
  142. }
  143. bool
  144. initModulePart_Name(PyObject* mod) {
  145. //
  146. // NameComparisonResult
  147. //
  148. if (PyType_Ready(&name_comparison_result_type) < 0) {
  149. return (false);
  150. }
  151. Py_INCREF(&name_comparison_result_type);
  152. // Add the enums to the module
  153. po_NameRelation = Py_BuildValue("{i:s,i:s,i:s,i:s}",
  154. NameComparisonResult::SUPERDOMAIN, "SUPERDOMAIN",
  155. NameComparisonResult::SUBDOMAIN, "SUBDOMAIN",
  156. NameComparisonResult::EQUAL, "EQUAL",
  157. NameComparisonResult::COMMONANCESTOR, "COMMONANCESTOR");
  158. addClassVariable(name_comparison_result_type, "NameRelation",
  159. po_NameRelation);
  160. PyModule_AddObject(mod, "NameComparisonResult",
  161. reinterpret_cast<PyObject*>(&name_comparison_result_type));
  162. //
  163. // Name
  164. //
  165. if (PyType_Ready(&name_type) < 0) {
  166. return (false);
  167. }
  168. Py_INCREF(&name_type);
  169. // Add the constants to the module
  170. addClassVariable(name_type, "MAX_WIRE",
  171. Py_BuildValue("I", Name::MAX_WIRE));
  172. addClassVariable(name_type, "MAX_LABELS",
  173. Py_BuildValue("I", Name::MAX_LABELS));
  174. addClassVariable(name_type, "MAX_LABELLEN",
  175. Py_BuildValue("I", Name::MAX_LABELLEN));
  176. addClassVariable(name_type, "MAX_COMPRESS_POINTER",
  177. Py_BuildValue("I", Name::MAX_COMPRESS_POINTER));
  178. addClassVariable(name_type, "COMPRESS_POINTER_MARK8",
  179. Py_BuildValue("I", Name::COMPRESS_POINTER_MARK8));
  180. addClassVariable(name_type, "COMPRESS_POINTER_MARK16",
  181. Py_BuildValue("I", Name::COMPRESS_POINTER_MARK16));
  182. addClassVariable(name_type, "ROOT_NAME",
  183. createNameObject(Name::ROOT_NAME()));
  184. PyModule_AddObject(mod, "Name",
  185. reinterpret_cast<PyObject*>(&name_type));
  186. // Add the exceptions to the module
  187. po_EmptyLabel = PyErr_NewException("pydnspp.EmptyLabel", NULL, NULL);
  188. PyModule_AddObject(mod, "EmptyLabel", po_EmptyLabel);
  189. po_TooLongName = PyErr_NewException("pydnspp.TooLongName", NULL, NULL);
  190. PyModule_AddObject(mod, "TooLongName", po_TooLongName);
  191. po_TooLongLabel = PyErr_NewException("pydnspp.TooLongLabel", NULL, NULL);
  192. PyModule_AddObject(mod, "TooLongLabel", po_TooLongLabel);
  193. po_BadLabelType = PyErr_NewException("pydnspp.BadLabelType", NULL, NULL);
  194. PyModule_AddObject(mod, "BadLabelType", po_BadLabelType);
  195. po_BadEscape = PyErr_NewException("pydnspp.BadEscape", NULL, NULL);
  196. PyModule_AddObject(mod, "BadEscape", po_BadEscape);
  197. po_IncompleteName = PyErr_NewException("pydnspp.IncompleteName", NULL, NULL);
  198. PyModule_AddObject(mod, "IncompleteName", po_IncompleteName);
  199. po_InvalidBufferPosition =
  200. PyErr_NewException("pydnspp.InvalidBufferPosition", NULL, NULL);
  201. PyModule_AddObject(mod, "InvalidBufferPosition", po_InvalidBufferPosition);
  202. // This one could have gone into the message_python.cc file, but is
  203. // already needed here.
  204. po_DNSMessageFORMERR = PyErr_NewException("pydnspp.DNSMessageFORMERR",
  205. NULL, NULL);
  206. PyModule_AddObject(mod, "DNSMessageFORMERR", po_DNSMessageFORMERR);
  207. return (true);
  208. }
  209. bool
  210. initModulePart_Opcode(PyObject* mod) {
  211. if (PyType_Ready(&opcode_type) < 0) {
  212. return (false);
  213. }
  214. Py_INCREF(&opcode_type);
  215. void* p = &opcode_type;
  216. if (PyModule_AddObject(mod, "Opcode", static_cast<PyObject*>(p)) != 0) {
  217. Py_DECREF(&opcode_type);
  218. return (false);
  219. }
  220. addClassVariable(opcode_type, "QUERY_CODE",
  221. Py_BuildValue("h", Opcode::QUERY_CODE));
  222. addClassVariable(opcode_type, "IQUERY_CODE",
  223. Py_BuildValue("h", Opcode::IQUERY_CODE));
  224. addClassVariable(opcode_type, "STATUS_CODE",
  225. Py_BuildValue("h", Opcode::STATUS_CODE));
  226. addClassVariable(opcode_type, "RESERVED3_CODE",
  227. Py_BuildValue("h", Opcode::RESERVED3_CODE));
  228. addClassVariable(opcode_type, "NOTIFY_CODE",
  229. Py_BuildValue("h", Opcode::NOTIFY_CODE));
  230. addClassVariable(opcode_type, "UPDATE_CODE",
  231. Py_BuildValue("h", Opcode::UPDATE_CODE));
  232. addClassVariable(opcode_type, "RESERVED6_CODE",
  233. Py_BuildValue("h", Opcode::RESERVED6_CODE));
  234. addClassVariable(opcode_type, "RESERVED7_CODE",
  235. Py_BuildValue("h", Opcode::RESERVED7_CODE));
  236. addClassVariable(opcode_type, "RESERVED8_CODE",
  237. Py_BuildValue("h", Opcode::RESERVED8_CODE));
  238. addClassVariable(opcode_type, "RESERVED9_CODE",
  239. Py_BuildValue("h", Opcode::RESERVED9_CODE));
  240. addClassVariable(opcode_type, "RESERVED10_CODE",
  241. Py_BuildValue("h", Opcode::RESERVED10_CODE));
  242. addClassVariable(opcode_type, "RESERVED11_CODE",
  243. Py_BuildValue("h", Opcode::RESERVED11_CODE));
  244. addClassVariable(opcode_type, "RESERVED12_CODE",
  245. Py_BuildValue("h", Opcode::RESERVED12_CODE));
  246. addClassVariable(opcode_type, "RESERVED13_CODE",
  247. Py_BuildValue("h", Opcode::RESERVED13_CODE));
  248. addClassVariable(opcode_type, "RESERVED14_CODE",
  249. Py_BuildValue("h", Opcode::RESERVED14_CODE));
  250. addClassVariable(opcode_type, "RESERVED15_CODE",
  251. Py_BuildValue("h", Opcode::RESERVED15_CODE));
  252. return (true);
  253. }
  254. bool
  255. initModulePart_Question(PyObject* mod) {
  256. if (PyType_Ready(&question_type) < 0) {
  257. return (false);
  258. }
  259. Py_INCREF(&question_type);
  260. PyModule_AddObject(mod, "Question",
  261. reinterpret_cast<PyObject*>(&question_type));
  262. return (true);
  263. }
  264. bool
  265. initModulePart_Rcode(PyObject* mod) {
  266. if (PyType_Ready(&rcode_type) < 0) {
  267. return (false);
  268. }
  269. Py_INCREF(&rcode_type);
  270. void* p = &rcode_type;
  271. if (PyModule_AddObject(mod, "Rcode", static_cast<PyObject*>(p)) != 0) {
  272. Py_DECREF(&rcode_type);
  273. return (false);
  274. }
  275. addClassVariable(rcode_type, "NOERROR_CODE",
  276. Py_BuildValue("h", Rcode::NOERROR_CODE));
  277. addClassVariable(rcode_type, "FORMERR_CODE",
  278. Py_BuildValue("h", Rcode::FORMERR_CODE));
  279. addClassVariable(rcode_type, "SERVFAIL_CODE",
  280. Py_BuildValue("h", Rcode::SERVFAIL_CODE));
  281. addClassVariable(rcode_type, "NXDOMAIN_CODE",
  282. Py_BuildValue("h", Rcode::NXDOMAIN_CODE));
  283. addClassVariable(rcode_type, "NOTIMP_CODE",
  284. Py_BuildValue("h", Rcode::NOTIMP_CODE));
  285. addClassVariable(rcode_type, "REFUSED_CODE",
  286. Py_BuildValue("h", Rcode::REFUSED_CODE));
  287. addClassVariable(rcode_type, "YXDOMAIN_CODE",
  288. Py_BuildValue("h", Rcode::YXDOMAIN_CODE));
  289. addClassVariable(rcode_type, "YXRRSET_CODE",
  290. Py_BuildValue("h", Rcode::YXRRSET_CODE));
  291. addClassVariable(rcode_type, "NXRRSET_CODE",
  292. Py_BuildValue("h", Rcode::NXRRSET_CODE));
  293. addClassVariable(rcode_type, "NOTAUTH_CODE",
  294. Py_BuildValue("h", Rcode::NOTAUTH_CODE));
  295. addClassVariable(rcode_type, "NOTZONE_CODE",
  296. Py_BuildValue("h", Rcode::NOTZONE_CODE));
  297. addClassVariable(rcode_type, "RESERVED11_CODE",
  298. Py_BuildValue("h", Rcode::RESERVED11_CODE));
  299. addClassVariable(rcode_type, "RESERVED12_CODE",
  300. Py_BuildValue("h", Rcode::RESERVED12_CODE));
  301. addClassVariable(rcode_type, "RESERVED13_CODE",
  302. Py_BuildValue("h", Rcode::RESERVED13_CODE));
  303. addClassVariable(rcode_type, "RESERVED14_CODE",
  304. Py_BuildValue("h", Rcode::RESERVED14_CODE));
  305. addClassVariable(rcode_type, "RESERVED15_CODE",
  306. Py_BuildValue("h", Rcode::RESERVED15_CODE));
  307. addClassVariable(rcode_type, "BADVERS_CODE",
  308. Py_BuildValue("h", Rcode::BADVERS_CODE));
  309. return (true);
  310. }
  311. bool
  312. initModulePart_Rdata(PyObject* mod) {
  313. if (PyType_Ready(&rdata_type) < 0) {
  314. return (false);
  315. }
  316. Py_INCREF(&rdata_type);
  317. PyModule_AddObject(mod, "Rdata",
  318. reinterpret_cast<PyObject*>(&rdata_type));
  319. // Add the exceptions to the class
  320. po_InvalidRdataLength = PyErr_NewException("pydnspp.InvalidRdataLength",
  321. NULL, NULL);
  322. PyModule_AddObject(mod, "InvalidRdataLength", po_InvalidRdataLength);
  323. po_InvalidRdataText = PyErr_NewException("pydnspp.InvalidRdataText",
  324. NULL, NULL);
  325. PyModule_AddObject(mod, "InvalidRdataText", po_InvalidRdataText);
  326. po_CharStringTooLong = PyErr_NewException("pydnspp.CharStringTooLong",
  327. NULL, NULL);
  328. PyModule_AddObject(mod, "CharStringTooLong", po_CharStringTooLong);
  329. return (true);
  330. }
  331. bool
  332. initModulePart_RRClass(PyObject* mod) {
  333. po_InvalidRRClass = PyErr_NewException("pydnspp.InvalidRRClass",
  334. NULL, NULL);
  335. Py_INCREF(po_InvalidRRClass);
  336. PyModule_AddObject(mod, "InvalidRRClass", po_InvalidRRClass);
  337. po_IncompleteRRClass = PyErr_NewException("pydnspp.IncompleteRRClass",
  338. NULL, NULL);
  339. Py_INCREF(po_IncompleteRRClass);
  340. PyModule_AddObject(mod, "IncompleteRRClass", po_IncompleteRRClass);
  341. if (PyType_Ready(&rrclass_type) < 0) {
  342. return (false);
  343. }
  344. Py_INCREF(&rrclass_type);
  345. PyModule_AddObject(mod, "RRClass",
  346. reinterpret_cast<PyObject*>(&rrclass_type));
  347. return (true);
  348. }
  349. bool
  350. initModulePart_RRset(PyObject* mod) {
  351. po_EmptyRRset = PyErr_NewException("pydnspp.EmptyRRset", NULL, NULL);
  352. PyModule_AddObject(mod, "EmptyRRset", po_EmptyRRset);
  353. // NameComparisonResult
  354. if (PyType_Ready(&rrset_type) < 0) {
  355. return (false);
  356. }
  357. Py_INCREF(&rrset_type);
  358. PyModule_AddObject(mod, "RRset",
  359. reinterpret_cast<PyObject*>(&rrset_type));
  360. return (true);
  361. }
  362. bool
  363. initModulePart_RRTTL(PyObject* mod) {
  364. po_InvalidRRTTL = PyErr_NewException("pydnspp.InvalidRRTTL", NULL, NULL);
  365. PyModule_AddObject(mod, "InvalidRRTTL", po_InvalidRRTTL);
  366. po_IncompleteRRTTL = PyErr_NewException("pydnspp.IncompleteRRTTL",
  367. NULL, NULL);
  368. PyModule_AddObject(mod, "IncompleteRRTTL", po_IncompleteRRTTL);
  369. if (PyType_Ready(&rrttl_type) < 0) {
  370. return (false);
  371. }
  372. Py_INCREF(&rrttl_type);
  373. PyModule_AddObject(mod, "RRTTL",
  374. reinterpret_cast<PyObject*>(&rrttl_type));
  375. return (true);
  376. }
  377. bool
  378. initModulePart_RRType(PyObject* mod) {
  379. // Add the exceptions to the module
  380. po_InvalidRRType = PyErr_NewException("pydnspp.InvalidRRType", NULL, NULL);
  381. PyModule_AddObject(mod, "InvalidRRType", po_InvalidRRType);
  382. po_IncompleteRRType = PyErr_NewException("pydnspp.IncompleteRRType",
  383. NULL, NULL);
  384. PyModule_AddObject(mod, "IncompleteRRType", po_IncompleteRRType);
  385. if (PyType_Ready(&rrtype_type) < 0) {
  386. return (false);
  387. }
  388. Py_INCREF(&rrtype_type);
  389. PyModule_AddObject(mod, "RRType",
  390. reinterpret_cast<PyObject*>(&rrtype_type));
  391. return (true);
  392. }
  393. bool
  394. initModulePart_TSIGError(PyObject* mod) {
  395. if (PyType_Ready(&tsigerror_type) < 0) {
  396. return (false);
  397. }
  398. void* p = &tsigerror_type;
  399. if (PyModule_AddObject(mod, "TSIGError", static_cast<PyObject*>(p)) < 0) {
  400. return (false);
  401. }
  402. Py_INCREF(&tsigerror_type);
  403. try {
  404. // Constant class variables
  405. // Error codes (bare values)
  406. installClassVariable(tsigerror_type, "BAD_SIG_CODE",
  407. Py_BuildValue("H", TSIGError::BAD_SIG_CODE));
  408. installClassVariable(tsigerror_type, "BAD_KEY_CODE",
  409. Py_BuildValue("H", TSIGError::BAD_KEY_CODE));
  410. installClassVariable(tsigerror_type, "BAD_TIME_CODE",
  411. Py_BuildValue("H", TSIGError::BAD_TIME_CODE));
  412. // Error codes (constant objects)
  413. installClassVariable(tsigerror_type, "NOERROR",
  414. createTSIGErrorObject(TSIGError::NOERROR()));
  415. installClassVariable(tsigerror_type, "FORMERR",
  416. createTSIGErrorObject(TSIGError::FORMERR()));
  417. installClassVariable(tsigerror_type, "SERVFAIL",
  418. createTSIGErrorObject(TSIGError::SERVFAIL()));
  419. installClassVariable(tsigerror_type, "NXDOMAIN",
  420. createTSIGErrorObject(TSIGError::NXDOMAIN()));
  421. installClassVariable(tsigerror_type, "NOTIMP",
  422. createTSIGErrorObject(TSIGError::NOTIMP()));
  423. installClassVariable(tsigerror_type, "REFUSED",
  424. createTSIGErrorObject(TSIGError::REFUSED()));
  425. installClassVariable(tsigerror_type, "YXDOMAIN",
  426. createTSIGErrorObject(TSIGError::YXDOMAIN()));
  427. installClassVariable(tsigerror_type, "YXRRSET",
  428. createTSIGErrorObject(TSIGError::YXRRSET()));
  429. installClassVariable(tsigerror_type, "NXRRSET",
  430. createTSIGErrorObject(TSIGError::NXRRSET()));
  431. installClassVariable(tsigerror_type, "NOTAUTH",
  432. createTSIGErrorObject(TSIGError::NOTAUTH()));
  433. installClassVariable(tsigerror_type, "NOTZONE",
  434. createTSIGErrorObject(TSIGError::NOTZONE()));
  435. installClassVariable(tsigerror_type, "RESERVED11",
  436. createTSIGErrorObject(TSIGError::RESERVED11()));
  437. installClassVariable(tsigerror_type, "RESERVED12",
  438. createTSIGErrorObject(TSIGError::RESERVED12()));
  439. installClassVariable(tsigerror_type, "RESERVED13",
  440. createTSIGErrorObject(TSIGError::RESERVED13()));
  441. installClassVariable(tsigerror_type, "RESERVED14",
  442. createTSIGErrorObject(TSIGError::RESERVED14()));
  443. installClassVariable(tsigerror_type, "RESERVED15",
  444. createTSIGErrorObject(TSIGError::RESERVED15()));
  445. installClassVariable(tsigerror_type, "BAD_SIG",
  446. createTSIGErrorObject(TSIGError::BAD_SIG()));
  447. installClassVariable(tsigerror_type, "BAD_KEY",
  448. createTSIGErrorObject(TSIGError::BAD_KEY()));
  449. installClassVariable(tsigerror_type, "BAD_TIME",
  450. createTSIGErrorObject(TSIGError::BAD_TIME()));
  451. } catch (const std::exception& ex) {
  452. const std::string ex_what =
  453. "Unexpected failure in TSIGError initialization: " +
  454. std::string(ex.what());
  455. PyErr_SetString(po_IscException, ex_what.c_str());
  456. return (false);
  457. } catch (...) {
  458. PyErr_SetString(PyExc_SystemError,
  459. "Unexpected failure in TSIGError initialization");
  460. return (false);
  461. }
  462. return (true);
  463. }
  464. bool
  465. initModulePart_TSIGKey(PyObject* mod) {
  466. if (PyType_Ready(&tsigkey_type) < 0) {
  467. return (false);
  468. }
  469. void* p = &tsigkey_type;
  470. if (PyModule_AddObject(mod, "TSIGKey", static_cast<PyObject*>(p)) != 0) {
  471. return (false);
  472. }
  473. Py_INCREF(&tsigkey_type);
  474. try {
  475. // Constant class variables
  476. installClassVariable(tsigkey_type, "HMACMD5_NAME",
  477. createNameObject(TSIGKey::HMACMD5_NAME()));
  478. installClassVariable(tsigkey_type, "HMACSHA1_NAME",
  479. createNameObject(TSIGKey::HMACSHA1_NAME()));
  480. installClassVariable(tsigkey_type, "HMACSHA256_NAME",
  481. createNameObject(TSIGKey::HMACSHA256_NAME()));
  482. installClassVariable(tsigkey_type, "HMACSHA224_NAME",
  483. createNameObject(TSIGKey::HMACSHA224_NAME()));
  484. installClassVariable(tsigkey_type, "HMACSHA384_NAME",
  485. createNameObject(TSIGKey::HMACSHA384_NAME()));
  486. installClassVariable(tsigkey_type, "HMACSHA512_NAME",
  487. createNameObject(TSIGKey::HMACSHA512_NAME()));
  488. } catch (const std::exception& ex) {
  489. const std::string ex_what =
  490. "Unexpected failure in TSIGKey initialization: " +
  491. std::string(ex.what());
  492. PyErr_SetString(po_IscException, ex_what.c_str());
  493. return (false);
  494. } catch (...) {
  495. PyErr_SetString(PyExc_SystemError,
  496. "Unexpected failure in TSIGKey initialization");
  497. return (false);
  498. }
  499. return (true);
  500. }
  501. bool
  502. initModulePart_TSIGKeyRing(PyObject* mod) {
  503. if (PyType_Ready(&tsigkeyring_type) < 0) {
  504. return (false);
  505. }
  506. Py_INCREF(&tsigkeyring_type);
  507. void* p = &tsigkeyring_type;
  508. if (PyModule_AddObject(mod, "TSIGKeyRing",
  509. static_cast<PyObject*>(p)) != 0) {
  510. Py_DECREF(&tsigkeyring_type);
  511. return (false);
  512. }
  513. addClassVariable(tsigkeyring_type, "SUCCESS",
  514. Py_BuildValue("I", TSIGKeyRing::SUCCESS));
  515. addClassVariable(tsigkeyring_type, "EXIST",
  516. Py_BuildValue("I", TSIGKeyRing::EXIST));
  517. addClassVariable(tsigkeyring_type, "NOTFOUND",
  518. Py_BuildValue("I", TSIGKeyRing::NOTFOUND));
  519. return (true);
  520. }
  521. bool
  522. initModulePart_TSIGContext(PyObject* mod) {
  523. if (PyType_Ready(&tsigcontext_type) < 0) {
  524. return (false);
  525. }
  526. void* p = &tsigcontext_type;
  527. if (PyModule_AddObject(mod, "TSIGContext",
  528. static_cast<PyObject*>(p)) < 0) {
  529. return (false);
  530. }
  531. Py_INCREF(&tsigcontext_type);
  532. try {
  533. // Class specific exceptions
  534. po_TSIGContextError = PyErr_NewException("pydnspp.TSIGContextError",
  535. po_IscException, NULL);
  536. PyObjectContainer(po_TSIGContextError).installToModule(
  537. mod, "TSIGContextError");
  538. // Constant class variables
  539. installClassVariable(tsigcontext_type, "STATE_INIT",
  540. Py_BuildValue("I", TSIGContext::INIT));
  541. installClassVariable(tsigcontext_type, "STATE_SENT_REQUEST",
  542. Py_BuildValue("I", TSIGContext::SENT_REQUEST));
  543. installClassVariable(tsigcontext_type, "STATE_RECEIVED_REQUEST",
  544. Py_BuildValue("I", TSIGContext::RECEIVED_REQUEST));
  545. installClassVariable(tsigcontext_type, "STATE_SENT_RESPONSE",
  546. Py_BuildValue("I", TSIGContext::SENT_RESPONSE));
  547. installClassVariable(tsigcontext_type, "STATE_VERIFIED_RESPONSE",
  548. Py_BuildValue("I",
  549. TSIGContext::VERIFIED_RESPONSE));
  550. installClassVariable(tsigcontext_type, "DEFAULT_FUDGE",
  551. Py_BuildValue("H", TSIGContext::DEFAULT_FUDGE));
  552. } catch (const std::exception& ex) {
  553. const std::string ex_what =
  554. "Unexpected failure in TSIGContext initialization: " +
  555. std::string(ex.what());
  556. PyErr_SetString(po_IscException, ex_what.c_str());
  557. return (false);
  558. } catch (...) {
  559. PyErr_SetString(PyExc_SystemError,
  560. "Unexpected failure in TSIGContext initialization");
  561. return (false);
  562. }
  563. return (true);
  564. }
  565. bool
  566. initModulePart_TSIG(PyObject* mod) {
  567. if (PyType_Ready(&tsig_type) < 0) {
  568. return (false);
  569. }
  570. void* p = &tsig_type;
  571. if (PyModule_AddObject(mod, "TSIG", static_cast<PyObject*>(p)) < 0) {
  572. return (false);
  573. }
  574. Py_INCREF(&tsig_type);
  575. return (true);
  576. }
  577. bool
  578. initModulePart_TSIGRecord(PyObject* mod) {
  579. if (PyType_Ready(&tsigrecord_type) < 0) {
  580. return (false);
  581. }
  582. void* p = &tsigrecord_type;
  583. if (PyModule_AddObject(mod, "TSIGRecord", static_cast<PyObject*>(p)) < 0) {
  584. return (false);
  585. }
  586. Py_INCREF(&tsigrecord_type);
  587. try {
  588. // Constant class variables
  589. installClassVariable(tsigrecord_type, "TSIG_TTL",
  590. Py_BuildValue("I", 0));
  591. } catch (const std::exception& ex) {
  592. const std::string ex_what =
  593. "Unexpected failure in TSIGRecord initialization: " +
  594. std::string(ex.what());
  595. PyErr_SetString(po_IscException, ex_what.c_str());
  596. return (false);
  597. } catch (...) {
  598. PyErr_SetString(PyExc_SystemError,
  599. "Unexpected failure in TSIGRecord initialization");
  600. return (false);
  601. }
  602. return (true);
  603. }
  604. PyModuleDef pydnspp = {
  605. { PyObject_HEAD_INIT(NULL) NULL, 0, NULL},
  606. "pydnspp",
  607. "Python bindings for the classes in the isc::dns namespace.\n\n"
  608. "These bindings match the original C++ API as closely as possible, "
  609. "but are not complete. Some classes are unnecessary (InputBuffer "
  610. "and OutputBuffer for instance), and others may be necessary, but "
  611. "were not up to now.",
  612. -1,
  613. NULL,
  614. NULL,
  615. NULL,
  616. NULL,
  617. NULL
  618. };
  619. }
  620. PyMODINIT_FUNC
  621. PyInit_pydnspp(void) {
  622. PyObject* mod = PyModule_Create(&pydnspp);
  623. if (mod == NULL) {
  624. return (NULL);
  625. }
  626. // Add the exceptions to the class
  627. po_IscException = PyErr_NewException("pydnspp.IscException", NULL, NULL);
  628. PyModule_AddObject(mod, "IscException", po_IscException);
  629. po_InvalidParameter = PyErr_NewException("pydnspp.InvalidParameter",
  630. NULL, NULL);
  631. PyModule_AddObject(mod, "InvalidParameter", po_InvalidParameter);
  632. // for each part included above, we call its specific initializer
  633. if (!initModulePart_Name(mod)) {
  634. return (NULL);
  635. }
  636. if (!initModulePart_MessageRenderer(mod)) {
  637. return (NULL);
  638. }
  639. if (!initModulePart_RRClass(mod)) {
  640. return (NULL);
  641. }
  642. if (!initModulePart_RRType(mod)) {
  643. return (NULL);
  644. }
  645. if (!initModulePart_RRTTL(mod)) {
  646. return (NULL);
  647. }
  648. if (!initModulePart_Rdata(mod)) {
  649. return (NULL);
  650. }
  651. if (!initModulePart_RRset(mod)) {
  652. return (NULL);
  653. }
  654. if (!initModulePart_Question(mod)) {
  655. return (NULL);
  656. }
  657. if (!initModulePart_Opcode(mod)) {
  658. return (NULL);
  659. }
  660. if (!initModulePart_Rcode(mod)) {
  661. return (NULL);
  662. }
  663. if (!initModulePart_Message(mod)) {
  664. return (NULL);
  665. }
  666. if (!initModulePart_EDNS(mod)) {
  667. return (NULL);
  668. }
  669. if (!initModulePart_TSIGKey(mod)) {
  670. return (NULL);
  671. }
  672. if (!initModulePart_TSIGKeyRing(mod)) {
  673. return (NULL);
  674. }
  675. if (!initModulePart_TSIG(mod)) {
  676. return (NULL);
  677. }
  678. if (!initModulePart_TSIGError(mod)) {
  679. return (NULL);
  680. }
  681. if (!initModulePart_TSIGRecord(mod)) {
  682. return (NULL);
  683. }
  684. if (!initModulePart_TSIGContext(mod)) {
  685. return (NULL);
  686. }
  687. return (mod);
  688. }