123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- static PyObject* po_EmptyLabel;
- static PyObject* po_TooLongName;
- static PyObject* po_TooLongLabel;
- static PyObject* po_BadLabelType;
- static PyObject* po_BadEscape;
- static PyObject* po_IncompleteName;
- static PyObject* po_InvalidBufferPosition;
- static PyObject* po_DNSMessageFORMERR;
- static PyObject* po_NameRelation;
- using namespace isc::dns;
- class s_NameComparisonResult : public PyObject {
- public:
- isc::dns::NameComparisonResult* ncr;
- };
- static int NameComparisonResult_init(s_NameComparisonResult*, PyObject*);
- static void NameComparisonResult_destroy(s_NameComparisonResult* self);
- static PyObject* NameComparisonResult_getOrder(s_NameComparisonResult* self);
- static PyObject* NameComparisonResult_getCommonLabels(s_NameComparisonResult* self);
- static PyObject* NameComparisonResult_getRelation(s_NameComparisonResult* self);
- static PyMethodDef NameComparisonResult_methods[] = {
- { "get_order", reinterpret_cast<PyCFunction>(NameComparisonResult_getOrder), METH_NOARGS,
- "Returns the order" },
- { "get_common_labels", reinterpret_cast<PyCFunction>(NameComparisonResult_getCommonLabels), METH_NOARGS,
- "Returns the number of common labels" },
- { "get_relation", reinterpret_cast<PyCFunction>(NameComparisonResult_getRelation), METH_NOARGS,
- "Returns the relation" },
- { NULL, NULL, 0, NULL }
- };
- static PyTypeObject name_comparison_result_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.NameComparisonResult",
- sizeof(s_NameComparisonResult),
- 0,
- (destructor)NameComparisonResult_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "This is a supplemental class used only as a return value of Name.compare(). "
- "It encapsulate a tuple of the comparison: ordering, number of common labels, "
- "and relationship as follows:\n"
- "- ordering: relative ordering under the DNSSEC order relation\n"
- "- labels: the number of common significant labels of the two names being"
- " compared\n"
- "- relationship: see NameComparisonResult.NameRelation\n",
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- NameComparisonResult_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)NameComparisonResult_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- static int
- NameComparisonResult_init(s_NameComparisonResult*, PyObject*) {
- PyErr_SetString(PyExc_NotImplementedError,
- "NameComparisonResult can't be built directly");
- return (-1);
- }
- static void
- NameComparisonResult_destroy(s_NameComparisonResult* self) {
- delete self->ncr;
- self->ncr = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- static PyObject*
- NameComparisonResult_getOrder(s_NameComparisonResult* self) {
- return (Py_BuildValue("i", self->ncr->getOrder()));
- }
- static PyObject*
- NameComparisonResult_getCommonLabels(s_NameComparisonResult* self) {
- return (Py_BuildValue("I", self->ncr->getCommonLabels()));
- }
- static PyObject*
- NameComparisonResult_getRelation(s_NameComparisonResult* self) {
- return (Py_BuildValue("I", self->ncr->getRelation()));
- }
- class s_Name : public PyObject {
- public:
- isc::dns::Name* name;
- size_t position;
- };
- static int Name_init(s_Name* self, PyObject* args);
- static void Name_destroy(s_Name* self);
- static PyObject* Name_toWire(s_Name* self, PyObject* args);
- static PyObject* Name_toText(s_Name* self);
- static PyObject* Name_str(PyObject* self);
- static PyObject* Name_getLabelCount(s_Name* self);
- static PyObject* Name_at(s_Name* self, PyObject* args);
- static PyObject* Name_getLength(s_Name* self);
- static PyObject* Name_compare(s_Name* self, PyObject* args);
- static PyObject* Name_equals(s_Name* self, PyObject* args);
- static PyObject* Name_richcmp(s_Name* self, s_Name* other, int op);
- static PyObject* Name_split(s_Name* self, PyObject* args);
- static PyObject* Name_reverse(s_Name* self);
- static PyObject* Name_concatenate(s_Name* self, PyObject* args);
- static PyObject* Name_downcase(s_Name* self);
- static PyObject* Name_isWildCard(s_Name* self);
- static PyMethodDef Name_methods[] = {
- { "at", reinterpret_cast<PyCFunction>(Name_at), METH_VARARGS,
- "Returns the integer value of the name data at the specified position" },
- { "get_length", reinterpret_cast<PyCFunction>(Name_getLength), METH_NOARGS,
- "Returns the length" },
- { "get_labelcount", reinterpret_cast<PyCFunction>(Name_getLabelCount), METH_NOARGS,
- "Returns the number of labels" },
- { "to_text", reinterpret_cast<PyCFunction>(Name_toText), METH_NOARGS,
- "Returns the string representation" },
- { "to_wire", reinterpret_cast<PyCFunction>(Name_toWire), METH_VARARGS,
- "Converts the Name 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" },
- { "compare", reinterpret_cast<PyCFunction>(Name_compare), METH_VARARGS,
- "Returns a NameComparisonResult object. The argument must be another Name object" },
- { "equals", reinterpret_cast<PyCFunction>(Name_equals), METH_VARARGS,
- "Returns true if the given Name object is equal to this one" },
- { "split", reinterpret_cast<PyCFunction>(Name_split), METH_VARARGS,
- "Splits the name, takes two arguments, the first is an integer "
- "specifying the first label to place in the result. The second "
- "is an integer specifying the number of labels to put in the "
- "result. Returns a new Name object" },
- { "reverse", reinterpret_cast<PyCFunction>(Name_reverse), METH_NOARGS,
- "Returns a new Name object that is the reverse of this one" },
- { "concatenate", reinterpret_cast<PyCFunction>(Name_concatenate), METH_VARARGS,
- "Concatenates the given Name object to this one and returns the "
- "result as a new Name object" },
- { "downcase", reinterpret_cast<PyCFunction>(Name_downcase), METH_NOARGS,
- "Downcases this name object (in-place). Returns a new reference to the Name." },
- { "is_wildcard", reinterpret_cast<PyCFunction>(Name_isWildCard), METH_NOARGS,
- "Returns True if the Name object represents a wildcard name." },
- { NULL, NULL, 0, NULL }
- };
- static PyTypeObject name_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pydnspp.Name",
- sizeof(s_Name),
- 0,
- (destructor)Name_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Name_str,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- "The Name class encapsulates DNS names.\n"
- "It provides interfaces to construct a name from string or wire-format data, "
- "transform a name into a string or wire-format data, compare two names, get "
- "access to various properties of a name, etc.",
- NULL,
- NULL,
- (richcmpfunc)Name_richcmp,
- 0,
- NULL,
- NULL,
- Name_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- (initproc)Name_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
-
-
- NULL,
- 0
- };
- static int
- Name_init(s_Name* self, PyObject* args) {
- const char* s;
- PyObject* downcase = Py_False;
-
-
-
-
- if (PyArg_ParseTuple(args, "s|O!", &s, &PyBool_Type, &downcase)) {
- try {
- const std::string n(s);
- self->name = new Name(n, downcase == Py_True);
- self->position = 0;
- } catch (const EmptyLabel&) {
- PyErr_SetString(po_EmptyLabel, "EmptyLabel");
- return (-1);
- } catch (const TooLongLabel&) {
- PyErr_SetString(po_TooLongLabel, "TooLongLabel");
- return (-1);
- } catch (const BadLabelType&) {
- PyErr_SetString(po_BadLabelType, "BadLabelType");
- return (-1);
- } catch (const BadEscape&) {
- PyErr_SetString(po_BadEscape, "BadEscape");
- return (-1);
- } catch (const TooLongName&) {
- PyErr_SetString(po_TooLongName, "TooLongName");
- return (-1);
- } catch (const IncompleteName&) {
- PyErr_SetString(po_IncompleteName, "IncompleteName");
- return (-1);
- #ifdef CATCHMEMERR
- } catch (const std::bad_alloc&) {
- PyErr_NoMemory();
- return (-1);
- #endif
- } catch (...) {
- PyErr_SetString(po_IscException, "Unexpected?!");
- return (-1);
- }
- return (0);
- }
- PyErr_Clear();
- PyObject* bytes_obj;
- const char* bytes;
- Py_ssize_t len;
- long position = 0;
-
-
- if (PyArg_ParseTuple(args, "O|lO!", &bytes_obj, &position,
- &PyBool_Type, &downcase) &&
- PyObject_AsCharBuffer(bytes_obj, &bytes, &len) != -1) {
- try {
- if (position < 0) {
-
- PyErr_SetString(PyExc_IndexError,
- "Name index shouldn't be negative");
- return (-1);
- }
- InputBuffer buffer(bytes, len);
- buffer.setPosition(position);
- self->name = new Name(buffer, downcase == Py_True);
- self->position = buffer.getPosition();
- } catch (const InvalidBufferPosition&) {
- PyErr_SetString(po_InvalidBufferPosition,
- "InvalidBufferPosition");
- return (-1);
- } catch (const DNSMessageFORMERR&) {
- PyErr_SetString(po_DNSMessageFORMERR, "DNSMessageFORMERR");
- return (-1);
- } catch (...) {
- PyErr_SetString(po_IscException, "Unexpected?!");
- return (-1);
- }
- return (0);
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "No valid types in Name constructor (should be string or sequence and offset");
- return (-1);
- }
- static void
- Name_destroy(s_Name* self) {
- delete self->name;
- self->name = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- static PyObject*
- Name_at(s_Name* self, PyObject* args) {
- int pos;
- if (!PyArg_ParseTuple(args, "i", &pos)) {
- return (NULL);
- }
- if (pos < 0) {
-
- PyErr_SetString(PyExc_IndexError,
- "name index shouldn't be negative");
- return (NULL);
- }
- try {
- return (Py_BuildValue("I", self->name->at(pos)));
- } catch (const isc::OutOfRange&) {
- PyErr_SetString(PyExc_IndexError,
- "name index out of range");
- return (NULL);
- }
- }
- static PyObject*
- Name_getLength(s_Name* self) {
- return (Py_BuildValue("i", self->name->getLength()));
- }
- static PyObject*
- Name_getLabelCount(s_Name* self) {
- return (Py_BuildValue("i", self->name->getLabelCount()));
- }
- static PyObject*
- Name_toText(s_Name* self) {
- return (Py_BuildValue("s", self->name->toText().c_str()));
- }
- static PyObject*
- Name_str(PyObject* self) {
-
-
-
- return (PyObject_CallMethod(self,
- const_cast<char*>("to_text"),
- const_cast<char*>("")));
- }
- static PyObject*
- Name_toWire(s_Name* 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);
- self->name->toWire(buffer);
- PyObject* name_bytes = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()), buffer.getLength());
- PyObject* result = PySequence_InPlaceConcat(bytes_o, name_bytes);
-
-
- Py_DECREF(name_bytes);
- return (result);
- } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
- self->name->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*
- Name_compare(s_Name* self, PyObject* args) {
- s_Name* other;
- if (!PyArg_ParseTuple(args, "O!", &name_type, &other))
- return (NULL);
- s_NameComparisonResult* ret = PyObject_New(s_NameComparisonResult, &name_comparison_result_type);
- if (ret != NULL) {
- ret->ncr = new NameComparisonResult(
- self->name->compare(*other->name));
- }
- return (ret);
- }
- static PyObject*
- Name_equals(s_Name* self, PyObject* args) {
- s_Name* other;
- if (!PyArg_ParseTuple(args, "O!", &name_type, &other))
- return (NULL);
- if (self->name->equals(*other->name))
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
- }
- static PyObject*
- Name_split(s_Name* self, PyObject* args) {
- int first, n;
- s_Name* ret = NULL;
- if (PyArg_ParseTuple(args, "ii", &first, &n)) {
- if (first < 0 || n < 0) {
-
- PyErr_SetString(PyExc_IndexError,
- "name index shouldn't be negative");
- return (NULL);
- }
- ret = PyObject_New(s_Name, &name_type);
- if (ret != NULL) {
- ret->name = NULL;
- try {
- ret->name = new Name(self->name->split(first, n));
- } catch(const isc::OutOfRange& oor) {
- PyErr_SetString(PyExc_IndexError, oor.what());
- ret->name = NULL;
- }
- if (ret->name == NULL) {
- Py_DECREF(ret);
- return (NULL);
- }
- }
- } else if (PyArg_ParseTuple(args, "i", &n)) {
- PyErr_Clear();
- if (n < 0) {
-
- PyErr_SetString(PyExc_IndexError,
- "name index shouldn't be negative");
- return (NULL);
- }
- ret = PyObject_New(s_Name, &name_type);
- if (ret != NULL) {
- ret->name = NULL;
- try {
- ret->name = new Name(self->name->split(n));
- } catch(const isc::OutOfRange& oor) {
- PyErr_SetString(PyExc_IndexError, oor.what());
- ret->name = NULL;
- }
- if (ret->name == NULL) {
- Py_DECREF(ret);
- return (NULL);
- }
- }
- }
- PyErr_Clear();
- PyErr_SetString(PyExc_TypeError,
- "No valid type in split argument");
- return (ret);
- }
- #include <iostream>
- static PyObject*
- Name_richcmp(s_Name* self, s_Name* other, int op) {
- bool c;
-
-
- if (!other || (self->ob_type != other->ob_type)) {
- Py_RETURN_FALSE;
- }
- switch (op) {
- case Py_LT:
- c = *self->name < *other->name;
- break;
- case Py_LE:
- c = *self->name <= *other->name;
- break;
- case Py_EQ:
- c = *self->name == *other->name;
- break;
- case Py_NE:
- c = *self->name != *other->name;
- break;
- case Py_GT:
- c = *self->name > *other->name;
- break;
- case Py_GE:
- c = *self->name >= *other->name;
- break;
- default:
- PyErr_SetString(PyExc_IndexError,
- "Unhandled rich comparison operator");
- return (NULL);
- }
- if (c) {
- Py_RETURN_TRUE;
- } else {
- Py_RETURN_FALSE;
- }
- }
- static PyObject*
- Name_reverse(s_Name* self) {
- s_Name* ret = PyObject_New(s_Name, &name_type);
- if (ret != NULL) {
- ret->name = new Name(self->name->reverse());
- if (ret->name == NULL) {
- Py_DECREF(ret);
- return (NULL);
- }
- }
- return (ret);
- }
- static PyObject*
- Name_concatenate(s_Name* self, PyObject* args) {
- s_Name* other;
- if (!PyArg_ParseTuple(args, "O!", &name_type, &other))
- return (NULL);
- s_Name* ret = PyObject_New(s_Name, &name_type);
- if (ret != NULL) {
- try {
- ret->name = new Name(self->name->concatenate(*other->name));
- } catch (const TooLongName& tln) {
- PyErr_SetString(po_TooLongName, tln.what());
- return (NULL);
- }
- }
- return (ret);
- }
- static PyObject*
- Name_downcase(s_Name* self) {
- self->name->downcase();
- Py_INCREF(self);
- return (self);
- }
- static PyObject*
- Name_isWildCard(s_Name* self) {
- if (self->name->isWildcard()) {
- Py_RETURN_TRUE;
- } else {
- Py_RETURN_FALSE;
- }
- }
- bool
- initModulePart_Name(PyObject* mod) {
-
-
-
-
-
-
- if (PyType_Ready(&name_comparison_result_type) < 0) {
- return (false);
- }
- Py_INCREF(&name_comparison_result_type);
-
- po_NameRelation = Py_BuildValue("{i:s,i:s,i:s,i:s}",
- NameComparisonResult::SUPERDOMAIN, "SUPERDOMAIN",
- NameComparisonResult::SUBDOMAIN, "SUBDOMAIN",
- NameComparisonResult::EQUAL, "EQUAL",
- NameComparisonResult::COMMONANCESTOR, "COMMONANCESTOR");
- addClassVariable(name_comparison_result_type, "NameRelation", po_NameRelation);
- PyModule_AddObject(mod, "NameComparisonResult",
- reinterpret_cast<PyObject*>(&name_comparison_result_type));
-
-
-
-
- if (PyType_Ready(&name_type) < 0) {
- return (false);
- }
- Py_INCREF(&name_type);
-
- addClassVariable(name_type, "MAX_WIRE", Py_BuildValue("I", Name::MAX_WIRE));
- addClassVariable(name_type, "MAX_LABELS", Py_BuildValue("I", Name::MAX_LABELS));
- addClassVariable(name_type, "MAX_LABELLEN", Py_BuildValue("I", Name::MAX_LABELLEN));
- addClassVariable(name_type, "MAX_COMPRESS_POINTER", Py_BuildValue("I", Name::MAX_COMPRESS_POINTER));
- addClassVariable(name_type, "COMPRESS_POINTER_MARK8", Py_BuildValue("I", Name::COMPRESS_POINTER_MARK8));
- addClassVariable(name_type, "COMPRESS_POINTER_MARK16", Py_BuildValue("I", Name::COMPRESS_POINTER_MARK16));
- s_Name* root_name = PyObject_New(s_Name, &name_type);
- root_name->name = new Name(Name::ROOT_NAME());
- PyObject* po_ROOT_NAME = root_name;
- addClassVariable(name_type, "ROOT_NAME", po_ROOT_NAME);
- PyModule_AddObject(mod, "Name",
- reinterpret_cast<PyObject*>(&name_type));
-
-
- po_EmptyLabel = PyErr_NewException("pydnspp.EmptyLabel", NULL, NULL);
- PyModule_AddObject(mod, "EmptyLabel", po_EmptyLabel);
- po_TooLongName = PyErr_NewException("pydnspp.TooLongName", NULL, NULL);
- PyModule_AddObject(mod, "TooLongName", po_TooLongName);
- po_TooLongLabel = PyErr_NewException("pydnspp.TooLongLabel", NULL, NULL);
- PyModule_AddObject(mod, "TooLongLabel", po_TooLongLabel);
- po_BadLabelType = PyErr_NewException("pydnspp.BadLabelType", NULL, NULL);
- PyModule_AddObject(mod, "BadLabelType", po_BadLabelType);
- po_BadEscape = PyErr_NewException("pydnspp.BadEscape", NULL, NULL);
- PyModule_AddObject(mod, "BadEscape", po_BadEscape);
- po_IncompleteName = PyErr_NewException("pydnspp.IncompleteName", NULL, NULL);
- PyModule_AddObject(mod, "IncompleteName", po_IncompleteName);
- po_InvalidBufferPosition = PyErr_NewException("pydnspp.InvalidBufferPosition", NULL, NULL);
- PyModule_AddObject(mod, "InvalidBufferPosition", po_InvalidBufferPosition);
-
-
- po_DNSMessageFORMERR = PyErr_NewException("pydnspp.DNSMessageFORMERR", NULL, NULL);
- PyModule_AddObject(mod, "DNSMessageFORMERR", po_DNSMessageFORMERR);
- return (true);
- }
|