1 /* This interface file is processed by SWIG to create a python wrapper interface to freeDiameter framework. */
2 %module fDpy
3 %begin %{
4 /*********************************************************************************************************
5 * Software License Agreement (BSD License)                                                               *
6 * Author: Sebastien Decugis <sdecugis@freediameter.net>							 *
7 *													 *
8 * Copyright (c) 2013, WIDE Project and NICT								 *
9 * All rights reserved.											 *
10 * 													 *
11 * Redistribution and use of this software in source and binary forms, with or without modification, are  *
12 * permitted provided that the following conditions are met:						 *
13 * 													 *
14 * * Redistributions of source code must retain the above 						 *
15 *   copyright notice, this list of conditions and the 							 *
16 *   following disclaimer.										 *
17 *    													 *
18 * * Redistributions in binary form must reproduce the above 						 *
19 *   copyright notice, this list of conditions and the 							 *
20 *   following disclaimer in the documentation and/or other						 *
21 *   materials provided with the distribution.								 *
22 * 													 *
23 * * Neither the name of the WIDE Project or NICT nor the 						 *
24 *   names of its contributors may be used to endorse or 						 *
25 *   promote products derived from this software without 						 *
26 *   specific prior written permission of WIDE Project and 						 *
27 *   NICT.												 *
28 * 													 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
37 *********************************************************************************************************/
38 %}
39 
40 %{
41 /* This text is included in the generated wrapper verbatim */
42 #define SWIG
43 #include <freeDiameter/extension.h>
44 %}
45 
46 
47 /* Include standard types & functions used in freeDiameter headers */
48 %include <stdint.i>
49 //%include <cdata.i>
50 %include <cstring.i>
51 %include <typemaps.i>
52 
53 
54 /* Inline functions seems to give problems to SWIG -- just remove the inline definition */
55 %define __inline__
56 %enddef
57 
58 
59 /* Make some global-variables read-only (mainly to avoid warnings) */
60 %immutable fd_g_config;
61 %immutable peer_state_str;
62 
63 #pragma SWIG nowarn=451
64 
65 /*****************
66  *  Exceptions  *
67 *****************/
68 %{
69 /* This is not thread-safe etc. but it should work /most of the time/. */
70 static int wrapper_errno;
71 static PyObject* wrapper_errno_py;
72 static const char * wrapper_error_txt; /* if NULL, use strerror(errno) */
73 #define DI_ERROR(code, pycode, str) {	\
74 	fd_log_debug("[dbg_interactive] ERROR: %s: %s", __PRETTY_FUNCTION__, str ? str : strerror(code)); \
75 	wrapper_errno = code;		\
76 	wrapper_errno_py = pycode;	\
77 	wrapper_error_txt = str;	\
78 }
79 
80 #define DI_ERROR_MALLOC	\
81 	 DI_ERROR(ENOMEM, PyExc_MemoryError, NULL)
82 
83 %}
84 
85 %exception {
86 	/* reset the errno */
87 	wrapper_errno = 0;
88 	/* Call the function  -- it will use DI_ERROR macro in case of error */
89 	$action
90 	/* Now, test for error */
91 	if (wrapper_errno) {
92 		const char * str = wrapper_error_txt ? wrapper_error_txt : strerror(wrapper_errno);
93 		PyObject * exc = wrapper_errno_py;
94 		if (!exc) {
95 			switch (wrapper_errno) {
96 				case ENOMEM: exc = PyExc_MemoryError; break;
97 				case EINVAL: exc = PyExc_ValueError; break;
98 				default: exc = PyExc_RuntimeError;
99 			}
100 		}
101 		SWIG_PYTHON_THREAD_BEGIN_BLOCK;
102 		PyErr_SetString(exc, str);
103 		SWIG_fail;
104 		SWIG_PYTHON_THREAD_END_BLOCK;
105 	}
106 }
107 
108 
109 /***********************************
110  Some types & typemaps for usability
111  ***********************************/
112 
113 %apply (char *STRING, size_t LENGTH) { ( char * string, size_t len ) };
114 %apply (char *STRING, size_t LENGTH) { ( uint8_t * string, size_t len ) };
115 
116 /* Generic typemap for functions that create something */
117 %typemap(in, numinputs=0,noblock=1) SWIGTYPE ** OUTPUT (void *temp = NULL) {
118 	$1 = (void *)&temp;
119 }
120 %typemap(argout,noblock=1) SWIGTYPE ** OUTPUT {
121 	%append_output(SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
122 }
123 
124 /* Case of the fd_*_dump functions */
125 %typemap(in,noblock=1,numinputs=0) (char ** buf, size_t *len, size_t *offset) ($*1_ltype temp = NULL, $*2_ltype tempn = 0) {
126 	$1 = &temp; $2 = &tempn; $3 = NULL;
127 }
128 %typemap(freearg,match="in") (char ** buf, size_t *len, size_t *offset) "";
129 %typemap(argout,noblock=1,fragment="SWIG_FromCharPtr")(char ** buf, size_t *len, size_t *offset) {
130   if (*$1) {
131     %append_output(SWIG_FromCharPtr(*$1));
132     free(*$1);
133   }
134 }
135 
136 /* Typemap to return a boolean value as output parameter */
137 %typemap(in, numinputs=0,noblock=1) int * BOOL_OUT (int temp) {
138 	$1 = &temp;
139 }
140 %typemap(argout,noblock=1) int * BOOL_OUT {
141 	PyObject * r;
142 	if (*$1)
143 		r = Py_True;
144 	else
145 		r = Py_False;
146 	Py_XINCREF(r);
147 	%append_output(r);
148 }
149 
150 /* To allow passing callback functions defined in python */
151 %typemap(in) PyObject *PyCb {
152 	if (!$input || ($input == Py_None)) {
153 		$1 = NULL;
154 	} else {
155 		if (!PyCallable_Check($input)) {
156 			PyErr_SetString(PyExc_TypeError, "Need a callable object!");
157 			SWIG_fail;
158 		}
159 		$1 = $input;
160 	}
161 }
162 
163 %{
164 /* Forward declaration for the peers module */
165 static void fd_add_cb(struct peer_info *peer, void *data);
166 
167 /* This one gives problems when included from the header file */
168 void fd_log_va( int, const char *, va_list);
fd_log_deprecated(int level,const char * format,...)169 void fd_log_deprecated( int level, const char *format, ... ) MARK_DEPRECATED
170 {
171 	va_list ap;
172 	va_start(ap, format);
173 	fd_log_va(level, format, ap);
174 	va_end(ap);
175 }
176 
177 %}
178 
179 /* Overwrite declaration to apply typemaps */
180 int fd_sess_fromsid ( uint8_t * string, size_t len, struct session ** OUTPUT, int * BOOL_OUT);
181 
182 
183 
184 /*********************************************************
185  Now, create wrappers for (almost) all objects from fD API
186  *********************************************************/
187 %include "freeDiameter/freeDiameter-host.h"
188 %include "freeDiameter/libfdproto.h"
189 %include "freeDiameter/libfdcore.h"
190 
191 /* Most of the functions from the API are not directly usable "as is".
192 See the specific following files and the dbg_interactive.py.sample file
193 for more usable python-style versions.
194 */
195 
196 %include "lists.i"
197 %include "dictionary.i"
198 %include "sessions.i"
199 %include "routing.i"
200 %include "messages.i"
201 %include "dispatch.i"
202 %include "queues.i"
203 
204 %include "peers.i"
205 %include "events.i"
206 %include "endpoints.i"
207 %include "hooks.i"
208 
209 %include "posix.i"
210