123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- #include <Python.h>
- #include <util/python/pycppwrapper_util.h>
- #include <dns/rrset.h>
- #include <dns/name.h>
- #include <dns/messagerenderer.h>
- #include "name_python.h"
- #include "pydnspp_common.h"
- #include "rrset_python.h"
- #include "rrclass_python.h"
- #include "rrtype_python.h"
- #include "rrttl_python.h"
- #include "rdata_python.h"
- #include "messagerenderer_python.h"
- using namespace std;
- using namespace isc::dns;
- using namespace isc::dns::python;
- using namespace isc::util;
- using namespace isc::util::python;
- namespace {
- class s_RRset : public PyObject {
- public:
- isc::dns::RRsetPtr cppobj;
- };
- int RRset_init(s_RRset* self, PyObject* args);
- void RRset_destroy(s_RRset* self);
- PyObject* RRset_getRdataCount(PyObject* self, PyObject* args);
- PyObject* RRset_getName(PyObject* self, PyObject* args);
- PyObject* RRset_getClass(PyObject* self, PyObject* args);
- PyObject* RRset_getType(PyObject* self, PyObject* args);
- PyObject* RRset_getTTL(PyObject* self, PyObject* args);
- PyObject* RRset_setName(PyObject* self, PyObject* args);
- PyObject* RRset_setTTL(PyObject* self, PyObject* args);
- PyObject* RRset_toText(PyObject* self, PyObject* args);
- PyObject* RRset_str(PyObject* self);
- PyObject* RRset_toWire(PyObject* self, PyObject* args);
- PyObject* RRset_addRdata(PyObject* self, PyObject* args);
- PyObject* RRset_getRdata(PyObject* po_self, PyObject* args);
- PyObject* RRset_removeRRsig(PyObject* self, PyObject* args);
- PyMethodDef RRset_methods[] = {
- { "get_rdata_count", RRset_getRdataCount, METH_NOARGS,
- "Returns the number of rdata fields." },
- { "get_name", RRset_getName, METH_NOARGS,
- "Returns the name of the RRset, as a Name object." },
- { "get_class", RRset_getClass, METH_NOARGS,
- "Returns the class of the RRset as an RRClass object." },
- { "get_type", RRset_getType, METH_NOARGS,
- "Returns the type of the RRset as an RRType object." },
- { "get_ttl", RRset_getTTL, METH_NOARGS,
- "Returns the TTL of the RRset as an RRTTL object." },
- { "set_name", RRset_setName, METH_VARARGS,
- "Sets the name of the RRset.\nTakes a Name object as an argument." },
- { "set_ttl", RRset_setTTL, METH_VARARGS,
- "Sets the TTL of the RRset.\nTakes an RRTTL object as an argument." },
- { "to_text", RRset_toText, METH_NOARGS,
- "Returns the text representation of the RRset as a string" },
- { "to_wire", RRset_toWire, METH_VARARGS,
- "Converts the RRset 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" },
- { "add_rdata", RRset_addRdata, METH_VARARGS,
- "Adds the rdata for one RR to the RRset.\nTakes an Rdata object as an argument" },
- { "get_rdata", RRset_getRdata, METH_NOARGS,
- "Returns a List containing all Rdata elements" },
- { "remove_rrsig", RRset_removeRRsig, METH_NOARGS,
- "Clears the list of RRsigs for this RRset" },
- { NULL, NULL, 0, NULL }
- };
- int
- RRset_init(s_RRset* self, PyObject* args) {
- PyObject* name;
- PyObject* rrclass;
- PyObject* rrtype;
- PyObject* rrttl;
- if (PyArg_ParseTuple(args, "O!O!O!O!", &name_type, &name,
- &rrclass_type, &rrclass,
- &rrtype_type, &rrtype,
- &rrttl_type, &rrttl
- )) {
- self->cppobj = RRsetPtr(new RRset(PyName_ToName(name),
- PyRRClass_ToRRClass(rrclass),
- PyRRType_ToRRType(rrtype),
- PyRRTTL_ToRRTTL(rrttl)));
- return (0);
- }
- self->cppobj = RRsetPtr();
- return (-1);
- }
- void
- RRset_destroy(s_RRset* self) {
-
-
- self->cppobj.reset();
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- RRset_getRdataCount(PyObject* self, PyObject*) {
- return (Py_BuildValue("I", static_cast<const s_RRset*>(self)->cppobj->
- getRdataCount()));
- }
- PyObject*
- RRset_getName(PyObject* self, PyObject*) {
- try {
- return (createNameObject(static_cast<const s_RRset*>(self)->cppobj->
- getName()));
- } catch (const exception& ex) {
- const string ex_what =
- "Unexpected failure getting rrset Name: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected failure getting rrset Name");
- }
- return (NULL);
- }
- PyObject*
- RRset_getClass(PyObject* self, PyObject*) {
- try {
- return (createRRClassObject(static_cast<const s_RRset*>(self)->cppobj->
- getClass()));
- } catch (const exception& ex) {
- const string ex_what =
- "Unexpected failure getting question RRClass: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected failure getting question RRClass");
- }
- return (NULL);
- }
- PyObject*
- RRset_getType(PyObject* self, PyObject*) {
- try {
- return (createRRTypeObject(static_cast<const s_RRset*>(self)->cppobj->
- getType()));
- } catch (const exception& ex) {
- const string ex_what =
- "Unexpected failure getting question RRType: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected failure getting question RRType");
- }
- return (NULL);
- }
- PyObject*
- RRset_getTTL(PyObject* self, PyObject*) {
- try {
- return (createRRTTLObject(static_cast<const s_RRset*>(self)->cppobj->
- getTTL()));
- } catch (const exception& ex) {
- const string ex_what =
- "Unexpected failure getting question TTL: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected failure getting question TTL");
- }
- return (NULL);
- }
- PyObject*
- RRset_setName(PyObject* self, PyObject* args) {
- PyObject* name;
- if (!PyArg_ParseTuple(args, "O!", &name_type, &name)) {
- return (NULL);
- }
- static_cast<s_RRset*>(self)->cppobj->setName(PyName_ToName(name));
- Py_RETURN_NONE;
- }
- PyObject*
- RRset_setTTL(PyObject* self, PyObject* args) {
- PyObject* rrttl;
- if (!PyArg_ParseTuple(args, "O!", &rrttl_type, &rrttl)) {
- return (NULL);
- }
- static_cast<s_RRset*>(self)->cppobj->setTTL(PyRRTTL_ToRRTTL(rrttl));
- Py_RETURN_NONE;
- }
- PyObject*
- RRset_toText(PyObject* self, PyObject*) {
- try {
- return (Py_BuildValue("s", static_cast<const s_RRset*>(self)->cppobj->
- toText().c_str()));
- } catch (const EmptyRRset& ers) {
- PyErr_SetString(po_EmptyRRset, ers.what());
- return (NULL);
- }
- }
- PyObject*
- RRset_str(PyObject* self) {
-
- return (PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- PyObject*
- RRset_toWire(PyObject* self_p, PyObject* args) {
- PyObject* bytes;
- PyObject* mr;
- const s_RRset* self(static_cast<const s_RRset*>(self_p));
- try {
- if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
- PyObject* bytes_o = bytes;
- OutputBuffer buffer(4096);
- self->cppobj->toWire(buffer);
- PyObject* n = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()), buffer.getLength());
- PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
-
-
- Py_DECREF(n);
- return (result);
- } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
- self->cppobj->toWire(PyMessageRenderer_ToMessageRenderer(mr));
-
-
- Py_RETURN_NONE;
- }
- } catch (const EmptyRRset& ers) {
- PyErr_Clear();
- PyErr_SetString(po_EmptyRRset, ers.what());
- return (NULL);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "toWire argument must be a sequence object or a MessageRenderer");
- return (NULL);
- }
- PyObject*
- RRset_addRdata(PyObject* self, PyObject* args) {
- PyObject* rdata;
- if (!PyArg_ParseTuple(args, "O!", &rdata_type, &rdata)) {
- return (NULL);
- }
- try {
- static_cast<s_RRset*>(self)->cppobj->addRdata(PyRdata_ToRdata(rdata));
- Py_RETURN_NONE;
- } catch (const std::bad_cast&) {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "Rdata type to add must match type of RRset");
- return (NULL);
- }
- }
- PyObject*
- RRset_getRdata(PyObject* po_self, PyObject*) {
- const s_RRset* const self = static_cast<s_RRset*>(po_self);
- try {
- PyObjectContainer list_container(PyList_New(0));
- for (RdataIteratorPtr it = self->cppobj->getRdataIterator();
- !it->isLast(); it->next()) {
- if (PyList_Append(list_container.get(),
- PyObjectContainer(
- createRdataObject(
- createRdata(self->cppobj->getType(),
- self->cppobj->getClass(),
- it->getCurrent()))).get())
- == -1) {
- isc_throw(PyCPPWrapperException, "PyList_Append failed, "
- "probably due to short memory");
- }
- }
- return (list_container.release());
- } catch (const exception& ex) {
- const string ex_what =
- "Unexpected failure getting rrset Rdata: " +
- string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected failure getting rrset Rdata");
- }
- return (NULL);
- }
- PyObject*
- RRset_removeRRsig(PyObject* self, PyObject*) {
- static_cast<s_RRset*>(self)->cppobj->removeRRsig();
- Py_RETURN_NONE;
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyObject* po_EmptyRRset;
- PyTypeObject rrset_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.RRset",
- sizeof(s_RRset),
- 0,
- (destructor)RRset_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- RRset_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The AbstractRRset class is an abstract base class that "
- "models a DNS RRset.\n\n"
- "An object of (a specific derived class of) AbstractRRset "
- "models an RRset as described in the DNS standard:\n"
- "A set of DNS resource records (RRs) of the same type and class. "
- "The standard requires the TTL of all RRs in an RRset be the same; "
- "this class follows that requirement.\n\n"
- "Note about duplicate RDATA: RFC2181 states that it's meaningless that an "
- "RRset contains two identical RRs and that name servers should suppress "
- "such duplicates.\n"
- "This class is not responsible for ensuring this requirement: For example, "
- "addRdata() method doesn't check if there's already RDATA identical "
- "to the one being added.\n"
- "This is because such checks can be expensive, and it's often easy to "
- "ensure the uniqueness requirement at the %data preparation phase "
- "(e.g. when loading a zone).",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- RRset_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)RRset_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- PyObject*
- createRRsetObject(const RRset& source) {
-
-
- RRsetPtr new_rrset = isc::dns::RRsetPtr(
- new isc::dns::RRset(source.getName(), source.getClass(),
- source.getType(), source.getTTL()));
- isc::dns::RdataIteratorPtr rdata_it(source.getRdataIterator());
- for (rdata_it->first(); !rdata_it->isLast(); rdata_it->next()) {
- new_rrset->addRdata(rdata_it->getCurrent());
- }
- isc::dns::RRsetPtr sigs = source.getRRsig();
- if (sigs) {
- new_rrset->addRRsig(sigs);
- }
- s_RRset* py_rrset =
- static_cast<s_RRset*>(rrset_type.tp_alloc(&rrset_type, 0));
- if (py_rrset == NULL) {
- isc_throw(PyCPPWrapperException, "Unexpected NULL C++ object, "
- "probably due to short memory");
- }
- py_rrset->cppobj = new_rrset;
- return (py_rrset);
- }
- bool
- PyRRset_Check(PyObject* obj) {
- if (obj == NULL) {
- isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
- }
- return (PyObject_TypeCheck(obj, &rrset_type));
- }
- RRset&
- PyRRset_ToRRset(PyObject* rrset_obj) {
- s_RRset* rrset = static_cast<s_RRset*>(rrset_obj);
- return (*rrset->cppobj);
- }
- RRsetPtr
- PyRRset_ToRRsetPtr(PyObject* rrset_obj) {
- if (rrset_obj == NULL) {
- isc_throw(PyCPPWrapperException,
- "obj argument NULL in RRset PyObject conversion");
- }
- s_RRset* rrset = static_cast<s_RRset*>(rrset_obj);
- return (rrset->cppobj);
- }
- }
- }
- }
|