123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- #include <cassert>
- #include <dns/edns.h>
- using namespace isc::dns;
- using namespace isc::dns::rdata;
- namespace {
- class s_EDNS : public PyObject {
- public:
- EDNS* edns;
- };
- int EDNS_init(s_EDNS* self, PyObject* args);
- void EDNS_destroy(s_EDNS* self);
- PyObject* EDNS_toText(const s_EDNS* self);
- PyObject* EDNS_str(PyObject* self);
- PyObject* EDNS_toWire(const s_EDNS* self, PyObject* args);
- PyObject* EDNS_getVersion(const s_EDNS* self);
- PyObject* EDNS_isDNSSECSupported(const s_EDNS* self);
- PyObject* EDNS_setDNSSECSupported(s_EDNS* self, PyObject* args);
- PyObject* EDNS_getUDPSize(const s_EDNS* self);
- PyObject* EDNS_setUDPSize(s_EDNS* self, PyObject* args);
- PyObject* EDNS_createFromRR(const s_EDNS* null_self, PyObject* args);
- PyMethodDef EDNS_methods[] = {
- { "to_text", reinterpret_cast<PyCFunction>(EDNS_toText), METH_NOARGS,
- "Returns the string representation" },
- { "to_wire", reinterpret_cast<PyCFunction>(EDNS_toWire), METH_VARARGS,
- "Converts the EDNS 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" },
- { "get_version",
- reinterpret_cast<PyCFunction>(EDNS_getVersion), METH_NOARGS,
- "Returns the version of EDNS." },
- { "is_dnssec_supported",
- reinterpret_cast<PyCFunction>(EDNS_isDNSSECSupported), METH_NOARGS,
- "Returns True if the message sender indicates DNSSEC is supported. "
- "If EDNS is included, this corresponds to the value of the DO bit. "
- "Otherwise, DNSSEC is considered not supported." },
- { "set_dnssec_supported",
- reinterpret_cast<PyCFunction>(EDNS_setDNSSECSupported), METH_VARARGS,
- "Specify whether DNSSEC is supported in the message." },
- { "get_udp_size",
- reinterpret_cast<PyCFunction>(EDNS_getUDPSize), METH_NOARGS,
- "Return the maximum buffer size of UDP messages for the sender "
- "of the message." },
- { "set_udp_size",
- reinterpret_cast<PyCFunction>(EDNS_setUDPSize), METH_VARARGS,
- "Specify the maximum buffer size of UDP messages that use this EDNS." },
- { "create_from_rr",
- reinterpret_cast<PyCFunction>(EDNS_createFromRR),
- METH_VARARGS | METH_STATIC,
- "Create a new EDNS object from a set of RR parameters, also providing "
- "the extended RCODE value." },
- { NULL, NULL, 0, NULL }
- };
- PyTypeObject edns_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "libdns_python.EDNS",
- sizeof(s_EDNS),
- 0,
- (destructor)EDNS_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- EDNS_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The EDNS class encapsulates DNS extensions "
- "provided by the EDNSx protocol.",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- EDNS_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)EDNS_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- EDNS*
- createFromRR(const Name& name, const RRClass& rrclass, const RRType& rrtype,
- const RRTTL& rrttl, const Rdata& rdata, uint8_t& extended_rcode)
- {
- try {
- return (createEDNSFromRR(name, rrclass, rrtype, rrttl, rdata,
- extended_rcode));
- } catch (const isc::InvalidParameter& ex) {
- PyErr_SetString(po_InvalidParameter, ex.what());
- } catch (const DNSMessageFORMERR& ex) {
- PyErr_SetString(po_DNSMessageFORMERR, ex.what());
- } catch (const DNSMessageBADVERS& ex) {
- PyErr_SetString(po_DNSMessageBADVERS, ex.what());
- } catch (...) {
- PyErr_SetString(po_IscException, "Unexpected exception");
- }
- return (NULL);
- }
- int
- EDNS_init(s_EDNS* self, PyObject* args) {
- uint8_t version = EDNS::SUPPORTED_VERSION;
- const s_Name* name;
- const s_RRClass* rrclass;
- const s_RRType* rrtype;
- const s_RRTTL* rrttl;
- const s_Rdata* rdata;
- if (PyArg_ParseTuple(args, "|b", &version)) {
- try {
- self->edns = new EDNS(version);
- } catch (const isc::InvalidParameter& ex) {
- PyErr_SetString(po_InvalidParameter, ex.what());
- return (-1);
- } catch (...) {
- PyErr_SetString(po_IscException, "Unexpected exception");
- return (-1);
- }
- return (0);
- } else if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
- &rrclass_type, &rrclass, &rrtype_type, &rrtype,
- &rrttl_type, &rrttl, &rdata_type, &rdata)) {
-
-
-
- uint8_t extended_rcode;
- self->edns = createFromRR(*name->name, *rrclass->rrclass,
- *rrtype->rrtype, *rrttl->rrttl,
- *rdata->rdata, extended_rcode);
- return (self->edns != NULL ? 0 : -1);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError, "Invalid arguments to EDNS constructor");
- return (-1);
- }
- void
- EDNS_destroy(s_EDNS* const self) {
- delete self->edns;
- self->edns = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- PyObject*
- EDNS_toText(const s_EDNS* const self) {
-
- return (Py_BuildValue("s", self->edns->toText().c_str()));
- }
- PyObject*
- EDNS_str(PyObject* const self) {
-
- return (PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- PyObject*
- EDNS_toWire(const s_EDNS* const self, PyObject* args) {
- PyObject* bytes;
- uint8_t extended_rcode;
- s_MessageRenderer* renderer;
- if (PyArg_ParseTuple(args, "Ob", &bytes, &extended_rcode) &&
- PySequence_Check(bytes)) {
- PyObject* bytes_o = bytes;
-
- OutputBuffer buffer(0);
- self->edns->toWire(buffer, extended_rcode);
- PyObject* rd_bytes = PyBytes_FromStringAndSize(
- static_cast<const char*>(buffer.getData()), buffer.getLength());
- PyObject* result = PySequence_InPlaceConcat(bytes_o, rd_bytes);
-
-
- Py_DECREF(rd_bytes);
- return (result);
- } else if (PyArg_ParseTuple(args, "O!b", &messagerenderer_type,
- &renderer, &extended_rcode)) {
- const unsigned int n = self->edns->toWire(*renderer->messagerenderer,
- extended_rcode);
- return (Py_BuildValue("I", n));
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError, "Incorrect arguments for EDNS.to_wire()");
- return (NULL);
- }
- PyObject*
- EDNS_getVersion(const s_EDNS* const self) {
- return (Py_BuildValue("B", self->edns->getVersion()));
- }
- PyObject*
- EDNS_isDNSSECSupported(const s_EDNS* const self) {
- if (self->edns->isDNSSECSupported()) {
- Py_RETURN_TRUE;
- } else {
- Py_RETURN_FALSE;
- }
- }
- PyObject*
- EDNS_setDNSSECSupported(s_EDNS* self, PyObject* args) {
- const PyObject *b;
- if (!PyArg_ParseTuple(args, "O!", &PyBool_Type, &b)) {
- return (NULL);
- }
- self->edns->setDNSSECSupported(b == Py_True);
- Py_RETURN_NONE;
- }
- PyObject*
- EDNS_getUDPSize(const s_EDNS* const self) {
- return (Py_BuildValue("I", self->edns->getUDPSize()));
- }
- PyObject*
- EDNS_setUDPSize(s_EDNS* self, PyObject* args) {
- unsigned int size;
- if (!PyArg_ParseTuple(args, "I", &size)) {
- return (NULL);
- }
- if (size > 65535) {
- PyErr_SetString(PyExc_OverflowError,
- "UDP size is not an unsigned 16-bit integer");
- return (NULL);
- }
- self->edns->setUDPSize(size);
- Py_RETURN_NONE;
- }
- PyObject*
- EDNS_createFromRR(const s_EDNS* null_self, PyObject* args) {
- const s_Name* name;
- const s_RRClass* rrclass;
- const s_RRType* rrtype;
- const s_RRTTL* rrttl;
- const s_Rdata* rdata;
- s_EDNS* edns_obj = NULL;
- assert(null_self == NULL);
- if (PyArg_ParseTuple(args, "O!O!O!O!O!", &name_type, &name,
- &rrclass_type, &rrclass, &rrtype_type, &rrtype,
- &rrttl_type, &rrttl, &rdata_type, &rdata)) {
- uint8_t extended_rcode;
- edns_obj = PyObject_New(s_EDNS, &edns_type);
- if (edns_obj == NULL) {
- return (NULL);
- }
- edns_obj->edns = createFromRR(*name->name, *rrclass->rrclass,
- *rrtype->rrtype, *rrttl->rrttl,
- *rdata->rdata, extended_rcode);
- if (edns_obj->edns != NULL) {
- PyObject* extrcode_obj = Py_BuildValue("B", extended_rcode);
- return (Py_BuildValue("OO", edns_obj, extrcode_obj));
- }
-
- Py_DECREF(edns_obj);
- return (NULL);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "Incorrect arguments for EDNS.create_from_rr()");
- return (NULL);
- }
- }
- bool
- initModulePart_EDNS(PyObject* mod) {
-
-
-
- if (PyType_Ready(&edns_type) < 0) {
- return (false);
- }
- Py_INCREF(&edns_type);
- void* p = &edns_type;
- PyModule_AddObject(mod, "EDNS", static_cast<PyObject*>(p));
- addClassVariable(edns_type, "SUPPORTED_VERSION",
- Py_BuildValue("B", EDNS::SUPPORTED_VERSION));
- return (true);
- }
|