dns_requestacl_python.cc 6.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// Enable this if you use s# variants with PyArg_ParseTuple(), see
// http://docs.python.org/py3k/c-api/arg.html#strings-and-buffers
//#define PY_SSIZE_T_CLEAN

// Python.h needs to be placed at the head of the program file, see:
// http://docs.python.org/py3k/extending/extending.html#a-simple-example
#include <Python.h>

#include <string>
#include <stdexcept>

#include <util/python/pycppwrapper_util.h>

#include <acl/acl.h>
#include <acl/dns.h>

31
#include "acl.h"
32
#include "dns_requestacl_python.h"
33
#include "dns_requestcontext_python.h"
34
35
36
37

using namespace std;
using namespace isc::util::python;
using namespace isc::acl;
38
using namespace isc::acl::python;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using namespace isc::acl::dns;
using namespace isc::acl::dns::python;

//
// Definition of the classes
//

// For each class, we need a struct, a helper functions (init, destroy,
// and static wrappers around the methods we export), a list of methods,
// and a type description

//
// RequestACL
//

// Trivial constructor.
s_RequestACL::s_RequestACL() {}

57
58
59
// Import pydoc text
#include "dns_requestacl_inc.cc"

60
61
namespace {
int
62
RequestACL_init(PyObject*, PyObject*, PyObject*) {
63
    PyErr_SetString(po_ACLError, "RequestACL cannot be directly constructed");
64
65
66
67
    return (-1);
}

void
68
RequestACL_destroy(PyObject* po_self) {
69
    s_RequestACL* const self = static_cast<s_RequestACL*>(po_self);
70
71
72
    self->cppobj.reset();
    Py_TYPE(self)->tp_free(self);
}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

PyObject*
RequestACL_execute(PyObject* po_self, PyObject* args) {
    s_RequestACL* const self = static_cast<s_RequestACL*>(po_self);

    try {
        const s_RequestContext* po_context;
        if (PyArg_ParseTuple(args, "O!", &requestcontext_type, &po_context)) {
            const BasicAction action =
                self->cppobj->execute(*po_context->cppobj);
            return (Py_BuildValue("I", action));
        }
    } catch (const exception& ex) {
        const string ex_what = "Failed to execute ACL: " + string(ex.what());
        PyErr_SetString(po_ACLError, ex_what.c_str());
    } catch (...) {
        PyErr_SetString(PyExc_RuntimeError,
                        "Unexpected exception in executing ACL");
    }

    return (NULL);
}

// This list contains the actual set of functions we have in
// python. Each entry has
// 1. Python method name
// 2. Our static function here
// 3. Argument type
// 4. Documentation
PyMethodDef RequestACL_methods[] = {
    { "execute", RequestACL_execute, METH_VARARGS, RequestACL_execute_doc },
    { NULL, NULL, 0, NULL }
};
106
107
108
109
110
111
112
113
114
115
116
} // end of unnamed namespace

namespace isc {
namespace acl {
namespace dns {
namespace python {
// This defines the complete type for reflection in python and
// parsing of PyObject* to s_RequestACL
// Most of the functions are not actually implemented and NULL here.
PyTypeObject requestacl_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
117
    "isc.acl.dns.RequestACL",
118
119
    sizeof(s_RequestACL),                 // tp_basicsize
    0,                                  // tp_itemsize
120
    RequestACL_destroy,                // tp_dealloc
121
122
123
124
125
126
127
128
    NULL,                               // tp_print
    NULL,                               // tp_getattr
    NULL,                               // tp_setattr
    NULL,                               // tp_reserved
    NULL,                               // tp_repr
    NULL,                               // tp_as_number
    NULL,                               // tp_as_sequence
    NULL,                               // tp_as_mapping
129
    NULL,                               // tp_hash
130
131
132
133
134
135
    NULL,                               // tp_call
    NULL,	                       // tp_str
    NULL,                               // tp_getattro
    NULL,                               // tp_setattro
    NULL,                               // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
136
    RequestACL_doc,
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    NULL,                               // tp_traverse
    NULL,                               // tp_clear
    NULL,				 // tp_richcompare
    0,                                  // tp_weaklistoffset
    NULL,                               // tp_iter
    NULL,                               // tp_iternext
    RequestACL_methods,                   // tp_methods
    NULL,                               // tp_members
    NULL,                               // tp_getset
    NULL,                               // tp_base
    NULL,                               // tp_dict
    NULL,                               // tp_descr_get
    NULL,                               // tp_descr_set
    0,                                  // tp_dictoffset
151
    RequestACL_init,                    // tp_init
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    NULL,                               // tp_alloc
    PyType_GenericNew,                  // tp_new
    NULL,                               // tp_free
    NULL,                               // tp_is_gc
    NULL,                               // tp_bases
    NULL,                               // tp_mro
    NULL,                               // tp_cache
    NULL,                               // tp_subclasses
    NULL,                               // tp_weaklist
    NULL,                               // tp_del
    0                                   // tp_version_tag
};

bool
initModulePart_RequestACL(PyObject* mod) {
    // We initialize the static description object with PyType_Ready(),
    // then add it to the module. This is not just a check! (leaving
    // this out results in segmentation faults)
    if (PyType_Ready(&requestacl_type) < 0) {
        return (false);
    }
    void* p = &requestacl_type;
    if (PyModule_AddObject(mod, "RequestACL", static_cast<PyObject*>(p)) < 0) {
        return (false);
    }
    Py_INCREF(&requestacl_type);

    return (true);
}
} // namespace python
} // namespace dns
} // namespace acl
} // namespace isc