finder_python.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // Enable this if you use s# variants with PyArg_ParseTuple(), see
  15. // http://docs.python.org/py3k/c-api/arg.html#strings-and-buffers
  16. //#define PY_SSIZE_T_CLEAN
  17. // Python.h needs to be placed at the head of the program file, see:
  18. // http://docs.python.org/py3k/extending/extending.html#a-simple-example
  19. #include <Python.h>
  20. #include <util/python/pycppwrapper_util.h>
  21. #include <datasrc/client.h>
  22. #include <datasrc/database.h>
  23. #include <datasrc/data_source.h>
  24. #include <datasrc/sqlite3_accessor.h>
  25. #include <datasrc/iterator.h>
  26. #include <datasrc/zone.h>
  27. #include <dns/python/name_python.h>
  28. #include <dns/python/rrset_python.h>
  29. #include <dns/python/rrclass_python.h>
  30. #include <dns/python/rrtype_python.h>
  31. #include <dns/python/pydnspp_common.h>
  32. #include "datasrc.h"
  33. #include "finder_python.h"
  34. #include "finder_inc.cc"
  35. using namespace std;
  36. using namespace isc::util::python;
  37. using namespace isc::dns::python;
  38. using namespace isc::datasrc;
  39. using namespace isc::datasrc::python;
  40. namespace {
  41. ZoneFinder::FindResultFlags
  42. getFindResultFlags(const ZoneFinder::FindResult& result) {
  43. ZoneFinder::FindResultFlags result_flags = ZoneFinder::RESULT_DEFAULT;
  44. if (result.isWildcard()) {
  45. result_flags = result_flags | ZoneFinder::RESULT_WILDCARD;
  46. }
  47. if (result.isNSECSigned()) {
  48. result_flags = result_flags | ZoneFinder::RESULT_NSEC_SIGNED;
  49. }
  50. if (result.isNSEC3Signed()) {
  51. result_flags = result_flags | ZoneFinder::RESULT_NSEC3_SIGNED;
  52. }
  53. return (result_flags);
  54. }
  55. }
  56. namespace isc_datasrc_internal {
  57. // This is the shared code for the find() call in the finder and the updater
  58. // Is is intentionally not available through any header, nor at our standard
  59. // namespace, as it is not supposed to be called anywhere but from finder and
  60. // updater
  61. PyObject* ZoneFinder_helper(ZoneFinder* finder, PyObject* args) {
  62. if (finder == NULL) {
  63. PyErr_SetString(getDataSourceException("Error"),
  64. "Internal error in find() wrapper; "
  65. "finder object NULL");
  66. return (NULL);
  67. }
  68. PyObject* name;
  69. PyObject* rrtype;
  70. unsigned int options_int = ZoneFinder::FIND_DEFAULT;
  71. if (PyArg_ParseTuple(args, "O!O!|I", &name_type, &name,
  72. &rrtype_type, &rrtype,
  73. &options_int)) {
  74. try {
  75. ZoneFinder::FindOptions options =
  76. static_cast<ZoneFinder::FindOptions>(options_int);
  77. const ZoneFinder::FindResult find_result(
  78. finder->find(PyName_ToName(name), PyRRType_ToRRType(rrtype),
  79. options));
  80. const ZoneFinder::Result r = find_result.code;
  81. isc::dns::ConstRRsetPtr rrsp = find_result.rrset;
  82. ZoneFinder::FindResultFlags result_flags =
  83. getFindResultFlags(find_result);
  84. if (rrsp) {
  85. // Use N instead of O so the refcount isn't increased twice
  86. return (Py_BuildValue("INI", r, createRRsetObject(*rrsp),
  87. result_flags));
  88. } else {
  89. return (Py_BuildValue("IOI", r, Py_None, result_flags));
  90. }
  91. } catch (const DataSourceError& dse) {
  92. PyErr_SetString(getDataSourceException("Error"), dse.what());
  93. return (NULL);
  94. } catch (const std::exception& exc) {
  95. PyErr_SetString(getDataSourceException("Error"), exc.what());
  96. return (NULL);
  97. } catch (...) {
  98. PyErr_SetString(getDataSourceException("Error"),
  99. "Unexpected exception");
  100. return (NULL);
  101. }
  102. } else {
  103. return (NULL);
  104. }
  105. }
  106. PyObject* ZoneFinder_helper_all(ZoneFinder* finder, PyObject* args) {
  107. if (finder == NULL) {
  108. PyErr_SetString(getDataSourceException("Error"),
  109. "Internal error in find_all() wrapper; "
  110. "finder object NULL");
  111. return (NULL);
  112. }
  113. PyObject* name;
  114. const unsigned int options_int = ZoneFinder::FIND_DEFAULT;
  115. if (PyArg_ParseTuple(args, "O!|I", &name_type, &name, &options_int)) {
  116. try {
  117. ZoneFinder::FindOptions options =
  118. static_cast<ZoneFinder::FindOptions>(options_int);
  119. std::vector<isc::dns::ConstRRsetPtr> target;
  120. const ZoneFinder::FindResult find_result(
  121. finder->findAll(PyName_ToName(name), target, options));
  122. const ZoneFinder::Result r = find_result.code;
  123. isc::dns::ConstRRsetPtr rrsp = find_result.rrset;
  124. ZoneFinder::FindResultFlags result_flags =
  125. getFindResultFlags(find_result);
  126. if (r == ZoneFinder::SUCCESS) {
  127. // Copy all the RRsets to the result list
  128. PyObjectContainer list_container(PyList_New(target.size()));
  129. for (size_t i(0); i < target.size(); ++i) {
  130. PyList_SET_ITEM(list_container.get(), i,
  131. createRRsetObject(*target[i]));
  132. }
  133. // Construct the result with the list. The Py_BuildValue
  134. // increases the refcount and the container decreases it
  135. // later. This way, it feels safer in case the build function
  136. // would fail.
  137. return (Py_BuildValue("IO", r, list_container.get()));
  138. } else {
  139. if (rrsp) {
  140. // Use N instead of O so the refcount isn't increased twice
  141. return (Py_BuildValue("INI", r, createRRsetObject(*rrsp),
  142. result_flags));
  143. } else {
  144. return (Py_BuildValue("IOI", r, Py_None, result_flags));
  145. }
  146. }
  147. } catch (const DataSourceError& dse) {
  148. PyErr_SetString(getDataSourceException("Error"), dse.what());
  149. return (NULL);
  150. } catch (const std::exception& exc) {
  151. PyErr_SetString(getDataSourceException("Error"), exc.what());
  152. return (NULL);
  153. } catch (...) {
  154. PyErr_SetString(getDataSourceException("Error"),
  155. "Unexpected exception");
  156. return (NULL);
  157. }
  158. } else {
  159. return (NULL);
  160. }
  161. }
  162. } // end namespace internal
  163. namespace {
  164. // The s_* Class simply covers one instantiation of the object
  165. class s_ZoneFinder : public PyObject {
  166. public:
  167. s_ZoneFinder() : cppobj(ZoneFinderPtr()), base_obj(NULL) {};
  168. ZoneFinderPtr cppobj;
  169. // This is a reference to a base object; if the object of this class
  170. // depends on another object to be in scope during its lifetime,
  171. // we use INCREF the base object upon creation, and DECREF it at
  172. // the end of the destructor
  173. // This is an optional argument to createXXX(). If NULL, it is ignored.
  174. PyObject* base_obj;
  175. };
  176. // Shortcut type which would be convenient for adding class variables safely.
  177. typedef CPPPyObjectContainer<s_ZoneFinder, ZoneFinder> ZoneFinderContainer;
  178. // General creation and destruction
  179. int
  180. ZoneFinder_init(PyObject*, PyObject*, PyObject*) {
  181. // can't be called directly
  182. PyErr_SetString(PyExc_TypeError,
  183. "ZoneFinder cannot be constructed directly");
  184. return (-1);
  185. }
  186. void
  187. ZoneFinder_destroy(PyObject* po_self) {
  188. s_ZoneFinder* self = static_cast<s_ZoneFinder*>(po_self);
  189. // cppobj is a shared ptr, but to make sure things are not destroyed in
  190. // the wrong order, we reset it here.
  191. self->cppobj.reset();
  192. if (self->base_obj != NULL) {
  193. Py_DECREF(self->base_obj);
  194. }
  195. Py_TYPE(self)->tp_free(self);
  196. }
  197. PyObject*
  198. ZoneFinder_getClass(PyObject* po_self, PyObject*) {
  199. s_ZoneFinder* self = static_cast<s_ZoneFinder*>(po_self);
  200. try {
  201. return (createRRClassObject(self->cppobj->getClass()));
  202. } catch (const std::exception& exc) {
  203. PyErr_SetString(getDataSourceException("Error"), exc.what());
  204. return (NULL);
  205. }
  206. }
  207. PyObject*
  208. ZoneFinder_getOrigin(PyObject* po_self, PyObject*) {
  209. s_ZoneFinder* self = static_cast<s_ZoneFinder*>(po_self);
  210. try {
  211. return (createNameObject(self->cppobj->getOrigin()));
  212. } catch (const std::exception& exc) {
  213. PyErr_SetString(getDataSourceException("Error"), exc.what());
  214. return (NULL);
  215. } catch (...) {
  216. PyErr_SetString(getDataSourceException("Error"),
  217. "Unexpected exception");
  218. return (NULL);
  219. }
  220. }
  221. PyObject*
  222. ZoneFinder_find(PyObject* po_self, PyObject* args) {
  223. s_ZoneFinder* const self = static_cast<s_ZoneFinder*>(po_self);
  224. return (isc_datasrc_internal::ZoneFinder_helper(self->cppobj.get(), args));
  225. }
  226. PyObject*
  227. ZoneFinder_find_all(PyObject* po_self, PyObject* args) {
  228. s_ZoneFinder* const self = static_cast<s_ZoneFinder*>(po_self);
  229. return (isc_datasrc_internal::ZoneFinder_helper_all(self->cppobj.get(),
  230. args));
  231. }
  232. PyObject*
  233. ZoneFinder_findPreviousName(PyObject* po_self, PyObject* args) {
  234. s_ZoneFinder* const self = static_cast<s_ZoneFinder*>(po_self);
  235. PyObject* name_obj;
  236. if (PyArg_ParseTuple(args, "O!", &name_type, &name_obj)) {
  237. try {
  238. return (createNameObject(
  239. self->cppobj->findPreviousName(PyName_ToName(name_obj))));
  240. } catch (const isc::NotImplemented& nie) {
  241. PyErr_SetString(getDataSourceException("NotImplemented"),
  242. nie.what());
  243. return (NULL);
  244. } catch (const std::exception& exc) {
  245. PyErr_SetString(getDataSourceException("Error"), exc.what());
  246. return (NULL);
  247. } catch (...) {
  248. PyErr_SetString(getDataSourceException("Error"),
  249. "Unexpected exception");
  250. return (NULL);
  251. }
  252. } else {
  253. return (NULL);
  254. }
  255. }
  256. // This list contains the actual set of functions we have in
  257. // python. Each entry has
  258. // 1. Python method name
  259. // 2. Our static function here
  260. // 3. Argument type
  261. // 4. Documentation
  262. PyMethodDef ZoneFinder_methods[] = {
  263. { "get_origin", ZoneFinder_getOrigin, METH_NOARGS,
  264. ZoneFinder_getOrigin_doc },
  265. { "get_class", ZoneFinder_getClass, METH_NOARGS, ZoneFinder_getClass_doc },
  266. { "find", ZoneFinder_find, METH_VARARGS, ZoneFinder_find_doc },
  267. { "find_all", ZoneFinder_find_all, METH_VARARGS, ZoneFinder_findAll_doc },
  268. { "find_previous_name", ZoneFinder_findPreviousName, METH_VARARGS,
  269. ZoneFinder_find_previous_name_doc },
  270. { NULL, NULL, 0, NULL }
  271. };
  272. } // end of unnamed namespace
  273. namespace isc {
  274. namespace datasrc {
  275. namespace python {
  276. PyTypeObject zonefinder_type = {
  277. PyVarObject_HEAD_INIT(NULL, 0)
  278. "datasrc.ZoneFinder",
  279. sizeof(s_ZoneFinder), // tp_basicsize
  280. 0, // tp_itemsize
  281. ZoneFinder_destroy, // tp_dealloc
  282. NULL, // tp_print
  283. NULL, // tp_getattr
  284. NULL, // tp_setattr
  285. NULL, // tp_reserved
  286. NULL, // tp_repr
  287. NULL, // tp_as_number
  288. NULL, // tp_as_sequence
  289. NULL, // tp_as_mapping
  290. NULL, // tp_hash
  291. NULL, // tp_call
  292. NULL, // tp_str
  293. NULL, // tp_getattro
  294. NULL, // tp_setattro
  295. NULL, // tp_as_buffer
  296. Py_TPFLAGS_DEFAULT, // tp_flags
  297. ZoneFinder_doc,
  298. NULL, // tp_traverse
  299. NULL, // tp_clear
  300. NULL, // tp_richcompare
  301. 0, // tp_weaklistoffset
  302. NULL, // tp_iter
  303. NULL, // tp_iternext
  304. ZoneFinder_methods, // tp_methods
  305. NULL, // tp_members
  306. NULL, // tp_getset
  307. NULL, // tp_base
  308. NULL, // tp_dict
  309. NULL, // tp_descr_get
  310. NULL, // tp_descr_set
  311. 0, // tp_dictoffset
  312. ZoneFinder_init, // tp_init
  313. NULL, // tp_alloc
  314. PyType_GenericNew, // tp_new
  315. NULL, // tp_free
  316. NULL, // tp_is_gc
  317. NULL, // tp_bases
  318. NULL, // tp_mro
  319. NULL, // tp_cache
  320. NULL, // tp_subclasses
  321. NULL, // tp_weaklist
  322. NULL, // tp_del
  323. 0 // tp_version_tag
  324. };
  325. PyObject*
  326. createZoneFinderObject(isc::datasrc::ZoneFinderPtr source, PyObject* base_obj) {
  327. s_ZoneFinder* py_zf = static_cast<s_ZoneFinder*>(
  328. zonefinder_type.tp_alloc(&zonefinder_type, 0));
  329. if (py_zf != NULL) {
  330. py_zf->cppobj = source;
  331. py_zf->base_obj = base_obj;
  332. if (base_obj != NULL) {
  333. Py_INCREF(base_obj);
  334. }
  335. }
  336. return (py_zf);
  337. }
  338. } // namespace python
  339. } // namespace datasrc
  340. } // namespace isc