pydnspp.cc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 <dns/message.h>
  28. #include <dns/opcode.h>
  29. #include <dns/tsig.h>
  30. #include <util/python/pycppwrapper_util.h>
  31. #include "pydnspp_common.h"
  32. #include "edns_python.h"
  33. #include "message_python.h"
  34. #include "messagerenderer_python.h"
  35. #include "name_python.h"
  36. #include "nsec3hash_python.h"
  37. #include "opcode_python.h"
  38. #include "pydnspp_common.h"
  39. #include "pydnspp_towire.h"
  40. #include "question_python.h"
  41. #include "rcode_python.h"
  42. #include "rdata_python.h"
  43. #include "rrclass_python.h"
  44. #include "rrset_python.h"
  45. #include "rrttl_python.h"
  46. #include "rrtype_python.h"
  47. #include "rrset_collection_python.h"
  48. #include "serial_python.h"
  49. #include "tsigerror_python.h"
  50. #include "tsigkey_python.h"
  51. #include "tsig_python.h"
  52. #include "tsig_rdata_python.h"
  53. #include "tsigrecord_python.h"
  54. #include "zone_checker_python.h"
  55. #include "zone_checker_python_inc.cc"
  56. using namespace isc::dns;
  57. using namespace isc::dns::python;
  58. using namespace isc::util::python;
  59. namespace {
  60. bool
  61. initModulePart_EDNS(PyObject* mod) {
  62. // After the type has been initialized, we initialize any exceptions
  63. // that are defined in the wrapper for this class, and add constants
  64. // to the type, if any
  65. if (!initClass(edns_type, "EDNS", mod)) {
  66. return (false);
  67. }
  68. addClassVariable(edns_type, "SUPPORTED_VERSION",
  69. Py_BuildValue("B", EDNS::SUPPORTED_VERSION));
  70. return (true);
  71. }
  72. bool
  73. initModulePart_Message(PyObject* mod) {
  74. if (!initClass(message_type, "Message", mod)) {
  75. return (false);
  76. }
  77. try {
  78. //
  79. // Constant class variables
  80. //
  81. // Parse mode
  82. installClassVariable(message_type, "PARSE",
  83. Py_BuildValue("I", Message::PARSE));
  84. installClassVariable(message_type, "RENDER",
  85. Py_BuildValue("I", Message::RENDER));
  86. // Parse options
  87. installClassVariable(message_type, "PARSE_DEFAULT",
  88. Py_BuildValue("I", Message::PARSE_DEFAULT));
  89. installClassVariable(message_type, "PRESERVE_ORDER",
  90. Py_BuildValue("I", Message::PRESERVE_ORDER));
  91. // Header flags
  92. installClassVariable(message_type, "HEADERFLAG_QR",
  93. Py_BuildValue("I", Message::HEADERFLAG_QR));
  94. installClassVariable(message_type, "HEADERFLAG_AA",
  95. Py_BuildValue("I", Message::HEADERFLAG_AA));
  96. installClassVariable(message_type, "HEADERFLAG_TC",
  97. Py_BuildValue("I", Message::HEADERFLAG_TC));
  98. installClassVariable(message_type, "HEADERFLAG_RD",
  99. Py_BuildValue("I", Message::HEADERFLAG_RD));
  100. installClassVariable(message_type, "HEADERFLAG_RA",
  101. Py_BuildValue("I", Message::HEADERFLAG_RA));
  102. installClassVariable(message_type, "HEADERFLAG_AD",
  103. Py_BuildValue("I", Message::HEADERFLAG_AD));
  104. installClassVariable(message_type, "HEADERFLAG_CD",
  105. Py_BuildValue("I", Message::HEADERFLAG_CD));
  106. // Sections
  107. installClassVariable(message_type, "SECTION_QUESTION",
  108. Py_BuildValue("I", Message::SECTION_QUESTION));
  109. installClassVariable(message_type, "SECTION_ANSWER",
  110. Py_BuildValue("I", Message::SECTION_ANSWER));
  111. installClassVariable(message_type, "SECTION_AUTHORITY",
  112. Py_BuildValue("I", Message::SECTION_AUTHORITY));
  113. installClassVariable(message_type, "SECTION_ADDITIONAL",
  114. Py_BuildValue("I", Message::SECTION_ADDITIONAL));
  115. // Protocol constant
  116. installClassVariable(message_type, "DEFAULT_MAX_UDPSIZE",
  117. Py_BuildValue("I", Message::DEFAULT_MAX_UDPSIZE));
  118. /* Class-specific exceptions */
  119. po_MessageTooShort =
  120. PyErr_NewException("pydnspp.MessageTooShort", NULL, NULL);
  121. PyObjectContainer(po_MessageTooShort).installToModule(
  122. mod, "MessageTooShort");
  123. po_InvalidMessageSection =
  124. PyErr_NewException("pydnspp.InvalidMessageSection", NULL, NULL);
  125. PyObjectContainer(po_InvalidMessageSection).installToModule(
  126. mod, "InvalidMessageSection");
  127. po_InvalidMessageOperation =
  128. PyErr_NewException("pydnspp.InvalidMessageOperation", NULL, NULL);
  129. PyObjectContainer(po_InvalidMessageOperation).installToModule(
  130. mod, "InvalidMessageOperation");
  131. po_InvalidMessageUDPSize =
  132. PyErr_NewException("pydnspp.InvalidMessageUDPSize", NULL, NULL);
  133. PyObjectContainer(po_InvalidMessageUDPSize).installToModule(
  134. mod, "InvalidMessageUDPSize");
  135. po_DNSMessageBADVERS =
  136. PyErr_NewException("pydnspp.DNSMessageBADVERS", NULL, NULL);
  137. PyObjectContainer(po_DNSMessageBADVERS).installToModule(
  138. mod, "DNSMessageBADVERS");
  139. po_UnknownNSEC3HashAlgorithm =
  140. PyErr_NewException("pydnspp.UnknownNSEC3HashAlgorithm", NULL, NULL);
  141. PyObjectContainer(po_UnknownNSEC3HashAlgorithm).installToModule(
  142. mod, "UnknownNSEC3HashAlgorithm");
  143. } catch (const std::exception& ex) {
  144. const std::string ex_what =
  145. "Unexpected failure in Message initialization: " +
  146. std::string(ex.what());
  147. PyErr_SetString(po_IscException, ex_what.c_str());
  148. return (false);
  149. } catch (...) {
  150. PyErr_SetString(PyExc_SystemError,
  151. "Unexpected failure in Message initialization");
  152. return (false);
  153. }
  154. return (true);
  155. }
  156. bool
  157. initModulePart_MessageRenderer(PyObject* mod) {
  158. if (!initClass(messagerenderer_type, "MessageRenderer", mod)) {
  159. return (false);
  160. }
  161. addClassVariable(messagerenderer_type, "CASE_INSENSITIVE",
  162. Py_BuildValue("I", MessageRenderer::CASE_INSENSITIVE));
  163. addClassVariable(messagerenderer_type, "CASE_SENSITIVE",
  164. Py_BuildValue("I", MessageRenderer::CASE_SENSITIVE));
  165. return (true);
  166. }
  167. bool
  168. initModulePart_NameComparisonResult(PyObject* mod) {
  169. if (!initClass(name_comparison_result_type,
  170. "NameComparisonResult", mod)) {
  171. return (false);
  172. }
  173. // Add the enums to the module
  174. po_NameRelation = Py_BuildValue("{i:s,i:s,i:s,i:s}",
  175. NameComparisonResult::SUPERDOMAIN, "SUPERDOMAIN",
  176. NameComparisonResult::SUBDOMAIN, "SUBDOMAIN",
  177. NameComparisonResult::EQUAL, "EQUAL",
  178. NameComparisonResult::COMMONANCESTOR, "COMMONANCESTOR");
  179. addClassVariable(name_comparison_result_type, "NameRelation",
  180. po_NameRelation);
  181. // Add the constants themselves too
  182. addClassVariable(name_comparison_result_type, "SUPERDOMAIN",
  183. Py_BuildValue("I", NameComparisonResult::SUPERDOMAIN));
  184. addClassVariable(name_comparison_result_type, "SUBDOMAIN",
  185. Py_BuildValue("I", NameComparisonResult::SUBDOMAIN));
  186. addClassVariable(name_comparison_result_type, "EQUAL",
  187. Py_BuildValue("I", NameComparisonResult::EQUAL));
  188. addClassVariable(name_comparison_result_type, "COMMONANCESTOR",
  189. Py_BuildValue("I", NameComparisonResult::COMMONANCESTOR));
  190. return (true);
  191. }
  192. bool
  193. initModulePart_Name(PyObject* mod) {
  194. if (!initClass(name_type, "Name", mod)) {
  195. return (false);
  196. }
  197. // Add the constants to the module
  198. addClassVariable(name_type, "MAX_WIRE",
  199. Py_BuildValue("I", Name::MAX_WIRE));
  200. addClassVariable(name_type, "MAX_LABELS",
  201. Py_BuildValue("I", Name::MAX_LABELS));
  202. addClassVariable(name_type, "MAX_LABELLEN",
  203. Py_BuildValue("I", Name::MAX_LABELLEN));
  204. addClassVariable(name_type, "MAX_COMPRESS_POINTER",
  205. Py_BuildValue("I", Name::MAX_COMPRESS_POINTER));
  206. addClassVariable(name_type, "COMPRESS_POINTER_MARK8",
  207. Py_BuildValue("I", Name::COMPRESS_POINTER_MARK8));
  208. addClassVariable(name_type, "COMPRESS_POINTER_MARK16",
  209. Py_BuildValue("I", Name::COMPRESS_POINTER_MARK16));
  210. addClassVariable(name_type, "ROOT_NAME",
  211. createNameObject(Name::ROOT_NAME()));
  212. // Add the exceptions to the module
  213. try {
  214. po_EmptyLabel = PyErr_NewException("pydnspp.EmptyLabel", NULL, NULL);
  215. PyObjectContainer(po_EmptyLabel).installToModule(mod, "EmptyLabel");
  216. po_TooLongName = PyErr_NewException("pydnspp.TooLongName", NULL, NULL);
  217. PyObjectContainer(po_TooLongName).installToModule(mod, "TooLongName");
  218. po_TooLongLabel = PyErr_NewException("pydnspp.TooLongLabel", NULL, NULL);
  219. PyObjectContainer(po_TooLongLabel).installToModule(mod, "TooLongLabel");
  220. po_BadLabelType = PyErr_NewException("pydnspp.BadLabelType", NULL, NULL);
  221. PyObjectContainer(po_BadLabelType).installToModule(mod, "BadLabelType");
  222. po_BadEscape = PyErr_NewException("pydnspp.BadEscape", NULL, NULL);
  223. PyObjectContainer(po_BadEscape).installToModule(mod, "BadEscape");
  224. po_IncompleteName = PyErr_NewException("pydnspp.IncompleteName", NULL,
  225. NULL);
  226. PyObjectContainer(po_IncompleteName).installToModule(mod, "IncompleteName");
  227. po_InvalidBufferPosition =
  228. PyErr_NewException("pydnspp.InvalidBufferPosition", NULL, NULL);
  229. PyObjectContainer(po_InvalidBufferPosition).installToModule(
  230. mod, "InvalidBufferPosition");
  231. // This one could have gone into the message_python.cc file, but is
  232. // already needed here.
  233. po_DNSMessageFORMERR = PyErr_NewException("pydnspp.DNSMessageFORMERR",
  234. NULL, NULL);
  235. PyObjectContainer(po_DNSMessageFORMERR).installToModule(
  236. mod, "DNSMessageFORMERR");
  237. } catch (const std::exception& ex) {
  238. const std::string ex_what =
  239. "Unexpected failure in Name initialization: " +
  240. std::string(ex.what());
  241. PyErr_SetString(po_IscException, ex_what.c_str());
  242. return (false);
  243. } catch (...) {
  244. PyErr_SetString(PyExc_SystemError,
  245. "Unexpected failure in Name initialization");
  246. return (false);
  247. }
  248. return (true);
  249. }
  250. bool
  251. initModulePart_Opcode(PyObject* mod) {
  252. if (!initClass(opcode_type, "Opcode", mod)) {
  253. return (false);
  254. }
  255. try {
  256. installClassVariable(opcode_type, "QUERY_CODE",
  257. Py_BuildValue("h", Opcode::QUERY_CODE));
  258. installClassVariable(opcode_type, "IQUERY_CODE",
  259. Py_BuildValue("h", Opcode::IQUERY_CODE));
  260. installClassVariable(opcode_type, "STATUS_CODE",
  261. Py_BuildValue("h", Opcode::STATUS_CODE));
  262. installClassVariable(opcode_type, "RESERVED3_CODE",
  263. Py_BuildValue("h", Opcode::RESERVED3_CODE));
  264. installClassVariable(opcode_type, "NOTIFY_CODE",
  265. Py_BuildValue("h", Opcode::NOTIFY_CODE));
  266. installClassVariable(opcode_type, "UPDATE_CODE",
  267. Py_BuildValue("h", Opcode::UPDATE_CODE));
  268. installClassVariable(opcode_type, "RESERVED6_CODE",
  269. Py_BuildValue("h", Opcode::RESERVED6_CODE));
  270. installClassVariable(opcode_type, "RESERVED7_CODE",
  271. Py_BuildValue("h", Opcode::RESERVED7_CODE));
  272. installClassVariable(opcode_type, "RESERVED8_CODE",
  273. Py_BuildValue("h", Opcode::RESERVED8_CODE));
  274. installClassVariable(opcode_type, "RESERVED9_CODE",
  275. Py_BuildValue("h", Opcode::RESERVED9_CODE));
  276. installClassVariable(opcode_type, "RESERVED10_CODE",
  277. Py_BuildValue("h", Opcode::RESERVED10_CODE));
  278. installClassVariable(opcode_type, "RESERVED11_CODE",
  279. Py_BuildValue("h", Opcode::RESERVED11_CODE));
  280. installClassVariable(opcode_type, "RESERVED12_CODE",
  281. Py_BuildValue("h", Opcode::RESERVED12_CODE));
  282. installClassVariable(opcode_type, "RESERVED13_CODE",
  283. Py_BuildValue("h", Opcode::RESERVED13_CODE));
  284. installClassVariable(opcode_type, "RESERVED14_CODE",
  285. Py_BuildValue("h", Opcode::RESERVED14_CODE));
  286. installClassVariable(opcode_type, "RESERVED15_CODE",
  287. Py_BuildValue("h", Opcode::RESERVED15_CODE));
  288. installClassVariable(opcode_type, "QUERY",
  289. createOpcodeObject(Opcode::QUERY()));
  290. installClassVariable(opcode_type, "IQUERY",
  291. createOpcodeObject(Opcode::IQUERY()));
  292. installClassVariable(opcode_type, "STATUS",
  293. createOpcodeObject(Opcode::STATUS()));
  294. installClassVariable(opcode_type, "RESERVED3",
  295. createOpcodeObject(Opcode::RESERVED3()));
  296. installClassVariable(opcode_type, "NOTIFY",
  297. createOpcodeObject(Opcode::NOTIFY()));
  298. installClassVariable(opcode_type, "UPDATE",
  299. createOpcodeObject(Opcode::UPDATE()));
  300. installClassVariable(opcode_type, "RESERVED6",
  301. createOpcodeObject(Opcode::RESERVED6()));
  302. installClassVariable(opcode_type, "RESERVED7",
  303. createOpcodeObject(Opcode::RESERVED7()));
  304. installClassVariable(opcode_type, "RESERVED8",
  305. createOpcodeObject(Opcode::RESERVED8()));
  306. installClassVariable(opcode_type, "RESERVED9",
  307. createOpcodeObject(Opcode::RESERVED9()));
  308. installClassVariable(opcode_type, "RESERVED10",
  309. createOpcodeObject(Opcode::RESERVED10()));
  310. installClassVariable(opcode_type, "RESERVED11",
  311. createOpcodeObject(Opcode::RESERVED11()));
  312. installClassVariable(opcode_type, "RESERVED12",
  313. createOpcodeObject(Opcode::RESERVED12()));
  314. installClassVariable(opcode_type, "RESERVED13",
  315. createOpcodeObject(Opcode::RESERVED13()));
  316. installClassVariable(opcode_type, "RESERVED14",
  317. createOpcodeObject(Opcode::RESERVED14()));
  318. installClassVariable(opcode_type, "RESERVED15",
  319. createOpcodeObject(Opcode::RESERVED15()));
  320. } catch (const std::exception& ex) {
  321. const std::string ex_what =
  322. "Unexpected failure in Opcode initialization: " +
  323. std::string(ex.what());
  324. PyErr_SetString(po_IscException, ex_what.c_str());
  325. return (false);
  326. } catch (...) {
  327. PyErr_SetString(PyExc_SystemError,
  328. "Unexpected failure in Opcode initialization");
  329. return (false);
  330. }
  331. return (true);
  332. }
  333. bool
  334. initModulePart_Question(PyObject* mod) {
  335. return (initClass(question_type, "Question", mod));
  336. }
  337. bool
  338. initModulePart_Rcode(PyObject* mod) {
  339. if (!initClass(rcode_type, "Rcode", mod)) {
  340. return (false);
  341. }
  342. try {
  343. installClassVariable(rcode_type, "NOERROR_CODE",
  344. Py_BuildValue("h", Rcode::NOERROR_CODE));
  345. installClassVariable(rcode_type, "FORMERR_CODE",
  346. Py_BuildValue("h", Rcode::FORMERR_CODE));
  347. installClassVariable(rcode_type, "SERVFAIL_CODE",
  348. Py_BuildValue("h", Rcode::SERVFAIL_CODE));
  349. installClassVariable(rcode_type, "NXDOMAIN_CODE",
  350. Py_BuildValue("h", Rcode::NXDOMAIN_CODE));
  351. installClassVariable(rcode_type, "NOTIMP_CODE",
  352. Py_BuildValue("h", Rcode::NOTIMP_CODE));
  353. installClassVariable(rcode_type, "REFUSED_CODE",
  354. Py_BuildValue("h", Rcode::REFUSED_CODE));
  355. installClassVariable(rcode_type, "YXDOMAIN_CODE",
  356. Py_BuildValue("h", Rcode::YXDOMAIN_CODE));
  357. installClassVariable(rcode_type, "YXRRSET_CODE",
  358. Py_BuildValue("h", Rcode::YXRRSET_CODE));
  359. installClassVariable(rcode_type, "NXRRSET_CODE",
  360. Py_BuildValue("h", Rcode::NXRRSET_CODE));
  361. installClassVariable(rcode_type, "NOTAUTH_CODE",
  362. Py_BuildValue("h", Rcode::NOTAUTH_CODE));
  363. installClassVariable(rcode_type, "NOTZONE_CODE",
  364. Py_BuildValue("h", Rcode::NOTZONE_CODE));
  365. installClassVariable(rcode_type, "RESERVED11_CODE",
  366. Py_BuildValue("h", Rcode::RESERVED11_CODE));
  367. installClassVariable(rcode_type, "RESERVED12_CODE",
  368. Py_BuildValue("h", Rcode::RESERVED12_CODE));
  369. installClassVariable(rcode_type, "RESERVED13_CODE",
  370. Py_BuildValue("h", Rcode::RESERVED13_CODE));
  371. installClassVariable(rcode_type, "RESERVED14_CODE",
  372. Py_BuildValue("h", Rcode::RESERVED14_CODE));
  373. installClassVariable(rcode_type, "RESERVED15_CODE",
  374. Py_BuildValue("h", Rcode::RESERVED15_CODE));
  375. installClassVariable(rcode_type, "BADVERS_CODE",
  376. Py_BuildValue("h", Rcode::BADVERS_CODE));
  377. installClassVariable(rcode_type, "NOERROR",
  378. createRcodeObject(Rcode::NOERROR()));
  379. installClassVariable(rcode_type, "FORMERR",
  380. createRcodeObject(Rcode::FORMERR()));
  381. installClassVariable(rcode_type, "SERVFAIL",
  382. createRcodeObject(Rcode::SERVFAIL()));
  383. installClassVariable(rcode_type, "NXDOMAIN",
  384. createRcodeObject(Rcode::NXDOMAIN()));
  385. installClassVariable(rcode_type, "NOTIMP",
  386. createRcodeObject(Rcode::NOTIMP()));
  387. installClassVariable(rcode_type, "REFUSED",
  388. createRcodeObject(Rcode::REFUSED()));
  389. installClassVariable(rcode_type, "YXDOMAIN",
  390. createRcodeObject(Rcode::YXDOMAIN()));
  391. installClassVariable(rcode_type, "YXRRSET",
  392. createRcodeObject(Rcode::YXRRSET()));
  393. installClassVariable(rcode_type, "NXRRSET",
  394. createRcodeObject(Rcode::NXRRSET()));
  395. installClassVariable(rcode_type, "NOTAUTH",
  396. createRcodeObject(Rcode::NOTAUTH()));
  397. installClassVariable(rcode_type, "NOTZONE",
  398. createRcodeObject(Rcode::NOTZONE()));
  399. installClassVariable(rcode_type, "RESERVED11",
  400. createRcodeObject(Rcode::RESERVED11()));
  401. installClassVariable(rcode_type, "RESERVED12",
  402. createRcodeObject(Rcode::RESERVED12()));
  403. installClassVariable(rcode_type, "RESERVED13",
  404. createRcodeObject(Rcode::RESERVED13()));
  405. installClassVariable(rcode_type, "RESERVED14",
  406. createRcodeObject(Rcode::RESERVED14()));
  407. installClassVariable(rcode_type, "RESERVED15",
  408. createRcodeObject(Rcode::RESERVED15()));
  409. installClassVariable(rcode_type, "BADVERS",
  410. createRcodeObject(Rcode::BADVERS()));
  411. } catch (const std::exception& ex) {
  412. const std::string ex_what =
  413. "Unexpected failure in Rcode initialization: " +
  414. std::string(ex.what());
  415. PyErr_SetString(po_IscException, ex_what.c_str());
  416. return (false);
  417. } catch (...) {
  418. PyErr_SetString(PyExc_SystemError,
  419. "Unexpected failure in Rcode initialization");
  420. return (false);
  421. }
  422. return (true);
  423. }
  424. bool
  425. initModulePart_Rdata(PyObject* mod) {
  426. if (!initClass(rdata_type, "Rdata", mod)) {
  427. return (false);
  428. }
  429. // Add the exceptions to the class
  430. try {
  431. po_InvalidRdataLength =
  432. PyErr_NewException("pydnspp.InvalidRdataLength", NULL, NULL);
  433. PyObjectContainer(po_InvalidRdataLength).installToModule(
  434. mod, "InvalidRdataLength");
  435. po_InvalidRdataText = PyErr_NewException("pydnspp.InvalidRdataText",
  436. NULL, NULL);
  437. PyObjectContainer(po_InvalidRdataText).installToModule(
  438. mod, "InvalidRdataText");
  439. po_CharStringTooLong = PyErr_NewException("pydnspp.CharStringTooLong",
  440. NULL, NULL);
  441. PyObjectContainer(po_CharStringTooLong).installToModule(
  442. mod, "CharStringTooLong");
  443. } catch (const std::exception& ex) {
  444. const std::string ex_what =
  445. "Unexpected failure in Rdata initialization: " +
  446. std::string(ex.what());
  447. PyErr_SetString(po_IscException, ex_what.c_str());
  448. return (false);
  449. } catch (...) {
  450. PyErr_SetString(PyExc_SystemError,
  451. "Unexpected failure in Rdata initialization");
  452. return (false);
  453. }
  454. return (true);
  455. }
  456. bool
  457. initModulePart_RRClass(PyObject* mod) {
  458. if (!initClass(rrclass_type, "RRClass", mod)) {
  459. return (false);
  460. }
  461. try {
  462. po_InvalidRRClass = PyErr_NewException("pydnspp.InvalidRRClass",
  463. NULL, NULL);
  464. PyObjectContainer(po_InvalidRRClass).installToModule(
  465. mod, "InvalidRRClass");
  466. po_IncompleteRRClass = PyErr_NewException("pydnspp.IncompleteRRClass",
  467. NULL, NULL);
  468. PyObjectContainer(po_IncompleteRRClass).installToModule(
  469. mod, "IncompleteRRClass");
  470. // Incorporate auto-generated RRClass constants
  471. #include <dns/python/rrclass_constants_inc.cc>
  472. } catch (const std::exception& ex) {
  473. const std::string ex_what =
  474. "Unexpected failure in RRClass initialization: " +
  475. std::string(ex.what());
  476. PyErr_SetString(po_IscException, ex_what.c_str());
  477. return (false);
  478. } catch (...) {
  479. PyErr_SetString(PyExc_SystemError,
  480. "Unexpected failure in RRClass initialization");
  481. return (false);
  482. }
  483. return (true);
  484. }
  485. bool
  486. initModulePart_RRset(PyObject* mod) {
  487. if (!initClass(rrset_type, "RRset", mod)) {
  488. return (false);
  489. }
  490. try {
  491. po_EmptyRRset = PyErr_NewException("pydnspp.EmptyRRset", NULL, NULL);
  492. PyObjectContainer(po_EmptyRRset).installToModule(mod, "EmptyRRset");
  493. } catch (const std::exception& ex) {
  494. const std::string ex_what =
  495. "Unexpected failure in RRset initialization: " +
  496. std::string(ex.what());
  497. PyErr_SetString(po_IscException, ex_what.c_str());
  498. return (false);
  499. } catch (...) {
  500. PyErr_SetString(PyExc_SystemError,
  501. "Unexpected failure in RRset initialization");
  502. return (false);
  503. }
  504. return (true);
  505. }
  506. bool
  507. initModulePart_RRTTL(PyObject* mod) {
  508. if (!initClass(rrttl_type, "RRTTL", mod)) {
  509. return (false);
  510. }
  511. try {
  512. po_InvalidRRTTL = PyErr_NewException("pydnspp.InvalidRRTTL",
  513. NULL, NULL);
  514. PyObjectContainer(po_InvalidRRTTL).installToModule(mod,
  515. "InvalidRRTTL");
  516. po_IncompleteRRTTL = PyErr_NewException("pydnspp.IncompleteRRTTL",
  517. NULL, NULL);
  518. PyObjectContainer(po_IncompleteRRTTL).installToModule(
  519. mod, "IncompleteRRTTL");
  520. } catch (const std::exception& ex) {
  521. const std::string ex_what =
  522. "Unexpected failure in RRTTL initialization: " +
  523. std::string(ex.what());
  524. PyErr_SetString(po_IscException, ex_what.c_str());
  525. return (false);
  526. } catch (...) {
  527. PyErr_SetString(PyExc_SystemError,
  528. "Unexpected failure in RRTTL initialization");
  529. return (false);
  530. }
  531. return (true);
  532. }
  533. bool
  534. initModulePart_RRType(PyObject* mod) {
  535. if (!initClass(rrtype_type, "RRType", mod)) {
  536. return (false);
  537. }
  538. try {
  539. po_InvalidRRType = PyErr_NewException("pydnspp.InvalidRRType",
  540. NULL, NULL);
  541. PyObjectContainer(po_InvalidRRType).installToModule(mod,
  542. "InvalidRRType");
  543. po_IncompleteRRType = PyErr_NewException("pydnspp.IncompleteRRType",
  544. NULL, NULL);
  545. PyObjectContainer(po_IncompleteRRType).installToModule(
  546. mod, "IncompleteRRType");
  547. // Incorporate auto-generated RRType constants
  548. #include <dns/python/rrtype_constants_inc.cc>
  549. } catch (const std::exception& ex) {
  550. const std::string ex_what =
  551. "Unexpected failure in RRType initialization: " +
  552. std::string(ex.what());
  553. PyErr_SetString(po_IscException, ex_what.c_str());
  554. return (false);
  555. } catch (...) {
  556. PyErr_SetString(PyExc_SystemError,
  557. "Unexpected failure in RRType initialization");
  558. return (false);
  559. }
  560. return (true);
  561. }
  562. bool
  563. initModulePart_Serial(PyObject* mod) {
  564. return (initClass(serial_type, "Serial", mod));
  565. }
  566. bool
  567. initModulePart_TSIGError(PyObject* mod) {
  568. if (!initClass(tsigerror_type, "TSIGError", mod)) {
  569. return (false);
  570. }
  571. try {
  572. // Constant class variables
  573. // Error codes (bare values)
  574. installClassVariable(tsigerror_type, "BAD_SIG_CODE",
  575. Py_BuildValue("H", TSIGError::BAD_SIG_CODE));
  576. installClassVariable(tsigerror_type, "BAD_KEY_CODE",
  577. Py_BuildValue("H", TSIGError::BAD_KEY_CODE));
  578. installClassVariable(tsigerror_type, "BAD_TIME_CODE",
  579. Py_BuildValue("H", TSIGError::BAD_TIME_CODE));
  580. // Error codes (constant objects)
  581. installClassVariable(tsigerror_type, "NOERROR",
  582. createTSIGErrorObject(TSIGError::NOERROR()));
  583. installClassVariable(tsigerror_type, "FORMERR",
  584. createTSIGErrorObject(TSIGError::FORMERR()));
  585. installClassVariable(tsigerror_type, "SERVFAIL",
  586. createTSIGErrorObject(TSIGError::SERVFAIL()));
  587. installClassVariable(tsigerror_type, "NXDOMAIN",
  588. createTSIGErrorObject(TSIGError::NXDOMAIN()));
  589. installClassVariable(tsigerror_type, "NOTIMP",
  590. createTSIGErrorObject(TSIGError::NOTIMP()));
  591. installClassVariable(tsigerror_type, "REFUSED",
  592. createTSIGErrorObject(TSIGError::REFUSED()));
  593. installClassVariable(tsigerror_type, "YXDOMAIN",
  594. createTSIGErrorObject(TSIGError::YXDOMAIN()));
  595. installClassVariable(tsigerror_type, "YXRRSET",
  596. createTSIGErrorObject(TSIGError::YXRRSET()));
  597. installClassVariable(tsigerror_type, "NXRRSET",
  598. createTSIGErrorObject(TSIGError::NXRRSET()));
  599. installClassVariable(tsigerror_type, "NOTAUTH",
  600. createTSIGErrorObject(TSIGError::NOTAUTH()));
  601. installClassVariable(tsigerror_type, "NOTZONE",
  602. createTSIGErrorObject(TSIGError::NOTZONE()));
  603. installClassVariable(tsigerror_type, "RESERVED11",
  604. createTSIGErrorObject(TSIGError::RESERVED11()));
  605. installClassVariable(tsigerror_type, "RESERVED12",
  606. createTSIGErrorObject(TSIGError::RESERVED12()));
  607. installClassVariable(tsigerror_type, "RESERVED13",
  608. createTSIGErrorObject(TSIGError::RESERVED13()));
  609. installClassVariable(tsigerror_type, "RESERVED14",
  610. createTSIGErrorObject(TSIGError::RESERVED14()));
  611. installClassVariable(tsigerror_type, "RESERVED15",
  612. createTSIGErrorObject(TSIGError::RESERVED15()));
  613. installClassVariable(tsigerror_type, "BAD_SIG",
  614. createTSIGErrorObject(TSIGError::BAD_SIG()));
  615. installClassVariable(tsigerror_type, "BAD_KEY",
  616. createTSIGErrorObject(TSIGError::BAD_KEY()));
  617. installClassVariable(tsigerror_type, "BAD_TIME",
  618. createTSIGErrorObject(TSIGError::BAD_TIME()));
  619. } catch (const std::exception& ex) {
  620. const std::string ex_what =
  621. "Unexpected failure in TSIGError initialization: " +
  622. std::string(ex.what());
  623. PyErr_SetString(po_IscException, ex_what.c_str());
  624. return (false);
  625. } catch (...) {
  626. PyErr_SetString(PyExc_SystemError,
  627. "Unexpected failure in TSIGError initialization");
  628. return (false);
  629. }
  630. return (true);
  631. }
  632. bool
  633. initModulePart_TSIGKey(PyObject* mod) {
  634. if (!initClass(tsigkey_type, "TSIGKey", mod)) {
  635. return (false);
  636. }
  637. try {
  638. // Constant class variables
  639. installClassVariable(tsigkey_type, "HMACMD5_NAME",
  640. createNameObject(TSIGKey::HMACMD5_NAME()));
  641. installClassVariable(tsigkey_type, "HMACSHA1_NAME",
  642. createNameObject(TSIGKey::HMACSHA1_NAME()));
  643. installClassVariable(tsigkey_type, "HMACSHA256_NAME",
  644. createNameObject(TSIGKey::HMACSHA256_NAME()));
  645. installClassVariable(tsigkey_type, "HMACSHA224_NAME",
  646. createNameObject(TSIGKey::HMACSHA224_NAME()));
  647. installClassVariable(tsigkey_type, "HMACSHA384_NAME",
  648. createNameObject(TSIGKey::HMACSHA384_NAME()));
  649. installClassVariable(tsigkey_type, "HMACSHA512_NAME",
  650. createNameObject(TSIGKey::HMACSHA512_NAME()));
  651. } catch (const std::exception& ex) {
  652. const std::string ex_what =
  653. "Unexpected failure in TSIGKey initialization: " +
  654. std::string(ex.what());
  655. PyErr_SetString(po_IscException, ex_what.c_str());
  656. return (false);
  657. } catch (...) {
  658. PyErr_SetString(PyExc_SystemError,
  659. "Unexpected failure in TSIGKey initialization");
  660. return (false);
  661. }
  662. return (true);
  663. }
  664. bool
  665. initModulePart_TSIGKeyRing(PyObject* mod) {
  666. if (!initClass(tsigkeyring_type, "TSIGKeyRing", mod)) {
  667. return (false);
  668. }
  669. addClassVariable(tsigkeyring_type, "SUCCESS",
  670. Py_BuildValue("I", TSIGKeyRing::SUCCESS));
  671. addClassVariable(tsigkeyring_type, "EXIST",
  672. Py_BuildValue("I", TSIGKeyRing::EXIST));
  673. addClassVariable(tsigkeyring_type, "NOTFOUND",
  674. Py_BuildValue("I", TSIGKeyRing::NOTFOUND));
  675. return (true);
  676. }
  677. bool
  678. initModulePart_TSIGContext(PyObject* mod) {
  679. if (!initClass(tsigcontext_type, "TSIGContext", mod)) {
  680. return (false);
  681. }
  682. try {
  683. // Class specific exceptions
  684. po_TSIGContextError = PyErr_NewException("pydnspp.TSIGContextError",
  685. po_IscException, NULL);
  686. PyObjectContainer(po_TSIGContextError).installToModule(
  687. mod, "TSIGContextError");
  688. // Constant class variables
  689. installClassVariable(tsigcontext_type, "STATE_INIT",
  690. Py_BuildValue("I", TSIGContext::INIT));
  691. installClassVariable(tsigcontext_type, "STATE_SENT_REQUEST",
  692. Py_BuildValue("I", TSIGContext::SENT_REQUEST));
  693. installClassVariable(tsigcontext_type, "STATE_RECEIVED_REQUEST",
  694. Py_BuildValue("I", TSIGContext::RECEIVED_REQUEST));
  695. installClassVariable(tsigcontext_type, "STATE_SENT_RESPONSE",
  696. Py_BuildValue("I", TSIGContext::SENT_RESPONSE));
  697. installClassVariable(tsigcontext_type, "STATE_VERIFIED_RESPONSE",
  698. Py_BuildValue("I",
  699. TSIGContext::VERIFIED_RESPONSE));
  700. installClassVariable(tsigcontext_type, "DEFAULT_FUDGE",
  701. Py_BuildValue("H", TSIGContext::DEFAULT_FUDGE));
  702. } catch (const std::exception& ex) {
  703. const std::string ex_what =
  704. "Unexpected failure in TSIGContext initialization: " +
  705. std::string(ex.what());
  706. PyErr_SetString(po_IscException, ex_what.c_str());
  707. return (false);
  708. } catch (...) {
  709. PyErr_SetString(PyExc_SystemError,
  710. "Unexpected failure in TSIGContext initialization");
  711. return (false);
  712. }
  713. return (true);
  714. }
  715. bool
  716. initModulePart_TSIG(PyObject* mod) {
  717. return (initClass(tsig_type, "TSIG", mod));
  718. }
  719. bool
  720. initModulePart_TSIGRecord(PyObject* mod) {
  721. if (!initClass(tsigrecord_type, "TSIGRecord", mod)) {
  722. return (false);
  723. }
  724. try {
  725. // Constant class variables
  726. installClassVariable(tsigrecord_type, "TSIG_TTL",
  727. Py_BuildValue("I", 0));
  728. } catch (const std::exception& ex) {
  729. const std::string ex_what =
  730. "Unexpected failure in TSIGRecord initialization: " +
  731. std::string(ex.what());
  732. PyErr_SetString(po_IscException, ex_what.c_str());
  733. return (false);
  734. } catch (...) {
  735. PyErr_SetString(PyExc_SystemError,
  736. "Unexpected failure in TSIGRecord initialization");
  737. return (false);
  738. }
  739. return (true);
  740. }
  741. PyMethodDef methods[] = {
  742. { "check_zone", internal::pyCheckZone, METH_VARARGS, dns_checkZone_doc },
  743. { NULL, NULL, 0, NULL }
  744. };
  745. PyModuleDef pydnspp = {
  746. { PyObject_HEAD_INIT(NULL) NULL, 0, NULL},
  747. "pydnspp",
  748. "Python bindings for the classes in the isc::dns namespace.\n\n"
  749. "These bindings match the original C++ API as closely as possible, "
  750. "but are not complete. Some classes are unnecessary (InputBuffer "
  751. "and OutputBuffer for instance), and others may be necessary, but "
  752. "were not up to now.",
  753. -1,
  754. methods,
  755. NULL,
  756. NULL,
  757. NULL,
  758. NULL
  759. };
  760. } // unnamed namespace
  761. PyMODINIT_FUNC
  762. PyInit_pydnspp(void) {
  763. PyObject* mod = PyModule_Create(&pydnspp);
  764. if (mod == NULL) {
  765. return (NULL);
  766. }
  767. try {
  768. // Add the exceptions to the class
  769. po_IscException = PyErr_NewException("pydnspp.IscException", NULL, NULL);
  770. PyObjectContainer(po_IscException).installToModule(mod, "IscException");
  771. po_InvalidOperation = PyErr_NewException("pydnspp.InvalidOperation",
  772. NULL, NULL);
  773. PyObjectContainer(po_InvalidOperation).installToModule(
  774. mod, "InvalidOperation");
  775. po_InvalidParameter = PyErr_NewException("pydnspp.InvalidParameter",
  776. NULL, NULL);
  777. PyObjectContainer(po_InvalidParameter).installToModule(
  778. mod, "InvalidParameter");
  779. } catch (const std::exception& ex) {
  780. const std::string ex_what =
  781. "Unexpected failure in pydnspp initialization: " +
  782. std::string(ex.what());
  783. PyErr_SetString(po_IscException, ex_what.c_str());
  784. return (NULL);
  785. } catch (...) {
  786. PyErr_SetString(PyExc_SystemError,
  787. "Unexpected failure in pydnspp initialization");
  788. return (NULL);
  789. }
  790. // for each part included above, we call its specific initializer
  791. if (!initModulePart_NameComparisonResult(mod)) {
  792. return (NULL);
  793. }
  794. if (!initModulePart_Name(mod)) {
  795. return (NULL);
  796. }
  797. if (!initModulePart_MessageRenderer(mod)) {
  798. return (NULL);
  799. }
  800. if (!initModulePart_NSEC3Hash(mod)) {
  801. return (NULL);
  802. }
  803. if (!initModulePart_RRClass(mod)) {
  804. return (NULL);
  805. }
  806. if (!initModulePart_RRType(mod)) {
  807. return (NULL);
  808. }
  809. if (!initModulePart_RRTTL(mod)) {
  810. return (NULL);
  811. }
  812. if (!initModulePart_Rdata(mod)) {
  813. return (NULL);
  814. }
  815. if (!initModulePart_RRset(mod)) {
  816. return (NULL);
  817. }
  818. if (!initModulePart_Question(mod)) {
  819. return (NULL);
  820. }
  821. if (!initModulePart_Opcode(mod)) {
  822. return (NULL);
  823. }
  824. if (!initModulePart_Rcode(mod)) {
  825. return (NULL);
  826. }
  827. if (!initModulePart_Message(mod)) {
  828. return (NULL);
  829. }
  830. if (!initModulePart_EDNS(mod)) {
  831. return (NULL);
  832. }
  833. if (!initModulePart_Serial(mod)) {
  834. return (NULL);
  835. }
  836. if (!initModulePart_TSIGKey(mod)) {
  837. return (NULL);
  838. }
  839. if (!initModulePart_TSIGKeyRing(mod)) {
  840. return (NULL);
  841. }
  842. if (!initModulePart_TSIG(mod)) {
  843. return (NULL);
  844. }
  845. if (!initModulePart_TSIGError(mod)) {
  846. return (NULL);
  847. }
  848. if (!initModulePart_TSIGRecord(mod)) {
  849. return (NULL);
  850. }
  851. if (!initModulePart_TSIGContext(mod)) {
  852. return (NULL);
  853. }
  854. if (!initModulePart_RRsetCollectionBase(mod)) {
  855. return (NULL);
  856. }
  857. if (!initModulePart_RRsetCollection(mod)) {
  858. return (NULL);
  859. }
  860. return (mod);
  861. }