pydnspp_common.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <Python.h>
  15. #include <exceptions/exceptions.h>
  16. #include <util/buffer.h>
  17. #include <dns/exceptions.h>
  18. #include <dns/name.h>
  19. #include <dns/messagerenderer.h>
  20. #include "pydnspp_common.h"
  21. #include "messagerenderer_python.h"
  22. #include "name_python.h"
  23. #include "rdata_python.h"
  24. #include "rrclass_python.h"
  25. #include "rrtype_python.h"
  26. #include "rrttl_python.h"
  27. #include "rrset_python.h"
  28. #include "rcode_python.h"
  29. #include "opcode_python.h"
  30. #include "tsigkey_python.h"
  31. #include "tsig_rdata_python.h"
  32. #include "tsigerror_python.h"
  33. #include "tsigrecord_python.h"
  34. #include "tsig_python.h"
  35. #include "question_python.h"
  36. #include "message_python.h"
  37. using namespace isc::dns::python;
  38. namespace isc {
  39. namespace dns {
  40. namespace python {
  41. // For our 'general' isc::Exceptions
  42. PyObject* po_IscException;
  43. PyObject* po_InvalidOperation;
  44. PyObject* po_InvalidParameter;
  45. // For our own isc::dns::Exception
  46. PyObject* po_DNSMessageBADVERS;
  47. int
  48. readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence) {
  49. PyObject* el = NULL;
  50. for (size_t i = 0; i < len; i++) {
  51. el = PySequence_GetItem(sequence, i);
  52. if (!el) {
  53. PyErr_SetString(PyExc_TypeError,
  54. "sequence too short");
  55. return (-1);
  56. }
  57. if (PyLong_Check(el)) {
  58. long l = PyLong_AsLong(el);
  59. if (l < 0 || l > 255) {
  60. PyErr_SetString(PyExc_TypeError,
  61. "number in fromWire sequence not between 0 and 255");
  62. return (-1);
  63. }
  64. data[i] = static_cast<uint8_t>(PyLong_AsLong(el));
  65. } else {
  66. PyErr_SetString(PyExc_TypeError,
  67. "not a number in fromWire sequence");
  68. return (-1);
  69. }
  70. }
  71. PySequence_DelSlice(sequence, 0, len);
  72. return (0);
  73. }
  74. int
  75. addClassVariable(PyTypeObject& c, const char* name, PyObject* obj) {
  76. if (obj == NULL) {
  77. PyErr_SetString(PyExc_ValueError,
  78. "NULL object is specified for a class variable");
  79. return (-1);
  80. }
  81. return (PyDict_SetItemString(c.tp_dict, name, obj));
  82. }
  83. bool
  84. initClass(PyTypeObject& type, const char* name, PyObject* mod) {
  85. // We initialize the static description object with PyType_Ready(),
  86. // then add it to the module. This is not just a check! (leaving
  87. // this out results in segmentation faults)
  88. //
  89. void* p = &type;
  90. if (PyType_Ready(&type) < 0 ||
  91. PyModule_AddObject(mod, name, static_cast<PyObject*>(p)) < 0) {
  92. return (false);
  93. }
  94. Py_INCREF(&type);
  95. return (true);
  96. }
  97. }
  98. }
  99. }