123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- #include <dns/question.h>
- using namespace isc::dns;
- class s_Question : public PyObject {
- public:
- QuestionPtr question;
- };
- static int Question_init(s_Question* self, PyObject* args);
- static void Question_destroy(s_Question* self);
- static PyObject* Question_getName(s_Question* self);
- static PyObject* Question_getType(s_Question* self);
- static PyObject* Question_getClass(s_Question* self);
- static PyObject* Question_toText(s_Question* self);
- static PyObject* Question_str(PyObject* self);
- static PyObject* Question_toWire(s_Question* self, PyObject* args);
- static PyMethodDef Question_methods[] = {
- { "get_name", reinterpret_cast<PyCFunction>(Question_getName), METH_NOARGS,
- "Returns the Name" },
- { "get_type", reinterpret_cast<PyCFunction>(Question_getType), METH_NOARGS,
- "Returns the RRType" },
- { "get_class", reinterpret_cast<PyCFunction>(Question_getClass), METH_NOARGS,
- "Returns the RRClass" },
- { "to_text", reinterpret_cast<PyCFunction>(Question_toText), METH_NOARGS,
- "Returns the string representation" },
- { "to_wire", reinterpret_cast<PyCFunction>(Question_toWire), METH_VARARGS,
- "Converts the Question 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 question_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.Question",
- sizeof(s_Question),
- 0,
- (destructor)Question_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Question_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The Question class encapsulates the common search key of DNS"
- "lookup, consisting of owner name, RR type and RR class.",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- Question_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)Question_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- static int
- Question_init(s_Question* self, PyObject* args) {
-
-
-
-
-
-
- s_Name* name;
- s_RRClass* rrclass;
- s_RRType* rrtype;
- const char* b;
- Py_ssize_t len;
- unsigned int position = 0;
- try {
- if (PyArg_ParseTuple(args, "O!O!O!", &name_type, &name,
- &rrclass_type, &rrclass,
- &rrtype_type, &rrtype
- )) {
- self->question = QuestionPtr(new Question(*name->name, *rrclass->rrclass,
- *rrtype->rrtype));
- return (0);
- } else if (PyArg_ParseTuple(args, "y#|I", &b, &len, &position)) {
- PyErr_Clear();
- InputBuffer inbuf(b, len);
- inbuf.setPosition(position);
- self->question = QuestionPtr(new Question(inbuf));
- return (0);
- }
- } catch (const DNSMessageFORMERR& dmfe) {
- PyErr_Clear();
- PyErr_SetString(po_DNSMessageFORMERR, dmfe.what());
- return (-1);
- } catch (const IncompleteRRClass& irc) {
- PyErr_Clear();
- PyErr_SetString(po_IncompleteRRClass, irc.what());
- return (-1);
- } catch (const IncompleteRRType& irt) {
- PyErr_Clear();
- PyErr_SetString(po_IncompleteRRType, irt.what());
- return (-1);
- }
- self->question = QuestionPtr();
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "no valid type in constructor argument");
- return (-1);
- }
- static void
- Question_destroy(s_Question* self) {
- self->question.reset();
- Py_TYPE(self)->tp_free(self);
- }
- static PyObject*
- Question_getName(s_Question* self) {
- s_Name* name;
-
- name = static_cast<s_Name*>(name_type.tp_alloc(&name_type, 0));
- if (name != NULL) {
- name->name = new Name(self->question->getName());
- }
- return (name);
- }
- static PyObject*
- Question_getType(s_Question* self) {
- s_RRType* rrtype;
- rrtype = static_cast<s_RRType*>(rrtype_type.tp_alloc(&rrtype_type, 0));
- if (rrtype != NULL) {
- rrtype->rrtype = new RRType(self->question->getType());
- }
- return (rrtype);
- }
- static PyObject*
- Question_getClass(s_Question* self) {
- s_RRClass* rrclass;
- rrclass = static_cast<s_RRClass*>(rrclass_type.tp_alloc(&rrclass_type, 0));
- if (rrclass != NULL) {
- rrclass->rrclass = new RRClass(self->question->getClass());
- }
- return (rrclass);
- }
- static PyObject*
- Question_toText(s_Question* self) {
-
- return (Py_BuildValue("s", self->question->toText().c_str()));
- }
- static PyObject*
- Question_str(PyObject* self) {
-
- return (PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- static PyObject*
- Question_toWire(s_Question* self, PyObject* args) {
- PyObject* bytes;
- s_MessageRenderer* mr;
-
- if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
- PyObject* bytes_o = bytes;
-
- OutputBuffer buffer(Name::MAX_WIRE + 4);
- self->question->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->question->toWire(*mr->messagerenderer);
-
-
- Py_RETURN_NONE;
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "toWire argument must be a sequence object or a MessageRenderer");
- return (NULL);
- }
- bool
- initModulePart_Question(PyObject* mod) {
-
-
-
-
- if (PyType_Ready(&question_type) < 0) {
- return (false);
- }
- Py_INCREF(&question_type);
- PyModule_AddObject(mod, "Question",
- reinterpret_cast<PyObject*>(&question_type));
-
- return (true);
- }
|