~ubuntu-branches/ubuntu/trusty/libprelude/trusty

« back to all changes in this revision

Viewing changes to bindings/low-level/python/libprelude-python.i

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-04-29 11:31:50 UTC
  • mfrom: (1.1.12 upstream) (2.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090429113150-qw9oxc1e50ldljck
Tags: 0.9.22-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Build-Depend on libltdl7-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****
 
2
*
 
3
* Copyright (C) 2003, 2004, 2005 PreludeIDS Technologies. All Rights Reserved.
 
4
* Author: Nicolas Delon <nicolas.delon@prelude-ids.com>
 
5
*
 
6
* This file is part of the Prelude library.
 
7
*
 
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)
 
11
* any later version.
 
12
*
 
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.
 
17
*
 
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.
 
21
*
 
22
*****/
 
23
 
 
24
 
 
25
%{
 
26
void swig_python_raise_exception(int error)
 
27
{
 
28
        PyObject *module;
 
29
        PyObject *exception_class;
 
30
        PyObject *exception;
 
31
 
 
32
        module = PyImport_ImportModule("prelude");
 
33
        exception_class = PyObject_GetAttrString(module, "PreludeError");
 
34
        exception = PyObject_CallFunction(exception_class, "i", error);
 
35
 
 
36
        if ( exception ) {
 
37
                PyErr_SetObject(exception_class, exception);
 
38
                Py_DECREF(exception);
 
39
        }
 
40
 
 
41
        Py_DECREF(module);
 
42
        Py_DECREF(exception_class);
 
43
}
 
44
 
 
45
PyObject *swig_python_string(prelude_string_t *string)
 
46
{
 
47
        if ( string )
 
48
                return PyString_FromStringAndSize(prelude_string_get_string(string), prelude_string_get_len(string));
 
49
        else {
 
50
                Py_INCREF(Py_None);
 
51
                return Py_None;
 
52
        }
 
53
}
 
54
 
 
55
PyObject *swig_python_data(idmef_data_t *data)
 
56
{
 
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);
 
61
 
 
62
        case IDMEF_DATA_TYPE_CHAR_STRING: 
 
63
                return PyString_FromStringAndSize((const char *)idmef_data_get_data(data), idmef_data_get_len(data) - 1);
 
64
 
 
65
        case IDMEF_DATA_TYPE_BYTE_STRING:
 
66
                return PyString_FromStringAndSize((const char *)idmef_data_get_data(data), idmef_data_get_len(data));
 
67
 
 
68
        case IDMEF_DATA_TYPE_UINT32:
 
69
                return PyLong_FromLongLong(idmef_data_get_uint32(data));
 
70
 
 
71
        case IDMEF_DATA_TYPE_UINT64:
 
72
                return PyLong_FromUnsignedLongLong(idmef_data_get_uint64(data));
 
73
 
 
74
        case IDMEF_DATA_TYPE_FLOAT:
 
75
                return PyFloat_FromDouble((double) idmef_data_get_float(data));
 
76
 
 
77
        default:
 
78
                Py_INCREF(Py_None);
 
79
                return Py_None;
 
80
        }
 
81
}
 
82
 
 
83
%}
 
84
 
 
85
 
 
86
/**
 
87
 * Some generic type typemaps
 
88
 */
 
89
 
 
90
 
 
91
%typemap(out) uint32_t {
 
92
        $result = PyLong_FromUnsignedLong($1);
 
93
};
 
94
 
 
95
%typemap(out) INTPOINTER * {
 
96
        if ($1 != NULL) {
 
97
                $result = PyLong_FromLong(*$1);
 
98
        } else {
 
99
                $result = Py_None;
 
100
        }
 
101
};
 
102
 
 
103
%typemap(out) UINTPOINTER * {
 
104
        if ($1 != NULL) {
 
105
                $result = PyLong_FromUnsignedLong(*$1);
 
106
        } else {
 
107
                $result = Py_None;
 
108
        }
 
109
};
 
110
 
 
111
%typemap(out) INT64POINTER * {
 
112
        if ($1 != NULL) {
 
113
                $result = PyLong_FromLongLong(*$1);
 
114
        } else {
 
115
                $result = Py_None;
 
116
        }
 
117
};
 
118
 
 
119
%typemap(out) UINT64POINTER * {
 
120
        if ($1 != NULL) {
 
121
                $result = PyLong_FromUnsignedLongLong(*$1);
 
122
        } else {
 
123
                $result = Py_None;
 
124
        }
 
125
};
 
126
 
 
127
 
 
128
/* This typemap is used to allow NULL pointers in _get_next_* functions
 
129
 */
 
130
%typemap(in) SWIGTYPE *LISTEDPARAM {
 
131
        if ( $input == Py_None ) {
 
132
                $1 = NULL;
 
133
        } else {
 
134
                if ( SWIG_ConvertPtr($input, (void **)&$1, $1_descriptor, SWIG_POINTER_EXCEPTION|0) )
 
135
                        return NULL;
 
136
        }
 
137
};
 
138
 
 
139
 
 
140
%typemap(in) const char * {
 
141
        if ( $input == Py_None )
 
142
                $1 = NULL;
 
143
        else if ( PyString_Check($input) )
 
144
                $1 = PyString_AsString($input);
 
145
        else {
 
146
                PyErr_Format(PyExc_TypeError,
 
147
                             "expected None or string, %s found", $input->ob_type->tp_name);
 
148
                return NULL;
 
149
        }
 
150
};
 
151
 
 
152
%typemap(freearg) const char * "";
 
153
 
 
154
%typemap(in) const unsigned char * {
 
155
        if ( PyString_Check($input) )
 
156
                $1 = (unsigned char *) PyString_AsString($input);
 
157
 
 
158
        else {
 
159
                PyErr_Format(PyExc_TypeError,
 
160
                             "expected string, %s found", $input->ob_type->tp_name);
 
161
                return NULL;
 
162
        }
 
163
                
 
164
};
 
165
 
 
166
 
 
167
%typemap(out) unsigned char *idmef_data_get_byte_string {
 
168
        if ( $1 ) 
 
169
                $result = PyString_FromStringAndSize((char *) $1, idmef_data_get_len(arg1));
 
170
        else
 
171
                $result = Py_BuildValue((char *) "");
 
172
};
 
173
 
 
174
 
 
175
%typemap(in) char **argv {
 
176
        /* Check if is a list */
 
177
        if ( PyList_Check($input) ) {
 
178
                int size = PyList_Size($input);
 
179
                int i = 0;
 
180
 
 
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));
 
186
                        else {
 
187
                                PyErr_SetString(PyExc_TypeError, "list must contain strings");
 
188
                                free($1);
 
189
                                return NULL;
 
190
                        }
 
191
                }
 
192
                $1[i] = 0;
 
193
        } else {
 
194
                PyErr_SetString(PyExc_TypeError, "not a list");
 
195
                return NULL;
 
196
        }
 
197
};
 
198
 
 
199
 
 
200
%typemap(freearg) char **argv {
 
201
        free($1);
 
202
};
 
203
 
 
204
 
 
205
 
 
206
 
 
207
 
 
208
 
 
209
 
 
210
 
 
211
/**
 
212
 * Prelude specific typemaps
 
213
 */
 
214
%exception {
 
215
   Py_BEGIN_ALLOW_THREADS
 
216
   $function
 
217
   Py_END_ALLOW_THREADS
 
218
}
 
219
 
 
220
 
 
221
%typemap(in) (char *data, size_t len) {
 
222
        if ( ! PyString_Check($input) ) {
 
223
                PyErr_SetString(PyExc_ValueError, "Expected a string");
 
224
                return NULL;
 
225
        }
 
226
 
 
227
        $1 = PyString_AsString($input);
 
228
        $2 = PyString_Size($input);
 
229
};
 
230
 
 
231
%typemap(in) (const char *data, size_t len) {
 
232
        if ( ! PyString_Check($input) ) {
 
233
                PyErr_SetString(PyExc_ValueError, "Expected a string");
 
234
                return NULL;
 
235
        }
 
236
 
 
237
        $1 = PyString_AsString($input);
 
238
        $2 = PyString_Size($input);
 
239
};
 
240
 
 
241
%typemap(in) (unsigned char *data, size_t len) {
 
242
        if ( ! PyString_Check($input) ) {
 
243
                PyErr_SetString(PyExc_ValueError, "Expected a string");
 
244
                return NULL;
 
245
        }
 
246
 
 
247
        $1 = (unsigned char *) PyString_AsString($input);
 
248
        $2 = PyString_Size($input);
 
249
};
 
250
 
 
251
%typemap(in) (const unsigned char *data, size_t len) {
 
252
        if ( ! PyString_Check($input) ) {
 
253
                PyErr_SetString(PyExc_ValueError, "Expected a string");
 
254
                return NULL;
 
255
        }
 
256
 
 
257
        $1 = (unsigned char *) PyString_AsString($input);
 
258
        $2 = PyString_Size($input);     
 
259
};
 
260
 
 
261
%typemap(in) (const void *data, size_t len) {
 
262
        if ( ! PyString_Check($input) ) {
 
263
                PyErr_SetString(PyExc_ValueError, "Expected a string");
 
264
                return NULL;
 
265
        }
 
266
 
 
267
        $1 = PyString_AsString($input);
 
268
        $2 = PyString_Size($input);
 
269
};
 
270
 
 
271
 
 
272
%typemap(in) (uint64_t *target_id, size_t size) {
 
273
        int i;
 
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);
 
280
                else
 
281
                        $1[i] = PyLong_AsUnsignedLongLong(o);
 
282
        }
 
283
};
 
284
 
 
285
 
 
286
%typemap(freearg) (uint64_t *target_id, size_t size) {
 
287
        free($1);
 
288
};
 
289
 
 
290
 
 
291
%typemap(out) FUNC_NO_ERROR {
 
292
        $result = PyInt_FromLong($1);
 
293
};
 
294
 
 
295
 
 
296
%typemap(out) int {
 
297
        if ( $1 < 0 ) {
 
298
                swig_python_raise_exception($1);
 
299
                $result = NULL;
 
300
        } else {
 
301
                $result = PyInt_FromLong($1);
 
302
        }
 
303
};
 
304
 
 
305
 
 
306
 
 
307
%typemap(out) int32_t {
 
308
        $result = PyInt_FromLong($1);
 
309
};
 
310
 
 
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);
 
316
        else {
 
317
                PyErr_Format(PyExc_TypeError,
 
318
                             "expected int or long, %s found", $input->ob_type->tp_name);
 
319
                return NULL;
 
320
        }
 
321
 
 
322
        $1 = &tmp;
 
323
};
 
324
 
 
325
 
 
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;
 
330
 
 
331
        PyTuple_SetItem(tuple, 0, PyLong_FromUnsignedLongLong(*$1));
 
332
        PyTuple_SetItem(tuple, 1, PyLong_FromUnsignedLong(*$2));
 
333
        PyTuple_SetItem(tuple, 2, PyInt_FromLong(ret));
 
334
 
 
335
        if ( *$3 ) {
 
336
                switch ( ret ) {
 
337
                case PRELUDE_OPTION_REPLY_TYPE_LIST:
 
338
                        value_obj = SWIG_NewPointerObj((void *) * $3, SWIG_TypeQuery("prelude_option_t *"), 0);
 
339
                        break;
 
340
                default:
 
341
                        value_obj = PyString_FromString(* $3);
 
342
                        break;
 
343
                }
 
344
        } else {
 
345
                value_obj = Py_None;
 
346
                Py_INCREF(Py_None);
 
347
        }
 
348
 
 
349
        PyTuple_SetItem(tuple, 3, value_obj);
 
350
 
 
351
        $result = tuple;
 
352
};
 
353
 
 
354
 
 
355
%typemap(in) int *argc (int tmp) {
 
356
        tmp = PyInt_AsLong($input);
 
357
        $1 = &tmp;
 
358
};
 
359
 
 
360
 
 
361
%typemap(in) prelude_string_t * {
 
362
       int ret;
 
363
 
 
364
       ret = prelude_string_new_dup_fast(&($1), PyString_AsString($input), PyString_Size($input));
 
365
       if ( ret < 0 ) {
 
366
               swig_python_raise_exception(ret);
 
367
               return NULL;
 
368
       }
 
369
};
 
370
 
 
371
 
 
372
%typemap(out) prelude_string_t * {
 
373
        $result = swig_python_string($1);
 
374
};
 
375
 
 
376
 
 
377
%typemap(out) idmef_data_t * {
 
378
        $result = swig_python_data($1);
 
379
};
 
380
 
 
381
 
 
382
%typemap(out) void * idmef_value_get_object {
 
383
        void *swig_type;
 
384
 
 
385
        swig_type = swig_idmef_value_get_descriptor(arg1);
 
386
        if ( ! swig_type ) {
 
387
                $result = Py_None;
 
388
                Py_INCREF(Py_None);
 
389
        } else {
 
390
                $result = SWIG_NewPointerObj($1, swig_type, 0);
 
391
        }
 
392
};
 
393
 
 
394
 
 
395
%clear idmef_value_t **ret;
 
396
%typemap(argout) idmef_value_t **ret {
 
397
        if ( PyInt_AsLong($result) == 0 ) {
 
398
                $result = Py_None;
 
399
                Py_INCREF(Py_None);
 
400
 
 
401
        } else if ( PyInt_AsLong($result) > 0 ) {
 
402
                $result = SWIG_NewPointerObj((void *) * $1, $*1_descriptor, 0);
 
403
        }
 
404
};
 
405
 
 
406
 
 
407
%typemap(in, numinputs=0) prelude_string_t *out {
 
408
        int ret;
 
409
        
 
410
        ret = prelude_string_new(&($1));
 
411
        if ( ret < 0 ) {
 
412
                swig_python_raise_exception(ret);
 
413
                return NULL;
 
414
        }
 
415
};
 
416
 
 
417
%typemap(argout) prelude_string_t *out {
 
418
        if ( result >= 0 )
 
419
                $result = PyString_FromStringAndSize(prelude_string_get_string($1), prelude_string_get_len($1));
 
420
 
 
421
        prelude_string_destroy($1);
 
422
};
 
423
 
 
424
 
 
425
%typemap(in, numinputs=0) prelude_msg_t **outmsg ($*1_type tmp) {
 
426
        tmp = NULL;
 
427
        $1 = ($1_ltype) &tmp;
 
428
};
 
429
 
 
430
%typemap(in, numinputs=0) prelude_connection_t **outconn ($*1_type tmp) {
 
431
        tmp = NULL;
 
432
        $1 = ($1_ltype) &tmp;
 
433
};
 
434
 
 
435
 
 
436
%typemap(in, numinputs=0) SWIGTYPE **OUTPARAM ($*1_type tmp) {
 
437
        $1 = ($1_ltype) &tmp;
 
438
};
 
439
 
 
440
 
 
441
 
 
442
%typemap(argout) SWIGTYPE **OUTPARAM {
 
443
        if ( result >= 0 )
 
444
                $result = SWIG_NewPointerObj((void *) * $1, $*1_descriptor, 0);
 
445
};
 
446
 
 
447
 
 
448
%typemap(in) SWIGTYPE *INPARAM {
 
449
        if ( $input == Py_None )
 
450
                return NULL;
 
451
 
 
452
        if ( SWIG_ConvertPtr($input, (void **)&arg$argnum, $1_descriptor, SWIG_POINTER_EXCEPTION|0) )
 
453
                return NULL;
 
454
}
 
455
 
 
456
 
 
457
/*
 
458
 *
 
459
 */
 
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
 
496
};
 
497
 
 
498
 
 
499
%pythoncode %{
 
500
class PreludeError(Exception):
 
501
    def __init__(self, errno, strerror=None):
 
502
        self.errno = errno
 
503
        self._strerror = strerror
 
504
    
 
505
    def __str__(self):
 
506
        if self._strerror:
 
507
            return self._strerror
 
508
        return prelude_strerror(self.errno)
 
509
%}