blob: bd2ea1df286f46bbaeeab278fb62b6f9808a4312 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua safe functions
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 *
12 * All the functions in this file runs with a Lua stack, and can
Thierry Fournierfb0b5462016-01-21 09:28:58 +010013 * return with a longjmp. All of these function must be launched
14 * in an environment able to catch a longjmp, otherwise a
15 * critical error can be raised.
16 */
Bertrand Jacquin892e4fd2021-01-21 21:14:07 +000017
18#define _GNU_SOURCE
19
Thierry Fournierfb0b5462016-01-21 09:28:58 +010020#include <lauxlib.h>
21#include <lua.h>
22#include <lualib.h>
23
Thierry Fournierb1f46562016-01-21 09:46:15 +010024#include <common/time.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010025#include <common/uri_auth.h>
Thierry Fournierb1f46562016-01-21 09:46:15 +010026
William Lallemand9ed62032016-11-21 17:49:11 +010027#include <types/cli.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010028#include <types/hlua.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010029#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010030#include <types/stats.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010031
Thierry Fournierf61aa632016-02-19 20:56:00 +010032#include <proto/proxy.h>
33#include <proto/server.h>
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <proto/stats.h>
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020035#include <proto/stick_table.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010036
Thierry Fournier1de16592016-01-27 09:49:07 +010037/* Contains the class reference of the concat object. */
38static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010039static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010040static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010041static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020042static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020043static int class_stktable_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010044
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020045#define MAX_STK_FILTER_LEN 4
Thierry Fournierf61aa632016-02-19 20:56:00 +010046#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010047
Thierry FOURNIERffbad792017-07-12 11:39:04 +020048static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010049
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010050int hlua_checkboolean(lua_State *L, int index)
51{
52 if (!lua_isboolean(L, index))
53 luaL_argerror(L, index, "boolean expected");
54 return lua_toboolean(L, index);
55}
56
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020057/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
58static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
59{
60#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
61 lua_pushinteger(L, val);
62#else
63 if (val > INT_MAX)
64 lua_pushnumber(L, (lua_Number)val);
65 else
66 lua_pushinteger(L, (int)val);
67#endif
68 return 1;
69}
70
71/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
72static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
73#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
74 /* 64 bits case, U64 is supported until LLONG_MAX */
75 if (val > LLONG_MAX)
76 lua_pushnumber(L, (lua_Number)val);
77 else
78 lua_pushinteger(L, val);
79#else
80 /* 32 bits case, U64 is supported until INT_MAX */
81 if (val > INT_MAX)
82 lua_pushnumber(L, (lua_Number)val);
83 else
84 lua_pushinteger(L, (int)val);
85#endif
86 return 1;
87}
88
Joseph Herlantb3d92e32018-11-15 09:35:04 -080089/* This function gets a struct field and converts it in Lua
90 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010091 */
92int hlua_fcn_pushfield(lua_State *L, struct field *field)
93{
94 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -080095 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010096 * by some macros. Windows maros are not supported.
97 * If the number cannot be represented as integer, we try to
98 * convert to float.
99 */
100 switch (field_format(field, 0)) {
101
102 case FF_EMPTY:
103 lua_pushnil(L);
104 return 1;
105
106 case FF_S32:
107 /* S32 is always supported. */
108 lua_pushinteger(L, field->u.s32);
109 return 1;
110
111 case FF_U32:
112#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
113 /* 64 bits case, U32 is always supported */
114 lua_pushinteger(L, field->u.u32);
115#else
116 /* 32 bits case, U32 is supported until INT_MAX. */
117 if (field->u.u32 > INT_MAX)
118 lua_pushnumber(L, (lua_Number)field->u.u32);
119 else
120 lua_pushinteger(L, field->u.u32);
121#endif
122 return 1;
123
124 case FF_S64:
125#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
126 /* 64 bits case, S64 is always supported */
127 lua_pushinteger(L, field->u.s64);
128#else
129 /* 64 bits case, S64 is supported beetween INT_MIN and INT_MAX */
130 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
131 lua_pushnumber(L, (lua_Number)field->u.s64);
132 else
133 lua_pushinteger(L, (int)field->u.s64);
134#endif
135 return 1;
136
137 case FF_U64:
138#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
139 /* 64 bits case, U64 is supported until LLONG_MAX */
140 if (field->u.u64 > LLONG_MAX)
141 lua_pushnumber(L, (lua_Number)field->u.u64);
142 else
143 lua_pushinteger(L, field->u.u64);
144#else
145 /* 64 bits case, U64 is supported until INT_MAX */
146 if (field->u.u64 > INT_MAX)
147 lua_pushnumber(L, (lua_Number)field->u.u64);
148 else
149 lua_pushinteger(L, (int)field->u.u64);
150#endif
151 return 1;
152
153 case FF_STR:
154 lua_pushstring(L, field->u.str);
155 return 1;
156
157 default:
158 break;
159 }
160
161 /* Default case, never reached. */
162 lua_pushnil(L);
163 return 1;
164}
165
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100166/* Some string are started or terminated by blank chars,
167 * this function removes the spaces, tabs, \r and
168 * \n at the begin and at the end of the string "str", and
169 * push the result in the lua stack.
170 * Returns a pointer to the Lua internal copy of the string.
171 */
172const char *hlua_pushstrippedstring(lua_State *L, const char *str)
173{
174 const char *p;
175 const char *e;
176
177 for (p = str; HTTP_IS_LWS(*p); p++);
178 for (e = p + strlen(p) - 1; e > p && HTTP_IS_LWS(*e); e--);
179
180 return lua_pushlstring(L, p, e - p);
181}
182
Thierry Fournierddd89882016-02-22 19:52:08 +0100183/* The three following functions are useful for adding entries
184 * in a table. These functions takes a string and respectively an
185 * integer, a string or a function and add it to the table in the
186 * top of the stack.
187 *
188 * These functions throws an error if no more stack size is
189 * available.
190 */
191void hlua_class_const_int(lua_State *L, const char *name, int value)
192{
Thierry Fournierddd89882016-02-22 19:52:08 +0100193 lua_pushstring(L, name);
194 lua_pushinteger(L, value);
195 lua_rawset(L, -3);
196}
197void hlua_class_const_str(lua_State *L, const char *name, const char *value)
198{
Thierry Fournierddd89882016-02-22 19:52:08 +0100199 lua_pushstring(L, name);
200 lua_pushstring(L, value);
201 lua_rawset(L, -3);
202}
203void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
204{
Thierry Fournierddd89882016-02-22 19:52:08 +0100205 lua_pushstring(L, name);
206 lua_pushcclosure(L, function, 0);
207 lua_rawset(L, -3);
208}
209
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800210/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100211int hlua_dump_object(struct lua_State *L)
212{
213 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
214 lua_pushfstring(L, "HAProxy class %s", name);
215 return 1;
216}
217
Thierry Fournier45e78d72016-02-19 18:34:46 +0100218/* This function register a table as metatable and. It names
219 * the metatable, and returns the associated reference.
220 * The original table is poped from the top of the stack.
221 * "name" is the referenced class name.
222 */
223int hlua_register_metatable(struct lua_State *L, char *name)
224{
225 /* Check the type of the top element. it must be
226 * a table.
227 */
228 if (lua_type(L, -1) != LUA_TTABLE)
229 luaL_error(L, "hlua_register_metatable() requires a type Table "
230 "in the top of the stack");
231
232 /* Add the __tostring function which identify the
233 * created object.
234 */
235 lua_pushstring(L, "__tostring");
236 lua_pushstring(L, name);
237 lua_pushcclosure(L, hlua_dump_object, 1);
238 lua_rawset(L, -3);
239
240 /* Register a named entry for the table. The table
241 * reference is copyed first because the function
242 * lua_setfield() pop the entry.
243 */
244 lua_pushvalue(L, -1);
245 lua_setfield(L, LUA_REGISTRYINDEX, name);
246
247 /* Creates the reference of the object. The
248 * function luaL_ref pop the top of the stack.
249 */
250 return luaL_ref(L, LUA_REGISTRYINDEX);
251}
252
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100253/* Return an object of the expected type, or throws an error. */
254void *hlua_checkudata(lua_State *L, int ud, int class_ref)
255{
256 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100257 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100258
259 /* Check if the stack entry is an array. */
260 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100261 luaL_argerror(L, ud, NULL);
262
263 /* pop the metatable of the referencecd object. */
264 if (!lua_getmetatable(L, ud))
265 luaL_argerror(L, ud, NULL);
266
267 /* pop the expected metatable. */
268 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
269
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100270 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100271 ret = lua_rawequal(L, -1, -2);
272 lua_pop(L, 2);
273 if (!ret)
274 luaL_argerror(L, ud, NULL);
275
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100276 /* Push on the stack at the entry [0] of the table. */
277 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100278
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100279 /* Check if this entry is userdata. */
280 p = lua_touserdata(L, -1);
281 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100282 luaL_argerror(L, ud, NULL);
283
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100284 /* Remove the entry returned by lua_rawgeti(). */
285 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100286
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100287 /* Return the associated struct. */
288 return p;
289}
290
Thierry Fournierb1f46562016-01-21 09:46:15 +0100291/* This function return the current date at epoch format in milliseconds. */
292int hlua_now(lua_State *L)
293{
294 lua_newtable(L);
295 lua_pushstring(L, "sec");
296 lua_pushinteger(L, now.tv_sec);
297 lua_rawset(L, -3);
298 lua_pushstring(L, "usec");
299 lua_pushinteger(L, now.tv_usec);
300 lua_rawset(L, -3);
301 return 1;
302}
303
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100304/* This functions expects a Lua string as HTTP date, parse it and
305 * returns an integer containing the epoch format of the date, or
306 * nil if the parsing fails.
307 */
308static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
309{
310 const char *str;
311 size_t len;
312 struct tm tm;
313 time_t time;
314
315 str = luaL_checklstring(L, 1, &len);
316
317 if (!fcn(str, len, &tm)) {
318 lua_pushnil(L);
319 return 1;
320 }
321
322 /* This function considers the content of the broken-down time
323 * is exprimed in the UTC timezone. timegm don't care about
324 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
325 * the timezone from the broken-down time, it must be fixed
326 * after the conversion.
327 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200328 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100329 if (time == -1) {
330 lua_pushnil(L);
331 return 1;
332 }
333
334 lua_pushinteger(L, (int)time);
335 return 1;
336}
337static int hlua_http_date(lua_State *L)
338{
339 return hlua_parse_date(L, parse_http_date);
340}
341static int hlua_imf_date(lua_State *L)
342{
343 return hlua_parse_date(L, parse_imf_date);
344}
345static int hlua_rfc850_date(lua_State *L)
346{
347 return hlua_parse_date(L, parse_rfc850_date);
348}
349static int hlua_asctime_date(lua_State *L)
350{
351 return hlua_parse_date(L, parse_asctime_date);
352}
353
Thierry Fourniereea77c02016-03-18 08:47:13 +0100354static int hlua_get_info(lua_State *L)
355{
356 int i;
357
358 stats_fill_info(stats, STATS_LEN);
359
360 lua_newtable(L);
361 for (i=0; i<INF_TOTAL_FIELDS; i++) {
362 lua_pushstring(L, info_field_names[i]);
363 hlua_fcn_pushfield(L, &stats[i]);
364 lua_settable(L, -3);
365 }
366 return 1;
367}
368
Thierry Fournier49d48422016-02-19 12:09:29 +0100369static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100370{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200371 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100372}
373
374static int hlua_concat_add(lua_State *L)
375{
Thierry Fournier49d48422016-02-19 12:09:29 +0100376 struct hlua_concat *b;
377 char *buffer;
378 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100379 const char *str;
380 size_t l;
381
382 /* First arg must be a concat object. */
383 b = hlua_check_concat(L, 1);
384
385 /* Second arg must be a string. */
386 str = luaL_checklstring(L, 2, &l);
387
Thierry Fournier49d48422016-02-19 12:09:29 +0100388 /* Get the buffer. */
389 lua_rawgeti(L, 1, 1);
390 buffer = lua_touserdata(L, -1);
391 lua_pop(L, 1);
392
393 /* Update the buffer size if it s required. The old buffer
394 * is crushed by the new in the object array, so it will
395 * be deleted by the GC.
396 * Note that in the first loop, the "new" variable is only
397 * used as a flag.
398 */
399 new = NULL;
400 while (b->size - b->len < l) {
401 b->size += HLUA_CONCAT_BLOCSZ;
402 new = buffer;
403 }
404 if (new) {
405 new = lua_newuserdata(L, b->size);
406 memcpy(new, buffer, b->len);
407 lua_rawseti(L, 1, 1);
408 buffer = new;
409 }
410
411 /* Copy string, and update metadata. */
412 memcpy(buffer + b->len, str, l);
413 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100414 return 0;
415}
416
417static int hlua_concat_dump(lua_State *L)
418{
Thierry Fournier49d48422016-02-19 12:09:29 +0100419 struct hlua_concat *b;
420 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100421
422 /* First arg must be a concat object. */
423 b = hlua_check_concat(L, 1);
424
Thierry Fournier49d48422016-02-19 12:09:29 +0100425 /* Get the buffer. */
426 lua_rawgeti(L, 1, 1);
427 buffer = lua_touserdata(L, -1);
428 lua_pop(L, 1);
429
Thierry Fournier1de16592016-01-27 09:49:07 +0100430 /* Push the soncatenated strng in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100431 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100432 return 1;
433}
434
435int hlua_concat_new(lua_State *L)
436{
Thierry Fournier49d48422016-02-19 12:09:29 +0100437 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100438
439 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200440 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100441 b->size = HLUA_CONCAT_BLOCSZ;
442 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100443 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100444 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
445 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100446
447 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
448 lua_setmetatable(L, -2);
449
Thierry Fournier1de16592016-01-27 09:49:07 +0100450 return 1;
451}
452
453static int concat_tostring(lua_State *L)
454{
455 const void *ptr = lua_topointer(L, 1);
456 lua_pushfstring(L, "Concat object: %p", ptr);
457 return 1;
458}
459
460static int hlua_concat_init(lua_State *L)
461{
462 /* Creates the buffered concat object. */
463 lua_newtable(L);
464
465 lua_pushstring(L, "__tostring");
466 lua_pushcclosure(L, concat_tostring, 0);
467 lua_settable(L, -3);
468
469 lua_pushstring(L, "__index"); /* Creates the index entry. */
470 lua_newtable(L); /* The "__index" content. */
471
472 lua_pushstring(L, "add");
473 lua_pushcclosure(L, hlua_concat_add, 0);
474 lua_settable(L, -3);
475
476 lua_pushstring(L, "dump");
477 lua_pushcclosure(L, hlua_concat_dump, 0);
478 lua_settable(L, -3);
479
480 lua_settable(L, -3); /* Sets the __index entry. */
481 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
482
483 return 1;
484}
485
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200486int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
487{
488 lua_newtable(L);
489
490 /* Pop a class stktbl metatable and affect it to the userdata. */
491 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
492 lua_setmetatable(L, -2);
493
494 lua_pushlightuserdata(L, tbl);
495 lua_rawseti(L, -2, 0);
496 return 1;
497}
498
499static struct stktable *hlua_check_stktable(lua_State *L, int ud)
500{
501 return hlua_checkudata(L, ud, class_stktable_ref);
502}
503
504/* Extract stick table attributes into Lua table */
505int hlua_stktable_info(lua_State *L)
506{
507 struct stktable *tbl;
508 int dt;
509
510 tbl = hlua_check_stktable(L, 1);
511
512 if (!tbl->id) {
513 lua_pushnil(L);
514 return 1;
515 }
516
517 lua_newtable(L);
518
519 lua_pushstring(L, "type");
520 lua_pushstring(L, stktable_types[tbl->type].kw);
521 lua_settable(L, -3);
522
523 lua_pushstring(L, "length");
524 lua_pushinteger(L, tbl->key_size);
525 lua_settable(L, -3);
526
527 lua_pushstring(L, "size");
528 hlua_fcn_pushunsigned(L, tbl->size);
529 lua_settable(L, -3);
530
531 lua_pushstring(L, "used");
532 hlua_fcn_pushunsigned(L, tbl->current);
533 lua_settable(L, -3);
534
535 lua_pushstring(L, "nopurge");
536 lua_pushboolean(L, tbl->nopurge > 0);
537 lua_settable(L, -3);
538
539 lua_pushstring(L, "expire");
540 lua_pushinteger(L, tbl->expire);
541 lua_settable(L, -3);
542
543 /* Save data types periods (if applicable) in 'data' table */
544 lua_pushstring(L, "data");
545 lua_newtable(L);
546
547 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
548 if (tbl->data_ofs[dt] == 0)
549 continue;
550
551 lua_pushstring(L, stktable_data_types[dt].name);
552
553 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
554 lua_pushinteger(L, tbl->data_arg[dt].u);
555 else
556 lua_pushinteger(L, -1);
557
558 lua_settable(L, -3);
559 }
560
561 lua_settable(L, -3);
562
563 return 1;
564}
565
566/* Helper to get extract stick table entry into Lua table */
567static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
568{
569 int dt;
570 void *ptr;
571
572 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
573
574 if (t->data_ofs[dt] == 0)
575 continue;
576
577 lua_pushstring(L, stktable_data_types[dt].name);
578
579 ptr = stktable_data_ptr(t, ts, dt);
580 switch (stktable_data_types[dt].std_type) {
581 case STD_T_SINT:
582 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
583 break;
584 case STD_T_UINT:
585 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
586 break;
587 case STD_T_ULL:
588 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
589 break;
590 case STD_T_FRQP:
591 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
592 t->data_arg[dt].u));
593 break;
Adis Neziroviceafb2b12020-05-05 13:57:28 +0200594 case STD_T_DICT: {
595 struct dict_entry *de;
596 de = stktable_data_cast(ptr, std_t_dict);
597 lua_pushstring(L, de ? (char *)de->value.key : "-");
598 break;
599 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200600 }
601
602 lua_settable(L, -3);
603 }
604}
605
606/* Looks in table <t> for a sticky session matching key <key>
607 * Returns table with session data or nil
608 *
609 * The returned table always contains 'use' and 'expire' (integer) fields.
610 * For frequency/rate counters, each data entry is returned as table with
611 * 'value' and 'period' fields.
612 */
613int hlua_stktable_lookup(lua_State *L)
614{
615 struct stktable *t;
616 struct sample smp;
617 struct stktable_key *skey;
618 struct stksess *ts;
619
620 t = hlua_check_stktable(L, 1);
621 smp.data.type = SMP_T_STR;
622 smp.flags = SMP_F_CONST;
Nathan Neulinger712841b2020-03-03 20:32:47 -0600623 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200624
625 skey = smp_to_stkey(&smp, t);
626 if (!skey) {
627 lua_pushnil(L);
628 return 1;
629 }
630
631 ts = stktable_lookup_key(t, skey);
632 if (!ts) {
633 lua_pushnil(L);
634 return 1;
635 }
636
637 lua_newtable(L);
638 lua_pushstring(L, "use");
639 lua_pushinteger(L, ts->ref_cnt - 1);
640 lua_settable(L, -3);
641
642 lua_pushstring(L, "expire");
643 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
644 lua_settable(L, -3);
645
646 hlua_stktable_entry(L, t, ts);
647 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
648 ts->ref_cnt--;
649 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
650
651 return 1;
652}
653
654struct stk_filter {
655 long long val;
656 int type;
657 int op;
658};
659
660
661/* Helper for returning errors to callers using Lua convention (nil, err) */
662static int hlua_error(lua_State *L, const char *fmt, ...) {
663 char buf[256];
664 int len;
665 va_list args;
666 va_start(args, fmt);
667 len = vsnprintf(buf, sizeof(buf), fmt, args);
668 va_end(args);
669
670 if (len < 0) {
671 ha_alert("hlua_error(): Could not write error message.\n");
672 lua_pushnil(L);
673 return 1;
674 } else if (len >= sizeof(buf))
675 ha_alert("hlua_error(): Error message was truncated.\n");
676
677 lua_pushnil(L);
678 lua_pushstring(L, buf);
679
680 return 2;
681}
682
683/* Dump the contents of stick table <t>*/
684int hlua_stktable_dump(lua_State *L)
685{
686 struct stktable *t;
687 struct ebmb_node *eb;
688 struct ebmb_node *n;
689 struct stksess *ts;
690 int type;
691 int op;
692 int dt;
693 long long val;
694 struct stk_filter filter[MAX_STK_FILTER_LEN];
695 int filter_count = 0;
696 int i;
697 int skip_entry;
698 void *ptr;
699
700 t = hlua_check_stktable(L, 1);
701 type = lua_type(L, 2);
702
703 switch (type) {
704 case LUA_TNONE:
705 case LUA_TNIL:
706 break;
707 case LUA_TTABLE:
708 lua_pushnil(L);
709 while (lua_next(L, 2) != 0) {
710 int entry_idx = 0;
711
712 if (filter_count >= MAX_STK_FILTER_LEN)
713 return hlua_error(L, "Filter table too large (len > %d)", MAX_STK_FILTER_LEN);
714
715 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
716 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
717
718 lua_pushnil(L);
719 while (lua_next(L, -2) != 0) {
720 switch (entry_idx) {
721 case 0:
722 if (lua_type(L, -1) != LUA_TSTRING)
723 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
724
725 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
726 if (dt < 0 || t->data_ofs[dt] == 0)
727 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
728 filter[filter_count].type = dt;
729 break;
730 case 1:
731 if (lua_type(L, -1) != LUA_TSTRING)
732 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
733
734 op = get_std_op(lua_tostring(L, -1));
735 if (op < 0)
736 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
737 filter[filter_count].op = op;
738 break;
739 case 2:
740 val = lua_tointeger(L, -1);
741 filter[filter_count].val = val;
742 filter_count++;
743 break;
744 default:
745 break;
746 }
747 entry_idx++;
748 lua_pop(L, 1);
749 }
750 lua_pop(L, 1);
751 }
752 break;
753 default:
754 return hlua_error(L, "filter table expected");
755 }
756
757 lua_newtable(L);
758
759 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
760 eb = ebmb_first(&t->keys);
761 for (n = eb; n; n = ebmb_next(n)) {
762 ts = ebmb_entry(n, struct stksess, key);
763 if (!ts) {
764 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
765 return 1;
766 }
767 ts->ref_cnt++;
768 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
769
770 /* multi condition/value filter */
771 skip_entry = 0;
772 for (i = 0; i < filter_count; i++) {
773 if (t->data_ofs[filter[i].type] == 0)
774 continue;
775
776 ptr = stktable_data_ptr(t, ts, filter[i].type);
777
778 switch (stktable_data_types[filter[i].type].std_type) {
779 case STD_T_SINT:
780 val = stktable_data_cast(ptr, std_t_sint);
781 break;
782 case STD_T_UINT:
783 val = stktable_data_cast(ptr, std_t_uint);
784 break;
785 case STD_T_ULL:
786 val = stktable_data_cast(ptr, std_t_ull);
787 break;
788 case STD_T_FRQP:
789 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
790 t->data_arg[filter[i].type].u);
791 break;
792 default:
793 continue;
794 break;
795 }
796
797 op = filter[i].op;
798
799 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
800 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
801 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
802 skip_entry = 1;
803 break;
804 }
805 }
806
807 if (skip_entry) {
808 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
809 ts->ref_cnt--;
810 continue;
811 }
812
813 if (t->type == SMP_T_IPV4) {
814 char addr[INET_ADDRSTRLEN];
815 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
816 lua_pushstring(L, addr);
817 } else if (t->type == SMP_T_IPV6) {
818 char addr[INET6_ADDRSTRLEN];
819 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
820 lua_pushstring(L, addr);
821 } else if (t->type == SMP_T_SINT) {
822 lua_pushinteger(L, *ts->key.key);
823 } else if (t->type == SMP_T_STR) {
824 lua_pushstring(L, (const char *)ts->key.key);
825 } else {
826 return hlua_error(L, "Unsupported stick table key type");
827 }
828
829 lua_newtable(L);
830 hlua_stktable_entry(L, t, ts);
831 lua_settable(L, -3);
832 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
833 ts->ref_cnt--;
834 }
835 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
836
837 return 1;
838}
839
Thierry Fournierff480422016-02-25 08:36:46 +0100840int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
841{
842 lua_newtable(L);
843
844 /* Pop a class sesison metatable and affect it to the userdata. */
845 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
846 lua_setmetatable(L, -2);
847
848 lua_pushlightuserdata(L, lst);
849 lua_rawseti(L, -2, 0);
850 return 1;
851}
852
853static struct listener *hlua_check_listener(lua_State *L, int ud)
854{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200855 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100856}
857
858int hlua_listener_get_stats(lua_State *L)
859{
860 struct listener *li;
861 int i;
862
863 li = hlua_check_listener(L, 1);
864
Willy Tarreauc95bad52016-12-22 00:13:31 +0100865 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100866 lua_pushnil(L);
867 return 1;
868 }
869
Willy Tarreauc95bad52016-12-22 00:13:31 +0100870 stats_fill_li_stats(li->bind_conf->frontend, li, ST_SHLGNDS, stats, STATS_LEN);
Thierry Fournierff480422016-02-25 08:36:46 +0100871
872 lua_newtable(L);
873 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
874 lua_pushstring(L, stat_field_names[i]);
875 hlua_fcn_pushfield(L, &stats[i]);
876 lua_settable(L, -3);
877 }
878 return 1;
879
880}
881
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100882int hlua_fcn_new_server(lua_State *L, struct server *srv)
883{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400884 char buffer[12];
885
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100886 lua_newtable(L);
887
888 /* Pop a class sesison metatable and affect it to the userdata. */
889 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
890 lua_setmetatable(L, -2);
891
892 lua_pushlightuserdata(L, srv);
893 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400894
895 /* Add server name. */
896 lua_pushstring(L, "name");
897 lua_pushstring(L, srv->id);
898 lua_settable(L, -3);
899
900 /* Add server puid. */
901 lua_pushstring(L, "puid");
902 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
903 lua_pushstring(L, buffer);
904 lua_settable(L, -3);
905
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100906 return 1;
907}
908
909static struct server *hlua_check_server(lua_State *L, int ud)
910{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200911 return hlua_checkudata(L, ud, class_server_ref);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100912}
913
914int hlua_server_get_stats(lua_State *L)
915{
916 struct server *srv;
917 int i;
918
919 srv = hlua_check_server(L, 1);
920
921 if (!srv->proxy) {
922 lua_pushnil(L);
923 return 1;
924 }
925
926 stats_fill_sv_stats(srv->proxy, srv, ST_SHLGNDS, stats, STATS_LEN);
927
928 lua_newtable(L);
929 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
930 lua_pushstring(L, stat_field_names[i]);
931 hlua_fcn_pushfield(L, &stats[i]);
932 lua_settable(L, -3);
933 }
934 return 1;
935
936}
937
938int hlua_server_get_addr(lua_State *L)
939{
940 struct server *srv;
941 char addr[INET6_ADDRSTRLEN];
942 luaL_Buffer b;
943
944 srv = hlua_check_server(L, 1);
945
946 luaL_buffinit(L, &b);
947
948 switch (srv->addr.ss_family) {
949 case AF_INET:
950 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
951 addr, INET_ADDRSTRLEN);
952 luaL_addstring(&b, addr);
953 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400954 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100955 luaL_addstring(&b, addr);
956 break;
957 case AF_INET6:
958 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400959 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100960 luaL_addstring(&b, addr);
961 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400962 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100963 luaL_addstring(&b, addr);
964 break;
965 case AF_UNIX:
966 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
967 break;
968 default:
969 luaL_addstring(&b, "<unknown>");
970 break;
971 }
972
973 luaL_pushresult(&b);
974 return 1;
975}
976
977int hlua_server_is_draining(lua_State *L)
978{
979 struct server *srv;
980
981 srv = hlua_check_server(L, 1);
982 lua_pushinteger(L, server_is_draining(srv));
983 return 1;
984}
985
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400986int hlua_server_set_maxconn(lua_State *L)
987{
988 struct server *srv;
989 const char *maxconn;
990 const char *err;
991
992 srv = hlua_check_server(L, 1);
993 maxconn = luaL_checkstring(L, 2);
994
995 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
996 err = server_parse_maxconn_change_request(srv, maxconn);
997 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
998 if (!err)
999 lua_pushnil(L);
1000 else
1001 hlua_pushstrippedstring(L, err);
1002 return 1;
1003}
1004
1005int hlua_server_get_maxconn(lua_State *L)
1006{
1007 struct server *srv;
1008
1009 srv = hlua_check_server(L, 1);
1010 lua_pushinteger(L, srv->maxconn);
1011 return 1;
1012}
1013
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001014int hlua_server_set_weight(lua_State *L)
1015{
1016 struct server *srv;
1017 const char *weight;
1018 const char *err;
1019
1020 srv = hlua_check_server(L, 1);
1021 weight = luaL_checkstring(L, 2);
1022
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001023 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001024 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001025 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001026 if (!err)
1027 lua_pushnil(L);
1028 else
1029 hlua_pushstrippedstring(L, err);
1030 return 1;
1031}
1032
1033int hlua_server_get_weight(lua_State *L)
1034{
1035 struct server *srv;
1036
1037 srv = hlua_check_server(L, 1);
1038 lua_pushinteger(L, srv->uweight);
1039 return 1;
1040}
1041
1042int hlua_server_set_addr(lua_State *L)
1043{
1044 struct server *srv;
1045 const char *addr;
1046 const char *err;
1047
1048 srv = hlua_check_server(L, 1);
1049 addr = luaL_checkstring(L, 2);
1050
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001051 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001052 err = server_parse_addr_change_request(srv, addr, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001053 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001054 if (!err)
1055 lua_pushnil(L);
1056 else
1057 hlua_pushstrippedstring(L, err);
1058 return 1;
1059}
1060
1061int hlua_server_shut_sess(lua_State *L)
1062{
1063 struct server *srv;
1064
1065 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001066 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001067 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001068 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001069 return 0;
1070}
1071
1072int hlua_server_set_drain(lua_State *L)
1073{
1074 struct server *srv;
1075
1076 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001077 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001078 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001079 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001080 return 0;
1081}
1082
1083int hlua_server_set_maint(lua_State *L)
1084{
1085 struct server *srv;
1086
1087 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001088 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001089 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001090 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001091 return 0;
1092}
1093
1094int hlua_server_set_ready(lua_State *L)
1095{
1096 struct server *srv;
1097
1098 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001099 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001100 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001101 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001102 return 0;
1103}
1104
1105int hlua_server_check_enable(lua_State *L)
1106{
1107 struct server *sv;
1108
1109 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001110 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001111 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001112 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001113 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001114 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001115 return 0;
1116}
1117
1118int hlua_server_check_disable(lua_State *L)
1119{
1120 struct server *sv;
1121
1122 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001123 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001124 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001125 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001126 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001127 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001128 return 0;
1129}
1130
1131int hlua_server_check_force_up(lua_State *L)
1132{
1133 struct server *sv;
1134
1135 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001136 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001137 if (!(sv->track)) {
1138 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001139 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001140 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001141 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001142 return 0;
1143}
1144
1145int hlua_server_check_force_nolb(lua_State *L)
1146{
1147 struct server *sv;
1148
1149 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001150 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001151 if (!(sv->track)) {
1152 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001153 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001154 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001155 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001156 return 0;
1157}
1158
1159int hlua_server_check_force_down(lua_State *L)
1160{
1161 struct server *sv;
1162
1163 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001164 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001165 if (!(sv->track)) {
1166 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001167 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001168 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001169 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001170 return 0;
1171}
1172
1173int hlua_server_agent_enable(lua_State *L)
1174{
1175 struct server *sv;
1176
1177 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001178 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001179 if (sv->agent.state & CHK_ST_CONFIGURED) {
1180 sv->agent.state |= CHK_ST_ENABLED;
1181 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001182 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001183 return 0;
1184}
1185
1186int hlua_server_agent_disable(lua_State *L)
1187{
1188 struct server *sv;
1189
1190 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001191 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001192 if (sv->agent.state & CHK_ST_CONFIGURED) {
1193 sv->agent.state &= ~CHK_ST_ENABLED;
1194 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001195 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001196 return 0;
1197}
1198
1199int hlua_server_agent_force_up(lua_State *L)
1200{
1201 struct server *sv;
1202
1203 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001204 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001205 if (sv->agent.state & CHK_ST_ENABLED) {
1206 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001207 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001208 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001209 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001210 return 0;
1211}
1212
1213int hlua_server_agent_force_down(lua_State *L)
1214{
1215 struct server *sv;
1216
1217 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001219 if (sv->agent.state & CHK_ST_ENABLED) {
1220 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001221 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001222 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001223 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001224 return 0;
1225}
1226
Thierry Fournierf61aa632016-02-19 20:56:00 +01001227int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1228{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001229 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001230 struct listener *lst;
1231 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001232 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001233
Thierry Fournierf61aa632016-02-19 20:56:00 +01001234 lua_newtable(L);
1235
1236 /* Pop a class sesison metatable and affect it to the userdata. */
1237 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1238 lua_setmetatable(L, -2);
1239
1240 lua_pushlightuserdata(L, px);
1241 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001242
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001243 /* Add proxy name. */
1244 lua_pushstring(L, "name");
1245 lua_pushstring(L, px->id);
1246 lua_settable(L, -3);
1247
Baptiste Assmann46c72552017-10-26 21:51:58 +02001248 /* Add proxy uuid. */
1249 lua_pushstring(L, "uuid");
1250 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1251 lua_pushstring(L, buffer);
1252 lua_settable(L, -3);
1253
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001254 /* Browse and register servers. */
1255 lua_pushstring(L, "servers");
1256 lua_newtable(L);
1257 for (srv = px->srv; srv; srv = srv->next) {
1258 lua_pushstring(L, srv->id);
1259 hlua_fcn_new_server(L, srv);
1260 lua_settable(L, -3);
1261 }
1262 lua_settable(L, -3);
1263
Thierry Fournierff480422016-02-25 08:36:46 +01001264 /* Browse and register listeners. */
1265 lua_pushstring(L, "listeners");
1266 lua_newtable(L);
1267 lid = 1;
1268 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1269 if (lst->name)
1270 lua_pushstring(L, lst->name);
1271 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001272 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001273 lid++;
1274 lua_pushstring(L, buffer);
1275 }
1276 hlua_fcn_new_listener(L, lst);
1277 lua_settable(L, -3);
1278 }
1279 lua_settable(L, -3);
1280
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001281 if (px->table && px->table->id) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001282 lua_pushstring(L, "stktable");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001283 hlua_fcn_new_stktable(L, px->table);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001284 lua_settable(L, -3);
1285 }
1286
Thierry Fournierf61aa632016-02-19 20:56:00 +01001287 return 1;
1288}
1289
1290static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1291{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001292 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001293}
1294
1295int hlua_proxy_pause(lua_State *L)
1296{
1297 struct proxy *px;
1298
1299 px = hlua_check_proxy(L, 1);
1300 pause_proxy(px);
1301 return 0;
1302}
1303
1304int hlua_proxy_resume(lua_State *L)
1305{
1306 struct proxy *px;
1307
1308 px = hlua_check_proxy(L, 1);
1309 resume_proxy(px);
1310 return 0;
1311}
1312
1313int hlua_proxy_stop(lua_State *L)
1314{
1315 struct proxy *px;
1316
1317 px = hlua_check_proxy(L, 1);
1318 stop_proxy(px);
1319 return 0;
1320}
1321
1322int hlua_proxy_get_cap(lua_State *L)
1323{
1324 struct proxy *px;
1325 const char *str;
1326
1327 px = hlua_check_proxy(L, 1);
1328 str = proxy_cap_str(px->cap);
1329 lua_pushstring(L, str);
1330 return 1;
1331}
1332
1333int hlua_proxy_get_stats(lua_State *L)
1334{
1335 struct proxy *px;
1336 int i;
1337
1338 px = hlua_check_proxy(L, 1);
1339 if (px->cap & PR_CAP_BE)
1340 stats_fill_be_stats(px, ST_SHLGNDS, stats, STATS_LEN);
1341 else
1342 stats_fill_fe_stats(px, stats, STATS_LEN);
1343 lua_newtable(L);
1344 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
1345 lua_pushstring(L, stat_field_names[i]);
1346 hlua_fcn_pushfield(L, &stats[i]);
1347 lua_settable(L, -3);
1348 }
1349 return 1;
1350}
1351
1352int hlua_proxy_get_mode(lua_State *L)
1353{
1354 struct proxy *px;
1355 const char *str;
1356
1357 px = hlua_check_proxy(L, 1);
1358 str = proxy_mode_str(px->mode);
1359 lua_pushstring(L, str);
1360 return 1;
1361}
1362
1363int hlua_proxy_shut_bcksess(lua_State *L)
1364{
1365 struct proxy *px;
1366
1367 px = hlua_check_proxy(L, 1);
1368 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1369 return 0;
1370}
1371
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001372int hlua_fcn_post_init(lua_State *L)
1373{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001374 struct proxy *px;
1375
1376 /* get core array. */
1377 if (lua_getglobal(L, "core") != LUA_TTABLE)
1378 lua_error(L);
1379
1380 /* Create proxies entry. */
1381 lua_pushstring(L, "proxies");
1382 lua_newtable(L);
1383
1384 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001385 for (px = proxies_list; px; px = px->next) {
Thierry Fournierf61aa632016-02-19 20:56:00 +01001386 lua_pushstring(L, px->id);
1387 hlua_fcn_new_proxy(L, px);
1388 lua_settable(L, -3);
1389 }
1390
1391 /* push "proxies" in "core" */
1392 lua_settable(L, -3);
1393
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001394 /* Create proxies entry. */
1395 lua_pushstring(L, "frontends");
1396 lua_newtable(L);
1397
1398 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001399 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001400 if (!(px->cap & PR_CAP_FE))
1401 continue;
1402 lua_pushstring(L, px->id);
1403 hlua_fcn_new_proxy(L, px);
1404 lua_settable(L, -3);
1405 }
1406
1407 /* push "frontends" in "core" */
1408 lua_settable(L, -3);
1409
1410 /* Create proxies entry. */
1411 lua_pushstring(L, "backends");
1412 lua_newtable(L);
1413
1414 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001415 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001416 if (!(px->cap & PR_CAP_BE))
1417 continue;
1418 lua_pushstring(L, px->id);
1419 hlua_fcn_new_proxy(L, px);
1420 lua_settable(L, -3);
1421 }
1422
1423 /* push "backend" in "core" */
1424 lua_settable(L, -3);
1425
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001426 return 1;
1427}
1428
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001429/* This Lua function take a string, a list of separators.
1430 * It tokenize the input string using the list of separators
1431 * as separator.
1432 *
1433 * The functionreturns a tablle filled with tokens.
1434 */
1435int hlua_tokenize(lua_State *L)
1436{
1437 const char *str;
1438 const char *sep;
1439 int index;
1440 const char *token;
1441 const char *p;
1442 const char *c;
1443 int ignore_empty;
1444
1445 ignore_empty = 0;
1446
1447 str = luaL_checkstring(L, 1);
1448 sep = luaL_checkstring(L, 2);
1449 if (lua_gettop(L) == 3)
1450 ignore_empty = hlua_checkboolean(L, 3);
1451
1452 lua_newtable(L);
1453 index = 1;
1454 token = str;
1455 p = str;
1456 while(1) {
1457 for (c = sep; *c != '\0'; c++)
1458 if (*p == *c)
1459 break;
1460 if (*p == *c) {
1461 if ((!ignore_empty) || (p - token > 0)) {
1462 lua_pushlstring(L, token, p - token);
1463 lua_rawseti(L, -2, index);
1464 index++;
1465 }
1466 token = p + 1;
1467 }
1468 if (*p == '\0')
1469 break;
1470 p++;
1471 }
1472
1473 return 1;
1474}
1475
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001476int hlua_parse_addr(lua_State *L)
1477{
1478 struct hlua_addr *addr;
1479 const char *str = luaL_checkstring(L, 1);
1480 unsigned char mask;
1481
1482 addr = lua_newuserdata(L, sizeof(struct hlua_addr));
1483 if (!addr) {
1484 lua_pushnil(L);
1485 return 1;
1486 }
1487
1488 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
1489 addr->type = AF_INET;
1490 return 1;
1491 }
1492
1493 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1494 len2mask6(mask, &addr->addr.v6.mask);
1495 addr->type = AF_INET6;
1496 return 1;
1497 }
1498
1499 lua_pop(L, 1);
1500 lua_pushnil(L);
1501 return 1;
1502}
1503
1504int hlua_match_addr(lua_State *L)
1505{
1506 struct hlua_addr *addr1;
1507 struct hlua_addr *addr2;
1508
1509 if (!lua_isuserdata(L, 1) ||
1510 !lua_isuserdata(L, 2)) {
1511 lua_pushboolean(L, 0);
1512 return 1;
1513 }
1514
1515 addr1 = lua_touserdata(L, 1);
1516 addr2 = lua_touserdata(L, 2);
1517
1518 if (addr1->type != addr2->type) {
1519 lua_pushboolean(L, 0);
1520 return 1;
1521 }
1522
1523 if (addr1->type == AF_INET) {
1524 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1525 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1526 lua_pushboolean(L, 1);
1527 return 1;
1528 }
1529 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001530 int i;
1531
1532 for (i = 0; i < 16; i += 4) {
1533 if ((*(uint32_t *)&addr1->addr.v6.ip.s6_addr[i] &
1534 *(uint32_t *)&addr2->addr.v6.mask.s6_addr[i]) !=
1535 (*(uint32_t *)&addr2->addr.v6.ip.s6_addr[i] &
1536 *(uint32_t *)&addr1->addr.v6.mask.s6_addr[i]))
1537 break;
1538 }
1539 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001540 lua_pushboolean(L, 1);
1541 return 1;
1542 }
1543 }
1544
1545 lua_pushboolean(L, 0);
1546 return 1;
1547}
1548
Dragan Dosen26743032019-04-30 15:54:36 +02001549static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001550{
1551 return (hlua_checkudata(L, ud, class_regex_ref));
1552}
1553
1554static int hlua_regex_comp(struct lua_State *L)
1555{
Dragan Dosen26743032019-04-30 15:54:36 +02001556 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001557 const char *str;
1558 int cs;
1559 char *err;
1560
1561 str = luaL_checkstring(L, 1);
1562 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1563 cs = lua_toboolean(L, 2);
1564
1565 regex = lua_newuserdata(L, sizeof(*regex));
1566
1567 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001568 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001569 lua_pushboolean(L, 0); /* status error */
1570 lua_pushstring(L, err); /* Reason */
1571 free(err);
1572 return 2;
1573 }
1574
1575 lua_pushboolean(L, 1); /* Status ok */
1576
1577 /* Create object */
1578 lua_newtable(L);
1579 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1580 lua_rawseti(L, -2, 0);
1581 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1582 lua_setmetatable(L, -2);
1583 return 2;
1584}
1585
1586static int hlua_regex_exec(struct lua_State *L)
1587{
Dragan Dosen26743032019-04-30 15:54:36 +02001588 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001589 const char *str;
1590 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001591 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001592
1593 regex = hlua_check_regex(L, 1);
1594 str = luaL_checklstring(L, 2, &len);
1595
Dragan Dosen26743032019-04-30 15:54:36 +02001596 if (!*regex) {
1597 lua_pushboolean(L, 0);
1598 return 1;
1599 }
1600
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001601 /* Copy the string because regex_exec2 require a 'char *'
1602 * and not a 'const char *'.
1603 */
1604 tmp = get_trash_chunk();
1605 if (len >= tmp->size) {
1606 lua_pushboolean(L, 0);
1607 return 1;
1608 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001609 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001610
Dragan Dosen26743032019-04-30 15:54:36 +02001611 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001612
1613 return 1;
1614}
1615
1616static int hlua_regex_match(struct lua_State *L)
1617{
Dragan Dosen26743032019-04-30 15:54:36 +02001618 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001619 const char *str;
1620 size_t len;
1621 regmatch_t pmatch[20];
1622 int ret;
1623 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001624 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001625
1626 regex = hlua_check_regex(L, 1);
1627 str = luaL_checklstring(L, 2, &len);
1628
Dragan Dosen26743032019-04-30 15:54:36 +02001629 if (!*regex) {
1630 lua_pushboolean(L, 0);
1631 return 1;
1632 }
1633
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001634 /* Copy the string because regex_exec2 require a 'char *'
1635 * and not a 'const char *'.
1636 */
1637 tmp = get_trash_chunk();
1638 if (len >= tmp->size) {
1639 lua_pushboolean(L, 0);
1640 return 1;
1641 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001642 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001643
Dragan Dosen26743032019-04-30 15:54:36 +02001644 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001645 lua_pushboolean(L, ret);
1646 lua_newtable(L);
1647 if (ret) {
1648 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1649 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1650 lua_rawseti(L, -2, i + 1);
1651 }
1652 }
1653 return 2;
1654}
1655
1656static int hlua_regex_free(struct lua_State *L)
1657{
Dragan Dosen26743032019-04-30 15:54:36 +02001658 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001659
1660 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001661 regex_free(*regex);
1662 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001663 return 0;
1664}
1665
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001666int hlua_fcn_reg_core_fcn(lua_State *L)
1667{
Thierry Fournier1de16592016-01-27 09:49:07 +01001668 if (!hlua_concat_init(L))
1669 return 0;
1670
Thierry Fournier4f99b272016-02-22 08:40:02 +01001671 hlua_class_function(L, "now", hlua_now);
1672 hlua_class_function(L, "http_date", hlua_http_date);
1673 hlua_class_function(L, "imf_date", hlua_imf_date);
1674 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1675 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1676 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001677 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001678 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1679 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001680 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001681
Thierry FOURNIER31904272017-10-25 12:59:51 +02001682 /* Create regex object. */
1683 lua_newtable(L);
1684 hlua_class_function(L, "new", hlua_regex_comp);
1685
1686 lua_newtable(L); /* The metatable. */
1687 lua_pushstring(L, "__index");
1688 lua_newtable(L);
1689 hlua_class_function(L, "exec", hlua_regex_exec);
1690 hlua_class_function(L, "match", hlua_regex_match);
1691 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1692 hlua_class_function(L, "__gc", hlua_regex_free);
1693
1694 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1695 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1696
1697 lua_setmetatable(L, -2);
1698 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1699
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001700 /* Create stktable object. */
1701 lua_newtable(L);
1702 lua_pushstring(L, "__index");
1703 lua_newtable(L);
1704 hlua_class_function(L, "info", hlua_stktable_info);
1705 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1706 hlua_class_function(L, "dump", hlua_stktable_dump);
1707 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1708 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1709
Thierry Fournierff480422016-02-25 08:36:46 +01001710 /* Create listener object. */
1711 lua_newtable(L);
1712 lua_pushstring(L, "__index");
1713 lua_newtable(L);
1714 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1715 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1716 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1717
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001718 /* Create server object. */
1719 lua_newtable(L);
1720 lua_pushstring(L, "__index");
1721 lua_newtable(L);
1722 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001723 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1724 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001725 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1726 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1727 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1728 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1729 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1730 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1731 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1732 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1733 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1734 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1735 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1736 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1737 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1738 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1739 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1740 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1741 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1742 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1743 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1744 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1745
Thierry Fournierf61aa632016-02-19 20:56:00 +01001746 /* Create proxy object. */
1747 lua_newtable(L);
1748 lua_pushstring(L, "__index");
1749 lua_newtable(L);
1750 hlua_class_function(L, "pause", hlua_proxy_pause);
1751 hlua_class_function(L, "resume", hlua_proxy_resume);
1752 hlua_class_function(L, "stop", hlua_proxy_stop);
1753 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1754 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1755 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1756 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1757 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1758 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1759
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001760 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001761}