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