123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848 |
- #define PY_SSIZE_T_CLEAN
- #include <Python.h>
- #include <exceptions/exceptions.h>
- #include <util/python/pycppwrapper_util.h>
- #include <dns/message.h>
- #include <dns/rcode.h>
- #include <dns/tsig.h>
- #include <dns/exceptions.h>
- #include <dns/messagerenderer.h>
- #include "name_python.h"
- #include "question_python.h"
- #include "edns_python.h"
- #include "rcode_python.h"
- #include "opcode_python.h"
- #include "rrset_python.h"
- #include "message_python.h"
- #include "messagerenderer_python.h"
- #include "tsig_python.h"
- #include "tsigrecord_python.h"
- #include "pydnspp_common.h"
- using namespace std;
- using namespace isc::dns;
- using namespace isc::dns::python;
- using namespace isc::util;
- using namespace isc::util::python;
- #include "message_python_inc.cc"
- namespace {
- class s_Message : public PyObject {
- public:
- isc::dns::Message* cppobj;
- };
- int Message_init(s_Message* self, PyObject* args);
- void Message_destroy(s_Message* self);
- PyObject* Message_getHeaderFlag(s_Message* self, PyObject* args);
- PyObject* Message_setHeaderFlag(s_Message* self, PyObject* args);
- PyObject* Message_getQid(s_Message* self);
- PyObject* Message_setQid(s_Message* self, PyObject* args);
- PyObject* Message_getRcode(s_Message* self);
- PyObject* Message_setRcode(s_Message* self, PyObject* args);
- PyObject* Message_getOpcode(s_Message* self);
- PyObject* Message_setOpcode(s_Message* self, PyObject* args);
- PyObject* Message_getEDNS(s_Message* self);
- PyObject* Message_setEDNS(s_Message* self, PyObject* args);
- PyObject* Message_getTSIGRecord(s_Message* self);
- PyObject* Message_getRRCount(s_Message* self, PyObject* args);
- PyObject* Message_getQuestion(PyObject* self, PyObject*);
- PyObject* Message_getSection(PyObject* self, PyObject* args);
- PyObject* Message_addQuestion(s_Message* self, PyObject* args);
- PyObject* Message_addRRset(s_Message* self, PyObject* args);
- PyObject* Message_clear(s_Message* self, PyObject* args);
- PyObject* Message_clearSection(PyObject* pyself, PyObject* args);
- PyObject* Message_makeResponse(s_Message* self);
- PyObject* Message_toText(s_Message* self);
- PyObject* Message_str(PyObject* self);
- PyObject* Message_toWire(s_Message* self, PyObject* args);
- PyObject* Message_fromWire(PyObject* pyself, PyObject* args);
- PyMethodDef Message_methods[] = {
- { "get_header_flag", reinterpret_cast<PyCFunction>(Message_getHeaderFlag),
- METH_VARARGS,
- "Return whether the specified header flag bit is set in the "
- "header section. Takes a MessageFlag object as the only argument." },
- { "set_header_flag",
- reinterpret_cast<PyCFunction>(Message_setHeaderFlag), METH_VARARGS,
- "Sets the specified header flag bit to 1. The message must be in "
- "RENDER mode. If not, an InvalidMessageOperation is raised. "
- "Takes a MessageFlag object as the only argument." },
- { "get_qid", reinterpret_cast<PyCFunction>(Message_getQid), METH_NOARGS,
- "Returns the query id" },
- { "set_qid", reinterpret_cast<PyCFunction>(Message_setQid), METH_VARARGS,
- "Sets the query id. If the message is not in RENDER mode, an "
- "InvalidMessageOperation is raised.\n"
- "The argument must be an integer" },
- { "get_rcode", reinterpret_cast<PyCFunction>(Message_getRcode), METH_NOARGS,
- "Returns the message Response code (an Rcode object)" },
- { "set_rcode", reinterpret_cast<PyCFunction>(Message_setRcode), METH_VARARGS,
- "Sets the message Response code (an Rcode object).\n"
- "If the message is not in RENDER mode, an "
- "InvalidMessageOperation is raised."},
- { "get_opcode", reinterpret_cast<PyCFunction>(Message_getOpcode), METH_NOARGS,
- "Returns the message opcode (an Opcode object)" },
- { "set_opcode", reinterpret_cast<PyCFunction>(Message_setOpcode), METH_VARARGS,
- "Sets the message opcode (an Opcode object).\n"
- "If the message is not in RENDER mode, an "
- "InvalidMessageOperation is raised."},
- { "get_edns", reinterpret_cast<PyCFunction>(Message_getEDNS), METH_NOARGS,
- "Return, if any, the EDNS associated with the message."
- },
- { "set_edns", reinterpret_cast<PyCFunction>(Message_setEDNS), METH_VARARGS,
- "Set EDNS for the message."
- },
- { "get_tsig_record",
- reinterpret_cast<PyCFunction>(Message_getTSIGRecord), METH_NOARGS,
- "Return, if any, the TSIG record contained in the received message. "
- "If no TSIG RR is set in the message, None will be returned."
- },
- { "get_rr_count", reinterpret_cast<PyCFunction>(Message_getRRCount), METH_VARARGS,
- "Returns the number of RRs contained in the given section." },
- { "get_question", Message_getQuestion, METH_NOARGS,
- "Returns a list of all Question objects in the message "
- "(should be either 0 or 1)" },
- { "get_section", Message_getSection, METH_VARARGS,
- "Returns a list of all RRset objects in the given section of the message\n"
- "The argument must be of type Section" },
- { "add_question", reinterpret_cast<PyCFunction>(Message_addQuestion), METH_VARARGS,
- "Add a Question to the message."
- "If the message is not in RENDER mode, an "
- "InvalidMessageOperation is raised."},
- { "add_rrset", reinterpret_cast<PyCFunction>(Message_addRRset), METH_VARARGS,
- "Add an RRset to the given section of the message.\n"
- "The first argument is of type Section\n"
- "The second is of type RRset"},
- { "clear", reinterpret_cast<PyCFunction>(Message_clear), METH_VARARGS,
- "Clears the message content (if any) and reinitialize the "
- "message in the given mode\n"
- "The argument must be either Message.PARSE or Message.RENDER"},
- { "clear_section", Message_clearSection, METH_VARARGS,
- Message_clearSection_doc },
- { "make_response", reinterpret_cast<PyCFunction>(Message_makeResponse), METH_NOARGS,
- "Prepare for making a response from a request.\n"
- "This will clear the DNS header except those fields that should be kept "
- "for the response, and clear answer and the following sections. "
- "See also dns_message_reply() of BIND9."},
- { "to_text", reinterpret_cast<PyCFunction>(Message_toText), METH_NOARGS,
- "Returns the string representation of the message" },
- { "to_wire", reinterpret_cast<PyCFunction>(Message_toWire), METH_VARARGS,
- "Render the message in wire format.\n"
- "The argument must be a MessageRenderer.\n"
- "If the given message is not in RENDER mode, an "
- "InvalidMessageOperation is raised.\n"
- },
- { "from_wire", Message_fromWire, METH_VARARGS, Message_fromWire_doc },
- { NULL, NULL, 0, NULL }
- };
- int
- Message_init(s_Message* self, PyObject* args) {
- int i;
- if (PyArg_ParseTuple(args, "i", &i)) {
- PyErr_Clear();
- if (i == Message::PARSE) {
- self->cppobj = new Message(Message::PARSE);
- return (0);
- } else if (i == Message::RENDER) {
- self->cppobj = new Message(Message::RENDER);
- return (0);
- } else {
- PyErr_SetString(PyExc_TypeError, "Message mode must be Message.PARSE or Message.RENDER");
- return (-1);
- }
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in constructor argument");
- return (-1);
- }
- void
- Message_destroy(s_Message* self) {
- delete self->cppobj;
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- Message_getHeaderFlag(s_Message* self, PyObject* args) {
- unsigned int messageflag;
- if (!PyArg_ParseTuple(args, "I", &messageflag)) {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in get_header_flag argument");
- return (NULL);
- }
- try {
- if (self->cppobj->getHeaderFlag(
- static_cast<Message::HeaderFlag>(messageflag))) {
- Py_RETURN_TRUE;
- } else {
- Py_RETURN_FALSE;
- }
- } catch (const isc::InvalidParameter& ip) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidParameter, ip.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_header_flag(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.get_header_flag()");
- }
- return (NULL);
- }
- PyObject*
- Message_setHeaderFlag(s_Message* self, PyObject* args) {
- long messageflag;
- PyObject *on = Py_True;
- if (!PyArg_ParseTuple(args, "l|O!", &messageflag, &PyBool_Type, &on)) {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in set_header_flag argument");
- return (NULL);
- }
- if (messageflag < 0 || messageflag > 0xffff) {
- PyErr_SetString(PyExc_ValueError, "Message header flag out of range");
- return (NULL);
- }
- try {
- self->cppobj->setHeaderFlag(
- static_cast<Message::HeaderFlag>(messageflag), on == Py_True);
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const isc::InvalidParameter& ip) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidParameter, ip.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.set_header_flag(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.set_header_flag()");
- }
- return (NULL);
- }
- PyObject*
- Message_getQid(s_Message* self) {
- return (Py_BuildValue("I", self->cppobj->getQid()));
- }
- PyObject*
- Message_setQid(s_Message* self, PyObject* args) {
- long id;
- if (!PyArg_ParseTuple(args, "l", &id)) {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in set_qid argument");
- return (NULL);
- }
- if (id < 0 || id > 0xffff) {
- PyErr_SetString(PyExc_ValueError,
- "Message id out of range");
- return (NULL);
- }
- try {
- self->cppobj->setQid(id);
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_qid(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.set_qid()");
- }
- return (NULL);
- }
- PyObject*
- Message_getRcode(s_Message* self) {
- try {
- return (createRcodeObject(self->cppobj->getRcode()));
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_rcode(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.get_rcode()");
- }
- return (NULL);
- }
- PyObject*
- Message_setRcode(s_Message* self, PyObject* args) {
- PyObject* rcode;
- if (!PyArg_ParseTuple(args, "O!", &rcode_type, &rcode)) {
- return (NULL);
- }
- try {
- self->cppobj->setRcode(PyRcode_ToRcode(rcode));
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.set_rcode(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.set_rcode()");
- }
- return (NULL);
- }
- PyObject*
- Message_getOpcode(s_Message* self) {
- try {
- return (createOpcodeObject(self->cppobj->getOpcode()));
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_opcode(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.get_opcode()");
- }
- return (NULL);
- }
- PyObject*
- Message_setOpcode(s_Message* self, PyObject* args) {
- PyObject* opcode;
- if (!PyArg_ParseTuple(args, "O!", &opcode_type, &opcode)) {
- return (NULL);
- }
- try {
- self->cppobj->setOpcode(PyOpcode_ToOpcode(opcode));
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.set_opcode(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.set_opcode()");
- }
- return (NULL);
- }
- PyObject*
- Message_getEDNS(s_Message* self) {
- ConstEDNSPtr src = self->cppobj->getEDNS();
- if (!src) {
- Py_RETURN_NONE;
- }
- try {
- return (createEDNSObject(*src));
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_edns(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError,
- "Unexpected exception in Message.get_edns()");
- }
- return (NULL);
- }
- PyObject*
- Message_setEDNS(s_Message* self, PyObject* args) {
- PyObject* edns;
- if (!PyArg_ParseTuple(args, "O!", &edns_type, &edns)) {
- return (NULL);
- }
- try {
- self->cppobj->setEDNS(EDNSPtr(new EDNS(PyEDNS_ToEDNS(edns))));
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.set_edns(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.set_edns()");
- }
- return (NULL);
- }
- PyObject*
- Message_getTSIGRecord(s_Message* self) {
- try {
- const TSIGRecord* tsig_record = self->cppobj->getTSIGRecord();
- if (tsig_record == NULL) {
- Py_RETURN_NONE;
- } else {
- return (createTSIGRecordObject(*tsig_record));
- }
- } catch (const InvalidMessageOperation& ex) {
- PyErr_SetString(po_InvalidMessageOperation, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_tsig_record(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.get_tsig_record()");
- }
- return (NULL);
- }
- PyObject*
- Message_getRRCount(s_Message* self, PyObject* args) {
- unsigned int section;
- if (!PyArg_ParseTuple(args, "I", §ion)) {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in get_rr_count argument");
- return (NULL);
- }
- try {
- return (Py_BuildValue("I", self->cppobj->getRRCount(
- static_cast<Message::Section>(section))));
- } catch (const isc::OutOfRange& ex) {
- PyErr_SetString(PyExc_OverflowError, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_rr_count(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.get_rr_count()");
- }
- return (NULL);
- }
- template <typename ItemType, typename CreatorParamType>
- class SectionInserter {
- typedef PyObject* (*creator_t)(const CreatorParamType&);
- public:
- SectionInserter(PyObject* pylist, creator_t creator) :
- pylist_(pylist), creator_(creator)
- {}
- void operator()(ItemType item) {
- if (PyList_Append(pylist_, PyObjectContainer(creator_(*item)).get())
- == -1) {
- isc_throw(PyCPPWrapperException, "PyList_Append failed, "
- "probably due to short memory");
- }
- }
- private:
- PyObject* pylist_;
- creator_t creator_;
- };
- typedef SectionInserter<ConstQuestionPtr, Question> QuestionInserter;
- typedef SectionInserter<ConstRRsetPtr, AbstractRRset> RRsetInserter;
- PyObject*
- Message_getQuestion(PyObject* po_self, PyObject*) {
- const s_Message* const self = static_cast<s_Message*>(po_self);
- try {
- PyObjectContainer list_container(PyList_New(0));
- for_each(self->cppobj->beginQuestion(),
- self->cppobj->endQuestion(),
- QuestionInserter(list_container.get(), createQuestionObject));
- return (list_container.release());
- } catch (const InvalidMessageSection& ex) {
- PyErr_SetString(po_InvalidMessageSection, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_question(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.get_question()");
- }
- return (NULL);
- }
- PyObject*
- Message_getSection(PyObject* po_self, PyObject* args) {
- const s_Message* const self = static_cast<s_Message*>(po_self);
- unsigned int section;
- if (!PyArg_ParseTuple(args, "I", §ion)) {
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in get_section argument");
- return (NULL);
- }
- try {
- PyObjectContainer list_container(PyList_New(0));
- const Message::Section msgsection =
- static_cast<Message::Section>(section);
- for_each(self->cppobj->beginSection(msgsection),
- self->cppobj->endSection(msgsection),
- RRsetInserter(list_container.get(), createRRsetObject));
- return (list_container.release());
- } catch (const isc::OutOfRange& ex) {
- PyErr_SetString(PyExc_OverflowError, ex.what());
- } catch (const InvalidMessageSection& ex) {
- PyErr_SetString(po_InvalidMessageSection, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.get_section(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.get_section()");
- }
- return (NULL);
- }
- PyObject*
- Message_addQuestion(s_Message* self, PyObject* args) {
- PyObject* question;
- if (!PyArg_ParseTuple(args, "O!", &question_type, &question)) {
- return (NULL);
- }
- try {
- self->cppobj->addQuestion(PyQuestion_ToQuestion(question));
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.add_question(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.add_question()");
- }
- return (NULL);
- }
- PyObject*
- Message_addRRset(s_Message* self, PyObject* args) {
- int section;
- PyObject* rrset;
- if (!PyArg_ParseTuple(args, "iO!", §ion, &rrset_type, &rrset)) {
- return (NULL);
- }
- try {
- self->cppobj->addRRset(static_cast<Message::Section>(section),
- PyRRset_ToRRsetPtr(rrset));
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const isc::OutOfRange& ex) {
- PyErr_SetString(PyExc_OverflowError, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.add_rrset(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.add_rrset()");
- }
- return (NULL);
- }
- PyObject*
- Message_clear(s_Message* self, PyObject* args) {
- int i;
- try {
- if (PyArg_ParseTuple(args, "i", &i)) {
- PyErr_Clear();
- if (i == Message::PARSE) {
- self->cppobj->clear(Message::PARSE);
- Py_RETURN_NONE;
- } else if (i == Message::RENDER) {
- self->cppobj->clear(Message::RENDER);
- Py_RETURN_NONE;
- } else {
- PyErr_SetString(PyExc_TypeError,
- "Message mode must be Message.PARSE or Message.RENDER");
- return (NULL);
- }
- }
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.clear(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.clear()");
- }
- return (NULL);
- }
- PyObject*
- Message_clearSection(PyObject* pyself, PyObject* args) {
- s_Message* const self = static_cast<s_Message*>(pyself);
- int section;
- if (!PyArg_ParseTuple(args, "i", §ion)) {
- return (NULL);
- }
- try {
- self->cppobj->clearSection(static_cast<Message::Section>(section));
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const isc::OutOfRange& ex) {
- PyErr_SetString(PyExc_OverflowError, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.clear_section(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.clear_section()");
- }
- return (NULL);
- }
- PyObject*
- Message_makeResponse(s_Message* self) {
- try {
- self->cppobj->makeResponse();
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.make_response(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.make_response()");
- }
- return (NULL);
- }
- PyObject*
- Message_toText(s_Message* self) {
-
- try {
- return (Py_BuildValue("s", self->cppobj->toText().c_str()));
- } catch (const InvalidMessageOperation& imo) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.to_text(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.to_text()");
- }
- return (NULL);
- }
- PyObject*
- Message_str(PyObject* self) {
-
- return (PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- PyObject*
- Message_toWire(s_Message* self, PyObject* args) {
- PyObject* mr;
- PyObject* tsig_ctx = NULL;
- if (PyArg_ParseTuple(args, "O!|O", &messagerenderer_type, &mr,
- &tsig_ctx)) {
- try {
- if ((tsig_ctx == NULL) || (tsig_ctx == Py_None)) {
- self->cppobj->toWire(PyMessageRenderer_ToMessageRenderer(mr));
- } else {
- self->cppobj->toWire(PyMessageRenderer_ToMessageRenderer(mr),
- PyTSIGContext_ToTSIGContext(tsig_ctx));
- }
-
-
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const TSIGContextError& ex) {
-
-
- PyErr_SetString(po_TSIGContextError, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.to_wire(): " + string(ex.what());
- PyErr_SetString(po_TSIGContextError, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.to_wire()");
- }
- return (NULL);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "Bad to_wire() arguments were passed");
- return (NULL);
- }
- PyObject*
- Message_fromWire(PyObject* pyself, PyObject* args) {
- s_Message* const self = static_cast<s_Message*>(pyself);
- const char* b;
- Py_ssize_t len;
- unsigned int options = Message::PARSE_DEFAULT;
-
- if (PyArg_ParseTuple(args, "y#", &b, &len) ||
- PyArg_ParseTuple(args, "y#I", &b, &len, &options)) {
-
-
- PyErr_Clear();
- InputBuffer inbuf(b, len);
- try {
- self->cppobj->fromWire(
- inbuf, static_cast<Message::ParseOptions>(options));
- Py_RETURN_NONE;
- } catch (const InvalidMessageOperation& imo) {
- PyErr_SetString(po_InvalidMessageOperation, imo.what());
- } catch (const DNSMessageFORMERR& dmfe) {
- PyErr_SetString(po_DNSMessageFORMERR, dmfe.what());
- } catch (const DNSMessageBADVERS& dmfe) {
- PyErr_SetString(po_DNSMessageBADVERS, dmfe.what());
- } catch (const MessageTooShort& mts) {
- PyErr_SetString(po_MessageTooShort, mts.what());
- } catch (const InvalidBufferPosition& ex) {
- PyErr_SetString(po_DNSMessageFORMERR, ex.what());
- } catch (const exception& ex) {
- const string ex_what = "Error in Message.from_wire(): " + string(ex.what());
- PyErr_SetString(po_IscException, ex_what.c_str());
- } catch (...) {
- PyErr_SetString(po_IscException,
- "Unexpected exception in Message.from_wire()");
- }
- return (NULL);
- }
- PyErr_SetString(PyExc_TypeError,
- "from_wire() arguments must be a byte object and "
- "(optional) parse options");
- return (NULL);
- }
- }
- namespace isc {
- namespace dns {
- namespace python {
- PyObject* po_MessageTooShort;
- PyObject* po_InvalidMessageSection;
- PyObject* po_InvalidMessageOperation;
- PyObject* po_InvalidMessageUDPSize;
- PyTypeObject message_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.Message",
- sizeof(s_Message),
- 0,
- (destructor)Message_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Message_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The Message class encapsulates a standard DNS message.",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- Message_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)Message_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- }
- }
- }
|