blob: 72eae57e71d6af7f966dea9876a0f787d6fa44b6 [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 Jacquinf4c12d42021-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
Willy Tarreau63617db2021-10-06 18:23:40 +020024#include <import/ebmbtree.h>
25
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/errors.h>
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +010028#include <haproxy/hlua.h>
Tim Duesterhus6eded622022-05-14 22:17:25 +020029#include <haproxy/hlua_fcn.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020030#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/pattern-t.h>
33#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020037#include <haproxy/stick_table.h>
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010038#include <haproxy/event_hdl.h>
Willy Tarreau27539402021-10-06 09:12:44 +020039#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010041#include <haproxy/tools.h>
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +020042#include <haproxy/mailers.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010043
Thierry Fournier1de16592016-01-27 09:49:07 +010044/* Contains the class reference of the concat object. */
45static int class_concat_ref;
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +020046static int class_queue_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010047static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010048static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010049static int class_listener_ref;
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010050static int class_event_sub_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020051static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020052static int class_stktable_ref;
Thierry Fournier467913c2022-09-30 11:03:38 +020053static int class_proxy_list_ref;
Thierry Fournier1edf36a2022-10-07 13:25:51 +020054static int class_server_list_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010055
Thierry Fournierf61aa632016-02-19 20:56:00 +010056#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010057
Thierry FOURNIERffbad792017-07-12 11:39:04 +020058static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010059
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010060int hlua_checkboolean(lua_State *L, int index)
61{
62 if (!lua_isboolean(L, index))
63 luaL_argerror(L, index, "boolean expected");
64 return lua_toboolean(L, index);
65}
66
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020067/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
68static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
69{
70#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
71 lua_pushinteger(L, val);
72#else
73 if (val > INT_MAX)
74 lua_pushnumber(L, (lua_Number)val);
75 else
76 lua_pushinteger(L, (int)val);
77#endif
78 return 1;
79}
80
81/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
82static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
83#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
84 /* 64 bits case, U64 is supported until LLONG_MAX */
85 if (val > LLONG_MAX)
86 lua_pushnumber(L, (lua_Number)val);
87 else
88 lua_pushinteger(L, val);
89#else
90 /* 32 bits case, U64 is supported until INT_MAX */
91 if (val > INT_MAX)
92 lua_pushnumber(L, (lua_Number)val);
93 else
94 lua_pushinteger(L, (int)val);
95#endif
96 return 1;
97}
98
Joseph Herlantb3d92e32018-11-15 09:35:04 -080099/* This function gets a struct field and converts it in Lua
100 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100101 */
102int hlua_fcn_pushfield(lua_State *L, struct field *field)
103{
104 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800105 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100106 * by some macros. Windows maros are not supported.
107 * If the number cannot be represented as integer, we try to
108 * convert to float.
109 */
110 switch (field_format(field, 0)) {
111
112 case FF_EMPTY:
113 lua_pushnil(L);
114 return 1;
115
116 case FF_S32:
117 /* S32 is always supported. */
118 lua_pushinteger(L, field->u.s32);
119 return 1;
120
121 case FF_U32:
122#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
123 /* 64 bits case, U32 is always supported */
124 lua_pushinteger(L, field->u.u32);
125#else
126 /* 32 bits case, U32 is supported until INT_MAX. */
127 if (field->u.u32 > INT_MAX)
128 lua_pushnumber(L, (lua_Number)field->u.u32);
129 else
130 lua_pushinteger(L, field->u.u32);
131#endif
132 return 1;
133
134 case FF_S64:
135#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
136 /* 64 bits case, S64 is always supported */
137 lua_pushinteger(L, field->u.s64);
138#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500139 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100140 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
141 lua_pushnumber(L, (lua_Number)field->u.s64);
142 else
143 lua_pushinteger(L, (int)field->u.s64);
144#endif
145 return 1;
146
147 case FF_U64:
148#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
149 /* 64 bits case, U64 is supported until LLONG_MAX */
150 if (field->u.u64 > LLONG_MAX)
151 lua_pushnumber(L, (lua_Number)field->u.u64);
152 else
153 lua_pushinteger(L, field->u.u64);
154#else
155 /* 64 bits case, U64 is supported until INT_MAX */
156 if (field->u.u64 > INT_MAX)
157 lua_pushnumber(L, (lua_Number)field->u.u64);
158 else
159 lua_pushinteger(L, (int)field->u.u64);
160#endif
161 return 1;
162
163 case FF_STR:
164 lua_pushstring(L, field->u.str);
165 return 1;
166
167 default:
168 break;
169 }
170
171 /* Default case, never reached. */
172 lua_pushnil(L);
173 return 1;
174}
175
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100176/* Some string are started or terminated by blank chars,
177 * this function removes the spaces, tabs, \r and
178 * \n at the begin and at the end of the string "str", and
179 * push the result in the lua stack.
180 * Returns a pointer to the Lua internal copy of the string.
181 */
182const char *hlua_pushstrippedstring(lua_State *L, const char *str)
183{
184 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100185 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100186
187 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100188
189 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100190
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100191 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100192}
193
Thierry Fournierddd89882016-02-22 19:52:08 +0100194/* The three following functions are useful for adding entries
195 * in a table. These functions takes a string and respectively an
196 * integer, a string or a function and add it to the table in the
197 * top of the stack.
198 *
199 * These functions throws an error if no more stack size is
200 * available.
201 */
202void hlua_class_const_int(lua_State *L, const char *name, int value)
203{
Thierry Fournierddd89882016-02-22 19:52:08 +0100204 lua_pushstring(L, name);
205 lua_pushinteger(L, value);
206 lua_rawset(L, -3);
207}
208void hlua_class_const_str(lua_State *L, const char *name, const char *value)
209{
Thierry Fournierddd89882016-02-22 19:52:08 +0100210 lua_pushstring(L, name);
211 lua_pushstring(L, value);
212 lua_rawset(L, -3);
213}
214void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
215{
Thierry Fournierddd89882016-02-22 19:52:08 +0100216 lua_pushstring(L, name);
217 lua_pushcclosure(L, function, 0);
218 lua_rawset(L, -3);
219}
220
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800221/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100222int hlua_dump_object(struct lua_State *L)
223{
224 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
225 lua_pushfstring(L, "HAProxy class %s", name);
226 return 1;
227}
228
Thierry Fournier45e78d72016-02-19 18:34:46 +0100229/* This function register a table as metatable and. It names
230 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500231 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100232 * "name" is the referenced class name.
233 */
234int hlua_register_metatable(struct lua_State *L, char *name)
235{
236 /* Check the type of the top element. it must be
237 * a table.
238 */
239 if (lua_type(L, -1) != LUA_TTABLE)
240 luaL_error(L, "hlua_register_metatable() requires a type Table "
241 "in the top of the stack");
242
243 /* Add the __tostring function which identify the
244 * created object.
245 */
246 lua_pushstring(L, "__tostring");
247 lua_pushstring(L, name);
248 lua_pushcclosure(L, hlua_dump_object, 1);
249 lua_rawset(L, -3);
250
251 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500252 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100253 * lua_setfield() pop the entry.
254 */
255 lua_pushvalue(L, -1);
256 lua_setfield(L, LUA_REGISTRYINDEX, name);
257
258 /* Creates the reference of the object. The
259 * function luaL_ref pop the top of the stack.
260 */
261 return luaL_ref(L, LUA_REGISTRYINDEX);
262}
263
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100264/* Return an object of the expected type, or throws an error. */
265void *hlua_checkudata(lua_State *L, int ud, int class_ref)
266{
267 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100268 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100269
270 /* Check if the stack entry is an array. */
271 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100272 luaL_argerror(L, ud, NULL);
273
274 /* pop the metatable of the referencecd object. */
275 if (!lua_getmetatable(L, ud))
276 luaL_argerror(L, ud, NULL);
277
278 /* pop the expected metatable. */
279 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
280
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100281 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100282 ret = lua_rawequal(L, -1, -2);
283 lua_pop(L, 2);
284 if (!ret)
285 luaL_argerror(L, ud, NULL);
286
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100287 /* Push on the stack at the entry [0] of the table. */
288 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100289
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100290 /* Check if this entry is userdata. */
291 p = lua_touserdata(L, -1);
292 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100293 luaL_argerror(L, ud, NULL);
294
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100295 /* Remove the entry returned by lua_rawgeti(). */
296 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100297
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100298 /* Return the associated struct. */
299 return p;
300}
301
Thierry Fournierb1f46562016-01-21 09:46:15 +0100302/* This function return the current date at epoch format in milliseconds. */
303int hlua_now(lua_State *L)
304{
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200305 /* WT: the doc says "returns the current time" and later says that it's
306 * monotonic. So the best fit is to use start_date+(now-start_time).
307 */
308 struct timeval tv;
309
Willy Tarreauc05d30e2023-04-28 14:50:29 +0200310 tv = NS_TO_TV(now_ns - start_time_ns);
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200311 tv_add(&tv, &tv, &start_date);
312
Thierry Fournierb1f46562016-01-21 09:46:15 +0100313 lua_newtable(L);
314 lua_pushstring(L, "sec");
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200315 lua_pushinteger(L, tv.tv_sec);
Thierry Fournierb1f46562016-01-21 09:46:15 +0100316 lua_rawset(L, -3);
317 lua_pushstring(L, "usec");
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200318 lua_pushinteger(L, tv.tv_usec);
Thierry Fournierb1f46562016-01-21 09:46:15 +0100319 lua_rawset(L, -3);
320 return 1;
321}
322
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100323/* This functions expects a Lua string as HTTP date, parse it and
324 * returns an integer containing the epoch format of the date, or
325 * nil if the parsing fails.
326 */
327static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
328{
329 const char *str;
330 size_t len;
331 struct tm tm;
332 time_t time;
333
334 str = luaL_checklstring(L, 1, &len);
335
336 if (!fcn(str, len, &tm)) {
337 lua_pushnil(L);
338 return 1;
339 }
340
341 /* This function considers the content of the broken-down time
342 * is exprimed in the UTC timezone. timegm don't care about
343 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
344 * the timezone from the broken-down time, it must be fixed
345 * after the conversion.
346 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200347 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100348 if (time == -1) {
349 lua_pushnil(L);
350 return 1;
351 }
352
353 lua_pushinteger(L, (int)time);
354 return 1;
355}
356static int hlua_http_date(lua_State *L)
357{
358 return hlua_parse_date(L, parse_http_date);
359}
360static int hlua_imf_date(lua_State *L)
361{
362 return hlua_parse_date(L, parse_imf_date);
363}
364static int hlua_rfc850_date(lua_State *L)
365{
366 return hlua_parse_date(L, parse_rfc850_date);
367}
368static int hlua_asctime_date(lua_State *L)
369{
370 return hlua_parse_date(L, parse_asctime_date);
371}
372
Thierry Fourniereea77c02016-03-18 08:47:13 +0100373static int hlua_get_info(lua_State *L)
374{
375 int i;
376
Willy Tarreau0b26b382021-05-08 07:43:53 +0200377 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100378
379 lua_newtable(L);
380 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200381 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100382 hlua_fcn_pushfield(L, &stats[i]);
383 lua_settable(L, -3);
384 }
385 return 1;
386}
387
Thierry Fournier49d48422016-02-19 12:09:29 +0100388static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100389{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200390 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100391}
392
393static int hlua_concat_add(lua_State *L)
394{
Thierry Fournier49d48422016-02-19 12:09:29 +0100395 struct hlua_concat *b;
396 char *buffer;
397 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100398 const char *str;
399 size_t l;
400
401 /* First arg must be a concat object. */
402 b = hlua_check_concat(L, 1);
403
404 /* Second arg must be a string. */
405 str = luaL_checklstring(L, 2, &l);
406
Thierry Fournier49d48422016-02-19 12:09:29 +0100407 /* Get the buffer. */
408 lua_rawgeti(L, 1, 1);
409 buffer = lua_touserdata(L, -1);
410 lua_pop(L, 1);
411
412 /* Update the buffer size if it s required. The old buffer
413 * is crushed by the new in the object array, so it will
414 * be deleted by the GC.
415 * Note that in the first loop, the "new" variable is only
416 * used as a flag.
417 */
418 new = NULL;
419 while (b->size - b->len < l) {
420 b->size += HLUA_CONCAT_BLOCSZ;
421 new = buffer;
422 }
423 if (new) {
424 new = lua_newuserdata(L, b->size);
425 memcpy(new, buffer, b->len);
426 lua_rawseti(L, 1, 1);
427 buffer = new;
428 }
429
430 /* Copy string, and update metadata. */
431 memcpy(buffer + b->len, str, l);
432 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100433 return 0;
434}
435
436static int hlua_concat_dump(lua_State *L)
437{
Thierry Fournier49d48422016-02-19 12:09:29 +0100438 struct hlua_concat *b;
439 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100440
441 /* First arg must be a concat object. */
442 b = hlua_check_concat(L, 1);
443
Thierry Fournier49d48422016-02-19 12:09:29 +0100444 /* Get the buffer. */
445 lua_rawgeti(L, 1, 1);
446 buffer = lua_touserdata(L, -1);
447 lua_pop(L, 1);
448
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500449 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100450 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100451 return 1;
452}
453
454int hlua_concat_new(lua_State *L)
455{
Thierry Fournier49d48422016-02-19 12:09:29 +0100456 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100457
458 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200459 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100460 b->size = HLUA_CONCAT_BLOCSZ;
461 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100462 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100463 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
464 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100465
466 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
467 lua_setmetatable(L, -2);
468
Thierry Fournier1de16592016-01-27 09:49:07 +0100469 return 1;
470}
471
472static int concat_tostring(lua_State *L)
473{
474 const void *ptr = lua_topointer(L, 1);
475 lua_pushfstring(L, "Concat object: %p", ptr);
476 return 1;
477}
478
Thierry Fournier599f2312022-09-30 10:40:39 +0200479static void hlua_concat_init(lua_State *L)
Thierry Fournier1de16592016-01-27 09:49:07 +0100480{
481 /* Creates the buffered concat object. */
482 lua_newtable(L);
483
484 lua_pushstring(L, "__tostring");
485 lua_pushcclosure(L, concat_tostring, 0);
486 lua_settable(L, -3);
487
488 lua_pushstring(L, "__index"); /* Creates the index entry. */
489 lua_newtable(L); /* The "__index" content. */
490
491 lua_pushstring(L, "add");
492 lua_pushcclosure(L, hlua_concat_add, 0);
493 lua_settable(L, -3);
494
495 lua_pushstring(L, "dump");
496 lua_pushcclosure(L, hlua_concat_dump, 0);
497 lua_settable(L, -3);
498
499 lua_settable(L, -3); /* Sets the __index entry. */
500 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Thierry Fournier1de16592016-01-27 09:49:07 +0100501}
502
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200503/* C backing storage for lua Queue class */
504struct hlua_queue {
505 uint32_t size;
506 struct mt_list list;
507 struct mt_list wait_tasks;
508};
509
510/* used to store lua objects in queue->list */
511struct hlua_queue_item {
512 int ref; /* lua object reference id */
513 struct mt_list list;
514};
515
516/* used to store wait entries in queue->wait_tasks */
517struct hlua_queue_wait
518{
519 struct task *task;
520 struct mt_list entry;
521};
522
523/* This is the memory pool containing struct hlua_queue_item (queue items)
524 */
525DECLARE_STATIC_POOL(pool_head_hlua_queue, "hlua_queue", sizeof(struct hlua_queue_item));
526
527/* This is the memory pool containing struct hlua_queue_wait
528 * (queue waiting tasks)
529 */
530DECLARE_STATIC_POOL(pool_head_hlua_queuew, "hlua_queuew", sizeof(struct hlua_queue_wait));
531
532static struct hlua_queue *hlua_check_queue(lua_State *L, int ud)
533{
534 return hlua_checkudata(L, ud, class_queue_ref);
535}
536
537/* queue:size(): returns an integer containing the current number of queued
538 * items.
539 */
540static int hlua_queue_size(lua_State *L)
541{
542 struct hlua_queue *queue = hlua_check_queue(L, 1);
543
544 BUG_ON(!queue);
Aurelien DARRAGON41630362023-07-11 15:42:00 +0200545 lua_pushinteger(L, HA_ATOMIC_LOAD(&queue->size));
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200546
547 return 1;
548}
549
550/* queue:push(): push an item (any type, except nil) at the end of the queue
551 *
552 * Returns boolean:true for success and boolean:false on error
553 */
554static int hlua_queue_push(lua_State *L)
555{
556 struct hlua_queue *queue = hlua_check_queue(L, 1);
557 struct hlua_queue_item *item;
558 struct mt_list *elt1, elt2;
559 struct hlua_queue_wait *waiter;
560
561 if (lua_gettop(L) != 2 || lua_isnoneornil(L, 2)) {
562 luaL_error(L, "unexpected argument");
563 /* not reached */
564 return 0;
565 }
566 BUG_ON(!queue);
Aurelien DARRAGONd7d507a2023-05-10 19:59:35 +0200567
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200568 item = pool_alloc(pool_head_hlua_queue);
569 if (!item) {
Aurelien DARRAGONd7d507a2023-05-10 19:59:35 +0200570 /* memory error */
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200571 lua_pushboolean(L, 0);
572 return 1;
573 }
Aurelien DARRAGONd7d507a2023-05-10 19:59:35 +0200574
575 /* get a reference from lua object at the top of the stack */
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200576 item->ref = hlua_ref(L);
Aurelien DARRAGONd7d507a2023-05-10 19:59:35 +0200577
578 /* push new entry to the queue */
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200579 MT_LIST_INIT(&item->list);
580 HA_ATOMIC_INC(&queue->size);
581 MT_LIST_APPEND(&queue->list, &item->list);
582
583 /* notify tasks waiting on queue:pop_wait() (if any) */
584 mt_list_for_each_entry_safe(waiter, &queue->wait_tasks, entry, elt1, elt2) {
585 task_wakeup(waiter->task, TASK_WOKEN_MSG);
586 }
587
588 lua_pushboolean(L, 1);
589 return 1;
590}
591
Aurelien DARRAGONbd8a94a2023-05-10 19:30:26 +0200592/* internal queue pop helper, returns 1 if it successfuly popped an item
593 * from the queue and pushed it on lua stack.
594 *
595 * Else it returns 0 (nothing is pushed on the stack)
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200596 */
597static int _hlua_queue_pop(lua_State *L, struct hlua_queue *queue)
598{
599 struct hlua_queue_item *item;
600
601 item = MT_LIST_POP(&queue->list, typeof(item), list);
Aurelien DARRAGONbd8a94a2023-05-10 19:30:26 +0200602 if (!item)
603 return 0; /* nothing in queue */
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200604
605 HA_ATOMIC_DEC(&queue->size);
606 /* push lua obj on the stack */
607 hlua_pushref(L, item->ref);
608
Aurelien DARRAGONc0af7cd2023-05-10 19:47:08 +0200609 /* obj ref should be released right away since it was pushed
610 * on the stack and will not be used anymore
611 */
612 hlua_unref(L, item->ref);
613
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200614 /* free the queue item */
615 pool_free(pool_head_hlua_queue, item);
616
617 return 1;
618}
Aurelien DARRAGONbd8a94a2023-05-10 19:30:26 +0200619
620/* queue:pop(): returns the first item at the top of que queue or nil if
621 * the queue is empty.
622 */
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200623static int hlua_queue_pop(lua_State *L)
624{
625 struct hlua_queue *queue = hlua_check_queue(L, 1);
626
627 BUG_ON(!queue);
Aurelien DARRAGONbd8a94a2023-05-10 19:30:26 +0200628 if (!_hlua_queue_pop(L, queue)) {
629 /* nothing in queue, push nil */
630 lua_pushnil(L);
631 }
632 return 1; /* either item or nil is at the top of the stack */
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200633}
634
635/* queue:pop_wait(): same as queue:pop() but doesn't return on empty queue.
636 *
637 * Aborts if used incorrectly and returns nil in case of memory error.
638 */
639static int _hlua_queue_pop_wait(lua_State *L, int status, lua_KContext ctx)
640{
641 struct hlua_queue *queue = hlua_check_queue(L, 1);
Aurelien DARRAGON202edb52023-07-13 10:47:33 +0200642 struct hlua_queue_wait *wait = lua_touserdata(L, 2);
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200643
644 /* new pop attempt */
Aurelien DARRAGON202edb52023-07-13 10:47:33 +0200645 if (!_hlua_queue_pop(L, queue)) {
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200646 hlua_yieldk(L, 0, 0, _hlua_queue_pop_wait, TICK_ETERNITY, 0); // wait retry
Aurelien DARRAGON202edb52023-07-13 10:47:33 +0200647 return 0; // never reached, yieldk won't return
648 }
649
650 /* remove task from waiting list */
651 MT_LIST_DELETE(&wait->entry);
652 pool_free(pool_head_hlua_queuew, wait);
653
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200654 return 1; // success
655}
656static int hlua_queue_pop_wait(lua_State *L)
657{
658 struct hlua_queue *queue = hlua_check_queue(L, 1);
659 struct hlua_queue_wait *wait;
660 struct hlua *hlua;
661
662 BUG_ON(!queue);
663
664 /* Get hlua struct, or NULL if we execute from main lua state */
665 hlua = hlua_gethlua(L);
666
667 if (!hlua || HLUA_CANT_YIELD(hlua)) {
668 luaL_error(L, "pop_wait() may only be used within task context "
669 "(requires yielding)");
670 return 0; /* not reached */
671 }
672
Aurelien DARRAGON202edb52023-07-13 10:47:33 +0200673 /* try opportunistic pop (there could already be pending items) */
674 if (_hlua_queue_pop(L, queue))
675 return 1; // success
676
677 /* no pending items, waiting required */
678
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200679 wait = pool_alloc(pool_head_hlua_queuew);
680 if (!wait) {
681 lua_pushnil(L);
682 return 1; /* memory error, return nil */
683 }
684
685 wait->task = hlua->task;
686 MT_LIST_INIT(&wait->entry);
687
688 /* add task to queue's wait list */
689 MT_LIST_TRY_APPEND(&queue->wait_tasks, &wait->entry);
690
Aurelien DARRAGON202edb52023-07-13 10:47:33 +0200691 /* push wait entry at index 2 on the stack (queue is already there) */
692 lua_pushlightuserdata(L, wait);
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200693
Aurelien DARRAGON202edb52023-07-13 10:47:33 +0200694 /* Go to waiting loop which immediately performs a new attempt to make
695 * sure we didn't miss a push during the wait entry initialization.
696 *
697 * _hlua_queue_pop_wait() won't return to us if it has to yield, which
698 * is the most likely scenario. What happens in this case is that yieldk
699 * call never returns, and instead Lua will call the continuation
700 * function after a successful resume, so the calling function will
701 * no longer be us, but Lua instead. And when the continuation function
702 * eventually returns (because it succesfully popped an item), Lua will
703 * directly give the hand back to the Lua function that called us.
704 *
705 * More info here: https://www.lua.org/manual/5.4/manual.html#4.7
706 */
707 return _hlua_queue_pop_wait(L, LUA_OK, 0);
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200708}
709
710static int hlua_queue_new(lua_State *L)
711{
712 struct hlua_queue *q;
713
714 lua_newtable(L);
715
716 /* set class metatable */
717 lua_rawgeti(L, LUA_REGISTRYINDEX, class_queue_ref);
718 lua_setmetatable(L, -2);
719
720 /* index:0 is queue userdata (c data) */
721 q = lua_newuserdata(L, sizeof(*q));
722 MT_LIST_INIT(&q->list);
723 MT_LIST_INIT(&q->wait_tasks);
724 q->size = 0;
725 lua_rawseti(L, -2, 0);
726
727 /* class methods */
728 hlua_class_function(L, "size", hlua_queue_size);
729 hlua_class_function(L, "pop", hlua_queue_pop);
730 hlua_class_function(L, "pop_wait", hlua_queue_pop_wait);
731 hlua_class_function(L, "push", hlua_queue_push);
732
733 return 1;
734}
735
736static int hlua_queue_gc(struct lua_State *L)
737{
738 struct hlua_queue *queue = hlua_check_queue(L, 1);
739 struct hlua_queue_wait *wait;
740 struct hlua_queue_item *item;
741
742 /* Purge waiting tasks (if any)
743 *
744 * It is normally not expected to have waiting tasks, except if such
745 * task has been aborted while in the middle of a queue:pop_wait()
746 * function call.
747 */
748 while ((wait = MT_LIST_POP(&queue->wait_tasks, typeof(wait), entry))) {
749 /* free the wait entry */
750 pool_free(pool_head_hlua_queuew, wait);
751 }
752
753 /* purge remaining (unconsumed) items in the queue */
754 while ((item = MT_LIST_POP(&queue->list, typeof(item), list))) {
755 /* free the queue item */
756 pool_free(pool_head_hlua_queue, item);
757 }
758
759 /* queue (userdata) will automatically be freed by lua gc */
760
761 return 0;
762}
763
764static void hlua_queue_init(lua_State *L)
765{
766 /* Creates the queue object. */
767 lua_newtable(L);
768
769 hlua_class_function(L, "__gc", hlua_queue_gc);
770
771 class_queue_ref = luaL_ref(L, LUA_REGISTRYINDEX);
772}
773
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200774int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
775{
776 lua_newtable(L);
777
778 /* Pop a class stktbl metatable and affect it to the userdata. */
779 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
780 lua_setmetatable(L, -2);
781
782 lua_pushlightuserdata(L, tbl);
783 lua_rawseti(L, -2, 0);
784 return 1;
785}
786
787static struct stktable *hlua_check_stktable(lua_State *L, int ud)
788{
789 return hlua_checkudata(L, ud, class_stktable_ref);
790}
791
792/* Extract stick table attributes into Lua table */
793int hlua_stktable_info(lua_State *L)
794{
795 struct stktable *tbl;
796 int dt;
797
798 tbl = hlua_check_stktable(L, 1);
799
800 if (!tbl->id) {
801 lua_pushnil(L);
802 return 1;
803 }
804
805 lua_newtable(L);
806
807 lua_pushstring(L, "type");
808 lua_pushstring(L, stktable_types[tbl->type].kw);
809 lua_settable(L, -3);
810
811 lua_pushstring(L, "length");
812 lua_pushinteger(L, tbl->key_size);
813 lua_settable(L, -3);
814
815 lua_pushstring(L, "size");
816 hlua_fcn_pushunsigned(L, tbl->size);
817 lua_settable(L, -3);
818
819 lua_pushstring(L, "used");
820 hlua_fcn_pushunsigned(L, tbl->current);
821 lua_settable(L, -3);
822
823 lua_pushstring(L, "nopurge");
824 lua_pushboolean(L, tbl->nopurge > 0);
825 lua_settable(L, -3);
826
827 lua_pushstring(L, "expire");
828 lua_pushinteger(L, tbl->expire);
829 lua_settable(L, -3);
830
831 /* Save data types periods (if applicable) in 'data' table */
832 lua_pushstring(L, "data");
833 lua_newtable(L);
834
835 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
836 if (tbl->data_ofs[dt] == 0)
837 continue;
838
839 lua_pushstring(L, stktable_data_types[dt].name);
840
841 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
842 lua_pushinteger(L, tbl->data_arg[dt].u);
843 else
844 lua_pushinteger(L, -1);
845
846 lua_settable(L, -3);
847 }
848
849 lua_settable(L, -3);
850
851 return 1;
852}
853
854/* Helper to get extract stick table entry into Lua table */
855static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
856{
857 int dt;
858 void *ptr;
859
860 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
861
Aurelien DARRAGONa8982412023-08-22 11:03:06 +0200862 ptr = stktable_data_ptr(t, ts, dt);
863 if (!ptr)
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200864 continue;
865
866 lua_pushstring(L, stktable_data_types[dt].name);
867
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200868 switch (stktable_data_types[dt].std_type) {
869 case STD_T_SINT:
870 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
871 break;
872 case STD_T_UINT:
873 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
874 break;
875 case STD_T_ULL:
876 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
877 break;
878 case STD_T_FRQP:
879 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
880 t->data_arg[dt].u));
881 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200882 case STD_T_DICT: {
883 struct dict_entry *de;
884 de = stktable_data_cast(ptr, std_t_dict);
885 lua_pushstring(L, de ? (char *)de->value.key : "-");
886 break;
887 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200888 }
889
890 lua_settable(L, -3);
891 }
892}
893
894/* Looks in table <t> for a sticky session matching key <key>
895 * Returns table with session data or nil
896 *
897 * The returned table always contains 'use' and 'expire' (integer) fields.
898 * For frequency/rate counters, each data entry is returned as table with
899 * 'value' and 'period' fields.
900 */
901int hlua_stktable_lookup(lua_State *L)
902{
903 struct stktable *t;
904 struct sample smp;
905 struct stktable_key *skey;
906 struct stksess *ts;
907
908 t = hlua_check_stktable(L, 1);
909 smp.data.type = SMP_T_STR;
910 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600911 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200912
913 skey = smp_to_stkey(&smp, t);
914 if (!skey) {
915 lua_pushnil(L);
916 return 1;
917 }
918
919 ts = stktable_lookup_key(t, skey);
920 if (!ts) {
921 lua_pushnil(L);
922 return 1;
923 }
924
925 lua_newtable(L);
926 lua_pushstring(L, "use");
927 lua_pushinteger(L, ts->ref_cnt - 1);
928 lua_settable(L, -3);
929
930 lua_pushstring(L, "expire");
931 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
932 lua_settable(L, -3);
933
934 hlua_stktable_entry(L, t, ts);
Willy Tarreau76642222022-10-11 12:02:50 +0200935 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200936 ts->ref_cnt--;
Willy Tarreau76642222022-10-11 12:02:50 +0200937 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200938
939 return 1;
940}
941
942struct stk_filter {
943 long long val;
944 int type;
945 int op;
946};
947
948
949/* Helper for returning errors to callers using Lua convention (nil, err) */
950static int hlua_error(lua_State *L, const char *fmt, ...) {
951 char buf[256];
952 int len;
953 va_list args;
954 va_start(args, fmt);
955 len = vsnprintf(buf, sizeof(buf), fmt, args);
956 va_end(args);
957
958 if (len < 0) {
959 ha_alert("hlua_error(): Could not write error message.\n");
960 lua_pushnil(L);
961 return 1;
962 } else if (len >= sizeof(buf))
963 ha_alert("hlua_error(): Error message was truncated.\n");
964
965 lua_pushnil(L);
966 lua_pushstring(L, buf);
967
968 return 2;
969}
970
971/* Dump the contents of stick table <t>*/
972int hlua_stktable_dump(lua_State *L)
973{
974 struct stktable *t;
975 struct ebmb_node *eb;
976 struct ebmb_node *n;
977 struct stksess *ts;
978 int type;
979 int op;
980 int dt;
981 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100982 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200983 int filter_count = 0;
984 int i;
985 int skip_entry;
986 void *ptr;
987
988 t = hlua_check_stktable(L, 1);
989 type = lua_type(L, 2);
990
991 switch (type) {
992 case LUA_TNONE:
993 case LUA_TNIL:
994 break;
995 case LUA_TTABLE:
996 lua_pushnil(L);
997 while (lua_next(L, 2) != 0) {
998 int entry_idx = 0;
999
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01001000 if (filter_count >= STKTABLE_FILTER_LEN)
1001 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001002
1003 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
1004 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
1005
1006 lua_pushnil(L);
1007 while (lua_next(L, -2) != 0) {
1008 switch (entry_idx) {
1009 case 0:
1010 if (lua_type(L, -1) != LUA_TSTRING)
1011 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
1012
1013 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
1014 if (dt < 0 || t->data_ofs[dt] == 0)
1015 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
1016 filter[filter_count].type = dt;
1017 break;
1018 case 1:
1019 if (lua_type(L, -1) != LUA_TSTRING)
1020 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
1021
1022 op = get_std_op(lua_tostring(L, -1));
1023 if (op < 0)
1024 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
1025 filter[filter_count].op = op;
1026 break;
1027 case 2:
1028 val = lua_tointeger(L, -1);
1029 filter[filter_count].val = val;
1030 filter_count++;
1031 break;
1032 default:
1033 break;
1034 }
1035 entry_idx++;
1036 lua_pop(L, 1);
1037 }
1038 lua_pop(L, 1);
1039 }
1040 break;
1041 default:
1042 return hlua_error(L, "filter table expected");
1043 }
1044
1045 lua_newtable(L);
1046
Willy Tarreau76642222022-10-11 12:02:50 +02001047 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001048 eb = ebmb_first(&t->keys);
1049 for (n = eb; n; n = ebmb_next(n)) {
1050 ts = ebmb_entry(n, struct stksess, key);
1051 if (!ts) {
Willy Tarreau76642222022-10-11 12:02:50 +02001052 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001053 return 1;
1054 }
1055 ts->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +02001056 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001057
1058 /* multi condition/value filter */
1059 skip_entry = 0;
1060 for (i = 0; i < filter_count; i++) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001061 ptr = stktable_data_ptr(t, ts, filter[i].type);
Aurelien DARRAGONa8982412023-08-22 11:03:06 +02001062 if (!ptr)
1063 continue;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001064
1065 switch (stktable_data_types[filter[i].type].std_type) {
1066 case STD_T_SINT:
1067 val = stktable_data_cast(ptr, std_t_sint);
1068 break;
1069 case STD_T_UINT:
1070 val = stktable_data_cast(ptr, std_t_uint);
1071 break;
1072 case STD_T_ULL:
1073 val = stktable_data_cast(ptr, std_t_ull);
1074 break;
1075 case STD_T_FRQP:
1076 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1077 t->data_arg[filter[i].type].u);
1078 break;
1079 default:
1080 continue;
1081 break;
1082 }
1083
1084 op = filter[i].op;
1085
1086 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
1087 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
1088 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
1089 skip_entry = 1;
1090 break;
1091 }
1092 }
1093
1094 if (skip_entry) {
Willy Tarreau76642222022-10-11 12:02:50 +02001095 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001096 ts->ref_cnt--;
1097 continue;
1098 }
1099
1100 if (t->type == SMP_T_IPV4) {
1101 char addr[INET_ADDRSTRLEN];
1102 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
1103 lua_pushstring(L, addr);
1104 } else if (t->type == SMP_T_IPV6) {
1105 char addr[INET6_ADDRSTRLEN];
1106 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
1107 lua_pushstring(L, addr);
1108 } else if (t->type == SMP_T_SINT) {
1109 lua_pushinteger(L, *ts->key.key);
1110 } else if (t->type == SMP_T_STR) {
1111 lua_pushstring(L, (const char *)ts->key.key);
1112 } else {
1113 return hlua_error(L, "Unsupported stick table key type");
1114 }
1115
1116 lua_newtable(L);
1117 hlua_stktable_entry(L, t, ts);
1118 lua_settable(L, -3);
Willy Tarreau76642222022-10-11 12:02:50 +02001119 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001120 ts->ref_cnt--;
1121 }
Willy Tarreau76642222022-10-11 12:02:50 +02001122 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001123
1124 return 1;
1125}
1126
Thierry Fournierff480422016-02-25 08:36:46 +01001127int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
1128{
1129 lua_newtable(L);
1130
1131 /* Pop a class sesison metatable and affect it to the userdata. */
1132 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
1133 lua_setmetatable(L, -2);
1134
1135 lua_pushlightuserdata(L, lst);
1136 lua_rawseti(L, -2, 0);
1137 return 1;
1138}
1139
1140static struct listener *hlua_check_listener(lua_State *L, int ud)
1141{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001142 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +01001143}
1144
1145int hlua_listener_get_stats(lua_State *L)
1146{
1147 struct listener *li;
1148 int i;
1149
1150 li = hlua_check_listener(L, 1);
1151
Willy Tarreauc95bad52016-12-22 00:13:31 +01001152 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +01001153 lua_pushnil(L);
1154 return 1;
1155 }
1156
William Dauchy655e14e2021-02-14 23:22:54 +01001157 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
1158 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +01001159
1160 lua_newtable(L);
1161 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001162 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +01001163 hlua_fcn_pushfield(L, &stats[i]);
1164 lua_settable(L, -3);
1165 }
1166 return 1;
1167
1168}
1169
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001170int hlua_server_gc(lua_State *L)
1171{
1172 struct server *srv = hlua_checkudata(L, 1, class_server_ref);
1173
1174 srv_drop(srv); /* srv_drop allows NULL srv */
1175 return 0;
1176}
1177
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001178static struct server *hlua_check_server(lua_State *L, int ud)
1179{
Amaury Denoyelle86f37072021-08-23 14:06:31 +02001180 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001181 if (srv->flags & SRV_F_DELETED) {
1182 return NULL;
1183 }
Amaury Denoyelle86f37072021-08-23 14:06:31 +02001184 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001185}
1186
1187int hlua_server_get_stats(lua_State *L)
1188{
1189 struct server *srv;
1190 int i;
1191
1192 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001193 if (srv == NULL) {
1194 lua_pushnil(L);
1195 return 1;
1196 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001197
1198 if (!srv->proxy) {
1199 lua_pushnil(L);
1200 return 1;
1201 }
1202
William Dauchyd3a9a492021-01-25 17:29:03 +01001203 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
1204 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001205
1206 lua_newtable(L);
1207 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001208 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001209 hlua_fcn_pushfield(L, &stats[i]);
1210 lua_settable(L, -3);
1211 }
1212 return 1;
1213
1214}
1215
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001216int hlua_server_get_proxy(lua_State *L)
1217{
1218 struct server *srv;
1219
1220 srv = hlua_check_server(L, 1);
1221 if (srv == NULL) {
1222 lua_pushnil(L);
1223 return 1;
1224 }
1225
1226 if (!srv->proxy) {
1227 lua_pushnil(L);
1228 return 1;
1229 }
1230
1231 hlua_fcn_new_proxy(L, srv->proxy);
1232 return 1;
1233}
1234
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001235int hlua_server_get_addr(lua_State *L)
1236{
1237 struct server *srv;
1238 char addr[INET6_ADDRSTRLEN];
1239 luaL_Buffer b;
1240
1241 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001242 if (srv == NULL) {
1243 lua_pushnil(L);
1244 return 1;
1245 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001246
1247 luaL_buffinit(L, &b);
1248
1249 switch (srv->addr.ss_family) {
1250 case AF_INET:
1251 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
1252 addr, INET_ADDRSTRLEN);
1253 luaL_addstring(&b, addr);
1254 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -04001255 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001256 luaL_addstring(&b, addr);
1257 break;
1258 case AF_INET6:
1259 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -04001260 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001261 luaL_addstring(&b, addr);
1262 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -04001263 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001264 luaL_addstring(&b, addr);
1265 break;
1266 case AF_UNIX:
1267 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
1268 break;
1269 default:
1270 luaL_addstring(&b, "<unknown>");
1271 break;
1272 }
1273
1274 luaL_pushresult(&b);
1275 return 1;
1276}
1277
Thierry Fournierb0467732022-10-07 12:07:24 +02001278int hlua_server_get_puid(lua_State *L)
1279{
1280 struct server *srv;
1281 char buffer[12];
1282
1283 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001284 if (srv == NULL) {
1285 lua_pushnil(L);
1286 return 1;
1287 }
Thierry Fournierb0467732022-10-07 12:07:24 +02001288
1289 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
1290 lua_pushstring(L, buffer);
1291 return 1;
1292}
1293
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001294int hlua_server_get_rid(lua_State *L)
1295{
1296 struct server *srv;
1297 char buffer[12];
1298
1299 srv = hlua_check_server(L, 1);
1300 if (srv == NULL) {
1301 lua_pushnil(L);
1302 return 1;
1303 }
1304
1305 snprintf(buffer, sizeof(buffer), "%d", srv->rid);
1306 lua_pushstring(L, buffer);
1307 return 1;
1308}
1309
Thierry Fournierb0467732022-10-07 12:07:24 +02001310int hlua_server_get_name(lua_State *L)
1311{
1312 struct server *srv;
1313
1314 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001315 if (srv == NULL) {
1316 lua_pushnil(L);
1317 return 1;
1318 }
1319
Thierry Fournierb0467732022-10-07 12:07:24 +02001320 lua_pushstring(L, srv->id);
1321 return 1;
1322}
1323
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001324/* __index metamethod for server class
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001325 * support for additional keys that are missing from the main table
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001326 * stack:1 = table (server class), stack:2 = requested key
1327 * Returns 1 if key is supported
1328 * else returns 0 to make lua return NIL value to the caller
1329 */
1330static int hlua_server_index(struct lua_State *L)
1331{
1332 const char *key = lua_tostring(L, 2);
1333
1334 if (!strcmp(key, "name")) {
1335 if (ONLY_ONCE())
1336 ha_warning("hlua: use of server 'name' attribute is deprecated and will eventually be removed, please use get_name() function instead: %s\n", hlua_traceback(L, ", "));
1337 lua_pushvalue(L, 1);
1338 hlua_server_get_name(L);
1339 return 1;
1340 }
1341 if (!strcmp(key, "puid")) {
1342 if (ONLY_ONCE())
1343 ha_warning("hlua: use of server 'puid' attribute is deprecated and will eventually be removed, please use get_puid() function instead: %s\n", hlua_traceback(L, ", "));
1344 lua_pushvalue(L, 1);
1345 hlua_server_get_puid(L);
1346 return 1;
1347 }
1348 /* unknown attribute */
1349 return 0;
1350}
1351
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001352int hlua_server_is_draining(lua_State *L)
1353{
1354 struct server *srv;
1355
1356 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001357 if (srv == NULL) {
1358 lua_pushnil(L);
1359 return 1;
1360 }
1361
Aurelien DARRAGON862a0fe2023-03-29 10:46:36 +02001362 lua_pushboolean(L, server_is_draining(srv));
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001363 return 1;
1364}
1365
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001366int hlua_server_is_backup(lua_State *L)
1367{
1368 struct server *srv;
1369
1370 srv = hlua_check_server(L, 1);
1371 if (srv == NULL) {
1372 lua_pushnil(L);
1373 return 1;
1374 }
1375
1376 lua_pushboolean(L, (srv->flags & SRV_F_BACKUP));
1377 return 1;
1378}
1379
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001380int hlua_server_is_dynamic(lua_State *L)
1381{
1382 struct server *srv;
1383
1384 srv = hlua_check_server(L, 1);
1385 if (srv == NULL) {
1386 lua_pushnil(L);
1387 return 1;
1388 }
1389
1390 lua_pushboolean(L, (srv->flags & SRV_F_DYNAMIC));
1391 return 1;
1392}
1393
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001394int hlua_server_get_cur_sess(lua_State *L)
1395{
1396 struct server *srv;
1397
1398 srv = hlua_check_server(L, 1);
1399 if (srv == NULL) {
1400 lua_pushnil(L);
1401 return 1;
1402 }
1403
1404 lua_pushinteger(L, srv->cur_sess);
1405 return 1;
1406}
1407
1408int hlua_server_get_pend_conn(lua_State *L)
1409{
1410 struct server *srv;
1411
1412 srv = hlua_check_server(L, 1);
1413 if (srv == NULL) {
1414 lua_pushnil(L);
1415 return 1;
1416 }
1417
1418 lua_pushinteger(L, srv->queue.length);
1419 return 1;
1420}
1421
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001422int hlua_server_set_maxconn(lua_State *L)
1423{
1424 struct server *srv;
1425 const char *maxconn;
1426 const char *err;
1427
1428 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001429 if (srv == NULL) {
1430 lua_pushnil(L);
1431 return 1;
1432 }
1433
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001434 maxconn = luaL_checkstring(L, 2);
1435
1436 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1437 err = server_parse_maxconn_change_request(srv, maxconn);
1438 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1439 if (!err)
1440 lua_pushnil(L);
1441 else
1442 hlua_pushstrippedstring(L, err);
1443 return 1;
1444}
1445
1446int hlua_server_get_maxconn(lua_State *L)
1447{
1448 struct server *srv;
1449
1450 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001451 if (srv == NULL) {
1452 lua_pushnil(L);
1453 return 1;
1454 }
1455
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001456 lua_pushinteger(L, srv->maxconn);
1457 return 1;
1458}
1459
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001460int hlua_server_set_weight(lua_State *L)
1461{
1462 struct server *srv;
1463 const char *weight;
1464 const char *err;
1465
1466 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001467 if (srv == NULL) {
1468 lua_pushnil(L);
1469 return 1;
1470 }
1471
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001472 weight = luaL_checkstring(L, 2);
1473
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001474 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001475 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001476 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001477 if (!err)
1478 lua_pushnil(L);
1479 else
1480 hlua_pushstrippedstring(L, err);
1481 return 1;
1482}
1483
1484int hlua_server_get_weight(lua_State *L)
1485{
1486 struct server *srv;
1487
1488 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001489 if (srv == NULL) {
1490 lua_pushnil(L);
1491 return 1;
1492 }
1493
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001494 lua_pushinteger(L, srv->uweight);
1495 return 1;
1496}
1497
1498int hlua_server_set_addr(lua_State *L)
1499{
1500 struct server *srv;
1501 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001502 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001503 const char *err;
1504
1505 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001506 if (srv == NULL) {
1507 lua_pushnil(L);
1508 return 1;
1509 }
1510
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001511 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001512 if (lua_gettop(L) >= 3)
1513 port = luaL_checkstring(L, 3);
1514 else
1515 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001516
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001517 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001518 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001519 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001520 if (!err)
1521 lua_pushnil(L);
1522 else
1523 hlua_pushstrippedstring(L, err);
1524 return 1;
1525}
1526
1527int hlua_server_shut_sess(lua_State *L)
1528{
1529 struct server *srv;
1530
1531 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001532 if (srv == NULL) {
1533 return 0;
1534 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001535 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001536 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001537 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001538 return 0;
1539}
1540
1541int hlua_server_set_drain(lua_State *L)
1542{
1543 struct server *srv;
1544
1545 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001546 if (srv == NULL) {
1547 return 0;
1548 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001549 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001550 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001551 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001552 return 0;
1553}
1554
1555int hlua_server_set_maint(lua_State *L)
1556{
1557 struct server *srv;
1558
1559 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001560 if (srv == NULL) {
1561 return 0;
1562 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001563 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001564 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001565 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001566 return 0;
1567}
1568
1569int hlua_server_set_ready(lua_State *L)
1570{
1571 struct server *srv;
1572
1573 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001574 if (srv == NULL) {
1575 return 0;
1576 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001577 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001578 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001579 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001580 return 0;
1581}
1582
1583int hlua_server_check_enable(lua_State *L)
1584{
1585 struct server *sv;
1586
1587 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001588 if (sv == NULL) {
1589 return 0;
1590 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001591 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001592 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001593 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001594 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001595 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001596 return 0;
1597}
1598
1599int hlua_server_check_disable(lua_State *L)
1600{
1601 struct server *sv;
1602
1603 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001604 if (sv == NULL) {
1605 return 0;
1606 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001607 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001608 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001609 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001610 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001611 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001612 return 0;
1613}
1614
1615int hlua_server_check_force_up(lua_State *L)
1616{
1617 struct server *sv;
1618
1619 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001620 if (sv == NULL) {
1621 return 0;
1622 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001623 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001624 if (!(sv->track)) {
1625 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001626 srv_set_running(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001627 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001628 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001629 return 0;
1630}
1631
1632int hlua_server_check_force_nolb(lua_State *L)
1633{
1634 struct server *sv;
1635
1636 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001637 if (sv == NULL) {
1638 return 0;
1639 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001640 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001641 if (!(sv->track)) {
1642 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001643 srv_set_stopping(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001644 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001645 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001646 return 0;
1647}
1648
1649int hlua_server_check_force_down(lua_State *L)
1650{
1651 struct server *sv;
1652
1653 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001654 if (sv == NULL) {
1655 return 0;
1656 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001657 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001658 if (!(sv->track)) {
1659 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001660 srv_set_stopped(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001661 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001662 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001663 return 0;
1664}
1665
1666int hlua_server_agent_enable(lua_State *L)
1667{
1668 struct server *sv;
1669
1670 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001671 if (sv == NULL) {
1672 return 0;
1673 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001674 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001675 if (sv->agent.state & CHK_ST_CONFIGURED) {
1676 sv->agent.state |= CHK_ST_ENABLED;
1677 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001678 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001679 return 0;
1680}
1681
1682int hlua_server_agent_disable(lua_State *L)
1683{
1684 struct server *sv;
1685
1686 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001687 if (sv == NULL) {
1688 return 0;
1689 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001690 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001691 if (sv->agent.state & CHK_ST_CONFIGURED) {
1692 sv->agent.state &= ~CHK_ST_ENABLED;
1693 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001694 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001695 return 0;
1696}
1697
1698int hlua_server_agent_force_up(lua_State *L)
1699{
1700 struct server *sv;
1701
1702 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001703 if (sv == NULL) {
1704 return 0;
1705 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001706 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001707 if (sv->agent.state & CHK_ST_ENABLED) {
1708 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001709 srv_set_running(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001710 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001711 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001712 return 0;
1713}
1714
1715int hlua_server_agent_force_down(lua_State *L)
1716{
1717 struct server *sv;
1718
1719 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001720 if (sv == NULL) {
1721 return 0;
1722 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001723 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001724 if (sv->agent.state & CHK_ST_ENABLED) {
1725 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001726 srv_set_stopped(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001727 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001728 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001729 return 0;
1730}
1731
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001732/* returns the tracked server, if any */
1733int hlua_server_tracking(lua_State *L)
1734{
1735 struct server *sv;
1736 struct server *tracked;
1737
1738 sv = hlua_check_server(L, 1);
1739 if (sv == NULL) {
1740 return 0;
1741 }
1742
1743 tracked = sv->track;
1744 if (tracked == NULL)
1745 lua_pushnil(L);
1746 else
1747 hlua_fcn_new_server(L, tracked);
1748
1749 return 1;
1750}
1751
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001752/* returns an array of servers tracking the current server */
1753int hlua_server_get_trackers(lua_State *L)
1754{
1755 struct server *sv;
1756 struct server *cur_tracker;
1757 int index;
1758
1759 sv = hlua_check_server(L, 1);
1760 if (sv == NULL) {
1761 return 0;
1762 }
1763
1764 lua_newtable(L);
1765 cur_tracker = sv->trackers;
1766 for (index = 1; cur_tracker; cur_tracker = cur_tracker->tracknext, index++) {
1767 if (!lua_checkstack(L, 5))
1768 luaL_error(L, "Lua out of memory error.");
1769 hlua_fcn_new_server(L, cur_tracker);
1770 /* array index starts at 1 in Lua */
1771 lua_rawseti(L, -2, index);
1772 }
1773 return 1;
1774}
1775
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001776/* hlua_event_sub wrapper for per-server subscription:
1777 *
1778 * hlua_event_sub() is called with sv->e_subs subscription list and
1779 * lua arguments are passed as-is (skipping the first argument which
1780 * is the server ctx)
1781 */
1782int hlua_server_event_sub(lua_State *L)
1783{
1784 struct server *sv;
1785
1786 sv = hlua_check_server(L, 1);
1787 if (sv == NULL) {
1788 return 0;
1789 }
1790 /* remove first argument from the stack (server) */
1791 lua_remove(L, 1);
1792
1793 /* try to subscribe within server's subscription list */
1794 return hlua_event_sub(L, &sv->e_subs);
1795}
1796
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001797int hlua_fcn_new_server(lua_State *L, struct server *srv)
1798{
1799 lua_newtable(L);
1800
1801 /* Pop a class server metatable and affect it to the userdata. */
1802 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
1803 lua_setmetatable(L, -2);
1804
1805 lua_pushlightuserdata(L, srv);
1806 lua_rawseti(L, -2, 0);
1807
1808 /* userdata is affected: increment server refcount */
1809 srv_take(srv);
1810
1811 /* set public methods */
1812 hlua_class_function(L, "get_name", hlua_server_get_name);
1813 hlua_class_function(L, "get_puid", hlua_server_get_puid);
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001814 hlua_class_function(L, "get_rid", hlua_server_get_rid);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001815 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001816 hlua_class_function(L, "is_backup", hlua_server_is_backup);
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001817 hlua_class_function(L, "is_dynamic", hlua_server_is_dynamic);
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001818 hlua_class_function(L, "get_cur_sess", hlua_server_get_cur_sess);
1819 hlua_class_function(L, "get_pend_conn", hlua_server_get_pend_conn);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001820 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1821 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
1822 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1823 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1824 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1825 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1826 hlua_class_function(L, "get_stats", hlua_server_get_stats);
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001827 hlua_class_function(L, "get_proxy", hlua_server_get_proxy);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001828 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1829 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1830 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1831 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1832 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1833 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1834 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1835 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1836 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1837 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1838 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1839 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1840 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001841 hlua_class_function(L, "tracking", hlua_server_tracking);
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001842 hlua_class_function(L, "get_trackers", hlua_server_get_trackers);
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001843 hlua_class_function(L, "event_sub", hlua_server_event_sub);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001844
1845 return 1;
1846}
1847
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001848static struct hlua_server_list *hlua_check_server_list(lua_State *L, int ud)
1849{
1850 return hlua_checkudata(L, ud, class_server_list_ref);
1851}
1852
1853/* does nothing and returns 0, only prevents insertions in the
1854 * table which represents the list of servers
1855 */
1856int hlua_listable_servers_newindex(lua_State *L) {
1857 return 0;
1858}
1859
1860/* first arg is the table (struct hlua_server_list * in metadata)
1861 * second arg is the required index
1862 */
1863int hlua_listable_servers_index(lua_State *L)
Thierry Fournierf61aa632016-02-19 20:56:00 +01001864{
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001865 struct hlua_server_list *hlua_srv;
1866 const char *name;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001867 struct server *srv;
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001868
1869 hlua_srv = hlua_check_server_list(L, 1);
1870 name = luaL_checkstring(L, 2);
1871
1872 /* Perform a server lookup in px list */
1873 srv = server_find_by_name(hlua_srv->px, name);
1874 if (srv == NULL) {
1875 lua_pushnil(L);
1876 return 1;
1877 }
1878
1879 hlua_fcn_new_server(L, srv);
1880 return 1;
1881}
1882
1883/* iterator must return key as string and value as server
1884 * object, if we reach end of list, it returns nil.
1885 * The context knows the last returned server. if the
1886 * context contains srv == NULL, we start enumeration.
1887 * Then, use 'srv->next' ptr to iterate through the list
1888 */
1889int hlua_listable_servers_pairs_iterator(lua_State *L)
1890{
1891 int context_index;
1892 struct hlua_server_list_iterator_context *ctx;
1893
1894 context_index = lua_upvalueindex(1);
1895 ctx = lua_touserdata(L, context_index);
1896
1897 if (ctx->cur == NULL) {
1898 /* First iteration, initialize list on the first server */
1899 ctx->cur = ctx->px->srv;
1900 } else {
1901
1902 /* Next server (next ptr is always valid, even if current
1903 * server has the SRV_F_DELETED flag set)
1904 */
1905 ctx->cur = ctx->cur->next;
1906 }
1907
1908 /* next server is null, end of iteration */
1909 if (ctx->cur == NULL) {
1910 lua_pushnil(L);
1911 return 1;
1912 }
1913
1914 lua_pushstring(L, ctx->cur->id);
1915 hlua_fcn_new_server(L, ctx->cur);
1916 return 2;
1917}
1918
1919/* init the iterator context, return iterator function
1920 * with context as closure. The only argument is a
1921 * server list object.
1922 */
1923int hlua_listable_servers_pairs(lua_State *L)
1924{
1925 struct hlua_server_list_iterator_context *ctx;
1926 struct hlua_server_list *hlua_srv_list;
1927
1928 hlua_srv_list = hlua_check_server_list(L, 1);
1929
1930 ctx = lua_newuserdata(L, sizeof(*ctx));
1931 ctx->px = hlua_srv_list->px;
1932 ctx->cur = NULL;
1933
1934 lua_pushcclosure(L, hlua_listable_servers_pairs_iterator, 1);
1935 return 1;
1936}
1937
1938void hlua_listable_servers(lua_State *L, struct proxy *px)
1939{
1940 struct hlua_server_list *list;
1941
1942 lua_newtable(L);
1943 list = lua_newuserdata(L, sizeof(*list));
1944 list->px = px;
1945 lua_rawseti(L, -2, 0);
1946 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_list_ref);
1947 lua_setmetatable(L, -2);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001948}
1949
1950static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1951{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001952 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001953}
1954
Thierry Fournierb0467732022-10-07 12:07:24 +02001955int hlua_proxy_get_name(lua_State *L)
1956{
1957 struct proxy *px;
1958
1959 px = hlua_check_proxy(L, 1);
1960 lua_pushstring(L, px->id);
1961 return 1;
1962}
1963
1964int hlua_proxy_get_uuid(lua_State *L)
1965{
1966 struct proxy *px;
1967 char buffer[17];
1968
1969 px = hlua_check_proxy(L, 1);
1970 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1971 lua_pushstring(L, buffer);
1972 return 1;
1973}
1974
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001975/* __index metamethod for proxy class
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001976 * support for additional keys that are missing from the main table
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001977 * stack:1 = table (proxy class), stack:2 = requested key
1978 * Returns 1 if key is supported
1979 * else returns 0 to make lua return NIL value to the caller
1980 */
1981static int hlua_proxy_index(struct lua_State *L)
1982{
1983 const char *key = lua_tostring(L, 2);
1984
1985 if (!strcmp(key, "name")) {
1986 if (ONLY_ONCE())
1987 ha_warning("hlua: use of proxy 'name' attribute is deprecated and will eventually be removed, please use get_name() function instead: %s\n", hlua_traceback(L, ", "));
1988 lua_pushvalue(L, 1);
1989 hlua_proxy_get_name(L);
1990 return 1;
1991 }
1992 if (!strcmp(key, "uuid")) {
1993 if (ONLY_ONCE())
1994 ha_warning("hlua: use of proxy 'uuid' attribute is deprecated and will eventually be removed, please use get_uuid() function instead: %s\n", hlua_traceback(L, ", "));
1995 lua_pushvalue(L, 1);
1996 hlua_proxy_get_uuid(L);
1997 return 1;
1998 }
1999 /* unknown attribute */
2000 return 0;
2001}
2002
Thierry Fournierf61aa632016-02-19 20:56:00 +01002003int hlua_proxy_pause(lua_State *L)
2004{
2005 struct proxy *px;
2006
2007 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002008 /* safe to call without PROXY_LOCK - pause_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01002009 pause_proxy(px);
2010 return 0;
2011}
2012
2013int hlua_proxy_resume(lua_State *L)
2014{
2015 struct proxy *px;
2016
2017 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002018 /* safe to call without PROXY_LOCK - resume_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01002019 resume_proxy(px);
2020 return 0;
2021}
2022
2023int hlua_proxy_stop(lua_State *L)
2024{
2025 struct proxy *px;
2026
2027 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002028 /* safe to call without PROXY_LOCK - stop_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01002029 stop_proxy(px);
2030 return 0;
2031}
2032
2033int hlua_proxy_get_cap(lua_State *L)
2034{
2035 struct proxy *px;
2036 const char *str;
2037
2038 px = hlua_check_proxy(L, 1);
2039 str = proxy_cap_str(px->cap);
2040 lua_pushstring(L, str);
2041 return 1;
2042}
2043
2044int hlua_proxy_get_stats(lua_State *L)
2045{
2046 struct proxy *px;
2047 int i;
2048
2049 px = hlua_check_proxy(L, 1);
2050 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01002051 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002052 else
William Dauchy0ef54392021-01-17 18:27:45 +01002053 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002054 lua_newtable(L);
2055 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02002056 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002057 hlua_fcn_pushfield(L, &stats[i]);
2058 lua_settable(L, -3);
2059 }
2060 return 1;
2061}
2062
2063int hlua_proxy_get_mode(lua_State *L)
2064{
2065 struct proxy *px;
2066 const char *str;
2067
2068 px = hlua_check_proxy(L, 1);
2069 str = proxy_mode_str(px->mode);
2070 lua_pushstring(L, str);
2071 return 1;
2072}
2073
2074int hlua_proxy_shut_bcksess(lua_State *L)
2075{
2076 struct proxy *px;
2077
2078 px = hlua_check_proxy(L, 1);
2079 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
2080 return 0;
2081}
2082
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02002083int hlua_proxy_get_srv_act(lua_State *L)
2084{
2085 struct proxy *px;
2086
2087 px = hlua_check_proxy(L, 1);
2088 lua_pushinteger(L, px->srv_act);
2089 return 1;
2090}
2091
2092int hlua_proxy_get_srv_bck(lua_State *L)
2093{
2094 struct proxy *px;
2095
2096 px = hlua_check_proxy(L, 1);
2097 lua_pushinteger(L, px->srv_bck);
2098 return 1;
2099}
2100
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02002101/* Get mailers config info, used to implement email alert sending
2102 * according to mailers config from lua.
2103 */
2104int hlua_proxy_get_mailers(lua_State *L)
2105{
2106 struct proxy *px;
2107 int it;
2108 struct mailer *mailer;
2109
2110 px = hlua_check_proxy(L, 1);
2111
2112 if (!px->email_alert.mailers.m)
2113 return 0; /* email-alert mailers not found on proxy */
2114
2115 lua_newtable(L);
2116
2117 /* option log-health-checks */
2118 lua_pushstring(L, "track_server_health");
2119 lua_pushboolean(L, (px->options2 & PR_O2_LOGHCHKS));
2120 lua_settable(L, -3);
2121
2122 /* email-alert level */
2123 lua_pushstring(L, "log_level");
2124 lua_pushinteger(L, px->email_alert.level);
2125 lua_settable(L, -3);
2126
2127 /* email-alert mailers */
2128 lua_pushstring(L, "mailservers");
2129 lua_newtable(L);
2130 for (it = 0, mailer = px->email_alert.mailers.m->mailer_list;
2131 it < px->email_alert.mailers.m->count; it++, mailer = mailer->next) {
2132 char *srv_address;
2133
2134 lua_pushstring(L, mailer->id);
2135
2136 /* For now, we depend on mailer->addr to restore mailer's address which
2137 * was converted using str2sa_range() on startup.
2138 *
2139 * FIXME?:
2140 * It could be a good idea to pass the raw address (unparsed) to allow fqdn
2141 * to be resolved at runtime, unless we consider this as a pure legacy mode
2142 * and mailers config support is going to be removed in the future?
2143 */
2144 srv_address = sa2str(&mailer->addr, get_host_port(&mailer->addr), 0);
2145 if (srv_address) {
2146 lua_pushstring(L, srv_address);
2147 ha_free(&srv_address);
2148 lua_settable(L, -3);
2149 }
2150 }
2151 lua_settable(L, -3);
2152
Aurelien DARRAGON2b8f7ab2023-07-07 16:55:43 +02002153 /* mailers timeout (from mailers section) */
2154 lua_pushstring(L, "mailservers_timeout");
2155 lua_pushinteger(L, px->email_alert.mailers.m->timeout.mail);
2156 lua_settable(L, -3);
2157
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02002158 /* email-alert myhostname */
2159 lua_pushstring(L, "smtp_hostname");
2160 lua_pushstring(L, px->email_alert.myhostname);
2161 lua_settable(L, -3);
2162
2163 /* email-alert from */
2164 lua_pushstring(L, "smtp_from");
2165 lua_pushstring(L, px->email_alert.from);
2166 lua_settable(L, -3);
2167
2168 /* email-alert to */
2169 lua_pushstring(L, "smtp_to");
2170 lua_pushstring(L, px->email_alert.to);
2171 lua_settable(L, -3);
2172
2173 return 1;
2174}
2175
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002176int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
2177{
2178 struct listener *lst;
2179 int lid;
2180 char buffer[17];
2181
2182 lua_newtable(L);
2183
2184 /* Pop a class proxy metatable and affect it to the userdata. */
2185 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
2186 lua_setmetatable(L, -2);
2187
2188 lua_pushlightuserdata(L, px);
2189 lua_rawseti(L, -2, 0);
2190
2191 /* set public methods */
2192 hlua_class_function(L, "get_name", hlua_proxy_get_name);
2193 hlua_class_function(L, "get_uuid", hlua_proxy_get_uuid);
2194 hlua_class_function(L, "pause", hlua_proxy_pause);
2195 hlua_class_function(L, "resume", hlua_proxy_resume);
2196 hlua_class_function(L, "stop", hlua_proxy_stop);
2197 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
2198 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
2199 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02002200 hlua_class_function(L, "get_srv_act", hlua_proxy_get_srv_act);
2201 hlua_class_function(L, "get_srv_bck", hlua_proxy_get_srv_bck);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002202 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02002203 hlua_class_function(L, "get_mailers", hlua_proxy_get_mailers);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002204
2205 /* Browse and register servers. */
2206 lua_pushstring(L, "servers");
2207 hlua_listable_servers(L, px);
2208 lua_settable(L, -3);
2209
2210 /* Browse and register listeners. */
2211 lua_pushstring(L, "listeners");
2212 lua_newtable(L);
2213 lid = 1;
2214 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
2215 if (lst->name)
2216 lua_pushstring(L, lst->name);
2217 else {
2218 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
2219 lid++;
2220 lua_pushstring(L, buffer);
2221 }
2222 hlua_fcn_new_listener(L, lst);
2223 lua_settable(L, -3);
2224 }
2225 lua_settable(L, -3);
2226
2227 if (px->table && px->table->id) {
2228 lua_pushstring(L, "stktable");
2229 hlua_fcn_new_stktable(L, px->table);
2230 lua_settable(L, -3);
2231 }
2232
2233 return 1;
2234}
2235
Thierry Fournier467913c2022-09-30 11:03:38 +02002236static struct hlua_proxy_list *hlua_check_proxy_list(lua_State *L, int ud)
Thierry Fournier3d4a6752016-02-19 20:53:30 +01002237{
Thierry Fournier467913c2022-09-30 11:03:38 +02002238 return hlua_checkudata(L, ud, class_proxy_list_ref);
2239}
Thierry Fournierf61aa632016-02-19 20:56:00 +01002240
Thierry Fournier467913c2022-09-30 11:03:38 +02002241/* does nothing and returns 0, only prevents insertions in the
2242 * table which represent list of proxies
2243 */
2244int hlua_listable_proxies_newindex(lua_State *L) {
2245 return 0;
2246}
Thierry Fournierf61aa632016-02-19 20:56:00 +01002247
Thierry Fournier467913c2022-09-30 11:03:38 +02002248/* first arg is the table (struct hlua_proxy_list * in metadata)
2249 * second arg is the required index
2250 */
2251int hlua_listable_proxies_index(lua_State *L)
2252{
2253 struct hlua_proxy_list *hlua_px;
2254 const char *name;
2255 struct proxy *px;
Thierry Fournierf61aa632016-02-19 20:56:00 +01002256
Thierry Fournier467913c2022-09-30 11:03:38 +02002257 hlua_px = hlua_check_proxy_list(L, 1);
2258 name = luaL_checkstring(L, 2);
2259
2260 px = NULL;
2261 if (hlua_px->capabilities & PR_CAP_FE) {
2262 px = proxy_find_by_name(name, PR_CAP_FE, 0);
2263 }
2264 if (!px && hlua_px->capabilities & PR_CAP_BE) {
2265 px = proxy_find_by_name(name, PR_CAP_BE, 0);
2266 }
2267 if (px == NULL) {
2268 lua_pushnil(L);
2269 return 1;
Thierry Fournierf61aa632016-02-19 20:56:00 +01002270 }
2271
Thierry Fournier467913c2022-09-30 11:03:38 +02002272 hlua_fcn_new_proxy(L, px);
2273 return 1;
2274}
Thierry Fournierf61aa632016-02-19 20:56:00 +01002275
Thierry Fournier467913c2022-09-30 11:03:38 +02002276static inline int hlua_listable_proxies_match(struct proxy *px, char cap) {
2277 return ((px->cap & cap) && !(px->cap & (PR_CAP_DEF | PR_CAP_INT)));
2278}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002279
Thierry Fournier467913c2022-09-30 11:03:38 +02002280/* iterator must return key as string and value as proxy
2281 * object, if we reach end of list, it returns nil
2282 */
2283int hlua_listable_proxies_pairs_iterator(lua_State *L)
2284{
2285 int context_index;
2286 struct hlua_proxy_list_iterator_context *ctx;
2287
2288 context_index = lua_upvalueindex(1);
2289 ctx = lua_touserdata(L, context_index);
2290
2291 if (ctx->next == NULL) {
2292 lua_pushnil(L);
2293 return 1;
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002294 }
2295
Thierry Fournier467913c2022-09-30 11:03:38 +02002296 lua_pushstring(L, ctx->next->id);
2297 hlua_fcn_new_proxy(L, ctx->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002298
Thierry Fournier467913c2022-09-30 11:03:38 +02002299 for (ctx->next = ctx->next->next;
2300 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
2301 ctx->next = ctx->next->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002302
Thierry Fournier467913c2022-09-30 11:03:38 +02002303 return 2;
2304}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002305
Thierry Fournier467913c2022-09-30 11:03:38 +02002306/* init the iterator context, return iterator function
2307 * with context as closure. The only argument is a
2308 * proxy object.
2309 */
2310int hlua_listable_proxies_pairs(lua_State *L)
2311{
2312 struct hlua_proxy_list_iterator_context *ctx;
2313 struct hlua_proxy_list *hlua_px;
2314
2315 hlua_px = hlua_check_proxy_list(L, 1);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002316
Thierry Fournier467913c2022-09-30 11:03:38 +02002317 ctx = lua_newuserdata(L, sizeof(*ctx));
2318
2319 ctx->capabilities = hlua_px->capabilities;
2320 for (ctx->next = proxies_list;
2321 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
2322 ctx->next = ctx->next->next);
2323 lua_pushcclosure(L, hlua_listable_proxies_pairs_iterator, 1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01002324 return 1;
2325}
2326
Thierry Fournier467913c2022-09-30 11:03:38 +02002327void hlua_listable_proxies(lua_State *L, char capabilities)
2328{
2329 struct hlua_proxy_list *list;
2330
2331 lua_newtable(L);
2332 list = lua_newuserdata(L, sizeof(*list));
2333 list->capabilities = capabilities;
2334 lua_rawseti(L, -2, 0);
2335 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_list_ref);
2336 lua_setmetatable(L, -2);
2337}
2338
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01002339int hlua_event_sub_unsub(lua_State *L)
2340{
2341 struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref);
2342
2343 BUG_ON(!sub);
2344 event_hdl_take(sub); /* keep a reference on sub until the item is GCed */
2345 event_hdl_unsubscribe(sub); /* will automatically call event_hdl_drop() */
2346 return 0;
2347}
2348
2349int hlua_event_sub_gc(lua_State *L)
2350{
2351 struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref);
2352
2353 BUG_ON(!sub);
2354 event_hdl_drop(sub); /* final drop of the reference */
2355 return 0;
2356}
2357
2358int hlua_fcn_new_event_sub(lua_State *L, struct event_hdl_sub *sub)
2359{
2360 lua_newtable(L);
2361
2362 /* Pop a class event_sub metatable and affect it to the userdata. */
2363 lua_rawgeti(L, LUA_REGISTRYINDEX, class_event_sub_ref);
2364 lua_setmetatable(L, -2);
2365
2366 lua_pushlightuserdata(L, sub);
2367 lua_rawseti(L, -2, 0);
2368
2369 /* userdata is affected: increment sub refcount */
2370 event_hdl_take(sub);
2371
2372 /* set public methods */
2373 hlua_class_function(L, "unsub", hlua_event_sub_unsub);
2374
2375 return 1;
2376}
2377
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002378/* This Lua function take a string, a list of separators.
2379 * It tokenize the input string using the list of separators
2380 * as separator.
2381 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05002382 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002383 */
2384int hlua_tokenize(lua_State *L)
2385{
2386 const char *str;
2387 const char *sep;
2388 int index;
2389 const char *token;
2390 const char *p;
2391 const char *c;
2392 int ignore_empty;
2393
2394 ignore_empty = 0;
2395
2396 str = luaL_checkstring(L, 1);
2397 sep = luaL_checkstring(L, 2);
2398 if (lua_gettop(L) == 3)
2399 ignore_empty = hlua_checkboolean(L, 3);
2400
2401 lua_newtable(L);
2402 index = 1;
2403 token = str;
2404 p = str;
2405 while(1) {
2406 for (c = sep; *c != '\0'; c++)
2407 if (*p == *c)
2408 break;
2409 if (*p == *c) {
2410 if ((!ignore_empty) || (p - token > 0)) {
2411 lua_pushlstring(L, token, p - token);
2412 lua_rawseti(L, -2, index);
2413 index++;
2414 }
2415 token = p + 1;
2416 }
2417 if (*p == '\0')
2418 break;
2419 p++;
2420 }
2421
2422 return 1;
2423}
2424
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002425int hlua_parse_addr(lua_State *L)
2426{
Christopher Faulet29e93262021-02-26 09:39:05 +01002427 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002428 const char *str = luaL_checkstring(L, 1);
2429 unsigned char mask;
2430
Christopher Faulet29e93262021-02-26 09:39:05 +01002431 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002432 if (!addr) {
2433 lua_pushnil(L);
2434 return 1;
2435 }
2436
2437 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01002438 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002439 return 1;
2440 }
2441
2442 if (str62net(str, &addr->addr.v6.ip, &mask)) {
2443 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01002444 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002445 return 1;
2446 }
2447
2448 lua_pop(L, 1);
2449 lua_pushnil(L);
2450 return 1;
2451}
2452
2453int hlua_match_addr(lua_State *L)
2454{
Christopher Faulet29e93262021-02-26 09:39:05 +01002455 struct net_addr *addr1;
2456 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002457
2458 if (!lua_isuserdata(L, 1) ||
2459 !lua_isuserdata(L, 2)) {
2460 lua_pushboolean(L, 0);
2461 return 1;
2462 }
2463
2464 addr1 = lua_touserdata(L, 1);
2465 addr2 = lua_touserdata(L, 2);
2466
Christopher Faulet29e93262021-02-26 09:39:05 +01002467 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002468 lua_pushboolean(L, 0);
2469 return 1;
2470 }
2471
Christopher Faulet29e93262021-02-26 09:39:05 +01002472 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002473 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
2474 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
2475 lua_pushboolean(L, 1);
2476 return 1;
2477 }
2478 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01002479 int i;
2480
2481 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01002482 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
2483 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
2484 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
2485 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01002486 break;
2487 }
2488 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002489 lua_pushboolean(L, 1);
2490 return 1;
2491 }
2492 }
2493
2494 lua_pushboolean(L, 0);
2495 return 1;
2496}
2497
Dragan Dosen26743032019-04-30 15:54:36 +02002498static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02002499{
2500 return (hlua_checkudata(L, ud, class_regex_ref));
2501}
2502
2503static int hlua_regex_comp(struct lua_State *L)
2504{
Dragan Dosen26743032019-04-30 15:54:36 +02002505 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002506 const char *str;
2507 int cs;
2508 char *err;
2509
2510 str = luaL_checkstring(L, 1);
2511 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
2512 cs = lua_toboolean(L, 2);
2513
2514 regex = lua_newuserdata(L, sizeof(*regex));
2515
2516 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02002517 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02002518 lua_pushboolean(L, 0); /* status error */
2519 lua_pushstring(L, err); /* Reason */
2520 free(err);
2521 return 2;
2522 }
2523
2524 lua_pushboolean(L, 1); /* Status ok */
2525
2526 /* Create object */
2527 lua_newtable(L);
2528 lua_pushvalue(L, -3); /* Get the userdata pointer. */
2529 lua_rawseti(L, -2, 0);
2530 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
2531 lua_setmetatable(L, -2);
2532 return 2;
2533}
2534
2535static int hlua_regex_exec(struct lua_State *L)
2536{
Dragan Dosen26743032019-04-30 15:54:36 +02002537 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002538 const char *str;
2539 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02002540 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002541
2542 regex = hlua_check_regex(L, 1);
2543 str = luaL_checklstring(L, 2, &len);
2544
Dragan Dosen26743032019-04-30 15:54:36 +02002545 if (!*regex) {
2546 lua_pushboolean(L, 0);
2547 return 1;
2548 }
2549
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002550 /* Copy the string because regex_exec2 require a 'char *'
2551 * and not a 'const char *'.
2552 */
2553 tmp = get_trash_chunk();
2554 if (len >= tmp->size) {
2555 lua_pushboolean(L, 0);
2556 return 1;
2557 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002558 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002559
Dragan Dosen26743032019-04-30 15:54:36 +02002560 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02002561
2562 return 1;
2563}
2564
2565static int hlua_regex_match(struct lua_State *L)
2566{
Dragan Dosen26743032019-04-30 15:54:36 +02002567 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002568 const char *str;
2569 size_t len;
2570 regmatch_t pmatch[20];
2571 int ret;
2572 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02002573 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002574
2575 regex = hlua_check_regex(L, 1);
2576 str = luaL_checklstring(L, 2, &len);
2577
Dragan Dosen26743032019-04-30 15:54:36 +02002578 if (!*regex) {
2579 lua_pushboolean(L, 0);
2580 return 1;
2581 }
2582
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002583 /* Copy the string because regex_exec2 require a 'char *'
2584 * and not a 'const char *'.
2585 */
2586 tmp = get_trash_chunk();
2587 if (len >= tmp->size) {
2588 lua_pushboolean(L, 0);
2589 return 1;
2590 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002591 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002592
Dragan Dosen26743032019-04-30 15:54:36 +02002593 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02002594 lua_pushboolean(L, ret);
2595 lua_newtable(L);
2596 if (ret) {
2597 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
2598 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
2599 lua_rawseti(L, -2, i + 1);
2600 }
2601 }
2602 return 2;
2603}
2604
2605static int hlua_regex_free(struct lua_State *L)
2606{
Dragan Dosen26743032019-04-30 15:54:36 +02002607 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002608
2609 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02002610 regex_free(*regex);
2611 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002612 return 0;
2613}
2614
Thierry Fournier599f2312022-09-30 10:40:39 +02002615void hlua_fcn_reg_core_fcn(lua_State *L)
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002616{
Thierry Fournier599f2312022-09-30 10:40:39 +02002617 hlua_concat_init(L);
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +02002618 hlua_queue_init(L);
Thierry Fournier1de16592016-01-27 09:49:07 +01002619
Thierry Fournier4f99b272016-02-22 08:40:02 +01002620 hlua_class_function(L, "now", hlua_now);
2621 hlua_class_function(L, "http_date", hlua_http_date);
2622 hlua_class_function(L, "imf_date", hlua_imf_date);
2623 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
2624 hlua_class_function(L, "asctime_date", hlua_asctime_date);
2625 hlua_class_function(L, "concat", hlua_concat_new);
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +02002626 hlua_class_function(L, "queue", hlua_queue_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01002627 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002628 hlua_class_function(L, "parse_addr", hlua_parse_addr);
2629 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002630 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01002631
Thierry FOURNIER31904272017-10-25 12:59:51 +02002632 /* Create regex object. */
2633 lua_newtable(L);
2634 hlua_class_function(L, "new", hlua_regex_comp);
2635
2636 lua_newtable(L); /* The metatable. */
2637 lua_pushstring(L, "__index");
2638 lua_newtable(L);
2639 hlua_class_function(L, "exec", hlua_regex_exec);
2640 hlua_class_function(L, "match", hlua_regex_match);
2641 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
2642 hlua_class_function(L, "__gc", hlua_regex_free);
2643
2644 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
2645 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
2646
2647 lua_setmetatable(L, -2);
2648 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
2649
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002650 /* Create stktable object. */
2651 lua_newtable(L);
2652 lua_pushstring(L, "__index");
2653 lua_newtable(L);
2654 hlua_class_function(L, "info", hlua_stktable_info);
2655 hlua_class_function(L, "lookup", hlua_stktable_lookup);
2656 hlua_class_function(L, "dump", hlua_stktable_dump);
2657 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2658 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
2659
Thierry Fournierff480422016-02-25 08:36:46 +01002660 /* Create listener object. */
2661 lua_newtable(L);
2662 lua_pushstring(L, "__index");
2663 lua_newtable(L);
2664 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
2665 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2666 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
2667
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01002668 /* Create event_sub object. */
2669 lua_newtable(L);
2670 hlua_class_function(L, "__gc", hlua_event_sub_gc);
2671 class_event_sub_ref = hlua_register_metatable(L, CLASS_EVENT_SUB);
2672
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002673 /* Create server object. */
2674 lua_newtable(L);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002675 hlua_class_function(L, "__gc", hlua_server_gc);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002676 hlua_class_function(L, "__index", hlua_server_index);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002677 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
2678
Thierry Fournierf61aa632016-02-19 20:56:00 +01002679 /* Create proxy object. */
2680 lua_newtable(L);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002681 hlua_class_function(L, "__index", hlua_proxy_index);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002682 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
2683
Thierry Fournier467913c2022-09-30 11:03:38 +02002684 /* list of proxy objects. Instead of having a static array
2685 * of proxies, we use special metamethods that rely on internal
2686 * proxies list so that the array is resolved at runtime.
2687 *
2688 * To emulate the same behavior than Lua array, we implement some
2689 * metatable functions:
2690 * - __newindex : prevent the insertion of a new item in the array
2691 * - __index : find a proxy in the list using "name" index
2692 * - __pairs : iterate through available proxies in the list
2693 */
2694 lua_newtable(L);
2695 hlua_class_function(L, "__index", hlua_listable_proxies_index);
2696 hlua_class_function(L, "__newindex", hlua_listable_proxies_newindex);
2697 hlua_class_function(L, "__pairs", hlua_listable_proxies_pairs);
2698 class_proxy_list_ref = hlua_register_metatable(L, CLASS_PROXY_LIST);
2699
2700 /* Create proxies entry. */
2701 lua_pushstring(L, "proxies");
2702 hlua_listable_proxies(L, PR_CAP_LISTEN);
2703 lua_settable(L, -3);
2704
2705 /* Create frontends entry. */
2706 lua_pushstring(L, "frontends");
2707 hlua_listable_proxies(L, PR_CAP_FE);
2708 lua_settable(L, -3);
2709
2710 /* Create backends entry. */
2711 lua_pushstring(L, "backends");
2712 hlua_listable_proxies(L, PR_CAP_BE);
2713 lua_settable(L, -3);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002714
2715 /* list of server. This object is similar to
2716 * CLASS_PROXY_LIST
2717 */
2718 lua_newtable(L);
2719 hlua_class_function(L, "__index", hlua_listable_servers_index);
2720 hlua_class_function(L, "__newindex", hlua_listable_servers_newindex);
2721 hlua_class_function(L, "__pairs", hlua_listable_servers_pairs);
2722 class_server_list_ref = hlua_register_metatable(L, CLASS_SERVER_LIST);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002723}