123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- #include <Python.h>
- #include <string>
- #include <stdexcept>
- #include <util/python/pycppwrapper_util.h>
- #include <dns/rdataclass.h>
- #include "pydnspp_common.h"
- #include "pydnspp_towire.h"
- #include "name_python.h"
- #include "tsig_rdata_python.h"
- using namespace std;
- using namespace isc::util::python;
- using namespace isc::dns;
- using namespace isc::dns::rdata;
- using namespace isc::dns::python;
- s_TSIG::s_TSIG() : cppobj(NULL) {
- }
- namespace {
- typedef CPPPyObjectContainer<s_TSIG, any::TSIG> TSIGContainer;
- int TSIG_init(s_TSIG* self, PyObject* args);
- void TSIG_destroy(s_TSIG* self);
- PyObject* TSIG_toText(const s_TSIG* const self);
- PyObject* TSIG_getAlgorithm(const s_TSIG* const self);
- PyObject* TSIG_getTimeSigned(const s_TSIG* const self);
- PyObject* TSIG_getFudge(const s_TSIG* const self);
- PyObject* TSIG_getOriginalID(const s_TSIG* const self);
- PyObject* TSIG_getError(const s_TSIG* const self);
- PyObject* TSIG_getMAC(const s_TSIG* const self);
- PyObject* TSIG_getOtherData(const s_TSIG* const self);
- PyObject* TSIG_str(PyObject* self);
- PyObject* TSIG_richcmp(const s_TSIG* const self,
- const s_TSIG* const other, int op);
- PyObject* TSIG_toWire(const s_TSIG* self, PyObject* args);
- PyMethodDef TSIG_methods[] = {
- { "get_algorithm", reinterpret_cast<PyCFunction>(TSIG_getAlgorithm),
- METH_NOARGS,
- "Return the algorithm name." },
- { "get_timesigned", reinterpret_cast<PyCFunction>(TSIG_getTimeSigned),
- METH_NOARGS,
- "Return the value of the Time Signed field. "
- "The returned value does not exceed 2^48-1."
- },
- { "get_fudge", reinterpret_cast<PyCFunction>(TSIG_getFudge),
- METH_NOARGS,
- "Return the value of the Fudge field." },
- { "get_original_id", reinterpret_cast<PyCFunction>(TSIG_getOriginalID),
- METH_NOARGS,
- "Return the value of the Original ID field." },
- { "get_error", reinterpret_cast<PyCFunction>(TSIG_getError),
- METH_NOARGS,
- "Return the value of the Error field." },
- { "get_mac", reinterpret_cast<PyCFunction>(TSIG_getMAC),
- METH_NOARGS,
- "Return the value of the MAC field."
- "If it's empty, return None." },
- { "get_other_data", reinterpret_cast<PyCFunction>(TSIG_getOtherData),
- METH_NOARGS,
- "Return the value of the Other Data field."
- "If it's empty, return None." },
- { "to_text", reinterpret_cast<PyCFunction>(TSIG_toText), METH_NOARGS,
- "Returns the text representation" },
- { "to_wire", reinterpret_cast<PyCFunction>(TSIG_toWire), METH_VARARGS,
- "Converts the TSIG object to wire format.\n"
- "The argument can be either a MessageRenderer or an object that "
- "implements the sequence interface. If the object is mutable "
- "(for instance a bytearray()), the wire data is added in-place.\n"
- "If it is not (for instance a bytes() object), a new object is "
- "returned" },
- { NULL, NULL, 0, NULL }
- };
- int
- TSIG_init(s_TSIG* self, PyObject* args) {
- try {
-
- const char* rdata_str;
- if (PyArg_ParseTuple(args, "s", &rdata_str)) {
- self->cppobj = new any::TSIG(string(rdata_str));
- return (0);
- }
- } catch (const exception& ex) {
- const string ex_what = "Failed to construct TSIG object: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- return (-1);
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in constructing TSIG");
- return (-1);
- }
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to TSIG constructor");
- return (-1);
- }
- void
- TSIG_destroy(s_TSIG* const self) {
- delete self->cppobj;
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- TSIG_getAlgorithm(const s_TSIG* const self) {
- try {
- return (createNameObject(self->cppobj->getAlgorithm()));
- } catch (const exception& ex) {
- const string ex_what =
- "Failed to get TSIG algorithm: " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
- "getting TSIG algorithm");
- }
- return (NULL);
- }
- PyObject*
- TSIG_getTimeSigned(const s_TSIG* const self) {
- return (Py_BuildValue("K", self->cppobj->getTimeSigned()));
- }
- PyObject*
- TSIG_getFudge(const s_TSIG* const self) {
- return (Py_BuildValue("H", self->cppobj->getFudge()));
- }
- PyObject*
- TSIG_getOriginalID(const s_TSIG* const self) {
- return (Py_BuildValue("H", self->cppobj->getOriginalID()));
- }
- PyObject*
- TSIG_getError(const s_TSIG* const self) {
- return (Py_BuildValue("H", self->cppobj->getError()));
- }
- PyObject*
- TSIG_getMAC(const s_TSIG* const self) {
- return (Py_BuildValue("y#", self->cppobj->getMAC(),
- self->cppobj->getMACSize()));
- }
- PyObject*
- TSIG_getOtherData(const s_TSIG* const self) {
- return (Py_BuildValue("y#", self->cppobj->getOtherData(),
- self->cppobj->getOtherLen()));
- }
- PyObject*
- TSIG_toText(const s_TSIG* const self) {
- try {
-
- return (Py_BuildValue("s", self->cppobj->toText().c_str()));
- } catch (const exception& ex) {
- const string ex_what =
- "Failed to convert TSIG object to text: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
- "converting TSIG object to text");
- }
- return (NULL);
- }
- PyObject*
- TSIG_str(PyObject* self) {
-
- return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- PyObject*
- TSIG_toWire(const s_TSIG* const self, PyObject* args) {
- typedef any::TSIG TSIGRdata;
- return (toWireWrapper<s_TSIG, TSIGRdata, ToWireCallVoid<const TSIGRdata> >(
- self, args));
- }
- PyObject*
- TSIG_richcmp(const s_TSIG* const self,
- const s_TSIG* const other,
- const int op)
- {
- bool c = false;
-
-
- if (other == NULL || (self->ob_type != other->ob_type)) {
- Py_RETURN_FALSE;
- }
-
- const int cmp = self->cppobj->compare(*other->cppobj);
- switch (op) {
- case Py_EQ:
- c = (cmp == 0);
- break;
- case Py_NE:
- c = (cmp != 0);
- break;
- case Py_GT:
- c = (cmp > 0);
- break;
- case Py_GE:
- c = (cmp >= 0);
- break;
- case Py_LT:
- c = (cmp < 0);
- break;
- case Py_LE:
- c = (cmp <= 0);
- break;
- default:
- PyErr_SetString(PyExc_IndexError,
- "Unhandled rich comparison operator for TSIG");
- return (NULL);
- }
- if (c) {
- Py_RETURN_TRUE;
- } else {
- Py_RETURN_FALSE;
- }
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyTypeObject tsig_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "libdns_python.TSIG",
- sizeof(s_TSIG),
- 0,
- reinterpret_cast<destructor>(TSIG_destroy),
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- TSIG_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The TSIG class objects represents the TSIG RDATA as defined in RFC2845.",
- NULL,
- NULL,
- reinterpret_cast<richcmpfunc>(TSIG_richcmp),
- 0,
- NULL,
- NULL,
- TSIG_methods,
- NULL,
- NULL,
-
-
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- reinterpret_cast<initproc>(TSIG_init),
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- bool
- initModulePart_TSIG(PyObject* mod) {
-
-
-
- if (PyType_Ready(&tsig_type) < 0) {
- return (false);
- }
- void* p = &tsig_type;
- if (PyModule_AddObject(mod, "TSIG", static_cast<PyObject*>(p)) < 0) {
- return (false);
- }
- Py_INCREF(&tsig_type);
- return (true);
- }
- PyObject*
- createTSIGObject(const any::TSIG& source) {
- TSIGContainer container = PyObject_New(s_TSIG, &tsig_type);
- container.set(new any::TSIG(source));
- return (container.release());
- }
- }
- }
- }
|