tsig_python.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <dns/tsig.h>
  15. using namespace isc::dns;
  16. //
  17. // Definition of the classes
  18. //
  19. // For each class, we need a struct, a helper functions (init, destroy,
  20. // and static wrappers around the methods we export), a list of methods,
  21. // and a type description
  22. namespace {
  23. // The s_* Class simply covers one instantiation of the object
  24. class s_TSIGContext : public PyObject {
  25. public:
  26. TSIGContext* tsig_ctx;
  27. };
  28. //
  29. // We declare the functions here, the definitions are below
  30. // the type definition of the object, since both can use the other
  31. //
  32. // General creation and destruction
  33. int TSIGContext_init(s_TSIGContext* self, PyObject* args);
  34. void TSIGContext_destroy(s_TSIGContext* self);
  35. // These are the functions we export
  36. // For a minimal support, we don't need them.
  37. // This list contains the actual set of functions we have in
  38. // python. Each entry has
  39. // 1. Python method name
  40. // 2. Our static function here
  41. // 3. Argument type
  42. // 4. Documentation
  43. PyMethodDef TSIGContext_methods[] = {
  44. { NULL, NULL, 0, NULL }
  45. };
  46. // This defines the complete type for reflection in python and
  47. // parsing of PyObject* to s_EDNS
  48. // Most of the functions are not actually implemented and NULL here.
  49. PyTypeObject tsig_context_type = {
  50. PyVarObject_HEAD_INIT(NULL, 0)
  51. "libdns_python.TSIGContext",
  52. sizeof(s_TSIGContext), // tp_basicsize
  53. 0, // tp_itemsize
  54. (destructor)TSIGContext_destroy, // tp_dealloc
  55. NULL, // tp_print
  56. NULL, // tp_getattr
  57. NULL, // tp_setattr
  58. NULL, // tp_reserved
  59. NULL, // tp_repr
  60. NULL, // tp_as_number
  61. NULL, // tp_as_sequence
  62. NULL, // tp_as_mapping
  63. NULL, // tp_hash
  64. NULL, // tp_call
  65. NULL, // tp_str
  66. NULL, // tp_getattro
  67. NULL, // tp_setattro
  68. NULL, // tp_as_buffer
  69. Py_TPFLAGS_DEFAULT, // tp_flags
  70. "The TSIGContext class maintains a context of a signed session of "
  71. "DNS transactions by TSIG.",
  72. NULL, // tp_traverse
  73. NULL, // tp_clear
  74. NULL, // tp_richcompare
  75. 0, // tp_weaklistoffset
  76. NULL, // tp_iter
  77. NULL, // tp_iternext
  78. TSIGContext_methods, // tp_methods
  79. NULL, // tp_members
  80. NULL, // tp_getset
  81. NULL, // tp_base
  82. NULL, // tp_dict
  83. NULL, // tp_descr_get
  84. NULL, // tp_descr_set
  85. 0, // tp_dictoffset
  86. (initproc)TSIGContext_init, // tp_init
  87. NULL, // tp_alloc
  88. PyType_GenericNew, // tp_new
  89. NULL, // tp_free
  90. NULL, // tp_is_gc
  91. NULL, // tp_bases
  92. NULL, // tp_mro
  93. NULL, // tp_cache
  94. NULL, // tp_subclasses
  95. NULL, // tp_weaklist
  96. NULL, // tp_del
  97. 0 // tp_version_tag
  98. };
  99. int
  100. TSIGContext_init(s_TSIGContext* self, PyObject* args) {
  101. const s_TSIGKey* tsigkey_obj;
  102. try {
  103. if (PyArg_ParseTuple(args, "O!", &tsigkey_type, &tsigkey_obj)) {
  104. self->tsig_ctx = new TSIGContext(*tsigkey_obj->tsigkey);
  105. return (0);
  106. }
  107. } catch (...) {
  108. PyErr_SetString(po_IscException, "Unexpected exception");
  109. return (-1);
  110. }
  111. PyErr_Clear();
  112. PyErr_SetString(PyExc_TypeError,
  113. "Invalid arguments to TSIGContext constructor");
  114. return (-1);
  115. }
  116. void
  117. TSIGContext_destroy(s_TSIGContext* const self) {
  118. delete self->tsig_ctx;
  119. self->tsig_ctx = NULL;
  120. Py_TYPE(self)->tp_free(self);
  121. }
  122. // Module Initialization, all statics are initialized here
  123. bool
  124. initModulePart_TSIGContext(PyObject* mod) {
  125. // We initialize the static description object with PyType_Ready(),
  126. // then add it to the module. This is not just a check! (leaving
  127. // this out results in segmentation faults)
  128. if (PyType_Ready(&tsig_context_type) < 0) {
  129. return (false);
  130. }
  131. Py_INCREF(&tsig_context_type);
  132. void* p = &tsig_context_type;
  133. PyModule_AddObject(mod, "TSIGContext", static_cast<PyObject*>(p));
  134. addClassVariable(tsig_context_type, "DEFAULT_FUDGE",
  135. Py_BuildValue("H", TSIGContext::DEFAULT_FUDGE));
  136. return (true);
  137. }
  138. } // end of anonymous namespace