123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- #define PY_SSIZE_T_CLEAN
- #include <Python.h>
- #include <string>
- #include <stdexcept>
- #include <exceptions/exceptions.h>
- #include <util/python/pycppwrapper_util.h>
- #include <dns/tsig.h>
- #include "pydnspp_common.h"
- #include "name_python.h"
- #include "tsigkey_python.h"
- #include "tsigerror_python.h"
- #include "tsigrecord_python.h"
- #include "tsig_python.h"
- using namespace std;
- using namespace isc;
- using namespace isc::util::python;
- using namespace isc::dns;
- using namespace isc::dns::python;
- s_TSIGContext::s_TSIGContext() : cppobj(NULL) {
- }
- namespace {
- typedef CPPPyObjectContainer<s_TSIGContext, TSIGContext> TSIGContextContainer;
- int TSIGContext_init(s_TSIGContext* self, PyObject* args);
- void TSIGContext_destroy(s_TSIGContext* self);
- PyObject* TSIGContext_getState(s_TSIGContext* self);
- PyObject* TSIGContext_getError(s_TSIGContext* self);
- PyObject* TSIGContext_sign(s_TSIGContext* self, PyObject* args);
- PyObject* TSIGContext_verify(s_TSIGContext* self, PyObject* args);
- PyMethodDef TSIGContext_methods[] = {
- { "get_state", reinterpret_cast<PyCFunction>(TSIGContext_getState),
- METH_NOARGS,
- "Return the current state of the context (mainly for tests)" },
- { "get_error", reinterpret_cast<PyCFunction>(TSIGContext_getError),
- METH_NOARGS,
- "Return the TSIG error as a result of the latest verification" },
- { "sign",
- reinterpret_cast<PyCFunction>(TSIGContext_sign), METH_VARARGS,
- "Sign a DNS message." },
- { "verify",
- reinterpret_cast<PyCFunction>(TSIGContext_verify), METH_VARARGS,
- "Verify a DNS message." },
- { NULL, NULL, 0, NULL }
- };
- int
- TSIGContext_init(s_TSIGContext* self, PyObject* args) {
- try {
-
- const s_TSIGKey* tsigkey_obj;
- if (PyArg_ParseTuple(args, "O!", &tsigkey_type, &tsigkey_obj)) {
- self->cppobj = new TSIGContext(*tsigkey_obj->cppobj);
- return (0);
- }
-
- PyErr_Clear();
- const s_Name* keyname_obj;
- const s_Name* algname_obj;
- const s_TSIGKeyRing* keyring_obj;
- if (PyArg_ParseTuple(args, "O!O!O!", &name_type, &keyname_obj,
- &name_type, &algname_obj, &tsigkeyring_type,
- &keyring_obj)) {
- self->cppobj = new TSIGContext(*keyname_obj->cppobj,
- *algname_obj->cppobj,
- *keyring_obj->cppobj);
- return (0);
- }
- } catch (const exception& ex) {
- const string ex_what = "Failed to construct TSIGContext object: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- return (-1);
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in constructing TSIGContext");
- return (-1);
- }
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to TSIGContext constructor");
- return (-1);
- }
- void
- TSIGContext_destroy(s_TSIGContext* const self) {
- delete self->cppobj;
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- TSIGContext_getState(s_TSIGContext* self) {
- return (Py_BuildValue("I", self->cppobj->getState()));
- }
- PyObject*
- TSIGContext_getError(s_TSIGContext* self) {
- try {
- PyObjectContainer container(createTSIGErrorObject(
- self->cppobj->getError()));
- return (Py_BuildValue("O", container.get()));
- } catch (const exception& ex) {
- const string ex_what =
- "Unexpectedly failed to get TSIGContext error: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in TSIGContext.get_error");
- }
- return (NULL);
- }
- PyObject*
- TSIGContext_sign(s_TSIGContext* self, PyObject* args) {
- long qid = 0;
- const char* mac;
- Py_ssize_t mac_size;
- if (PyArg_ParseTuple(args, "ly#", &qid, &mac, &mac_size)) {
- if (qid < 0 || qid > 0xffff) {
- PyErr_SetString(PyExc_ValueError,
- "TSIGContext.sign: QID out of range");
- return (NULL);
- }
- try {
- ConstTSIGRecordPtr record = self->cppobj->sign(qid, mac, mac_size);
- return (createTSIGRecordObject(*record));
- } catch (const TSIGContextError& ex) {
- PyErr_SetString(po_TSIGContextError, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Unexpected failure in TSIG sign: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected failure in TSIG sign");
- }
- } else {
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to TSIGContext.sign");
- }
- return (NULL);
- }
- PyObject*
- TSIGContext_verify(s_TSIGContext* self, PyObject* args) {
- const char* data;
- Py_ssize_t data_len;
- s_TSIGRecord* py_record;
- PyObject* py_maybe_none;
- TSIGRecord* record;
- if (PyArg_ParseTuple(args, "O!y#", &tsigrecord_type, &py_record,
- &data, &data_len)) {
- record = py_record->cppobj;
- } else if (PyArg_ParseTuple(args, "Oy#", &py_maybe_none, &data,
- &data_len)) {
- record = NULL;
- } else {
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to TSIGContext.verify");
- return (NULL);
- }
- PyErr_Clear();
- try {
- const TSIGError error = self->cppobj->verify(record, data, data_len);
- return (createTSIGErrorObject(error));
- } catch (const TSIGContextError& ex) {
- PyErr_SetString(po_TSIGContextError, ex.what());
- } catch (const InvalidParameter& ex) {
- PyErr_SetString(po_InvalidParameter, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Unexpected failure in TSIG verify: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError, "Unexpected failure in TSIG verify");
- }
- return (NULL);
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyObject* po_TSIGContextError;
- PyTypeObject tsigcontext_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.TSIGContext",
- sizeof(s_TSIGContext),
- 0,
- reinterpret_cast<destructor>(TSIGContext_destroy),
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
-
-
-
-
-
- Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
- "The TSIGContext class objects is...(COMPLETE THIS)",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- TSIGContext_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- reinterpret_cast<initproc>(TSIGContext_init),
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- namespace internal {
- bool
- initModulePart_TSIGContext(PyObject* mod) {
-
-
-
- if (PyType_Ready(&tsigcontext_type) < 0) {
- return (false);
- }
- void* p = &tsigcontext_type;
- if (PyModule_AddObject(mod, "TSIGContext",
- static_cast<PyObject*>(p)) < 0) {
- return (false);
- }
- Py_INCREF(&tsigcontext_type);
- try {
-
- po_TSIGContextError = PyErr_NewException("pydnspp.TSIGContextError",
- po_IscException, NULL);
- PyObjectContainer(po_TSIGContextError).installToModule(
- mod, "TSIGContextError");
-
- installClassVariable(tsigcontext_type, "STATE_INIT",
- Py_BuildValue("I", TSIGContext::INIT));
- installClassVariable(tsigcontext_type, "STATE_SENT_REQUEST",
- Py_BuildValue("I", TSIGContext::SENT_REQUEST));
- installClassVariable(tsigcontext_type, "STATE_RECEIVED_REQUEST",
- Py_BuildValue("I", TSIGContext::RECEIVED_REQUEST));
- installClassVariable(tsigcontext_type, "STATE_SENT_RESPONSE",
- Py_BuildValue("I", TSIGContext::SENT_RESPONSE));
- installClassVariable(tsigcontext_type, "STATE_VERIFIED_RESPONSE",
- Py_BuildValue("I",
- TSIGContext::VERIFIED_RESPONSE));
- installClassVariable(tsigcontext_type, "DEFAULT_FUDGE",
- Py_BuildValue("H", TSIGContext::DEFAULT_FUDGE));
- } catch (const exception& ex) {
- const string ex_what =
- "Unexpected failure in TSIGContext initialization: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- return (false);
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected failure in TSIGContext initialization");
- return (false);
- }
- return (true);
- }
- }
- }
- }
- }
|