blob: 324674fbb60730e508d26a545d29cac89e67f09c [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* Do not include this directly, use dbg_interactive.i instead */
37
38/****** ROUTING *********/
39
40struct rt_data {
41};
42
43%extend rt_data {
44 rt_data() {
45 struct rt_data * r = NULL;
46 int ret = fd_rtd_init(&r);
47 if (ret != 0) {
48 DI_ERROR(ret, NULL, NULL);
49 return NULL;
50 }
51 return r;
52 }
53 ~rt_data() {
54 struct rt_data *r = self;
55 fd_rtd_free(&r);
56 }
57 %apply (char *STRING, int LENGTH) { (char * peerid, size_t peeridlen) };
58 %apply (char *STRING, int LENGTH) { (char * realm, size_t realmlen) };
59 void add(char * peerid, size_t peeridlen, char * realm, size_t realmlen) {
60 int ret = fd_rtd_candidate_add($self, peerid, peeridlen, realm, realmlen);
61 if (ret != 0) {
62 DI_ERROR(ret, NULL, NULL);
63 }
64 }
65 void remove(char * STRING, size_t LENGTH) {
66 fd_rtd_candidate_del($self, (os0_t)STRING, LENGTH);
67 }
68 int error(char * peerid, size_t peeridlen, char * STRING, size_t LENGTH, uint32_t rcode) {
69 int n;
70 int ret = fd_rtd_error_add($self, peerid, peeridlen, (os0_t)STRING, LENGTH, rcode, NULL, &n);
71 if (ret != 0) {
72 DI_ERROR(ret, NULL, NULL);
73 }
74 return n;
75 }
76 struct fd_list * extract(int score = 0) {
77 struct fd_list * li = NULL;
78 fd_rtd_candidate_extract($self, &li, score);
79 return li;
80 }
81}
82
83
84
85%extend rtd_candidate {
86 void dump() {
87 fd_log_debug("candidate %p", $self);
88 fd_log_debug(" id : %s", $self->diamid);
89 fd_log_debug(" rlm: %s", $self->realm);
90 fd_log_debug(" sc : %d", $self->score);
91 }
92}
93
94
95%{
96/* call it (will be called from a different thread than the interpreter, when message arrives) */
97static int call_the_python_rt_fwd_callback(void * pycb, struct msg **msg) {
98 PyObject *PyMsg;
99 PyObject *cb, *result = NULL;
100 int ret = 0;
101
102 if (!pycb) {
103 fd_log_debug("Internal error: missing the callback!");
104 return ENOTSUP;
105 }
106 cb = pycb;
107
108 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
109 /* Convert the arguments */
110 PyMsg = SWIG_NewPointerObj((void *)*msg, SWIGTYPE_p_msg, 0 );
111
112 /* Call the function */
113 result = PyObject_CallFunction(cb, "(O)", PyMsg);
114
115 /* The result is supposedly composed of: [ ret, *msg ] */
116 if ((result == NULL) || (!PyList_Check(result)) || (PyList_Size(result) != 2)) {
117 fd_log_debug("Error: The Python callback did not return [ ret, msg ].");
118 ret = EINVAL;
119 goto out;
120 }
121
122 /* Convert the return values */
123 if (!SWIG_IsOK(SWIG_AsVal_int(PyList_GetItem(result, 0), &ret))) {
124 fd_log_debug("Error: Cannot convert the first return value to integer.");
125 ret = EINVAL;
126 goto out;
127 }
128 if (ret) {
129 TRACE_DEBUG(INFO, "The Python callback returned the error code %d (%s)", ret, strerror(ret));
130 goto out;
131 }
132
133 if (!SWIG_IsOK(SWIG_ConvertPtr(PyList_GetItem(result, 1), (void *)msg, SWIGTYPE_p_msg, SWIG_POINTER_DISOWN))) {
134 fd_log_debug("Error: Cannot convert the second return value to message.");
135 ret = EINVAL;
136 goto out;
137 }
138
139out:
140 Py_XDECREF(result);
141
142 SWIG_PYTHON_THREAD_END_BLOCK;
143 return ret;
144}
145%}
146
147
148struct fd_rt_fwd_hdl {
149};
150
151%extend fd_rt_fwd_hdl{
152 fd_rt_fwd_hdl(PyObject * PyCb, enum fd_rt_fwd_dir dir) {
153 struct fd_rt_fwd_hdl * r = NULL;
154 int ret;
155
156 Py_XINCREF(PyCb);
157
158 ret = fd_rt_fwd_register( call_the_python_rt_fwd_callback, PyCb, dir, &r );
159 if (ret != 0) {
160 DI_ERROR(ret, NULL, NULL);
161 return NULL;
162 }
163 return r;
164 }
165
166 ~fd_rt_fwd_hdl() {
167 PyObject * func;
168 int ret = fd_rt_fwd_unregister ( $self, (void *) &func );
169 if (ret != 0) {
170 DI_ERROR(ret, NULL, NULL);
171 return;
172 }
173 Py_XDECREF(func);
174 return;
175 }
176}
177
178
179%{
180/* call it (will be called from a different thread than the interpreter, when message arrives) */
181static int call_the_python_rt_out_callback(void * pycb, struct msg **msg, struct fd_list * candidates) {
182 PyObject *PyMsg, *PyCands;
183 PyObject *cb, *result = NULL;
184 int ret = 0;
185
186 if (!pycb) {
187 fd_log_debug("Internal error: missing the callback!");
188 return ENOTSUP;
189 }
190 cb = pycb;
191
192 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
193 /* Convert the arguments */
194 PyMsg = SWIG_NewPointerObj((void *)*msg, SWIGTYPE_p_msg, 0 );
195 PyCands = SWIG_NewPointerObj((void *)candidates, SWIGTYPE_p_fd_list, 0 );
196
197 /* Call the function */
198 result = PyObject_CallFunction(cb, "(OO)", PyMsg, PyCands);
199
200 /* The result is supposedly composed of: [ ret, *msg ] */
201 if (result == NULL){
202 fd_log_debug("Error: The Python callback raised an exception.");
203 ret = EINVAL;
204 goto out;
205 }
206
207 /* Convert the return values */
208 if (!SWIG_IsOK(SWIG_AsVal_int(result, &ret))) {
209 fd_log_debug("Error: Cannot convert the return value to integer.");
210 ret = EINVAL;
211 goto out;
212 }
213out:
214 Py_XDECREF(result);
215
216 SWIG_PYTHON_THREAD_END_BLOCK;
217 return ret;
218}
219%}
220
221
222struct fd_rt_out_hdl {
223};
224
225%extend fd_rt_out_hdl{
226 fd_rt_out_hdl(PyObject * PyCb, int priority = 0) {
227 struct fd_rt_out_hdl * r = NULL;
228 int ret;
229
230 Py_XINCREF(PyCb);
231
232 ret = fd_rt_out_register( call_the_python_rt_out_callback, PyCb, priority, &r );
233 if (ret != 0) {
234 DI_ERROR(ret, NULL, NULL);
235 return NULL;
236 }
237 return r;
238 }
239
240 ~fd_rt_out_hdl() {
241 PyObject * func;
242 int ret = fd_rt_out_unregister ( $self, (void *) &func );
243 if (ret != 0) {
244 DI_ERROR(ret, NULL, NULL);
245 return;
246 }
247 Py_XDECREF(func);
248 return;
249 }
250}
251