pydnspp_common.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_InvalidParameter;
  44. // For our own isc::dns::Exception
  45. PyObject* po_DNSMessageBADVERS;
  46. int
  47. readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence) {
  48. PyObject* el = NULL;
  49. for (size_t i = 0; i < len; i++) {
  50. el = PySequence_GetItem(sequence, i);
  51. if (!el) {
  52. PyErr_SetString(PyExc_TypeError,
  53. "sequence too short");
  54. return (-1);
  55. }
  56. if (PyLong_Check(el)) {
  57. long l = PyLong_AsLong(el);
  58. if (l < 0 || l > 255) {
  59. PyErr_SetString(PyExc_TypeError,
  60. "number in fromWire sequence not between 0 and 255");
  61. return (-1);
  62. }
  63. data[i] = static_cast<uint8_t>(PyLong_AsLong(el));
  64. } else {
  65. PyErr_SetString(PyExc_TypeError,
  66. "not a number in fromWire sequence");
  67. return (-1);
  68. }
  69. }
  70. PySequence_DelSlice(sequence, 0, len);
  71. return (0);
  72. }
  73. int
  74. addClassVariable(PyTypeObject& c, const char* name, PyObject* obj) {
  75. if (obj == NULL) {
  76. PyErr_SetString(PyExc_ValueError,
  77. "NULL object is specified for a class variable");
  78. return (-1);
  79. }
  80. return (PyDict_SetItemString(c.tp_dict, name, obj));
  81. }
  82. }
  83. }
  84. }