Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 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 | /****** HOOKS *********/ |
| 39 | |
| 40 | |
| 41 | /* Functions to handle the PMD */ |
| 42 | %{ |
| 43 | |
| 44 | struct fd_hook_permsgdata { |
| 45 | PyObject *PyPML; |
| 46 | }; |
| 47 | |
| 48 | static void init_permsgdata (struct fd_hook_permsgdata * pmd) { |
| 49 | /* The PMD is None by default */ |
| 50 | Py_INCREF(Py_None); |
| 51 | pmd->PyPML = Py_None; |
| 52 | } |
| 53 | |
| 54 | static void fini_permsgdata (struct fd_hook_permsgdata * pmd) { |
| 55 | Py_DECREF(pmd->PyPML); |
| 56 | } |
| 57 | |
| 58 | %} |
| 59 | |
| 60 | struct fd_hook_data_hdl { |
| 61 | }; |
| 62 | |
| 63 | %nodefaultctor fd_hook_data_hdl; |
| 64 | %extend fd_hook_data_hdl { |
| 65 | fd_hook_data_hdl() { |
| 66 | struct fd_hook_data_hdl * hdl = NULL; |
| 67 | int ret; |
| 68 | |
| 69 | ret = fd_hook_data_register ( sizeof(struct fd_hook_permsgdata), init_permsgdata, fini_permsgdata, &hdl ); |
| 70 | if (ret != 0) { |
| 71 | DI_ERROR(ret, NULL, NULL); |
| 72 | return NULL; |
| 73 | } |
| 74 | return hdl; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* Now the hook itself */ |
| 80 | %{ |
| 81 | static void call_the_python_hook_callback(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata) { |
| 82 | PyObject *cb, *result = NULL; |
| 83 | PyObject *PyType, *PyMsg, *PyPeer, *PyOther, *PyOldPmd; |
| 84 | |
| 85 | if (!regdata) { |
| 86 | LOG_E("Internal error: missing the callback!"); |
| 87 | return; |
| 88 | } |
| 89 | cb = regdata; |
| 90 | |
| 91 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
| 92 | /* Convert the arguments */ |
| 93 | PyType = PyLong_FromLong(type); |
| 94 | Py_INCREF(PyType); |
| 95 | |
| 96 | PyMsg = SWIG_NewPointerObj((void *) msg, SWIGTYPE_p_msg, 0 ); |
| 97 | |
| 98 | PyPeer = SWIG_NewPointerObj((void *) peer, SWIGTYPE_p_peer_hdr, 0 ); |
| 99 | if (other == NULL) { |
| 100 | PyOther=Py_None; |
| 101 | Py_INCREF(Py_None); |
| 102 | } else { |
| 103 | switch (type) { |
| 104 | case HOOK_DATA_RECEIVED: |
| 105 | PyOther= SWIG_NewPointerObj( other, SWIGTYPE_p_fd_cnx_rcvdata, 0 ); |
| 106 | break; |
| 107 | |
| 108 | case HOOK_MESSAGE_RECEIVED: |
| 109 | case HOOK_MESSAGE_ROUTING_ERROR: |
| 110 | case HOOK_MESSAGE_DROPPED: |
| 111 | case HOOK_PEER_CONNECT_FAILED: |
| 112 | PyOther= SWIG_NewPointerObj( other, SWIGTYPE_p_char, 0 ); |
| 113 | break; |
| 114 | |
| 115 | case HOOK_MESSAGE_PARSING_ERROR: |
| 116 | if (msg) { |
| 117 | PyOther= SWIG_NewPointerObj( other, SWIGTYPE_p_char, 0 ); |
| 118 | } else { |
| 119 | PyOther= SWIG_NewPointerObj( other, SWIGTYPE_p_fd_cnx_rcvdata, 0 ); |
| 120 | } |
| 121 | break; |
| 122 | default: |
| 123 | /* In other cases, other should be NULL */ |
| 124 | LOG_E("Internal error: got a value of *other"); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | |
| 129 | if (pmd == NULL) { |
| 130 | Py_INCREF(Py_None); |
| 131 | PyOldPmd=Py_None; |
| 132 | } else { |
| 133 | PyOldPmd=pmd->PyPML; |
| 134 | } |
| 135 | |
| 136 | /* Call the function */ |
| 137 | result = PyObject_CallFunction(cb, "(OOOOO)", PyType, PyMsg, PyPeer, PyOther, PyOldPmd); |
| 138 | |
| 139 | SWIG_PYTHON_THREAD_END_BLOCK; |
| 140 | if (pmd == NULL) |
| 141 | return; |
| 142 | |
| 143 | Py_DECREF(pmd->PyPML); |
| 144 | Py_INCREF(result); |
| 145 | pmd->PyPML = result; |
| 146 | } |
| 147 | %} |
| 148 | |
| 149 | |
| 150 | |
| 151 | struct fd_hook_hdl { |
| 152 | }; |
| 153 | |
| 154 | %nodefaultctor fd_hook_hdl; |
| 155 | %extend fd_hook_hdl { |
| 156 | fd_hook_hdl(uint32_t type_mask, PyObject * PyCb) { |
| 157 | struct fd_hook_hdl *hdl; |
| 158 | int ret; |
| 159 | |
| 160 | Py_XINCREF(PyCb); |
| 161 | |
| 162 | ret = fd_hook_register ( type_mask, call_the_python_hook_callback, PyCb, NULL, &hdl ); |
| 163 | if (ret != 0) { |
| 164 | DI_ERROR(ret, NULL, NULL); |
| 165 | return NULL; |
| 166 | } |
| 167 | return hdl; |
| 168 | } |
| 169 | fd_hook_hdl(uint32_t type_mask, PyObject * PyCb, struct fd_hook_data_hdl *datahdl) { |
| 170 | struct fd_hook_hdl *hdl; |
| 171 | int ret; |
| 172 | |
| 173 | Py_XINCREF(PyCb); |
| 174 | |
| 175 | ret = fd_hook_register ( type_mask, call_the_python_hook_callback, PyCb, datahdl, &hdl ); |
| 176 | if (ret != 0) { |
| 177 | DI_ERROR(ret, NULL, NULL); |
| 178 | return NULL; |
| 179 | } |
| 180 | return hdl; |
| 181 | } |
| 182 | ~fd_hook_hdl() { |
| 183 | struct fd_hook_hdl * hdl = self; |
| 184 | int ret = fd_hook_unregister(hdl); |
| 185 | if (ret != 0) { |
| 186 | DI_ERROR(ret, NULL, NULL); |
| 187 | } |
| 188 | return; |
| 189 | } |
| 190 | } |