blob: 0f06188ae8be05068e4c63f94c819cbb9e3f2d47 [file] [log] [blame]
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001#include <sys/socket.h>
2
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01003#include <lauxlib.h>
4#include <lua.h>
5#include <lualib.h>
6
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007#include <ebpttree.h>
8
9#include <common/cfgparse.h>
10
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010011#include <types/connection.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010012#include <types/hlua.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010013#include <types/proto_tcp.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010014#include <types/proxy.h>
15
Thierry FOURNIER55da1652015-01-23 11:36:30 +010016#include <proto/arg.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010017#include <proto/channel.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010018#include <proto/hdr_idx.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010019#include <proto/obj_type.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010020#include <proto/payload.h>
21#include <proto/proto_http.h>
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +010022#include <proto/proto_tcp.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010023#include <proto/raw_sock.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010024#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010025#include <proto/server.h>
26#include <proto/session.h>
27#include <proto/ssl_sock.h>
28#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010029#include <proto/task.h>
30
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010031/* Lua uses longjmp to perform yield or throwing errors. This
32 * macro is used only for identifying the function that can
33 * not return because a longjmp is executed.
34 * __LJMP marks a prototype of hlua file that can use longjmp.
35 * WILL_LJMP() marks an lua function that will use longjmp.
36 * MAY_LJMP() marks an lua function that may use longjmp.
37 */
38#define __LJMP
39#define WILL_LJMP(func) func
40#define MAY_LJMP(func) func
41
Thierry FOURNIER380d0932015-01-23 14:27:52 +010042/* The main Lua execution context. */
43struct hlua gL;
44
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010045/* This is the memory pool containing all the signal structs. These
46 * struct are used to store each requiered signal between two tasks.
47 */
48struct pool_head *pool2_hlua_com;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +010049struct pool_head *pool2_hlua_sleep;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010050
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051/* Used for Socket connection. */
52static struct proxy socket_proxy;
53static struct server socket_tcp;
54#ifdef USE_OPENSSL
55static struct server socket_ssl;
56#endif
57
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010058/* List head of the function called at the initialisation time. */
59struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
60
Thierry FOURNIER380d0932015-01-23 14:27:52 +010061/* Store the fast lua context for coroutines. This tree uses the
62 * Lua stack pointer value as indexed entry, and store the associated
63 * hlua context.
64 */
65struct eb_root hlua_ctx = EB_ROOT_UNIQUE;
66
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010067/* The following variables contains the reference of the different
68 * Lua classes. These references are useful for identify metadata
69 * associated with an object.
70 */
71static int class_core_ref;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010072static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010073static int class_socket_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010074
Thierry FOURNIER55da1652015-01-23 11:36:30 +010075/* These functions converts types between HAProxy internal args or
76 * sample and LUA types. Another function permits to check if the
77 * LUA stack contains arguments according with an required ARG_T
78 * format.
79 */
80static int hlua_arg2lua(lua_State *L, const struct arg *arg);
81static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
82__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, unsigned int mask);
83static int hlua_smp2lua(lua_State *L, const struct sample *smp);
84static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
85
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +010086/* Used to check an Lua function type in the stack. It creates and
87 * returns a reference of the function. This function throws an
88 * error if the rgument is not a "function".
89 */
90__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
91{
92 if (!lua_isfunction(L, argno)) {
93 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
94 WILL_LJMP(luaL_argerror(L, argno, msg));
95 }
96 lua_pushvalue(L, argno);
97 return luaL_ref(L, LUA_REGISTRYINDEX);
98}
99
100/* The three following functions are useful for adding entries
101 * in a table. These functions takes a string and respectively an
102 * integer, a string or a function and add it to the table in the
103 * top of the stack.
104 *
105 * These functions throws an error if no more stack size is
106 * available.
107 */
108__LJMP static inline void hlua_class_const_int(lua_State *L, const char *name,
109 unsigned int value)
110{
111 if (!lua_checkstack(L, 2))
112 WILL_LJMP(luaL_error(L, "full stack"));
113 lua_pushstring(L, name);
114 lua_pushunsigned(L, value);
115 lua_settable(L, -3);
116}
117__LJMP static inline void hlua_class_const_str(lua_State *L, const char *name,
118 const char *value)
119{
120 if (!lua_checkstack(L, 2))
121 WILL_LJMP(luaL_error(L, "full stack"));
122 lua_pushstring(L, name);
123 lua_pushstring(L, value);
124 lua_settable(L, -3);
125}
126__LJMP static inline void hlua_class_function(lua_State *L, const char *name,
127 int (*function)(lua_State *L))
128{
129 if (!lua_checkstack(L, 2))
130 WILL_LJMP(luaL_error(L, "full stack"));
131 lua_pushstring(L, name);
132 lua_pushcclosure(L, function, 0);
133 lua_settable(L, -3);
134}
135
136/* This function check the number of arguments available in the
137 * stack. If the number of arguments available is not the same
138 * then <nb> an error is throwed.
139 */
140__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
141{
142 if (lua_gettop(L) == nb)
143 return;
144 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
145}
146
147/* Return true if the data in stack[<ud>] is an object of
148 * type <class_ref>.
149 */
150static int hlua_udataistype(lua_State *L, int ud, int class_ref)
151{
152 void *p = lua_touserdata(L, ud);
153 if (!p)
154 return 0;
155
156 if (!lua_getmetatable(L, ud))
157 return 0;
158
159 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
160 if (!lua_rawequal(L, -1, -2)) {
161 lua_pop(L, 2);
162 return 0;
163 }
164
165 lua_pop(L, 2);
166 return 1;
167}
168
169/* Return an object of the expected type, or throws an error. */
170__LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref)
171{
172 if (!hlua_udataistype(L, ud, class_ref))
173 WILL_LJMP(luaL_argerror(L, 1, NULL));
174 return lua_touserdata(L, ud);
175}
176
177/* This fucntion push an error string prefixed by the file name
178 * and the line number where the error is encountered.
179 */
180static int hlua_pusherror(lua_State *L, const char *fmt, ...)
181{
182 va_list argp;
183 va_start(argp, fmt);
184 luaL_where(L, 1);
185 lua_pushvfstring(L, fmt, argp);
186 va_end(argp);
187 lua_concat(L, 2);
188 return 1;
189}
190
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100191/* This function register a new signal. "lua" is the current lua
192 * execution context. It contains a pointer to the associated task.
193 * "link" is a list head attached to an other task that must be wake
194 * the lua task if an event occurs. This is useful with external
195 * events like TCP I/O or sleep functions. This funcion allocate
196 * memory for the signal.
197 */
198static int hlua_com_new(struct hlua *lua, struct list *link)
199{
200 struct hlua_com *com = pool_alloc2(pool2_hlua_com);
201 if (!com)
202 return 0;
203 LIST_ADDQ(&lua->com, &com->purge_me);
204 LIST_ADDQ(link, &com->wake_me);
205 com->task = lua->task;
206 return 1;
207}
208
209/* This function purge all the pending signals when the LUA execution
210 * is finished. This prevent than a coprocess try to wake a deleted
211 * task. This function remove the memory associated to the signal.
212 */
213static void hlua_com_purge(struct hlua *lua)
214{
215 struct hlua_com *com, *back;
216
217 /* Delete all pending communication signals. */
218 list_for_each_entry_safe(com, back, &lua->com, purge_me) {
219 LIST_DEL(&com->purge_me);
220 LIST_DEL(&com->wake_me);
221 pool_free2(pool2_hlua_com, com);
222 }
223}
224
225/* This function sends signals. It wakes all the tasks attached
226 * to a list head, and remove the signal, and free the used
227 * memory.
228 */
229static void hlua_com_wake(struct list *wake)
230{
231 struct hlua_com *com, *back;
232
233 /* Wake task and delete all pending communication signals. */
234 list_for_each_entry_safe(com, back, wake, wake_me) {
235 LIST_DEL(&com->purge_me);
236 LIST_DEL(&com->wake_me);
237 task_wakeup(com->task, TASK_WOKEN_MSG);
238 pool_free2(pool2_hlua_com, com);
239 }
240}
241
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100242/* This functions is used with sample fetch and converters. It
243 * converts the HAProxy configuration argument in a lua stack
244 * values.
245 *
246 * It takes an array of "arg", and each entry of the array is
247 * converted and pushed in the LUA stack.
248 */
249static int hlua_arg2lua(lua_State *L, const struct arg *arg)
250{
251 switch (arg->type) {
252 case ARGT_SINT:
253 lua_pushinteger(L, arg->data.sint);
254 break;
255
256 case ARGT_UINT:
257 case ARGT_TIME:
258 case ARGT_SIZE:
259 lua_pushunsigned(L, arg->data.sint);
260 break;
261
262 case ARGT_STR:
263 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
264 break;
265
266 case ARGT_IPV4:
267 case ARGT_IPV6:
268 case ARGT_MSK4:
269 case ARGT_MSK6:
270 case ARGT_FE:
271 case ARGT_BE:
272 case ARGT_TAB:
273 case ARGT_SRV:
274 case ARGT_USR:
275 case ARGT_MAP:
276 default:
277 lua_pushnil(L);
278 break;
279 }
280 return 1;
281}
282
283/* This function take one entrie in an LUA stack at the index "ud",
284 * and try to convert it in an HAProxy argument entry. This is useful
285 * with sample fetch wrappers. The input arguments are gived to the
286 * lua wrapper and converted as arg list by thi function.
287 */
288static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
289{
290 switch (lua_type(L, ud)) {
291
292 case LUA_TNUMBER:
293 case LUA_TBOOLEAN:
294 arg->type = ARGT_SINT;
295 arg->data.sint = lua_tointeger(L, ud);
296 break;
297
298 case LUA_TSTRING:
299 arg->type = ARGT_STR;
300 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
301 break;
302
303 case LUA_TUSERDATA:
304 case LUA_TNIL:
305 case LUA_TTABLE:
306 case LUA_TFUNCTION:
307 case LUA_TTHREAD:
308 case LUA_TLIGHTUSERDATA:
309 arg->type = ARGT_SINT;
310 arg->data.uint = 0;
311 break;
312 }
313 return 1;
314}
315
316/* the following functions are used to convert a struct sample
317 * in Lua type. This useful to convert the return of the
318 * fetchs or converters.
319 */
320static int hlua_smp2lua(lua_State *L, const struct sample *smp)
321{
322 switch (smp->type) {
323 case SMP_T_SINT:
324 lua_pushinteger(L, smp->data.sint);
325 break;
326
327 case SMP_T_BOOL:
328 case SMP_T_UINT:
329 lua_pushunsigned(L, smp->data.uint);
330 break;
331
332 case SMP_T_BIN:
333 case SMP_T_STR:
334 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
335 break;
336
337 case SMP_T_METH:
338 switch (smp->data.meth.meth) {
339 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
340 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
341 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
342 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
343 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
344 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
345 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
346 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
347 case HTTP_METH_OTHER:
348 lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len);
349 break;
350 default:
351 lua_pushnil(L);
352 break;
353 }
354 break;
355
356 case SMP_T_IPV4:
357 case SMP_T_IPV6:
358 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
359 default:
360 lua_pushnil(L);
361 break;
362 }
363 return 1;
364}
365
366/* the following functions are used to convert an Lua type in a
367 * struct sample. This is useful to provide data from a converter
368 * to the LUA code.
369 */
370static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
371{
372 switch (lua_type(L, ud)) {
373
374 case LUA_TNUMBER:
375 smp->type = SMP_T_SINT;
376 smp->data.sint = lua_tointeger(L, ud);
377 break;
378
379
380 case LUA_TBOOLEAN:
381 smp->type = SMP_T_BOOL;
382 smp->data.uint = lua_toboolean(L, ud);
383 break;
384
385 case LUA_TSTRING:
386 smp->type = SMP_T_STR;
387 smp->flags |= SMP_F_CONST;
388 smp->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.str.len);
389 break;
390
391 case LUA_TUSERDATA:
392 case LUA_TNIL:
393 case LUA_TTABLE:
394 case LUA_TFUNCTION:
395 case LUA_TTHREAD:
396 case LUA_TLIGHTUSERDATA:
397 smp->type = SMP_T_BOOL;
398 smp->data.uint = 0;
399 break;
400 }
401 return 1;
402}
403
404/* This function check the "argp" builded by another conversion function
405 * is in accord with the expected argp defined by the "mask". The fucntion
406 * returns true or false. It can be adjust the types if there compatibles.
407 */
408__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, unsigned int mask)
409{
410 int min_arg;
411 int idx;
412
413 idx = 0;
414 min_arg = ARGM(mask);
415 mask >>= ARGM_BITS;
416
417 while (1) {
418
419 /* Check oversize. */
420 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
421 WILL_LJMP(luaL_argerror(L, first + idx, "Malformad argument mask"));
422 }
423
424 /* Check for mandatory arguments. */
425 if (argp[idx].type == ARGT_STOP) {
426 if (idx + 1 < min_arg)
427 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
428 return 0;
429 }
430
431 /* Check for exceed the number of requiered argument. */
432 if ((mask & ARGT_MASK) == ARGT_STOP &&
433 argp[idx].type != ARGT_STOP) {
434 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
435 }
436
437 if ((mask & ARGT_MASK) == ARGT_STOP &&
438 argp[idx].type == ARGT_STOP) {
439 return 0;
440 }
441
442 /* Compatibility mask. */
443 switch (argp[idx].type) {
444 case ARGT_SINT:
445 switch (mask & ARGT_MASK) {
446 case ARGT_UINT: argp[idx].type = mask & ARGT_MASK; break;
447 case ARGT_TIME: argp[idx].type = mask & ARGT_MASK; break;
448 case ARGT_SIZE: argp[idx].type = mask & ARGT_MASK; break;
449 }
450 break;
451 }
452
453 /* Check for type of argument. */
454 if ((mask & ARGT_MASK) != argp[idx].type) {
455 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
456 arg_type_names[(mask & ARGT_MASK)],
457 arg_type_names[argp[idx].type & ARGT_MASK]);
458 WILL_LJMP(luaL_argerror(L, first + idx, msg));
459 }
460
461 /* Next argument. */
462 mask >>= ARGT_BITS;
463 idx++;
464 }
465}
466
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100467/*
468 * The following functions are used to make correspondance between the the
469 * executed lua pointer and the "struct hlua *" that contain the context.
470 * They run with the tree head "hlua_ctx", they just perform lookup in the
471 * tree.
472 *
473 * - hlua_gethlua : return the hlua context associated with an lua_State.
474 * - hlua_delhlua : remove the association between hlua context and lua_state.
475 * - hlua_sethlua : create the association between hlua context and lua_state.
476 */
477static inline struct hlua *hlua_gethlua(lua_State *L)
478{
479 struct ebpt_node *node;
480
481 node = ebpt_lookup(&hlua_ctx, L);
482 if (!node)
483 return NULL;
484 return ebpt_entry(node, struct hlua, node);
485}
486static inline void hlua_delhlua(struct hlua *hlua)
487{
488 if (hlua->node.key)
489 ebpt_delete(&hlua->node);
490}
491static inline void hlua_sethlua(struct hlua *hlua)
492{
493 hlua->node.key = hlua->T;
494 ebpt_insert(&hlua_ctx, &hlua->node);
495}
496
497/* This function initialises the Lua environment stored in the session.
498 * It must be called at the start of the session. This function creates
499 * an LUA coroutine. It can not be use to crete the main LUA context.
500 */
501int hlua_ctx_init(struct hlua *lua, struct task *task)
502{
503 lua->Mref = LUA_REFNIL;
504 lua->state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100505 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100506 lua->T = lua_newthread(gL.T);
507 if (!lua->T) {
508 lua->Tref = LUA_REFNIL;
509 return 0;
510 }
511 hlua_sethlua(lua);
512 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
513 lua->task = task;
514 return 1;
515}
516
517/* Used to destroy the Lua coroutine when the attached session or task
518 * is destroyed. The destroy also the memory context. The struct "lua"
519 * is not freed.
520 */
521void hlua_ctx_destroy(struct hlua *lua)
522{
523 /* Remove context. */
524 hlua_delhlua(lua);
525
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100526 /* Purge all the pending signals. */
527 hlua_com_purge(lua);
528
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100529 /* The thread is garbage collected by Lua. */
530 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
531 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
532}
533
534/* This function is used to restore the Lua context when a coroutine
535 * fails. This function copy the common memory between old coroutine
536 * and the new coroutine. The old coroutine is destroyed, and its
537 * replaced by the new coroutine.
538 * If the flag "keep_msg" is set, the last entry of the old is assumed
539 * as string error message and it is copied in the new stack.
540 */
541static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
542{
543 lua_State *T;
544 int new_ref;
545
546 /* Renew the main LUA stack doesn't have sense. */
547 if (lua == &gL)
548 return 0;
549
550 /* Remove context. */
551 hlua_delhlua(lua);
552
553 /* New Lua coroutine. */
554 T = lua_newthread(gL.T);
555 if (!T)
556 return 0;
557
558 /* Copy last error message. */
559 if (keep_msg)
560 lua_xmove(lua->T, T, 1);
561
562 /* Copy data between the coroutines. */
563 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
564 lua_xmove(lua->T, T, 1);
565 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
566
567 /* Destroy old data. */
568 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
569
570 /* The thread is garbage collected by Lua. */
571 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
572
573 /* Fill the struct with the new coroutine values. */
574 lua->Mref = new_ref;
575 lua->T = T;
576 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
577
578 /* Set context. */
579 hlua_sethlua(lua);
580
581 return 1;
582}
583
584/* This function start or resumes the Lua stack execution. If the flag
585 * "yield_allowed" if no set and the LUA stack execution returns a yield
586 * The function return an error.
587 *
588 * The function can returns 4 values:
589 * - HLUA_E_OK : The execution is terminated without any errors.
590 * - HLUA_E_AGAIN : The execution must continue at the next associated
591 * task wakeup.
592 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
593 * the top of the stack.
594 * - HLUA_E_ERR : An error has occured without error message.
595 *
596 * If an error occured, the stack is renewed and it is ready to run new
597 * LUA code.
598 */
599static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
600{
601 int ret;
602 const char *msg;
603
604 lua->state = HLUA_RUN;
605
606 /* Call the function. */
607 ret = lua_resume(lua->T, gL.T, lua->nargs);
608 switch (ret) {
609
610 case LUA_OK:
611 ret = HLUA_E_OK;
612 break;
613
614 case LUA_YIELD:
615 if (!yield_allowed) {
616 lua_settop(lua->T, 0); /* Empty the stack. */
617 if (!lua_checkstack(lua->T, 1)) {
618 ret = HLUA_E_ERR;
619 break;
620 }
621 lua_pushfstring(lua->T, "yield not allowed");
622 ret = HLUA_E_ERRMSG;
623 break;
624 }
625 ret = HLUA_E_AGAIN;
626 break;
627
628 case LUA_ERRRUN:
629 if (!lua_checkstack(lua->T, 1)) {
630 ret = HLUA_E_ERR;
631 break;
632 }
633 msg = lua_tostring(lua->T, -1);
634 lua_settop(lua->T, 0); /* Empty the stack. */
635 lua_pop(lua->T, 1);
636 if (msg)
637 lua_pushfstring(lua->T, "runtime error: %s", msg);
638 else
639 lua_pushfstring(lua->T, "unknown runtime error");
640 ret = HLUA_E_ERRMSG;
641 break;
642
643 case LUA_ERRMEM:
644 lua_settop(lua->T, 0); /* Empty the stack. */
645 if (!lua_checkstack(lua->T, 1)) {
646 ret = HLUA_E_ERR;
647 break;
648 }
649 lua_pushfstring(lua->T, "out of memory error");
650 ret = HLUA_E_ERRMSG;
651 break;
652
653 case LUA_ERRERR:
654 if (!lua_checkstack(lua->T, 1)) {
655 ret = HLUA_E_ERR;
656 break;
657 }
658 msg = lua_tostring(lua->T, -1);
659 lua_settop(lua->T, 0); /* Empty the stack. */
660 lua_pop(lua->T, 1);
661 if (msg)
662 lua_pushfstring(lua->T, "message handler error: %s", msg);
663 else
664 lua_pushfstring(lua->T, "message handler error");
665 ret = HLUA_E_ERRMSG;
666 break;
667
668 default:
669 lua_settop(lua->T, 0); /* Empty the stack. */
670 if (!lua_checkstack(lua->T, 1)) {
671 ret = HLUA_E_ERR;
672 break;
673 }
674 lua_pushfstring(lua->T, "unknonwn error");
675 ret = HLUA_E_ERRMSG;
676 break;
677 }
678
679 switch (ret) {
680 case HLUA_E_AGAIN:
681 break;
682
683 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100684 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100685 hlua_ctx_renew(lua, 1);
686 lua->state = HLUA_STOP;
687 break;
688
689 case HLUA_E_ERR:
690 lua->state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100691 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100692 hlua_ctx_renew(lua, 0);
693 break;
694
695 case HLUA_E_OK:
696 lua->state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100697 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100698 break;
699 }
700
701 return ret;
702}
703
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100704/* A class is a lot of memory that contain data. This data can be a table,
705 * an integer or user data. This data is associated with a metatable. This
706 * metatable have an original version registred in the global context with
707 * the name of the object (_G[<name>] = <metable> ).
708 *
709 * A metable is a table that modify the standard behavior of a standard
710 * access to the associated data. The entries of this new metatable are
711 * defined as is:
712 *
713 * http://lua-users.org/wiki/MetatableEvents
714 *
715 * __index
716 *
717 * we access an absent field in a table, the result is nil. This is
718 * true, but it is not the whole truth. Actually, such access triggers
719 * the interpreter to look for an __index metamethod: If there is no
720 * such method, as usually happens, then the access results in nil;
721 * otherwise, the metamethod will provide the result.
722 *
723 * Control 'prototype' inheritance. When accessing "myTable[key]" and
724 * the key does not appear in the table, but the metatable has an __index
725 * property:
726 *
727 * - if the value is a function, the function is called, passing in the
728 * table and the key; the return value of that function is returned as
729 * the result.
730 *
731 * - if the value is another table, the value of the key in that table is
732 * asked for and returned (and if it doesn't exist in that table, but that
733 * table's metatable has an __index property, then it continues on up)
734 *
735 * - Use "rawget(myTable,key)" to skip this metamethod.
736 *
737 * http://www.lua.org/pil/13.4.1.html
738 *
739 * __newindex
740 *
741 * Like __index, but control property assignment.
742 *
743 * __mode - Control weak references. A string value with one or both
744 * of the characters 'k' and 'v' which specifies that the the
745 * keys and/or values in the table are weak references.
746 *
747 * __call - Treat a table like a function. When a table is followed by
748 * parenthesis such as "myTable( 'foo' )" and the metatable has
749 * a __call key pointing to a function, that function is invoked
750 * (passing any specified arguments) and the return value is
751 * returned.
752 *
753 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
754 * called, if the metatable for myTable has a __metatable
755 * key, the value of that key is returned instead of the
756 * actual metatable.
757 *
758 * __tostring - Control string representation. When the builtin
759 * "tostring( myTable )" function is called, if the metatable
760 * for myTable has a __tostring property set to a function,
761 * that function is invoked (passing myTable to it) and the
762 * return value is used as the string representation.
763 *
764 * __len - Control table length. When the table length is requested using
765 * the length operator ( '#' ), if the metatable for myTable has
766 * a __len key pointing to a function, that function is invoked
767 * (passing myTable to it) and the return value used as the value
768 * of "#myTable".
769 *
770 * __gc - Userdata finalizer code. When userdata is set to be garbage
771 * collected, if the metatable has a __gc field pointing to a
772 * function, that function is first invoked, passing the userdata
773 * to it. The __gc metamethod is not called for tables.
774 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
775 *
776 * Special metamethods for redefining standard operators:
777 * http://www.lua.org/pil/13.1.html
778 *
779 * __add "+"
780 * __sub "-"
781 * __mul "*"
782 * __div "/"
783 * __unm "!"
784 * __pow "^"
785 * __concat ".."
786 *
787 * Special methods for redfining standar relations
788 * http://www.lua.org/pil/13.2.html
789 *
790 * __eq "=="
791 * __lt "<"
792 * __le "<="
793 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100794
795/*
796 *
797 *
798 * Class Socket
799 *
800 *
801 */
802
803__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
804{
805 return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
806}
807
808/* This function is the handler called for each I/O on the established
809 * connection. It is used for notify space avalaible to send or data
810 * received.
811 */
812static void hlua_socket_handler(struct stream_interface *si)
813{
814 struct appctx *appctx = objt_appctx(si->end);
815 struct connection *c = objt_conn(si->ib->cons->end);
816
817 /* Wakeup the main session if the client connection is closed. */
818 if (!c || channel_output_closed(si->ib) || channel_input_closed(si->ob)) {
819 if (appctx->ctx.hlua.socket) {
820 appctx->ctx.hlua.socket->s = NULL;
821 appctx->ctx.hlua.socket = NULL;
822 }
823 si_shutw(si);
824 si_shutr(si);
825 si->ib->flags |= CF_READ_NULL;
826 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
827 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
828 return;
829 }
830
831 if (!(c->flags & CO_FL_CONNECTED))
832 return;
833
834 /* This function is called after the connect. */
835 appctx->ctx.hlua.connected = 1;
836
837 /* Wake the tasks which wants to write if the buffer have avalaible space. */
838 if (channel_may_recv(si->ob))
839 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
840
841 /* Wake the tasks which wants to read if the buffer contains data. */
842 if (channel_is_empty(si->ib))
843 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
844}
845
846/* This function is called when the "struct session" is destroyed.
847 * Remove the link from the object to this session.
848 * Wake all the pending signals.
849 */
850static void hlua_socket_release(struct stream_interface *si)
851{
852 struct appctx *appctx = objt_appctx(si->end);
853
854 /* Remove my link in the original object. */
855 if (appctx->ctx.hlua.socket)
856 appctx->ctx.hlua.socket->s = NULL;
857
858 /* Wake all the task waiting for me. */
859 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
860 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
861}
862
863/* If the garbage collectio of the object is launch, nobody
864 * uses this object. If the session does not exists, just quit.
865 * Send the shutdown signal to the session. In some cases,
866 * pending signal can rest in the read and write lists. destroy
867 * it.
868 */
869__LJMP static int hlua_socket_gc(lua_State *L)
870{
871 MAY_LJMP(check_args(L, 1, "__gc"));
872
873 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
874 struct appctx *appctx;
875
876 if (!socket->s)
877 return 0;
878
879 /* Remove all reference between the Lua stack and the coroutine session. */
880 appctx = objt_appctx(socket->s->si[0].end);
881 session_shutdown(socket->s, SN_ERR_KILLED);
882 socket->s = NULL;
883 appctx->ctx.hlua.socket = NULL;
884
885 return 0;
886}
887
888/* The close function send shutdown signal and break the
889 * links between the session and the object.
890 */
891__LJMP static int hlua_socket_close(lua_State *L)
892{
893 MAY_LJMP(check_args(L, 1, "close"));
894
895 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
896 struct appctx *appctx;
897
898 if (!socket->s)
899 return 0;
900
901 /* Close the session and remove the associated stop task. */
902 session_shutdown(socket->s, SN_ERR_KILLED);
903 appctx = objt_appctx(socket->s->si[0].end);
904 appctx->ctx.hlua.socket = NULL;
905 socket->s = NULL;
906
907 return 0;
908}
909
910/* This Lua function assumes that the stack contain three parameters.
911 * 1 - USERDATA containing a struct socket
912 * 2 - INTEGER with values of the macro defined below
913 * If the integer is -1, we must read at most one line.
914 * If the integer is -2, we ust read all the data until the
915 * end of the stream.
916 * If the integer is positive value, we must read a number of
917 * bytes corresponding to this value.
918 */
919#define HLSR_READ_LINE (-1)
920#define HLSR_READ_ALL (-2)
921__LJMP static int hlua_socket_receive_yield(struct lua_State *L)
922{
923 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
924 int wanted = lua_tointeger(L, 2);
925 struct hlua *hlua = hlua_gethlua(L);
926 struct appctx *appctx;
927 int len;
928 int nblk;
929 char *blk1;
930 int len1;
931 char *blk2;
932 int len2;
933
934 /* Check if this lua stack is schedulable. */
935 if (!hlua || !hlua->task)
936 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
937 "'frontend', 'backend' or 'task'"));
938
939 /* check for connection closed. If some data where read, return it. */
940 if (!socket->s)
941 goto connection_closed;
942
943 if (wanted == HLSR_READ_LINE) {
944
945 /* Read line. */
946 nblk = bo_getline_nc(socket->s->si[0].ob, &blk1, &len1, &blk2, &len2);
947 if (nblk < 0) /* Connection close. */
948 goto connection_closed;
949 if (nblk == 0) /* No data avalaible. */
950 goto connection_empty;
951 }
952
953 else if (wanted == HLSR_READ_ALL) {
954
955 /* Read all the available data. */
956 nblk = bo_getblk_nc(socket->s->si[0].ob, &blk1, &len1, &blk2, &len2);
957 if (nblk < 0) /* Connection close. */
958 goto connection_closed;
959 if (nblk == 0) /* No data avalaible. */
960 goto connection_empty;
961 }
962
963 else {
964
965 /* Read a block of data. */
966 nblk = bo_getblk_nc(socket->s->si[0].ob, &blk1, &len1, &blk2, &len2);
967 if (nblk < 0) /* Connection close. */
968 goto connection_closed;
969 if (nblk == 0) /* No data avalaible. */
970 goto connection_empty;
971
972 if (len1 > wanted) {
973 nblk = 1;
974 len1 = wanted;
975 } if (nblk == 2 && len1 + len2 > wanted)
976 len2 = wanted - len1;
977 }
978
979 len = len1;
980
981 luaL_addlstring(&socket->b, blk1, len1);
982 if (nblk == 2) {
983 len += len2;
984 luaL_addlstring(&socket->b, blk2, len2);
985 }
986
987 /* Consume data. */
988 bo_skip(socket->s->si[0].ob, len);
989
990 /* Don't wait anything. */
991 si_update(&socket->s->si[0]);
992
993 /* If the pattern reclaim to read all the data
994 * in the connection, got out.
995 */
996 if (wanted == HLSR_READ_ALL)
997 goto connection_empty;
998 else if (wanted >= 0 && len < wanted)
999 goto connection_empty;
1000
1001 /* Return result. */
1002 luaL_pushresult(&socket->b);
1003 return 1;
1004
1005connection_closed:
1006
1007 /* If the buffer containds data. */
1008 if (socket->b.n > 0) {
1009 luaL_pushresult(&socket->b);
1010 return 1;
1011 }
1012 lua_pushnil(L);
1013 lua_pushstring(L, "connection closed.");
1014 return 2;
1015
1016connection_empty:
1017
1018 appctx = objt_appctx(socket->s->si[0].end);
1019 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read))
1020 WILL_LJMP(luaL_error(L, "out of memory"));
1021 WILL_LJMP(lua_yieldk(L, 0, 0, hlua_socket_receive_yield));
1022 return 0;
1023}
1024
1025/* This Lus function gets two parameters. The first one can be string
1026 * or a number. If the string is "*l", the user require one line. If
1027 * the string is "*a", the user require all the content of the stream.
1028 * If the value is a number, the user require a number of bytes equal
1029 * to the value. The default value is "*l" (a line).
1030 *
1031 * This paraeter with a variable type is converted in integer. This
1032 * integer takes this values:
1033 * -1 : read a line
1034 * -2 : read all the stream
1035 * >0 : amount if bytes.
1036 *
1037 * The second parameter is optinal. It contains a string that must be
1038 * concatenated with the read data.
1039 */
1040__LJMP static int hlua_socket_receive(struct lua_State *L)
1041{
1042 int wanted = HLSR_READ_LINE;
1043 const char *pattern;
1044 int type;
1045 char *error;
1046 size_t len;
1047
1048 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1049 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1050
1051 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1052
1053 /* check for pattern. */
1054 if (lua_gettop(L) >= 2) {
1055 type = lua_type(L, 2);
1056 if (type == LUA_TSTRING) {
1057 pattern = lua_tostring(L, 2);
1058 if (strcmp(pattern, "*a") == 0)
1059 wanted = HLSR_READ_ALL;
1060 else if (strcmp(pattern, "*l") == 0)
1061 wanted = HLSR_READ_LINE;
1062 else {
1063 wanted = strtoll(pattern, &error, 10);
1064 if (*error != '\0')
1065 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1066 }
1067 }
1068 else if (type == LUA_TNUMBER) {
1069 wanted = lua_tointeger(L, 2);
1070 if (wanted < 0)
1071 WILL_LJMP(luaL_error(L, "Unsupported size."));
1072 }
1073 }
1074
1075 /* Set pattern. */
1076 lua_pushinteger(L, wanted);
1077 lua_replace(L, 2);
1078
1079 /* init bufffer, and fiil it wih prefix. */
1080 luaL_buffinit(L, &socket->b);
1081
1082 /* Check prefix. */
1083 if (lua_gettop(L) >= 3) {
1084 if (lua_type(L, 3) != LUA_TSTRING)
1085 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1086 pattern = lua_tolstring(L, 3, &len);
1087 luaL_addlstring(&socket->b, pattern, len);
1088 }
1089
1090 return __LJMP(hlua_socket_receive_yield(L));
1091}
1092
1093/* Write the Lua input string in the output buffer.
1094 * This fucntion returns a yield if no space are available.
1095 */
1096static int hlua_socket_write_yield(struct lua_State *L)
1097{
1098 struct hlua_socket *socket;
1099 struct hlua *hlua = hlua_gethlua(L);
1100 struct appctx *appctx;
1101 size_t buf_len;
1102 const char *buf;
1103 int len;
1104 int send_len;
1105 int sent;
1106
1107 /* Check if this lua stack is schedulable. */
1108 if (!hlua || !hlua->task)
1109 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1110 "'frontend', 'backend' or 'task'"));
1111
1112 /* Get object */
1113 socket = MAY_LJMP(hlua_checksocket(L, 1));
1114 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1115 sent = MAY_LJMP(luaL_checkunsigned(L, 3));
1116
1117 /* Check for connection close. */
1118 if (!socket->s || channel_output_closed(socket->s->req)) {
1119 lua_pushinteger(L, -1);
1120 return 1;
1121 }
1122
1123 /* Update the input buffer data. */
1124 buf += sent;
1125 send_len = buf_len - sent;
1126
1127 /* All the data are sent. */
1128 if (sent >= buf_len)
1129 return 1; /* Implicitly return the length sent. */
1130
1131 /* Check for avalaible space. */
1132 len = buffer_total_space(socket->s->si[0].ib->buf);
1133 if (len <= 0)
1134 goto hlua_socket_write_yield_return;
1135
1136 /* send data */
1137 if (len < send_len)
1138 send_len = len;
1139 len = bi_putblk(socket->s->si[0].ib, buf+sent, send_len);
1140
1141 /* "Not enough space" (-1), "Buffer too little to contain
1142 * the data" (-2) are not expected because the available length
1143 * is tested.
1144 * Other unknown error are also not expected.
1145 */
1146 if (len <= 0) {
1147 MAY_LJMP(hlua_socket_close(L));
1148 lua_pop(L, 1);
1149 lua_pushunsigned(L, -1);
1150 return 1;
1151 }
1152
1153 /* update buffers. */
1154 si_update(&socket->s->si[0]);
1155 socket->s->si[0].ib->rex = TICK_ETERNITY;
1156 socket->s->si[0].ob->wex = TICK_ETERNITY;
1157
1158 /* Update length sent. */
1159 lua_pop(L, 1);
1160 lua_pushunsigned(L, sent + len);
1161
1162 /* All the data buffer is sent ? */
1163 if (sent + len >= buf_len)
1164 return 1;
1165
1166hlua_socket_write_yield_return:
1167 appctx = objt_appctx(socket->s->si[0].end);
1168 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1169 WILL_LJMP(luaL_error(L, "out of memory"));
1170 WILL_LJMP(lua_yieldk(L, 0, 0, hlua_socket_write_yield));
1171 return 0;
1172}
1173
1174/* This function initiate the send of data. It just check the input
1175 * parameters and push an integer in the Lua stack that contain the
1176 * amount of data writed in the buffer. This is used by the function
1177 * "hlua_socket_write_yield" that can yield.
1178 *
1179 * The Lua function gets between 3 and 4 parameters. The first one is
1180 * the associated object. The second is a string buffer. The third is
1181 * a facultative integer that represents where is the buffer position
1182 * of the start of the data that can send. The first byte is the
1183 * position "1". The default value is "1". The fourth argument is a
1184 * facultative integer that represents where is the buffer position
1185 * of the end of the data that can send. The default is the last byte.
1186 */
1187static int hlua_socket_send(struct lua_State *L)
1188{
1189 int i;
1190 int j;
1191 const char *buf;
1192 size_t buf_len;
1193
1194 /* Check number of arguments. */
1195 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1196 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1197
1198 /* Get the string. */
1199 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1200
1201 /* Get and check j. */
1202 if (lua_gettop(L) == 4) {
1203 j = MAY_LJMP(luaL_checkinteger(L, 4));
1204 if (j < 0)
1205 j = buf_len + j + 1;
1206 if (j > buf_len)
1207 j = buf_len + 1;
1208 lua_pop(L, 1);
1209 }
1210 else
1211 j = buf_len;
1212
1213 /* Get and check i. */
1214 if (lua_gettop(L) == 3) {
1215 i = MAY_LJMP(luaL_checkinteger(L, 3));
1216 if (i < 0)
1217 i = buf_len + i + 1;
1218 if (i > buf_len)
1219 i = buf_len + 1;
1220 lua_pop(L, 1);
1221 } else
1222 i = 1;
1223
1224 /* Check bth i and j. */
1225 if (i > j) {
1226 lua_pushunsigned(L, 0);
1227 return 1;
1228 }
1229 if (i == 0 && j == 0) {
1230 lua_pushunsigned(L, 0);
1231 return 1;
1232 }
1233 if (i == 0)
1234 i = 1;
1235 if (j == 0)
1236 j = 1;
1237
1238 /* Pop the string. */
1239 lua_pop(L, 1);
1240
1241 /* Update the buffer length. */
1242 buf += i - 1;
1243 buf_len = j - i + 1;
1244 lua_pushlstring(L, buf, buf_len);
1245
1246 /* This unsigned is used to remember the amount of sent data. */
1247 lua_pushunsigned(L, 0);
1248
1249 return MAY_LJMP(hlua_socket_write_yield(L));
1250}
1251
1252#define SOCKET_INFO_EXPANDED_FORM "[0000:0000:0000:0000:0000:0000:0000:0000]:12345"
1253static char _socket_info_expanded_form[] = SOCKET_INFO_EXPANDED_FORM;
1254#define SOCKET_INFO_MAX_LEN (sizeof(_socket_info_expanded_form))
1255__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
1256{
1257 static char buffer[SOCKET_INFO_MAX_LEN];
1258 int ret;
1259 int len;
1260 char *p;
1261
1262 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
1263 if (ret <= 0) {
1264 lua_pushnil(L);
1265 return 1;
1266 }
1267
1268 if (ret == AF_UNIX) {
1269 lua_pushstring(L, buffer+1);
1270 return 1;
1271 }
1272 else if (ret == AF_INET6) {
1273 buffer[0] = '[';
1274 len = strlen(buffer);
1275 buffer[len] = ']';
1276 len++;
1277 buffer[len] = ':';
1278 len++;
1279 p = buffer;
1280 }
1281 else if (ret == AF_INET) {
1282 p = buffer + 1;
1283 len = strlen(p);
1284 p[len] = ':';
1285 len++;
1286 }
1287 else {
1288 lua_pushnil(L);
1289 return 1;
1290 }
1291
1292 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
1293 lua_pushnil(L);
1294 return 1;
1295 }
1296
1297 lua_pushstring(L, p);
1298 return 1;
1299}
1300
1301/* Returns information about the peer of the connection. */
1302__LJMP static int hlua_socket_getpeername(struct lua_State *L)
1303{
1304 MAY_LJMP(check_args(L, 1, "getpeername"));
1305
1306 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1307
1308 /* Check if the tcp object is avalaible. */
1309 if (!socket->s) {
1310 lua_pushnil(L);
1311 return 1;
1312 }
1313
1314 struct connection *conn = objt_conn(socket->s->si[1].end);
1315 if (!conn) {
1316 lua_pushnil(L);
1317 return 1;
1318 }
1319
1320 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
1321 unsigned int salen = sizeof(conn->addr.to);
1322 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
1323 lua_pushnil(L);
1324 return 1;
1325 }
1326 conn->flags |= CO_FL_ADDR_TO_SET;
1327 }
1328
1329 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
1330}
1331
1332/* Returns information about my connection side. */
1333static int hlua_socket_getsockname(struct lua_State *L)
1334{
1335 MAY_LJMP(check_args(L, 1, "getsockname"));
1336
1337 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1338
1339 /* Check if the tcp object is avalaible. */
1340 if (!socket->s) {
1341 lua_pushnil(L);
1342 return 1;
1343 }
1344
1345 struct connection *conn = objt_conn(socket->s->si[1].end);
1346 if (!conn) {
1347 lua_pushnil(L);
1348 return 1;
1349 }
1350
1351 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
1352 unsigned int salen = sizeof(conn->addr.from);
1353 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
1354 lua_pushnil(L);
1355 return 1;
1356 }
1357 conn->flags |= CO_FL_ADDR_FROM_SET;
1358 }
1359
1360 return hlua_socket_info(L, &conn->addr.from);
1361}
1362
1363/* This struct define the applet. */
1364static struct si_applet update_applet = {
1365 .obj_type = OBJ_TYPE_APPLET,
1366 .name = "<LUA_TCP>",
1367 .fct = hlua_socket_handler,
1368 .release = hlua_socket_release,
1369};
1370
1371__LJMP static int hlua_socket_connect_yield(struct lua_State *L)
1372{
1373 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1374 struct hlua *hlua = hlua_gethlua(L);
1375 struct appctx *appctx;
1376
1377 /* Check for connection close. */
1378 if (!hlua || !socket->s || channel_output_closed(socket->s->req)) {
1379 lua_pushnil(L);
1380 lua_pushstring(L, "Can't connect");
1381 return 2;
1382 }
1383
1384 appctx = objt_appctx(socket->s->si[0].end);
1385
1386 /* Check for connection established. */
1387 if (appctx->ctx.hlua.connected) {
1388 lua_pushinteger(L, 1);
1389 return 1;
1390 }
1391
1392 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1393 WILL_LJMP(luaL_error(L, "out of memory error"));
1394 WILL_LJMP(lua_yieldk(L, 0, 0, hlua_socket_connect_yield));
1395 return 0;
1396}
1397
1398/* This function fail or initite the connection. */
1399__LJMP static int hlua_socket_connect(struct lua_State *L)
1400{
1401 struct hlua_socket *socket;
1402 unsigned int port;
1403 const char *ip;
1404 struct connection *conn;
1405
1406 MAY_LJMP(check_args(L, 3, "connect"));
1407
1408 /* Get args. */
1409 socket = MAY_LJMP(hlua_checksocket(L, 1));
1410 ip = MAY_LJMP(luaL_checkstring(L, 2));
1411 port = MAY_LJMP(luaL_checkunsigned(L, 3));
1412
1413 conn = si_alloc_conn(socket->s->req->cons, 0);
1414 if (!conn)
1415 WILL_LJMP(luaL_error(L, "connect: internal error"));
1416
1417 /* Parse ip address. */
1418 conn->addr.to.ss_family = AF_UNSPEC;
1419 if (!str2ip2(ip, &conn->addr.to, 0))
1420 WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip));
1421
1422 /* Set port. */
1423 if (conn->addr.to.ss_family == AF_INET)
1424 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
1425 else if (conn->addr.to.ss_family == AF_INET6)
1426 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
1427
1428 /* it is important not to call the wakeup function directly but to
1429 * pass through task_wakeup(), because this one knows how to apply
1430 * priorities to tasks.
1431 */
1432 task_wakeup(socket->s->task, TASK_WOKEN_INIT);
1433
1434 WILL_LJMP(lua_yieldk(L, 0, 0, hlua_socket_connect_yield));
1435
1436 return 0;
1437}
1438
1439__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
1440{
1441 struct hlua_socket *socket;
1442
1443 MAY_LJMP(check_args(L, 3, "connect_ssl"));
1444 socket = MAY_LJMP(hlua_checksocket(L, 1));
1445 socket->s->target = &socket_ssl.obj_type;
1446 return MAY_LJMP(hlua_socket_connect(L));
1447}
1448
1449__LJMP static int hlua_socket_setoption(struct lua_State *L)
1450{
1451 return 0;
1452}
1453
1454__LJMP static int hlua_socket_settimeout(struct lua_State *L)
1455{
1456 MAY_LJMP(check_args(L, 2, "settimeout"));
1457
1458 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1459 unsigned int tmout = MAY_LJMP(luaL_checkunsigned(L, 2)) * 1000;
1460
1461 socket->s->req->rto = tmout;
1462 socket->s->req->wto = tmout;
1463 socket->s->rep->rto = tmout;
1464 socket->s->rep->wto = tmout;
1465
1466 return 0;
1467}
1468
1469__LJMP static int hlua_socket_new(lua_State *L)
1470{
1471 struct hlua_socket *socket;
1472 struct appctx *appctx;
1473
1474 /* Check stack size. */
1475 if (!lua_checkstack(L, 2)) {
1476 hlua_pusherror(L, "socket: full stack");
1477 goto out_fail_conf;
1478 }
1479
1480 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
1481 memset(socket, 0, sizeof(*socket));
1482
1483 /* Pop a class session metatable and affect it to the userdata. */
1484 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
1485 lua_setmetatable(L, -2);
1486
1487 /*
1488 *
1489 * Get memory for the request.
1490 *
1491 */
1492
1493 socket->s = pool_alloc2(pool2_session);
1494 if (!socket->s) {
1495 hlua_pusherror(L, "socket: out of memory");
1496 goto out_fail_conf;
1497 }
1498
1499 socket->s->task = task_new();
1500 if (!socket->s->task) {
1501 hlua_pusherror(L, "socket: out of memory");
1502 goto out_free_session;
1503 }
1504
1505 socket->s->req = pool_alloc2(pool2_channel);
1506 if (!socket->s->req) {
1507 hlua_pusherror(L, "socket: out of memory");
1508 goto out_fail_req;
1509 }
1510
1511 socket->s->req->buf = pool_alloc2(pool2_buffer);
1512 if (!socket->s->req->buf) {
1513 hlua_pusherror(L, "socket: out of memory");
1514 goto out_fail_req_buf;
1515 }
1516
1517 socket->s->rep = pool_alloc2(pool2_channel);
1518 if (!socket->s->rep) {
1519 hlua_pusherror(L, "socket: out of memory");
1520 goto out_fail_rep;
1521 }
1522
1523 socket->s->rep->buf = pool_alloc2(pool2_buffer);
1524 if (!socket->s->rep->buf) {
1525 hlua_pusherror(L, "socket: out of memory");
1526 goto out_fail_rep_buf;
1527 }
1528
1529 /* Configura empty Lua for the session. */
1530 socket->s->hlua.T = NULL;
1531 socket->s->hlua.Tref = LUA_REFNIL;
1532 socket->s->hlua.Mref = LUA_REFNIL;
1533 socket->s->hlua.nargs = 0;
1534 socket->s->hlua.state = HLUA_STOP;
1535 LIST_INIT(&socket->s->hlua.com);
1536
1537 /* session initialisation. */
1538 session_init_srv_conn(socket->s);
1539
1540 /*
1541 *
1542 * Configure the associated task.
1543 *
1544 */
1545
1546 /* This is the dedicated function to process the session. This function
1547 * is able to establish the conection, process the timeouts, etc ...
1548 */
1549 socket->s->task->process = process_session;
1550
1551 /* Back reference to session. This is used by process_session(). */
1552 socket->s->task->context = socket->s;
1553
1554 /* The priority of the task is normal. */
1555 socket->s->task->nice = 0;
1556
1557 /* Init the next run to eternity. Later in this function, this task is
1558 * waked.
1559 */
1560 socket->s->task->expire = TICK_ETERNITY;
1561
1562 /*
1563 *
1564 * Initialize the attached buffers
1565 *
1566 */
1567 socket->s->req->buf->size = global.tune.bufsize;
1568 socket->s->rep->buf->size = global.tune.bufsize;
1569
1570 /*
1571 *
1572 * Initialize channels.
1573 *
1574 */
1575
1576 /* This function reset the struct. It must be called
1577 * before the configuration.
1578 */
1579 channel_init(socket->s->req);
1580 channel_init(socket->s->rep);
1581
1582 socket->s->req->prod = &socket->s->si[0];
1583 socket->s->req->cons = &socket->s->si[1];
1584
1585 socket->s->rep->prod = &socket->s->si[1];
1586 socket->s->rep->cons = &socket->s->si[0];
1587
1588 socket->s->si[0].ib = socket->s->req;
1589 socket->s->si[0].ob = socket->s->rep;
1590
1591 socket->s->si[1].ib = socket->s->rep;
1592 socket->s->si[1].ob = socket->s->req;
1593
1594 socket->s->req->analysers = 0;
1595 socket->s->req->rto = socket_proxy.timeout.client;
1596 socket->s->req->wto = socket_proxy.timeout.server;
1597 socket->s->req->rex = TICK_ETERNITY;
1598 socket->s->req->wex = TICK_ETERNITY;
1599 socket->s->req->analyse_exp = TICK_ETERNITY;
1600
1601 socket->s->rep->analysers = 0;
1602 socket->s->rep->rto = socket_proxy.timeout.server;
1603 socket->s->rep->wto = socket_proxy.timeout.client;
1604 socket->s->rep->rex = TICK_ETERNITY;
1605 socket->s->rep->wex = TICK_ETERNITY;
1606 socket->s->rep->analyse_exp = TICK_ETERNITY;
1607
1608 /*
1609 *
1610 * Configure the session.
1611 *
1612 */
1613
1614 /* The session dont have listener. The listener is used with real
1615 * proxies.
1616 */
1617 socket->s->listener = NULL;
1618
1619 /* The flags are initialized to 0. Values are setted later. */
1620 socket->s->flags = 0;
1621
1622 /* Assign the configured proxy to the new session. */
1623 socket->s->be = &socket_proxy;
1624 socket->s->fe = &socket_proxy;
1625
1626 /* XXX: Set namy variables */
1627 socket->s->store_count = 0;
1628 memset(socket->s->stkctr, 0, sizeof(socket->s->stkctr));
1629
1630 /* Configure logs. */
1631 socket->s->logs.logwait = 0;
1632 socket->s->logs.level = 0;
1633 socket->s->logs.accept_date = date; /* user-visible date for logging */
1634 socket->s->logs.tv_accept = now; /* corrected date for internal use */
1635 socket->s->do_log = NULL;
1636
1637 /* Function used if an error is occured. */
1638 socket->s->srv_error = default_srv_error;
1639
1640 /* Init the list of buffers. */
1641 LIST_INIT(&socket->s->buffer_wait);
1642
1643 /* Dont configure the unique ID. */
1644 socket->s->uniq_id = 0;
1645 socket->s->unique_id = NULL;
1646
1647 /* XXX: ? */
1648 socket->s->pend_pos = NULL;
1649
1650 /* XXX: See later. */
1651 socket->s->txn.sessid = NULL;
1652 socket->s->txn.srv_cookie = NULL;
1653 socket->s->txn.cli_cookie = NULL;
1654 socket->s->txn.uri = NULL;
1655 socket->s->txn.req.cap = NULL;
1656 socket->s->txn.rsp.cap = NULL;
1657 socket->s->txn.hdr_idx.v = NULL;
1658 socket->s->txn.hdr_idx.size = 0;
1659 socket->s->txn.hdr_idx.used = 0;
1660
1661 /* Configure "left" stream interface as applet. This "si" produce
1662 * and use the data received from the server. The applet is initialized
1663 * and is attached to the stream interface.
1664 */
1665
1666 /* The data producer is already connected. It is the applet. */
1667 socket->s->req->flags = CF_READ_ATTACHED;
1668
1669 channel_auto_connect(socket->s->req); /* don't wait to establish connection */
1670 channel_auto_close(socket->s->req); /* let the producer forward close requests */
1671
1672 si_reset(&socket->s->si[0], socket->s->task);
1673 si_set_state(&socket->s->si[0], SI_ST_EST); /* connection established (resource exists) */
1674
1675 appctx = stream_int_register_handler(&socket->s->si[0], &update_applet);
1676 if (!appctx)
1677 goto out_fail_conn1;
1678 appctx->ctx.hlua.socket = socket;
1679 appctx->ctx.hlua.connected = 0;
1680 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
1681 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
1682
1683 /* Configure "right" stream interface. this "si" is used to connect
1684 * and retrieve data from the server. The connection is initialized
1685 * with the "struct server".
1686 */
1687 si_reset(&socket->s->si[1], socket->s->task);
1688 si_set_state(&socket->s->si[1], SI_ST_INI);
1689 socket->s->si[1].conn_retries = socket_proxy.conn_retries;
1690
1691 /* Force destination server. */
1692 socket->s->flags |= SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET | SN_BE_ASSIGNED;
1693 socket->s->target = &socket_tcp.obj_type;
1694
1695 /* This session is added to te lists of alive sessions. */
1696 LIST_ADDQ(&sessions, &socket->s->list);
1697
1698 /* XXX: I think that this list is used by stats. */
1699 LIST_INIT(&socket->s->back_refs);
1700
1701 /* Update statistics counters. */
1702 socket_proxy.feconn++; /* beconn will be increased later */
1703 jobs++;
1704 totalconn++;
1705
1706 /* Return yield waiting for connection. */
1707 return 1;
1708
1709out_fail_conn1:
1710 pool_free2(pool2_buffer, socket->s->rep->buf);
1711out_fail_rep_buf:
1712 pool_free2(pool2_channel, socket->s->rep);
1713out_fail_rep:
1714 pool_free2(pool2_buffer, socket->s->req->buf);
1715out_fail_req_buf:
1716 pool_free2(pool2_channel, socket->s->req);
1717out_fail_req:
1718 task_free(socket->s->task);
1719out_free_session:
1720 pool_free2(pool2_session, socket->s);
1721out_fail_conf:
1722 WILL_LJMP(lua_error(L));
1723 return 0;
1724}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001725
1726/*
1727 *
1728 *
1729 * Class TXN
1730 *
1731 *
1732 */
1733
1734/* Returns a struct hlua_session if the stack entry "ud" is
1735 * a class session, otherwise it throws an error.
1736 */
1737__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
1738{
1739 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
1740}
1741
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01001742__LJMP static int hlua_setpriv(lua_State *L)
1743{
1744 MAY_LJMP(check_args(L, 2, "set_priv"));
1745
1746 /* It is useles to retrieve the session, but this function
1747 * runs only in a session context.
1748 */
1749 MAY_LJMP(hlua_checktxn(L, 1));
1750 struct hlua *hlua = hlua_gethlua(L);
1751
1752 /* Remove previous value. */
1753 if (hlua->Mref != -1)
1754 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
1755
1756 /* Get and store new value. */
1757 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
1758 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
1759
1760 return 0;
1761}
1762
1763__LJMP static int hlua_getpriv(lua_State *L)
1764{
1765 MAY_LJMP(check_args(L, 1, "get_priv"));
1766
1767 /* It is useles to retrieve the session, but this function
1768 * runs only in a session context.
1769 */
1770 MAY_LJMP(hlua_checktxn(L, 1));
1771 struct hlua *hlua = hlua_gethlua(L);
1772
1773 /* Push configuration index in the stack. */
1774 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
1775
1776 return 1;
1777}
1778
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001779/* Create stack entry containing a class TXN. This function
1780 * return 0 if the stack does not contains free slots,
1781 * otherwise it returns 1.
1782 */
1783static int hlua_txn_new(lua_State *L, struct session *s, struct proxy *p, void *l7)
1784{
1785 struct hlua_txn *hs;
1786
1787 /* Check stack size. */
1788 if (!lua_checkstack(L, 2))
1789 return 0;
1790
1791 /* NOTE: The allocation never fails. The failure
1792 * throw an error, and the function never returns.
1793 * if the throw is not avalaible, the process is aborted.
1794 */
1795 hs = lua_newuserdata(L, sizeof(struct hlua_txn));
1796 hs->s = s;
1797 hs->p = p;
1798 hs->l7 = l7;
1799
1800 /* Pop a class sesison metatable and affect it to the userdata. */
1801 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
1802 lua_setmetatable(L, -2);
1803
1804 return 1;
1805}
1806
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01001807/* This function is an LUA binding. It is called with each sample-fetch.
1808 * It uses closure argument to store the associated sample-fetch. It
1809 * returns only one argument or throws an error. An error is throwed
1810 * only if an error is encoutered during the argument parsing. If
1811 * the "sample-fetch" function fails, nil is returned.
1812 */
1813__LJMP static int hlua_run_sample_fetch(lua_State *L)
1814{
1815 struct hlua_txn *s;
1816 struct hlua_sample_fetch *f;
1817 struct arg args[ARGM_NBARGS];
1818 int i;
1819 struct sample smp;
1820
1821 /* Get closure arguments. */
1822 f = (struct hlua_sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
1823
1824 /* Get traditionnal arguments. */
1825 s = MAY_LJMP(hlua_checktxn(L, 1));
1826
1827 /* Get extra arguments. */
1828 for (i = 0; i <= lua_gettop(L); i++) {
1829 if (i >= ARGM_NBARGS)
1830 break;
1831 hlua_lua2arg(L, i + 2, &args[i]);
1832 }
1833 args[i].type = ARGT_STOP;
1834
1835 /* Check arguments. */
1836 MAY_LJMP(hlua_lua2arg_check(L, 1, args, f->f->arg_mask));
1837
1838 /* Run the special args cehcker. */
1839 if (!f->f->val_args(args, NULL)) {
1840 lua_pushfstring(L, "error in arguments");
1841 WILL_LJMP(lua_error(L));
1842 }
1843
1844 /* Initialise the sample. */
1845 memset(&smp, 0, sizeof(smp));
1846
1847 /* Run the sample fetch process. */
1848 if (!f->f->process(s->p, s->s, s->l7, 0, args, &smp, f->f->kw, f->f->private)) {
1849 lua_pushnil(L);
1850 return 1;
1851 }
1852
1853 /* Convert the returned sample in lua value. */
1854 hlua_smp2lua(L, &smp);
1855 return 1;
1856}
1857
Thierry FOURNIER9a819e72015-02-16 20:22:55 +01001858/* This function is an LUA binding. It creates ans returns
1859 * an array of HTTP headers. This function does not fails.
1860 */
1861static int hlua_session_getheaders(lua_State *L)
1862{
1863 struct hlua_txn *s = MAY_LJMP(hlua_checktxn(L, 1));
1864 struct session *sess = s->s;
1865 const char *cur_ptr, *cur_next, *p;
1866 int old_idx, cur_idx;
1867 struct hdr_idx_elem *cur_hdr;
1868 const char *hn, *hv;
1869 int hnl, hvl;
1870
1871 /* Create the table. */
1872 lua_newtable(L);
1873
1874 /* Build array of headers. */
1875 old_idx = 0;
1876 cur_next = sess->req->buf->p + hdr_idx_first_pos(&sess->txn.hdr_idx);
1877
1878 while (1) {
1879 cur_idx = sess->txn.hdr_idx.v[old_idx].next;
1880 if (!cur_idx)
1881 break;
1882 old_idx = cur_idx;
1883
1884 cur_hdr = &sess->txn.hdr_idx.v[cur_idx];
1885 cur_ptr = cur_next;
1886 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
1887
1888 /* Now we have one full header at cur_ptr of len cur_hdr->len,
1889 * and the next header starts at cur_next. We'll check
1890 * this header in the list as well as against the default
1891 * rule.
1892 */
1893
1894 /* look for ': *'. */
1895 hn = cur_ptr;
1896 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
1897 if (p >= cur_ptr+cur_hdr->len)
1898 continue;
1899 hnl = p - hn;
1900 p++;
1901 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
1902 p++;
1903 if (p >= cur_ptr+cur_hdr->len)
1904 continue;
1905 hv = p;
1906 hvl = cur_ptr+cur_hdr->len-p;
1907
1908 /* Push values in the table. */
1909 lua_pushlstring(L, hn, hnl);
1910 lua_pushlstring(L, hv, hvl);
1911 lua_settable(L, -3);
1912 }
1913
1914 return 1;
1915}
1916
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01001917static struct task *hlua_sleep_process_task(struct task *task)
1918{
1919 struct hlua_sleep *t = task->context;
1920
1921 /* Check time and got to sleep a little bit more if the
1922 * expires is not come.
1923 */
1924 if (now_ms < t->wakeup_ms) {
1925 task_schedule(t->task, t->wakeup_ms);
1926 return NULL;
1927 }
1928
1929 /* Wake associated signals. */
1930 hlua_com_wake(&t->com);
1931
1932 /* Delete task. */
1933 task_delete(task); /* The task may remain in the wait queue. */
1934 task_free(task);
1935 pool_free2(pool2_hlua_sleep, t);
1936
1937 return NULL;
1938}
1939
1940__LJMP static int hlua_sleep_yield(lua_State *L)
1941{
1942 int wakeup_ms = lua_tointeger(L, -1);
1943 if (now_ms < wakeup_ms)
1944 WILL_LJMP(lua_yieldk(L, 0, 0, hlua_sleep_yield));
1945 return 0;
1946}
1947
1948__LJMP static inline int _hlua_sleep(lua_State *L, int delay)
1949{
1950 struct hlua_sleep *t;
1951 struct hlua *hlua = hlua_gethlua(L);
1952
1953 /* If hlua is not set, I'm in start mode. I can run
1954 * a blocking sleep.
1955 */
1956 if (!hlua || !hlua->task) {
1957 usleep(delay * 1000);
1958 return 0;
1959 }
1960
1961 /* Reserve memory. */
1962 t = pool_alloc2(pool2_hlua_sleep);
1963 if (!t)
1964 WILL_LJMP(luaL_error(L, "lua: out of memory"));
1965 t->task = task_new();
1966 if (!t->task)
1967 WILL_LJMP(luaL_error(L, "lua: out of memory"));
1968
1969 /* Init and schedule the sleep process. */
1970 t->task->process = hlua_sleep_process_task;
1971 t->task->context = t;
1972 t->wakeup_ms = tick_add(now_ms, delay);
1973 task_schedule(t->task, t->wakeup_ms);
1974
1975 /* Init the signal between the sleep task and the current lua task. */
1976 LIST_INIT(&t->com);
1977 if (!hlua_com_new(hlua, &t->com))
1978 WILL_LJMP(luaL_error(L, "out of memory error"));
1979
1980 /* Store the wakeup time in the lua stack. */
1981 lua_pushinteger(L, t->wakeup_ms);
1982
1983 WILL_LJMP(lua_yieldk(L, 0, 0, hlua_sleep_yield));
1984 return 0;
1985}
1986
1987__LJMP static int hlua_sleep(lua_State *L)
1988{
1989 unsigned int delay;
1990
1991 /* Check number of arguments. */
1992 if (lua_gettop(L) != 1)
1993 WILL_LJMP(luaL_error(L, "sleep: needs 1 argument"));
1994
1995 delay = MAY_LJMP(luaL_checkunsigned(L, 1)) * 1000;
1996
1997 return MAY_LJMP(_hlua_sleep(L, delay));
1998}
1999
2000static int hlua_msleep(lua_State *L)
2001{
2002 unsigned int delay;
2003
2004 /* Check number of arguments. */
2005 if (lua_gettop(L) != 1)
2006 WILL_LJMP(luaL_error(L, "sleep: needs 1 argument"));
2007
2008 delay = MAY_LJMP(luaL_checkunsigned(L, 1));
2009
2010 return MAY_LJMP(_hlua_sleep(L, delay));
2011}
2012
Thierry FOURNIER24f33532015-01-23 12:13:00 +01002013/* This function is used as a calback of a task. It is called by the
2014 * HAProxy task subsystem when the task is awaked. The LUA runtime can
2015 * return an E_AGAIN signal, the emmiter of this signal must set a
2016 * signal to wake the task.
2017 */
2018static struct task *hlua_process_task(struct task *task)
2019{
2020 struct hlua *hlua = task->context;
2021 enum hlua_exec status;
2022
2023 /* We need to remove the task from the wait queue before executing
2024 * the Lua code because we don't know if it needs to wait for
2025 * another timer or not in the case of E_AGAIN.
2026 */
2027 task_delete(task);
2028
2029 /* Execute the Lua code. */
2030 status = hlua_ctx_resume(hlua, 1);
2031
2032 switch (status) {
2033 /* finished or yield */
2034 case HLUA_E_OK:
2035 hlua_ctx_destroy(hlua);
2036 task_delete(task);
2037 task_free(task);
2038 break;
2039
2040 case HLUA_E_AGAIN: /* co process wake me later. */
2041 break;
2042
2043 /* finished with error. */
2044 case HLUA_E_ERRMSG:
2045 send_log(NULL, LOG_ERR, "Lua task: %s.", lua_tostring(hlua->T, -1));
2046 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2047 Alert("Lua task: %s.\n", lua_tostring(hlua->T, -1));
2048 hlua_ctx_destroy(hlua);
2049 task_delete(task);
2050 task_free(task);
2051 break;
2052
2053 case HLUA_E_ERR:
2054 default:
2055 send_log(NULL, LOG_ERR, "Lua task: unknown error.");
2056 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2057 Alert("Lua task: unknown error.\n");
2058 hlua_ctx_destroy(hlua);
2059 task_delete(task);
2060 task_free(task);
2061 break;
2062 }
2063 return NULL;
2064}
2065
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002066/* This function is an LUA binding that register LUA function to be
2067 * executed after the HAProxy configuration parsing and before the
2068 * HAProxy scheduler starts. This function expect only one LUA
2069 * argument that is a function. This function returns nothing, but
2070 * throws if an error is encountered.
2071 */
2072__LJMP static int hlua_register_init(lua_State *L)
2073{
2074 struct hlua_init_function *init;
2075 int ref;
2076
2077 MAY_LJMP(check_args(L, 1, "register_init"));
2078
2079 ref = MAY_LJMP(hlua_checkfunction(L, 1));
2080
2081 init = malloc(sizeof(*init));
2082 if (!init)
2083 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2084
2085 init->function_ref = ref;
2086 LIST_ADDQ(&hlua_init_functions, &init->l);
2087 return 0;
2088}
2089
Thierry FOURNIER24f33532015-01-23 12:13:00 +01002090/* This functio is an LUA binding. It permits to register a task
2091 * executed in parallel of the main HAroxy activity. The task is
2092 * created and it is set in the HAProxy scheduler. It can be called
2093 * from the "init" section, "post init" or during the runtime.
2094 *
2095 * Lua prototype:
2096 *
2097 * <none> core.register_task(<function>)
2098 */
2099static int hlua_register_task(lua_State *L)
2100{
2101 struct hlua *hlua;
2102 struct task *task;
2103 int ref;
2104
2105 MAY_LJMP(check_args(L, 1, "register_task"));
2106
2107 ref = MAY_LJMP(hlua_checkfunction(L, 1));
2108
2109 hlua = malloc(sizeof(*hlua));
2110 if (!hlua)
2111 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2112
2113 task = task_new();
2114 task->context = hlua;
2115 task->process = hlua_process_task;
2116
2117 if (!hlua_ctx_init(hlua, task))
2118 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2119
2120 /* Restore the function in the stack. */
2121 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
2122 hlua->nargs = 0;
2123
2124 /* Schedule task. */
2125 task_schedule(task, now_ms);
2126
2127 return 0;
2128}
2129
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01002130/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
2131 * doesn't allow "yield" functions because the HAProxy engine cannot
2132 * resume converters.
2133 */
2134static int hlua_sample_conv_wrapper(struct session *session, const struct arg *arg_p,
2135 struct sample *smp, void *private)
2136{
2137 struct hlua_function *fcn = (struct hlua_function *)private;
2138
2139 /* If it is the first run, initialize the data for the call. */
2140 if (session->hlua.state == HLUA_STOP) {
2141 /* Check stack available size. */
2142 if (!lua_checkstack(session->hlua.T, 1)) {
2143 send_log(session->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
2144 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2145 Alert("Lua converter '%s': full stack.\n", fcn->name);
2146 return 0;
2147 }
2148
2149 /* Restore the function in the stack. */
2150 lua_rawgeti(session->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
2151
2152 /* convert input sample and pust-it in the stack. */
2153 if (!lua_checkstack(session->hlua.T, 1)) {
2154 send_log(session->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
2155 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2156 Alert("Lua converter '%s': full stack.\n", fcn->name);
2157 return 0;
2158 }
2159 hlua_smp2lua(session->hlua.T, smp);
2160 session->hlua.nargs = 2;
2161
2162 /* push keywords in the stack. */
2163 if (arg_p) {
2164 for (; arg_p->type != ARGT_STOP; arg_p++) {
2165 if (!lua_checkstack(session->hlua.T, 1)) {
2166 send_log(session->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
2167 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2168 Alert("Lua converter '%s': full stack.\n", fcn->name);
2169 return 0;
2170 }
2171 hlua_arg2lua(session->hlua.T, arg_p);
2172 session->hlua.nargs++;
2173 }
2174 }
2175
2176 /* Set the currently running flag. */
2177 session->hlua.state = HLUA_RUN;
2178 }
2179
2180 /* Execute the function. */
2181 switch (hlua_ctx_resume(&session->hlua, 0)) {
2182 /* finished. */
2183 case HLUA_E_OK:
2184 /* Convert the returned value in sample. */
2185 hlua_lua2smp(session->hlua.T, -1, smp);
2186 lua_pop(session->hlua.T, 1);
2187 return 1;
2188
2189 /* yield. */
2190 case HLUA_E_AGAIN:
2191 send_log(session->be, LOG_ERR, "Lua converter '%s': cannot use yielded functions.", fcn->name);
2192 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2193 Alert("Lua converter '%s': cannot use yielded functions.\n", fcn->name);
2194 return 0;
2195
2196 /* finished with error. */
2197 case HLUA_E_ERRMSG:
2198 /* Display log. */
2199 send_log(session->be, LOG_ERR, "Lua converter '%s': %s.", fcn->name, lua_tostring(session->hlua.T, -1));
2200 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2201 Alert("Lua converter '%s': %s.\n", fcn->name, lua_tostring(session->hlua.T, -1));
2202 lua_pop(session->hlua.T, 1);
2203 return 0;
2204
2205 case HLUA_E_ERR:
2206 /* Display log. */
2207 send_log(session->be, LOG_ERR, "Lua converter '%s' returns an unknown error.", fcn->name);
2208 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2209 Alert("Lua converter '%s' returns an unknown error.\n", fcn->name);
2210
2211 default:
2212 return 0;
2213 }
2214}
2215
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01002216/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
2217 * doesn't allow "yield" functions because the HAProxy engine cannot
2218 * resume sample-fetches.
2219 */
2220static int hlua_sample_fetch_wrapper(struct proxy *px, struct session *s, void *l7,
2221 unsigned int opt, const struct arg *arg_p,
2222 struct sample *smp, const char *kw, void *private)
2223{
2224 struct hlua_function *fcn = (struct hlua_function *)private;
2225
2226 /* If it is the first run, initialize the data for the call. */
2227 if (s->hlua.state == HLUA_STOP) {
2228 /* Check stack available size. */
2229 if (!lua_checkstack(s->hlua.T, 2)) {
2230 send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
2231 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2232 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
2233 return 0;
2234 }
2235
2236 /* Restore the function in the stack. */
2237 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
2238
2239 /* push arguments in the stack. */
2240 if (!hlua_txn_new(s->hlua.T, s, px, l7)) {
2241 send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
2242 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2243 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
2244 return 0;
2245 }
2246 s->hlua.nargs = 1;
2247
2248 /* push keywords in the stack. */
2249 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
2250 /* Check stack available size. */
2251 if (!lua_checkstack(s->hlua.T, 1)) {
2252 send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
2253 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2254 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
2255 return 0;
2256 }
2257 if (!lua_checkstack(s->hlua.T, 1)) {
2258 send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
2259 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2260 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
2261 return 0;
2262 }
2263 hlua_arg2lua(s->hlua.T, arg_p);
2264 s->hlua.nargs++;
2265 }
2266
2267 /* Set the currently running flag. */
2268 s->hlua.state = HLUA_RUN;
2269 }
2270
2271 /* Execute the function. */
2272 switch (hlua_ctx_resume(&s->hlua, 0)) {
2273 /* finished. */
2274 case HLUA_E_OK:
2275 /* Convert the returned value in sample. */
2276 hlua_lua2smp(s->hlua.T, -1, smp);
2277 lua_pop(s->hlua.T, 1);
2278
2279 /* Set the end of execution flag. */
2280 smp->flags &= ~SMP_F_MAY_CHANGE;
2281 return 1;
2282
2283 /* yield. */
2284 case HLUA_E_AGAIN:
2285 send_log(px, LOG_ERR, "Lua sample-fetch '%s': cannot use yielded functions.", fcn->name);
2286 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2287 Alert("Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
2288 return 0;
2289
2290 /* finished with error. */
2291 case HLUA_E_ERRMSG:
2292 /* Display log. */
2293 send_log(px, LOG_ERR, "Lua sample-fetch '%s': %s.", fcn->name, lua_tostring(s->hlua.T, -1));
2294 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2295 Alert("Lua sample-fetch '%s': %s.\n", fcn->name, lua_tostring(s->hlua.T, -1));
2296 lua_pop(s->hlua.T, 1);
2297 return 0;
2298
2299 case HLUA_E_ERR:
2300 /* Display log. */
2301 send_log(px, LOG_ERR, "Lua sample-fetch '%s' returns an unknown error.", fcn->name);
2302 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2303 Alert("Lua sample-fetch '%s': returns an unknown error.\n", fcn->name);
2304
2305 default:
2306 return 0;
2307 }
2308}
2309
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01002310/* This function is an LUA binding used for registering
2311 * "sample-conv" functions. It expects a converter name used
2312 * in the haproxy configuration file, and an LUA function.
2313 */
2314__LJMP static int hlua_register_converters(lua_State *L)
2315{
2316 struct sample_conv_kw_list *sck;
2317 const char *name;
2318 int ref;
2319 int len;
2320 struct hlua_function *fcn;
2321
2322 MAY_LJMP(check_args(L, 2, "register_converters"));
2323
2324 /* First argument : converter name. */
2325 name = MAY_LJMP(luaL_checkstring(L, 1));
2326
2327 /* Second argument : lua function. */
2328 ref = MAY_LJMP(hlua_checkfunction(L, 2));
2329
2330 /* Allocate and fill the sample fetch keyword struct. */
2331 sck = malloc(sizeof(struct sample_conv_kw_list) +
2332 sizeof(struct sample_conv) * 2);
2333 if (!sck)
2334 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2335 fcn = malloc(sizeof(*fcn));
2336 if (!fcn)
2337 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2338
2339 /* Fill fcn. */
2340 fcn->name = strdup(name);
2341 if (!fcn->name)
2342 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2343 fcn->function_ref = ref;
2344
2345 /* List head */
2346 sck->list.n = sck->list.p = NULL;
2347
2348 /* converter keyword. */
2349 len = strlen("lua.") + strlen(name) + 1;
2350 sck->kw[0].kw = malloc(len);
2351 if (!sck->kw[0].kw)
2352 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2353
2354 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
2355 sck->kw[0].process = hlua_sample_conv_wrapper;
2356 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
2357 sck->kw[0].val_args = NULL;
2358 sck->kw[0].in_type = SMP_T_STR;
2359 sck->kw[0].out_type = SMP_T_STR;
2360 sck->kw[0].private = fcn;
2361
2362 /* End of array. */
2363 memset(&sck->kw[1], 0, sizeof(struct sample_conv));
2364
2365 /* Register this new converter */
2366 sample_register_convs(sck);
2367
2368 return 0;
2369}
2370
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01002371/* This fucntion is an LUA binding used for registering
2372 * "sample-fetch" functions. It expects a converter name used
2373 * in the haproxy configuration file, and an LUA function.
2374 */
2375__LJMP static int hlua_register_fetches(lua_State *L)
2376{
2377 const char *name;
2378 int ref;
2379 int len;
2380 struct sample_fetch_kw_list *sfk;
2381 struct hlua_function *fcn;
2382
2383 MAY_LJMP(check_args(L, 2, "register_fetches"));
2384
2385 /* First argument : sample-fetch name. */
2386 name = MAY_LJMP(luaL_checkstring(L, 1));
2387
2388 /* Second argument : lua function. */
2389 ref = MAY_LJMP(hlua_checkfunction(L, 2));
2390
2391 /* Allocate and fill the sample fetch keyword struct. */
2392 sfk = malloc(sizeof(struct sample_fetch_kw_list) +
2393 sizeof(struct sample_fetch) * 2);
2394 if (!sfk)
2395 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2396 fcn = malloc(sizeof(*fcn));
2397 if (!fcn)
2398 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2399
2400 /* Fill fcn. */
2401 fcn->name = strdup(name);
2402 if (!fcn->name)
2403 WILL_LJMP(luaL_error(L, "lua out of memory error."));
2404 fcn->function_ref = ref;
2405
2406 /* List head */
2407 sfk->list.n = sfk->list.p = NULL;
2408
2409 /* sample-fetch keyword. */
2410 len = strlen("lua.") + strlen(name) + 1;
2411 sfk->kw[0].kw = malloc(len);
2412 if (!sfk->kw[0].kw)
2413 return luaL_error(L, "lua out of memory error.");
2414
2415 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
2416 sfk->kw[0].process = hlua_sample_fetch_wrapper;
2417 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
2418 sfk->kw[0].val_args = NULL;
2419 sfk->kw[0].out_type = SMP_T_STR;
2420 sfk->kw[0].use = SMP_USE_HTTP_ANY;
2421 sfk->kw[0].val = 0;
2422 sfk->kw[0].private = fcn;
2423
2424 /* End of array. */
2425 memset(&sfk->kw[1], 0, sizeof(struct sample_fetch));
2426
2427 /* Register this new fetch. */
2428 sample_register_fetches(sfk);
2429
2430 return 0;
2431}
2432
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01002433/* global {tcp|http}-request parser. Return 1 in succes case, else return 0. */
2434static int hlua_parse_rule(const char **args, int *cur_arg, struct proxy *px,
2435 struct hlua_rule **rule_p, char **err)
2436{
2437 struct hlua_rule *rule;
2438
2439 /* Memory for the rule. */
2440 rule = malloc(sizeof(*rule));
2441 if (!rule) {
2442 memprintf(err, "out of memory error");
2443 return 0;
2444 }
2445 *rule_p = rule;
2446
2447 /* The requiered arg is a function name. */
2448 if (!args[*cur_arg]) {
2449 memprintf(err, "expect Lua function name");
2450 return 0;
2451 }
2452
2453 /* Lookup for the symbol, and check if it is a function. */
2454 lua_getglobal(gL.T, args[*cur_arg]);
2455 if (lua_isnil(gL.T, -1)) {
2456 lua_pop(gL.T, 1);
2457 memprintf(err, "Lua function '%s' not found", args[*cur_arg]);
2458 return 0;
2459 }
2460 if (!lua_isfunction(gL.T, -1)) {
2461 lua_pop(gL.T, 1);
2462 memprintf(err, "'%s' is not a function", args[*cur_arg]);
2463 return 0;
2464 }
2465
2466 /* Reference the Lua function and store the reference. */
2467 rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
2468 rule->fcn.name = strdup(args[*cur_arg]);
2469 if (!rule->fcn.name) {
2470 memprintf(err, "out of memory error.");
2471 return 0;
2472 }
2473 (*cur_arg)++;
2474
2475 /* TODO: later accept arguments. */
2476 rule->args = NULL;
2477
2478 return 1;
2479}
2480
2481/* This function is a wrapper to execute each LUA function declared
2482 * as an action wrapper during the initialisation period. This function
2483 * return 1 if the processing is finished (with oe without error) and
2484 * return 0 if the function must be called again because the LUA
2485 * returns a yield.
2486 */
2487static int hlua_request_act_wrapper(struct hlua_rule *rule, struct proxy *px,
2488 struct session *s, struct http_txn *http_txn,
2489 unsigned int analyzer)
2490{
2491 char **arg;
2492
2493 /* If it is the first run, initialize the data for the call. */
2494 if (s->hlua.state == HLUA_STOP) {
2495 /* Check stack available size. */
2496 if (!lua_checkstack(s->hlua.T, 1)) {
2497 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
2498 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2499 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
2500 return 0;
2501 }
2502
2503 /* Restore the function in the stack. */
2504 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->fcn.function_ref);
2505
2506 /* Create and and push object session in the stack. */
2507 if (!hlua_txn_new(s->hlua.T, s, px, http_txn)) {
2508 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
2509 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2510 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
2511 return 0;
2512 }
2513 s->hlua.nargs = 1;
2514
2515 /* push keywords in the stack. */
2516 for (arg = rule->args; arg && *arg; arg++) {
2517 if (!lua_checkstack(s->hlua.T, 1)) {
2518 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
2519 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2520 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
2521 return 0;
2522 }
2523 lua_pushstring(s->hlua.T, *arg);
2524 s->hlua.nargs++;
2525 }
2526
2527 /* Set the currently running flag. */
2528 s->hlua.state = HLUA_RUN;
2529 }
2530
2531 /* Execute the function. */
2532 switch (hlua_ctx_resume(&s->hlua, 1)) {
2533 /* finished. */
2534 case HLUA_E_OK:
2535 return 1;
2536
2537 /* yield. */
2538 case HLUA_E_AGAIN:
2539 /* Some actions can be wake up when a "write" event
2540 * is detected on a response channel. This is useful
2541 * only for actions targetted on the requests.
2542 */
2543 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) {
2544 s->rep->flags |= CF_WAKE_WRITE;
2545 s->rep->analysers |= analyzer;
2546 }
2547 return 0;
2548
2549 /* finished with error. */
2550 case HLUA_E_ERRMSG:
2551 /* Display log. */
2552 send_log(px, LOG_ERR, "Lua function '%s': %s.", rule->fcn.name, lua_tostring(s->hlua.T, -1));
2553 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2554 Alert("Lua function '%s': %s.\n", rule->fcn.name, lua_tostring(s->hlua.T, -1));
2555 lua_pop(s->hlua.T, 1);
2556 return 1;
2557
2558 case HLUA_E_ERR:
2559 /* Display log. */
2560 send_log(px, LOG_ERR, "Lua function '%s' return an unknown error.", rule->fcn.name);
2561 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2562 Alert("Lua function '%s' return an unknown error.\n", rule->fcn.name);
2563
2564 default:
2565 return 1;
2566 }
2567}
2568
2569/* Lua execution wrapper for "tcp-request". This function uses
2570 * "hlua_request_act_wrapper" for executing the LUA code.
2571 */
2572int hlua_tcp_req_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
2573 struct session *s)
2574{
2575 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data,
2576 px, s, NULL, AN_REQ_INSPECT_FE);
2577}
2578
2579/* Lua execution wrapper for "tcp-response". This function uses
2580 * "hlua_request_act_wrapper" for executing the LUA code.
2581 */
2582int hlua_tcp_res_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
2583 struct session *s)
2584{
2585 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data,
2586 px, s, NULL, AN_RES_INSPECT);
2587}
2588
2589/* Lua execution wrapper for http-request.
2590 * This function uses "hlua_request_act_wrapper" for executing
2591 * the LUA code.
2592 */
2593int hlua_http_req_act_wrapper(struct http_req_rule *rule, struct proxy *px,
2594 struct session *s, struct http_txn *http_txn)
2595{
2596 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
2597 s, http_txn, AN_REQ_HTTP_PROCESS_FE);
2598}
2599
2600/* Lua execution wrapper for http-response.
2601 * This function uses "hlua_request_act_wrapper" for executing
2602 * the LUA code.
2603 */
2604int hlua_http_res_act_wrapper(struct http_res_rule *rule, struct proxy *px,
2605 struct session *s, struct http_txn *http_txn)
2606{
2607 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
2608 s, http_txn, AN_RES_HTTP_PROCESS_BE);
2609}
2610
2611/* tcp-request <*> configuration wrapper. */
2612static int tcp_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
2613 struct tcp_rule *rule, char **err)
2614{
2615 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data, err))
2616 return -1;
2617 rule->action = TCP_ACT_CUSTOM;
2618 rule->action_ptr = hlua_tcp_req_act_wrapper;
2619 return 1;
2620}
2621
2622/* tcp-response <*> configuration wrapper. */
2623static int tcp_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
2624 struct tcp_rule *rule, char **err)
2625{
2626 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data, err))
2627 return -1;
2628 rule->action = TCP_ACT_CUSTOM;
2629 rule->action_ptr = hlua_tcp_res_act_wrapper;
2630 return 1;
2631}
2632
2633/* http-request <*> configuration wrapper. */
2634static int http_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
2635 struct http_req_rule *rule, char **err)
2636{
2637 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
2638 return -1;
2639 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
2640 rule->action_ptr = hlua_http_req_act_wrapper;
2641 return 1;
2642}
2643
2644/* http-response <*> configuration wrapper. */
2645static int http_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
2646 struct http_res_rule *rule, char **err)
2647{
2648 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
2649 return -1;
2650 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
2651 rule->action_ptr = hlua_http_res_act_wrapper;
2652 return 1;
2653}
2654
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01002655/* This function is called by the main configuration key "lua-load". It loads and
2656 * execute an lua file during the parsing of the HAProxy configuration file. It is
2657 * the main lua entry point.
2658 *
2659 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
2660 * occured, otherwise it returns 0.
2661 *
2662 * In some error case, LUA set an error message in top of the stack. This function
2663 * returns this error message in the HAProxy logs and pop it from the stack.
2664 */
2665static int hlua_load(char **args, int section_type, struct proxy *curpx,
2666 struct proxy *defpx, const char *file, int line,
2667 char **err)
2668{
2669 int error;
2670
2671 /* Just load and compile the file. */
2672 error = luaL_loadfile(gL.T, args[1]);
2673 if (error) {
2674 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
2675 lua_pop(gL.T, 1);
2676 return -1;
2677 }
2678
2679 /* If no syntax error where detected, execute the code. */
2680 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
2681 switch (error) {
2682 case LUA_OK:
2683 break;
2684 case LUA_ERRRUN:
2685 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
2686 lua_pop(gL.T, 1);
2687 return -1;
2688 case LUA_ERRMEM:
2689 memprintf(err, "lua out of memory error\n");
2690 return -1;
2691 case LUA_ERRERR:
2692 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
2693 lua_pop(gL.T, 1);
2694 return -1;
2695 case LUA_ERRGCMM:
2696 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
2697 lua_pop(gL.T, 1);
2698 return -1;
2699 default:
2700 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
2701 lua_pop(gL.T, 1);
2702 return -1;
2703 }
2704
2705 return 0;
2706}
2707
2708/* configuration keywords declaration */
2709static struct cfg_kw_list cfg_kws = {{ },{
2710 { CFG_GLOBAL, "lua-load", hlua_load },
2711 { 0, NULL, NULL },
2712}};
2713
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01002714static struct http_req_action_kw_list http_req_kws = {"lua", { }, {
2715 { "lua", http_req_action_register_lua },
2716 { NULL, NULL }
2717}};
2718
2719static struct http_res_action_kw_list http_res_kws = {"lua", { }, {
2720 { "lua", http_res_action_register_lua },
2721 { NULL, NULL }
2722}};
2723
2724static struct tcp_action_kw_list tcp_req_cont_kws = {"lua", { }, {
2725 { "lua", tcp_req_action_register_lua },
2726 { NULL, NULL }
2727}};
2728
2729static struct tcp_action_kw_list tcp_res_cont_kws = {"lua", { }, {
2730 { "lua", tcp_res_action_register_lua },
2731 { NULL, NULL }
2732}};
2733
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002734int hlua_post_init()
2735{
2736 struct hlua_init_function *init;
2737 const char *msg;
2738 enum hlua_exec ret;
2739
2740 list_for_each_entry(init, &hlua_init_functions, l) {
2741 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
2742 ret = hlua_ctx_resume(&gL, 0);
2743 switch (ret) {
2744 case HLUA_E_OK:
2745 lua_pop(gL.T, -1);
2746 return 1;
2747 case HLUA_E_AGAIN:
2748 Alert("lua init: yield not allowed.\n");
2749 return 0;
2750 case HLUA_E_ERRMSG:
2751 msg = lua_tostring(gL.T, -1);
2752 Alert("lua init: %s.\n", msg);
2753 return 0;
2754 case HLUA_E_ERR:
2755 default:
2756 Alert("lua init: unknown runtime error.\n");
2757 return 0;
2758 }
2759 }
2760 return 1;
2761}
2762
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01002763void hlua_init(void)
2764{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01002765 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01002766 int idx;
2767 struct sample_fetch *sf;
2768 struct hlua_sample_fetch *hsf;
2769 char *p;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01002770
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01002771 /* Initialise com signals pool session. */
2772 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
2773
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01002774 /* Initialise sleep pool. */
2775 pool2_hlua_sleep = create_pool("hlua_sleep", sizeof(struct hlua_sleep), MEM_F_SHARED);
2776
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01002777 /* Register configuration keywords. */
2778 cfg_register_keywords(&cfg_kws);
2779
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01002780 /* Register custom HTTP rules. */
2781 http_req_keywords_register(&http_req_kws);
2782 http_res_keywords_register(&http_res_kws);
2783 tcp_req_cont_keywords_register(&tcp_req_cont_kws);
2784 tcp_res_cont_keywords_register(&tcp_res_cont_kws);
2785
Thierry FOURNIER380d0932015-01-23 14:27:52 +01002786 /* Init main lua stack. */
2787 gL.Mref = LUA_REFNIL;
2788 gL.state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01002789 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01002790 gL.T = luaL_newstate();
2791 hlua_sethlua(&gL);
2792 gL.Tref = LUA_REFNIL;
2793 gL.task = NULL;
2794
2795 /* Initialise lua. */
2796 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01002797
2798 /*
2799 *
2800 * Create "core" object.
2801 *
2802 */
2803
2804 /* This integer entry is just used as base value for the object "core". */
2805 lua_pushinteger(gL.T, 0);
2806
2807 /* Create and fill the metatable. */
2808 lua_newtable(gL.T);
2809
2810 /* Create and fill the __index entry. */
2811 lua_pushstring(gL.T, "__index");
2812 lua_newtable(gL.T);
2813
2814 /* Push the loglevel constants. */
2815 for (i=0; i<NB_LOG_LEVELS; i++)
2816 hlua_class_const_int(gL.T, log_levels[i], i);
2817
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002818 /* Register special functions. */
2819 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01002820 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01002821 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01002822 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01002823 hlua_class_function(gL.T, "sleep", hlua_sleep);
2824 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002825 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01002826
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01002827 /* Store the table __index in the metable. */
2828 lua_settable(gL.T, -3);
2829
2830 /* Register previous table in the registry with named entry. */
2831 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
2832 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CORE); /* register class session. */
2833
2834 /* Register previous table in the registry with reference. */
2835 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
2836 class_core_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
2837
2838 /* Create new object with class Core. */
2839 lua_setmetatable(gL.T, -2);
2840 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002841
2842 /*
2843 *
2844 * Register class TXN
2845 *
2846 */
2847
2848 /* Create and fill the metatable. */
2849 lua_newtable(gL.T);
2850
2851 /* Create and fille the __index entry. */
2852 lua_pushstring(gL.T, "__index");
2853 lua_newtable(gL.T);
2854
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01002855 /* Browse existing fetches and create the associated
2856 * object method.
2857 */
2858 sf = NULL;
2859 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
2860
2861 /* Dont register the keywork if the arguments check function are
2862 * not safe during the runtime.
2863 */
2864 if ((sf->val_args != NULL) &&
2865 (sf->val_args != val_payload_lv) &&
2866 (sf->val_args != val_hdr))
2867 continue;
2868
2869 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
2870 * by an underscore.
2871 */
2872 strncpy(trash.str, sf->kw, trash.size);
2873 trash.str[trash.size - 1] = '\0';
2874 for (p = trash.str; *p; p++)
2875 if (*p == '.' || *p == '-' || *p == '+')
2876 *p = '_';
2877
2878 /* Register the function. */
2879 lua_pushstring(gL.T, trash.str);
2880 hsf = lua_newuserdata(gL.T, sizeof(struct hlua_sample_fetch));
2881 hsf->f = sf;
2882 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
2883 lua_settable(gL.T, -3);
2884 }
2885
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01002886 /* Register Lua functions. */
Thierry FOURNIER9a819e72015-02-16 20:22:55 +01002887 hlua_class_function(gL.T, "get_headers", hlua_session_getheaders);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01002888 hlua_class_function(gL.T, "set_priv", hlua_setpriv);
2889 hlua_class_function(gL.T, "get_priv", hlua_getpriv);
2890
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002891 lua_settable(gL.T, -3);
2892
2893 /* Register previous table in the registry with reference and named entry. */
2894 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
2895 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
2896 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002897
2898 /*
2899 *
2900 * Register class Socket
2901 *
2902 */
2903
2904 /* Create and fill the metatable. */
2905 lua_newtable(gL.T);
2906
2907 /* Create and fille the __index entry. */
2908 lua_pushstring(gL.T, "__index");
2909 lua_newtable(gL.T);
2910
2911 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
2912 hlua_class_function(gL.T, "connect", hlua_socket_connect);
2913 hlua_class_function(gL.T, "send", hlua_socket_send);
2914 hlua_class_function(gL.T, "receive", hlua_socket_receive);
2915 hlua_class_function(gL.T, "close", hlua_socket_close);
2916 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
2917 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
2918 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
2919 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
2920
2921 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
2922
2923 /* Register the garbage collector entry. */
2924 lua_pushstring(gL.T, "__gc");
2925 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
2926 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
2927
2928 /* Register previous table in the registry with reference and named entry. */
2929 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
2930 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
2931 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
2932 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
2933
2934 /* Proxy and server configuration initialisation. */
2935 memset(&socket_proxy, 0, sizeof(socket_proxy));
2936 init_new_proxy(&socket_proxy);
2937 socket_proxy.parent = NULL;
2938 socket_proxy.last_change = now.tv_sec;
2939 socket_proxy.id = "LUA-SOCKET";
2940 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
2941 socket_proxy.maxconn = 0;
2942 socket_proxy.accept = NULL;
2943 socket_proxy.options2 |= PR_O2_INDEPSTR;
2944 socket_proxy.srv = NULL;
2945 socket_proxy.conn_retries = 0;
2946 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
2947
2948 /* Init TCP server: unchanged parameters */
2949 memset(&socket_tcp, 0, sizeof(socket_tcp));
2950 socket_tcp.next = NULL;
2951 socket_tcp.proxy = &socket_proxy;
2952 socket_tcp.obj_type = OBJ_TYPE_SERVER;
2953 LIST_INIT(&socket_tcp.actconns);
2954 LIST_INIT(&socket_tcp.pendconns);
2955 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
2956 socket_tcp.last_change = 0;
2957 socket_tcp.id = "LUA-TCP-CONN";
2958 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
2959 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
2960 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
2961
2962 /* XXX: Copy default parameter from default server,
2963 * but the default server is not initialized.
2964 */
2965 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
2966 socket_tcp.minconn = socket_proxy.defsrv.minconn;
2967 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
2968 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
2969 socket_tcp.onerror = socket_proxy.defsrv.onerror;
2970 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
2971 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
2972 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
2973 socket_tcp.uweight = socket_proxy.defsrv.iweight;
2974 socket_tcp.iweight = socket_proxy.defsrv.iweight;
2975
2976 socket_tcp.check.status = HCHK_STATUS_INI;
2977 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
2978 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
2979 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
2980 socket_tcp.check.server = &socket_tcp;
2981
2982 socket_tcp.agent.status = HCHK_STATUS_INI;
2983 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
2984 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
2985 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
2986 socket_tcp.agent.server = &socket_tcp;
2987
2988 socket_tcp.xprt = &raw_sock;
2989
2990#ifdef USE_OPENSSL
2991
2992 char *args[4];
2993 struct srv_kw *kw;
2994 int tmp_error;
2995 char *error;
2996
2997 /* Init TCP server: unchanged parameters */
2998 memset(&socket_ssl, 0, sizeof(socket_ssl));
2999 socket_ssl.next = NULL;
3000 socket_ssl.proxy = &socket_proxy;
3001 socket_ssl.obj_type = OBJ_TYPE_SERVER;
3002 LIST_INIT(&socket_ssl.actconns);
3003 LIST_INIT(&socket_ssl.pendconns);
3004 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
3005 socket_ssl.last_change = 0;
3006 socket_ssl.id = "LUA-SSL-CONN";
3007 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
3008 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
3009 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
3010
3011 /* XXX: Copy default parameter from default server,
3012 * but the default server is not initialized.
3013 */
3014 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
3015 socket_ssl.minconn = socket_proxy.defsrv.minconn;
3016 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
3017 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
3018 socket_ssl.onerror = socket_proxy.defsrv.onerror;
3019 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
3020 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
3021 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
3022 socket_ssl.uweight = socket_proxy.defsrv.iweight;
3023 socket_ssl.iweight = socket_proxy.defsrv.iweight;
3024
3025 socket_ssl.check.status = HCHK_STATUS_INI;
3026 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
3027 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
3028 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
3029 socket_ssl.check.server = &socket_ssl;
3030
3031 socket_ssl.agent.status = HCHK_STATUS_INI;
3032 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
3033 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
3034 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
3035 socket_ssl.agent.server = &socket_ssl;
3036
3037 socket_ssl.xprt = &raw_sock;
3038
3039 args[0] = "ssl";
3040 args[1] = "verify";
3041 args[2] = "none";
3042 args[3] = NULL;
3043
3044 for (idx=0; idx<3; idx++) {
3045 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
3046 /*
3047 *
3048 * If the keyword is not known, we can search in the registered
3049 * server keywords. This is usefull to configure special SSL
3050 * features like client certificates and ssl_verify.
3051 *
3052 */
3053 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
3054 if (tmp_error != 0) {
3055 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
3056 abort(); /* This must be never arrives because the command line
3057 not editable by the user. */
3058 }
3059 idx += kw->skip;
3060 }
3061 }
3062
3063 /* Initialize SSL server. */
3064 if (socket_ssl.xprt == &ssl_sock) {
3065 socket_ssl.use_ssl = 1;
3066 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
3067 }
3068#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01003069}