1 %module lion
2 %header %{
3 #include "lion.h"
4 %}
5 %rename(lion_set_handler) lion_set_handler_python;
6 %inline %{
7 
lion_set_handler_python(lion_t * handle,PyObject * event_handler)8 lion_handler_t lion_set_handler_python(lion_t *handle, PyObject *event_handler){
9    PyObject *old=NULL;
10 
11    if (event_handler != NULL){
12       if(!PyCallable_Check(event_handler)){
13          PyErr_SetString(PyExc_TypeError, "parameter must be callable");
14          return NULL;
15       }
16       Py_XINCREF(event_handler);
17       old = (PyObject *)lion_set_handler(handle,(lion_handler_t)event_handler);
18       Py_XDECREF(old);
19       return (lion_handler_t)old;
20    }
21    PyErr_SetString(PyExc_ValueError,"parameter must not be null");
22    return NULL;
23 }
24 
lion_userinput(lion_t * handle,void * user_data,int status,int size,char * line)25 int lion_userinput( lion_t *handle, void *user_data, int status, int size, char *line){
26    PyObject *py_handle;
27    PyObject *result;
28    PyObject *arglist;
29 
30 	// py_handle = SWIG_NewPointerObj((handle), SWIGTYPE_p_lion_t, 0);
31    py_handle = SWIG_NewPointerObj((handle), SWIGTYPE_p_lion_t, 0);
32    arglist = Py_BuildValue("N s i i s",py_handle,user_data,status,size,line);
33    result = PyEval_CallObject((PyObject*)lion_get_handler(handle),arglist);
34    Py_DECREF(py_handle);
35    Py_DECREF(arglist);
36    if (result == NULL){
37       return (int)NULL;
38    }else{
39       Py_DECREF(result);
40       return 0;
41    }
42 }
43 
44 %}
45 
46 typedef int (*lion_handler_t)(lion_t *,
47 							  void *, int, int, char *);
48 
49 enum net_status {
50 
51 	// Pipe events
52 	LION_PIPE_FAILED,              // 0
53 	LION_PIPE_RUNNING,             // 1
54 	LION_PIPE_EXIT,                // 2
55 
56 
57 	// File events
58 	LION_FILE_OPEN,                // 3
59 	LION_FILE_CLOSED,              // 4
60 	LION_FILE_FAILED,              // 5
61 
62 
63 	// Networking events
64 	LION_CONNECTION_CLOSED,        // 6
65 	LION_CONNECTION_CONNECTED,     // 7
66 	LION_CONNECTION_NEW,           // 8
67 	LION_CONNECTION_LOST,          // 9
68 
69 	// Generic events
70 	LION_BUFFER_EMPTY,             // 10
71 	LION_BUFFER_USED,              // 11
72 	LION_BINARY,                   // 12
73 	LION_INPUT                     // 13
74 
75 #ifdef WITH_SSL
76 	,
77 	LION_CONNECTION_SECURE_FAILED, // 14
78 	LION_CONNECTION_SECURE_ENABLED // 15
79 #endif
80 
81 };
82 
83 
84 enum lion_type {
85 	LION_TYPE_NONE = 0,
86 	LION_TYPE_SOCKET,
87 	LION_TYPE_FILE,
88 	LION_TYPE_PIPE,
89 	LION_TYPE_UDP
90 };
91 
92 enum lion_flags {
93 
94 	LION_FLAG_NONE = 0,
95 	LION_FLAG_FULFILL = 1,
96 	LION_FLAG_EXCLUSIVE = 2
97 
98 };
99 
100 // Automatically updated ever loop from time();
101 extern time_t lion_global_time;
102 
103 #ifndef _lion_userinput
104 #ifndef DEBUG
105 // RELEASE build will just call the events
106 #define _lion_userinput(ha, ud, st, si, li)  (ha->event_handler) ?   \
107                   ha->event_handler(ha, ud, st, si, li) : \
108                   lion_userinput(ha, ud, st, si, li);
109 #else // DEBUG
110 // DEBUG build keeps a reference counter.
111 #define _lion_userinput(ha, ud, st, si, li)      { ha->in_event++; \
112                                              (ha->event_handler) ?   \
113                       ha->event_handler(ha, ud, st, si, li) : \
114                           lion_userinput(ha, ud, st, si, li); \
115                                                   ha->in_event--; }
116 #endif // DEBUG
117 #endif // _lion_userinput
118 
119 // Initialisation functions
120 
121 extern int           lion_init          ( void);
122 extern void          lion_free          ( void);
123 extern void          lion_compress_level( int level );
124 extern void          lion_buffersize    ( unsigned int );
125 
126 // Top-level functions
127 
128 extern int           lion_poll          ( int utimeout, int timeout );
129 extern lion_t *lion_find          ( int (*)(lion_t *, void *, void *),
130 								   void *, void * );
131 
132 //
133 // GENERIC FUNCTIONS
134 //
135 
136 extern void          lion_setbinary     ( lion_t * );
137     // Close ensures flushing of data, disconnect just drops.
138 extern void          lion_close         ( lion_t *node );
139 extern void          lion_disconnect    ( lion_t * );
140 extern int           lion_printf        ( lion_t *node, char const *fmt, ... );
141     // You are better off using lion_send than lion_output, in that send
142     // has the logic for compression, and output is called from it.
143 extern int           lion_send          ( lion_t *node, char *, unsigned int );
144 extern int           lion_output        ( lion_t *node, char *buffer,
145 								   unsigned int len );
146 extern void          lion_disable_read  ( lion_t * );
147 extern void          lion_enable_read   ( lion_t * );
148 extern void          lion_set_userdata  ( lion_t *, void * );
149 extern void         *lion_get_userdata  ( lion_t * );
150 extern enum lion_type lion_gettype      ( lion_t * );
151     // You probably shouldn't use this fileno call.
152 extern int           lion_fileno        ( lion_t *);
153 
154 extern void          lion_get_bytes     ( lion_t *node, bytes_t *in,
155 								   bytes_t *out );
156 extern time_t        lion_get_duration  ( lion_t *node );
157 extern void          lion_get_cps       ( lion_t *node, float *in, float *out );
158 
159 extern void          lion_rate_in       ( lion_t *node, int cps );
160 extern void          lion_rate_out      ( lion_t *node, int cps );
161 
162 extern unsigned long lion_addr          ( char * );
163 
164 //
165 // NETWORKING I/O FUNCTIONS
166 //
167 
168 extern int           lion_isconnected   ( lion_t * );
169 extern lion_t *lion_connect       ( char *host, int port, unsigned long iface,
170 								   int iport,
171 								   int lion_flags, void *user_data );
172 extern lion_t *lion_listen        ( int *port, unsigned long iface,
173 								   int lion_flags, void *user_data );
174 extern lion_t *lion_accept        ( lion_t *node, int close_old,
175 								   int lion_flags, void *user_data,
176 								   unsigned long *remhost, int *remport );
177 
178 extern void          lion_getsockname   ( lion_t *node,
179 								   unsigned long *addr,
180 								   int *port);
181 extern void          lion_getpeername   ( lion_t *node,
182 								   unsigned long *addr,
183 								   int *port);
184 
185 extern char         *lion_ftp_port      ( unsigned long addr, int port );
186 extern int           lion_ftp_pasv      ( char *, unsigned long *, int * );
187 
188 extern char         *lion_ntoa          ( unsigned long );
189 
190 // Additional SSL extensions
191 
192 enum ssl_type { LION_SSL_OFF, LION_SSL_CLIENT, LION_SSL_SERVER,
193 				LION_SSL_FILE };
194 typedef enum ssl_type ssl_type_t;
195 
196 #ifdef WITH_SSL
197 
198 // Enable ssl for a connection
199 extern int           lion_ssl_set       ( lion_t *, ssl_type_t );
200 extern int           lion_ssl_enabled   ( lion_t * );
201 
202 // These should be set before calling net_init if you want
203 // them considered. But they are optional, unless you want those features.
204 extern void          lion_ssl_ciphers   ( char *);
205 extern void          lion_ssl_rsafile   ( char *);
206 extern void          lion_ssl_egdfile   ( char *);
207 
208 // For file hashing, set the key.
209 extern void          lion_ssl_setkey    ( lion_t *, char *, unsigned int );
210 extern void          lion_ssl_clearkey  ( lion_t * );
211 
212 #endif
213 
214 //
215 // FILE I/O FUNCTIONS
216 //
217 
218 // open will return direct errors, _pending will always return a valid node
219 // but post events for any failures.
220 extern lion_t *lion_open          ( char *file, int flags, mode_t modes,
221 								   int lion_flags, void *user_data );
222 
223 //
224 // PIPE I/O FUNCTIONS
225 //
226 extern lion_t *lion_fork          ( int (*start_address)(lion_t *,void *, void *),
227 								   int flags, void *user_data, void *arg );
228 extern lion_t *lion_execve        ( char *base, char **argv, char **envp,
229 								   int with_stderr, int lion_flags,
230 								   void *user_data );
231 
232 extern lion_t *lion_system        ( char *cmd, int with_stderr,
233 								   int lion_flags, void *user_data );
234 
235 extern void          lion_want_returncode( lion_t *node );
236 extern void          lion_exitchild     ( int retcode);
237 
238 //
239 // MISC FUNCTIONS
240 //
241 
242 extern lion_handler_t lion_set_handler  ( lion_t *handle,
243 								   lion_handler_t event_handler );
244 
245 extern lion_handler_t lion_get_handler  ( lion_t *handle );
246 
247 //
248 // GROUP FUNCTIONS
249 //
250 extern int           lion_group_new     ( void );
251 extern void          lion_group_free    ( int );
252 extern void          lion_group_add     ( lion_t *, int );
253 extern void          lion_group_remove  ( lion_t *, int );
254 extern void          lion_group_rate_in   ( int, int );
255 extern void          lion_group_rate_out  ( int, int );
256 
257 extern void          lion_global_rate_in  ( int );
258 extern void          lion_global_rate_out ( int );
259 
260 //
261 // UDP FUNCTIONS
262 //
263 
264 extern lion_t *lion_udp_new       ( int *port, unsigned long iface,
265 								   int lion_flags, void *user_data );
266 
267 extern lion_t *lion_udp_bind      ( lion_t *, unsigned long host,
268 								   int port, void *user_data );
269 extern lion_t *lion_udp_bind_handle( lion_t *, lion_t *,
270 									void *user_data );
271 
272 extern int           lion_add_multicast ( lion_t *, unsigned long );
273 extern int           lion_drop_multicast( lion_t *, unsigned long );
274 
275