Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1 | #include <sys/socket.h> |
| 2 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 3 | #include <lauxlib.h> |
| 4 | #include <lua.h> |
| 5 | #include <lualib.h> |
| 6 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 7 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 8 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 9 | #endif |
| 10 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11 | #include <ebpttree.h> |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 15 | #include <types/connection.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 16 | #include <types/hlua.h> |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 17 | #include <types/proto_tcp.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 18 | #include <types/proxy.h> |
| 19 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 20 | #include <proto/arg.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 21 | #include <proto/channel.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 22 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 23 | #include <proto/hlua.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 24 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 25 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 26 | #include <proto/payload.h> |
| 27 | #include <proto/proto_http.h> |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 28 | #include <proto/proto_tcp.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 29 | #include <proto/raw_sock.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 30 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 31 | #include <proto/server.h> |
| 32 | #include <proto/session.h> |
| 33 | #include <proto/ssl_sock.h> |
| 34 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 35 | #include <proto/task.h> |
| 36 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 37 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 38 | * macro is used only for identifying the function that can |
| 39 | * not return because a longjmp is executed. |
| 40 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 41 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 42 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 43 | */ |
| 44 | #define __LJMP |
| 45 | #define WILL_LJMP(func) func |
| 46 | #define MAY_LJMP(func) func |
| 47 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 48 | /* The main Lua execution context. */ |
| 49 | struct hlua gL; |
| 50 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 51 | /* This is the memory pool containing all the signal structs. These |
| 52 | * struct are used to store each requiered signal between two tasks. |
| 53 | */ |
| 54 | struct pool_head *pool2_hlua_com; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 55 | struct pool_head *pool2_hlua_sleep; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 56 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 57 | /* Used for Socket connection. */ |
| 58 | static struct proxy socket_proxy; |
| 59 | static struct server socket_tcp; |
| 60 | #ifdef USE_OPENSSL |
| 61 | static struct server socket_ssl; |
| 62 | #endif |
| 63 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 64 | /* List head of the function called at the initialisation time. */ |
| 65 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 66 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 67 | /* Store the fast lua context for coroutines. This tree uses the |
| 68 | * Lua stack pointer value as indexed entry, and store the associated |
| 69 | * hlua context. |
| 70 | */ |
| 71 | struct eb_root hlua_ctx = EB_ROOT_UNIQUE; |
| 72 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 73 | /* The following variables contains the reference of the different |
| 74 | * Lua classes. These references are useful for identify metadata |
| 75 | * associated with an object. |
| 76 | */ |
| 77 | static int class_core_ref; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 78 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 79 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 80 | static int class_channel_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 81 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 82 | /* Global Lua execution timeout. By default Lua, execution linked |
| 83 | * with session (actions, sample-fetches and converters) have a |
| 84 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 85 | * because a task may remain alive during all the haproxy execution. |
| 86 | */ |
| 87 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 88 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
| 89 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 90 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 91 | * it is used for preventing infinite loops. |
| 92 | * |
| 93 | * I test the scheer with an infinite loop containing one incrementation |
| 94 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 95 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 96 | * to one interrupt each 10 000 instructions. |
| 97 | * |
| 98 | * configured | Number of |
| 99 | * instructions | loops executed |
| 100 | * between two | in milions |
| 101 | * forced yields | |
| 102 | * ---------------+--------------- |
| 103 | * 10 | 160 |
| 104 | * 500 | 670 |
| 105 | * 1000 | 680 |
| 106 | * 5000 | 700 |
| 107 | * 7000 | 700 |
| 108 | * 8000 | 700 |
| 109 | * 9000 | 710 <- ceil |
| 110 | * 10000 | 710 |
| 111 | * 100000 | 710 |
| 112 | * 1000000 | 710 |
| 113 | * |
| 114 | */ |
| 115 | static unsigned int hlua_nb_instruction = 10000; |
| 116 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 117 | /* These functions converts types between HAProxy internal args or |
| 118 | * sample and LUA types. Another function permits to check if the |
| 119 | * LUA stack contains arguments according with an required ARG_T |
| 120 | * format. |
| 121 | */ |
| 122 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 123 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
| 124 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, unsigned int mask); |
| 125 | static int hlua_smp2lua(lua_State *L, const struct sample *smp); |
| 126 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 127 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 128 | /* Used to check an Lua function type in the stack. It creates and |
| 129 | * returns a reference of the function. This function throws an |
| 130 | * error if the rgument is not a "function". |
| 131 | */ |
| 132 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 133 | { |
| 134 | if (!lua_isfunction(L, argno)) { |
| 135 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 136 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 137 | } |
| 138 | lua_pushvalue(L, argno); |
| 139 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 140 | } |
| 141 | |
| 142 | /* The three following functions are useful for adding entries |
| 143 | * in a table. These functions takes a string and respectively an |
| 144 | * integer, a string or a function and add it to the table in the |
| 145 | * top of the stack. |
| 146 | * |
| 147 | * These functions throws an error if no more stack size is |
| 148 | * available. |
| 149 | */ |
| 150 | __LJMP static inline void hlua_class_const_int(lua_State *L, const char *name, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 151 | int value) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 152 | { |
| 153 | if (!lua_checkstack(L, 2)) |
| 154 | WILL_LJMP(luaL_error(L, "full stack")); |
| 155 | lua_pushstring(L, name); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 156 | lua_pushinteger(L, value); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 157 | lua_settable(L, -3); |
| 158 | } |
| 159 | __LJMP static inline void hlua_class_const_str(lua_State *L, const char *name, |
| 160 | const char *value) |
| 161 | { |
| 162 | if (!lua_checkstack(L, 2)) |
| 163 | WILL_LJMP(luaL_error(L, "full stack")); |
| 164 | lua_pushstring(L, name); |
| 165 | lua_pushstring(L, value); |
| 166 | lua_settable(L, -3); |
| 167 | } |
| 168 | __LJMP static inline void hlua_class_function(lua_State *L, const char *name, |
| 169 | int (*function)(lua_State *L)) |
| 170 | { |
| 171 | if (!lua_checkstack(L, 2)) |
| 172 | WILL_LJMP(luaL_error(L, "full stack")); |
| 173 | lua_pushstring(L, name); |
| 174 | lua_pushcclosure(L, function, 0); |
| 175 | lua_settable(L, -3); |
| 176 | } |
| 177 | |
| 178 | /* This function check the number of arguments available in the |
| 179 | * stack. If the number of arguments available is not the same |
| 180 | * then <nb> an error is throwed. |
| 181 | */ |
| 182 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 183 | { |
| 184 | if (lua_gettop(L) == nb) |
| 185 | return; |
| 186 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 187 | } |
| 188 | |
| 189 | /* Return true if the data in stack[<ud>] is an object of |
| 190 | * type <class_ref>. |
| 191 | */ |
| 192 | static int hlua_udataistype(lua_State *L, int ud, int class_ref) |
| 193 | { |
| 194 | void *p = lua_touserdata(L, ud); |
| 195 | if (!p) |
| 196 | return 0; |
| 197 | |
| 198 | if (!lua_getmetatable(L, ud)) |
| 199 | return 0; |
| 200 | |
| 201 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 202 | if (!lua_rawequal(L, -1, -2)) { |
| 203 | lua_pop(L, 2); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | lua_pop(L, 2); |
| 208 | return 1; |
| 209 | } |
| 210 | |
| 211 | /* Return an object of the expected type, or throws an error. */ |
| 212 | __LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 213 | { |
| 214 | if (!hlua_udataistype(L, ud, class_ref)) |
| 215 | WILL_LJMP(luaL_argerror(L, 1, NULL)); |
| 216 | return lua_touserdata(L, ud); |
| 217 | } |
| 218 | |
| 219 | /* This fucntion push an error string prefixed by the file name |
| 220 | * and the line number where the error is encountered. |
| 221 | */ |
| 222 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 223 | { |
| 224 | va_list argp; |
| 225 | va_start(argp, fmt); |
| 226 | luaL_where(L, 1); |
| 227 | lua_pushvfstring(L, fmt, argp); |
| 228 | va_end(argp); |
| 229 | lua_concat(L, 2); |
| 230 | return 1; |
| 231 | } |
| 232 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 233 | /* This function register a new signal. "lua" is the current lua |
| 234 | * execution context. It contains a pointer to the associated task. |
| 235 | * "link" is a list head attached to an other task that must be wake |
| 236 | * the lua task if an event occurs. This is useful with external |
| 237 | * events like TCP I/O or sleep functions. This funcion allocate |
| 238 | * memory for the signal. |
| 239 | */ |
| 240 | static int hlua_com_new(struct hlua *lua, struct list *link) |
| 241 | { |
| 242 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 243 | if (!com) |
| 244 | return 0; |
| 245 | LIST_ADDQ(&lua->com, &com->purge_me); |
| 246 | LIST_ADDQ(link, &com->wake_me); |
| 247 | com->task = lua->task; |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | /* This function purge all the pending signals when the LUA execution |
| 252 | * is finished. This prevent than a coprocess try to wake a deleted |
| 253 | * task. This function remove the memory associated to the signal. |
| 254 | */ |
| 255 | static void hlua_com_purge(struct hlua *lua) |
| 256 | { |
| 257 | struct hlua_com *com, *back; |
| 258 | |
| 259 | /* Delete all pending communication signals. */ |
| 260 | list_for_each_entry_safe(com, back, &lua->com, purge_me) { |
| 261 | LIST_DEL(&com->purge_me); |
| 262 | LIST_DEL(&com->wake_me); |
| 263 | pool_free2(pool2_hlua_com, com); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* This function sends signals. It wakes all the tasks attached |
| 268 | * to a list head, and remove the signal, and free the used |
| 269 | * memory. |
| 270 | */ |
| 271 | static void hlua_com_wake(struct list *wake) |
| 272 | { |
| 273 | struct hlua_com *com, *back; |
| 274 | |
| 275 | /* Wake task and delete all pending communication signals. */ |
| 276 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 277 | LIST_DEL(&com->purge_me); |
| 278 | LIST_DEL(&com->wake_me); |
| 279 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 280 | pool_free2(pool2_hlua_com, com); |
| 281 | } |
| 282 | } |
| 283 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 284 | /* This functions is used with sample fetch and converters. It |
| 285 | * converts the HAProxy configuration argument in a lua stack |
| 286 | * values. |
| 287 | * |
| 288 | * It takes an array of "arg", and each entry of the array is |
| 289 | * converted and pushed in the LUA stack. |
| 290 | */ |
| 291 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 292 | { |
| 293 | switch (arg->type) { |
| 294 | case ARGT_SINT: |
| 295 | lua_pushinteger(L, arg->data.sint); |
| 296 | break; |
| 297 | |
| 298 | case ARGT_UINT: |
| 299 | case ARGT_TIME: |
| 300 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 301 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 302 | break; |
| 303 | |
| 304 | case ARGT_STR: |
| 305 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 306 | break; |
| 307 | |
| 308 | case ARGT_IPV4: |
| 309 | case ARGT_IPV6: |
| 310 | case ARGT_MSK4: |
| 311 | case ARGT_MSK6: |
| 312 | case ARGT_FE: |
| 313 | case ARGT_BE: |
| 314 | case ARGT_TAB: |
| 315 | case ARGT_SRV: |
| 316 | case ARGT_USR: |
| 317 | case ARGT_MAP: |
| 318 | default: |
| 319 | lua_pushnil(L); |
| 320 | break; |
| 321 | } |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | /* This function take one entrie in an LUA stack at the index "ud", |
| 326 | * and try to convert it in an HAProxy argument entry. This is useful |
| 327 | * with sample fetch wrappers. The input arguments are gived to the |
| 328 | * lua wrapper and converted as arg list by thi function. |
| 329 | */ |
| 330 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 331 | { |
| 332 | switch (lua_type(L, ud)) { |
| 333 | |
| 334 | case LUA_TNUMBER: |
| 335 | case LUA_TBOOLEAN: |
| 336 | arg->type = ARGT_SINT; |
| 337 | arg->data.sint = lua_tointeger(L, ud); |
| 338 | break; |
| 339 | |
| 340 | case LUA_TSTRING: |
| 341 | arg->type = ARGT_STR; |
| 342 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 343 | break; |
| 344 | |
| 345 | case LUA_TUSERDATA: |
| 346 | case LUA_TNIL: |
| 347 | case LUA_TTABLE: |
| 348 | case LUA_TFUNCTION: |
| 349 | case LUA_TTHREAD: |
| 350 | case LUA_TLIGHTUSERDATA: |
| 351 | arg->type = ARGT_SINT; |
| 352 | arg->data.uint = 0; |
| 353 | break; |
| 354 | } |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | /* the following functions are used to convert a struct sample |
| 359 | * in Lua type. This useful to convert the return of the |
| 360 | * fetchs or converters. |
| 361 | */ |
| 362 | static int hlua_smp2lua(lua_State *L, const struct sample *smp) |
| 363 | { |
| 364 | switch (smp->type) { |
| 365 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 366 | case SMP_T_BOOL: |
| 367 | case SMP_T_UINT: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 368 | lua_pushinteger(L, smp->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 369 | break; |
| 370 | |
| 371 | case SMP_T_BIN: |
| 372 | case SMP_T_STR: |
| 373 | lua_pushlstring(L, smp->data.str.str, smp->data.str.len); |
| 374 | break; |
| 375 | |
| 376 | case SMP_T_METH: |
| 377 | switch (smp->data.meth.meth) { |
| 378 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 379 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 380 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 381 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 382 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 383 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 384 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 385 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 386 | case HTTP_METH_OTHER: |
| 387 | lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len); |
| 388 | break; |
| 389 | default: |
| 390 | lua_pushnil(L); |
| 391 | break; |
| 392 | } |
| 393 | break; |
| 394 | |
| 395 | case SMP_T_IPV4: |
| 396 | case SMP_T_IPV6: |
| 397 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
| 398 | default: |
| 399 | lua_pushnil(L); |
| 400 | break; |
| 401 | } |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | /* the following functions are used to convert an Lua type in a |
| 406 | * struct sample. This is useful to provide data from a converter |
| 407 | * to the LUA code. |
| 408 | */ |
| 409 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 410 | { |
| 411 | switch (lua_type(L, ud)) { |
| 412 | |
| 413 | case LUA_TNUMBER: |
| 414 | smp->type = SMP_T_SINT; |
| 415 | smp->data.sint = lua_tointeger(L, ud); |
| 416 | break; |
| 417 | |
| 418 | |
| 419 | case LUA_TBOOLEAN: |
| 420 | smp->type = SMP_T_BOOL; |
| 421 | smp->data.uint = lua_toboolean(L, ud); |
| 422 | break; |
| 423 | |
| 424 | case LUA_TSTRING: |
| 425 | smp->type = SMP_T_STR; |
| 426 | smp->flags |= SMP_F_CONST; |
| 427 | smp->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.str.len); |
| 428 | break; |
| 429 | |
| 430 | case LUA_TUSERDATA: |
| 431 | case LUA_TNIL: |
| 432 | case LUA_TTABLE: |
| 433 | case LUA_TFUNCTION: |
| 434 | case LUA_TTHREAD: |
| 435 | case LUA_TLIGHTUSERDATA: |
| 436 | smp->type = SMP_T_BOOL; |
| 437 | smp->data.uint = 0; |
| 438 | break; |
| 439 | } |
| 440 | return 1; |
| 441 | } |
| 442 | |
| 443 | /* This function check the "argp" builded by another conversion function |
| 444 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 445 | * returns true or false. It can be adjust the types if there compatibles. |
| 446 | */ |
| 447 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, unsigned int mask) |
| 448 | { |
| 449 | int min_arg; |
| 450 | int idx; |
| 451 | |
| 452 | idx = 0; |
| 453 | min_arg = ARGM(mask); |
| 454 | mask >>= ARGM_BITS; |
| 455 | |
| 456 | while (1) { |
| 457 | |
| 458 | /* Check oversize. */ |
| 459 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 460 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* Check for mandatory arguments. */ |
| 464 | if (argp[idx].type == ARGT_STOP) { |
| 465 | if (idx + 1 < min_arg) |
| 466 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* Check for exceed the number of requiered argument. */ |
| 471 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 472 | argp[idx].type != ARGT_STOP) { |
| 473 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 474 | } |
| 475 | |
| 476 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 477 | argp[idx].type == ARGT_STOP) { |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* Compatibility mask. */ |
| 482 | switch (argp[idx].type) { |
| 483 | case ARGT_SINT: |
| 484 | switch (mask & ARGT_MASK) { |
| 485 | case ARGT_UINT: argp[idx].type = mask & ARGT_MASK; break; |
| 486 | case ARGT_TIME: argp[idx].type = mask & ARGT_MASK; break; |
| 487 | case ARGT_SIZE: argp[idx].type = mask & ARGT_MASK; break; |
| 488 | } |
| 489 | break; |
| 490 | } |
| 491 | |
| 492 | /* Check for type of argument. */ |
| 493 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 494 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 495 | arg_type_names[(mask & ARGT_MASK)], |
| 496 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 497 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 498 | } |
| 499 | |
| 500 | /* Next argument. */ |
| 501 | mask >>= ARGT_BITS; |
| 502 | idx++; |
| 503 | } |
| 504 | } |
| 505 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 506 | /* |
| 507 | * The following functions are used to make correspondance between the the |
| 508 | * executed lua pointer and the "struct hlua *" that contain the context. |
| 509 | * They run with the tree head "hlua_ctx", they just perform lookup in the |
| 510 | * tree. |
| 511 | * |
| 512 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
| 513 | * - hlua_delhlua : remove the association between hlua context and lua_state. |
| 514 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 515 | */ |
| 516 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 517 | { |
| 518 | struct ebpt_node *node; |
| 519 | |
| 520 | node = ebpt_lookup(&hlua_ctx, L); |
| 521 | if (!node) |
| 522 | return NULL; |
| 523 | return ebpt_entry(node, struct hlua, node); |
| 524 | } |
| 525 | static inline void hlua_delhlua(struct hlua *hlua) |
| 526 | { |
| 527 | if (hlua->node.key) |
| 528 | ebpt_delete(&hlua->node); |
| 529 | } |
| 530 | static inline void hlua_sethlua(struct hlua *hlua) |
| 531 | { |
| 532 | hlua->node.key = hlua->T; |
| 533 | ebpt_insert(&hlua_ctx, &hlua->node); |
| 534 | } |
| 535 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 536 | /* This function just ensure that the yield will be always |
| 537 | * returned with a timeout and permit to set some flags |
| 538 | */ |
| 539 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 540 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 541 | { |
| 542 | struct hlua *hlua = hlua_gethlua(L); |
| 543 | |
| 544 | /* Set the wake timeout. If timeout is required, we set |
| 545 | * the expiration time. |
| 546 | */ |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 547 | hlua->wake_time = tick_first(timeout, hlua->expire); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 548 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 549 | hlua->flags |= flags; |
| 550 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 551 | /* Process the yield. */ |
| 552 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 553 | } |
| 554 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 555 | /* This function initialises the Lua environment stored in the session. |
| 556 | * It must be called at the start of the session. This function creates |
| 557 | * an LUA coroutine. It can not be use to crete the main LUA context. |
| 558 | */ |
| 559 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 560 | { |
| 561 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 562 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 563 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 564 | lua->T = lua_newthread(gL.T); |
| 565 | if (!lua->T) { |
| 566 | lua->Tref = LUA_REFNIL; |
| 567 | return 0; |
| 568 | } |
| 569 | hlua_sethlua(lua); |
| 570 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 571 | lua->task = task; |
| 572 | return 1; |
| 573 | } |
| 574 | |
| 575 | /* Used to destroy the Lua coroutine when the attached session or task |
| 576 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 577 | * is not freed. |
| 578 | */ |
| 579 | void hlua_ctx_destroy(struct hlua *lua) |
| 580 | { |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 581 | if (!lua->T) |
| 582 | return; |
| 583 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 584 | /* Remove context. */ |
| 585 | hlua_delhlua(lua); |
| 586 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 587 | /* Purge all the pending signals. */ |
| 588 | hlua_com_purge(lua); |
| 589 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 590 | /* The thread is garbage collected by Lua. */ |
| 591 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 592 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 593 | } |
| 594 | |
| 595 | /* This function is used to restore the Lua context when a coroutine |
| 596 | * fails. This function copy the common memory between old coroutine |
| 597 | * and the new coroutine. The old coroutine is destroyed, and its |
| 598 | * replaced by the new coroutine. |
| 599 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 600 | * as string error message and it is copied in the new stack. |
| 601 | */ |
| 602 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 603 | { |
| 604 | lua_State *T; |
| 605 | int new_ref; |
| 606 | |
| 607 | /* Renew the main LUA stack doesn't have sense. */ |
| 608 | if (lua == &gL) |
| 609 | return 0; |
| 610 | |
| 611 | /* Remove context. */ |
| 612 | hlua_delhlua(lua); |
| 613 | |
| 614 | /* New Lua coroutine. */ |
| 615 | T = lua_newthread(gL.T); |
| 616 | if (!T) |
| 617 | return 0; |
| 618 | |
| 619 | /* Copy last error message. */ |
| 620 | if (keep_msg) |
| 621 | lua_xmove(lua->T, T, 1); |
| 622 | |
| 623 | /* Copy data between the coroutines. */ |
| 624 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 625 | lua_xmove(lua->T, T, 1); |
| 626 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 627 | |
| 628 | /* Destroy old data. */ |
| 629 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 630 | |
| 631 | /* The thread is garbage collected by Lua. */ |
| 632 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 633 | |
| 634 | /* Fill the struct with the new coroutine values. */ |
| 635 | lua->Mref = new_ref; |
| 636 | lua->T = T; |
| 637 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 638 | |
| 639 | /* Set context. */ |
| 640 | hlua_sethlua(lua); |
| 641 | |
| 642 | return 1; |
| 643 | } |
| 644 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 645 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 646 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 647 | struct hlua *hlua = hlua_gethlua(L); |
| 648 | |
| 649 | /* Lua cannot yield when its returning from a function, |
| 650 | * so, we can fix the interrupt hook to 1 instruction, |
| 651 | * expecting that the function is finnished. |
| 652 | */ |
| 653 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 654 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | /* restore the interrupt condition. */ |
| 659 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 660 | |
| 661 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 662 | * If the state is not yieldable, trying yield causes an error. |
| 663 | */ |
| 664 | if (lua_isyieldable(L)) |
| 665 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 666 | |
| 667 | /* If we cannot yield, check the timeout. */ |
| 668 | if (tick_is_expired(hlua->expire, now_ms)) { |
| 669 | lua_pushfstring(L, "execution timeout"); |
| 670 | WILL_LJMP(lua_error(L)); |
| 671 | } |
| 672 | |
| 673 | /* Try to interrupt the process at the end of the current |
| 674 | * unyieldable function. |
| 675 | */ |
| 676 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 679 | /* This function start or resumes the Lua stack execution. If the flag |
| 680 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 681 | * The function return an error. |
| 682 | * |
| 683 | * The function can returns 4 values: |
| 684 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 685 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 686 | * task wakeup. |
| 687 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 688 | * the top of the stack. |
| 689 | * - HLUA_E_ERR : An error has occured without error message. |
| 690 | * |
| 691 | * If an error occured, the stack is renewed and it is ready to run new |
| 692 | * LUA code. |
| 693 | */ |
| 694 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 695 | { |
| 696 | int ret; |
| 697 | const char *msg; |
| 698 | |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 699 | HLUA_SET_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 700 | |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 701 | /* If we want to resume the task, then check first the execution timeout. |
| 702 | * if it is reached, we can interrupt the Lua processing. |
| 703 | */ |
| 704 | if (tick_is_expired(lua->expire, now_ms)) |
| 705 | goto timeout_reached; |
| 706 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 707 | resume_execution: |
| 708 | |
| 709 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 710 | * instructions. it is used for preventing infinite loops. |
| 711 | */ |
| 712 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 713 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 714 | /* Remove all flags except the running flags. */ |
| 715 | lua->flags = HLUA_RUN; |
| 716 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 717 | /* Call the function. */ |
| 718 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 719 | switch (ret) { |
| 720 | |
| 721 | case LUA_OK: |
| 722 | ret = HLUA_E_OK; |
| 723 | break; |
| 724 | |
| 725 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 726 | /* Check if the execution timeout is expired. It it is the case, we |
| 727 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 728 | */ |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 729 | if (tick_is_expired(lua->expire, now_ms)) { |
| 730 | |
| 731 | timeout_reached: |
| 732 | |
| 733 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 734 | if (!lua_checkstack(lua->T, 1)) { |
| 735 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 736 | break; |
| 737 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 738 | lua_pushfstring(lua->T, "execution timeout"); |
| 739 | ret = HLUA_E_ERRMSG; |
| 740 | break; |
| 741 | } |
| 742 | /* Process the forced yield. if the general yield is not allowed or |
| 743 | * if no task were associated this the current Lua execution |
| 744 | * coroutine, we resume the execution. Else we want to return in the |
| 745 | * scheduler and we want to be waked up again, to continue the |
| 746 | * current Lua execution. So we schedule our own task. |
| 747 | */ |
| 748 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 749 | if (!yield_allowed || !lua->task) |
| 750 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 751 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 752 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 753 | if (!yield_allowed) { |
| 754 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 755 | if (!lua_checkstack(lua->T, 1)) { |
| 756 | ret = HLUA_E_ERR; |
| 757 | break; |
| 758 | } |
| 759 | lua_pushfstring(lua->T, "yield not allowed"); |
| 760 | ret = HLUA_E_ERRMSG; |
| 761 | break; |
| 762 | } |
| 763 | ret = HLUA_E_AGAIN; |
| 764 | break; |
| 765 | |
| 766 | case LUA_ERRRUN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 767 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 768 | if (!lua_checkstack(lua->T, 1)) { |
| 769 | ret = HLUA_E_ERR; |
| 770 | break; |
| 771 | } |
| 772 | msg = lua_tostring(lua->T, -1); |
| 773 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 774 | lua_pop(lua->T, 1); |
| 775 | if (msg) |
| 776 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 777 | else |
| 778 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 779 | ret = HLUA_E_ERRMSG; |
| 780 | break; |
| 781 | |
| 782 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 783 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 784 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 785 | if (!lua_checkstack(lua->T, 1)) { |
| 786 | ret = HLUA_E_ERR; |
| 787 | break; |
| 788 | } |
| 789 | lua_pushfstring(lua->T, "out of memory error"); |
| 790 | ret = HLUA_E_ERRMSG; |
| 791 | break; |
| 792 | |
| 793 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 794 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 795 | if (!lua_checkstack(lua->T, 1)) { |
| 796 | ret = HLUA_E_ERR; |
| 797 | break; |
| 798 | } |
| 799 | msg = lua_tostring(lua->T, -1); |
| 800 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 801 | lua_pop(lua->T, 1); |
| 802 | if (msg) |
| 803 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 804 | else |
| 805 | lua_pushfstring(lua->T, "message handler error"); |
| 806 | ret = HLUA_E_ERRMSG; |
| 807 | break; |
| 808 | |
| 809 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 810 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 811 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 812 | if (!lua_checkstack(lua->T, 1)) { |
| 813 | ret = HLUA_E_ERR; |
| 814 | break; |
| 815 | } |
| 816 | lua_pushfstring(lua->T, "unknonwn error"); |
| 817 | ret = HLUA_E_ERRMSG; |
| 818 | break; |
| 819 | } |
| 820 | |
| 821 | switch (ret) { |
| 822 | case HLUA_E_AGAIN: |
| 823 | break; |
| 824 | |
| 825 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 826 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 827 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 828 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 829 | break; |
| 830 | |
| 831 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 832 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 833 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 834 | hlua_ctx_renew(lua, 0); |
| 835 | break; |
| 836 | |
| 837 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 838 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 839 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 840 | break; |
| 841 | } |
| 842 | |
| 843 | return ret; |
| 844 | } |
| 845 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 846 | /* This function is an LUA binding. It provides a function |
| 847 | * for deleting ACL from a referenced ACL file. |
| 848 | */ |
| 849 | __LJMP static int hlua_del_acl(lua_State *L) |
| 850 | { |
| 851 | const char *name; |
| 852 | const char *key; |
| 853 | struct pat_ref *ref; |
| 854 | |
| 855 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 856 | |
| 857 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 858 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 859 | |
| 860 | ref = pat_ref_lookup(name); |
| 861 | if (!ref) |
| 862 | WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name)); |
| 863 | |
| 864 | pat_ref_delete(ref, key); |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | /* This function is an LUA binding. It provides a function |
| 869 | * for deleting map entry from a referenced map file. |
| 870 | */ |
| 871 | static int hlua_del_map(lua_State *L) |
| 872 | { |
| 873 | const char *name; |
| 874 | const char *key; |
| 875 | struct pat_ref *ref; |
| 876 | |
| 877 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 878 | |
| 879 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 880 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 881 | |
| 882 | ref = pat_ref_lookup(name); |
| 883 | if (!ref) |
| 884 | WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name)); |
| 885 | |
| 886 | pat_ref_delete(ref, key); |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /* This function is an LUA binding. It provides a function |
| 891 | * for adding ACL pattern from a referenced ACL file. |
| 892 | */ |
| 893 | static int hlua_add_acl(lua_State *L) |
| 894 | { |
| 895 | const char *name; |
| 896 | const char *key; |
| 897 | struct pat_ref *ref; |
| 898 | |
| 899 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 900 | |
| 901 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 902 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 903 | |
| 904 | ref = pat_ref_lookup(name); |
| 905 | if (!ref) |
| 906 | WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name)); |
| 907 | |
| 908 | if (pat_ref_find_elt(ref, key) == NULL) |
| 909 | pat_ref_add(ref, key, NULL, NULL); |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | /* This function is an LUA binding. It provides a function |
| 914 | * for setting map pattern and sample from a referenced map |
| 915 | * file. |
| 916 | */ |
| 917 | static int hlua_set_map(lua_State *L) |
| 918 | { |
| 919 | const char *name; |
| 920 | const char *key; |
| 921 | const char *value; |
| 922 | struct pat_ref *ref; |
| 923 | |
| 924 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 925 | |
| 926 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 927 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 928 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 929 | |
| 930 | ref = pat_ref_lookup(name); |
| 931 | if (!ref) |
| 932 | WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name)); |
| 933 | |
| 934 | if (pat_ref_find_elt(ref, key) != NULL) |
| 935 | pat_ref_set(ref, key, value, NULL); |
| 936 | else |
| 937 | pat_ref_add(ref, key, value, NULL); |
| 938 | return 0; |
| 939 | } |
| 940 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 941 | /* A class is a lot of memory that contain data. This data can be a table, |
| 942 | * an integer or user data. This data is associated with a metatable. This |
| 943 | * metatable have an original version registred in the global context with |
| 944 | * the name of the object (_G[<name>] = <metable> ). |
| 945 | * |
| 946 | * A metable is a table that modify the standard behavior of a standard |
| 947 | * access to the associated data. The entries of this new metatable are |
| 948 | * defined as is: |
| 949 | * |
| 950 | * http://lua-users.org/wiki/MetatableEvents |
| 951 | * |
| 952 | * __index |
| 953 | * |
| 954 | * we access an absent field in a table, the result is nil. This is |
| 955 | * true, but it is not the whole truth. Actually, such access triggers |
| 956 | * the interpreter to look for an __index metamethod: If there is no |
| 957 | * such method, as usually happens, then the access results in nil; |
| 958 | * otherwise, the metamethod will provide the result. |
| 959 | * |
| 960 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 961 | * the key does not appear in the table, but the metatable has an __index |
| 962 | * property: |
| 963 | * |
| 964 | * - if the value is a function, the function is called, passing in the |
| 965 | * table and the key; the return value of that function is returned as |
| 966 | * the result. |
| 967 | * |
| 968 | * - if the value is another table, the value of the key in that table is |
| 969 | * asked for and returned (and if it doesn't exist in that table, but that |
| 970 | * table's metatable has an __index property, then it continues on up) |
| 971 | * |
| 972 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 973 | * |
| 974 | * http://www.lua.org/pil/13.4.1.html |
| 975 | * |
| 976 | * __newindex |
| 977 | * |
| 978 | * Like __index, but control property assignment. |
| 979 | * |
| 980 | * __mode - Control weak references. A string value with one or both |
| 981 | * of the characters 'k' and 'v' which specifies that the the |
| 982 | * keys and/or values in the table are weak references. |
| 983 | * |
| 984 | * __call - Treat a table like a function. When a table is followed by |
| 985 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 986 | * a __call key pointing to a function, that function is invoked |
| 987 | * (passing any specified arguments) and the return value is |
| 988 | * returned. |
| 989 | * |
| 990 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 991 | * called, if the metatable for myTable has a __metatable |
| 992 | * key, the value of that key is returned instead of the |
| 993 | * actual metatable. |
| 994 | * |
| 995 | * __tostring - Control string representation. When the builtin |
| 996 | * "tostring( myTable )" function is called, if the metatable |
| 997 | * for myTable has a __tostring property set to a function, |
| 998 | * that function is invoked (passing myTable to it) and the |
| 999 | * return value is used as the string representation. |
| 1000 | * |
| 1001 | * __len - Control table length. When the table length is requested using |
| 1002 | * the length operator ( '#' ), if the metatable for myTable has |
| 1003 | * a __len key pointing to a function, that function is invoked |
| 1004 | * (passing myTable to it) and the return value used as the value |
| 1005 | * of "#myTable". |
| 1006 | * |
| 1007 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1008 | * collected, if the metatable has a __gc field pointing to a |
| 1009 | * function, that function is first invoked, passing the userdata |
| 1010 | * to it. The __gc metamethod is not called for tables. |
| 1011 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1012 | * |
| 1013 | * Special metamethods for redefining standard operators: |
| 1014 | * http://www.lua.org/pil/13.1.html |
| 1015 | * |
| 1016 | * __add "+" |
| 1017 | * __sub "-" |
| 1018 | * __mul "*" |
| 1019 | * __div "/" |
| 1020 | * __unm "!" |
| 1021 | * __pow "^" |
| 1022 | * __concat ".." |
| 1023 | * |
| 1024 | * Special methods for redfining standar relations |
| 1025 | * http://www.lua.org/pil/13.2.html |
| 1026 | * |
| 1027 | * __eq "==" |
| 1028 | * __lt "<" |
| 1029 | * __le "<=" |
| 1030 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1031 | |
| 1032 | /* |
| 1033 | * |
| 1034 | * |
| 1035 | * Class Socket |
| 1036 | * |
| 1037 | * |
| 1038 | */ |
| 1039 | |
| 1040 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1041 | { |
| 1042 | return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
| 1043 | } |
| 1044 | |
| 1045 | /* This function is the handler called for each I/O on the established |
| 1046 | * connection. It is used for notify space avalaible to send or data |
| 1047 | * received. |
| 1048 | */ |
| 1049 | static void hlua_socket_handler(struct stream_interface *si) |
| 1050 | { |
| 1051 | struct appctx *appctx = objt_appctx(si->end); |
| 1052 | struct connection *c = objt_conn(si->ib->cons->end); |
| 1053 | |
| 1054 | /* Wakeup the main session if the client connection is closed. */ |
| 1055 | if (!c || channel_output_closed(si->ib) || channel_input_closed(si->ob)) { |
| 1056 | if (appctx->ctx.hlua.socket) { |
| 1057 | appctx->ctx.hlua.socket->s = NULL; |
| 1058 | appctx->ctx.hlua.socket = NULL; |
| 1059 | } |
| 1060 | si_shutw(si); |
| 1061 | si_shutr(si); |
| 1062 | si->ib->flags |= CF_READ_NULL; |
| 1063 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1064 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1065 | return; |
| 1066 | } |
| 1067 | |
| 1068 | if (!(c->flags & CO_FL_CONNECTED)) |
| 1069 | return; |
| 1070 | |
| 1071 | /* This function is called after the connect. */ |
| 1072 | appctx->ctx.hlua.connected = 1; |
| 1073 | |
| 1074 | /* Wake the tasks which wants to write if the buffer have avalaible space. */ |
| 1075 | if (channel_may_recv(si->ob)) |
| 1076 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1077 | |
| 1078 | /* Wake the tasks which wants to read if the buffer contains data. */ |
| 1079 | if (channel_is_empty(si->ib)) |
| 1080 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1081 | } |
| 1082 | |
| 1083 | /* This function is called when the "struct session" is destroyed. |
| 1084 | * Remove the link from the object to this session. |
| 1085 | * Wake all the pending signals. |
| 1086 | */ |
| 1087 | static void hlua_socket_release(struct stream_interface *si) |
| 1088 | { |
| 1089 | struct appctx *appctx = objt_appctx(si->end); |
| 1090 | |
| 1091 | /* Remove my link in the original object. */ |
| 1092 | if (appctx->ctx.hlua.socket) |
| 1093 | appctx->ctx.hlua.socket->s = NULL; |
| 1094 | |
| 1095 | /* Wake all the task waiting for me. */ |
| 1096 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1097 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1098 | } |
| 1099 | |
| 1100 | /* If the garbage collectio of the object is launch, nobody |
| 1101 | * uses this object. If the session does not exists, just quit. |
| 1102 | * Send the shutdown signal to the session. In some cases, |
| 1103 | * pending signal can rest in the read and write lists. destroy |
| 1104 | * it. |
| 1105 | */ |
| 1106 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1107 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1108 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1109 | struct appctx *appctx; |
| 1110 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1111 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1112 | |
| 1113 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1114 | if (!socket->s) |
| 1115 | return 0; |
| 1116 | |
| 1117 | /* Remove all reference between the Lua stack and the coroutine session. */ |
| 1118 | appctx = objt_appctx(socket->s->si[0].end); |
| 1119 | session_shutdown(socket->s, SN_ERR_KILLED); |
| 1120 | socket->s = NULL; |
| 1121 | appctx->ctx.hlua.socket = NULL; |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | /* The close function send shutdown signal and break the |
| 1127 | * links between the session and the object. |
| 1128 | */ |
| 1129 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1130 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1131 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1132 | struct appctx *appctx; |
| 1133 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1134 | MAY_LJMP(check_args(L, 1, "close")); |
| 1135 | |
| 1136 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1137 | if (!socket->s) |
| 1138 | return 0; |
| 1139 | |
| 1140 | /* Close the session and remove the associated stop task. */ |
| 1141 | session_shutdown(socket->s, SN_ERR_KILLED); |
| 1142 | appctx = objt_appctx(socket->s->si[0].end); |
| 1143 | appctx->ctx.hlua.socket = NULL; |
| 1144 | socket->s = NULL; |
| 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | /* This Lua function assumes that the stack contain three parameters. |
| 1150 | * 1 - USERDATA containing a struct socket |
| 1151 | * 2 - INTEGER with values of the macro defined below |
| 1152 | * If the integer is -1, we must read at most one line. |
| 1153 | * If the integer is -2, we ust read all the data until the |
| 1154 | * end of the stream. |
| 1155 | * If the integer is positive value, we must read a number of |
| 1156 | * bytes corresponding to this value. |
| 1157 | */ |
| 1158 | #define HLSR_READ_LINE (-1) |
| 1159 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1160 | __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1161 | { |
| 1162 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1163 | int wanted = lua_tointeger(L, 2); |
| 1164 | struct hlua *hlua = hlua_gethlua(L); |
| 1165 | struct appctx *appctx; |
| 1166 | int len; |
| 1167 | int nblk; |
| 1168 | char *blk1; |
| 1169 | int len1; |
| 1170 | char *blk2; |
| 1171 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1172 | int skip_at_end = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1173 | |
| 1174 | /* Check if this lua stack is schedulable. */ |
| 1175 | if (!hlua || !hlua->task) |
| 1176 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1177 | "'frontend', 'backend' or 'task'")); |
| 1178 | |
| 1179 | /* check for connection closed. If some data where read, return it. */ |
| 1180 | if (!socket->s) |
| 1181 | goto connection_closed; |
| 1182 | |
| 1183 | if (wanted == HLSR_READ_LINE) { |
| 1184 | |
| 1185 | /* Read line. */ |
| 1186 | nblk = bo_getline_nc(socket->s->si[0].ob, &blk1, &len1, &blk2, &len2); |
| 1187 | if (nblk < 0) /* Connection close. */ |
| 1188 | goto connection_closed; |
| 1189 | if (nblk == 0) /* No data avalaible. */ |
| 1190 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1191 | |
| 1192 | /* remove final \r\n. */ |
| 1193 | if (nblk == 1) { |
| 1194 | if (blk1[len1-1] == '\n') { |
| 1195 | len1--; |
| 1196 | skip_at_end++; |
| 1197 | if (blk1[len1-1] == '\r') { |
| 1198 | len1--; |
| 1199 | skip_at_end++; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | else { |
| 1204 | if (blk2[len2-1] == '\n') { |
| 1205 | len2--; |
| 1206 | skip_at_end++; |
| 1207 | if (blk2[len2-1] == '\r') { |
| 1208 | len2--; |
| 1209 | skip_at_end++; |
| 1210 | } |
| 1211 | } |
| 1212 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | else if (wanted == HLSR_READ_ALL) { |
| 1216 | |
| 1217 | /* Read all the available data. */ |
| 1218 | nblk = bo_getblk_nc(socket->s->si[0].ob, &blk1, &len1, &blk2, &len2); |
| 1219 | if (nblk < 0) /* Connection close. */ |
| 1220 | goto connection_closed; |
| 1221 | if (nblk == 0) /* No data avalaible. */ |
| 1222 | goto connection_empty; |
| 1223 | } |
| 1224 | |
| 1225 | else { |
| 1226 | |
| 1227 | /* Read a block of data. */ |
| 1228 | nblk = bo_getblk_nc(socket->s->si[0].ob, &blk1, &len1, &blk2, &len2); |
| 1229 | if (nblk < 0) /* Connection close. */ |
| 1230 | goto connection_closed; |
| 1231 | if (nblk == 0) /* No data avalaible. */ |
| 1232 | goto connection_empty; |
| 1233 | |
| 1234 | if (len1 > wanted) { |
| 1235 | nblk = 1; |
| 1236 | len1 = wanted; |
| 1237 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1238 | len2 = wanted - len1; |
| 1239 | } |
| 1240 | |
| 1241 | len = len1; |
| 1242 | |
| 1243 | luaL_addlstring(&socket->b, blk1, len1); |
| 1244 | if (nblk == 2) { |
| 1245 | len += len2; |
| 1246 | luaL_addlstring(&socket->b, blk2, len2); |
| 1247 | } |
| 1248 | |
| 1249 | /* Consume data. */ |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1250 | bo_skip(socket->s->si[0].ob, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1251 | |
| 1252 | /* Don't wait anything. */ |
| 1253 | si_update(&socket->s->si[0]); |
| 1254 | |
| 1255 | /* If the pattern reclaim to read all the data |
| 1256 | * in the connection, got out. |
| 1257 | */ |
| 1258 | if (wanted == HLSR_READ_ALL) |
| 1259 | goto connection_empty; |
| 1260 | else if (wanted >= 0 && len < wanted) |
| 1261 | goto connection_empty; |
| 1262 | |
| 1263 | /* Return result. */ |
| 1264 | luaL_pushresult(&socket->b); |
| 1265 | return 1; |
| 1266 | |
| 1267 | connection_closed: |
| 1268 | |
| 1269 | /* If the buffer containds data. */ |
| 1270 | if (socket->b.n > 0) { |
| 1271 | luaL_pushresult(&socket->b); |
| 1272 | return 1; |
| 1273 | } |
| 1274 | lua_pushnil(L); |
| 1275 | lua_pushstring(L, "connection closed."); |
| 1276 | return 2; |
| 1277 | |
| 1278 | connection_empty: |
| 1279 | |
| 1280 | appctx = objt_appctx(socket->s->si[0].end); |
| 1281 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read)) |
| 1282 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1283 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | /* This Lus function gets two parameters. The first one can be string |
| 1288 | * or a number. If the string is "*l", the user require one line. If |
| 1289 | * the string is "*a", the user require all the content of the stream. |
| 1290 | * If the value is a number, the user require a number of bytes equal |
| 1291 | * to the value. The default value is "*l" (a line). |
| 1292 | * |
| 1293 | * This paraeter with a variable type is converted in integer. This |
| 1294 | * integer takes this values: |
| 1295 | * -1 : read a line |
| 1296 | * -2 : read all the stream |
| 1297 | * >0 : amount if bytes. |
| 1298 | * |
| 1299 | * The second parameter is optinal. It contains a string that must be |
| 1300 | * concatenated with the read data. |
| 1301 | */ |
| 1302 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1303 | { |
| 1304 | int wanted = HLSR_READ_LINE; |
| 1305 | const char *pattern; |
| 1306 | int type; |
| 1307 | char *error; |
| 1308 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1309 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1310 | |
| 1311 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1312 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1313 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1314 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1315 | |
| 1316 | /* check for pattern. */ |
| 1317 | if (lua_gettop(L) >= 2) { |
| 1318 | type = lua_type(L, 2); |
| 1319 | if (type == LUA_TSTRING) { |
| 1320 | pattern = lua_tostring(L, 2); |
| 1321 | if (strcmp(pattern, "*a") == 0) |
| 1322 | wanted = HLSR_READ_ALL; |
| 1323 | else if (strcmp(pattern, "*l") == 0) |
| 1324 | wanted = HLSR_READ_LINE; |
| 1325 | else { |
| 1326 | wanted = strtoll(pattern, &error, 10); |
| 1327 | if (*error != '\0') |
| 1328 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1329 | } |
| 1330 | } |
| 1331 | else if (type == LUA_TNUMBER) { |
| 1332 | wanted = lua_tointeger(L, 2); |
| 1333 | if (wanted < 0) |
| 1334 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /* Set pattern. */ |
| 1339 | lua_pushinteger(L, wanted); |
| 1340 | lua_replace(L, 2); |
| 1341 | |
| 1342 | /* init bufffer, and fiil it wih prefix. */ |
| 1343 | luaL_buffinit(L, &socket->b); |
| 1344 | |
| 1345 | /* Check prefix. */ |
| 1346 | if (lua_gettop(L) >= 3) { |
| 1347 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1348 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1349 | pattern = lua_tolstring(L, 3, &len); |
| 1350 | luaL_addlstring(&socket->b, pattern, len); |
| 1351 | } |
| 1352 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1353 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | /* Write the Lua input string in the output buffer. |
| 1357 | * This fucntion returns a yield if no space are available. |
| 1358 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1359 | static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1360 | { |
| 1361 | struct hlua_socket *socket; |
| 1362 | struct hlua *hlua = hlua_gethlua(L); |
| 1363 | struct appctx *appctx; |
| 1364 | size_t buf_len; |
| 1365 | const char *buf; |
| 1366 | int len; |
| 1367 | int send_len; |
| 1368 | int sent; |
| 1369 | |
| 1370 | /* Check if this lua stack is schedulable. */ |
| 1371 | if (!hlua || !hlua->task) |
| 1372 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1373 | "'frontend', 'backend' or 'task'")); |
| 1374 | |
| 1375 | /* Get object */ |
| 1376 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1377 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1378 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1379 | |
| 1380 | /* Check for connection close. */ |
| 1381 | if (!socket->s || channel_output_closed(socket->s->req)) { |
| 1382 | lua_pushinteger(L, -1); |
| 1383 | return 1; |
| 1384 | } |
| 1385 | |
| 1386 | /* Update the input buffer data. */ |
| 1387 | buf += sent; |
| 1388 | send_len = buf_len - sent; |
| 1389 | |
| 1390 | /* All the data are sent. */ |
| 1391 | if (sent >= buf_len) |
| 1392 | return 1; /* Implicitly return the length sent. */ |
| 1393 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1394 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1395 | * the request buffer if its not required. |
| 1396 | */ |
| 1397 | if (socket->s->req->buf->size == 0) { |
| 1398 | if (!session_alloc_recv_buffer(socket->s, &socket->s->req->buf)) { |
| 1399 | socket->s->req->prod->flags |= SI_FL_WAIT_ROOM; |
| 1400 | goto hlua_socket_write_yield_return; |
| 1401 | } |
| 1402 | } |
| 1403 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1404 | /* Check for avalaible space. */ |
| 1405 | len = buffer_total_space(socket->s->si[0].ib->buf); |
| 1406 | if (len <= 0) |
| 1407 | goto hlua_socket_write_yield_return; |
| 1408 | |
| 1409 | /* send data */ |
| 1410 | if (len < send_len) |
| 1411 | send_len = len; |
| 1412 | len = bi_putblk(socket->s->si[0].ib, buf+sent, send_len); |
| 1413 | |
| 1414 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1415 | * the data" (-2) are not expected because the available length |
| 1416 | * is tested. |
| 1417 | * Other unknown error are also not expected. |
| 1418 | */ |
| 1419 | if (len <= 0) { |
| 1420 | MAY_LJMP(hlua_socket_close(L)); |
| 1421 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1422 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1423 | return 1; |
| 1424 | } |
| 1425 | |
| 1426 | /* update buffers. */ |
| 1427 | si_update(&socket->s->si[0]); |
| 1428 | socket->s->si[0].ib->rex = TICK_ETERNITY; |
| 1429 | socket->s->si[0].ob->wex = TICK_ETERNITY; |
| 1430 | |
| 1431 | /* Update length sent. */ |
| 1432 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1433 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1434 | |
| 1435 | /* All the data buffer is sent ? */ |
| 1436 | if (sent + len >= buf_len) |
| 1437 | return 1; |
| 1438 | |
| 1439 | hlua_socket_write_yield_return: |
| 1440 | appctx = objt_appctx(socket->s->si[0].end); |
| 1441 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 1442 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1443 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | /* This function initiate the send of data. It just check the input |
| 1448 | * parameters and push an integer in the Lua stack that contain the |
| 1449 | * amount of data writed in the buffer. This is used by the function |
| 1450 | * "hlua_socket_write_yield" that can yield. |
| 1451 | * |
| 1452 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 1453 | * the associated object. The second is a string buffer. The third is |
| 1454 | * a facultative integer that represents where is the buffer position |
| 1455 | * of the start of the data that can send. The first byte is the |
| 1456 | * position "1". The default value is "1". The fourth argument is a |
| 1457 | * facultative integer that represents where is the buffer position |
| 1458 | * of the end of the data that can send. The default is the last byte. |
| 1459 | */ |
| 1460 | static int hlua_socket_send(struct lua_State *L) |
| 1461 | { |
| 1462 | int i; |
| 1463 | int j; |
| 1464 | const char *buf; |
| 1465 | size_t buf_len; |
| 1466 | |
| 1467 | /* Check number of arguments. */ |
| 1468 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 1469 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 1470 | |
| 1471 | /* Get the string. */ |
| 1472 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 1473 | |
| 1474 | /* Get and check j. */ |
| 1475 | if (lua_gettop(L) == 4) { |
| 1476 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 1477 | if (j < 0) |
| 1478 | j = buf_len + j + 1; |
| 1479 | if (j > buf_len) |
| 1480 | j = buf_len + 1; |
| 1481 | lua_pop(L, 1); |
| 1482 | } |
| 1483 | else |
| 1484 | j = buf_len; |
| 1485 | |
| 1486 | /* Get and check i. */ |
| 1487 | if (lua_gettop(L) == 3) { |
| 1488 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 1489 | if (i < 0) |
| 1490 | i = buf_len + i + 1; |
| 1491 | if (i > buf_len) |
| 1492 | i = buf_len + 1; |
| 1493 | lua_pop(L, 1); |
| 1494 | } else |
| 1495 | i = 1; |
| 1496 | |
| 1497 | /* Check bth i and j. */ |
| 1498 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1499 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1500 | return 1; |
| 1501 | } |
| 1502 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1503 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1504 | return 1; |
| 1505 | } |
| 1506 | if (i == 0) |
| 1507 | i = 1; |
| 1508 | if (j == 0) |
| 1509 | j = 1; |
| 1510 | |
| 1511 | /* Pop the string. */ |
| 1512 | lua_pop(L, 1); |
| 1513 | |
| 1514 | /* Update the buffer length. */ |
| 1515 | buf += i - 1; |
| 1516 | buf_len = j - i + 1; |
| 1517 | lua_pushlstring(L, buf, buf_len); |
| 1518 | |
| 1519 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1520 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1521 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1522 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | #define SOCKET_INFO_EXPANDED_FORM "[0000:0000:0000:0000:0000:0000:0000:0000]:12345" |
| 1526 | static char _socket_info_expanded_form[] = SOCKET_INFO_EXPANDED_FORM; |
| 1527 | #define SOCKET_INFO_MAX_LEN (sizeof(_socket_info_expanded_form)) |
| 1528 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 1529 | { |
| 1530 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 1531 | int ret; |
| 1532 | int len; |
| 1533 | char *p; |
| 1534 | |
| 1535 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 1536 | if (ret <= 0) { |
| 1537 | lua_pushnil(L); |
| 1538 | return 1; |
| 1539 | } |
| 1540 | |
| 1541 | if (ret == AF_UNIX) { |
| 1542 | lua_pushstring(L, buffer+1); |
| 1543 | return 1; |
| 1544 | } |
| 1545 | else if (ret == AF_INET6) { |
| 1546 | buffer[0] = '['; |
| 1547 | len = strlen(buffer); |
| 1548 | buffer[len] = ']'; |
| 1549 | len++; |
| 1550 | buffer[len] = ':'; |
| 1551 | len++; |
| 1552 | p = buffer; |
| 1553 | } |
| 1554 | else if (ret == AF_INET) { |
| 1555 | p = buffer + 1; |
| 1556 | len = strlen(p); |
| 1557 | p[len] = ':'; |
| 1558 | len++; |
| 1559 | } |
| 1560 | else { |
| 1561 | lua_pushnil(L); |
| 1562 | return 1; |
| 1563 | } |
| 1564 | |
| 1565 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 1566 | lua_pushnil(L); |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | lua_pushstring(L, p); |
| 1571 | return 1; |
| 1572 | } |
| 1573 | |
| 1574 | /* Returns information about the peer of the connection. */ |
| 1575 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 1576 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1577 | struct hlua_socket *socket; |
| 1578 | struct connection *conn; |
| 1579 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 1581 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1582 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1583 | |
| 1584 | /* Check if the tcp object is avalaible. */ |
| 1585 | if (!socket->s) { |
| 1586 | lua_pushnil(L); |
| 1587 | return 1; |
| 1588 | } |
| 1589 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1590 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1591 | if (!conn) { |
| 1592 | lua_pushnil(L); |
| 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
| 1597 | unsigned int salen = sizeof(conn->addr.to); |
| 1598 | if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) { |
| 1599 | lua_pushnil(L); |
| 1600 | return 1; |
| 1601 | } |
| 1602 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 1603 | } |
| 1604 | |
| 1605 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 1606 | } |
| 1607 | |
| 1608 | /* Returns information about my connection side. */ |
| 1609 | static int hlua_socket_getsockname(struct lua_State *L) |
| 1610 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1611 | struct hlua_socket *socket; |
| 1612 | struct connection *conn; |
| 1613 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1614 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 1615 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1616 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1617 | |
| 1618 | /* Check if the tcp object is avalaible. */ |
| 1619 | if (!socket->s) { |
| 1620 | lua_pushnil(L); |
| 1621 | return 1; |
| 1622 | } |
| 1623 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1624 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | if (!conn) { |
| 1626 | lua_pushnil(L); |
| 1627 | return 1; |
| 1628 | } |
| 1629 | |
| 1630 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
| 1631 | unsigned int salen = sizeof(conn->addr.from); |
| 1632 | if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) { |
| 1633 | lua_pushnil(L); |
| 1634 | return 1; |
| 1635 | } |
| 1636 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 1637 | } |
| 1638 | |
| 1639 | return hlua_socket_info(L, &conn->addr.from); |
| 1640 | } |
| 1641 | |
| 1642 | /* This struct define the applet. */ |
| 1643 | static struct si_applet update_applet = { |
| 1644 | .obj_type = OBJ_TYPE_APPLET, |
| 1645 | .name = "<LUA_TCP>", |
| 1646 | .fct = hlua_socket_handler, |
| 1647 | .release = hlua_socket_release, |
| 1648 | }; |
| 1649 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1650 | __LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | { |
| 1652 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1653 | struct hlua *hlua = hlua_gethlua(L); |
| 1654 | struct appctx *appctx; |
| 1655 | |
| 1656 | /* Check for connection close. */ |
| 1657 | if (!hlua || !socket->s || channel_output_closed(socket->s->req)) { |
| 1658 | lua_pushnil(L); |
| 1659 | lua_pushstring(L, "Can't connect"); |
| 1660 | return 2; |
| 1661 | } |
| 1662 | |
| 1663 | appctx = objt_appctx(socket->s->si[0].end); |
| 1664 | |
| 1665 | /* Check for connection established. */ |
| 1666 | if (appctx->ctx.hlua.connected) { |
| 1667 | lua_pushinteger(L, 1); |
| 1668 | return 1; |
| 1669 | } |
| 1670 | |
| 1671 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 1672 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1673 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1674 | return 0; |
| 1675 | } |
| 1676 | |
| 1677 | /* This function fail or initite the connection. */ |
| 1678 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 1679 | { |
| 1680 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1681 | int port; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | const char *ip; |
| 1683 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 1684 | struct hlua *hlua; |
| 1685 | struct appctx *appctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1686 | |
| 1687 | MAY_LJMP(check_args(L, 3, "connect")); |
| 1688 | |
| 1689 | /* Get args. */ |
| 1690 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1691 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1692 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | |
| 1694 | conn = si_alloc_conn(socket->s->req->cons, 0); |
| 1695 | if (!conn) |
| 1696 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 1697 | |
| 1698 | /* Parse ip address. */ |
| 1699 | conn->addr.to.ss_family = AF_UNSPEC; |
| 1700 | if (!str2ip2(ip, &conn->addr.to, 0)) |
| 1701 | WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip)); |
| 1702 | |
| 1703 | /* Set port. */ |
| 1704 | if (conn->addr.to.ss_family == AF_INET) |
| 1705 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 1706 | else if (conn->addr.to.ss_family == AF_INET6) |
| 1707 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 1708 | |
| 1709 | /* it is important not to call the wakeup function directly but to |
| 1710 | * pass through task_wakeup(), because this one knows how to apply |
| 1711 | * priorities to tasks. |
| 1712 | */ |
| 1713 | task_wakeup(socket->s->task, TASK_WOKEN_INIT); |
| 1714 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 1715 | hlua = hlua_gethlua(L); |
| 1716 | appctx = objt_appctx(socket->s->si[0].end); |
| 1717 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 1718 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1719 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 1724 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 1726 | { |
| 1727 | struct hlua_socket *socket; |
| 1728 | |
| 1729 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 1730 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1731 | socket->s->target = &socket_ssl.obj_type; |
| 1732 | return MAY_LJMP(hlua_socket_connect(L)); |
| 1733 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 1734 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1735 | |
| 1736 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 1737 | { |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 1742 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1743 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1744 | int tmout; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1745 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1746 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 1747 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1748 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1749 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1750 | |
| 1751 | socket->s->req->rto = tmout; |
| 1752 | socket->s->req->wto = tmout; |
| 1753 | socket->s->rep->rto = tmout; |
| 1754 | socket->s->rep->wto = tmout; |
| 1755 | |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | __LJMP static int hlua_socket_new(lua_State *L) |
| 1760 | { |
| 1761 | struct hlua_socket *socket; |
| 1762 | struct appctx *appctx; |
| 1763 | |
| 1764 | /* Check stack size. */ |
| 1765 | if (!lua_checkstack(L, 2)) { |
| 1766 | hlua_pusherror(L, "socket: full stack"); |
| 1767 | goto out_fail_conf; |
| 1768 | } |
| 1769 | |
| 1770 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
| 1771 | memset(socket, 0, sizeof(*socket)); |
| 1772 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 1773 | /* Check if the various memory pools are intialized. */ |
| 1774 | if (!pool2_session || !pool2_channel || !pool2_buffer) { |
| 1775 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 1776 | goto out_fail_conf; |
| 1777 | } |
| 1778 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | /* Pop a class session metatable and affect it to the userdata. */ |
| 1780 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 1781 | lua_setmetatable(L, -2); |
| 1782 | |
| 1783 | /* |
| 1784 | * |
| 1785 | * Get memory for the request. |
| 1786 | * |
| 1787 | */ |
| 1788 | |
| 1789 | socket->s = pool_alloc2(pool2_session); |
| 1790 | if (!socket->s) { |
| 1791 | hlua_pusherror(L, "socket: out of memory"); |
| 1792 | goto out_fail_conf; |
| 1793 | } |
| 1794 | |
| 1795 | socket->s->task = task_new(); |
| 1796 | if (!socket->s->task) { |
| 1797 | hlua_pusherror(L, "socket: out of memory"); |
| 1798 | goto out_free_session; |
| 1799 | } |
| 1800 | |
| 1801 | socket->s->req = pool_alloc2(pool2_channel); |
| 1802 | if (!socket->s->req) { |
| 1803 | hlua_pusherror(L, "socket: out of memory"); |
| 1804 | goto out_fail_req; |
| 1805 | } |
| 1806 | |
| 1807 | socket->s->req->buf = pool_alloc2(pool2_buffer); |
| 1808 | if (!socket->s->req->buf) { |
| 1809 | hlua_pusherror(L, "socket: out of memory"); |
| 1810 | goto out_fail_req_buf; |
| 1811 | } |
| 1812 | |
| 1813 | socket->s->rep = pool_alloc2(pool2_channel); |
| 1814 | if (!socket->s->rep) { |
| 1815 | hlua_pusherror(L, "socket: out of memory"); |
| 1816 | goto out_fail_rep; |
| 1817 | } |
| 1818 | |
| 1819 | socket->s->rep->buf = pool_alloc2(pool2_buffer); |
| 1820 | if (!socket->s->rep->buf) { |
| 1821 | hlua_pusherror(L, "socket: out of memory"); |
| 1822 | goto out_fail_rep_buf; |
| 1823 | } |
| 1824 | |
| 1825 | /* Configura empty Lua for the session. */ |
| 1826 | socket->s->hlua.T = NULL; |
| 1827 | socket->s->hlua.Tref = LUA_REFNIL; |
| 1828 | socket->s->hlua.Mref = LUA_REFNIL; |
| 1829 | socket->s->hlua.nargs = 0; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1830 | socket->s->hlua.flags = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | LIST_INIT(&socket->s->hlua.com); |
| 1832 | |
| 1833 | /* session initialisation. */ |
| 1834 | session_init_srv_conn(socket->s); |
| 1835 | |
| 1836 | /* |
| 1837 | * |
| 1838 | * Configure the associated task. |
| 1839 | * |
| 1840 | */ |
| 1841 | |
| 1842 | /* This is the dedicated function to process the session. This function |
| 1843 | * is able to establish the conection, process the timeouts, etc ... |
| 1844 | */ |
| 1845 | socket->s->task->process = process_session; |
| 1846 | |
| 1847 | /* Back reference to session. This is used by process_session(). */ |
| 1848 | socket->s->task->context = socket->s; |
| 1849 | |
| 1850 | /* The priority of the task is normal. */ |
| 1851 | socket->s->task->nice = 0; |
| 1852 | |
| 1853 | /* Init the next run to eternity. Later in this function, this task is |
| 1854 | * waked. |
| 1855 | */ |
| 1856 | socket->s->task->expire = TICK_ETERNITY; |
| 1857 | |
| 1858 | /* |
| 1859 | * |
| 1860 | * Initialize the attached buffers |
| 1861 | * |
| 1862 | */ |
| 1863 | socket->s->req->buf->size = global.tune.bufsize; |
| 1864 | socket->s->rep->buf->size = global.tune.bufsize; |
| 1865 | |
| 1866 | /* |
| 1867 | * |
| 1868 | * Initialize channels. |
| 1869 | * |
| 1870 | */ |
| 1871 | |
| 1872 | /* This function reset the struct. It must be called |
| 1873 | * before the configuration. |
| 1874 | */ |
| 1875 | channel_init(socket->s->req); |
| 1876 | channel_init(socket->s->rep); |
| 1877 | |
| 1878 | socket->s->req->prod = &socket->s->si[0]; |
| 1879 | socket->s->req->cons = &socket->s->si[1]; |
| 1880 | |
| 1881 | socket->s->rep->prod = &socket->s->si[1]; |
| 1882 | socket->s->rep->cons = &socket->s->si[0]; |
| 1883 | |
| 1884 | socket->s->si[0].ib = socket->s->req; |
| 1885 | socket->s->si[0].ob = socket->s->rep; |
| 1886 | |
| 1887 | socket->s->si[1].ib = socket->s->rep; |
| 1888 | socket->s->si[1].ob = socket->s->req; |
| 1889 | |
| 1890 | socket->s->req->analysers = 0; |
| 1891 | socket->s->req->rto = socket_proxy.timeout.client; |
| 1892 | socket->s->req->wto = socket_proxy.timeout.server; |
| 1893 | socket->s->req->rex = TICK_ETERNITY; |
| 1894 | socket->s->req->wex = TICK_ETERNITY; |
| 1895 | socket->s->req->analyse_exp = TICK_ETERNITY; |
| 1896 | |
| 1897 | socket->s->rep->analysers = 0; |
| 1898 | socket->s->rep->rto = socket_proxy.timeout.server; |
| 1899 | socket->s->rep->wto = socket_proxy.timeout.client; |
| 1900 | socket->s->rep->rex = TICK_ETERNITY; |
| 1901 | socket->s->rep->wex = TICK_ETERNITY; |
| 1902 | socket->s->rep->analyse_exp = TICK_ETERNITY; |
| 1903 | |
| 1904 | /* |
| 1905 | * |
| 1906 | * Configure the session. |
| 1907 | * |
| 1908 | */ |
| 1909 | |
| 1910 | /* The session dont have listener. The listener is used with real |
| 1911 | * proxies. |
| 1912 | */ |
| 1913 | socket->s->listener = NULL; |
| 1914 | |
| 1915 | /* The flags are initialized to 0. Values are setted later. */ |
| 1916 | socket->s->flags = 0; |
| 1917 | |
| 1918 | /* Assign the configured proxy to the new session. */ |
| 1919 | socket->s->be = &socket_proxy; |
| 1920 | socket->s->fe = &socket_proxy; |
| 1921 | |
| 1922 | /* XXX: Set namy variables */ |
| 1923 | socket->s->store_count = 0; |
| 1924 | memset(socket->s->stkctr, 0, sizeof(socket->s->stkctr)); |
| 1925 | |
| 1926 | /* Configure logs. */ |
| 1927 | socket->s->logs.logwait = 0; |
| 1928 | socket->s->logs.level = 0; |
| 1929 | socket->s->logs.accept_date = date; /* user-visible date for logging */ |
| 1930 | socket->s->logs.tv_accept = now; /* corrected date for internal use */ |
| 1931 | socket->s->do_log = NULL; |
| 1932 | |
| 1933 | /* Function used if an error is occured. */ |
| 1934 | socket->s->srv_error = default_srv_error; |
| 1935 | |
| 1936 | /* Init the list of buffers. */ |
| 1937 | LIST_INIT(&socket->s->buffer_wait); |
| 1938 | |
| 1939 | /* Dont configure the unique ID. */ |
| 1940 | socket->s->uniq_id = 0; |
| 1941 | socket->s->unique_id = NULL; |
| 1942 | |
| 1943 | /* XXX: ? */ |
| 1944 | socket->s->pend_pos = NULL; |
| 1945 | |
| 1946 | /* XXX: See later. */ |
| 1947 | socket->s->txn.sessid = NULL; |
| 1948 | socket->s->txn.srv_cookie = NULL; |
| 1949 | socket->s->txn.cli_cookie = NULL; |
| 1950 | socket->s->txn.uri = NULL; |
| 1951 | socket->s->txn.req.cap = NULL; |
| 1952 | socket->s->txn.rsp.cap = NULL; |
| 1953 | socket->s->txn.hdr_idx.v = NULL; |
| 1954 | socket->s->txn.hdr_idx.size = 0; |
| 1955 | socket->s->txn.hdr_idx.used = 0; |
| 1956 | |
| 1957 | /* Configure "left" stream interface as applet. This "si" produce |
| 1958 | * and use the data received from the server. The applet is initialized |
| 1959 | * and is attached to the stream interface. |
| 1960 | */ |
| 1961 | |
| 1962 | /* The data producer is already connected. It is the applet. */ |
| 1963 | socket->s->req->flags = CF_READ_ATTACHED; |
| 1964 | |
| 1965 | channel_auto_connect(socket->s->req); /* don't wait to establish connection */ |
| 1966 | channel_auto_close(socket->s->req); /* let the producer forward close requests */ |
| 1967 | |
| 1968 | si_reset(&socket->s->si[0], socket->s->task); |
| 1969 | si_set_state(&socket->s->si[0], SI_ST_EST); /* connection established (resource exists) */ |
| 1970 | |
| 1971 | appctx = stream_int_register_handler(&socket->s->si[0], &update_applet); |
| 1972 | if (!appctx) |
| 1973 | goto out_fail_conn1; |
| 1974 | appctx->ctx.hlua.socket = socket; |
| 1975 | appctx->ctx.hlua.connected = 0; |
| 1976 | LIST_INIT(&appctx->ctx.hlua.wake_on_write); |
| 1977 | LIST_INIT(&appctx->ctx.hlua.wake_on_read); |
| 1978 | |
| 1979 | /* Configure "right" stream interface. this "si" is used to connect |
| 1980 | * and retrieve data from the server. The connection is initialized |
| 1981 | * with the "struct server". |
| 1982 | */ |
| 1983 | si_reset(&socket->s->si[1], socket->s->task); |
| 1984 | si_set_state(&socket->s->si[1], SI_ST_INI); |
| 1985 | socket->s->si[1].conn_retries = socket_proxy.conn_retries; |
| 1986 | |
| 1987 | /* Force destination server. */ |
| 1988 | socket->s->flags |= SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET | SN_BE_ASSIGNED; |
| 1989 | socket->s->target = &socket_tcp.obj_type; |
| 1990 | |
| 1991 | /* This session is added to te lists of alive sessions. */ |
| 1992 | LIST_ADDQ(&sessions, &socket->s->list); |
| 1993 | |
| 1994 | /* XXX: I think that this list is used by stats. */ |
| 1995 | LIST_INIT(&socket->s->back_refs); |
| 1996 | |
| 1997 | /* Update statistics counters. */ |
| 1998 | socket_proxy.feconn++; /* beconn will be increased later */ |
| 1999 | jobs++; |
| 2000 | totalconn++; |
| 2001 | |
| 2002 | /* Return yield waiting for connection. */ |
| 2003 | return 1; |
| 2004 | |
| 2005 | out_fail_conn1: |
| 2006 | pool_free2(pool2_buffer, socket->s->rep->buf); |
| 2007 | out_fail_rep_buf: |
| 2008 | pool_free2(pool2_channel, socket->s->rep); |
| 2009 | out_fail_rep: |
| 2010 | pool_free2(pool2_buffer, socket->s->req->buf); |
| 2011 | out_fail_req_buf: |
| 2012 | pool_free2(pool2_channel, socket->s->req); |
| 2013 | out_fail_req: |
| 2014 | task_free(socket->s->task); |
| 2015 | out_free_session: |
| 2016 | pool_free2(pool2_session, socket->s); |
| 2017 | out_fail_conf: |
| 2018 | WILL_LJMP(lua_error(L)); |
| 2019 | return 0; |
| 2020 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2021 | |
| 2022 | /* |
| 2023 | * |
| 2024 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2025 | * Class Channel |
| 2026 | * |
| 2027 | * |
| 2028 | */ |
| 2029 | |
| 2030 | /* Returns the struct hlua_channel join to the class channel in the |
| 2031 | * stack entry "ud" or throws an argument error. |
| 2032 | */ |
| 2033 | __LJMP static struct hlua_channel *hlua_checkchannel(lua_State *L, int ud) |
| 2034 | { |
| 2035 | return (struct hlua_channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
| 2036 | } |
| 2037 | |
| 2038 | /* Creates new channel object and put it on the top of the stack. |
| 2039 | * If the stask does not have a free slots, the function fails |
| 2040 | * and returns 0; |
| 2041 | */ |
Thierry FOURNIER | bd1f132 | 2015-03-05 17:04:41 +0100 | [diff] [blame] | 2042 | static int hlua_channel_new(lua_State *L, struct session *s, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2043 | { |
| 2044 | struct hlua_channel *chn; |
| 2045 | |
| 2046 | /* Check stack size. */ |
| 2047 | if (!lua_checkstack(L, 2)) |
| 2048 | return 0; |
| 2049 | |
| 2050 | /* NOTE: The allocation never fails. The failure |
| 2051 | * throw an error, and the function never returns. |
| 2052 | */ |
| 2053 | chn = lua_newuserdata(L, sizeof(*chn)); |
| 2054 | chn->chn = channel; |
Thierry FOURNIER | bd1f132 | 2015-03-05 17:04:41 +0100 | [diff] [blame] | 2055 | chn->s = s; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2056 | |
| 2057 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2058 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2059 | lua_setmetatable(L, -2); |
| 2060 | |
| 2061 | return 1; |
| 2062 | } |
| 2063 | |
| 2064 | /* Duplicate all the data present in the input channel and put it |
| 2065 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2066 | * the stack if the channel is closed and all the data are consumed, |
| 2067 | * returns 0 if no data are available, otherwise it returns the length |
| 2068 | * of the builded string. |
| 2069 | */ |
| 2070 | static inline int _hlua_channel_dup(struct hlua_channel *chn, lua_State *L) |
| 2071 | { |
| 2072 | char *blk1; |
| 2073 | char *blk2; |
| 2074 | int len1; |
| 2075 | int len2; |
| 2076 | int ret; |
| 2077 | luaL_Buffer b; |
| 2078 | |
| 2079 | ret = bi_getblk_nc(chn->chn, &blk1, &len1, &blk2, &len2); |
| 2080 | if (unlikely(ret == 0)) |
| 2081 | return 0; |
| 2082 | |
| 2083 | if (unlikely(ret < 0)) { |
| 2084 | lua_pushnil(L); |
| 2085 | return -1; |
| 2086 | } |
| 2087 | |
| 2088 | luaL_buffinit(L, &b); |
| 2089 | luaL_addlstring(&b, blk1, len1); |
| 2090 | if (unlikely(ret == 2)) |
| 2091 | luaL_addlstring(&b, blk2, len2); |
| 2092 | luaL_pushresult(&b); |
| 2093 | |
| 2094 | if (unlikely(ret == 2)) |
| 2095 | return len1 + len2; |
| 2096 | return len1; |
| 2097 | } |
| 2098 | |
| 2099 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2100 | * a yield. This function keep the data in the buffer. |
| 2101 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2102 | __LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2103 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2104 | struct hlua_channel *chn; |
| 2105 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2106 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2107 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2108 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2109 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2110 | return 1; |
| 2111 | } |
| 2112 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2113 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2114 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2115 | { |
| 2116 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2117 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2118 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2119 | } |
| 2120 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2121 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2122 | * a yield. This function consumes the data in the buffer. It returns |
| 2123 | * a string containing the data or a nil pointer if no data are available |
| 2124 | * and the channel is closed. |
| 2125 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2126 | __LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2127 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2128 | struct hlua_channel *chn; |
| 2129 | int ret; |
| 2130 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2131 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2132 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2133 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2134 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2135 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2136 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2137 | if (unlikely(ret == -1)) |
| 2138 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2139 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2140 | chn->chn->buf->i -= ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2141 | return 1; |
| 2142 | } |
| 2143 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2144 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2145 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2146 | { |
| 2147 | MAY_LJMP(check_args(L, 1, "get")); |
| 2148 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2149 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2150 | } |
| 2151 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2152 | /* This functions consumes and returns one line. If the channel is closed, |
| 2153 | * and the last data does not contains a final '\n', the data are returned |
| 2154 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2155 | * value. |
| 2156 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2157 | __LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2158 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2159 | char *blk1; |
| 2160 | char *blk2; |
| 2161 | int len1; |
| 2162 | int len2; |
| 2163 | int len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2164 | struct hlua_channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2165 | int ret; |
| 2166 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2167 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2168 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2169 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2170 | ret = bi_getline_nc(chn->chn, &blk1, &len1, &blk2, &len2); |
| 2171 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2172 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2173 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2174 | if (ret == -1) { |
| 2175 | lua_pushnil(L); |
| 2176 | return 1; |
| 2177 | } |
| 2178 | |
| 2179 | luaL_buffinit(L, &b); |
| 2180 | luaL_addlstring(&b, blk1, len1); |
| 2181 | len = len1; |
| 2182 | if (unlikely(ret == 2)) { |
| 2183 | luaL_addlstring(&b, blk2, len2); |
| 2184 | len += len2; |
| 2185 | } |
| 2186 | luaL_pushresult(&b); |
| 2187 | buffer_replace2(chn->chn->buf, chn->chn->buf->p, chn->chn->buf->p + len, NULL, 0); |
| 2188 | return 1; |
| 2189 | } |
| 2190 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2191 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2192 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2193 | { |
| 2194 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2195 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2196 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2197 | } |
| 2198 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2199 | /* This function takes a string as input, and append it at the |
| 2200 | * input side of channel. If the data is too big, but a space |
| 2201 | * is probably available after sending some data, the function |
| 2202 | * yield. If the data is bigger than the buffer, or if the |
| 2203 | * channel is closed, it returns -1. otherwise, it returns the |
| 2204 | * amount of data writed. |
| 2205 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2206 | __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2207 | { |
| 2208 | struct hlua_channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2209 | size_t len; |
| 2210 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2211 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2212 | int ret; |
| 2213 | int max; |
| 2214 | |
| 2215 | max = channel_recv_limit(chn->chn) - buffer_len(chn->chn->buf); |
| 2216 | if (max > len - l) |
| 2217 | max = len - l; |
| 2218 | |
| 2219 | ret = bi_putblk(chn->chn, str+l, max); |
| 2220 | if (ret == -2 || ret == -3) { |
| 2221 | lua_pushinteger(L, -1); |
| 2222 | return 1; |
| 2223 | } |
| 2224 | if (ret == -1) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2225 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2226 | l += ret; |
| 2227 | lua_pop(L, 1); |
| 2228 | lua_pushinteger(L, l); |
| 2229 | |
| 2230 | max = channel_recv_limit(chn->chn) - buffer_len(chn->chn->buf); |
| 2231 | if (max == 0 && chn->chn->buf->o == 0) { |
| 2232 | /* There are no space avalaible, and the output buffer is empty. |
| 2233 | * in this case, we cannot add more data, so we cannot yield, |
| 2234 | * we return the amount of copyied data. |
| 2235 | */ |
| 2236 | return 1; |
| 2237 | } |
| 2238 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2239 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2240 | return 1; |
| 2241 | } |
| 2242 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2243 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2244 | * of the writed string, or -1 if the channel is closed or if the |
| 2245 | * buffer size is too little for the data. |
| 2246 | */ |
| 2247 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2248 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2249 | size_t len; |
| 2250 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2251 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2252 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2253 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2254 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2255 | lua_pushinteger(L, 0); |
| 2256 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2257 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2258 | } |
| 2259 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2260 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2261 | * his process by cleaning the buffer. The result is a replacement |
| 2262 | * of the current data. It returns the length of the writed string, |
| 2263 | * or -1 if the channel is closed or if the buffer size is too |
| 2264 | * little for the data. |
| 2265 | */ |
| 2266 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2267 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2268 | struct hlua_channel *chn; |
| 2269 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2270 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2271 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2272 | lua_pushinteger(L, 0); |
| 2273 | |
| 2274 | chn->chn->buf->i = 0; |
| 2275 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2276 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | /* Append data in the output side of the buffer. This data is immediatly |
| 2280 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2281 | * cannot contains the data, the function yield. The function returns -1 |
| 2282 | * if the channel is closed. |
| 2283 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2284 | __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2285 | { |
| 2286 | struct hlua_channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2287 | size_t len; |
| 2288 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2289 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2290 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2291 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2292 | |
| 2293 | if (unlikely(channel_output_closed(chn->chn))) { |
| 2294 | lua_pushinteger(L, -1); |
| 2295 | return 1; |
| 2296 | } |
| 2297 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2298 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2299 | * the request buffer if its not required. |
| 2300 | */ |
| 2301 | if (chn->chn->buf->size == 0) { |
| 2302 | if (!session_alloc_recv_buffer(chn->s, &chn->chn->buf)) { |
| 2303 | chn->chn->prod->flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2304 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2305 | } |
| 2306 | } |
| 2307 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2308 | /* the writed data will be immediatly sent, so we can check |
| 2309 | * the avalaible space without taking in account the reserve. |
| 2310 | * The reserve is guaranted for the processing of incoming |
| 2311 | * data, because the buffer will be flushed. |
| 2312 | */ |
| 2313 | max = chn->chn->buf->size - buffer_len(chn->chn->buf); |
| 2314 | |
| 2315 | /* If there are no space avalaible, and the output buffer is empty. |
| 2316 | * in this case, we cannot add more data, so we cannot yield, |
| 2317 | * we return the amount of copyied data. |
| 2318 | */ |
| 2319 | if (max == 0 && chn->chn->buf->o == 0) |
| 2320 | return 1; |
| 2321 | |
| 2322 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2323 | if (max > len - l) |
| 2324 | max = len - l; |
| 2325 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2326 | /* The buffer avalaible size may be not contiguous. This test |
| 2327 | * detects a non contiguous buffer and realign it. |
| 2328 | */ |
Thierry FOURNIER | d2b597a | 2015-03-07 14:38:50 +0100 | [diff] [blame] | 2329 | if (bi_space_for_replace(chn->chn->buf) < max) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2330 | buffer_slow_realign(chn->chn->buf); |
| 2331 | |
| 2332 | /* Copy input data in the buffer. */ |
Thierry FOURNIER | 506e46c | 2015-03-04 11:44:47 +0100 | [diff] [blame] | 2333 | max = buffer_replace2(chn->chn->buf, chn->chn->buf->p, chn->chn->buf->p, str+l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2334 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2335 | /* buffer replace considers that the input part is filled. |
| 2336 | * so, I must forward these new data in the output part. |
| 2337 | */ |
| 2338 | b_adv(chn->chn->buf, max); |
| 2339 | |
| 2340 | l += max; |
| 2341 | lua_pop(L, 1); |
| 2342 | lua_pushinteger(L, l); |
| 2343 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2344 | /* If there are no space avalaible, and the output buffer is empty. |
| 2345 | * in this case, we cannot add more data, so we cannot yield, |
| 2346 | * we return the amount of copyied data. |
| 2347 | */ |
| 2348 | max = chn->chn->buf->size - buffer_len(chn->chn->buf); |
| 2349 | if (max == 0 && chn->chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2350 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2351 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2352 | if (l < len) { |
| 2353 | /* If we are waiting for space in the response buffer, we |
| 2354 | * must set the flag WAKERESWR. This flag required the task |
| 2355 | * wake up if any activity is detected on the response buffer. |
| 2356 | */ |
| 2357 | if (chn->chn == chn->s->rep) |
| 2358 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2359 | else |
| 2360 | HLUA_SET_WAKEREQWR(hlua); |
| 2361 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2362 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2363 | |
| 2364 | return 1; |
| 2365 | } |
| 2366 | |
| 2367 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2368 | * yield the LUA process, and resume it without checking the |
| 2369 | * input arguments. |
| 2370 | */ |
| 2371 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2372 | { |
| 2373 | MAY_LJMP(check_args(L, 2, "send")); |
| 2374 | lua_pushinteger(L, 0); |
| 2375 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2376 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | /* This function forward and amount of butes. The data pass from |
| 2380 | * the input side of the buffer to the output side, and can be |
| 2381 | * forwarded. This function never fails. |
| 2382 | * |
| 2383 | * The Lua function takes an amount of bytes to be forwarded in |
| 2384 | * imput. It returns the number of bytes forwarded. |
| 2385 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2386 | __LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2387 | { |
| 2388 | struct hlua_channel *chn; |
| 2389 | int len; |
| 2390 | int l; |
| 2391 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2392 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2393 | |
| 2394 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2395 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2396 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2397 | |
| 2398 | max = len - l; |
| 2399 | if (max > chn->chn->buf->i) |
| 2400 | max = chn->chn->buf->i; |
| 2401 | channel_forward(chn->chn, max); |
| 2402 | l += max; |
| 2403 | |
| 2404 | lua_pop(L, 1); |
| 2405 | lua_pushinteger(L, l); |
| 2406 | |
| 2407 | /* Check if it miss bytes to forward. */ |
| 2408 | if (l < len) { |
| 2409 | /* The the input channel or the output channel are closed, we |
| 2410 | * must return the amount of data forwarded. |
| 2411 | */ |
| 2412 | if (channel_input_closed(chn->chn) || channel_output_closed(chn->chn)) |
| 2413 | return 1; |
| 2414 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2415 | /* If we are waiting for space data in the response buffer, we |
| 2416 | * must set the flag WAKERESWR. This flag required the task |
| 2417 | * wake up if any activity is detected on the response buffer. |
| 2418 | */ |
| 2419 | if (chn->chn == chn->s->rep) |
| 2420 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2421 | else |
| 2422 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2423 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2424 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2425 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | return 1; |
| 2429 | } |
| 2430 | |
| 2431 | /* Just check the input and prepare the stack for the previous |
| 2432 | * function "hlua_channel_forward_yield" |
| 2433 | */ |
| 2434 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2435 | { |
| 2436 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2437 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2438 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2439 | |
| 2440 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2441 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /* Just returns the number of bytes available in the input |
| 2445 | * side of the buffer. This function never fails. |
| 2446 | */ |
| 2447 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2448 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2449 | struct hlua_channel *chn; |
| 2450 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2451 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2452 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2453 | lua_pushinteger(L, chn->chn->buf->i); |
| 2454 | return 1; |
| 2455 | } |
| 2456 | |
| 2457 | /* Just returns the number of bytes available in the output |
| 2458 | * side of the buffer. This function never fails. |
| 2459 | */ |
| 2460 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2461 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2462 | struct hlua_channel *chn; |
| 2463 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2464 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2465 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2466 | lua_pushinteger(L, chn->chn->buf->o); |
| 2467 | return 1; |
| 2468 | } |
| 2469 | |
| 2470 | |
| 2471 | /* |
| 2472 | * |
| 2473 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2474 | * Class TXN |
| 2475 | * |
| 2476 | * |
| 2477 | */ |
| 2478 | |
| 2479 | /* Returns a struct hlua_session if the stack entry "ud" is |
| 2480 | * a class session, otherwise it throws an error. |
| 2481 | */ |
| 2482 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 2483 | { |
| 2484 | return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
| 2485 | } |
| 2486 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 2487 | __LJMP static int hlua_setpriv(lua_State *L) |
| 2488 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2489 | struct hlua *hlua; |
| 2490 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 2491 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 2492 | |
| 2493 | /* It is useles to retrieve the session, but this function |
| 2494 | * runs only in a session context. |
| 2495 | */ |
| 2496 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2497 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 2498 | |
| 2499 | /* Remove previous value. */ |
| 2500 | if (hlua->Mref != -1) |
| 2501 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 2502 | |
| 2503 | /* Get and store new value. */ |
| 2504 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 2505 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 2506 | |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
| 2510 | __LJMP static int hlua_getpriv(lua_State *L) |
| 2511 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2512 | struct hlua *hlua; |
| 2513 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 2514 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 2515 | |
| 2516 | /* It is useles to retrieve the session, but this function |
| 2517 | * runs only in a session context. |
| 2518 | */ |
| 2519 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2520 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 2521 | |
| 2522 | /* Push configuration index in the stack. */ |
| 2523 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 2524 | |
| 2525 | return 1; |
| 2526 | } |
| 2527 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2528 | /* Create stack entry containing a class TXN. This function |
| 2529 | * return 0 if the stack does not contains free slots, |
| 2530 | * otherwise it returns 1. |
| 2531 | */ |
| 2532 | static int hlua_txn_new(lua_State *L, struct session *s, struct proxy *p, void *l7) |
| 2533 | { |
| 2534 | struct hlua_txn *hs; |
| 2535 | |
| 2536 | /* Check stack size. */ |
| 2537 | if (!lua_checkstack(L, 2)) |
| 2538 | return 0; |
| 2539 | |
| 2540 | /* NOTE: The allocation never fails. The failure |
| 2541 | * throw an error, and the function never returns. |
| 2542 | * if the throw is not avalaible, the process is aborted. |
| 2543 | */ |
| 2544 | hs = lua_newuserdata(L, sizeof(struct hlua_txn)); |
| 2545 | hs->s = s; |
| 2546 | hs->p = p; |
| 2547 | hs->l7 = l7; |
| 2548 | |
| 2549 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2550 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 2551 | lua_setmetatable(L, -2); |
| 2552 | |
| 2553 | return 1; |
| 2554 | } |
| 2555 | |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 2556 | /* This function returns a channel object associated |
| 2557 | * with the request channel. This function never fails, |
| 2558 | * however if the stack is full, it throws an error. |
| 2559 | */ |
| 2560 | __LJMP static int hlua_txn_req_channel(lua_State *L) |
| 2561 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2562 | struct hlua_txn *s; |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 2563 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2564 | MAY_LJMP(check_args(L, 1, "req_channel")); |
| 2565 | s = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 2566 | |
Thierry FOURNIER | bd1f132 | 2015-03-05 17:04:41 +0100 | [diff] [blame] | 2567 | if (!hlua_channel_new(L, s->s, s->s->req)) |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 2568 | WILL_LJMP(luaL_error(L, "full stack")); |
| 2569 | |
| 2570 | return 1; |
| 2571 | } |
| 2572 | |
| 2573 | /* This function returns a channel object associated |
| 2574 | * with the response channel. This function never fails, |
| 2575 | * however if the stack is full, it throws an error. |
| 2576 | */ |
| 2577 | __LJMP static int hlua_txn_res_channel(lua_State *L) |
| 2578 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2579 | struct hlua_txn *s; |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 2580 | |
Willy Tarreau | eee4539 | 2015-03-10 14:22:28 +0100 | [diff] [blame] | 2581 | MAY_LJMP(check_args(L, 1, "res_channel")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2582 | s = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 2583 | |
Thierry FOURNIER | bd1f132 | 2015-03-05 17:04:41 +0100 | [diff] [blame] | 2584 | if (!hlua_channel_new(L, s->s, s->s->rep)) |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 2585 | WILL_LJMP(luaL_error(L, "full stack")); |
| 2586 | |
| 2587 | return 1; |
| 2588 | } |
| 2589 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 2590 | /* This function is an Lua binding that send pending data |
| 2591 | * to the client, and close the stream interface. |
| 2592 | */ |
| 2593 | __LJMP static int hlua_txn_close(lua_State *L) |
| 2594 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2595 | struct hlua_txn *s; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 2596 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2597 | MAY_LJMP(check_args(L, 1, "close")); |
| 2598 | s = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 2599 | |
| 2600 | channel_abort(s->s->si[0].ib); |
| 2601 | channel_auto_close(s->s->si[0].ib); |
| 2602 | channel_erase(s->s->si[0].ib); |
| 2603 | channel_auto_read(s->s->si[0].ob); |
| 2604 | channel_auto_close(s->s->si[0].ob); |
| 2605 | channel_shutr_now(s->s->si[0].ob); |
| 2606 | |
| 2607 | return 0; |
| 2608 | } |
| 2609 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 2610 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 2611 | * It uses closure argument to store the associated sample-fetch. It |
Cyril Bonté | 928ae5c | 2015-03-02 00:08:41 +0100 | [diff] [blame] | 2612 | * returns only one argument or throws an error. An error is thrown |
| 2613 | * only if an error is encountered during the argument parsing. If |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 2614 | * the "sample-fetch" function fails, nil is returned. |
| 2615 | */ |
| 2616 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 2617 | { |
| 2618 | struct hlua_txn *s; |
| 2619 | struct hlua_sample_fetch *f; |
Cyril Bonté | 928ae5c | 2015-03-02 00:08:41 +0100 | [diff] [blame] | 2620 | struct arg args[ARGM_NBARGS + 1]; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 2621 | int i; |
| 2622 | struct sample smp; |
| 2623 | |
| 2624 | /* Get closure arguments. */ |
| 2625 | f = (struct hlua_sample_fetch *)lua_touserdata(L, lua_upvalueindex(1)); |
| 2626 | |
| 2627 | /* Get traditionnal arguments. */ |
| 2628 | s = MAY_LJMP(hlua_checktxn(L, 1)); |
| 2629 | |
| 2630 | /* Get extra arguments. */ |
Cyril Bonté | 928ae5c | 2015-03-02 00:08:41 +0100 | [diff] [blame] | 2631 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 2632 | if (i >= ARGM_NBARGS) |
| 2633 | break; |
| 2634 | hlua_lua2arg(L, i + 2, &args[i]); |
| 2635 | } |
| 2636 | args[i].type = ARGT_STOP; |
| 2637 | |
| 2638 | /* Check arguments. */ |
| 2639 | MAY_LJMP(hlua_lua2arg_check(L, 1, args, f->f->arg_mask)); |
| 2640 | |
Cyril Bonté | 928ae5c | 2015-03-02 00:08:41 +0100 | [diff] [blame] | 2641 | /* Run the special args checker. */ |
| 2642 | if (f->f->val_args && !f->f->val_args(args, NULL)) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 2643 | lua_pushfstring(L, "error in arguments"); |
| 2644 | WILL_LJMP(lua_error(L)); |
| 2645 | } |
| 2646 | |
| 2647 | /* Initialise the sample. */ |
| 2648 | memset(&smp, 0, sizeof(smp)); |
| 2649 | |
| 2650 | /* Run the sample fetch process. */ |
| 2651 | if (!f->f->process(s->p, s->s, s->l7, 0, args, &smp, f->f->kw, f->f->private)) { |
| 2652 | lua_pushnil(L); |
| 2653 | return 1; |
| 2654 | } |
| 2655 | |
| 2656 | /* Convert the returned sample in lua value. */ |
| 2657 | hlua_smp2lua(L, &smp); |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 2661 | /* This function is an LUA binding. It creates ans returns |
| 2662 | * an array of HTTP headers. This function does not fails. |
| 2663 | */ |
| 2664 | static int hlua_session_getheaders(lua_State *L) |
| 2665 | { |
| 2666 | struct hlua_txn *s = MAY_LJMP(hlua_checktxn(L, 1)); |
| 2667 | struct session *sess = s->s; |
| 2668 | const char *cur_ptr, *cur_next, *p; |
| 2669 | int old_idx, cur_idx; |
| 2670 | struct hdr_idx_elem *cur_hdr; |
| 2671 | const char *hn, *hv; |
| 2672 | int hnl, hvl; |
| 2673 | |
| 2674 | /* Create the table. */ |
| 2675 | lua_newtable(L); |
| 2676 | |
| 2677 | /* Build array of headers. */ |
| 2678 | old_idx = 0; |
| 2679 | cur_next = sess->req->buf->p + hdr_idx_first_pos(&sess->txn.hdr_idx); |
| 2680 | |
| 2681 | while (1) { |
| 2682 | cur_idx = sess->txn.hdr_idx.v[old_idx].next; |
| 2683 | if (!cur_idx) |
| 2684 | break; |
| 2685 | old_idx = cur_idx; |
| 2686 | |
| 2687 | cur_hdr = &sess->txn.hdr_idx.v[cur_idx]; |
| 2688 | cur_ptr = cur_next; |
| 2689 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 2690 | |
| 2691 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 2692 | * and the next header starts at cur_next. We'll check |
| 2693 | * this header in the list as well as against the default |
| 2694 | * rule. |
| 2695 | */ |
| 2696 | |
| 2697 | /* look for ': *'. */ |
| 2698 | hn = cur_ptr; |
| 2699 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 2700 | if (p >= cur_ptr+cur_hdr->len) |
| 2701 | continue; |
| 2702 | hnl = p - hn; |
| 2703 | p++; |
| 2704 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 2705 | p++; |
| 2706 | if (p >= cur_ptr+cur_hdr->len) |
| 2707 | continue; |
| 2708 | hv = p; |
| 2709 | hvl = cur_ptr+cur_hdr->len-p; |
| 2710 | |
| 2711 | /* Push values in the table. */ |
| 2712 | lua_pushlstring(L, hn, hnl); |
| 2713 | lua_pushlstring(L, hv, hvl); |
| 2714 | lua_settable(L, -3); |
| 2715 | } |
| 2716 | |
| 2717 | return 1; |
| 2718 | } |
| 2719 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2720 | __LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2721 | { |
| 2722 | int wakeup_ms = lua_tointeger(L, -1); |
| 2723 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2724 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | __LJMP static int hlua_sleep(lua_State *L) |
| 2729 | { |
| 2730 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2731 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2732 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2733 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2734 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2735 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2736 | wakeup_ms = tick_add(now_ms, delay); |
| 2737 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2738 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2739 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 2740 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2741 | } |
| 2742 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2743 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2744 | { |
| 2745 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2746 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2747 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2748 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2749 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2750 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2751 | wakeup_ms = tick_add(now_ms, delay); |
| 2752 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2753 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2754 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 2755 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 2756 | } |
| 2757 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 2758 | /* This functionis an LUA binding. it permits to give back |
| 2759 | * the hand at the HAProxy scheduler. It is used when the |
| 2760 | * LUA processing consumes a lot of time. |
| 2761 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2762 | __LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2763 | { |
| 2764 | return 0; |
| 2765 | } |
| 2766 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 2767 | __LJMP static int hlua_yield(lua_State *L) |
| 2768 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 2769 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 2770 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 2771 | } |
| 2772 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 2773 | /* This function change the nice of the currently executed |
| 2774 | * task. It is used set low or high priority at the current |
| 2775 | * task. |
| 2776 | */ |
| 2777 | __LJMP static int hlua_setnice(lua_State *L) |
| 2778 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2779 | struct hlua *hlua; |
| 2780 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 2781 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2782 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 2783 | hlua = hlua_gethlua(L); |
| 2784 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 2785 | |
| 2786 | /* If he task is not set, I'm in a start mode. */ |
| 2787 | if (!hlua || !hlua->task) |
| 2788 | return 0; |
| 2789 | |
| 2790 | if (nice < -1024) |
| 2791 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2792 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 2793 | nice = 1024; |
| 2794 | |
| 2795 | hlua->task->nice = nice; |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 2799 | /* This function is used as a calback of a task. It is called by the |
| 2800 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 2801 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 2802 | * signal to wake the task. |
| 2803 | */ |
| 2804 | static struct task *hlua_process_task(struct task *task) |
| 2805 | { |
| 2806 | struct hlua *hlua = task->context; |
| 2807 | enum hlua_exec status; |
| 2808 | |
| 2809 | /* We need to remove the task from the wait queue before executing |
| 2810 | * the Lua code because we don't know if it needs to wait for |
| 2811 | * another timer or not in the case of E_AGAIN. |
| 2812 | */ |
| 2813 | task_delete(task); |
| 2814 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 2815 | /* If it is the first call to the task, we must initialize the |
| 2816 | * execution timeouts. |
| 2817 | */ |
| 2818 | if (!HLUA_IS_RUNNING(hlua)) |
| 2819 | hlua->expire = tick_add(now_ms, hlua_timeout_task); |
| 2820 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 2821 | /* Execute the Lua code. */ |
| 2822 | status = hlua_ctx_resume(hlua, 1); |
| 2823 | |
| 2824 | switch (status) { |
| 2825 | /* finished or yield */ |
| 2826 | case HLUA_E_OK: |
| 2827 | hlua_ctx_destroy(hlua); |
| 2828 | task_delete(task); |
| 2829 | task_free(task); |
| 2830 | break; |
| 2831 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 2832 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 2833 | if (hlua->wake_time != TICK_ETERNITY) |
| 2834 | task_schedule(task, hlua->wake_time); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 2835 | break; |
| 2836 | |
| 2837 | /* finished with error. */ |
| 2838 | case HLUA_E_ERRMSG: |
| 2839 | send_log(NULL, LOG_ERR, "Lua task: %s.", lua_tostring(hlua->T, -1)); |
| 2840 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 2841 | Alert("Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
| 2842 | hlua_ctx_destroy(hlua); |
| 2843 | task_delete(task); |
| 2844 | task_free(task); |
| 2845 | break; |
| 2846 | |
| 2847 | case HLUA_E_ERR: |
| 2848 | default: |
| 2849 | send_log(NULL, LOG_ERR, "Lua task: unknown error."); |
| 2850 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 2851 | Alert("Lua task: unknown error.\n"); |
| 2852 | hlua_ctx_destroy(hlua); |
| 2853 | task_delete(task); |
| 2854 | task_free(task); |
| 2855 | break; |
| 2856 | } |
| 2857 | return NULL; |
| 2858 | } |
| 2859 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 2860 | /* This function is an LUA binding that register LUA function to be |
| 2861 | * executed after the HAProxy configuration parsing and before the |
| 2862 | * HAProxy scheduler starts. This function expect only one LUA |
| 2863 | * argument that is a function. This function returns nothing, but |
| 2864 | * throws if an error is encountered. |
| 2865 | */ |
| 2866 | __LJMP static int hlua_register_init(lua_State *L) |
| 2867 | { |
| 2868 | struct hlua_init_function *init; |
| 2869 | int ref; |
| 2870 | |
| 2871 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 2872 | |
| 2873 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 2874 | |
| 2875 | init = malloc(sizeof(*init)); |
| 2876 | if (!init) |
| 2877 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 2878 | |
| 2879 | init->function_ref = ref; |
| 2880 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 2884 | /* This functio is an LUA binding. It permits to register a task |
| 2885 | * executed in parallel of the main HAroxy activity. The task is |
| 2886 | * created and it is set in the HAProxy scheduler. It can be called |
| 2887 | * from the "init" section, "post init" or during the runtime. |
| 2888 | * |
| 2889 | * Lua prototype: |
| 2890 | * |
| 2891 | * <none> core.register_task(<function>) |
| 2892 | */ |
| 2893 | static int hlua_register_task(lua_State *L) |
| 2894 | { |
| 2895 | struct hlua *hlua; |
| 2896 | struct task *task; |
| 2897 | int ref; |
| 2898 | |
| 2899 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 2900 | |
| 2901 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 2902 | |
| 2903 | hlua = malloc(sizeof(*hlua)); |
| 2904 | if (!hlua) |
| 2905 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 2906 | |
| 2907 | task = task_new(); |
| 2908 | task->context = hlua; |
| 2909 | task->process = hlua_process_task; |
| 2910 | |
| 2911 | if (!hlua_ctx_init(hlua, task)) |
| 2912 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 2913 | |
| 2914 | /* Restore the function in the stack. */ |
| 2915 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 2916 | hlua->nargs = 0; |
| 2917 | |
| 2918 | /* Schedule task. */ |
| 2919 | task_schedule(task, now_ms); |
| 2920 | |
| 2921 | return 0; |
| 2922 | } |
| 2923 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 2924 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 2925 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 2926 | * resume converters. |
| 2927 | */ |
| 2928 | static int hlua_sample_conv_wrapper(struct session *session, const struct arg *arg_p, |
| 2929 | struct sample *smp, void *private) |
| 2930 | { |
| 2931 | struct hlua_function *fcn = (struct hlua_function *)private; |
| 2932 | |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 2933 | /* In the execution wrappers linked with a session, the |
| 2934 | * Lua context can be not initialized. This behavior |
| 2935 | * permits to save performances because a systematic |
| 2936 | * Lua initialization cause 5% performances loss. |
| 2937 | */ |
| 2938 | if (!session->hlua.T && !hlua_ctx_init(&session->hlua, session->task)) { |
| 2939 | send_log(session->be, LOG_ERR, "Lua converter '%s': can't initialize Lua context.", fcn->name); |
| 2940 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 2941 | Alert("Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 2945 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 2946 | if (!HLUA_IS_RUNNING(&session->hlua)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 2947 | /* Check stack available size. */ |
| 2948 | if (!lua_checkstack(session->hlua.T, 1)) { |
| 2949 | send_log(session->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name); |
| 2950 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 2951 | Alert("Lua converter '%s': full stack.\n", fcn->name); |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | /* Restore the function in the stack. */ |
| 2956 | lua_rawgeti(session->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 2957 | |
| 2958 | /* convert input sample and pust-it in the stack. */ |
| 2959 | if (!lua_checkstack(session->hlua.T, 1)) { |
| 2960 | send_log(session->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name); |
| 2961 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 2962 | Alert("Lua converter '%s': full stack.\n", fcn->name); |
| 2963 | return 0; |
| 2964 | } |
| 2965 | hlua_smp2lua(session->hlua.T, smp); |
| 2966 | session->hlua.nargs = 2; |
| 2967 | |
| 2968 | /* push keywords in the stack. */ |
| 2969 | if (arg_p) { |
| 2970 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
| 2971 | if (!lua_checkstack(session->hlua.T, 1)) { |
| 2972 | send_log(session->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name); |
| 2973 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 2974 | Alert("Lua converter '%s': full stack.\n", fcn->name); |
| 2975 | return 0; |
| 2976 | } |
| 2977 | hlua_arg2lua(session->hlua.T, arg_p); |
| 2978 | session->hlua.nargs++; |
| 2979 | } |
| 2980 | } |
| 2981 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 2982 | /* We must initialize the execution timeouts. */ |
| 2983 | session->hlua.expire = tick_add(now_ms, hlua_timeout_session); |
| 2984 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 2985 | /* Set the currently running flag. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 2986 | HLUA_SET_RUN(&session->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | /* Execute the function. */ |
| 2990 | switch (hlua_ctx_resume(&session->hlua, 0)) { |
| 2991 | /* finished. */ |
| 2992 | case HLUA_E_OK: |
| 2993 | /* Convert the returned value in sample. */ |
| 2994 | hlua_lua2smp(session->hlua.T, -1, smp); |
| 2995 | lua_pop(session->hlua.T, 1); |
| 2996 | return 1; |
| 2997 | |
| 2998 | /* yield. */ |
| 2999 | case HLUA_E_AGAIN: |
| 3000 | send_log(session->be, LOG_ERR, "Lua converter '%s': cannot use yielded functions.", fcn->name); |
| 3001 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3002 | Alert("Lua converter '%s': cannot use yielded functions.\n", fcn->name); |
| 3003 | return 0; |
| 3004 | |
| 3005 | /* finished with error. */ |
| 3006 | case HLUA_E_ERRMSG: |
| 3007 | /* Display log. */ |
| 3008 | send_log(session->be, LOG_ERR, "Lua converter '%s': %s.", fcn->name, lua_tostring(session->hlua.T, -1)); |
| 3009 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3010 | Alert("Lua converter '%s': %s.\n", fcn->name, lua_tostring(session->hlua.T, -1)); |
| 3011 | lua_pop(session->hlua.T, 1); |
| 3012 | return 0; |
| 3013 | |
| 3014 | case HLUA_E_ERR: |
| 3015 | /* Display log. */ |
| 3016 | send_log(session->be, LOG_ERR, "Lua converter '%s' returns an unknown error.", fcn->name); |
| 3017 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3018 | Alert("Lua converter '%s' returns an unknown error.\n", fcn->name); |
| 3019 | |
| 3020 | default: |
| 3021 | return 0; |
| 3022 | } |
| 3023 | } |
| 3024 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3025 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 3026 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 3027 | * resume sample-fetches. |
| 3028 | */ |
| 3029 | static int hlua_sample_fetch_wrapper(struct proxy *px, struct session *s, void *l7, |
| 3030 | unsigned int opt, const struct arg *arg_p, |
| 3031 | struct sample *smp, const char *kw, void *private) |
| 3032 | { |
| 3033 | struct hlua_function *fcn = (struct hlua_function *)private; |
| 3034 | |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 3035 | /* In the execution wrappers linked with a session, the |
| 3036 | * Lua context can be not initialized. This behavior |
| 3037 | * permits to save performances because a systematic |
| 3038 | * Lua initialization cause 5% performances loss. |
| 3039 | */ |
| 3040 | if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) { |
| 3041 | send_log(s->be, LOG_ERR, "Lua sample-fetch '%s': can't initialize Lua context.", fcn->name); |
| 3042 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3043 | Alert("Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 3044 | return 0; |
| 3045 | } |
| 3046 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3047 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 3048 | if (!HLUA_IS_RUNNING(&s->hlua)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3049 | /* Check stack available size. */ |
| 3050 | if (!lua_checkstack(s->hlua.T, 2)) { |
| 3051 | send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
| 3052 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3053 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 3054 | return 0; |
| 3055 | } |
| 3056 | |
| 3057 | /* Restore the function in the stack. */ |
| 3058 | lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 3059 | |
| 3060 | /* push arguments in the stack. */ |
| 3061 | if (!hlua_txn_new(s->hlua.T, s, px, l7)) { |
| 3062 | send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
| 3063 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3064 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 3065 | return 0; |
| 3066 | } |
| 3067 | s->hlua.nargs = 1; |
| 3068 | |
| 3069 | /* push keywords in the stack. */ |
| 3070 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 3071 | /* Check stack available size. */ |
| 3072 | if (!lua_checkstack(s->hlua.T, 1)) { |
| 3073 | send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
| 3074 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3075 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 3076 | return 0; |
| 3077 | } |
| 3078 | if (!lua_checkstack(s->hlua.T, 1)) { |
| 3079 | send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
| 3080 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3081 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 3082 | return 0; |
| 3083 | } |
| 3084 | hlua_arg2lua(s->hlua.T, arg_p); |
| 3085 | s->hlua.nargs++; |
| 3086 | } |
| 3087 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 3088 | /* We must initialize the execution timeouts. */ |
| 3089 | s->hlua.expire = tick_add(now_ms, hlua_timeout_session); |
| 3090 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3091 | /* Set the currently running flag. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 3092 | HLUA_SET_RUN(&s->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | /* Execute the function. */ |
| 3096 | switch (hlua_ctx_resume(&s->hlua, 0)) { |
| 3097 | /* finished. */ |
| 3098 | case HLUA_E_OK: |
| 3099 | /* Convert the returned value in sample. */ |
| 3100 | hlua_lua2smp(s->hlua.T, -1, smp); |
| 3101 | lua_pop(s->hlua.T, 1); |
| 3102 | |
| 3103 | /* Set the end of execution flag. */ |
| 3104 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 3105 | return 1; |
| 3106 | |
| 3107 | /* yield. */ |
| 3108 | case HLUA_E_AGAIN: |
| 3109 | send_log(px, LOG_ERR, "Lua sample-fetch '%s': cannot use yielded functions.", fcn->name); |
| 3110 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3111 | Alert("Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name); |
| 3112 | return 0; |
| 3113 | |
| 3114 | /* finished with error. */ |
| 3115 | case HLUA_E_ERRMSG: |
| 3116 | /* Display log. */ |
| 3117 | send_log(px, LOG_ERR, "Lua sample-fetch '%s': %s.", fcn->name, lua_tostring(s->hlua.T, -1)); |
| 3118 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3119 | Alert("Lua sample-fetch '%s': %s.\n", fcn->name, lua_tostring(s->hlua.T, -1)); |
| 3120 | lua_pop(s->hlua.T, 1); |
| 3121 | return 0; |
| 3122 | |
| 3123 | case HLUA_E_ERR: |
| 3124 | /* Display log. */ |
| 3125 | send_log(px, LOG_ERR, "Lua sample-fetch '%s' returns an unknown error.", fcn->name); |
| 3126 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3127 | Alert("Lua sample-fetch '%s': returns an unknown error.\n", fcn->name); |
| 3128 | |
| 3129 | default: |
| 3130 | return 0; |
| 3131 | } |
| 3132 | } |
| 3133 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3134 | /* This function is an LUA binding used for registering |
| 3135 | * "sample-conv" functions. It expects a converter name used |
| 3136 | * in the haproxy configuration file, and an LUA function. |
| 3137 | */ |
| 3138 | __LJMP static int hlua_register_converters(lua_State *L) |
| 3139 | { |
| 3140 | struct sample_conv_kw_list *sck; |
| 3141 | const char *name; |
| 3142 | int ref; |
| 3143 | int len; |
| 3144 | struct hlua_function *fcn; |
| 3145 | |
| 3146 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 3147 | |
| 3148 | /* First argument : converter name. */ |
| 3149 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 3150 | |
| 3151 | /* Second argument : lua function. */ |
| 3152 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 3153 | |
| 3154 | /* Allocate and fill the sample fetch keyword struct. */ |
| 3155 | sck = malloc(sizeof(struct sample_conv_kw_list) + |
| 3156 | sizeof(struct sample_conv) * 2); |
| 3157 | if (!sck) |
| 3158 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3159 | fcn = malloc(sizeof(*fcn)); |
| 3160 | if (!fcn) |
| 3161 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3162 | |
| 3163 | /* Fill fcn. */ |
| 3164 | fcn->name = strdup(name); |
| 3165 | if (!fcn->name) |
| 3166 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3167 | fcn->function_ref = ref; |
| 3168 | |
| 3169 | /* List head */ |
| 3170 | sck->list.n = sck->list.p = NULL; |
| 3171 | |
| 3172 | /* converter keyword. */ |
| 3173 | len = strlen("lua.") + strlen(name) + 1; |
| 3174 | sck->kw[0].kw = malloc(len); |
| 3175 | if (!sck->kw[0].kw) |
| 3176 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3177 | |
| 3178 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 3179 | sck->kw[0].process = hlua_sample_conv_wrapper; |
| 3180 | sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 3181 | sck->kw[0].val_args = NULL; |
| 3182 | sck->kw[0].in_type = SMP_T_STR; |
| 3183 | sck->kw[0].out_type = SMP_T_STR; |
| 3184 | sck->kw[0].private = fcn; |
| 3185 | |
| 3186 | /* End of array. */ |
| 3187 | memset(&sck->kw[1], 0, sizeof(struct sample_conv)); |
| 3188 | |
| 3189 | /* Register this new converter */ |
| 3190 | sample_register_convs(sck); |
| 3191 | |
| 3192 | return 0; |
| 3193 | } |
| 3194 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3195 | /* This fucntion is an LUA binding used for registering |
| 3196 | * "sample-fetch" functions. It expects a converter name used |
| 3197 | * in the haproxy configuration file, and an LUA function. |
| 3198 | */ |
| 3199 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 3200 | { |
| 3201 | const char *name; |
| 3202 | int ref; |
| 3203 | int len; |
| 3204 | struct sample_fetch_kw_list *sfk; |
| 3205 | struct hlua_function *fcn; |
| 3206 | |
| 3207 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 3208 | |
| 3209 | /* First argument : sample-fetch name. */ |
| 3210 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 3211 | |
| 3212 | /* Second argument : lua function. */ |
| 3213 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 3214 | |
| 3215 | /* Allocate and fill the sample fetch keyword struct. */ |
| 3216 | sfk = malloc(sizeof(struct sample_fetch_kw_list) + |
| 3217 | sizeof(struct sample_fetch) * 2); |
| 3218 | if (!sfk) |
| 3219 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3220 | fcn = malloc(sizeof(*fcn)); |
| 3221 | if (!fcn) |
| 3222 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3223 | |
| 3224 | /* Fill fcn. */ |
| 3225 | fcn->name = strdup(name); |
| 3226 | if (!fcn->name) |
| 3227 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3228 | fcn->function_ref = ref; |
| 3229 | |
| 3230 | /* List head */ |
| 3231 | sfk->list.n = sfk->list.p = NULL; |
| 3232 | |
| 3233 | /* sample-fetch keyword. */ |
| 3234 | len = strlen("lua.") + strlen(name) + 1; |
| 3235 | sfk->kw[0].kw = malloc(len); |
| 3236 | if (!sfk->kw[0].kw) |
| 3237 | return luaL_error(L, "lua out of memory error."); |
| 3238 | |
| 3239 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 3240 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
| 3241 | sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 3242 | sfk->kw[0].val_args = NULL; |
| 3243 | sfk->kw[0].out_type = SMP_T_STR; |
| 3244 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 3245 | sfk->kw[0].val = 0; |
| 3246 | sfk->kw[0].private = fcn; |
| 3247 | |
| 3248 | /* End of array. */ |
| 3249 | memset(&sfk->kw[1], 0, sizeof(struct sample_fetch)); |
| 3250 | |
| 3251 | /* Register this new fetch. */ |
| 3252 | sample_register_fetches(sfk); |
| 3253 | |
| 3254 | return 0; |
| 3255 | } |
| 3256 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3257 | /* global {tcp|http}-request parser. Return 1 in succes case, else return 0. */ |
| 3258 | static int hlua_parse_rule(const char **args, int *cur_arg, struct proxy *px, |
| 3259 | struct hlua_rule **rule_p, char **err) |
| 3260 | { |
| 3261 | struct hlua_rule *rule; |
| 3262 | |
| 3263 | /* Memory for the rule. */ |
| 3264 | rule = malloc(sizeof(*rule)); |
| 3265 | if (!rule) { |
| 3266 | memprintf(err, "out of memory error"); |
| 3267 | return 0; |
| 3268 | } |
| 3269 | *rule_p = rule; |
| 3270 | |
| 3271 | /* The requiered arg is a function name. */ |
| 3272 | if (!args[*cur_arg]) { |
| 3273 | memprintf(err, "expect Lua function name"); |
| 3274 | return 0; |
| 3275 | } |
| 3276 | |
| 3277 | /* Lookup for the symbol, and check if it is a function. */ |
| 3278 | lua_getglobal(gL.T, args[*cur_arg]); |
| 3279 | if (lua_isnil(gL.T, -1)) { |
| 3280 | lua_pop(gL.T, 1); |
| 3281 | memprintf(err, "Lua function '%s' not found", args[*cur_arg]); |
| 3282 | return 0; |
| 3283 | } |
| 3284 | if (!lua_isfunction(gL.T, -1)) { |
| 3285 | lua_pop(gL.T, 1); |
| 3286 | memprintf(err, "'%s' is not a function", args[*cur_arg]); |
| 3287 | return 0; |
| 3288 | } |
| 3289 | |
| 3290 | /* Reference the Lua function and store the reference. */ |
| 3291 | rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 3292 | rule->fcn.name = strdup(args[*cur_arg]); |
| 3293 | if (!rule->fcn.name) { |
| 3294 | memprintf(err, "out of memory error."); |
| 3295 | return 0; |
| 3296 | } |
| 3297 | (*cur_arg)++; |
| 3298 | |
| 3299 | /* TODO: later accept arguments. */ |
| 3300 | rule->args = NULL; |
| 3301 | |
| 3302 | return 1; |
| 3303 | } |
| 3304 | |
| 3305 | /* This function is a wrapper to execute each LUA function declared |
| 3306 | * as an action wrapper during the initialisation period. This function |
| 3307 | * return 1 if the processing is finished (with oe without error) and |
| 3308 | * return 0 if the function must be called again because the LUA |
| 3309 | * returns a yield. |
| 3310 | */ |
| 3311 | static int hlua_request_act_wrapper(struct hlua_rule *rule, struct proxy *px, |
| 3312 | struct session *s, struct http_txn *http_txn, |
| 3313 | unsigned int analyzer) |
| 3314 | { |
| 3315 | char **arg; |
| 3316 | |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 3317 | /* In the execution wrappers linked with a session, the |
| 3318 | * Lua context can be not initialized. This behavior |
| 3319 | * permits to save performances because a systematic |
| 3320 | * Lua initialization cause 5% performances loss. |
| 3321 | */ |
| 3322 | if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) { |
| 3323 | send_log(px, LOG_ERR, "Lua action '%s': can't initialize Lua context.", rule->fcn.name); |
| 3324 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3325 | Alert("Lua action '%s': can't initialize Lua context.\n", rule->fcn.name); |
| 3326 | return 0; |
| 3327 | } |
| 3328 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3329 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 3330 | if (!HLUA_IS_RUNNING(&s->hlua)) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3331 | /* Check stack available size. */ |
| 3332 | if (!lua_checkstack(s->hlua.T, 1)) { |
| 3333 | send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name); |
| 3334 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3335 | Alert("Lua function '%s': full stack.\n", rule->fcn.name); |
| 3336 | return 0; |
| 3337 | } |
| 3338 | |
| 3339 | /* Restore the function in the stack. */ |
| 3340 | lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->fcn.function_ref); |
| 3341 | |
| 3342 | /* Create and and push object session in the stack. */ |
| 3343 | if (!hlua_txn_new(s->hlua.T, s, px, http_txn)) { |
| 3344 | send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name); |
| 3345 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3346 | Alert("Lua function '%s': full stack.\n", rule->fcn.name); |
| 3347 | return 0; |
| 3348 | } |
| 3349 | s->hlua.nargs = 1; |
| 3350 | |
| 3351 | /* push keywords in the stack. */ |
| 3352 | for (arg = rule->args; arg && *arg; arg++) { |
| 3353 | if (!lua_checkstack(s->hlua.T, 1)) { |
| 3354 | send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name); |
| 3355 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3356 | Alert("Lua function '%s': full stack.\n", rule->fcn.name); |
| 3357 | return 0; |
| 3358 | } |
| 3359 | lua_pushstring(s->hlua.T, *arg); |
| 3360 | s->hlua.nargs++; |
| 3361 | } |
| 3362 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 3363 | /* We must initialize the execution timeouts. */ |
| 3364 | s->hlua.expire = tick_add(now_ms, hlua_timeout_session); |
| 3365 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3366 | /* Set the currently running flag. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 3367 | HLUA_SET_RUN(&s->hlua); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | /* Execute the function. */ |
| 3371 | switch (hlua_ctx_resume(&s->hlua, 1)) { |
| 3372 | /* finished. */ |
| 3373 | case HLUA_E_OK: |
| 3374 | return 1; |
| 3375 | |
| 3376 | /* yield. */ |
| 3377 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 3378 | /* Set timeout in the required channel. */ |
| 3379 | if (s->hlua.wake_time != TICK_ETERNITY) { |
| 3380 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
| 3381 | s->req->analyse_exp = s->hlua.wake_time; |
| 3382 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
| 3383 | s->rep->analyse_exp = s->hlua.wake_time; |
| 3384 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3385 | /* Some actions can be wake up when a "write" event |
| 3386 | * is detected on a response channel. This is useful |
| 3387 | * only for actions targetted on the requests. |
| 3388 | */ |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3389 | if (HLUA_IS_WAKERESWR(&s->hlua)) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3390 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame^] | 3391 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
| 3392 | s->rep->analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3393 | } |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3394 | if (HLUA_IS_WAKEREQWR(&s->hlua)) |
| 3395 | s->req->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3396 | return 0; |
| 3397 | |
| 3398 | /* finished with error. */ |
| 3399 | case HLUA_E_ERRMSG: |
| 3400 | /* Display log. */ |
| 3401 | send_log(px, LOG_ERR, "Lua function '%s': %s.", rule->fcn.name, lua_tostring(s->hlua.T, -1)); |
| 3402 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3403 | Alert("Lua function '%s': %s.\n", rule->fcn.name, lua_tostring(s->hlua.T, -1)); |
| 3404 | lua_pop(s->hlua.T, 1); |
| 3405 | return 1; |
| 3406 | |
| 3407 | case HLUA_E_ERR: |
| 3408 | /* Display log. */ |
| 3409 | send_log(px, LOG_ERR, "Lua function '%s' return an unknown error.", rule->fcn.name); |
| 3410 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3411 | Alert("Lua function '%s' return an unknown error.\n", rule->fcn.name); |
| 3412 | |
| 3413 | default: |
| 3414 | return 1; |
| 3415 | } |
| 3416 | } |
| 3417 | |
| 3418 | /* Lua execution wrapper for "tcp-request". This function uses |
| 3419 | * "hlua_request_act_wrapper" for executing the LUA code. |
| 3420 | */ |
| 3421 | int hlua_tcp_req_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px, |
| 3422 | struct session *s) |
| 3423 | { |
| 3424 | return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data, |
| 3425 | px, s, NULL, AN_REQ_INSPECT_FE); |
| 3426 | } |
| 3427 | |
| 3428 | /* Lua execution wrapper for "tcp-response". This function uses |
| 3429 | * "hlua_request_act_wrapper" for executing the LUA code. |
| 3430 | */ |
| 3431 | int hlua_tcp_res_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px, |
| 3432 | struct session *s) |
| 3433 | { |
| 3434 | return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data, |
| 3435 | px, s, NULL, AN_RES_INSPECT); |
| 3436 | } |
| 3437 | |
| 3438 | /* Lua execution wrapper for http-request. |
| 3439 | * This function uses "hlua_request_act_wrapper" for executing |
| 3440 | * the LUA code. |
| 3441 | */ |
| 3442 | int hlua_http_req_act_wrapper(struct http_req_rule *rule, struct proxy *px, |
| 3443 | struct session *s, struct http_txn *http_txn) |
| 3444 | { |
| 3445 | return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px, |
| 3446 | s, http_txn, AN_REQ_HTTP_PROCESS_FE); |
| 3447 | } |
| 3448 | |
| 3449 | /* Lua execution wrapper for http-response. |
| 3450 | * This function uses "hlua_request_act_wrapper" for executing |
| 3451 | * the LUA code. |
| 3452 | */ |
| 3453 | int hlua_http_res_act_wrapper(struct http_res_rule *rule, struct proxy *px, |
| 3454 | struct session *s, struct http_txn *http_txn) |
| 3455 | { |
| 3456 | return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px, |
| 3457 | s, http_txn, AN_RES_HTTP_PROCESS_BE); |
| 3458 | } |
| 3459 | |
| 3460 | /* tcp-request <*> configuration wrapper. */ |
| 3461 | static int tcp_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 3462 | struct tcp_rule *rule, char **err) |
| 3463 | { |
| 3464 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data, err)) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3465 | return 0; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3466 | rule->action = TCP_ACT_CUSTOM; |
| 3467 | rule->action_ptr = hlua_tcp_req_act_wrapper; |
| 3468 | return 1; |
| 3469 | } |
| 3470 | |
| 3471 | /* tcp-response <*> configuration wrapper. */ |
| 3472 | static int tcp_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 3473 | struct tcp_rule *rule, char **err) |
| 3474 | { |
| 3475 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data, err)) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3476 | return 0; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3477 | rule->action = TCP_ACT_CUSTOM; |
| 3478 | rule->action_ptr = hlua_tcp_res_act_wrapper; |
| 3479 | return 1; |
| 3480 | } |
| 3481 | |
| 3482 | /* http-request <*> configuration wrapper. */ |
| 3483 | static int http_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 3484 | struct http_req_rule *rule, char **err) |
| 3485 | { |
| 3486 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err)) |
| 3487 | return -1; |
| 3488 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 3489 | rule->action_ptr = hlua_http_req_act_wrapper; |
| 3490 | return 1; |
| 3491 | } |
| 3492 | |
| 3493 | /* http-response <*> configuration wrapper. */ |
| 3494 | static int http_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 3495 | struct http_res_rule *rule, char **err) |
| 3496 | { |
| 3497 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err)) |
| 3498 | return -1; |
| 3499 | rule->action = HTTP_RES_ACT_CUSTOM_CONT; |
| 3500 | rule->action_ptr = hlua_http_res_act_wrapper; |
| 3501 | return 1; |
| 3502 | } |
| 3503 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 3504 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 3505 | struct proxy *defpx, const char *file, int line, |
| 3506 | char **err, unsigned int *timeout) |
| 3507 | { |
| 3508 | const char *error; |
| 3509 | |
| 3510 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 3511 | if (error && *error != '\0') { |
| 3512 | memprintf(err, "%s: invalid timeout", args[0]); |
| 3513 | return -1; |
| 3514 | } |
| 3515 | return 0; |
| 3516 | } |
| 3517 | |
| 3518 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 3519 | struct proxy *defpx, const char *file, int line, |
| 3520 | char **err) |
| 3521 | { |
| 3522 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 3523 | file, line, err, &hlua_timeout_session); |
| 3524 | } |
| 3525 | |
| 3526 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 3527 | struct proxy *defpx, const char *file, int line, |
| 3528 | char **err) |
| 3529 | { |
| 3530 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 3531 | file, line, err, &hlua_timeout_task); |
| 3532 | } |
| 3533 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 3534 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 3535 | struct proxy *defpx, const char *file, int line, |
| 3536 | char **err) |
| 3537 | { |
| 3538 | char *error; |
| 3539 | |
| 3540 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 3541 | if (*error != '\0') { |
| 3542 | memprintf(err, "%s: invalid number", args[0]); |
| 3543 | return -1; |
| 3544 | } |
| 3545 | return 0; |
| 3546 | } |
| 3547 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 3548 | /* This function is called by the main configuration key "lua-load". It loads and |
| 3549 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 3550 | * the main lua entry point. |
| 3551 | * |
| 3552 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 3553 | * occured, otherwise it returns 0. |
| 3554 | * |
| 3555 | * In some error case, LUA set an error message in top of the stack. This function |
| 3556 | * returns this error message in the HAProxy logs and pop it from the stack. |
| 3557 | */ |
| 3558 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 3559 | struct proxy *defpx, const char *file, int line, |
| 3560 | char **err) |
| 3561 | { |
| 3562 | int error; |
| 3563 | |
| 3564 | /* Just load and compile the file. */ |
| 3565 | error = luaL_loadfile(gL.T, args[1]); |
| 3566 | if (error) { |
| 3567 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 3568 | lua_pop(gL.T, 1); |
| 3569 | return -1; |
| 3570 | } |
| 3571 | |
| 3572 | /* If no syntax error where detected, execute the code. */ |
| 3573 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 3574 | switch (error) { |
| 3575 | case LUA_OK: |
| 3576 | break; |
| 3577 | case LUA_ERRRUN: |
| 3578 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 3579 | lua_pop(gL.T, 1); |
| 3580 | return -1; |
| 3581 | case LUA_ERRMEM: |
| 3582 | memprintf(err, "lua out of memory error\n"); |
| 3583 | return -1; |
| 3584 | case LUA_ERRERR: |
| 3585 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 3586 | lua_pop(gL.T, 1); |
| 3587 | return -1; |
| 3588 | case LUA_ERRGCMM: |
| 3589 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 3590 | lua_pop(gL.T, 1); |
| 3591 | return -1; |
| 3592 | default: |
| 3593 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 3594 | lua_pop(gL.T, 1); |
| 3595 | return -1; |
| 3596 | } |
| 3597 | |
| 3598 | return 0; |
| 3599 | } |
| 3600 | |
| 3601 | /* configuration keywords declaration */ |
| 3602 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 3603 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 3604 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 3605 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 3606 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 3607 | { 0, NULL, NULL }, |
| 3608 | }}; |
| 3609 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3610 | static struct http_req_action_kw_list http_req_kws = {"lua", { }, { |
| 3611 | { "lua", http_req_action_register_lua }, |
| 3612 | { NULL, NULL } |
| 3613 | }}; |
| 3614 | |
| 3615 | static struct http_res_action_kw_list http_res_kws = {"lua", { }, { |
| 3616 | { "lua", http_res_action_register_lua }, |
| 3617 | { NULL, NULL } |
| 3618 | }}; |
| 3619 | |
| 3620 | static struct tcp_action_kw_list tcp_req_cont_kws = {"lua", { }, { |
| 3621 | { "lua", tcp_req_action_register_lua }, |
| 3622 | { NULL, NULL } |
| 3623 | }}; |
| 3624 | |
| 3625 | static struct tcp_action_kw_list tcp_res_cont_kws = {"lua", { }, { |
| 3626 | { "lua", tcp_res_action_register_lua }, |
| 3627 | { NULL, NULL } |
| 3628 | }}; |
| 3629 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 3630 | int hlua_post_init() |
| 3631 | { |
| 3632 | struct hlua_init_function *init; |
| 3633 | const char *msg; |
| 3634 | enum hlua_exec ret; |
| 3635 | |
| 3636 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 3637 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 3638 | ret = hlua_ctx_resume(&gL, 0); |
| 3639 | switch (ret) { |
| 3640 | case HLUA_E_OK: |
| 3641 | lua_pop(gL.T, -1); |
| 3642 | return 1; |
| 3643 | case HLUA_E_AGAIN: |
| 3644 | Alert("lua init: yield not allowed.\n"); |
| 3645 | return 0; |
| 3646 | case HLUA_E_ERRMSG: |
| 3647 | msg = lua_tostring(gL.T, -1); |
| 3648 | Alert("lua init: %s.\n", msg); |
| 3649 | return 0; |
| 3650 | case HLUA_E_ERR: |
| 3651 | default: |
| 3652 | Alert("lua init: unknown runtime error.\n"); |
| 3653 | return 0; |
| 3654 | } |
| 3655 | } |
| 3656 | return 1; |
| 3657 | } |
| 3658 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 3659 | void hlua_init(void) |
| 3660 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 3661 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 3662 | int idx; |
| 3663 | struct sample_fetch *sf; |
| 3664 | struct hlua_sample_fetch *hsf; |
| 3665 | char *p; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3666 | #ifdef USE_OPENSSL |
| 3667 | char *args[4]; |
| 3668 | struct srv_kw *kw; |
| 3669 | int tmp_error; |
| 3670 | char *error; |
| 3671 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 3672 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 3673 | /* Initialise com signals pool session. */ |
| 3674 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 3675 | |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3676 | /* Initialise sleep pool. */ |
| 3677 | pool2_hlua_sleep = create_pool("hlua_sleep", sizeof(struct hlua_sleep), MEM_F_SHARED); |
| 3678 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 3679 | /* Register configuration keywords. */ |
| 3680 | cfg_register_keywords(&cfg_kws); |
| 3681 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 3682 | /* Register custom HTTP rules. */ |
| 3683 | http_req_keywords_register(&http_req_kws); |
| 3684 | http_res_keywords_register(&http_res_kws); |
| 3685 | tcp_req_cont_keywords_register(&tcp_req_cont_kws); |
| 3686 | tcp_res_cont_keywords_register(&tcp_res_cont_kws); |
| 3687 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 3688 | /* Init main lua stack. */ |
| 3689 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 3690 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 3691 | LIST_INIT(&gL.com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 3692 | gL.T = luaL_newstate(); |
| 3693 | hlua_sethlua(&gL); |
| 3694 | gL.Tref = LUA_REFNIL; |
| 3695 | gL.task = NULL; |
| 3696 | |
| 3697 | /* Initialise lua. */ |
| 3698 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 3699 | |
| 3700 | /* |
| 3701 | * |
| 3702 | * Create "core" object. |
| 3703 | * |
| 3704 | */ |
| 3705 | |
| 3706 | /* This integer entry is just used as base value for the object "core". */ |
| 3707 | lua_pushinteger(gL.T, 0); |
| 3708 | |
| 3709 | /* Create and fill the metatable. */ |
| 3710 | lua_newtable(gL.T); |
| 3711 | |
| 3712 | /* Create and fill the __index entry. */ |
| 3713 | lua_pushstring(gL.T, "__index"); |
| 3714 | lua_newtable(gL.T); |
| 3715 | |
| 3716 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3717 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 3718 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 3719 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 3720 | /* Register special functions. */ |
| 3721 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 3722 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3723 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3724 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 3725 | hlua_class_function(gL.T, "yield", hlua_yield); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 3726 | hlua_class_function(gL.T, "set_nice", hlua_setnice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3727 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 3728 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 3729 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 3730 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 3731 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 3732 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3733 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 3734 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 3735 | /* Store the table __index in the metable. */ |
| 3736 | lua_settable(gL.T, -3); |
| 3737 | |
| 3738 | /* Register previous table in the registry with named entry. */ |
| 3739 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 3740 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CORE); /* register class session. */ |
| 3741 | |
| 3742 | /* Register previous table in the registry with reference. */ |
| 3743 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 3744 | class_core_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 3745 | |
| 3746 | /* Create new object with class Core. */ |
| 3747 | lua_setmetatable(gL.T, -2); |
| 3748 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3749 | |
| 3750 | /* |
| 3751 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3752 | * Register class Channel |
| 3753 | * |
| 3754 | */ |
| 3755 | |
| 3756 | /* Create and fill the metatable. */ |
| 3757 | lua_newtable(gL.T); |
| 3758 | |
| 3759 | /* Create and fille the __index entry. */ |
| 3760 | lua_pushstring(gL.T, "__index"); |
| 3761 | lua_newtable(gL.T); |
| 3762 | |
| 3763 | /* Register . */ |
| 3764 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 3765 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 3766 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 3767 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 3768 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 3769 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 3770 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 3771 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 3772 | hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len); |
| 3773 | |
| 3774 | lua_settable(gL.T, -3); |
| 3775 | |
| 3776 | /* Register previous table in the registry with reference and named entry. */ |
| 3777 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 3778 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */ |
| 3779 | class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 3780 | |
| 3781 | /* |
| 3782 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3783 | * Register class TXN |
| 3784 | * |
| 3785 | */ |
| 3786 | |
| 3787 | /* Create and fill the metatable. */ |
| 3788 | lua_newtable(gL.T); |
| 3789 | |
| 3790 | /* Create and fille the __index entry. */ |
| 3791 | lua_pushstring(gL.T, "__index"); |
| 3792 | lua_newtable(gL.T); |
| 3793 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 3794 | /* Browse existing fetches and create the associated |
| 3795 | * object method. |
| 3796 | */ |
| 3797 | sf = NULL; |
| 3798 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 3799 | |
| 3800 | /* Dont register the keywork if the arguments check function are |
| 3801 | * not safe during the runtime. |
| 3802 | */ |
| 3803 | if ((sf->val_args != NULL) && |
| 3804 | (sf->val_args != val_payload_lv) && |
| 3805 | (sf->val_args != val_hdr)) |
| 3806 | continue; |
| 3807 | |
| 3808 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 3809 | * by an underscore. |
| 3810 | */ |
| 3811 | strncpy(trash.str, sf->kw, trash.size); |
| 3812 | trash.str[trash.size - 1] = '\0'; |
| 3813 | for (p = trash.str; *p; p++) |
| 3814 | if (*p == '.' || *p == '-' || *p == '+') |
| 3815 | *p = '_'; |
| 3816 | |
| 3817 | /* Register the function. */ |
| 3818 | lua_pushstring(gL.T, trash.str); |
| 3819 | hsf = lua_newuserdata(gL.T, sizeof(struct hlua_sample_fetch)); |
| 3820 | hsf->f = sf; |
| 3821 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
| 3822 | lua_settable(gL.T, -3); |
| 3823 | } |
| 3824 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3825 | /* Register Lua functions. */ |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 3826 | hlua_class_function(gL.T, "get_headers", hlua_session_getheaders); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3827 | hlua_class_function(gL.T, "set_priv", hlua_setpriv); |
| 3828 | hlua_class_function(gL.T, "get_priv", hlua_getpriv); |
Thierry FOURNIER | e94e774 | 2015-02-17 14:59:53 +0100 | [diff] [blame] | 3829 | hlua_class_function(gL.T, "req_channel", hlua_txn_req_channel); |
| 3830 | hlua_class_function(gL.T, "res_channel", hlua_txn_res_channel); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3831 | hlua_class_function(gL.T, "close", hlua_txn_close); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3832 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3833 | lua_settable(gL.T, -3); |
| 3834 | |
| 3835 | /* Register previous table in the registry with reference and named entry. */ |
| 3836 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 3837 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */ |
| 3838 | class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3839 | |
| 3840 | /* |
| 3841 | * |
| 3842 | * Register class Socket |
| 3843 | * |
| 3844 | */ |
| 3845 | |
| 3846 | /* Create and fill the metatable. */ |
| 3847 | lua_newtable(gL.T); |
| 3848 | |
| 3849 | /* Create and fille the __index entry. */ |
| 3850 | lua_pushstring(gL.T, "__index"); |
| 3851 | lua_newtable(gL.T); |
| 3852 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3853 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3854 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3855 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3856 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 3857 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 3858 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 3859 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 3860 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 3861 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 3862 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 3863 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 3864 | |
| 3865 | lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
| 3866 | |
| 3867 | /* Register the garbage collector entry. */ |
| 3868 | lua_pushstring(gL.T, "__gc"); |
| 3869 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
| 3870 | lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
| 3871 | |
| 3872 | /* Register previous table in the registry with reference and named entry. */ |
| 3873 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 3874 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 3875 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */ |
| 3876 | class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */ |
| 3877 | |
| 3878 | /* Proxy and server configuration initialisation. */ |
| 3879 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 3880 | init_new_proxy(&socket_proxy); |
| 3881 | socket_proxy.parent = NULL; |
| 3882 | socket_proxy.last_change = now.tv_sec; |
| 3883 | socket_proxy.id = "LUA-SOCKET"; |
| 3884 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 3885 | socket_proxy.maxconn = 0; |
| 3886 | socket_proxy.accept = NULL; |
| 3887 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 3888 | socket_proxy.srv = NULL; |
| 3889 | socket_proxy.conn_retries = 0; |
| 3890 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 3891 | |
| 3892 | /* Init TCP server: unchanged parameters */ |
| 3893 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 3894 | socket_tcp.next = NULL; |
| 3895 | socket_tcp.proxy = &socket_proxy; |
| 3896 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 3897 | LIST_INIT(&socket_tcp.actconns); |
| 3898 | LIST_INIT(&socket_tcp.pendconns); |
| 3899 | socket_tcp.state = SRV_ST_RUNNING; /* early server setup */ |
| 3900 | socket_tcp.last_change = 0; |
| 3901 | socket_tcp.id = "LUA-TCP-CONN"; |
| 3902 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 3903 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 3904 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 3905 | |
| 3906 | /* XXX: Copy default parameter from default server, |
| 3907 | * but the default server is not initialized. |
| 3908 | */ |
| 3909 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 3910 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 3911 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 3912 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 3913 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 3914 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 3915 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 3916 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 3917 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 3918 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 3919 | |
| 3920 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 3921 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 3922 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 3923 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 3924 | socket_tcp.check.server = &socket_tcp; |
| 3925 | |
| 3926 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 3927 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 3928 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 3929 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 3930 | socket_tcp.agent.server = &socket_tcp; |
| 3931 | |
| 3932 | socket_tcp.xprt = &raw_sock; |
| 3933 | |
| 3934 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3935 | /* Init TCP server: unchanged parameters */ |
| 3936 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 3937 | socket_ssl.next = NULL; |
| 3938 | socket_ssl.proxy = &socket_proxy; |
| 3939 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 3940 | LIST_INIT(&socket_ssl.actconns); |
| 3941 | LIST_INIT(&socket_ssl.pendconns); |
| 3942 | socket_ssl.state = SRV_ST_RUNNING; /* early server setup */ |
| 3943 | socket_ssl.last_change = 0; |
| 3944 | socket_ssl.id = "LUA-SSL-CONN"; |
| 3945 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 3946 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 3947 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 3948 | |
| 3949 | /* XXX: Copy default parameter from default server, |
| 3950 | * but the default server is not initialized. |
| 3951 | */ |
| 3952 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 3953 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 3954 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 3955 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 3956 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 3957 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 3958 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 3959 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 3960 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 3961 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 3962 | |
| 3963 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 3964 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 3965 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 3966 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 3967 | socket_ssl.check.server = &socket_ssl; |
| 3968 | |
| 3969 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 3970 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 3971 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 3972 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 3973 | socket_ssl.agent.server = &socket_ssl; |
| 3974 | |
| 3975 | socket_ssl.xprt = &raw_sock; |
| 3976 | |
| 3977 | args[0] = "ssl"; |
| 3978 | args[1] = "verify"; |
| 3979 | args[2] = "none"; |
| 3980 | args[3] = NULL; |
| 3981 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3982 | for (idx = 0; idx < 3; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3983 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 3984 | /* |
| 3985 | * |
| 3986 | * If the keyword is not known, we can search in the registered |
| 3987 | * server keywords. This is usefull to configure special SSL |
| 3988 | * features like client certificates and ssl_verify. |
| 3989 | * |
| 3990 | */ |
| 3991 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 3992 | if (tmp_error != 0) { |
| 3993 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 3994 | abort(); /* This must be never arrives because the command line |
| 3995 | not editable by the user. */ |
| 3996 | } |
| 3997 | idx += kw->skip; |
| 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | /* Initialize SSL server. */ |
| 4002 | if (socket_ssl.xprt == &ssl_sock) { |
| 4003 | socket_ssl.use_ssl = 1; |
| 4004 | ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy); |
| 4005 | } |
| 4006 | #endif |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 4007 | } |