blob: 5f96f5cfe7fb58eeb39ade15d60d9b8f6bb3d007 [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>
Willy Tarreau27539402021-10-06 09:12:44 +020038#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010040#include <haproxy/tools.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010041
Thierry Fournier1de16592016-01-27 09:49:07 +010042/* Contains the class reference of the concat object. */
43static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010044static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010045static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010046static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020047static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020048static int class_stktable_ref;
Thierry Fournier467913c2022-09-30 11:03:38 +020049static int class_proxy_list_ref;
Thierry Fournier1edf36a2022-10-07 13:25:51 +020050static int class_server_list_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010051
Thierry Fournierf61aa632016-02-19 20:56:00 +010052#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010053
Thierry FOURNIERffbad792017-07-12 11:39:04 +020054static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010055
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010056int hlua_checkboolean(lua_State *L, int index)
57{
58 if (!lua_isboolean(L, index))
59 luaL_argerror(L, index, "boolean expected");
60 return lua_toboolean(L, index);
61}
62
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020063/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
64static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
65{
66#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
67 lua_pushinteger(L, val);
68#else
69 if (val > INT_MAX)
70 lua_pushnumber(L, (lua_Number)val);
71 else
72 lua_pushinteger(L, (int)val);
73#endif
74 return 1;
75}
76
77/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
78static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
79#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
80 /* 64 bits case, U64 is supported until LLONG_MAX */
81 if (val > LLONG_MAX)
82 lua_pushnumber(L, (lua_Number)val);
83 else
84 lua_pushinteger(L, val);
85#else
86 /* 32 bits case, U64 is supported until INT_MAX */
87 if (val > INT_MAX)
88 lua_pushnumber(L, (lua_Number)val);
89 else
90 lua_pushinteger(L, (int)val);
91#endif
92 return 1;
93}
94
Joseph Herlantb3d92e32018-11-15 09:35:04 -080095/* This function gets a struct field and converts it in Lua
96 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010097 */
98int hlua_fcn_pushfield(lua_State *L, struct field *field)
99{
100 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800101 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100102 * by some macros. Windows maros are not supported.
103 * If the number cannot be represented as integer, we try to
104 * convert to float.
105 */
106 switch (field_format(field, 0)) {
107
108 case FF_EMPTY:
109 lua_pushnil(L);
110 return 1;
111
112 case FF_S32:
113 /* S32 is always supported. */
114 lua_pushinteger(L, field->u.s32);
115 return 1;
116
117 case FF_U32:
118#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
119 /* 64 bits case, U32 is always supported */
120 lua_pushinteger(L, field->u.u32);
121#else
122 /* 32 bits case, U32 is supported until INT_MAX. */
123 if (field->u.u32 > INT_MAX)
124 lua_pushnumber(L, (lua_Number)field->u.u32);
125 else
126 lua_pushinteger(L, field->u.u32);
127#endif
128 return 1;
129
130 case FF_S64:
131#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
132 /* 64 bits case, S64 is always supported */
133 lua_pushinteger(L, field->u.s64);
134#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500135 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100136 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
137 lua_pushnumber(L, (lua_Number)field->u.s64);
138 else
139 lua_pushinteger(L, (int)field->u.s64);
140#endif
141 return 1;
142
143 case FF_U64:
144#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
145 /* 64 bits case, U64 is supported until LLONG_MAX */
146 if (field->u.u64 > LLONG_MAX)
147 lua_pushnumber(L, (lua_Number)field->u.u64);
148 else
149 lua_pushinteger(L, field->u.u64);
150#else
151 /* 64 bits case, U64 is supported until INT_MAX */
152 if (field->u.u64 > INT_MAX)
153 lua_pushnumber(L, (lua_Number)field->u.u64);
154 else
155 lua_pushinteger(L, (int)field->u.u64);
156#endif
157 return 1;
158
159 case FF_STR:
160 lua_pushstring(L, field->u.str);
161 return 1;
162
163 default:
164 break;
165 }
166
167 /* Default case, never reached. */
168 lua_pushnil(L);
169 return 1;
170}
171
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100172/* Some string are started or terminated by blank chars,
173 * this function removes the spaces, tabs, \r and
174 * \n at the begin and at the end of the string "str", and
175 * push the result in the lua stack.
176 * Returns a pointer to the Lua internal copy of the string.
177 */
178const char *hlua_pushstrippedstring(lua_State *L, const char *str)
179{
180 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100181 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100182
183 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100184
185 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100186
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100187 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100188}
189
Thierry Fournierddd89882016-02-22 19:52:08 +0100190/* The three following functions are useful for adding entries
191 * in a table. These functions takes a string and respectively an
192 * integer, a string or a function and add it to the table in the
193 * top of the stack.
194 *
195 * These functions throws an error if no more stack size is
196 * available.
197 */
198void hlua_class_const_int(lua_State *L, const char *name, int value)
199{
Thierry Fournierddd89882016-02-22 19:52:08 +0100200 lua_pushstring(L, name);
201 lua_pushinteger(L, value);
202 lua_rawset(L, -3);
203}
204void hlua_class_const_str(lua_State *L, const char *name, const char *value)
205{
Thierry Fournierddd89882016-02-22 19:52:08 +0100206 lua_pushstring(L, name);
207 lua_pushstring(L, value);
208 lua_rawset(L, -3);
209}
210void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
211{
Thierry Fournierddd89882016-02-22 19:52:08 +0100212 lua_pushstring(L, name);
213 lua_pushcclosure(L, function, 0);
214 lua_rawset(L, -3);
215}
216
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800217/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100218int hlua_dump_object(struct lua_State *L)
219{
220 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
221 lua_pushfstring(L, "HAProxy class %s", name);
222 return 1;
223}
224
Thierry Fournier45e78d72016-02-19 18:34:46 +0100225/* This function register a table as metatable and. It names
226 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500227 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100228 * "name" is the referenced class name.
229 */
230int hlua_register_metatable(struct lua_State *L, char *name)
231{
232 /* Check the type of the top element. it must be
233 * a table.
234 */
235 if (lua_type(L, -1) != LUA_TTABLE)
236 luaL_error(L, "hlua_register_metatable() requires a type Table "
237 "in the top of the stack");
238
239 /* Add the __tostring function which identify the
240 * created object.
241 */
242 lua_pushstring(L, "__tostring");
243 lua_pushstring(L, name);
244 lua_pushcclosure(L, hlua_dump_object, 1);
245 lua_rawset(L, -3);
246
247 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500248 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100249 * lua_setfield() pop the entry.
250 */
251 lua_pushvalue(L, -1);
252 lua_setfield(L, LUA_REGISTRYINDEX, name);
253
254 /* Creates the reference of the object. The
255 * function luaL_ref pop the top of the stack.
256 */
257 return luaL_ref(L, LUA_REGISTRYINDEX);
258}
259
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100260/* Return an object of the expected type, or throws an error. */
261void *hlua_checkudata(lua_State *L, int ud, int class_ref)
262{
263 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100264 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100265
266 /* Check if the stack entry is an array. */
267 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100268 luaL_argerror(L, ud, NULL);
269
270 /* pop the metatable of the referencecd object. */
271 if (!lua_getmetatable(L, ud))
272 luaL_argerror(L, ud, NULL);
273
274 /* pop the expected metatable. */
275 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
276
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100277 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100278 ret = lua_rawequal(L, -1, -2);
279 lua_pop(L, 2);
280 if (!ret)
281 luaL_argerror(L, ud, NULL);
282
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100283 /* Push on the stack at the entry [0] of the table. */
284 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100285
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100286 /* Check if this entry is userdata. */
287 p = lua_touserdata(L, -1);
288 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100289 luaL_argerror(L, ud, NULL);
290
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100291 /* Remove the entry returned by lua_rawgeti(). */
292 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100293
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100294 /* Return the associated struct. */
295 return p;
296}
297
Thierry Fournierb1f46562016-01-21 09:46:15 +0100298/* This function return the current date at epoch format in milliseconds. */
299int hlua_now(lua_State *L)
300{
301 lua_newtable(L);
302 lua_pushstring(L, "sec");
303 lua_pushinteger(L, now.tv_sec);
304 lua_rawset(L, -3);
305 lua_pushstring(L, "usec");
306 lua_pushinteger(L, now.tv_usec);
307 lua_rawset(L, -3);
308 return 1;
309}
310
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100311/* This functions expects a Lua string as HTTP date, parse it and
312 * returns an integer containing the epoch format of the date, or
313 * nil if the parsing fails.
314 */
315static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
316{
317 const char *str;
318 size_t len;
319 struct tm tm;
320 time_t time;
321
322 str = luaL_checklstring(L, 1, &len);
323
324 if (!fcn(str, len, &tm)) {
325 lua_pushnil(L);
326 return 1;
327 }
328
329 /* This function considers the content of the broken-down time
330 * is exprimed in the UTC timezone. timegm don't care about
331 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
332 * the timezone from the broken-down time, it must be fixed
333 * after the conversion.
334 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200335 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100336 if (time == -1) {
337 lua_pushnil(L);
338 return 1;
339 }
340
341 lua_pushinteger(L, (int)time);
342 return 1;
343}
344static int hlua_http_date(lua_State *L)
345{
346 return hlua_parse_date(L, parse_http_date);
347}
348static int hlua_imf_date(lua_State *L)
349{
350 return hlua_parse_date(L, parse_imf_date);
351}
352static int hlua_rfc850_date(lua_State *L)
353{
354 return hlua_parse_date(L, parse_rfc850_date);
355}
356static int hlua_asctime_date(lua_State *L)
357{
358 return hlua_parse_date(L, parse_asctime_date);
359}
360
Thierry Fourniereea77c02016-03-18 08:47:13 +0100361static int hlua_get_info(lua_State *L)
362{
363 int i;
364
Willy Tarreau0b26b382021-05-08 07:43:53 +0200365 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100366
367 lua_newtable(L);
368 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200369 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100370 hlua_fcn_pushfield(L, &stats[i]);
371 lua_settable(L, -3);
372 }
373 return 1;
374}
375
Thierry Fournier49d48422016-02-19 12:09:29 +0100376static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100377{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200378 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100379}
380
381static int hlua_concat_add(lua_State *L)
382{
Thierry Fournier49d48422016-02-19 12:09:29 +0100383 struct hlua_concat *b;
384 char *buffer;
385 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100386 const char *str;
387 size_t l;
388
389 /* First arg must be a concat object. */
390 b = hlua_check_concat(L, 1);
391
392 /* Second arg must be a string. */
393 str = luaL_checklstring(L, 2, &l);
394
Thierry Fournier49d48422016-02-19 12:09:29 +0100395 /* Get the buffer. */
396 lua_rawgeti(L, 1, 1);
397 buffer = lua_touserdata(L, -1);
398 lua_pop(L, 1);
399
400 /* Update the buffer size if it s required. The old buffer
401 * is crushed by the new in the object array, so it will
402 * be deleted by the GC.
403 * Note that in the first loop, the "new" variable is only
404 * used as a flag.
405 */
406 new = NULL;
407 while (b->size - b->len < l) {
408 b->size += HLUA_CONCAT_BLOCSZ;
409 new = buffer;
410 }
411 if (new) {
412 new = lua_newuserdata(L, b->size);
413 memcpy(new, buffer, b->len);
414 lua_rawseti(L, 1, 1);
415 buffer = new;
416 }
417
418 /* Copy string, and update metadata. */
419 memcpy(buffer + b->len, str, l);
420 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100421 return 0;
422}
423
424static int hlua_concat_dump(lua_State *L)
425{
Thierry Fournier49d48422016-02-19 12:09:29 +0100426 struct hlua_concat *b;
427 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100428
429 /* First arg must be a concat object. */
430 b = hlua_check_concat(L, 1);
431
Thierry Fournier49d48422016-02-19 12:09:29 +0100432 /* Get the buffer. */
433 lua_rawgeti(L, 1, 1);
434 buffer = lua_touserdata(L, -1);
435 lua_pop(L, 1);
436
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500437 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100438 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100439 return 1;
440}
441
442int hlua_concat_new(lua_State *L)
443{
Thierry Fournier49d48422016-02-19 12:09:29 +0100444 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100445
446 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200447 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100448 b->size = HLUA_CONCAT_BLOCSZ;
449 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100450 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100451 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
452 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100453
454 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
455 lua_setmetatable(L, -2);
456
Thierry Fournier1de16592016-01-27 09:49:07 +0100457 return 1;
458}
459
460static int concat_tostring(lua_State *L)
461{
462 const void *ptr = lua_topointer(L, 1);
463 lua_pushfstring(L, "Concat object: %p", ptr);
464 return 1;
465}
466
Thierry Fournier599f2312022-09-30 10:40:39 +0200467static void hlua_concat_init(lua_State *L)
Thierry Fournier1de16592016-01-27 09:49:07 +0100468{
469 /* Creates the buffered concat object. */
470 lua_newtable(L);
471
472 lua_pushstring(L, "__tostring");
473 lua_pushcclosure(L, concat_tostring, 0);
474 lua_settable(L, -3);
475
476 lua_pushstring(L, "__index"); /* Creates the index entry. */
477 lua_newtable(L); /* The "__index" content. */
478
479 lua_pushstring(L, "add");
480 lua_pushcclosure(L, hlua_concat_add, 0);
481 lua_settable(L, -3);
482
483 lua_pushstring(L, "dump");
484 lua_pushcclosure(L, hlua_concat_dump, 0);
485 lua_settable(L, -3);
486
487 lua_settable(L, -3); /* Sets the __index entry. */
488 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Thierry Fournier1de16592016-01-27 09:49:07 +0100489}
490
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200491int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
492{
493 lua_newtable(L);
494
495 /* Pop a class stktbl metatable and affect it to the userdata. */
496 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
497 lua_setmetatable(L, -2);
498
499 lua_pushlightuserdata(L, tbl);
500 lua_rawseti(L, -2, 0);
501 return 1;
502}
503
504static struct stktable *hlua_check_stktable(lua_State *L, int ud)
505{
506 return hlua_checkudata(L, ud, class_stktable_ref);
507}
508
509/* Extract stick table attributes into Lua table */
510int hlua_stktable_info(lua_State *L)
511{
512 struct stktable *tbl;
513 int dt;
514
515 tbl = hlua_check_stktable(L, 1);
516
517 if (!tbl->id) {
518 lua_pushnil(L);
519 return 1;
520 }
521
522 lua_newtable(L);
523
524 lua_pushstring(L, "type");
525 lua_pushstring(L, stktable_types[tbl->type].kw);
526 lua_settable(L, -3);
527
528 lua_pushstring(L, "length");
529 lua_pushinteger(L, tbl->key_size);
530 lua_settable(L, -3);
531
532 lua_pushstring(L, "size");
533 hlua_fcn_pushunsigned(L, tbl->size);
534 lua_settable(L, -3);
535
536 lua_pushstring(L, "used");
537 hlua_fcn_pushunsigned(L, tbl->current);
538 lua_settable(L, -3);
539
540 lua_pushstring(L, "nopurge");
541 lua_pushboolean(L, tbl->nopurge > 0);
542 lua_settable(L, -3);
543
544 lua_pushstring(L, "expire");
545 lua_pushinteger(L, tbl->expire);
546 lua_settable(L, -3);
547
548 /* Save data types periods (if applicable) in 'data' table */
549 lua_pushstring(L, "data");
550 lua_newtable(L);
551
552 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
553 if (tbl->data_ofs[dt] == 0)
554 continue;
555
556 lua_pushstring(L, stktable_data_types[dt].name);
557
558 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
559 lua_pushinteger(L, tbl->data_arg[dt].u);
560 else
561 lua_pushinteger(L, -1);
562
563 lua_settable(L, -3);
564 }
565
566 lua_settable(L, -3);
567
568 return 1;
569}
570
571/* Helper to get extract stick table entry into Lua table */
572static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
573{
574 int dt;
575 void *ptr;
576
577 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
578
579 if (t->data_ofs[dt] == 0)
580 continue;
581
582 lua_pushstring(L, stktable_data_types[dt].name);
583
584 ptr = stktable_data_ptr(t, ts, dt);
585 switch (stktable_data_types[dt].std_type) {
586 case STD_T_SINT:
587 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
588 break;
589 case STD_T_UINT:
590 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
591 break;
592 case STD_T_ULL:
593 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
594 break;
595 case STD_T_FRQP:
596 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
597 t->data_arg[dt].u));
598 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200599 case STD_T_DICT: {
600 struct dict_entry *de;
601 de = stktable_data_cast(ptr, std_t_dict);
602 lua_pushstring(L, de ? (char *)de->value.key : "-");
603 break;
604 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200605 }
606
607 lua_settable(L, -3);
608 }
609}
610
611/* Looks in table <t> for a sticky session matching key <key>
612 * Returns table with session data or nil
613 *
614 * The returned table always contains 'use' and 'expire' (integer) fields.
615 * For frequency/rate counters, each data entry is returned as table with
616 * 'value' and 'period' fields.
617 */
618int hlua_stktable_lookup(lua_State *L)
619{
620 struct stktable *t;
621 struct sample smp;
622 struct stktable_key *skey;
623 struct stksess *ts;
624
625 t = hlua_check_stktable(L, 1);
626 smp.data.type = SMP_T_STR;
627 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600628 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200629
630 skey = smp_to_stkey(&smp, t);
631 if (!skey) {
632 lua_pushnil(L);
633 return 1;
634 }
635
636 ts = stktable_lookup_key(t, skey);
637 if (!ts) {
638 lua_pushnil(L);
639 return 1;
640 }
641
642 lua_newtable(L);
643 lua_pushstring(L, "use");
644 lua_pushinteger(L, ts->ref_cnt - 1);
645 lua_settable(L, -3);
646
647 lua_pushstring(L, "expire");
648 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
649 lua_settable(L, -3);
650
651 hlua_stktable_entry(L, t, ts);
Willy Tarreau76642222022-10-11 12:02:50 +0200652 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200653 ts->ref_cnt--;
Willy Tarreau76642222022-10-11 12:02:50 +0200654 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200655
656 return 1;
657}
658
659struct stk_filter {
660 long long val;
661 int type;
662 int op;
663};
664
665
666/* Helper for returning errors to callers using Lua convention (nil, err) */
667static int hlua_error(lua_State *L, const char *fmt, ...) {
668 char buf[256];
669 int len;
670 va_list args;
671 va_start(args, fmt);
672 len = vsnprintf(buf, sizeof(buf), fmt, args);
673 va_end(args);
674
675 if (len < 0) {
676 ha_alert("hlua_error(): Could not write error message.\n");
677 lua_pushnil(L);
678 return 1;
679 } else if (len >= sizeof(buf))
680 ha_alert("hlua_error(): Error message was truncated.\n");
681
682 lua_pushnil(L);
683 lua_pushstring(L, buf);
684
685 return 2;
686}
687
688/* Dump the contents of stick table <t>*/
689int hlua_stktable_dump(lua_State *L)
690{
691 struct stktable *t;
692 struct ebmb_node *eb;
693 struct ebmb_node *n;
694 struct stksess *ts;
695 int type;
696 int op;
697 int dt;
698 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100699 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200700 int filter_count = 0;
701 int i;
702 int skip_entry;
703 void *ptr;
704
705 t = hlua_check_stktable(L, 1);
706 type = lua_type(L, 2);
707
708 switch (type) {
709 case LUA_TNONE:
710 case LUA_TNIL:
711 break;
712 case LUA_TTABLE:
713 lua_pushnil(L);
714 while (lua_next(L, 2) != 0) {
715 int entry_idx = 0;
716
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100717 if (filter_count >= STKTABLE_FILTER_LEN)
718 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200719
720 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
721 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
722
723 lua_pushnil(L);
724 while (lua_next(L, -2) != 0) {
725 switch (entry_idx) {
726 case 0:
727 if (lua_type(L, -1) != LUA_TSTRING)
728 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
729
730 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
731 if (dt < 0 || t->data_ofs[dt] == 0)
732 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
733 filter[filter_count].type = dt;
734 break;
735 case 1:
736 if (lua_type(L, -1) != LUA_TSTRING)
737 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
738
739 op = get_std_op(lua_tostring(L, -1));
740 if (op < 0)
741 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
742 filter[filter_count].op = op;
743 break;
744 case 2:
745 val = lua_tointeger(L, -1);
746 filter[filter_count].val = val;
747 filter_count++;
748 break;
749 default:
750 break;
751 }
752 entry_idx++;
753 lua_pop(L, 1);
754 }
755 lua_pop(L, 1);
756 }
757 break;
758 default:
759 return hlua_error(L, "filter table expected");
760 }
761
762 lua_newtable(L);
763
Willy Tarreau76642222022-10-11 12:02:50 +0200764 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200765 eb = ebmb_first(&t->keys);
766 for (n = eb; n; n = ebmb_next(n)) {
767 ts = ebmb_entry(n, struct stksess, key);
768 if (!ts) {
Willy Tarreau76642222022-10-11 12:02:50 +0200769 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200770 return 1;
771 }
772 ts->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +0200773 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200774
775 /* multi condition/value filter */
776 skip_entry = 0;
777 for (i = 0; i < filter_count; i++) {
778 if (t->data_ofs[filter[i].type] == 0)
779 continue;
780
781 ptr = stktable_data_ptr(t, ts, filter[i].type);
782
783 switch (stktable_data_types[filter[i].type].std_type) {
784 case STD_T_SINT:
785 val = stktable_data_cast(ptr, std_t_sint);
786 break;
787 case STD_T_UINT:
788 val = stktable_data_cast(ptr, std_t_uint);
789 break;
790 case STD_T_ULL:
791 val = stktable_data_cast(ptr, std_t_ull);
792 break;
793 case STD_T_FRQP:
794 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
795 t->data_arg[filter[i].type].u);
796 break;
797 default:
798 continue;
799 break;
800 }
801
802 op = filter[i].op;
803
804 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
805 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
806 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
807 skip_entry = 1;
808 break;
809 }
810 }
811
812 if (skip_entry) {
Willy Tarreau76642222022-10-11 12:02:50 +0200813 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200814 ts->ref_cnt--;
815 continue;
816 }
817
818 if (t->type == SMP_T_IPV4) {
819 char addr[INET_ADDRSTRLEN];
820 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
821 lua_pushstring(L, addr);
822 } else if (t->type == SMP_T_IPV6) {
823 char addr[INET6_ADDRSTRLEN];
824 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
825 lua_pushstring(L, addr);
826 } else if (t->type == SMP_T_SINT) {
827 lua_pushinteger(L, *ts->key.key);
828 } else if (t->type == SMP_T_STR) {
829 lua_pushstring(L, (const char *)ts->key.key);
830 } else {
831 return hlua_error(L, "Unsupported stick table key type");
832 }
833
834 lua_newtable(L);
835 hlua_stktable_entry(L, t, ts);
836 lua_settable(L, -3);
Willy Tarreau76642222022-10-11 12:02:50 +0200837 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200838 ts->ref_cnt--;
839 }
Willy Tarreau76642222022-10-11 12:02:50 +0200840 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200841
842 return 1;
843}
844
Thierry Fournierff480422016-02-25 08:36:46 +0100845int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
846{
847 lua_newtable(L);
848
849 /* Pop a class sesison metatable and affect it to the userdata. */
850 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
851 lua_setmetatable(L, -2);
852
853 lua_pushlightuserdata(L, lst);
854 lua_rawseti(L, -2, 0);
855 return 1;
856}
857
858static struct listener *hlua_check_listener(lua_State *L, int ud)
859{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200860 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100861}
862
863int hlua_listener_get_stats(lua_State *L)
864{
865 struct listener *li;
866 int i;
867
868 li = hlua_check_listener(L, 1);
869
Willy Tarreauc95bad52016-12-22 00:13:31 +0100870 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100871 lua_pushnil(L);
872 return 1;
873 }
874
William Dauchy655e14e2021-02-14 23:22:54 +0100875 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
876 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +0100877
878 lua_newtable(L);
879 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200880 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100881 hlua_fcn_pushfield(L, &stats[i]);
882 lua_settable(L, -3);
883 }
884 return 1;
885
886}
887
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200888int hlua_server_gc(lua_State *L)
889{
890 struct server *srv = hlua_checkudata(L, 1, class_server_ref);
891
892 srv_drop(srv); /* srv_drop allows NULL srv */
893 return 0;
894}
895
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100896static struct server *hlua_check_server(lua_State *L, int ud)
897{
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200898 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200899 if (srv->flags & SRV_F_DELETED) {
900 return NULL;
901 }
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200902 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100903}
904
905int hlua_server_get_stats(lua_State *L)
906{
907 struct server *srv;
908 int i;
909
910 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200911 if (srv == NULL) {
912 lua_pushnil(L);
913 return 1;
914 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100915
916 if (!srv->proxy) {
917 lua_pushnil(L);
918 return 1;
919 }
920
William Dauchyd3a9a492021-01-25 17:29:03 +0100921 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
922 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100923
924 lua_newtable(L);
925 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200926 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100927 hlua_fcn_pushfield(L, &stats[i]);
928 lua_settable(L, -3);
929 }
930 return 1;
931
932}
933
934int hlua_server_get_addr(lua_State *L)
935{
936 struct server *srv;
937 char addr[INET6_ADDRSTRLEN];
938 luaL_Buffer b;
939
940 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200941 if (srv == NULL) {
942 lua_pushnil(L);
943 return 1;
944 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100945
946 luaL_buffinit(L, &b);
947
948 switch (srv->addr.ss_family) {
949 case AF_INET:
950 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
951 addr, INET_ADDRSTRLEN);
952 luaL_addstring(&b, addr);
953 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400954 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100955 luaL_addstring(&b, addr);
956 break;
957 case AF_INET6:
958 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400959 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100960 luaL_addstring(&b, addr);
961 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400962 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100963 luaL_addstring(&b, addr);
964 break;
965 case AF_UNIX:
966 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
967 break;
968 default:
969 luaL_addstring(&b, "<unknown>");
970 break;
971 }
972
973 luaL_pushresult(&b);
974 return 1;
975}
976
Thierry Fournierb0467732022-10-07 12:07:24 +0200977int hlua_server_get_puid(lua_State *L)
978{
979 struct server *srv;
980 char buffer[12];
981
982 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200983 if (srv == NULL) {
984 lua_pushnil(L);
985 return 1;
986 }
Thierry Fournierb0467732022-10-07 12:07:24 +0200987
988 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
989 lua_pushstring(L, buffer);
990 return 1;
991}
992
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +0100993int hlua_server_get_rid(lua_State *L)
994{
995 struct server *srv;
996 char buffer[12];
997
998 srv = hlua_check_server(L, 1);
999 if (srv == NULL) {
1000 lua_pushnil(L);
1001 return 1;
1002 }
1003
1004 snprintf(buffer, sizeof(buffer), "%d", srv->rid);
1005 lua_pushstring(L, buffer);
1006 return 1;
1007}
1008
Thierry Fournierb0467732022-10-07 12:07:24 +02001009int hlua_server_get_name(lua_State *L)
1010{
1011 struct server *srv;
1012
1013 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001014 if (srv == NULL) {
1015 lua_pushnil(L);
1016 return 1;
1017 }
1018
Thierry Fournierb0467732022-10-07 12:07:24 +02001019 lua_pushstring(L, srv->id);
1020 return 1;
1021}
1022
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001023/* __index metamethod for server class
1024 * support for additionnal keys that are missing from the main table
1025 * stack:1 = table (server class), stack:2 = requested key
1026 * Returns 1 if key is supported
1027 * else returns 0 to make lua return NIL value to the caller
1028 */
1029static int hlua_server_index(struct lua_State *L)
1030{
1031 const char *key = lua_tostring(L, 2);
1032
1033 if (!strcmp(key, "name")) {
1034 if (ONLY_ONCE())
1035 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, ", "));
1036 lua_pushvalue(L, 1);
1037 hlua_server_get_name(L);
1038 return 1;
1039 }
1040 if (!strcmp(key, "puid")) {
1041 if (ONLY_ONCE())
1042 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, ", "));
1043 lua_pushvalue(L, 1);
1044 hlua_server_get_puid(L);
1045 return 1;
1046 }
1047 /* unknown attribute */
1048 return 0;
1049}
1050
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001051int hlua_server_is_draining(lua_State *L)
1052{
1053 struct server *srv;
1054
1055 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001056 if (srv == NULL) {
1057 lua_pushnil(L);
1058 return 1;
1059 }
1060
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001061 lua_pushinteger(L, server_is_draining(srv));
1062 return 1;
1063}
1064
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001065int hlua_server_set_maxconn(lua_State *L)
1066{
1067 struct server *srv;
1068 const char *maxconn;
1069 const char *err;
1070
1071 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001072 if (srv == NULL) {
1073 lua_pushnil(L);
1074 return 1;
1075 }
1076
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001077 maxconn = luaL_checkstring(L, 2);
1078
1079 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1080 err = server_parse_maxconn_change_request(srv, maxconn);
1081 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1082 if (!err)
1083 lua_pushnil(L);
1084 else
1085 hlua_pushstrippedstring(L, err);
1086 return 1;
1087}
1088
1089int hlua_server_get_maxconn(lua_State *L)
1090{
1091 struct server *srv;
1092
1093 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001094 if (srv == NULL) {
1095 lua_pushnil(L);
1096 return 1;
1097 }
1098
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001099 lua_pushinteger(L, srv->maxconn);
1100 return 1;
1101}
1102
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001103int hlua_server_set_weight(lua_State *L)
1104{
1105 struct server *srv;
1106 const char *weight;
1107 const char *err;
1108
1109 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001110 if (srv == NULL) {
1111 lua_pushnil(L);
1112 return 1;
1113 }
1114
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001115 weight = luaL_checkstring(L, 2);
1116
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001117 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001118 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001119 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001120 if (!err)
1121 lua_pushnil(L);
1122 else
1123 hlua_pushstrippedstring(L, err);
1124 return 1;
1125}
1126
1127int hlua_server_get_weight(lua_State *L)
1128{
1129 struct server *srv;
1130
1131 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001132 if (srv == NULL) {
1133 lua_pushnil(L);
1134 return 1;
1135 }
1136
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001137 lua_pushinteger(L, srv->uweight);
1138 return 1;
1139}
1140
1141int hlua_server_set_addr(lua_State *L)
1142{
1143 struct server *srv;
1144 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001145 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001146 const char *err;
1147
1148 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001149 if (srv == NULL) {
1150 lua_pushnil(L);
1151 return 1;
1152 }
1153
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001154 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001155 if (lua_gettop(L) >= 3)
1156 port = luaL_checkstring(L, 3);
1157 else
1158 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001159
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001160 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001161 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001162 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001163 if (!err)
1164 lua_pushnil(L);
1165 else
1166 hlua_pushstrippedstring(L, err);
1167 return 1;
1168}
1169
1170int hlua_server_shut_sess(lua_State *L)
1171{
1172 struct server *srv;
1173
1174 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001175 if (srv == NULL) {
1176 return 0;
1177 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001178 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001179 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001180 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001181 return 0;
1182}
1183
1184int hlua_server_set_drain(lua_State *L)
1185{
1186 struct server *srv;
1187
1188 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001189 if (srv == NULL) {
1190 return 0;
1191 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001192 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001193 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001194 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001195 return 0;
1196}
1197
1198int hlua_server_set_maint(lua_State *L)
1199{
1200 struct server *srv;
1201
1202 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001203 if (srv == NULL) {
1204 return 0;
1205 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001206 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001207 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001208 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001209 return 0;
1210}
1211
1212int hlua_server_set_ready(lua_State *L)
1213{
1214 struct server *srv;
1215
1216 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001217 if (srv == NULL) {
1218 return 0;
1219 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001220 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001221 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001223 return 0;
1224}
1225
1226int hlua_server_check_enable(lua_State *L)
1227{
1228 struct server *sv;
1229
1230 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001231 if (sv == NULL) {
1232 return 0;
1233 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001234 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001235 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001236 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001237 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001238 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001239 return 0;
1240}
1241
1242int hlua_server_check_disable(lua_State *L)
1243{
1244 struct server *sv;
1245
1246 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001247 if (sv == NULL) {
1248 return 0;
1249 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001250 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001251 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001252 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001253 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001254 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001255 return 0;
1256}
1257
1258int hlua_server_check_force_up(lua_State *L)
1259{
1260 struct server *sv;
1261
1262 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001263 if (sv == NULL) {
1264 return 0;
1265 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001266 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001267 if (!(sv->track)) {
1268 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001269 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001270 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001271 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001272 return 0;
1273}
1274
1275int hlua_server_check_force_nolb(lua_State *L)
1276{
1277 struct server *sv;
1278
1279 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001280 if (sv == NULL) {
1281 return 0;
1282 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001283 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001284 if (!(sv->track)) {
1285 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001286 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001287 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001288 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001289 return 0;
1290}
1291
1292int hlua_server_check_force_down(lua_State *L)
1293{
1294 struct server *sv;
1295
1296 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001297 if (sv == NULL) {
1298 return 0;
1299 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001300 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001301 if (!(sv->track)) {
1302 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001303 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001304 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001305 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001306 return 0;
1307}
1308
1309int hlua_server_agent_enable(lua_State *L)
1310{
1311 struct server *sv;
1312
1313 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001314 if (sv == NULL) {
1315 return 0;
1316 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001317 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001318 if (sv->agent.state & CHK_ST_CONFIGURED) {
1319 sv->agent.state |= CHK_ST_ENABLED;
1320 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001321 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001322 return 0;
1323}
1324
1325int hlua_server_agent_disable(lua_State *L)
1326{
1327 struct server *sv;
1328
1329 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001330 if (sv == NULL) {
1331 return 0;
1332 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001333 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001334 if (sv->agent.state & CHK_ST_CONFIGURED) {
1335 sv->agent.state &= ~CHK_ST_ENABLED;
1336 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001337 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001338 return 0;
1339}
1340
1341int hlua_server_agent_force_up(lua_State *L)
1342{
1343 struct server *sv;
1344
1345 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001346 if (sv == NULL) {
1347 return 0;
1348 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001349 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001350 if (sv->agent.state & CHK_ST_ENABLED) {
1351 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001352 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001353 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001354 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001355 return 0;
1356}
1357
1358int hlua_server_agent_force_down(lua_State *L)
1359{
1360 struct server *sv;
1361
1362 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001363 if (sv == NULL) {
1364 return 0;
1365 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001366 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001367 if (sv->agent.state & CHK_ST_ENABLED) {
1368 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001369 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001370 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001371 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001372 return 0;
1373}
1374
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001375int hlua_fcn_new_server(lua_State *L, struct server *srv)
1376{
1377 lua_newtable(L);
1378
1379 /* Pop a class server metatable and affect it to the userdata. */
1380 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
1381 lua_setmetatable(L, -2);
1382
1383 lua_pushlightuserdata(L, srv);
1384 lua_rawseti(L, -2, 0);
1385
1386 /* userdata is affected: increment server refcount */
1387 srv_take(srv);
1388
1389 /* set public methods */
1390 hlua_class_function(L, "get_name", hlua_server_get_name);
1391 hlua_class_function(L, "get_puid", hlua_server_get_puid);
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001392 hlua_class_function(L, "get_rid", hlua_server_get_rid);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001393 hlua_class_function(L, "is_draining", hlua_server_is_draining);
1394 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1395 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
1396 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1397 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1398 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1399 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1400 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1401 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1402 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1403 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1404 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1405 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1406 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1407 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1408 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1409 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1410 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1411 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1412 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1413 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1414
1415 return 1;
1416}
1417
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001418static struct hlua_server_list *hlua_check_server_list(lua_State *L, int ud)
1419{
1420 return hlua_checkudata(L, ud, class_server_list_ref);
1421}
1422
1423/* does nothing and returns 0, only prevents insertions in the
1424 * table which represents the list of servers
1425 */
1426int hlua_listable_servers_newindex(lua_State *L) {
1427 return 0;
1428}
1429
1430/* first arg is the table (struct hlua_server_list * in metadata)
1431 * second arg is the required index
1432 */
1433int hlua_listable_servers_index(lua_State *L)
Thierry Fournierf61aa632016-02-19 20:56:00 +01001434{
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001435 struct hlua_server_list *hlua_srv;
1436 const char *name;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001437 struct server *srv;
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001438
1439 hlua_srv = hlua_check_server_list(L, 1);
1440 name = luaL_checkstring(L, 2);
1441
1442 /* Perform a server lookup in px list */
1443 srv = server_find_by_name(hlua_srv->px, name);
1444 if (srv == NULL) {
1445 lua_pushnil(L);
1446 return 1;
1447 }
1448
1449 hlua_fcn_new_server(L, srv);
1450 return 1;
1451}
1452
1453/* iterator must return key as string and value as server
1454 * object, if we reach end of list, it returns nil.
1455 * The context knows the last returned server. if the
1456 * context contains srv == NULL, we start enumeration.
1457 * Then, use 'srv->next' ptr to iterate through the list
1458 */
1459int hlua_listable_servers_pairs_iterator(lua_State *L)
1460{
1461 int context_index;
1462 struct hlua_server_list_iterator_context *ctx;
1463
1464 context_index = lua_upvalueindex(1);
1465 ctx = lua_touserdata(L, context_index);
1466
1467 if (ctx->cur == NULL) {
1468 /* First iteration, initialize list on the first server */
1469 ctx->cur = ctx->px->srv;
1470 } else {
1471
1472 /* Next server (next ptr is always valid, even if current
1473 * server has the SRV_F_DELETED flag set)
1474 */
1475 ctx->cur = ctx->cur->next;
1476 }
1477
1478 /* next server is null, end of iteration */
1479 if (ctx->cur == NULL) {
1480 lua_pushnil(L);
1481 return 1;
1482 }
1483
1484 lua_pushstring(L, ctx->cur->id);
1485 hlua_fcn_new_server(L, ctx->cur);
1486 return 2;
1487}
1488
1489/* init the iterator context, return iterator function
1490 * with context as closure. The only argument is a
1491 * server list object.
1492 */
1493int hlua_listable_servers_pairs(lua_State *L)
1494{
1495 struct hlua_server_list_iterator_context *ctx;
1496 struct hlua_server_list *hlua_srv_list;
1497
1498 hlua_srv_list = hlua_check_server_list(L, 1);
1499
1500 ctx = lua_newuserdata(L, sizeof(*ctx));
1501 ctx->px = hlua_srv_list->px;
1502 ctx->cur = NULL;
1503
1504 lua_pushcclosure(L, hlua_listable_servers_pairs_iterator, 1);
1505 return 1;
1506}
1507
1508void hlua_listable_servers(lua_State *L, struct proxy *px)
1509{
1510 struct hlua_server_list *list;
1511
1512 lua_newtable(L);
1513 list = lua_newuserdata(L, sizeof(*list));
1514 list->px = px;
1515 lua_rawseti(L, -2, 0);
1516 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_list_ref);
1517 lua_setmetatable(L, -2);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001518}
1519
1520static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1521{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001522 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001523}
1524
Thierry Fournierb0467732022-10-07 12:07:24 +02001525int hlua_proxy_get_name(lua_State *L)
1526{
1527 struct proxy *px;
1528
1529 px = hlua_check_proxy(L, 1);
1530 lua_pushstring(L, px->id);
1531 return 1;
1532}
1533
1534int hlua_proxy_get_uuid(lua_State *L)
1535{
1536 struct proxy *px;
1537 char buffer[17];
1538
1539 px = hlua_check_proxy(L, 1);
1540 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1541 lua_pushstring(L, buffer);
1542 return 1;
1543}
1544
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001545/* __index metamethod for proxy class
1546 * support for additionnal keys that are missing from the main table
1547 * stack:1 = table (proxy class), stack:2 = requested key
1548 * Returns 1 if key is supported
1549 * else returns 0 to make lua return NIL value to the caller
1550 */
1551static int hlua_proxy_index(struct lua_State *L)
1552{
1553 const char *key = lua_tostring(L, 2);
1554
1555 if (!strcmp(key, "name")) {
1556 if (ONLY_ONCE())
1557 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, ", "));
1558 lua_pushvalue(L, 1);
1559 hlua_proxy_get_name(L);
1560 return 1;
1561 }
1562 if (!strcmp(key, "uuid")) {
1563 if (ONLY_ONCE())
1564 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, ", "));
1565 lua_pushvalue(L, 1);
1566 hlua_proxy_get_uuid(L);
1567 return 1;
1568 }
1569 /* unknown attribute */
1570 return 0;
1571}
1572
Thierry Fournierf61aa632016-02-19 20:56:00 +01001573int hlua_proxy_pause(lua_State *L)
1574{
1575 struct proxy *px;
1576
1577 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001578 /* safe to call without PROXY_LOCK - pause_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001579 pause_proxy(px);
1580 return 0;
1581}
1582
1583int hlua_proxy_resume(lua_State *L)
1584{
1585 struct proxy *px;
1586
1587 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001588 /* safe to call without PROXY_LOCK - resume_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001589 resume_proxy(px);
1590 return 0;
1591}
1592
1593int hlua_proxy_stop(lua_State *L)
1594{
1595 struct proxy *px;
1596
1597 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001598 /* safe to call without PROXY_LOCK - stop_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001599 stop_proxy(px);
1600 return 0;
1601}
1602
1603int hlua_proxy_get_cap(lua_State *L)
1604{
1605 struct proxy *px;
1606 const char *str;
1607
1608 px = hlua_check_proxy(L, 1);
1609 str = proxy_cap_str(px->cap);
1610 lua_pushstring(L, str);
1611 return 1;
1612}
1613
1614int hlua_proxy_get_stats(lua_State *L)
1615{
1616 struct proxy *px;
1617 int i;
1618
1619 px = hlua_check_proxy(L, 1);
1620 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001621 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001622 else
William Dauchy0ef54392021-01-17 18:27:45 +01001623 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001624 lua_newtable(L);
1625 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001626 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001627 hlua_fcn_pushfield(L, &stats[i]);
1628 lua_settable(L, -3);
1629 }
1630 return 1;
1631}
1632
1633int hlua_proxy_get_mode(lua_State *L)
1634{
1635 struct proxy *px;
1636 const char *str;
1637
1638 px = hlua_check_proxy(L, 1);
1639 str = proxy_mode_str(px->mode);
1640 lua_pushstring(L, str);
1641 return 1;
1642}
1643
1644int hlua_proxy_shut_bcksess(lua_State *L)
1645{
1646 struct proxy *px;
1647
1648 px = hlua_check_proxy(L, 1);
1649 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1650 return 0;
1651}
1652
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001653int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1654{
1655 struct listener *lst;
1656 int lid;
1657 char buffer[17];
1658
1659 lua_newtable(L);
1660
1661 /* Pop a class proxy metatable and affect it to the userdata. */
1662 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1663 lua_setmetatable(L, -2);
1664
1665 lua_pushlightuserdata(L, px);
1666 lua_rawseti(L, -2, 0);
1667
1668 /* set public methods */
1669 hlua_class_function(L, "get_name", hlua_proxy_get_name);
1670 hlua_class_function(L, "get_uuid", hlua_proxy_get_uuid);
1671 hlua_class_function(L, "pause", hlua_proxy_pause);
1672 hlua_class_function(L, "resume", hlua_proxy_resume);
1673 hlua_class_function(L, "stop", hlua_proxy_stop);
1674 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1675 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1676 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1677 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1678
1679 /* Browse and register servers. */
1680 lua_pushstring(L, "servers");
1681 hlua_listable_servers(L, px);
1682 lua_settable(L, -3);
1683
1684 /* Browse and register listeners. */
1685 lua_pushstring(L, "listeners");
1686 lua_newtable(L);
1687 lid = 1;
1688 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1689 if (lst->name)
1690 lua_pushstring(L, lst->name);
1691 else {
1692 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
1693 lid++;
1694 lua_pushstring(L, buffer);
1695 }
1696 hlua_fcn_new_listener(L, lst);
1697 lua_settable(L, -3);
1698 }
1699 lua_settable(L, -3);
1700
1701 if (px->table && px->table->id) {
1702 lua_pushstring(L, "stktable");
1703 hlua_fcn_new_stktable(L, px->table);
1704 lua_settable(L, -3);
1705 }
1706
1707 return 1;
1708}
1709
Thierry Fournier467913c2022-09-30 11:03:38 +02001710static struct hlua_proxy_list *hlua_check_proxy_list(lua_State *L, int ud)
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001711{
Thierry Fournier467913c2022-09-30 11:03:38 +02001712 return hlua_checkudata(L, ud, class_proxy_list_ref);
1713}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001714
Thierry Fournier467913c2022-09-30 11:03:38 +02001715/* does nothing and returns 0, only prevents insertions in the
1716 * table which represent list of proxies
1717 */
1718int hlua_listable_proxies_newindex(lua_State *L) {
1719 return 0;
1720}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001721
Thierry Fournier467913c2022-09-30 11:03:38 +02001722/* first arg is the table (struct hlua_proxy_list * in metadata)
1723 * second arg is the required index
1724 */
1725int hlua_listable_proxies_index(lua_State *L)
1726{
1727 struct hlua_proxy_list *hlua_px;
1728 const char *name;
1729 struct proxy *px;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001730
Thierry Fournier467913c2022-09-30 11:03:38 +02001731 hlua_px = hlua_check_proxy_list(L, 1);
1732 name = luaL_checkstring(L, 2);
1733
1734 px = NULL;
1735 if (hlua_px->capabilities & PR_CAP_FE) {
1736 px = proxy_find_by_name(name, PR_CAP_FE, 0);
1737 }
1738 if (!px && hlua_px->capabilities & PR_CAP_BE) {
1739 px = proxy_find_by_name(name, PR_CAP_BE, 0);
1740 }
1741 if (px == NULL) {
1742 lua_pushnil(L);
1743 return 1;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001744 }
1745
Thierry Fournier467913c2022-09-30 11:03:38 +02001746 hlua_fcn_new_proxy(L, px);
1747 return 1;
1748}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001749
Thierry Fournier467913c2022-09-30 11:03:38 +02001750static inline int hlua_listable_proxies_match(struct proxy *px, char cap) {
1751 return ((px->cap & cap) && !(px->cap & (PR_CAP_DEF | PR_CAP_INT)));
1752}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001753
Thierry Fournier467913c2022-09-30 11:03:38 +02001754/* iterator must return key as string and value as proxy
1755 * object, if we reach end of list, it returns nil
1756 */
1757int hlua_listable_proxies_pairs_iterator(lua_State *L)
1758{
1759 int context_index;
1760 struct hlua_proxy_list_iterator_context *ctx;
1761
1762 context_index = lua_upvalueindex(1);
1763 ctx = lua_touserdata(L, context_index);
1764
1765 if (ctx->next == NULL) {
1766 lua_pushnil(L);
1767 return 1;
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001768 }
1769
Thierry Fournier467913c2022-09-30 11:03:38 +02001770 lua_pushstring(L, ctx->next->id);
1771 hlua_fcn_new_proxy(L, ctx->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001772
Thierry Fournier467913c2022-09-30 11:03:38 +02001773 for (ctx->next = ctx->next->next;
1774 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
1775 ctx->next = ctx->next->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001776
Thierry Fournier467913c2022-09-30 11:03:38 +02001777 return 2;
1778}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001779
Thierry Fournier467913c2022-09-30 11:03:38 +02001780/* init the iterator context, return iterator function
1781 * with context as closure. The only argument is a
1782 * proxy object.
1783 */
1784int hlua_listable_proxies_pairs(lua_State *L)
1785{
1786 struct hlua_proxy_list_iterator_context *ctx;
1787 struct hlua_proxy_list *hlua_px;
1788
1789 hlua_px = hlua_check_proxy_list(L, 1);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001790
Thierry Fournier467913c2022-09-30 11:03:38 +02001791 ctx = lua_newuserdata(L, sizeof(*ctx));
1792
1793 ctx->capabilities = hlua_px->capabilities;
1794 for (ctx->next = proxies_list;
1795 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
1796 ctx->next = ctx->next->next);
1797 lua_pushcclosure(L, hlua_listable_proxies_pairs_iterator, 1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001798 return 1;
1799}
1800
Thierry Fournier467913c2022-09-30 11:03:38 +02001801void hlua_listable_proxies(lua_State *L, char capabilities)
1802{
1803 struct hlua_proxy_list *list;
1804
1805 lua_newtable(L);
1806 list = lua_newuserdata(L, sizeof(*list));
1807 list->capabilities = capabilities;
1808 lua_rawseti(L, -2, 0);
1809 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_list_ref);
1810 lua_setmetatable(L, -2);
1811}
1812
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001813/* This Lua function take a string, a list of separators.
1814 * It tokenize the input string using the list of separators
1815 * as separator.
1816 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001817 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001818 */
1819int hlua_tokenize(lua_State *L)
1820{
1821 const char *str;
1822 const char *sep;
1823 int index;
1824 const char *token;
1825 const char *p;
1826 const char *c;
1827 int ignore_empty;
1828
1829 ignore_empty = 0;
1830
1831 str = luaL_checkstring(L, 1);
1832 sep = luaL_checkstring(L, 2);
1833 if (lua_gettop(L) == 3)
1834 ignore_empty = hlua_checkboolean(L, 3);
1835
1836 lua_newtable(L);
1837 index = 1;
1838 token = str;
1839 p = str;
1840 while(1) {
1841 for (c = sep; *c != '\0'; c++)
1842 if (*p == *c)
1843 break;
1844 if (*p == *c) {
1845 if ((!ignore_empty) || (p - token > 0)) {
1846 lua_pushlstring(L, token, p - token);
1847 lua_rawseti(L, -2, index);
1848 index++;
1849 }
1850 token = p + 1;
1851 }
1852 if (*p == '\0')
1853 break;
1854 p++;
1855 }
1856
1857 return 1;
1858}
1859
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001860int hlua_parse_addr(lua_State *L)
1861{
Christopher Faulet29e93262021-02-26 09:39:05 +01001862 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001863 const char *str = luaL_checkstring(L, 1);
1864 unsigned char mask;
1865
Christopher Faulet29e93262021-02-26 09:39:05 +01001866 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001867 if (!addr) {
1868 lua_pushnil(L);
1869 return 1;
1870 }
1871
1872 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001873 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001874 return 1;
1875 }
1876
1877 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1878 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001879 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001880 return 1;
1881 }
1882
1883 lua_pop(L, 1);
1884 lua_pushnil(L);
1885 return 1;
1886}
1887
1888int hlua_match_addr(lua_State *L)
1889{
Christopher Faulet29e93262021-02-26 09:39:05 +01001890 struct net_addr *addr1;
1891 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001892
1893 if (!lua_isuserdata(L, 1) ||
1894 !lua_isuserdata(L, 2)) {
1895 lua_pushboolean(L, 0);
1896 return 1;
1897 }
1898
1899 addr1 = lua_touserdata(L, 1);
1900 addr2 = lua_touserdata(L, 2);
1901
Christopher Faulet29e93262021-02-26 09:39:05 +01001902 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001903 lua_pushboolean(L, 0);
1904 return 1;
1905 }
1906
Christopher Faulet29e93262021-02-26 09:39:05 +01001907 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001908 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1909 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1910 lua_pushboolean(L, 1);
1911 return 1;
1912 }
1913 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001914 int i;
1915
1916 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001917 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1918 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1919 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1920 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001921 break;
1922 }
1923 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001924 lua_pushboolean(L, 1);
1925 return 1;
1926 }
1927 }
1928
1929 lua_pushboolean(L, 0);
1930 return 1;
1931}
1932
Dragan Dosen26743032019-04-30 15:54:36 +02001933static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001934{
1935 return (hlua_checkudata(L, ud, class_regex_ref));
1936}
1937
1938static int hlua_regex_comp(struct lua_State *L)
1939{
Dragan Dosen26743032019-04-30 15:54:36 +02001940 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001941 const char *str;
1942 int cs;
1943 char *err;
1944
1945 str = luaL_checkstring(L, 1);
1946 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1947 cs = lua_toboolean(L, 2);
1948
1949 regex = lua_newuserdata(L, sizeof(*regex));
1950
1951 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001952 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001953 lua_pushboolean(L, 0); /* status error */
1954 lua_pushstring(L, err); /* Reason */
1955 free(err);
1956 return 2;
1957 }
1958
1959 lua_pushboolean(L, 1); /* Status ok */
1960
1961 /* Create object */
1962 lua_newtable(L);
1963 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1964 lua_rawseti(L, -2, 0);
1965 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1966 lua_setmetatable(L, -2);
1967 return 2;
1968}
1969
1970static int hlua_regex_exec(struct lua_State *L)
1971{
Dragan Dosen26743032019-04-30 15:54:36 +02001972 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001973 const char *str;
1974 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001975 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001976
1977 regex = hlua_check_regex(L, 1);
1978 str = luaL_checklstring(L, 2, &len);
1979
Dragan Dosen26743032019-04-30 15:54:36 +02001980 if (!*regex) {
1981 lua_pushboolean(L, 0);
1982 return 1;
1983 }
1984
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001985 /* Copy the string because regex_exec2 require a 'char *'
1986 * and not a 'const char *'.
1987 */
1988 tmp = get_trash_chunk();
1989 if (len >= tmp->size) {
1990 lua_pushboolean(L, 0);
1991 return 1;
1992 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001993 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001994
Dragan Dosen26743032019-04-30 15:54:36 +02001995 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001996
1997 return 1;
1998}
1999
2000static int hlua_regex_match(struct lua_State *L)
2001{
Dragan Dosen26743032019-04-30 15:54:36 +02002002 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002003 const char *str;
2004 size_t len;
2005 regmatch_t pmatch[20];
2006 int ret;
2007 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02002008 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002009
2010 regex = hlua_check_regex(L, 1);
2011 str = luaL_checklstring(L, 2, &len);
2012
Dragan Dosen26743032019-04-30 15:54:36 +02002013 if (!*regex) {
2014 lua_pushboolean(L, 0);
2015 return 1;
2016 }
2017
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002018 /* Copy the string because regex_exec2 require a 'char *'
2019 * and not a 'const char *'.
2020 */
2021 tmp = get_trash_chunk();
2022 if (len >= tmp->size) {
2023 lua_pushboolean(L, 0);
2024 return 1;
2025 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002026 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002027
Dragan Dosen26743032019-04-30 15:54:36 +02002028 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02002029 lua_pushboolean(L, ret);
2030 lua_newtable(L);
2031 if (ret) {
2032 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
2033 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
2034 lua_rawseti(L, -2, i + 1);
2035 }
2036 }
2037 return 2;
2038}
2039
2040static int hlua_regex_free(struct lua_State *L)
2041{
Dragan Dosen26743032019-04-30 15:54:36 +02002042 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002043
2044 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02002045 regex_free(*regex);
2046 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002047 return 0;
2048}
2049
Thierry Fournier599f2312022-09-30 10:40:39 +02002050void hlua_fcn_reg_core_fcn(lua_State *L)
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002051{
Thierry Fournier599f2312022-09-30 10:40:39 +02002052 hlua_concat_init(L);
Thierry Fournier1de16592016-01-27 09:49:07 +01002053
Thierry Fournier4f99b272016-02-22 08:40:02 +01002054 hlua_class_function(L, "now", hlua_now);
2055 hlua_class_function(L, "http_date", hlua_http_date);
2056 hlua_class_function(L, "imf_date", hlua_imf_date);
2057 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
2058 hlua_class_function(L, "asctime_date", hlua_asctime_date);
2059 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01002060 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002061 hlua_class_function(L, "parse_addr", hlua_parse_addr);
2062 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002063 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01002064
Thierry FOURNIER31904272017-10-25 12:59:51 +02002065 /* Create regex object. */
2066 lua_newtable(L);
2067 hlua_class_function(L, "new", hlua_regex_comp);
2068
2069 lua_newtable(L); /* The metatable. */
2070 lua_pushstring(L, "__index");
2071 lua_newtable(L);
2072 hlua_class_function(L, "exec", hlua_regex_exec);
2073 hlua_class_function(L, "match", hlua_regex_match);
2074 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
2075 hlua_class_function(L, "__gc", hlua_regex_free);
2076
2077 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
2078 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
2079
2080 lua_setmetatable(L, -2);
2081 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
2082
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002083 /* Create stktable object. */
2084 lua_newtable(L);
2085 lua_pushstring(L, "__index");
2086 lua_newtable(L);
2087 hlua_class_function(L, "info", hlua_stktable_info);
2088 hlua_class_function(L, "lookup", hlua_stktable_lookup);
2089 hlua_class_function(L, "dump", hlua_stktable_dump);
2090 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2091 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
2092
Thierry Fournierff480422016-02-25 08:36:46 +01002093 /* Create listener object. */
2094 lua_newtable(L);
2095 lua_pushstring(L, "__index");
2096 lua_newtable(L);
2097 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
2098 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2099 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
2100
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002101 /* Create server object. */
2102 lua_newtable(L);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002103 hlua_class_function(L, "__gc", hlua_server_gc);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002104 hlua_class_function(L, "__index", hlua_server_index);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002105 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
2106
Thierry Fournierf61aa632016-02-19 20:56:00 +01002107 /* Create proxy object. */
2108 lua_newtable(L);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002109 hlua_class_function(L, "__index", hlua_proxy_index);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002110 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
2111
Thierry Fournier467913c2022-09-30 11:03:38 +02002112 /* list of proxy objects. Instead of having a static array
2113 * of proxies, we use special metamethods that rely on internal
2114 * proxies list so that the array is resolved at runtime.
2115 *
2116 * To emulate the same behavior than Lua array, we implement some
2117 * metatable functions:
2118 * - __newindex : prevent the insertion of a new item in the array
2119 * - __index : find a proxy in the list using "name" index
2120 * - __pairs : iterate through available proxies in the list
2121 */
2122 lua_newtable(L);
2123 hlua_class_function(L, "__index", hlua_listable_proxies_index);
2124 hlua_class_function(L, "__newindex", hlua_listable_proxies_newindex);
2125 hlua_class_function(L, "__pairs", hlua_listable_proxies_pairs);
2126 class_proxy_list_ref = hlua_register_metatable(L, CLASS_PROXY_LIST);
2127
2128 /* Create proxies entry. */
2129 lua_pushstring(L, "proxies");
2130 hlua_listable_proxies(L, PR_CAP_LISTEN);
2131 lua_settable(L, -3);
2132
2133 /* Create frontends entry. */
2134 lua_pushstring(L, "frontends");
2135 hlua_listable_proxies(L, PR_CAP_FE);
2136 lua_settable(L, -3);
2137
2138 /* Create backends entry. */
2139 lua_pushstring(L, "backends");
2140 hlua_listable_proxies(L, PR_CAP_BE);
2141 lua_settable(L, -3);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002142
2143 /* list of server. This object is similar to
2144 * CLASS_PROXY_LIST
2145 */
2146 lua_newtable(L);
2147 hlua_class_function(L, "__index", hlua_listable_servers_index);
2148 hlua_class_function(L, "__newindex", hlua_listable_servers_newindex);
2149 hlua_class_function(L, "__pairs", hlua_listable_servers_pairs);
2150 class_server_list_ref = hlua_register_metatable(L, CLASS_SERVER_LIST);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002151}