123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #include <dns/rrclass.h>
- using namespace isc::dns;
- static PyObject* po_InvalidRRClass;
- static PyObject* po_IncompleteRRClass;
- class s_RRClass : public PyObject {
- public:
- RRClass* rrclass;
- };
- static int RRClass_init(s_RRClass* self, PyObject* args);
- static void RRClass_destroy(s_RRClass* self);
- static PyObject* RRClass_toText(s_RRClass* self);
- static PyObject* RRClass_str(PyObject* self);
- static PyObject* RRClass_toWire(s_RRClass* self, PyObject* args);
- static PyObject* RRClass_getCode(s_RRClass* self);
- static PyObject* RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op);
- static PyObject* RRClass_IN(s_RRClass *self);
- static PyObject* RRClass_CH(s_RRClass *self);
- static PyObject* RRClass_HS(s_RRClass *self);
- static PyObject* RRClass_NONE(s_RRClass *self);
- static PyObject* RRClass_ANY(s_RRClass *self);
- static PyMethodDef RRClass_methods[] = {
- { "to_text", reinterpret_cast<PyCFunction>(RRClass_toText), METH_NOARGS,
- "Returns the string representation" },
- { "to_wire", reinterpret_cast<PyCFunction>(RRClass_toWire), METH_VARARGS,
- "Converts the RRClass 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_code", reinterpret_cast<PyCFunction>(RRClass_getCode), METH_NOARGS,
- "Returns the class code as an integer" },
- { "IN", reinterpret_cast<PyCFunction>(RRClass_IN), METH_NOARGS | METH_STATIC, "Creates an IN RRClass" },
- { "CH", reinterpret_cast<PyCFunction>(RRClass_CH), METH_NOARGS | METH_STATIC, "Creates a CH RRClass" },
- { "HS", reinterpret_cast<PyCFunction>(RRClass_HS), METH_NOARGS | METH_STATIC, "Creates an HS RRClass" },
- { "NONE", reinterpret_cast<PyCFunction>(RRClass_NONE), METH_NOARGS | METH_STATIC, "Creates a NONE RRClass" },
- { "ANY", reinterpret_cast<PyCFunction>(RRClass_ANY), METH_NOARGS | METH_STATIC, "Creates an ANY RRClass" },
- { NULL, NULL, 0, NULL }
- };
- static PyTypeObject rrclass_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "libdns_python.RRClass",
- sizeof(s_RRClass),
- 0,
- (destructor)RRClass_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- RRClass_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The RRClass class encapsulates DNS resource record classes.\n"
- "This class manages the 16-bit integer class codes in quite a straightforward"
- "way. The only non trivial task is to handle textual representations of"
- "RR classes, such as \"IN\", \"CH\", or \"CLASS65534\".",
- NULL,
- NULL,
- (richcmpfunc)RRClass_richcmp,
- 0,
- NULL,
- NULL,
- RRClass_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)RRClass_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- static int
- RRClass_init(s_RRClass* self, PyObject* args) {
- const char* s;
- unsigned int i;
- PyObject* bytes = NULL;
-
-
-
-
-
-
- try {
- if (PyArg_ParseTuple(args, "s", &s)) {
- self->rrclass = new RRClass(s);
- return (0);
- } else if (PyArg_ParseTuple(args, "I", &i)) {
- PyErr_Clear();
- if (i > 65535) {
- PyErr_SetString(po_InvalidRRClass, "Class number too high");
- return (-1);
- }
- self->rrclass = new RRClass(i);
- return (0);
- } else if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
- uint8_t data[2];
- int result = readDataFromSequence(data, 2, bytes);
- if (result != 0) {
- return (result);
- }
- InputBuffer ib(data, 2);
- self->rrclass = new RRClass(ib);
- PyErr_Clear();
- return (0);
- }
-
-
- } catch (const InvalidRRClass& ic) {
- PyErr_Clear();
- PyErr_SetString(po_InvalidRRClass, ic.what());
- return (-1);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in constructor argument");
- return (-1);
- }
- static void
- RRClass_destroy(s_RRClass* self) {
- delete self->rrclass;
- self->rrclass = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- static PyObject*
- RRClass_toText(s_RRClass* self) {
-
- return (Py_BuildValue("s", self->rrclass->toText().c_str()));
- }
- static PyObject*
- RRClass_str(PyObject* self) {
-
- return PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>(""));
- }
- static PyObject*
- RRClass_toWire(s_RRClass* self, PyObject* args) {
- PyObject* bytes;
- s_MessageRenderer* mr;
-
- if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
- PyObject* bytes_o = bytes;
-
- OutputBuffer buffer(2);
- self->rrclass->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, (PyObject**) &mr)) {
- self->rrclass->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*
- RRClass_getCode(s_RRClass* self) {
- return (Py_BuildValue("I", self->rrclass->getCode()));
- }
- static PyObject*
- RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op) {
- bool c;
-
-
- if (!other || (self->ob_type != other->ob_type)) {
- Py_RETURN_FALSE;
- }
- switch (op) {
- case Py_LT:
- c = *self->rrclass < *other->rrclass;
- break;
- case Py_LE:
- c = *self->rrclass < *other->rrclass ||
- *self->rrclass == *other->rrclass;
- break;
- case Py_EQ:
- c = *self->rrclass == *other->rrclass;
- break;
- case Py_NE:
- c = *self->rrclass != *other->rrclass;
- break;
- case Py_GT:
- c = *other->rrclass < *self->rrclass;
- break;
- case Py_GE:
- c = *other->rrclass < *self->rrclass ||
- *self->rrclass == *other->rrclass;
- break;
- default:
- PyErr_SetString(PyExc_IndexError,
- "Unhandled rich comparison operator");
- return (NULL);
- }
- if (c)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
- }
- static PyObject* RRClass_createStatic(RRClass stc) {
- s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
- if (ret != NULL) {
- ret->rrclass = new RRClass(stc);
- }
- return (ret);
- }
- static PyObject* RRClass_IN(s_RRClass *self UNUSED_PARAM) {
- return (RRClass_createStatic(RRClass::IN()));
- }
- static PyObject* RRClass_CH(s_RRClass *self UNUSED_PARAM) {
- return (RRClass_createStatic(RRClass::CH()));
- }
- static PyObject* RRClass_HS(s_RRClass *self UNUSED_PARAM) {
- return (RRClass_createStatic(RRClass::HS()));
- }
- static PyObject* RRClass_NONE(s_RRClass *self UNUSED_PARAM) {
- return (RRClass_createStatic(RRClass::NONE()));
- }
- static PyObject* RRClass_ANY(s_RRClass *self UNUSED_PARAM) {
- return (RRClass_createStatic(RRClass::ANY()));
- }
- bool
- initModulePart_RRClass(PyObject* mod) {
-
- po_InvalidRRClass = PyErr_NewException("libdns_python.InvalidRRClass", NULL, NULL);
- Py_INCREF(po_InvalidRRClass);
- PyModule_AddObject(mod, "InvalidRRClass", po_InvalidRRClass);
- po_IncompleteRRClass = PyErr_NewException("libdns_python.IncompleteRRClass", NULL, NULL);
- Py_INCREF(po_IncompleteRRClass);
- PyModule_AddObject(mod, "IncompleteRRClass", po_IncompleteRRClass);
-
-
-
- if (PyType_Ready(&rrclass_type) < 0) {
- return (false);
- }
- Py_INCREF(&rrclass_type);
- PyModule_AddObject(mod, "RRClass",
- reinterpret_cast<PyObject*>(&rrclass_type));
-
- return (true);
- }
|