3
* Copyright (C) 2003, 2004, 2005 PreludeIDS Technologies. All Rights Reserved.
4
* Author: Nicolas Delon <nicolas.delon@prelude-ids.com>
6
* This file is part of the Prelude library.
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2, or (at your option)
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; see the file COPYING. If not, write to
20
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
void swig_python_raise_exception(int error)
29
PyObject *exception_class;
32
module = PyImport_ImportModule("prelude");
33
exception_class = PyObject_GetAttrString(module, "PreludeError");
34
exception = PyObject_CallFunction(exception_class, "i", error);
37
PyErr_SetObject(exception_class, exception);
42
Py_DECREF(exception_class);
45
PyObject *swig_python_string(prelude_string_t *string)
48
return PyString_FromStringAndSize(prelude_string_get_string(string), prelude_string_get_len(string));
55
PyObject *swig_python_data(idmef_data_t *data)
57
switch ( idmef_data_get_type(data) ) {
58
case IDMEF_DATA_TYPE_CHAR:
59
case IDMEF_DATA_TYPE_BYTE:
60
return PyString_FromStringAndSize((const char *)idmef_data_get_data(data), 1);
62
case IDMEF_DATA_TYPE_CHAR_STRING:
63
return PyString_FromStringAndSize((const char *)idmef_data_get_data(data), idmef_data_get_len(data) - 1);
65
case IDMEF_DATA_TYPE_BYTE_STRING:
66
return PyString_FromStringAndSize((const char *)idmef_data_get_data(data), idmef_data_get_len(data));
68
case IDMEF_DATA_TYPE_UINT32:
69
return PyLong_FromLongLong(idmef_data_get_uint32(data));
71
case IDMEF_DATA_TYPE_UINT64:
72
return PyLong_FromUnsignedLongLong(idmef_data_get_uint64(data));
74
case IDMEF_DATA_TYPE_FLOAT:
75
return PyFloat_FromDouble((double) idmef_data_get_float(data));
87
* Some generic type typemaps
91
%typemap(out) uint32_t {
92
$result = PyLong_FromUnsignedLong($1);
95
%typemap(out) INTPOINTER * {
97
$result = PyLong_FromLong(*$1);
103
%typemap(out) UINTPOINTER * {
105
$result = PyLong_FromUnsignedLong(*$1);
111
%typemap(out) INT64POINTER * {
113
$result = PyLong_FromLongLong(*$1);
119
%typemap(out) UINT64POINTER * {
121
$result = PyLong_FromUnsignedLongLong(*$1);
128
/* This typemap is used to allow NULL pointers in _get_next_* functions
130
%typemap(in) SWIGTYPE *LISTEDPARAM {
131
if ( $input == Py_None ) {
134
if ( SWIG_ConvertPtr($input, (void **)&$1, $1_descriptor, SWIG_POINTER_EXCEPTION|0) )
140
%typemap(in) const char * {
141
if ( $input == Py_None )
143
else if ( PyString_Check($input) )
144
$1 = PyString_AsString($input);
146
PyErr_Format(PyExc_TypeError,
147
"expected None or string, %s found", $input->ob_type->tp_name);
152
%typemap(freearg) const char * "";
154
%typemap(in) const unsigned char * {
155
if ( PyString_Check($input) )
156
$1 = (unsigned char *) PyString_AsString($input);
159
PyErr_Format(PyExc_TypeError,
160
"expected string, %s found", $input->ob_type->tp_name);
167
%typemap(out) unsigned char *idmef_data_get_byte_string {
169
$result = PyString_FromStringAndSize((char *) $1, idmef_data_get_len(arg1));
171
$result = Py_BuildValue((char *) "");
175
%typemap(in) char **argv {
176
/* Check if is a list */
177
if ( PyList_Check($input) ) {
178
int size = PyList_Size($input);
181
$1 = (char **) malloc((size+1) * sizeof(char *));
182
for ( i = 0; i < size; i++ ) {
183
PyObject *o = PyList_GetItem($input,i);
184
if ( PyString_Check(o) )
185
$1[i] = PyString_AsString(PyList_GetItem($input, i));
187
PyErr_SetString(PyExc_TypeError, "list must contain strings");
194
PyErr_SetString(PyExc_TypeError, "not a list");
200
%typemap(freearg) char **argv {
212
* Prelude specific typemaps
215
Py_BEGIN_ALLOW_THREADS
221
%typemap(in) (char *data, size_t len) {
222
if ( ! PyString_Check($input) ) {
223
PyErr_SetString(PyExc_ValueError, "Expected a string");
227
$1 = PyString_AsString($input);
228
$2 = PyString_Size($input);
231
%typemap(in) (const char *data, size_t len) {
232
if ( ! PyString_Check($input) ) {
233
PyErr_SetString(PyExc_ValueError, "Expected a string");
237
$1 = PyString_AsString($input);
238
$2 = PyString_Size($input);
241
%typemap(in) (unsigned char *data, size_t len) {
242
if ( ! PyString_Check($input) ) {
243
PyErr_SetString(PyExc_ValueError, "Expected a string");
247
$1 = (unsigned char *) PyString_AsString($input);
248
$2 = PyString_Size($input);
251
%typemap(in) (const unsigned char *data, size_t len) {
252
if ( ! PyString_Check($input) ) {
253
PyErr_SetString(PyExc_ValueError, "Expected a string");
257
$1 = (unsigned char *) PyString_AsString($input);
258
$2 = PyString_Size($input);
261
%typemap(in) (const void *data, size_t len) {
262
if ( ! PyString_Check($input) ) {
263
PyErr_SetString(PyExc_ValueError, "Expected a string");
267
$1 = PyString_AsString($input);
268
$2 = PyString_Size($input);
272
%typemap(in) (uint64_t *target_id, size_t size) {
274
$2 = PyList_Size($input);
275
$1 = malloc($2 * sizeof(uint64_t));
276
for ( i = 0; i < $2; i++ ) {
277
PyObject *o = PyList_GetItem($input, i);
278
if ( PyInt_Check(o) )
279
$1[i] = (unsigned long) PyInt_AsLong(o);
281
$1[i] = PyLong_AsUnsignedLongLong(o);
286
%typemap(freearg) (uint64_t *target_id, size_t size) {
291
%typemap(out) FUNC_NO_ERROR {
292
$result = PyInt_FromLong($1);
298
swig_python_raise_exception($1);
301
$result = PyInt_FromLong($1);
307
%typemap(out) int32_t {
308
$result = PyInt_FromLong($1);
311
%typemap(in) const time_t * (time_t tmp) {
312
if ( PyInt_Check($input) )
313
tmp = (time_t) PyInt_AsLong($input);
314
else if ( PyLong_Check($input) )
315
tmp = (time_t) PyLong_AsUnsignedLong($input);
317
PyErr_Format(PyExc_TypeError,
318
"expected int or long, %s found", $input->ob_type->tp_name);
326
%typemap(argout) (uint64_t *source_id, uint32_t *request_id, void **value) {
327
int ret = PyInt_AsLong($result);
328
PyObject *tuple = PyTuple_New(4);
329
PyObject *value_obj = Py_None;
331
PyTuple_SetItem(tuple, 0, PyLong_FromUnsignedLongLong(*$1));
332
PyTuple_SetItem(tuple, 1, PyLong_FromUnsignedLong(*$2));
333
PyTuple_SetItem(tuple, 2, PyInt_FromLong(ret));
337
case PRELUDE_OPTION_REPLY_TYPE_LIST:
338
value_obj = SWIG_NewPointerObj((void *) * $3, SWIG_TypeQuery("prelude_option_t *"), 0);
341
value_obj = PyString_FromString(* $3);
349
PyTuple_SetItem(tuple, 3, value_obj);
355
%typemap(in) int *argc (int tmp) {
356
tmp = PyInt_AsLong($input);
361
%typemap(in) prelude_string_t * {
364
ret = prelude_string_new_dup_fast(&($1), PyString_AsString($input), PyString_Size($input));
366
swig_python_raise_exception(ret);
372
%typemap(out) prelude_string_t * {
373
$result = swig_python_string($1);
377
%typemap(out) idmef_data_t * {
378
$result = swig_python_data($1);
382
%typemap(out) void * idmef_value_get_object {
385
swig_type = swig_idmef_value_get_descriptor(arg1);
390
$result = SWIG_NewPointerObj($1, swig_type, 0);
395
%clear idmef_value_t **ret;
396
%typemap(argout) idmef_value_t **ret {
397
if ( PyInt_AsLong($result) == 0 ) {
401
} else if ( PyInt_AsLong($result) > 0 ) {
402
$result = SWIG_NewPointerObj((void *) * $1, $*1_descriptor, 0);
407
%typemap(in, numinputs=0) prelude_string_t *out {
410
ret = prelude_string_new(&($1));
412
swig_python_raise_exception(ret);
417
%typemap(argout) prelude_string_t *out {
419
$result = PyString_FromStringAndSize(prelude_string_get_string($1), prelude_string_get_len($1));
421
prelude_string_destroy($1);
425
%typemap(in, numinputs=0) prelude_msg_t **outmsg ($*1_type tmp) {
427
$1 = ($1_ltype) &tmp;
430
%typemap(in, numinputs=0) prelude_connection_t **outconn ($*1_type tmp) {
432
$1 = ($1_ltype) &tmp;
436
%typemap(in, numinputs=0) SWIGTYPE **OUTPARAM ($*1_type tmp) {
437
$1 = ($1_ltype) &tmp;
442
%typemap(argout) SWIGTYPE **OUTPARAM {
444
$result = SWIG_NewPointerObj((void *) * $1, $*1_descriptor, 0);
448
%typemap(in) SWIGTYPE *INPARAM {
449
if ( $input == Py_None )
452
if ( SWIG_ConvertPtr($input, (void **)&arg$argnum, $1_descriptor, SWIG_POINTER_EXCEPTION|0) )
460
%apply FUNC_NO_ERROR {
461
int idmef_additional_data_compare,
462
int idmef_reference_compare,
463
int idmef_classification_compare,
464
int idmef_user_id_compare,
465
int idmef_user_compare,
466
int idmef_address_compare,
467
int idmef_process_compare,
468
int idmef_web_service_compare,
469
int idmef_snmp_service_compare,
470
int idmef_service_compare,
471
int idmef_node_compare,
472
int idmef_source_compare,
473
int idmef_file_access_compare,
474
int idmef_inode_compare,
475
int idmef_linkage_compare,
476
int idmef_checksum_compare,
477
int idmef_file_compare,
478
int idmef_target_compare,
479
int idmef_analyzer_compare,
480
int idmef_alertident_compare,
481
int idmef_impact_compare,
482
int idmef_action_compare,
483
int idmef_confidence_compare,
484
int idmef_assessment_compare,
485
int idmef_tool_alert_compare,
486
int idmef_correlation_alert_compare,
487
int idmef_overflow_alert_compare,
488
int idmef_alert_compare,
489
int idmef_heartbeat_compare,
490
int idmef_message_compare,
491
int prelude_string_compare,
492
int idmef_data_compare,
493
int idmef_time_compare,
494
int idmef_path_compare,
495
int idmef_path_ncompare
500
class PreludeError(Exception):
501
def __init__(self, errno, strerror=None):
503
self._strerror = strerror
507
return self._strerror
508
return prelude_strerror(self.errno)