123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- #include <Python.h>
- #include <stdexcept>
- #include <util/python/pycppwrapper_util.h>
- #include <dns/name.h>
- #include <dns/tsigkey.h>
- #include <dns/rdata.h>
- #include "pydnspp_common.h"
- #include "name_python.h"
- #include "tsigkey_python.h"
- using namespace std;
- using namespace isc::util::python;
- using namespace isc::dns;
- using namespace isc::dns::python;
- namespace {
- class s_TSIGKey : public PyObject {
- public:
- s_TSIGKey() : cppobj(NULL) {};
- TSIGKey* cppobj;
- };
- int TSIGKey_init(s_TSIGKey* self, PyObject* args);
- void TSIGKey_destroy(s_TSIGKey* self);
- PyObject* TSIGKey_getKeyName(const s_TSIGKey* self);
- PyObject* TSIGKey_getAlgorithmName(const s_TSIGKey* self);
- PyObject* TSIGKey_getSecret(const s_TSIGKey* self);
- PyObject* TSIGKey_toText(const s_TSIGKey* self);
- PyMethodDef TSIGKey_methods[] = {
- { "get_key_name",
- reinterpret_cast<PyCFunction>(TSIGKey_getKeyName), METH_NOARGS,
- "Return the key name." },
- { "get_algorithm_name",
- reinterpret_cast<PyCFunction>(TSIGKey_getAlgorithmName), METH_NOARGS,
- "Return the algorithm name." },
- { "get_secret",
- reinterpret_cast<PyCFunction>(TSIGKey_getSecret), METH_NOARGS,
- "Return the value of the TSIG secret." },
- { "to_text", reinterpret_cast<PyCFunction>(TSIGKey_toText), METH_NOARGS,
- "Returns the string representation (name:secret:algorithm)" },
- { NULL, NULL, 0, NULL }
- };
- int
- TSIGKey_init(s_TSIGKey* self, PyObject* args) {
- try {
- const char* str;
- if (PyArg_ParseTuple(args, "s", &str)) {
- self->cppobj = new TSIGKey(str);
- return (0);
- }
- PyErr_Clear();
- const PyObject* key_name;
- const PyObject* algorithm_name;
- PyObject* bytes_obj;
- const char* secret;
- Py_ssize_t secret_len;
- if (PyArg_ParseTuple(args, "O!O!O", &name_type, &key_name,
- &name_type, &algorithm_name, &bytes_obj) &&
- PyObject_AsCharBuffer(bytes_obj, &secret, &secret_len) == 0) {
- if (secret_len == 0) {
- secret = NULL;
- }
- self->cppobj = new TSIGKey(PyName_ToName(key_name),
- PyName_ToName(algorithm_name),
- secret, secret_len);
- return (0);
- }
- } catch (const isc::InvalidParameter& ex) {
- PyErr_SetString(po_InvalidParameter, ex.what());
- return (-1);
- } catch (...) {
- PyErr_SetString(po_IscException, "Unexpected exception");
- return (-1);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to TSIGKey constructor");
- return (-1);
- }
- void
- TSIGKey_destroy(s_TSIGKey* const self) {
- delete self->cppobj;
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- TSIGKey_getKeyName(const s_TSIGKey* const self) {
- try {
- return (createNameObject(self->cppobj->getKeyName()));
- } catch (const exception& ex) {
- const string ex_what =
- "Failed to get key name of TSIGKey: " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
- "getting key name of TSIGKey");
- }
- return (NULL);
- }
- PyObject*
- TSIGKey_getAlgorithmName(const s_TSIGKey* const self) {
- try {
- return (createNameObject(self->cppobj->getAlgorithmName()));
- } catch (const exception& ex) {
- const string ex_what =
- "Failed to get algorithm name of TSIGKey: " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
- "getting algorithm name of TSIGKey");
- }
- return (NULL);
- }
- PyObject*
- TSIGKey_getSecret(const s_TSIGKey* const self) {
- return (Py_BuildValue("y#", self->cppobj->getSecret(),
- self->cppobj->getSecretLength()));
- }
- PyObject*
- TSIGKey_toText(const s_TSIGKey* self) {
- return (Py_BuildValue("s", self->cppobj->toText().c_str()));
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyTypeObject tsigkey_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.TSIGKey",
- sizeof(s_TSIGKey),
- 0,
- (destructor)TSIGKey_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The TSIGKey class holds a TSIG key along with some related attributes as "
- "defined in RFC2845.",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- TSIGKey_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)TSIGKey_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- bool
- PyTSIGKey_Check(PyObject* obj) {
- if (obj == NULL) {
- isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
- }
- return (PyObject_TypeCheck(obj, &tsigkey_type));
- }
- const TSIGKey&
- PyTSIGKey_ToTSIGKey(const PyObject* tsigkey_obj) {
- const s_TSIGKey* tsigkey = static_cast<const s_TSIGKey*>(tsigkey_obj);
- return (*tsigkey->cppobj);
- }
- }
- }
- }
- namespace {
- class s_TSIGKeyRing : public PyObject {
- public:
- s_TSIGKeyRing() : cppobj(NULL) {};
- TSIGKeyRing* cppobj;
- };
- int TSIGKeyRing_init(s_TSIGKeyRing* self, PyObject* args);
- void TSIGKeyRing_destroy(s_TSIGKeyRing* self);
- PyObject* TSIGKeyRing_size(const s_TSIGKeyRing* self);
- PyObject* TSIGKeyRing_add(const s_TSIGKeyRing* self, PyObject* args);
- PyObject* TSIGKeyRing_remove(const s_TSIGKeyRing* self, PyObject* args);
- PyObject* TSIGKeyRing_find(const s_TSIGKeyRing* self, PyObject* args);
- PyMethodDef TSIGKeyRing_methods[] = {
- { "size", reinterpret_cast<PyCFunction>(TSIGKeyRing_size), METH_NOARGS,
- "Return the number of keys stored in the TSIGKeyRing." },
- { "add", reinterpret_cast<PyCFunction>(TSIGKeyRing_add), METH_VARARGS,
- "Add a TSIGKey to the TSIGKeyRing." },
- { "remove", reinterpret_cast<PyCFunction>(TSIGKeyRing_remove),
- METH_VARARGS,
- "Remove a TSIGKey for the given name from the TSIGKeyRing." },
- { "find", reinterpret_cast<PyCFunction>(TSIGKeyRing_find), METH_VARARGS,
- "Find a TSIGKey for the given name in the TSIGKeyRing. "
- "It returns a tuple of (result_code, key)." },
- { NULL, NULL, 0, NULL }
- };
- int
- TSIGKeyRing_init(s_TSIGKeyRing* self, PyObject* args) {
- if (!PyArg_ParseTuple(args, "")) {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to TSIGKeyRing constructor");
- return (-1);
- }
- self->cppobj = new(nothrow) TSIGKeyRing();
- if (self->cppobj == NULL) {
- PyErr_SetString(po_IscException, "Allocating TSIGKeyRing failed");
- return (-1);
- }
- return (0);
- }
- void
- TSIGKeyRing_destroy(s_TSIGKeyRing* self) {
- delete self->cppobj;
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- TSIGKeyRing_size(const s_TSIGKeyRing* const self) {
- return (Py_BuildValue("I", self->cppobj->size()));
- }
- PyObject*
- TSIGKeyRing_add(const s_TSIGKeyRing* const self, PyObject* args) {
- s_TSIGKey* tsigkey;
- if (PyArg_ParseTuple(args, "O!", &tsigkey_type, &tsigkey)) {
- try {
- const TSIGKeyRing::Result result =
- self->cppobj->add(*tsigkey->cppobj);
- return (Py_BuildValue("I", result));
- } catch (...) {
- PyErr_SetString(po_IscException, "Unexpected exception");
- return (NULL);
- }
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError, "Invalid arguments to TSIGKeyRing.add");
- return (NULL);
- }
- PyObject*
- TSIGKeyRing_remove(const s_TSIGKeyRing* self, PyObject* args) {
- PyObject* key_name;
- if (PyArg_ParseTuple(args, "O!", &name_type, &key_name)) {
- const TSIGKeyRing::Result result =
- self->cppobj->remove(PyName_ToName(key_name));
- return (Py_BuildValue("I", result));
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError, "Invalid arguments to TSIGKeyRing.add");
- return (NULL);
- }
- PyObject*
- TSIGKeyRing_find(const s_TSIGKeyRing* self, PyObject* args) {
- PyObject* key_name;
- PyObject* algorithm_name;
- if (PyArg_ParseTuple(args, "O!O!", &name_type, &key_name,
- &name_type, &algorithm_name)) {
- const TSIGKeyRing::FindResult result =
- self->cppobj->find(PyName_ToName(key_name),
- PyName_ToName(algorithm_name));
- if (result.key != NULL) {
- s_TSIGKey* key = PyObject_New(s_TSIGKey, &tsigkey_type);
- if (key == NULL) {
- return (NULL);
- }
- key->cppobj = new(nothrow) TSIGKey(*result.key);
- if (key->cppobj == NULL) {
- Py_DECREF(key);
- PyErr_SetString(po_IscException,
- "Allocating TSIGKey object failed");
- return (NULL);
- }
- return (Py_BuildValue("IN", result.code, key));
- } else {
- return (Py_BuildValue("Is", result.code, NULL));
- }
- }
- return (NULL);
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyTypeObject tsigkeyring_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.TSIGKeyRing",
- sizeof(s_TSIGKeyRing),
- 0,
- (destructor)TSIGKeyRing_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "A simple repository of a set of TSIGKey objects.",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- TSIGKeyRing_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)TSIGKeyRing_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- bool
- PyTSIGKeyRing_Check(PyObject* obj) {
- if (obj == NULL) {
- isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
- }
- return (PyObject_TypeCheck(obj, &tsigkeyring_type));
- }
- const TSIGKeyRing&
- PyTSIGKeyRing_ToTSIGKeyRing(const PyObject* tsigkeyring_obj) {
- if (tsigkeyring_obj == NULL) {
- isc_throw(PyCPPWrapperException,
- "obj argument NULL in TSIGKeyRing PyObject conversion");
- }
- const s_TSIGKeyRing* tsigkeyring =
- static_cast<const s_TSIGKeyRing*>(tsigkeyring_obj);
- return (*tsigkeyring->cppobj);
- }
- }
- }
- }
|