| 1 | /* |
| 2 | * Copyright (c) 2017-2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/param.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include "kore.h" |
| 21 | |
| 22 | #if !defined(KORE_NO_HTTP) |
| 23 | #include "http.h" |
| 24 | #endif |
| 25 | |
| 26 | #if defined(KORE_USE_PYTHON) |
| 27 | #include "python_api.h" |
| 28 | #endif |
| 29 | |
| 30 | static void native_runtime_execute(void *); |
| 31 | static int native_runtime_onload(void *, int); |
| 32 | static void native_runtime_signal(void *, int); |
| 33 | static void native_runtime_connect(void *, struct connection *); |
| 34 | static void native_runtime_configure(void *, int, char **); |
| 35 | #if !defined(KORE_NO_HTTP) |
| 36 | static int native_runtime_http_request(void *, struct http_request *); |
| 37 | static void native_runtime_http_request_free(void *, struct http_request *); |
| 38 | static void native_runtime_http_body_chunk(void *, struct http_request *, |
| 39 | const void *, size_t); |
| 40 | static int native_runtime_validator(void *, struct http_request *, |
| 41 | const void *); |
| 42 | |
| 43 | static void native_runtime_wsmessage(void *, struct connection *, u_int8_t, |
| 44 | const void *, size_t); |
| 45 | #endif |
| 46 | |
| 47 | struct kore_runtime kore_native_runtime = { |
| 48 | KORE_RUNTIME_NATIVE, |
| 49 | #if !defined(KORE_NO_HTTP) |
| 50 | .http_request = native_runtime_http_request, |
| 51 | .http_request_free = native_runtime_http_request_free, |
| 52 | .http_body_chunk = native_runtime_http_body_chunk, |
| 53 | .validator = native_runtime_validator, |
| 54 | .wsconnect = native_runtime_connect, |
| 55 | .wsmessage = native_runtime_wsmessage, |
| 56 | .wsdisconnect = native_runtime_connect, |
| 57 | #endif |
| 58 | .onload = native_runtime_onload, |
| 59 | .signal = native_runtime_signal, |
| 60 | .connect = native_runtime_connect, |
| 61 | .execute = native_runtime_execute, |
| 62 | .configure = native_runtime_configure |
| 63 | }; |
| 64 | |
| 65 | struct kore_runtime_call * |
| 66 | kore_runtime_getcall(const char *symbol) |
| 67 | { |
| 68 | void *ptr; |
| 69 | struct kore_runtime_call *rcall; |
| 70 | struct kore_runtime *runtime; |
| 71 | |
| 72 | ptr = kore_module_getsym(symbol, &runtime); |
| 73 | if (ptr == NULL) |
| 74 | return (NULL); |
| 75 | |
| 76 | rcall = kore_malloc(sizeof(*rcall)); |
| 77 | rcall->addr = ptr; |
| 78 | rcall->runtime = runtime; |
| 79 | |
| 80 | return (rcall); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | kore_runtime_execute(struct kore_runtime_call *rcall) |
| 85 | { |
| 86 | rcall->runtime->execute(rcall->addr); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | kore_runtime_configure(struct kore_runtime_call *rcall, int argc, char **argv) |
| 91 | { |
| 92 | rcall->runtime->configure(rcall->addr, argc, argv); |
| 93 | } |
| 94 | |
| 95 | int |
| 96 | kore_runtime_onload(struct kore_runtime_call *rcall, int action) |
| 97 | { |
| 98 | return (rcall->runtime->onload(rcall->addr, action)); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | kore_runtime_connect(struct kore_runtime_call *rcall, struct connection *c) |
| 103 | { |
| 104 | rcall->runtime->connect(rcall->addr, c); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | kore_runtime_signal(struct kore_runtime_call *rcall, int sig) |
| 109 | { |
| 110 | rcall->runtime->signal(rcall->addr, sig); |
| 111 | } |
| 112 | |
| 113 | #if !defined(KORE_NO_HTTP) |
| 114 | int |
| 115 | kore_runtime_http_request(struct kore_runtime_call *rcall, |
| 116 | struct http_request *req) |
| 117 | { |
| 118 | return (rcall->runtime->http_request(rcall->addr, req)); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | kore_runtime_http_request_free(struct kore_runtime_call *rcall, |
| 123 | struct http_request *req) |
| 124 | { |
| 125 | rcall->runtime->http_request_free(rcall->addr, req); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | kore_runtime_http_body_chunk(struct kore_runtime_call *rcall, |
| 130 | struct http_request *req, const void *data, size_t len) |
| 131 | { |
| 132 | rcall->runtime->http_body_chunk(rcall->addr, req, data, len); |
| 133 | } |
| 134 | |
| 135 | int |
| 136 | kore_runtime_validator(struct kore_runtime_call *rcall, |
| 137 | struct http_request *req, const void *data) |
| 138 | { |
| 139 | return (rcall->runtime->validator(rcall->addr, req, data)); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | kore_runtime_wsconnect(struct kore_runtime_call *rcall, struct connection *c) |
| 144 | { |
| 145 | rcall->runtime->wsconnect(rcall->addr, c); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | kore_runtime_wsmessage(struct kore_runtime_call *rcall, struct connection *c, |
| 150 | u_int8_t op, const void *data, size_t len) |
| 151 | { |
| 152 | rcall->runtime->wsmessage(rcall->addr, c, op, data, len); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | kore_runtime_wsdisconnect(struct kore_runtime_call *rcall, struct connection *c) |
| 157 | { |
| 158 | rcall->runtime->wsdisconnect(rcall->addr, c); |
| 159 | } |
| 160 | #endif |
| 161 | |
| 162 | static void |
| 163 | native_runtime_execute(void *addr) |
| 164 | { |
| 165 | void (*cb)(void); |
| 166 | |
| 167 | *(void **)&(cb) = addr; |
| 168 | cb(); |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | native_runtime_configure(void *addr, int argc, char **argv) |
| 173 | { |
| 174 | void (*cb)(int, char **); |
| 175 | |
| 176 | *(void **)&(cb) = addr; |
| 177 | cb(argc, argv); |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | native_runtime_connect(void *addr, struct connection *c) |
| 182 | { |
| 183 | void (*cb)(struct connection *); |
| 184 | |
| 185 | *(void **)&(cb) = addr; |
| 186 | cb(c); |
| 187 | } |
| 188 | |
| 189 | static int |
| 190 | native_runtime_onload(void *addr, int action) |
| 191 | { |
| 192 | int (*cb)(int); |
| 193 | |
| 194 | *(void **)&(cb) = addr; |
| 195 | return (cb(action)); |
| 196 | } |
| 197 | |
| 198 | static void |
| 199 | native_runtime_signal(void *addr, int sig) |
| 200 | { |
| 201 | void (*cb)(int); |
| 202 | |
| 203 | *(void **)&(cb) = addr; |
| 204 | cb(sig); |
| 205 | } |
| 206 | |
| 207 | #if !defined(KORE_NO_HTTP) |
| 208 | static int |
| 209 | native_runtime_http_request(void *addr, struct http_request *req) |
| 210 | { |
| 211 | int (*cb)(struct http_request *); |
| 212 | |
| 213 | *(void **)&(cb) = addr; |
| 214 | return (cb(req)); |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | native_runtime_http_request_free(void *addr, struct http_request *req) |
| 219 | { |
| 220 | int (*cb)(struct http_request *); |
| 221 | |
| 222 | *(void **)&(cb) = addr; |
| 223 | cb(req); |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | native_runtime_http_body_chunk(void *addr, struct http_request *req, |
| 228 | const void *data, size_t len) |
| 229 | { |
| 230 | void (*cb)(struct http_request *, const void *, size_t); |
| 231 | |
| 232 | *(void **)&(cb) = addr; |
| 233 | |
| 234 | cb(req, data, len); |
| 235 | } |
| 236 | |
| 237 | static int |
| 238 | native_runtime_validator(void *addr, struct http_request *req, const void *data) |
| 239 | { |
| 240 | int (*cb)(struct http_request *, const void *); |
| 241 | |
| 242 | *(void **)&(cb) = addr; |
| 243 | return (cb(req, data)); |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | native_runtime_wsmessage(void *addr, struct connection *c, u_int8_t op, |
| 248 | const void *data, size_t len) |
| 249 | { |
| 250 | void (*cb)(struct connection *, u_int8_t, const void *, size_t); |
| 251 | |
| 252 | *(void **)&(cb) = addr; |
| 253 | cb(c, op, data, len); |
| 254 | |
| 255 | } |
| 256 | #endif |