Branch data Line data Source code
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 : :
15 : : // Enable this if you use s# variants with PyArg_ParseTuple(), see
16 : : // http://docs.python.org/py3k/c-api/arg.html#strings-and-buffers
17 : : //#define PY_SSIZE_T_CLEAN
18 : :
19 : : // Python.h needs to be placed at the head of the program file, see:
20 : : // http://docs.python.org/py3k/extending/extending.html#a-simple-example
21 : : #include <Python.h>
22 : :
23 : : #include <string>
24 : : #include <stdexcept>
25 : :
26 : : #include <util/python/pycppwrapper_util.h>
27 : :
28 : : #include <acl/acl.h>
29 : : #include <acl/dns.h>
30 : :
31 : : #include "dns.h"
32 : : #include "dns_requestacl_python.h"
33 : : #include "dns_requestcontext_python.h"
34 : :
35 : : using namespace std;
36 : : using namespace isc::util::python;
37 : : using namespace isc::acl;
38 : : using namespace isc::acl::dns;
39 : : using namespace isc::acl::dns::python;
40 : :
41 : : //
42 : : // Definition of the classes
43 : : //
44 : :
45 : : // For each class, we need a struct, a helper functions (init, destroy,
46 : : // and static wrappers around the methods we export), a list of methods,
47 : : // and a type description
48 : :
49 : : //
50 : : // RequestACL
51 : : //
52 : :
53 : : // Trivial constructor.
54 : 0 : s_RequestACL::s_RequestACL() {}
55 : :
56 : : // Import pydoc text
57 : : #include "dns_requestacl_inc.cc"
58 : :
59 : : namespace {
60 : : int
61 : 1 : RequestACL_init(PyObject*, PyObject*, PyObject*) {
62 : : PyErr_SetString(getACLException("Error"),
63 : 1 : "RequestACL cannot be directly constructed");
64 : 1 : return (-1);
65 : : }
66 : :
67 : : void
68 : 117 : RequestACL_destroy(PyObject* po_self) {
69 : 117 : s_RequestACL* const self = static_cast<s_RequestACL*>(po_self);
70 : 117 : self->cppobj.reset();
71 : 117 : Py_TYPE(self)->tp_free(self);
72 : 117 : }
73 : :
74 : : PyObject*
75 : 91 : RequestACL_execute(PyObject* po_self, PyObject* args) {
76 : 91 : s_RequestACL* const self = static_cast<s_RequestACL*>(po_self);
77 : :
78 : : try {
79 : : const s_RequestContext* po_context;
80 [ + - ][ + + ]: 91 : if (PyArg_ParseTuple(args, "O!", &requestcontext_type, &po_context)) {
81 : : const BasicAction action =
82 : 264 : self->cppobj->execute(*po_context->cppobj);
83 [ + - ]: 91 : return (Py_BuildValue("I", action));
84 : : }
85 : 0 : } catch (const exception& ex) {
86 [ # # ]: 0 : const string ex_what = "Failed to execute ACL: " + string(ex.what());
87 [ # # ][ # # ]: 0 : PyErr_SetString(getACLException("Error"), ex_what.c_str());
88 [ # # ]: 0 : } catch (...) {
89 : : PyErr_SetString(PyExc_RuntimeError,
90 [ # # ]: 0 : "Unexpected exception in executing ACL");
91 : : }
92 : :
93 : : return (NULL);
94 : : }
95 : :
96 : : // This list contains the actual set of functions we have in
97 : : // python. Each entry has
98 : : // 1. Python method name
99 : : // 2. Our static function here
100 : : // 3. Argument type
101 : : // 4. Documentation
102 : : PyMethodDef RequestACL_methods[] = {
103 : : { "execute", RequestACL_execute, METH_VARARGS, RequestACL_execute_doc },
104 : : { NULL, NULL, 0, NULL }
105 : : };
106 : : } // end of unnamed namespace
107 : :
108 : : namespace isc {
109 : : namespace acl {
110 : : namespace dns {
111 : : namespace python {
112 : : // This defines the complete type for reflection in python and
113 : : // parsing of PyObject* to s_RequestACL
114 : : // Most of the functions are not actually implemented and NULL here.
115 : : PyTypeObject requestacl_type = {
116 : : PyVarObject_HEAD_INIT(NULL, 0)
117 : : "isc.acl._dns.RequestACL",
118 : : sizeof(s_RequestACL), // tp_basicsize
119 : : 0, // tp_itemsize
120 : : RequestACL_destroy, // tp_dealloc
121 : : NULL, // tp_print
122 : : NULL, // tp_getattr
123 : : NULL, // tp_setattr
124 : : NULL, // tp_reserved
125 : : NULL, // tp_repr
126 : : NULL, // tp_as_number
127 : : NULL, // tp_as_sequence
128 : : NULL, // tp_as_mapping
129 : : NULL, // tp_hash
130 : : NULL, // tp_call
131 : : NULL, // tp_str
132 : : NULL, // tp_getattro
133 : : NULL, // tp_setattro
134 : : NULL, // tp_as_buffer
135 : : Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, // tp_flags
136 : : RequestACL_doc,
137 : : NULL, // tp_traverse
138 : : NULL, // tp_clear
139 : : NULL, // tp_richcompare
140 : : 0, // tp_weaklistoffset
141 : : NULL, // tp_iter
142 : : NULL, // tp_iternext
143 : : RequestACL_methods, // tp_methods
144 : : NULL, // tp_members
145 : : NULL, // tp_getset
146 : : NULL, // tp_base
147 : : NULL, // tp_dict
148 : : NULL, // tp_descr_get
149 : : NULL, // tp_descr_set
150 : : 0, // tp_dictoffset
151 : : RequestACL_init, // tp_init
152 : : NULL, // tp_alloc
153 : : PyType_GenericNew, // tp_new
154 : : NULL, // tp_free
155 : : NULL, // tp_is_gc
156 : : NULL, // tp_bases
157 : : NULL, // tp_mro
158 : : NULL, // tp_cache
159 : : NULL, // tp_subclasses
160 : : NULL, // tp_weaklist
161 : : NULL, // tp_del
162 : : 0 // tp_version_tag
163 : : };
164 : :
165 : : bool
166 : 2 : initModulePart_RequestACL(PyObject* mod) {
167 : : // We initialize the static description object with PyType_Ready(),
168 : : // then add it to the module. This is not just a check! (leaving
169 : : // this out results in segmentation faults)
170 [ + - ]: 2 : if (PyType_Ready(&requestacl_type) < 0) {
171 : : return (false);
172 : : }
173 : 2 : void* p = &requestacl_type;
174 [ + - ]: 2 : if (PyModule_AddObject(mod, "RequestACL", static_cast<PyObject*>(p)) < 0) {
175 : : return (false);
176 : : }
177 : 2 : Py_INCREF(&requestacl_type);
178 : :
179 : 2 : return (true);
180 : : }
181 : : } // namespace python
182 : : } // namespace dns
183 : : } // namespace acl
184 : 0 : } // namespace isc
|