mirror of
https://github.com/NLnetLabs/unbound.git
synced 2026-08-28 10:50:05 +02:00
python module compiles.
git-svn-id: file:///svn/unbound/trunk@1557 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
+21
-17
@@ -16,6 +16,7 @@
|
||||
#include "util/config_file.h"
|
||||
#include "util/data/msgreply.h"
|
||||
#include "util/data/packed_rrset.h"
|
||||
#include "util/data/dname.h"
|
||||
#include "util/storage/lruhash.h"
|
||||
#include "services/cache/dns.h"
|
||||
%}
|
||||
@@ -137,11 +138,11 @@ struct query_info {
|
||||
};
|
||||
|
||||
PyObject* _get_qname(struct query_info* q) {
|
||||
return PyString_FromStringAndSize(q->qname, q->qname_len);
|
||||
return PyString_FromStringAndSize((char*)q->qname, q->qname_len);
|
||||
}
|
||||
|
||||
PyObject* _get_qname_components(struct query_info* q) {
|
||||
return GetNameAsLabelList(q->qname, q->qname_len);
|
||||
return GetNameAsLabelList((const char*)q->qname, q->qname_len);
|
||||
}
|
||||
%}
|
||||
|
||||
@@ -149,7 +150,7 @@ struct query_info {
|
||||
PyObject* dnameAsStr(const char* dname) {
|
||||
char buf[LDNS_MAX_DOMAINLEN+1];
|
||||
buf[0] = '\0';
|
||||
dname_str(dname, buf);
|
||||
dname_str((uint8_t*)dname, buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
%}
|
||||
@@ -199,10 +200,10 @@ uint16_t ntohs(uint16_t netshort);
|
||||
|
||||
%inline %{
|
||||
PyObject* _get_dname(struct packed_rrset_key* k) {
|
||||
return PyString_FromStringAndSize(k->dname, k->dname_len);
|
||||
return PyString_FromStringAndSize((char*)k->dname, k->dname_len);
|
||||
}
|
||||
PyObject* _get_dname_components(struct packed_rrset_key* k) {
|
||||
return GetNameAsLabelList(k->dname, k->dname_len);
|
||||
return GetNameAsLabelList((char*)k->dname, k->dname_len);
|
||||
}
|
||||
%}
|
||||
|
||||
@@ -286,23 +287,28 @@ struct packed_rrset_data {
|
||||
|
||||
%inline %{
|
||||
PyObject* _get_data_rr_len(struct packed_rrset_data* d, int idx) {
|
||||
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count)))
|
||||
if ((d != NULL) && (idx >= 0) &&
|
||||
((size_t)idx < (d->count+d->rrsig_count)))
|
||||
return PyInt_FromLong(d->rr_len[idx]);
|
||||
return Py_None;
|
||||
}
|
||||
void _set_data_rr_ttl(struct packed_rrset_data* d, int idx, uint32_t ttl)
|
||||
{
|
||||
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count)))
|
||||
if ((d != NULL) && (idx >= 0) &&
|
||||
((size_t)idx < (d->count+d->rrsig_count)))
|
||||
d->rr_ttl[idx] = ttl;
|
||||
}
|
||||
PyObject* _get_data_rr_ttl(struct packed_rrset_data* d, int idx) {
|
||||
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count)))
|
||||
if ((d != NULL) && (idx >= 0) &&
|
||||
((size_t)idx < (d->count+d->rrsig_count)))
|
||||
return PyInt_FromLong(d->rr_ttl[idx]);
|
||||
return Py_None;
|
||||
}
|
||||
PyObject* _get_data_rr_data(struct packed_rrset_data* d, int idx) {
|
||||
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count)))
|
||||
return PyString_FromStringAndSize(d->rr_data[idx],d->rr_len[idx]);
|
||||
if ((d != NULL) && (idx >= 0) &&
|
||||
((size_t)idx < (d->count+d->rrsig_count)))
|
||||
return PyString_FromStringAndSize((char*)d->rr_data[idx],
|
||||
d->rr_len[idx]);
|
||||
return Py_None;
|
||||
}
|
||||
%}
|
||||
@@ -346,8 +352,8 @@ struct reply_info {
|
||||
};
|
||||
|
||||
struct rrset_ref {
|
||||
struct ub_packed_rrset_key* key;
|
||||
rrset_id_t id;
|
||||
struct ub_packed_rrset_key* key;
|
||||
rrset_id_t id;
|
||||
};
|
||||
|
||||
struct dns_msg {
|
||||
@@ -369,13 +375,13 @@ struct dns_msg {
|
||||
|
||||
%inline %{
|
||||
struct ub_packed_rrset_key* _rrset_rrsets_get(struct reply_info* r, int idx) {
|
||||
if ((r != NULL) && (idx >= 0) && (idx < r->rrset_count))
|
||||
if ((r != NULL) && (idx >= 0) && ((size_t)idx < r->rrset_count))
|
||||
return r->rrsets[idx];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct rrset_ref* _rrset_ref_get(struct reply_info* r, int idx) {
|
||||
if ((r != NULL) && (idx >= 0) && (idx < r->rrset_count)) {
|
||||
if ((r != NULL) && (idx >= 0) && ((size_t)idx < r->rrset_count)) {
|
||||
//printf("_rrset_ref_get: %lX key:%lX\n", r->ref + idx, r->ref[idx].key);
|
||||
return &(r->ref[idx]);
|
||||
// return &(r->ref[idx]);
|
||||
@@ -705,9 +711,7 @@ int set_return_msg(struct module_qstate* qstate,
|
||||
ldns_pkt* pkt = 0;
|
||||
ldns_status status;
|
||||
ldns_rr_list* rr_list1 = 0,*rr_list2 = 0,*rr_list3 = 0,*rr_list4 = 0;
|
||||
ldns_buffer *qb = 0;
|
||||
struct dns_msg* m = 0;
|
||||
struct msg_parse* p = 0;
|
||||
ldns_buffer *qb = 0;
|
||||
int res = 1;
|
||||
|
||||
if ((!checkList(question)) || (!checkList(answer)) || (!checkList(authority)) || (!checkList(additional)))
|
||||
|
||||
+41
-33
@@ -40,12 +40,14 @@
|
||||
#include "pythonmod_utils.h"
|
||||
#include <Python.h>
|
||||
|
||||
// Generated
|
||||
/* Generated */
|
||||
#include "pythonmod/interface.h"
|
||||
|
||||
int pythonmod_init(struct module_env* env, int id)
|
||||
{
|
||||
// Initialize module
|
||||
/* Initialize module */
|
||||
FILE* script_py = NULL;
|
||||
PyObject* py_cfg, *res;
|
||||
struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env));
|
||||
if (!pe)
|
||||
{
|
||||
@@ -60,14 +62,14 @@ int pythonmod_init(struct module_env* env, int id)
|
||||
pe->data = NULL;
|
||||
pe->qstate = NULL;
|
||||
|
||||
// Initialize module
|
||||
/* Initialize module */
|
||||
if ((pe->fname = env->cfg->python_script) == NULL)
|
||||
{
|
||||
log_err("pythonmod: no script given.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Initialize Python libraries
|
||||
/* Initialize Python libraries */
|
||||
if (!Py_IsInitialized())
|
||||
{
|
||||
Py_SetProgramName("unbound");
|
||||
@@ -78,7 +80,7 @@ int pythonmod_init(struct module_env* env, int id)
|
||||
SWIG_init();
|
||||
}
|
||||
|
||||
// Initialize Python
|
||||
/* Initialize Python */
|
||||
PyRun_SimpleString("import sys \n");
|
||||
PyRun_SimpleString("sys.path.append('.') \n");
|
||||
PyRun_SimpleString("sys.path.append('"RUN_DIR"') \n");
|
||||
@@ -88,22 +90,21 @@ int pythonmod_init(struct module_env* env, int id)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check Python file load
|
||||
FILE* script_py = NULL;
|
||||
/* Check Python file load */
|
||||
if ((script_py = fopen(pe->fname, "r")) == NULL)
|
||||
{
|
||||
log_err("pythonmod: can't open file %s for reading", pe->fname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Load file
|
||||
/* Load file */
|
||||
pe->module = PyImport_AddModule("__main__");
|
||||
pe->dict = PyModule_GetDict(pe->module);
|
||||
pe->data = Py_None;
|
||||
Py_INCREF(pe->data);
|
||||
PyModule_AddObject(pe->module, "mod_env", pe->data);
|
||||
|
||||
//TODO: deallocation of pe->... if an error occurs
|
||||
/* TODO: deallocation of pe->... if an error occurs */
|
||||
|
||||
if (PyRun_SimpleFile(script_py, pe->fname) < 0)
|
||||
{
|
||||
@@ -135,8 +136,8 @@ int pythonmod_init(struct module_env* env, int id)
|
||||
}
|
||||
|
||||
PyEval_AcquireLock();
|
||||
PyObject* py_cfg = SWIG_NewPointerObj((void*) env->cfg, SWIGTYPE_p_config_file, 0);
|
||||
PyObject* res = PyObject_CallFunction(pe->func_init, "iO", id, py_cfg);
|
||||
py_cfg = SWIG_NewPointerObj((void*) env->cfg, SWIGTYPE_p_config_file, 0);
|
||||
res = PyObject_CallFunction(pe->func_init, "iO", id, py_cfg);
|
||||
if (PyErr_Occurred())
|
||||
{
|
||||
log_err("pythonmod: Exception occurred in function init");
|
||||
@@ -156,25 +157,27 @@ void pythonmod_deinit(struct module_env* env, int id)
|
||||
if(pe == NULL)
|
||||
return;
|
||||
|
||||
// Free Python resources
|
||||
/* Free Python resources */
|
||||
if(pe->module != NULL)
|
||||
{
|
||||
// Deinit module
|
||||
PyObject* res;
|
||||
|
||||
/* Deinit module */
|
||||
PyEval_AcquireLock();
|
||||
PyObject* res = PyObject_CallFunction(pe->func_deinit, "i", id);
|
||||
res = PyObject_CallFunction(pe->func_deinit, "i", id);
|
||||
if (PyErr_Occurred()) {
|
||||
log_err("pythonmod: Exception occurred in function deinit");
|
||||
PyErr_Print();
|
||||
}
|
||||
// Free result if any
|
||||
/* Free result if any */
|
||||
Py_XDECREF(res);
|
||||
// Free shared data if any
|
||||
/* Free shared data if any */
|
||||
Py_XDECREF(pe->data);
|
||||
|
||||
Py_Finalize();
|
||||
}
|
||||
|
||||
// Module is deallocated in Python
|
||||
/* Module is deallocated in Python */
|
||||
env->modinfo[id] = NULL;
|
||||
}
|
||||
|
||||
@@ -182,15 +185,17 @@ void pythonmod_inform_super(struct module_qstate* qstate, int id, struct module_
|
||||
{
|
||||
struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id];
|
||||
struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id];
|
||||
PyObject* py_qstate, *py_sqstate, *res;
|
||||
|
||||
log_query_info(VERB_ALGO, "pythonmod: inform_super, sub is", &qstate->qinfo);
|
||||
log_query_info(VERB_ALGO, "super is", &super->qinfo);
|
||||
|
||||
PyObject* py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
|
||||
PyObject* py_sqstate = SWIG_NewPointerObj((void*) super, SWIGTYPE_p_module_qstate, 0);
|
||||
py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
|
||||
py_sqstate = SWIG_NewPointerObj((void*) super, SWIGTYPE_p_module_qstate, 0);
|
||||
|
||||
PyEval_AcquireLock();
|
||||
PyObject* res = PyObject_CallFunction(pe->func_inform, "iOOO", id, py_qstate, py_sqstate, pq->data);
|
||||
res = PyObject_CallFunction(pe->func_inform, "iOOO", id, py_qstate,
|
||||
py_sqstate, pq->data);
|
||||
|
||||
if (PyErr_Occurred())
|
||||
{
|
||||
@@ -211,27 +216,30 @@ void pythonmod_inform_super(struct module_qstate* qstate, int id, struct module_
|
||||
PyEval_ReleaseLock();
|
||||
}
|
||||
|
||||
void pythonmod_operate(struct module_qstate* qstate, enum module_ev event, int id, struct outbound_entry* outbound)
|
||||
void pythonmod_operate(struct module_qstate* qstate, enum module_ev event,
|
||||
int id, struct outbound_entry* ATTR_UNUSED(outbound))
|
||||
{
|
||||
struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id];
|
||||
struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id];
|
||||
PyObject* py_qstate, *res;
|
||||
|
||||
if ( pq == NULL)
|
||||
{
|
||||
// create qstate
|
||||
/* create qstate */
|
||||
pq = qstate->minfo[id] = malloc(sizeof(struct pythonmod_qstate));
|
||||
|
||||
//Initialize per query data
|
||||
/* Initialize per query data */
|
||||
pq->data = Py_None;
|
||||
Py_INCREF(pq->data);
|
||||
}
|
||||
|
||||
// Lock Python
|
||||
/* Lock Python */
|
||||
PyEval_AcquireLock();
|
||||
|
||||
// Call operate
|
||||
PyObject* py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
|
||||
PyObject* res = PyObject_CallFunction(pe->func_operate, "iiOO", id, (int) event, py_qstate, pq->data);
|
||||
/* Call operate */
|
||||
py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
|
||||
res = PyObject_CallFunction(pe->func_operate, "iiOO", id, (int) event,
|
||||
py_qstate, pq->data);
|
||||
if (PyErr_Occurred())
|
||||
{
|
||||
log_err("pythonmod: Exception occurred in function operate, event: %s", strmodulevent(event));
|
||||
@@ -246,22 +254,22 @@ void pythonmod_operate(struct module_qstate* qstate, enum module_ev event, int i
|
||||
Py_XDECREF(res);
|
||||
Py_XDECREF(py_qstate);
|
||||
|
||||
// Unlock Python
|
||||
/* Unlock Python */
|
||||
PyEval_ReleaseLock();
|
||||
|
||||
}
|
||||
|
||||
void pythonmod_clear(struct module_qstate* qstate, int id)
|
||||
{
|
||||
struct pythonmod_qstate* pq;
|
||||
if (qstate == NULL)
|
||||
return;
|
||||
|
||||
struct pythonmod_qstate* pq = qstate->minfo[id];
|
||||
pq = (struct pythonmod_qstate*)qstate->minfo[id];
|
||||
log_info("pythonmod: clear, id: %d, pq:%lX", id, (unsigned long int)pq);
|
||||
if(pq != NULL)
|
||||
{
|
||||
Py_DECREF(pq->data);
|
||||
// Free qstate
|
||||
/* Free qstate */
|
||||
free(pq);
|
||||
}
|
||||
|
||||
@@ -278,8 +286,8 @@ size_t pythonmod_get_mem(struct module_env* env, int id)
|
||||
}
|
||||
|
||||
/**
|
||||
* The module function block
|
||||
*/
|
||||
* The module function block
|
||||
*/
|
||||
static struct module_func_block pythonmod_block = {
|
||||
"python",
|
||||
&pythonmod_init, &pythonmod_deinit, &pythonmod_operate, &pythonmod_inform_super,
|
||||
|
||||
@@ -40,6 +40,9 @@
|
||||
#include <Python.h>
|
||||
|
||||
struct PyObject;
|
||||
#if S_SPLINT_S
|
||||
typedef struct PyObject PyObject;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Global state for the module.
|
||||
|
||||
@@ -33,11 +33,14 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "config.h"
|
||||
#include "util/module.h"
|
||||
#include "util/net_help.h"
|
||||
#include "services/cache/dns.h"
|
||||
#include "util/data/msgparse.h"
|
||||
#include "util/data/msgreply.h"
|
||||
#include "util/storage/slabhash.h"
|
||||
#include "util/regional.h"
|
||||
#include <Python.h>
|
||||
|
||||
/** Store the reply_info and query_info pair in message cache (qstate->msg_cache) */
|
||||
|
||||
Reference in New Issue
Block a user