1 /*****
2 *
3 * Copyright (C) 2005-2015 CS-SI. All Rights Reserved.
4 * Author: Yoann Vandoorselaere <yoannv@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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 *****/
23 
24 #if defined(SWIGPYTHON) || defined(SWIGLUA)
25 %module prelude
26 #else
27 %module Prelude
28 #endif
29 
30 %warnfilter(509);
31 %feature("nothread", "1");
32 
33 %include "std_string.i"
34 %include "std_vector.i"
35 %include "exception.i"
36 
37 %{
38 #pragma GCC diagnostic ignored "-Wunused-variable"
39 
40 #include <list>
41 #include <sstream>
42 
43 #include "prelude.hxx"
44 #include "prelude-log.hxx"
45 #include "prelude-error.hxx"
46 #include "prelude-connection.hxx"
47 #include "prelude-connection-pool.hxx"
48 #include "prelude-client-profile.hxx"
49 #include "prelude-client.hxx"
50 #include "prelude-client-easy.hxx"
51 #include "idmef-criteria.hxx"
52 #include "idmef-value.hxx"
53 #include "idmef-path.hxx"
54 #include "idmef-time.hxx"
55 #include "idmef.hxx"
56 
57 using namespace Prelude;
58 %}
59 
60 
61 typedef char int8_t;
62 typedef unsigned char uint8_t;
63 
64 typedef short int16_t;
65 typedef unsigned short uint16_t;
66 
67 typedef int int32_t;
68 typedef unsigned int uint32_t;
69 
70 typedef long long int64_t;
71 typedef unsigned long long uint64_t;
72 
73 %ignore prelude_error_t;
74 typedef signed int prelude_error_t;
75 
76 %ignore idmef_class_id_t;
77 typedef int idmef_class_id_t;
78 
79 typedef long long time_t;
80 
81 
82 %exception {
83         try {
84                 $action
catch(Prelude::PreludeError & e)85         } catch(Prelude::PreludeError &e) {
86                 SWIG_exception(SWIG_RuntimeError, e.what());
87                 SWIG_fail;
88         }
89 }
90 
91 
92 #ifdef SWIGPERL
93 %include perl/libpreludecpp-perl.i
94 #endif
95 
96 #ifdef SWIGPYTHON
97 %include python/libpreludecpp-python.i
98 #endif
99 
100 #ifdef SWIGRUBY
101 %include ruby/libpreludecpp-ruby.i
102 #endif
103 
104 #ifdef SWIGLUA
105 %include lua/libpreludecpp-lua.i
106 #endif
107 
108 
109 %ignore operator <<(std::ostream &os, const Prelude::IDMEF &idmef);
110 %ignore operator >>(std::istream &is, const Prelude::IDMEF &idmef);
111 
112 
113 %template() std::vector<std::string>;
114 %template() std::vector<Prelude::IDMEF>;
115 %template() std::vector<Prelude::IDMEFValue>;
116 %template() std::vector<Prelude::Connection>;
117 
118 #ifdef SWIG_COMPILE_LIBPRELUDE
119 %extend Prelude::IDMEF {
get(const char * path)120         Prelude::IDMEFValue get(const char *path) {
121                 Prelude::IDMEFValue value;
122                 Prelude::IDMEFPath ipath = Prelude::IDMEFPath(*self, path);
123 
124                 value = ipath.get(*self);
125                 if ( value.isNull() && ipath.isAmbiguous() ) {
126                         std::vector<Prelude::IDMEFValue> v;
127                         return Prelude::IDMEFValue(v);
128                 }
129 
130                 return value;
131         }
132 }
133 
134 %extend Prelude::IDMEFPath {
get(Prelude::IDMEF & message)135         Prelude::IDMEFValue get(Prelude::IDMEF &message) {
136                 Prelude::IDMEFValue value;
137 
138                 value = self->get(message);
139                 if ( value.isNull() && self->isAmbiguous() ) {
140                         std::vector<Prelude::IDMEFValue> v;
141                         return Prelude::IDMEFValue(v);
142                 }
143 
144                 return value;
145         }
146 }
147 #endif
148 
149 %ignore Prelude::IDMEF::get;
150 %ignore Prelude::IDMEFPath::get;
151 
152 
153 %fragment("SWIG_FromBytePtrAndSize", "header", fragment="SWIG_FromCharPtrAndSize") %{
154 #ifndef SWIG_FromBytePtrAndSize
155 # define SWIG_FromBytePtrAndSize(arg, len) SWIG_FromCharPtrAndSize(arg, len)
156 #endif
157 %}
158 
159 
160 %fragment("IDMEFValue_to_SWIG", "header", fragment="SWIG_From_double",
161                                           fragment="SWIG_From_float",
162                                           fragment="SWIG_From_int", fragment="SWIG_From_unsigned_SS_int",
163                                           fragment="SWIG_From_long_SS_long", fragment="SWIG_From_unsigned_SS_long_SS_long",
164                                           fragment="SWIG_FromCharPtr", fragment="SWIG_FromCharPtrAndSize", fragment="SWIG_FromBytePtrAndSize",
165                                           fragment="IDMEFValueList_to_SWIG") {
166 
IDMEFValue_to_SWIG(TARGET_LANGUAGE_SELF self,const Prelude::IDMEFValue & result,void * extra,TARGET_LANGUAGE_OUTPUT_TYPE ret)167 int IDMEFValue_to_SWIG(TARGET_LANGUAGE_SELF self, const Prelude::IDMEFValue &result, void *extra, TARGET_LANGUAGE_OUTPUT_TYPE ret)
168 {
169         idmef_value_t *value = result;
170         Prelude::IDMEFValue::IDMEFValueTypeEnum type = result.getType();
171 
172         if ( type == Prelude::IDMEFValue::TYPE_STRING ) {
173                 prelude_string_t *str = idmef_value_get_string(value);
174                 *ret = SWIG_FromCharPtrAndSize(prelude_string_get_string(str), prelude_string_get_len(str));
175         }
176 
177         else if ( type == Prelude::IDMEFValue::TYPE_INT8 )
178                 *ret = SWIG_From_int(idmef_value_get_int8(value));
179 
180         else if ( type == Prelude::IDMEFValue::TYPE_UINT8 )
181                 *ret = SWIG_From_unsigned_SS_int(idmef_value_get_uint8(value));
182 
183         else if ( type == Prelude::IDMEFValue::TYPE_INT16 )
184                 *ret = SWIG_From_int(idmef_value_get_int16(value));
185 
186         else if ( type == Prelude::IDMEFValue::TYPE_UINT16 )
187                 *ret = SWIG_From_unsigned_SS_int(idmef_value_get_uint16(value));
188 
189         else if ( type == Prelude::IDMEFValue::TYPE_INT32 )
190                 *ret = SWIG_From_int(idmef_value_get_int32(value));
191 
192         else if ( type == Prelude::IDMEFValue::TYPE_UINT32 )
193                 *ret = SWIG_From_unsigned_SS_int(idmef_value_get_uint32(value));
194 
195         else if ( type == Prelude::IDMEFValue::TYPE_INT64 )
196                 *ret = SWIG_From_long_SS_long(idmef_value_get_int64(value));
197 
198         else if ( type == Prelude::IDMEFValue::TYPE_UINT64 )
199                 *ret = SWIG_From_unsigned_SS_long_SS_long(idmef_value_get_uint64(value));
200 
201         else if ( type == Prelude::IDMEFValue::TYPE_FLOAT )
202                 *ret = SWIG_From_float(idmef_value_get_float(value));
203 
204         else if ( type == Prelude::IDMEFValue::TYPE_DOUBLE )
205                 *ret = SWIG_From_double(idmef_value_get_double(value));
206 
207         else if ( type == Prelude::IDMEFValue::TYPE_ENUM ) {
208                 const char *s = idmef_class_enum_to_string(idmef_value_get_class(value), idmef_value_get_enum(value));
209                 *ret = SWIG_FromCharPtr(s);
210         }
211 
212         else if ( type == Prelude::IDMEFValue::TYPE_TIME ) {
213                 Prelude::IDMEFTime t = result;
214                 *ret = SWIG_NewPointerObj(new Prelude::IDMEFTime(t), $descriptor(Prelude::IDMEFTime *), 1);
215         }
216 
217         else if ( type == Prelude::IDMEFValue::TYPE_LIST )
218                 *ret = IDMEFValueList_to_SWIG(self, result, extra);
219 
220         else if ( type == Prelude::IDMEFValue::TYPE_DATA ) {
221                 idmef_data_t *d = idmef_value_get_data(value);
222                 idmef_data_type_t t = idmef_data_get_type(d);
223 
224                 if ( t == IDMEF_DATA_TYPE_BYTE || t == IDMEF_DATA_TYPE_BYTE_STRING )
225                         *ret = SWIG_FromBytePtrAndSize((const char *)idmef_data_get_data(d), idmef_data_get_len(d));
226 
227                 else if ( t == IDMEF_DATA_TYPE_CHAR )
228                         *ret = SWIG_FromCharPtrAndSize((const char *)idmef_data_get_data(d), idmef_data_get_len(d));
229 
230                 else if ( t == IDMEF_DATA_TYPE_CHAR_STRING )
231                         *ret = SWIG_FromCharPtrAndSize((const char *)idmef_data_get_data(d), idmef_data_get_len(d) - 1);
232 
233                 else if ( t == IDMEF_DATA_TYPE_FLOAT )
234                         *ret = SWIG_From_float(idmef_data_get_float(d));
235 
236                 else if ( t == IDMEF_DATA_TYPE_UINT32 || IDMEF_DATA_TYPE_INT )
237                         *ret = SWIG_From_unsigned_SS_long_SS_long(idmef_data_get_int(d));
238         }
239 
240         else if ( type == Prelude::IDMEFValue::TYPE_CLASS ) {
241                 idmef_object_t *obj = (idmef_object_t *) idmef_value_get_object(value);
242                 *ret = SWIG_NewPointerObj(new Prelude::IDMEF(idmef_object_ref(obj)), $descriptor(Prelude::IDMEF *), 1);
243         }
244 
245         else return -1;
246 
247         return 1;
248 }
249 }
250 
251 %ignore Prelude::IDMEFValue::operator const char*() const;
252 %ignore Prelude::IDMEFValue::operator std::vector<IDMEFValue>() const;
253 %ignore Prelude::IDMEFValue::operator Prelude::IDMEFTime() const;
254 %ignore Prelude::IDMEFValue::operator int8_t() const;
255 %ignore Prelude::IDMEFValue::operator uint8_t() const;
256 %ignore Prelude::IDMEFValue::operator int16_t() const;
257 %ignore Prelude::IDMEFValue::operator uint16_t() const;
258 %ignore Prelude::IDMEFValue::operator int32_t() const;
259 %ignore Prelude::IDMEFValue::operator uint32_t() const;
260 %ignore Prelude::IDMEFValue::operator int64_t() const;
261 %ignore Prelude::IDMEFValue::operator uint64_t() const;
262 %ignore Prelude::IDMEFValue::operator float() const;
263 %ignore Prelude::IDMEFValue::operator double() const;
264 
265 /*
266  * Force SWIG to use the IDMEFValue * version of the Set() function,
267  * so that the user might provide NULL IDMEFValue. Force usage of the
268  * std::string value, for binary data.
269  */
270 %ignore Prelude::IDMEF::set(char const *, int8_t);
271 %ignore Prelude::IDMEF::set(char const *, uint8_t);
272 %ignore Prelude::IDMEF::set(char const *, int16_t);
273 %ignore Prelude::IDMEF::set(char const *, uint16_t);
274 %ignore Prelude::IDMEF::set(char const *, char const *);
275 %ignore Prelude::IDMEF::set(char const *, Prelude::IDMEFValue &value);
276 %ignore Prelude::IDMEFPath::set(Prelude::IDMEF &, int8_t) const;
277 %ignore Prelude::IDMEFPath::set(Prelude::IDMEF &, uint8_t) const;
278 %ignore Prelude::IDMEFPath::set(Prelude::IDMEF &, int16_t) const;
279 %ignore Prelude::IDMEFPath::set(Prelude::IDMEF &, uint16_t) const;
280 %ignore Prelude::IDMEFPath::set(Prelude::IDMEF &, char const *) const;
281 %ignore Prelude::IDMEFPath::set(Prelude::IDMEF &, Prelude::IDMEFValue &) const;
282 %ignore Prelude::IDMEFValue::IDMEFValue(char const *);
283 
284 %ignore idmef_path_t;
285 %ignore idmef_object_t;
286 %ignore idmef_criteria_t;
287 %ignore prelude_client_t;
288 %ignore prelude_client_profile_t;
289 %ignore prelude_connection_t;
290 %ignore prelude_connection_pool_t;
291 %ignore operator idmef_path_t *() const;
292 %ignore operator idmef_criteria_t *() const;
293 %ignore operator idmef_object_t *() const;
294 %ignore operator prelude_connection_t *();
295 %ignore operator prelude_connection_pool_t *();
296 %ignore operator idmef_message_t *() const;
297 %ignore operator idmef_time_t *() const;
298 %ignore operator idmef_value_t *() const;
299 %ignore operator prelude_client_profile_t *() const;
300 
301 /*
302  * We need to unlock the interpreter lock before calling certain methods
303  * because they might acquire internal libprelude mutex that may also be
304  * acquired undirectly through the libprelude asynchronous stack.
305  *
306  * [Thread 2]: Libprelude async stack
307  * -> Lock internal mutexX
308  *    -> prelude_log()
309  *       -> SWIG/C log callback
310  *          -> Wait on Interpreter lock [DEADLOCK]
311  *             -> Python logging callback (never called)
312  *
313  * [Thread 1] ConnectionPool::Recv()
314  *  -> Acquire Interpreter lock
315  *      *** At this time, thread 2 lock internal mutexX
316  *      -> Wait on internal mutexX [DEADLOCK]
317  *
318  * In this situation, [Thread 1] hold the Interpreter lock and is
319  * waiting on mutexX, which itself cannot be released by [Thread 2]
320  * until [Thread 1] unlock the Interpreter lock.
321  *
322  * One rule to prevent deadlock is to always acquire mutex in the same
323  * order. We thus need to make sure the interpreter lock is released
324  * before calling C++ method that are susceptible to lock a mutex that
325  * is also triggered from the asynchronous interface.
326  *
327  * Note that we are not releasing the Interpreter lock in all C++ call,
328  * because it come at a performance cost, so we only try to do it when
329  * needed.
330  */
331 
332 #ifdef SWIG_COMPILE_LIBPRELUDE
333 %feature("exceptionclass") Prelude::PreludeError;
334 %feature("kwargs") Prelude::ClientEasy::ClientEasy;
335 %feature("kwargs") Prelude::Client::recvIDMEF;
336 %feature("kwargs") Prelude::IDMEFClass::getPath;
337 %feature("kwargs") Prelude::IDMEFPath::getClass;
338 %feature("kwargs") Prelude::IDMEFPath::getValueType;
339 %feature("kwargs") Prelude::IDMEFPath::setIndex;
340 %feature("kwargs") Prelude::IDMEFPath::getIndex;
341 %feature("kwargs") Prelude::IDMEFPath::undefineIndex;
342 %feature("kwargs") Prelude::IDMEFPath::compare;
343 %feature("kwargs") Prelude::IDMEFPath::getName;
344 %feature("kwargs") Prelude::IDMEFPath::isList;
345 
346 %include prelude.hxx
347 %include prelude-client-profile.hxx
348 
349 %feature("nothread", "0");
350 %include prelude-connection.hxx
351 %include prelude-connection-pool.hxx
352 %include prelude-client.hxx
353 %feature("nothread", "1");
354 
355 %include prelude-log.hxx
356 %include prelude-error.hxx
357 %include prelude-client-easy.hxx
358 %include idmef-criteria.hxx
359 %include idmef-value.hxx
360 %include idmef-path.hxx
361 %include idmef-time.hxx
362 %include idmef-class.hxx
363 %include idmef.hxx
364 #endif
365 
366