Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 1 | /* spoa-server: processing Python |
| 2 | * |
| 3 | * Copyright 2018 OZON / Thierry Fournier <thierry.fournier@ozon.io> |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 4 | * Copyright (C) 2020 Gilchrist Dadaglo <gilchrist@dadaglo.com> |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 11 | * This program is provided in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Define PY_SSIZE_T_CLEAN before including Python.h |
| 20 | * as per https://docs.python.org/3/c-api/arg.html and https://docs.python.org/2/c-api/arg.html |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 21 | */ |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 22 | #define PY_SSIZE_T_CLEAN |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 23 | |
| 24 | #include <Python.h> |
| 25 | |
| 26 | #include <arpa/inet.h> |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <string.h> |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 30 | #include <limits.h> |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 31 | |
| 32 | #include "spoa.h" |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 33 | #include "ps_python.h" |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 34 | |
| 35 | /* Embedding python documentation: |
| 36 | * |
| 37 | * https://docs.python.org/2/extending/embedding.html |
| 38 | * https://docs.python.org/2/extending/extending.html#extending-python-with-c-or-c |
| 39 | * https://docs.python.org/2/extending/extending.html#calling-python-functions-from-c |
| 40 | */ |
| 41 | |
| 42 | static PyObject *module_ipaddress; |
| 43 | static PyObject *ipv4_address; |
| 44 | static PyObject *ipv6_address; |
| 45 | static PyObject *spoa_error; |
Gilchrist Dadaglo | 042f697 | 2020-12-08 14:37:13 +0000 | [diff] [blame] | 46 | static PyObject *empty_tuple; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 47 | static struct worker *worker; |
| 48 | |
| 49 | static int ps_python_start_worker(struct worker *w); |
| 50 | static int ps_python_load_file(struct worker *w, const char *file); |
| 51 | static int ps_python_exec_message(struct worker *w, void *ref, int nargs, struct spoe_kv *args); |
| 52 | |
| 53 | static struct ps ps_python_bindings = { |
| 54 | .init_worker = ps_python_start_worker, |
| 55 | .load_file = ps_python_load_file, |
| 56 | .exec_message = ps_python_exec_message, |
| 57 | .ext = ".py", |
| 58 | }; |
| 59 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 60 | static int ps_python_check_overflow(Py_ssize_t len) |
| 61 | { |
| 62 | /* There might be an overflow when converting from Py_ssize_t to int. |
| 63 | * This function will catch those cases. |
| 64 | * Also, spoa "struct chunk" is limited to int size. |
| 65 | * We should not send data bigger than it can handle. |
| 66 | */ |
| 67 | if (len >= (Py_ssize_t)INT_MAX) { |
| 68 | PyErr_Format(spoa_error, |
William Dauchy | bbe18ac | 2020-08-01 16:28:51 +0200 | [diff] [blame] | 69 | "%zd is over 2GB. Please split in smaller pieces.", \ |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 70 | len); |
| 71 | return -1; |
| 72 | } else { |
| 73 | return Py_SAFE_DOWNCAST(len, Py_ssize_t, int); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #if IS_PYTHON_3K |
| 78 | static PyObject *module_spoa; |
| 79 | static PyObject *PyInit_spoa_module(void); |
| 80 | #endif /* IS_PYTHON_3K */ |
| 81 | |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 82 | static PyObject *ps_python_register_message(PyObject *self, PyObject *args) |
| 83 | { |
| 84 | const char *name; |
| 85 | PyObject *ref; |
| 86 | |
| 87 | if (!PyArg_ParseTuple(args, "sO!", &name, &PyFunction_Type, &ref)) |
| 88 | return NULL; |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 89 | Py_XINCREF(ref); /* because the function is internally referenced */ |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 90 | |
| 91 | ps_register_message(&ps_python_bindings, name, (void *)ref); |
| 92 | |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 93 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static PyObject *ps_python_set_var_null(PyObject *self, PyObject *args) |
| 97 | { |
| 98 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 99 | Py_ssize_t name_len; |
| 100 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 101 | int scope; |
| 102 | |
| 103 | if (!PyArg_ParseTuple(args, "s#i", &name, &name_len, &scope)) |
| 104 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 105 | name_len_i = ps_python_check_overflow(name_len); |
| 106 | if (name_len_i == -1) |
| 107 | return NULL; |
| 108 | if (!set_var_null(worker, name, name_len_i, scope)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 109 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 112 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static PyObject *ps_python_set_var_boolean(PyObject *self, PyObject *args) |
| 116 | { |
| 117 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 118 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 119 | int scope; |
| 120 | int value; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 121 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 122 | |
| 123 | if (!PyArg_ParseTuple(args, "s#ii", &name, &name_len, &scope, &value)) |
| 124 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 125 | name_len_i = ps_python_check_overflow(name_len); |
| 126 | if (name_len_i == -1) |
| 127 | return NULL; |
| 128 | if (!set_var_bool(worker, name, name_len_i, scope, value)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 129 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 130 | return NULL; |
| 131 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 132 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static PyObject *ps_python_set_var_int32(PyObject *self, PyObject *args) |
| 136 | { |
| 137 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 138 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 139 | int scope; |
| 140 | int32_t value; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 141 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 142 | |
| 143 | if (!PyArg_ParseTuple(args, "s#ii", &name, &name_len, &scope, &value)) |
| 144 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 145 | name_len_i = ps_python_check_overflow(name_len); |
| 146 | if (name_len_i == -1) |
| 147 | return NULL; |
| 148 | if (!set_var_int32(worker, name, name_len_i, scope, value)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 149 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 152 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static PyObject *ps_python_set_var_uint32(PyObject *self, PyObject *args) |
| 156 | { |
| 157 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 158 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 159 | int scope; |
| 160 | uint32_t value; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 161 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 162 | |
| 163 | if (!PyArg_ParseTuple(args, "s#iI", &name, &name_len, &scope, &value)) |
| 164 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 165 | name_len_i = ps_python_check_overflow(name_len); |
| 166 | if (name_len_i == -1) |
| 167 | return NULL; |
| 168 | if (!set_var_uint32(worker, name, name_len_i, scope, value)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 169 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 170 | return NULL; |
| 171 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 172 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static PyObject *ps_python_set_var_int64(PyObject *self, PyObject *args) |
| 176 | { |
| 177 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 178 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 179 | int scope; |
| 180 | int64_t value; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 181 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 182 | |
| 183 | if (!PyArg_ParseTuple(args, "s#il", &name, &name_len, &scope, &value)) |
| 184 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 185 | name_len_i = ps_python_check_overflow(name_len); |
| 186 | if (name_len_i == -1) |
| 187 | return NULL; |
| 188 | if (!set_var_int64(worker, name, name_len_i, scope, value)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 189 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 192 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static PyObject *ps_python_set_var_uint64(PyObject *self, PyObject *args) |
| 196 | { |
| 197 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 198 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 199 | int scope; |
| 200 | uint64_t value; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 201 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 202 | |
| 203 | if (!PyArg_ParseTuple(args, "s#ik", &name, &name_len, &scope, &value)) |
| 204 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 205 | name_len_i = ps_python_check_overflow(name_len); |
| 206 | if (name_len_i == -1) |
| 207 | return NULL; |
| 208 | if (!set_var_uint64(worker, name, name_len_i, scope, value)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 209 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 210 | return NULL; |
| 211 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 212 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static PyObject *ps_python_set_var_ipv4(PyObject *self, PyObject *args) |
| 216 | { |
| 217 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 218 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 219 | int scope; |
| 220 | PyObject *ipv4; |
| 221 | PyObject *value; |
| 222 | struct in_addr ip; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 223 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 224 | |
| 225 | if (!PyArg_ParseTuple(args, "s#iO", &name, &name_len, &scope, &ipv4)) |
| 226 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 227 | name_len_i = ps_python_check_overflow(name_len); |
| 228 | if (name_len_i == -1) |
| 229 | return NULL; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 230 | if (!PyObject_IsInstance(ipv4, ipv4_address)) { |
| 231 | PyErr_Format(spoa_error, "must be 'IPv4Address', not '%s'", ipv4->ob_type->tp_name); |
| 232 | return NULL; |
| 233 | } |
| 234 | /* Execute packed ... I think .. */ |
| 235 | value = PyObject_GetAttrString(ipv4, "packed"); |
| 236 | if (value == NULL) |
| 237 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 238 | if (PY_STRING_GET_SIZE(value) != sizeof(ip)) { |
Gilchrist Dadaglo | 6884742 | 2020-12-08 14:37:08 +0000 | [diff] [blame] | 239 | PyErr_Format(spoa_error, "IPv4 manipulation internal error"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 240 | return NULL; |
| 241 | } |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 242 | memcpy(&ip, PY_STRING_AS_STRING(value), PY_STRING_GET_SIZE(value)); |
| 243 | if (!set_var_ipv4(worker, name, name_len_i, scope, &ip)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 244 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
Gilchrist Dadaglo | 9f0c984 | 2020-08-24 19:21:32 +0000 | [diff] [blame] | 247 | /* Once we set the IP value in the worker, we don't need it anymore... */ |
| 248 | Py_XDECREF(value); |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 249 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static PyObject *ps_python_set_var_ipv6(PyObject *self, PyObject *args) |
| 253 | { |
| 254 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 255 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 256 | int scope; |
| 257 | PyObject *ipv6; |
| 258 | PyObject *value; |
| 259 | struct in6_addr ip; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 260 | int name_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 261 | |
| 262 | if (!PyArg_ParseTuple(args, "s#iO", &name, &name_len, &scope, &ipv6)) |
| 263 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 264 | name_len_i = ps_python_check_overflow(name_len); |
| 265 | if (name_len_i == -1) |
| 266 | return NULL; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 267 | if (!PyObject_IsInstance(ipv6, ipv6_address)) { |
| 268 | PyErr_Format(spoa_error, "must be 'IPv6Address', not '%s'", ipv6->ob_type->tp_name); |
| 269 | return NULL; |
| 270 | } |
| 271 | /* Execute packed ... I think .. */ |
| 272 | value = PyObject_GetAttrString(ipv6, "packed"); |
| 273 | if (value == NULL) |
| 274 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 275 | if (PY_STRING_GET_SIZE(value) != sizeof(ip)) { |
Gilchrist Dadaglo | 6884742 | 2020-12-08 14:37:08 +0000 | [diff] [blame] | 276 | PyErr_Format(spoa_error, "IPv6 manipulation internal error"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 277 | return NULL; |
| 278 | } |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 279 | memcpy(&ip, PY_STRING_AS_STRING(value), PY_STRING_GET_SIZE(value)); |
| 280 | if (!set_var_ipv6(worker, name, name_len_i, scope, &ip)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 281 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
Gilchrist Dadaglo | 9f0c984 | 2020-08-24 19:21:32 +0000 | [diff] [blame] | 284 | /* Once we set the IP value in the worker, we don't need it anymore... */ |
| 285 | Py_XDECREF(value); |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 286 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static PyObject *ps_python_set_var_str(PyObject *self, PyObject *args) |
| 290 | { |
| 291 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 292 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 293 | int scope; |
| 294 | const char *value; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 295 | Py_ssize_t value_len; |
| 296 | int name_len_i; |
| 297 | int value_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 298 | |
| 299 | if (!PyArg_ParseTuple(args, "s#is#", &name, &name_len, &scope, &value, &value_len)) |
| 300 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 301 | name_len_i = ps_python_check_overflow(name_len); |
| 302 | value_len_i = ps_python_check_overflow(value_len); |
| 303 | if (name_len_i == -1 || value_len_i == -1) |
| 304 | return NULL; |
| 305 | if (!set_var_string(worker, name, name_len_i, scope, value, value_len_i)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 306 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 309 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static PyObject *ps_python_set_var_bin(PyObject *self, PyObject *args) |
| 313 | { |
| 314 | const char *name; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 315 | Py_ssize_t name_len; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 316 | int scope; |
| 317 | const char *value; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 318 | Py_ssize_t value_len; |
| 319 | int name_len_i; |
| 320 | int value_len_i; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 321 | |
| 322 | if (!PyArg_ParseTuple(args, "s#is#", &name, &name_len, &scope, &value, &value_len)) |
| 323 | return NULL; |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 324 | name_len_i = ps_python_check_overflow(name_len); |
| 325 | value_len_i = ps_python_check_overflow(value_len); |
| 326 | if (name_len_i == -1 || value_len_i == -1) |
| 327 | return NULL; |
| 328 | if (!set_var_bin(worker, name, name_len_i, scope, value, value_len_i)) { |
Gilchrist Dadaglo | 85b2582 | 2020-12-08 14:37:09 +0000 | [diff] [blame] | 329 | PyErr_SetString(spoa_error, "No more memory space available"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
Gilchrist Dadaglo | d5c428e | 2020-12-08 14:37:07 +0000 | [diff] [blame] | 332 | Py_RETURN_NONE; |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | |
| 336 | static PyMethodDef spoa_methods[] = { |
| 337 | {"register_message", ps_python_register_message, METH_VARARGS, |
| 338 | "Register binding for SPOA message."}, |
| 339 | {"set_var_null", ps_python_set_var_null, METH_VARARGS, |
| 340 | "Set SPOA NULL variable"}, |
| 341 | {"set_var_boolean", ps_python_set_var_boolean, METH_VARARGS, |
| 342 | "Set SPOA boolean variable"}, |
| 343 | {"set_var_int32", ps_python_set_var_int32, METH_VARARGS, |
| 344 | "Set SPOA int32 variable"}, |
| 345 | {"set_var_uint32", ps_python_set_var_uint32, METH_VARARGS, |
| 346 | "Set SPOA uint32 variable"}, |
| 347 | {"set_var_int64", ps_python_set_var_int64, METH_VARARGS, |
| 348 | "Set SPOA int64 variable"}, |
| 349 | {"set_var_uint64", ps_python_set_var_uint64, METH_VARARGS, |
| 350 | "Set SPOA uint64 variable"}, |
| 351 | {"set_var_ipv4", ps_python_set_var_ipv4, METH_VARARGS, |
| 352 | "Set SPOA ipv4 variable"}, |
| 353 | {"set_var_ipv6", ps_python_set_var_ipv6, METH_VARARGS, |
| 354 | "Set SPOA ipv6 variable"}, |
| 355 | {"set_var_str", ps_python_set_var_str, METH_VARARGS, |
| 356 | "Set SPOA str variable"}, |
| 357 | {"set_var_bin", ps_python_set_var_bin, METH_VARARGS, |
| 358 | "Set SPOA bin variable"}, |
| 359 | { /* end */ } |
| 360 | }; |
| 361 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 362 | #if IS_PYTHON_3K |
| 363 | static struct PyModuleDef spoa_module_definition = { |
| 364 | PyModuleDef_HEAD_INIT, /* m_base */ |
| 365 | "spoa", /* m_name */ |
| 366 | "HAProxy SPOA module for python", /* m_doc */ |
| 367 | -1, /* m_size */ |
| 368 | spoa_methods, /* m_methods */ |
| 369 | NULL, /* m_slots */ |
| 370 | NULL, /* m_traverse */ |
| 371 | NULL, /* m_clear */ |
| 372 | NULL /* m_free */ |
| 373 | }; |
| 374 | |
| 375 | static PyObject *PyInit_spoa_module(void) |
| 376 | { |
| 377 | return module_spoa; |
| 378 | } |
| 379 | #endif /* IS_PYTHON_3K */ |
| 380 | |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 381 | static int ps_python_start_worker(struct worker *w) |
| 382 | { |
| 383 | PyObject *m; |
| 384 | PyObject *module_name; |
| 385 | PyObject *value; |
| 386 | int ret; |
| 387 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 388 | #if IS_PYTHON_27 |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 389 | Py_SetProgramName("spoa-server"); |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 390 | #endif /* IS_PYTHON_27 */ |
| 391 | #if IS_PYTHON_3K |
| 392 | Py_SetProgramName(Py_DecodeLocale("spoa-server", NULL)); |
| 393 | PyImport_AppendInittab("spoa", &PyInit_spoa_module); |
| 394 | #endif /* IS_PYTHON_3K */ |
| 395 | |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 396 | Py_Initialize(); |
| 397 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 398 | module_name = PY_STRING_FROM_STRING("ipaddress"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 399 | if (module_name == NULL) { |
| 400 | PyErr_Print(); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | module_ipaddress = PyImport_Import(module_name); |
| 405 | Py_DECREF(module_name); |
| 406 | if (module_ipaddress == NULL) { |
| 407 | PyErr_Print(); |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | ipv4_address = PyObject_GetAttrString(module_ipaddress, "IPv4Address"); |
| 412 | if (ipv4_address == NULL) { |
Gilchrist Dadaglo | d1c0cf8 | 2020-12-08 14:37:12 +0000 | [diff] [blame] | 413 | Py_DECREF(module_ipaddress); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 414 | PyErr_Print(); |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | ipv6_address = PyObject_GetAttrString(module_ipaddress, "IPv6Address"); |
Gilchrist Dadaglo | d00ce06 | 2020-08-24 19:21:35 +0000 | [diff] [blame] | 419 | if (ipv6_address == NULL) { |
Gilchrist Dadaglo | d1c0cf8 | 2020-12-08 14:37:12 +0000 | [diff] [blame] | 420 | Py_DECREF(ipv4_address); |
| 421 | Py_DECREF(module_ipaddress); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 422 | PyErr_Print(); |
| 423 | return 0; |
| 424 | } |
| 425 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 426 | PY_INIT_MODULE(m, "spoa", spoa_methods, &spoa_module_definition); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 427 | if (m == NULL) { |
Gilchrist Dadaglo | d1c0cf8 | 2020-12-08 14:37:12 +0000 | [diff] [blame] | 428 | Py_DECREF(ipv4_address); |
| 429 | Py_DECREF(ipv6_address); |
| 430 | Py_DECREF(module_ipaddress); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 431 | PyErr_Print(); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | spoa_error = PyErr_NewException("spoa.error", NULL, NULL); |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 436 | /* PyModule_AddObject will steal the reference to spoa_error |
| 437 | * in case of success only |
| 438 | * We need to increment the counters to continue using it |
| 439 | * but cleanup in case of failure |
| 440 | */ |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 441 | Py_INCREF(spoa_error); |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 442 | ret = PyModule_AddObject(m, "error", spoa_error); |
| 443 | if (ret == -1) { |
| 444 | Py_DECREF(m); |
| 445 | Py_DECREF(spoa_error); |
| 446 | PyErr_Print(); |
| 447 | return 0; |
| 448 | } |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 449 | |
| 450 | |
| 451 | value = PyLong_FromLong(SPOE_SCOPE_PROC); |
| 452 | if (value == NULL) { |
| 453 | PyErr_Print(); |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | ret = PyModule_AddObject(m, "scope_proc", value); |
| 458 | if (ret == -1) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 459 | Py_DECREF(m); |
| 460 | Py_DECREF(value); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 461 | PyErr_Print(); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | value = PyLong_FromLong(SPOE_SCOPE_SESS); |
| 466 | if (value == NULL) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 467 | Py_DECREF(m); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 468 | PyErr_Print(); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | ret = PyModule_AddObject(m, "scope_sess", value); |
| 473 | if (ret == -1) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 474 | Py_DECREF(m); |
| 475 | Py_DECREF(value); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 476 | PyErr_Print(); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | value = PyLong_FromLong(SPOE_SCOPE_TXN); |
| 481 | if (value == NULL) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 482 | Py_DECREF(m); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 483 | PyErr_Print(); |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | ret = PyModule_AddObject(m, "scope_txn", value); |
| 488 | if (ret == -1) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 489 | Py_DECREF(m); |
| 490 | Py_DECREF(value); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 491 | PyErr_Print(); |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | value = PyLong_FromLong(SPOE_SCOPE_REQ); |
| 496 | if (value == NULL) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 497 | Py_DECREF(m); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 498 | PyErr_Print(); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | ret = PyModule_AddObject(m, "scope_req", value); |
| 503 | if (ret == -1) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 504 | Py_DECREF(m); |
| 505 | Py_DECREF(value); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 506 | PyErr_Print(); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | value = PyLong_FromLong(SPOE_SCOPE_RES); |
| 511 | if (value == NULL) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 512 | Py_DECREF(m); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 513 | PyErr_Print(); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | ret = PyModule_AddObject(m, "scope_res", value); |
| 518 | if (ret == -1) { |
Gilchrist Dadaglo | 132d8f6 | 2020-12-08 14:37:11 +0000 | [diff] [blame] | 519 | Py_DECREF(m); |
| 520 | Py_DECREF(value); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 521 | PyErr_Print(); |
| 522 | return 0; |
| 523 | } |
| 524 | |
Gilchrist Dadaglo | 042f697 | 2020-12-08 14:37:13 +0000 | [diff] [blame] | 525 | empty_tuple = PyTuple_New(0); |
| 526 | if (empty_tuple == NULL) { |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 527 | PyErr_Print(); |
| 528 | return 0; |
| 529 | } |
| 530 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 531 | #if IS_PYTHON_3K |
| 532 | module_spoa = m; |
| 533 | #endif /* IS_PYTHON_3K */ |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 534 | worker = w; |
| 535 | return 1; |
| 536 | } |
| 537 | |
| 538 | static int ps_python_load_file(struct worker *w, const char *file) |
| 539 | { |
| 540 | FILE *fp; |
| 541 | int ret; |
| 542 | |
| 543 | fp = fopen(file, "r"); |
| 544 | if (fp == NULL) { |
| 545 | LOG("python: Cannot read file \"%s\": %s", file, strerror(errno)); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | ret = PyRun_SimpleFile(fp, file); |
| 550 | fclose(fp); |
| 551 | if (ret != 0) { |
| 552 | PyErr_Print(); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | return 1; |
| 557 | } |
| 558 | |
| 559 | static int ps_python_exec_message(struct worker *w, void *ref, int nargs, struct spoe_kv *args) |
| 560 | { |
| 561 | int i; |
| 562 | PyObject *python_ref = ref; |
| 563 | PyObject *fkw; |
| 564 | PyObject *kw_args; |
| 565 | PyObject *result; |
| 566 | PyObject *ent; |
| 567 | PyObject *key; |
| 568 | PyObject *value; |
| 569 | PyObject *func; |
| 570 | int ret; |
| 571 | char ipbuf[64]; |
| 572 | const char *p; |
| 573 | PyObject *ip_dict; |
| 574 | PyObject *ip_name; |
| 575 | PyObject *ip_value; |
| 576 | |
| 577 | /* Dict containing arguments */ |
| 578 | |
| 579 | kw_args = PyList_New(0); |
| 580 | if (kw_args == NULL) { |
| 581 | PyErr_Print(); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | for (i = 0; i < nargs; i++) { |
| 586 | |
| 587 | /* New dict containing one argument */ |
| 588 | |
| 589 | ent = PyDict_New(); |
| 590 | if (ent == NULL) { |
| 591 | Py_DECREF(kw_args); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 592 | PyErr_Print(); |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /* Create the name entry */ |
| 597 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 598 | key = PY_STRING_FROM_STRING("name"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 599 | if (key == NULL) { |
| 600 | Py_DECREF(kw_args); |
Gilchrist Dadaglo | 2417ebc | 2020-08-24 19:21:34 +0000 | [diff] [blame] | 601 | Py_DECREF(ent); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 602 | PyErr_Print(); |
| 603 | return 0; |
| 604 | } |
| 605 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 606 | value = PY_STRING_FROM_STRING_AND_SIZE(args[i].name.str, args[i].name.len); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 607 | if (value == NULL) { |
| 608 | Py_DECREF(kw_args); |
| 609 | Py_DECREF(ent); |
| 610 | Py_DECREF(key); |
| 611 | PyErr_Print(); |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | ret = PyDict_SetItem(ent, key, value); |
| 616 | Py_DECREF(key); |
| 617 | Py_DECREF(value); |
| 618 | if (ret == -1) { |
| 619 | Py_DECREF(kw_args); |
| 620 | Py_DECREF(ent); |
| 621 | PyErr_Print(); |
| 622 | return 0; |
| 623 | } |
| 624 | |
Gilchrist Dadaglo | c7485ac | 2020-12-08 14:37:10 +0000 | [diff] [blame] | 625 | /* Create the value entry */ |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 626 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 627 | key = PY_STRING_FROM_STRING("value"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 628 | if (key == NULL) { |
| 629 | Py_DECREF(kw_args); |
| 630 | Py_DECREF(ent); |
| 631 | PyErr_Print(); |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | switch (args[i].value.type) { |
| 636 | case SPOE_DATA_T_NULL: |
Gilchrist Dadaglo | bdd0349 | 2020-12-08 14:37:14 +0000 | [diff] [blame] | 637 | Py_INCREF(Py_None); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 638 | value = Py_None; |
| 639 | break; |
| 640 | case SPOE_DATA_T_BOOL: |
| 641 | value = PyBool_FromLong(args[i].value.u.boolean); |
| 642 | break; |
| 643 | case SPOE_DATA_T_INT32: |
| 644 | value = PyLong_FromLong(args[i].value.u.sint32); |
| 645 | break; |
| 646 | case SPOE_DATA_T_UINT32: |
| 647 | value = PyLong_FromLong(args[i].value.u.uint32); |
| 648 | break; |
| 649 | case SPOE_DATA_T_INT64: |
| 650 | value = PyLong_FromLong(args[i].value.u.sint64); |
| 651 | break; |
| 652 | case SPOE_DATA_T_UINT64: |
| 653 | value = PyLong_FromUnsignedLong(args[i].value.u.uint64); |
| 654 | break; |
| 655 | case SPOE_DATA_T_IPV4: |
| 656 | case SPOE_DATA_T_IPV6: |
| 657 | if (args[i].value.type == SPOE_DATA_T_IPV4) |
| 658 | p = inet_ntop(AF_INET, &args[i].value.u.ipv4, ipbuf, 64); |
| 659 | else |
| 660 | p = inet_ntop(AF_INET6, &args[i].value.u.ipv6, ipbuf, 64); |
| 661 | if (!p) |
| 662 | strcpy(ipbuf, "0.0.0.0"); |
| 663 | |
| 664 | func = PyObject_GetAttrString(module_ipaddress, "ip_address"); |
| 665 | if (func == NULL) { |
| 666 | Py_DECREF(kw_args); |
| 667 | Py_DECREF(ent); |
Gilchrist Dadaglo | 2417ebc | 2020-08-24 19:21:34 +0000 | [diff] [blame] | 668 | Py_DECREF(key); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 669 | PyErr_Print(); |
| 670 | return 0; |
| 671 | } |
| 672 | ip_dict = PyDict_New(); |
| 673 | if (ip_dict == NULL) { |
| 674 | Py_DECREF(kw_args); |
| 675 | Py_DECREF(ent); |
Gilchrist Dadaglo | 2417ebc | 2020-08-24 19:21:34 +0000 | [diff] [blame] | 676 | Py_DECREF(key); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 677 | Py_DECREF(func); |
| 678 | PyErr_Print(); |
| 679 | return 0; |
| 680 | } |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 681 | ip_name = PY_STRING_FROM_STRING("address"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 682 | if (ip_name == NULL) { |
| 683 | Py_DECREF(kw_args); |
| 684 | Py_DECREF(ent); |
Gilchrist Dadaglo | 2417ebc | 2020-08-24 19:21:34 +0000 | [diff] [blame] | 685 | Py_DECREF(key); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 686 | Py_DECREF(func); |
| 687 | Py_DECREF(ip_dict); |
| 688 | PyErr_Print(); |
| 689 | return 0; |
| 690 | } |
| 691 | ip_value = PyUnicode_FromString(ipbuf); |
| 692 | if (ip_value == NULL) { |
| 693 | Py_DECREF(kw_args); |
| 694 | Py_DECREF(ent); |
Gilchrist Dadaglo | 2417ebc | 2020-08-24 19:21:34 +0000 | [diff] [blame] | 695 | Py_DECREF(key); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 696 | Py_DECREF(func); |
| 697 | Py_DECREF(ip_dict); |
| 698 | Py_DECREF(ip_name); |
| 699 | PyErr_Print(); |
| 700 | return 0; |
| 701 | } |
| 702 | ret = PyDict_SetItem(ip_dict, ip_name, ip_value); |
| 703 | Py_DECREF(ip_name); |
| 704 | Py_DECREF(ip_value); |
| 705 | if (ret == -1) { |
Gilchrist Dadaglo | 2417ebc | 2020-08-24 19:21:34 +0000 | [diff] [blame] | 706 | Py_DECREF(kw_args); |
| 707 | Py_DECREF(ent); |
| 708 | Py_DECREF(key); |
| 709 | Py_DECREF(func); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 710 | Py_DECREF(ip_dict); |
| 711 | PyErr_Print(); |
| 712 | return 0; |
| 713 | } |
Gilchrist Dadaglo | 042f697 | 2020-12-08 14:37:13 +0000 | [diff] [blame] | 714 | value = PyObject_Call(func, empty_tuple, ip_dict); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 715 | Py_DECREF(func); |
| 716 | Py_DECREF(ip_dict); |
| 717 | break; |
| 718 | |
| 719 | case SPOE_DATA_T_STR: |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 720 | value = PY_STRING_FROM_STRING_AND_SIZE(args[i].value.u.buffer.str, args[i].value.u.buffer.len); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 721 | break; |
| 722 | case SPOE_DATA_T_BIN: |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 723 | value = PY_BYTES_FROM_STRING_AND_SIZE(args[i].value.u.buffer.str, args[i].value.u.buffer.len); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 724 | break; |
| 725 | default: |
Gilchrist Dadaglo | bdd0349 | 2020-12-08 14:37:14 +0000 | [diff] [blame] | 726 | Py_INCREF(Py_None); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 727 | value = Py_None; |
| 728 | break; |
| 729 | } |
| 730 | if (value == NULL) { |
| 731 | Py_DECREF(kw_args); |
| 732 | Py_DECREF(ent); |
| 733 | Py_DECREF(key); |
| 734 | PyErr_Print(); |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | ret = PyDict_SetItem(ent, key, value); |
| 739 | Py_DECREF(key); |
| 740 | Py_DECREF(value); |
| 741 | if (ret == -1) { |
| 742 | Py_DECREF(kw_args); |
| 743 | Py_DECREF(ent); |
| 744 | PyErr_Print(); |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | /* Add dict to the list */ |
| 749 | |
| 750 | ret = PyList_Append(kw_args, ent); |
| 751 | Py_DECREF(ent); |
| 752 | if (ret == -1) { |
| 753 | Py_DECREF(kw_args); |
| 754 | PyErr_Print(); |
| 755 | return 0; |
| 756 | } |
| 757 | } |
| 758 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 759 | /* Dictionary { args = <list-of-args> } for the function */ |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 760 | |
| 761 | fkw = PyDict_New(); |
| 762 | if (fkw == NULL) { |
| 763 | Py_DECREF(kw_args); |
| 764 | PyErr_Print(); |
| 765 | return 0; |
| 766 | } |
| 767 | |
Gilchrist Dadaglo | 3e235d3 | 2020-05-06 12:25:31 +0000 | [diff] [blame] | 768 | key = PY_STRING_FROM_STRING("args"); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 769 | if (key == NULL) { |
| 770 | Py_DECREF(kw_args); |
| 771 | Py_DECREF(fkw); |
| 772 | PyErr_Print(); |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | ret = PyDict_SetItem(fkw, key, kw_args); |
| 777 | Py_DECREF(kw_args); |
| 778 | Py_DECREF(key); |
| 779 | if (ret == -1) { |
| 780 | Py_DECREF(fkw); |
| 781 | PyErr_Print(); |
| 782 | return 0; |
| 783 | } |
| 784 | |
Gilchrist Dadaglo | 042f697 | 2020-12-08 14:37:13 +0000 | [diff] [blame] | 785 | result = PyObject_Call(python_ref, empty_tuple, fkw); |
Gilchrist Dadaglo | 222f060 | 2020-08-24 19:21:31 +0000 | [diff] [blame] | 786 | Py_DECREF(fkw); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 787 | if (result == NULL) { |
| 788 | PyErr_Print(); |
| 789 | return 0; |
| 790 | } |
Gilchrist Dadaglo | bdd0349 | 2020-12-08 14:37:14 +0000 | [diff] [blame] | 791 | Py_DECREF(result); |
Thierry FOURNIER | 00a0225 | 2018-02-25 20:59:57 +0100 | [diff] [blame] | 792 | |
| 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | __attribute__((constructor)) |
| 797 | static void __ps_python_init(void) |
| 798 | { |
| 799 | ps_register(&ps_python_bindings); |
| 800 | } |