123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include <Python.h>
- #include <dns/opcode.h>
- #include <util/python/pycppwrapper_util.h>
- #include "pydnspp_common.h"
- #include "opcode_python.h"
- #include "edns_python.h"
- using namespace isc::dns;
- using namespace isc::dns::python;
- using namespace isc::util;
- using namespace isc::util::python;
- namespace {
- class s_Opcode : public PyObject {
- public:
- s_Opcode() : cppobj(NULL), static_code(false) {}
- const isc::dns::Opcode* cppobj;
- bool static_code;
- };
- typedef CPPPyObjectContainer<s_Opcode, Opcode> OpcodeContainer;
- int Opcode_init(s_Opcode* const self, PyObject* args);
- void Opcode_destroy(s_Opcode* const self);
- PyObject* Opcode_getCode(const s_Opcode* const self);
- PyObject* Opcode_toText(const s_Opcode* const self);
- PyObject* Opcode_str(PyObject* self);
- PyMethodDef Opcode_methods[] = {
- { "get_code", reinterpret_cast<PyCFunction>(Opcode_getCode), METH_NOARGS,
- "Returns the code value" },
- { "to_text", reinterpret_cast<PyCFunction>(Opcode_toText), METH_NOARGS,
- "Returns the text representation" },
- { NULL, NULL, 0, NULL }
- };
- int
- Opcode_init(s_Opcode* const self, PyObject* args) {
- uint8_t code = 0;
- if (PyArg_ParseTuple(args, "b", &code)) {
- try {
- self->cppobj = new Opcode(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);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError, "Invalid arguments to Opcode constructor");
- return (-1);
- }
- void
- Opcode_destroy(s_Opcode* const self) {
-
-
- if (!self->static_code) {
- delete self->cppobj;
- }
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- Opcode_getCode(const s_Opcode* const self) {
- return (Py_BuildValue("I", self->cppobj->getCode()));
- }
- PyObject*
- Opcode_toText(const s_Opcode* const self) {
- return (Py_BuildValue("s", self->cppobj->toText().c_str()));
- }
- PyObject*
- Opcode_str(PyObject* self) {
-
- return (PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- PyObject*
- Opcode_richcmp(const s_Opcode* const self, const s_Opcode* 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; Opcode");
- return (NULL);
- case Py_LE:
- PyErr_SetString(PyExc_TypeError, "Unorderable type; Opcode");
- 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; Opcode");
- return (NULL);
- case Py_GE:
- PyErr_SetString(PyExc_TypeError, "Unorderable type; Opcode");
- return (NULL);
- }
- if (c)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyTypeObject opcode_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.Opcode",
- sizeof(s_Opcode),
- 0,
- (destructor)Opcode_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Opcode_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The Opcode class objects represent standard OPCODEs "
- "of the header section of DNS messages.",
- NULL,
- NULL,
- (richcmpfunc)Opcode_richcmp,
- 0,
- NULL,
- NULL,
- Opcode_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)Opcode_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- PyObject*
- createOpcodeObject(const Opcode& source) {
- OpcodeContainer container(PyObject_New(s_Opcode, &opcode_type));
- container.set(new Opcode(source));
- return (container.release());
- }
- bool
- PyOpcode_Check(PyObject* obj) {
- if (obj == NULL) {
- isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
- }
- return (PyObject_TypeCheck(obj, &opcode_type));
- }
- const Opcode&
- PyOpcode_ToOpcode(const PyObject* opcode_obj) {
- if (opcode_obj == NULL) {
- isc_throw(PyCPPWrapperException,
- "obj argument NULL in Opcode PyObject conversion");
- }
- const s_Opcode* opcode = static_cast<const s_Opcode*>(opcode_obj);
- return (*opcode->cppobj);
- }
- }
- }
- }
|