123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #include <dns/rdata.h>
- using namespace isc::dns;
- using namespace isc::util;
- using namespace isc::dns::rdata;
- static PyObject* po_InvalidRdataLength;
- static PyObject* po_InvalidRdataText;
- static PyObject* po_CharStringTooLong;
- class s_Rdata : public PyObject {
- public:
- RdataPtr rdata;
- };
- static int Rdata_init(s_Rdata* self, PyObject* args);
- static void Rdata_destroy(s_Rdata* self);
- static PyObject* Rdata_toText(s_Rdata* self);
- static PyObject* Rdata_str(PyObject* self);
- static PyObject* Rdata_toWire(s_Rdata* self, PyObject* args);
- static PyObject* RData_richcmp(s_Rdata* self, s_Rdata* other, int op);
- static PyMethodDef Rdata_methods[] = {
- { "to_text", reinterpret_cast<PyCFunction>(Rdata_toText), METH_NOARGS,
- "Returns the string representation" },
- { "to_wire", reinterpret_cast<PyCFunction>(Rdata_toWire), METH_VARARGS,
- "Converts the Rdata 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" },
- { NULL, NULL, 0, NULL }
- };
- static PyTypeObject rdata_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.Rdata",
- sizeof(s_Rdata),
- 0,
- (destructor)Rdata_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Rdata_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The Rdata class is an abstract base class that provides "
- "a set of common interfaces to manipulate concrete RDATA objects.",
- NULL,
- NULL,
- (richcmpfunc)RData_richcmp,
- 0,
- NULL,
- NULL,
- Rdata_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)Rdata_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- static int
- Rdata_init(s_Rdata* self, PyObject* args) {
- s_RRType* rrtype;
- s_RRClass* rrclass;
- const char* s;
- const char* data;
- Py_ssize_t len;
-
- if (PyArg_ParseTuple(args, "O!O!s", &rrtype_type, &rrtype,
- &rrclass_type, &rrclass,
- &s)) {
- self->rdata = createRdata(*rrtype->rrtype, *rrclass->rrclass, s);
- return (0);
- } else if (PyArg_ParseTuple(args, "O!O!y#", &rrtype_type, &rrtype,
- &rrclass_type, &rrclass, &data, &len)) {
- InputBuffer input_buffer(data, len);
- self->rdata = createRdata(*rrtype->rrtype, *rrclass->rrclass,
- input_buffer, len);
- return (0);
- }
- return (-1);
- }
- static void
- Rdata_destroy(s_Rdata* self) {
-
-
- self->rdata.reset();
- Py_TYPE(self)->tp_free(self);
- }
- static PyObject*
- Rdata_toText(s_Rdata* self) {
-
- return (Py_BuildValue("s", self->rdata->toText().c_str()));
- }
- static PyObject*
- Rdata_str(PyObject* self) {
-
- return (PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- static PyObject*
- Rdata_toWire(s_Rdata* self, PyObject* args) {
- PyObject* bytes;
- s_MessageRenderer* mr;
-
- if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
- PyObject* bytes_o = bytes;
-
- OutputBuffer buffer(4);
- self->rdata->toWire(buffer);
- 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!", &messagerenderer_type, &mr)) {
- self->rdata->toWire(*mr->messagerenderer);
-
-
- Py_RETURN_NONE;
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "toWire argument must be a sequence object or a MessageRenderer");
- return (NULL);
- }
- static PyObject*
- RData_richcmp(s_Rdata* self, s_Rdata* other, int op) {
- bool c;
-
-
- if (!other || (self->ob_type != other->ob_type)) {
- Py_RETURN_FALSE;
- }
- switch (op) {
- case Py_LT:
- c = self->rdata->compare(*other->rdata) < 0;
- break;
- case Py_LE:
- c = self->rdata->compare(*other->rdata) < 0 ||
- self->rdata->compare(*other->rdata) == 0;
- break;
- case Py_EQ:
- c = self->rdata->compare(*other->rdata) == 0;
- break;
- case Py_NE:
- c = self->rdata->compare(*other->rdata) != 0;
- break;
- case Py_GT:
- c = self->rdata->compare(*other->rdata) > 0;
- break;
- case Py_GE:
- c = self->rdata->compare(*other->rdata) > 0 ||
- self->rdata->compare(*other->rdata) == 0;
- break;
- default:
- PyErr_SetString(PyExc_IndexError,
- "Unhandled rich comparison operator");
- return (NULL);
- }
- if (c)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
- }
- bool
- initModulePart_Rdata(PyObject* mod) {
-
-
-
- if (PyType_Ready(&rdata_type) < 0) {
- return (false);
- }
- Py_INCREF(&rdata_type);
- PyModule_AddObject(mod, "Rdata",
- reinterpret_cast<PyObject*>(&rdata_type));
-
- po_InvalidRdataLength = PyErr_NewException("pydnspp.InvalidRdataLength", NULL, NULL);
- PyModule_AddObject(mod, "InvalidRdataLength", po_InvalidRdataLength);
- po_InvalidRdataText = PyErr_NewException("pydnspp.InvalidRdataText", NULL, NULL);
- PyModule_AddObject(mod, "InvalidRdataText", po_InvalidRdataText);
- po_CharStringTooLong = PyErr_NewException("pydnspp.CharStringTooLong", NULL, NULL);
- PyModule_AddObject(mod, "CharStringTooLong", po_CharStringTooLong);
-
- return (true);
- }
|