dns.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2011 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 <stdexcept>
  16. #include <boost/shared_ptr.hpp>
  17. #include <util/python/pycppwrapper_util.h>
  18. #include <cc/data.h>
  19. #include <acl/acl.h>
  20. #include <acl/dns.h>
  21. #include "dns.h"
  22. #include "dns_requestcontext_python.h"
  23. #include "dns_requestacl_python.h"
  24. #include "dns_requestloader_python.h"
  25. using namespace std;
  26. using boost::shared_ptr;
  27. using namespace isc::util::python;
  28. using namespace isc::data;
  29. using namespace isc::acl::dns;
  30. using namespace isc::acl::dns::python;
  31. #include "dnsacl_inc.cc"
  32. namespace {
  33. // This is a Python binding object corresponding to the singleton loader used
  34. // in the C++ version of the library.
  35. // We can define it as a pure object rather than through an accessor function,
  36. // because in Python we can ensure it has been created and initialized
  37. // in the module initializer by the time it's actually used.
  38. s_RequestLoader* po_REQUEST_LOADER;
  39. PyMethodDef methods[] = {
  40. { NULL, NULL, 0, NULL }
  41. };
  42. PyModuleDef dnsacl = {
  43. { PyObject_HEAD_INIT(NULL) NULL, 0, NULL},
  44. "isc.acl.dns",
  45. dnsacl_doc,
  46. -1,
  47. methods,
  48. NULL,
  49. NULL,
  50. NULL,
  51. NULL
  52. };
  53. } // end of unnamed namespace
  54. namespace isc {
  55. namespace acl {
  56. namespace dns {
  57. namespace python {
  58. PyObject*
  59. getACLException(const char* ex_name) {
  60. PyObject* ex_obj = NULL;
  61. PyObject* acl_module = PyImport_AddModule("isc.acl.acl");
  62. if (acl_module != NULL) {
  63. PyObject* acl_dict = PyModule_GetDict(acl_module);
  64. if (acl_dict != NULL) {
  65. ex_obj = PyDict_GetItemString(acl_dict, ex_name);
  66. }
  67. }
  68. if (ex_obj == NULL) {
  69. ex_obj = PyExc_RuntimeError;
  70. }
  71. return (ex_obj);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. PyMODINIT_FUNC
  78. PyInit_dns(void) {
  79. PyObject* mod = PyModule_Create(&dnsacl);
  80. if (mod == NULL) {
  81. return (NULL);
  82. }
  83. if (!initModulePart_RequestContext(mod)) {
  84. Py_DECREF(mod);
  85. return (NULL);
  86. }
  87. if (!initModulePart_RequestACL(mod)) {
  88. Py_DECREF(mod);
  89. return (NULL);
  90. }
  91. if (!initModulePart_RequestLoader(mod)) {
  92. Py_DECREF(mod);
  93. return (NULL);
  94. }
  95. // Module constants
  96. try {
  97. if (po_REQUEST_LOADER == NULL) {
  98. po_REQUEST_LOADER = static_cast<s_RequestLoader*>(
  99. requestloader_type.tp_alloc(&requestloader_type, 0));
  100. }
  101. if (po_REQUEST_LOADER != NULL) {
  102. // We gain and keep our own reference to the singleton object
  103. // for the same reason as that for exception objects (see comments
  104. // in pycppwrapper_util for more details). Note also that we don't
  105. // bother to release the reference even if exception is thrown
  106. // below (in fact, we cannot delete the singleton loader).
  107. po_REQUEST_LOADER->cppobj = &getRequestLoader();
  108. Py_INCREF(po_REQUEST_LOADER);
  109. }
  110. PyObjectContainer(po_REQUEST_LOADER).installToModule(mod,
  111. "REQUEST_LOADER");
  112. } catch (...) {
  113. Py_DECREF(mod);
  114. return (NULL);
  115. }
  116. return (mod);
  117. }