123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #include <Python.h>
- #include <exceptions/exceptions.h>
- #include <dns/rcode.h>
- #include <util/python/pycppwrapper_util.h>
- #include "pydnspp_common.h"
- #include "rcode_python.h"
- using namespace isc::dns;
- using namespace isc::dns::python;
- using namespace isc::util::python;
- namespace {
- class s_Rcode : public PyObject {
- public:
- s_Rcode() : cppobj(NULL), static_code(false) {};
- const Rcode* cppobj;
- bool static_code;
- };
- typedef CPPPyObjectContainer<s_Rcode, Rcode> RcodeContainer;
- int Rcode_init(s_Rcode* const self, PyObject* args);
- void Rcode_destroy(s_Rcode* const self);
- PyObject* Rcode_getCode(const s_Rcode* const self);
- PyObject* Rcode_getExtendedCode(const s_Rcode* const self);
- PyObject* Rcode_toText(const s_Rcode* const self);
- PyObject* Rcode_str(PyObject* self);
- PyObject* Rcode_richcmp(const s_Rcode* const self,
- const s_Rcode* const other, int op);
- PyMethodDef Rcode_methods[] = {
- { "get_code", reinterpret_cast<PyCFunction>(Rcode_getCode), METH_NOARGS,
- "Returns the code value" },
- { "get_extended_code",
- reinterpret_cast<PyCFunction>(Rcode_getExtendedCode), METH_NOARGS,
- "Returns the upper 8-bit part of the extended code value" },
- { "to_text", reinterpret_cast<PyCFunction>(Rcode_toText), METH_NOARGS,
- "Returns the text representation" },
- { NULL, NULL, 0, NULL }
- };
- int
- Rcode_init(s_Rcode* const self, PyObject* args) {
- long code = 0;
- int ext_code = 0;
- if (PyArg_ParseTuple(args, "l", &code)) {
- if (code < 0 || code > 0xffff) {
- PyErr_SetString(PyExc_ValueError, "Rcode out of range");
- return (-1);
- }
- ext_code = -1;
- } else if (PyArg_ParseTuple(args, "li", &code, &ext_code)) {
- if (code < 0 || code > 0xff || ext_code < 0 || ext_code > 0xff) {
- PyErr_SetString(PyExc_ValueError, "Rcode out of range");
- return (-1);
- }
- } else {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to Rcode constructor");
- return (-1);
- }
- try {
- if (ext_code == -1) {
- self->cppobj = new Rcode(code);
- } else {
- self->cppobj = new Rcode(code, ext_code);
- }
- self->static_code = false;
- } catch (const isc::OutOfRange& ex) {
- PyErr_SetString(PyExc_OverflowError, ex.what());
- return (-1);
- } catch (...) {
- PyErr_SetString(po_IscException, "Unexpected exception");
- return (-1);
- }
- return (0);
- }
- void
- Rcode_destroy(s_Rcode* const self) {
-
-
- if (!self->static_code) {
- delete self->cppobj;
- }
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- Rcode_getCode(const s_Rcode* const self) {
- return (Py_BuildValue("I", self->cppobj->getCode()));
- }
- PyObject*
- Rcode_getExtendedCode(const s_Rcode* const self) {
- return (Py_BuildValue("I", self->cppobj->getExtendedCode()));
- }
- PyObject*
- Rcode_toText(const s_Rcode* const self) {
- return (Py_BuildValue("s", self->cppobj->toText().c_str()));
- }
- PyObject*
- Rcode_str(PyObject* self) {
-
- return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- PyObject*
- Rcode_richcmp(const s_Rcode* const self, const s_Rcode* const other,
- const int op)
- {
- bool c = false;
-
-
- if (!other || (self->ob_type != other->ob_type)) {
- Py_RETURN_FALSE;
- }
-
- switch (op) {
- case Py_LT:
- PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
- return (NULL);
- case Py_LE:
- PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
- return (NULL);
- case Py_EQ:
- c = (*self->cppobj == *other->cppobj);
- break;
- case Py_NE:
- c = (*self->cppobj != *other->cppobj);
- break;
- case Py_GT:
- PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
- return (NULL);
- case Py_GE:
- PyErr_SetString(PyExc_TypeError, "Unorderable type; Rcode");
- return (NULL);
- }
- if (c)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyTypeObject rcode_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.Rcode",
- sizeof(s_Rcode),
- 0,
- (destructor)Rcode_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Rcode_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The Rcode class objects represent standard RCODEs"
- "of the header section of DNS messages.",
- NULL,
- NULL,
- reinterpret_cast<richcmpfunc>(Rcode_richcmp),
- 0,
- NULL,
- NULL,
- Rcode_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)Rcode_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- PyObject*
- createRcodeObject(const Rcode& source) {
- RcodeContainer container(PyObject_New(s_Rcode, &rcode_type));
- container.set(new Rcode(source));
- return (container.release());
- }
- bool
- PyRcode_Check(PyObject* obj) {
- if (obj == NULL) {
- isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
- }
- return (PyObject_TypeCheck(obj, &rcode_type));
- }
- const Rcode&
- PyRcode_ToRcode(const PyObject* rcode_obj) {
- if (rcode_obj == NULL) {
- isc_throw(PyCPPWrapperException,
- "obj argument NULL in Rcode PyObject conversion");
- }
- const s_Rcode* rcode = static_cast<const s_Rcode*>(rcode_obj);
- return (*rcode->cppobj);
- }
- }
- }
- }
|