pydnspp.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // And of course care has to be taken that all identifiers be unique
  23. #define PY_SSIZE_T_CLEAN
  24. #include <Python.h>
  25. #include <structmember.h>
  26. #include <config.h>
  27. #include <exceptions/exceptions.h>
  28. #include <util/buffer.h>
  29. #include <dns/exceptions.h>
  30. #include <dns/name.h>
  31. #include <dns/messagerenderer.h>
  32. #include "pydnspp_common.h"
  33. #include "messagerenderer_python.h"
  34. #include "name_python.h"
  35. #include "rcode_python.h"
  36. #include "tsigkey_python.h"
  37. #include "tsig_rdata_python.h"
  38. #include "tsigerror_python.h"
  39. #include "tsigrecord_python.h"
  40. #include "tsig_python.h"
  41. namespace isc {
  42. namespace dns {
  43. namespace python {
  44. // For our 'general' isc::Exceptions
  45. PyObject* po_IscException;
  46. PyObject* po_InvalidParameter;
  47. // For our own isc::dns::Exception
  48. PyObject* po_DNSMessageBADVERS;
  49. }
  50. }
  51. }
  52. // order is important here!
  53. using namespace isc::dns::python;
  54. #include <dns/python/rrclass_python.cc> // needs Messagerenderer
  55. #include <dns/python/rrtype_python.cc> // needs Messagerenderer
  56. #include <dns/python/rrttl_python.cc> // needs Messagerenderer
  57. #include <dns/python/rdata_python.cc> // needs Type, Class
  58. #include <dns/python/rrset_python.cc> // needs Rdata, RRTTL
  59. #include <dns/python/question_python.cc> // needs RRClass, RRType, RRTTL,
  60. // Name
  61. #include <dns/python/opcode_python.cc>
  62. #include <dns/python/edns_python.cc> // needs Messagerenderer, Rcode
  63. #include <dns/python/message_python.cc> // needs RRset, Question
  64. //
  65. // Definition of the module
  66. //
  67. namespace {
  68. PyModuleDef pydnspp = {
  69. { PyObject_HEAD_INIT(NULL) NULL, 0, NULL},
  70. "pydnspp",
  71. "Python bindings for the classes in the isc::dns namespace.\n\n"
  72. "These bindings match the original C++ API as closely as possible, "
  73. "but are not complete. Some classes are unnecessary (InputBuffer "
  74. "and OutputBuffer for instance), and others may be necessary, but "
  75. "were not up to now.",
  76. -1,
  77. NULL,
  78. NULL,
  79. NULL,
  80. NULL,
  81. NULL
  82. };
  83. }
  84. PyMODINIT_FUNC
  85. PyInit_pydnspp(void) {
  86. PyObject* mod = PyModule_Create(&pydnspp);
  87. if (mod == NULL) {
  88. return (NULL);
  89. }
  90. // Add the exceptions to the class
  91. po_IscException = PyErr_NewException("pydnspp.IscException", NULL, NULL);
  92. PyModule_AddObject(mod, "IscException", po_IscException);
  93. po_InvalidParameter = PyErr_NewException("pydnspp.InvalidParameter",
  94. NULL, NULL);
  95. PyModule_AddObject(mod, "InvalidParameter", po_InvalidParameter);
  96. // for each part included above, we call its specific initializer
  97. if (!initModulePart_Name(mod)) {
  98. return (NULL);
  99. }
  100. if (!initModulePart_MessageRenderer(mod)) {
  101. return (NULL);
  102. }
  103. if (!initModulePart_RRClass(mod)) {
  104. return (NULL);
  105. }
  106. if (!initModulePart_RRType(mod)) {
  107. return (NULL);
  108. }
  109. if (!initModulePart_RRTTL(mod)) {
  110. return (NULL);
  111. }
  112. if (!initModulePart_Rdata(mod)) {
  113. return (NULL);
  114. }
  115. if (!initModulePart_RRset(mod)) {
  116. return (NULL);
  117. }
  118. if (!initModulePart_Question(mod)) {
  119. return (NULL);
  120. }
  121. if (!initModulePart_Opcode(mod)) {
  122. return (NULL);
  123. }
  124. if (!initModulePart_Rcode(mod)) {
  125. return (NULL);
  126. }
  127. if (!initModulePart_Message(mod)) {
  128. return (NULL);
  129. }
  130. if (!initModulePart_EDNS(mod)) {
  131. return (NULL);
  132. }
  133. if (!initModulePart_TSIGKey(mod)) {
  134. return (NULL);
  135. }
  136. if (!initModulePart_TSIGKeyRing(mod)) {
  137. return (NULL);
  138. }
  139. if (!initModulePart_TSIG(mod)) {
  140. return (NULL);
  141. }
  142. if (!initModulePart_TSIGError(mod)) {
  143. return (NULL);
  144. }
  145. if (!initModulePart_TSIGRecord(mod)) {
  146. return (NULL);
  147. }
  148. if (!initModulePart_TSIGContext(mod)) {
  149. return (NULL);
  150. }
  151. return (mod);
  152. }