blob: ff90321815d1c8f8100ef399cc11a437af110cf2 [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
993int hlua_server_get_name(lua_State *L)
994{
995 struct server *srv;
996
997 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200998 if (srv == NULL) {
999 lua_pushnil(L);
1000 return 1;
1001 }
1002
Thierry Fournierb0467732022-10-07 12:07:24 +02001003 lua_pushstring(L, srv->id);
1004 return 1;
1005}
1006
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001007/* __index metamethod for server class
1008 * support for additionnal keys that are missing from the main table
1009 * stack:1 = table (server class), stack:2 = requested key
1010 * Returns 1 if key is supported
1011 * else returns 0 to make lua return NIL value to the caller
1012 */
1013static int hlua_server_index(struct lua_State *L)
1014{
1015 const char *key = lua_tostring(L, 2);
1016
1017 if (!strcmp(key, "name")) {
1018 if (ONLY_ONCE())
1019 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, ", "));
1020 lua_pushvalue(L, 1);
1021 hlua_server_get_name(L);
1022 return 1;
1023 }
1024 if (!strcmp(key, "puid")) {
1025 if (ONLY_ONCE())
1026 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, ", "));
1027 lua_pushvalue(L, 1);
1028 hlua_server_get_puid(L);
1029 return 1;
1030 }
1031 /* unknown attribute */
1032 return 0;
1033}
1034
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001035int hlua_server_is_draining(lua_State *L)
1036{
1037 struct server *srv;
1038
1039 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001040 if (srv == NULL) {
1041 lua_pushnil(L);
1042 return 1;
1043 }
1044
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001045 lua_pushinteger(L, server_is_draining(srv));
1046 return 1;
1047}
1048
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001049int hlua_server_set_maxconn(lua_State *L)
1050{
1051 struct server *srv;
1052 const char *maxconn;
1053 const char *err;
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
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001061 maxconn = luaL_checkstring(L, 2);
1062
1063 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1064 err = server_parse_maxconn_change_request(srv, maxconn);
1065 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1066 if (!err)
1067 lua_pushnil(L);
1068 else
1069 hlua_pushstrippedstring(L, err);
1070 return 1;
1071}
1072
1073int hlua_server_get_maxconn(lua_State *L)
1074{
1075 struct server *srv;
1076
1077 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001078 if (srv == NULL) {
1079 lua_pushnil(L);
1080 return 1;
1081 }
1082
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001083 lua_pushinteger(L, srv->maxconn);
1084 return 1;
1085}
1086
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001087int hlua_server_set_weight(lua_State *L)
1088{
1089 struct server *srv;
1090 const char *weight;
1091 const char *err;
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
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001099 weight = luaL_checkstring(L, 2);
1100
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001101 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001102 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001103 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001104 if (!err)
1105 lua_pushnil(L);
1106 else
1107 hlua_pushstrippedstring(L, err);
1108 return 1;
1109}
1110
1111int hlua_server_get_weight(lua_State *L)
1112{
1113 struct server *srv;
1114
1115 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001116 if (srv == NULL) {
1117 lua_pushnil(L);
1118 return 1;
1119 }
1120
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001121 lua_pushinteger(L, srv->uweight);
1122 return 1;
1123}
1124
1125int hlua_server_set_addr(lua_State *L)
1126{
1127 struct server *srv;
1128 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001129 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001130 const char *err;
1131
1132 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001133 if (srv == NULL) {
1134 lua_pushnil(L);
1135 return 1;
1136 }
1137
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001138 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001139 if (lua_gettop(L) >= 3)
1140 port = luaL_checkstring(L, 3);
1141 else
1142 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001143
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001144 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001145 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001146 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001147 if (!err)
1148 lua_pushnil(L);
1149 else
1150 hlua_pushstrippedstring(L, err);
1151 return 1;
1152}
1153
1154int hlua_server_shut_sess(lua_State *L)
1155{
1156 struct server *srv;
1157
1158 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001159 if (srv == NULL) {
1160 return 0;
1161 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001162 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001163 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001164 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001165 return 0;
1166}
1167
1168int hlua_server_set_drain(lua_State *L)
1169{
1170 struct server *srv;
1171
1172 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001173 if (srv == NULL) {
1174 return 0;
1175 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001176 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001177 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001178 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001179 return 0;
1180}
1181
1182int hlua_server_set_maint(lua_State *L)
1183{
1184 struct server *srv;
1185
1186 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001187 if (srv == NULL) {
1188 return 0;
1189 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001190 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001191 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001192 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001193 return 0;
1194}
1195
1196int hlua_server_set_ready(lua_State *L)
1197{
1198 struct server *srv;
1199
1200 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001201 if (srv == NULL) {
1202 return 0;
1203 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001204 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001205 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001206 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001207 return 0;
1208}
1209
1210int hlua_server_check_enable(lua_State *L)
1211{
1212 struct server *sv;
1213
1214 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001215 if (sv == NULL) {
1216 return 0;
1217 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001219 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001220 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001221 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001223 return 0;
1224}
1225
1226int hlua_server_check_disable(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_force_up(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->track)) {
1252 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001253 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001254 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001255 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001256 return 0;
1257}
1258
1259int hlua_server_check_force_nolb(lua_State *L)
1260{
1261 struct server *sv;
1262
1263 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001264 if (sv == NULL) {
1265 return 0;
1266 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001267 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001268 if (!(sv->track)) {
1269 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001270 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001271 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001272 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001273 return 0;
1274}
1275
1276int hlua_server_check_force_down(lua_State *L)
1277{
1278 struct server *sv;
1279
1280 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001281 if (sv == NULL) {
1282 return 0;
1283 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001284 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001285 if (!(sv->track)) {
1286 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001287 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001288 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001289 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001290 return 0;
1291}
1292
1293int hlua_server_agent_enable(lua_State *L)
1294{
1295 struct server *sv;
1296
1297 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001298 if (sv == NULL) {
1299 return 0;
1300 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001301 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001302 if (sv->agent.state & CHK_ST_CONFIGURED) {
1303 sv->agent.state |= CHK_ST_ENABLED;
1304 }
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_disable(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_force_up(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_ENABLED) {
1335 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001336 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001337 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001338 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001339 return 0;
1340}
1341
1342int hlua_server_agent_force_down(lua_State *L)
1343{
1344 struct server *sv;
1345
1346 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001347 if (sv == NULL) {
1348 return 0;
1349 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001350 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001351 if (sv->agent.state & CHK_ST_ENABLED) {
1352 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001353 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001354 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001355 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001356 return 0;
1357}
1358
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001359int hlua_fcn_new_server(lua_State *L, struct server *srv)
1360{
1361 lua_newtable(L);
1362
1363 /* Pop a class server metatable and affect it to the userdata. */
1364 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
1365 lua_setmetatable(L, -2);
1366
1367 lua_pushlightuserdata(L, srv);
1368 lua_rawseti(L, -2, 0);
1369
1370 /* userdata is affected: increment server refcount */
1371 srv_take(srv);
1372
1373 /* set public methods */
1374 hlua_class_function(L, "get_name", hlua_server_get_name);
1375 hlua_class_function(L, "get_puid", hlua_server_get_puid);
1376 hlua_class_function(L, "is_draining", hlua_server_is_draining);
1377 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1378 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
1379 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1380 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1381 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1382 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1383 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1384 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1385 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1386 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1387 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1388 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1389 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1390 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1391 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1392 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1393 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1394 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1395 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1396 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1397
1398 return 1;
1399}
1400
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001401static struct hlua_server_list *hlua_check_server_list(lua_State *L, int ud)
1402{
1403 return hlua_checkudata(L, ud, class_server_list_ref);
1404}
1405
1406/* does nothing and returns 0, only prevents insertions in the
1407 * table which represents the list of servers
1408 */
1409int hlua_listable_servers_newindex(lua_State *L) {
1410 return 0;
1411}
1412
1413/* first arg is the table (struct hlua_server_list * in metadata)
1414 * second arg is the required index
1415 */
1416int hlua_listable_servers_index(lua_State *L)
Thierry Fournierf61aa632016-02-19 20:56:00 +01001417{
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001418 struct hlua_server_list *hlua_srv;
1419 const char *name;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001420 struct server *srv;
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001421
1422 hlua_srv = hlua_check_server_list(L, 1);
1423 name = luaL_checkstring(L, 2);
1424
1425 /* Perform a server lookup in px list */
1426 srv = server_find_by_name(hlua_srv->px, name);
1427 if (srv == NULL) {
1428 lua_pushnil(L);
1429 return 1;
1430 }
1431
1432 hlua_fcn_new_server(L, srv);
1433 return 1;
1434}
1435
1436/* iterator must return key as string and value as server
1437 * object, if we reach end of list, it returns nil.
1438 * The context knows the last returned server. if the
1439 * context contains srv == NULL, we start enumeration.
1440 * Then, use 'srv->next' ptr to iterate through the list
1441 */
1442int hlua_listable_servers_pairs_iterator(lua_State *L)
1443{
1444 int context_index;
1445 struct hlua_server_list_iterator_context *ctx;
1446
1447 context_index = lua_upvalueindex(1);
1448 ctx = lua_touserdata(L, context_index);
1449
1450 if (ctx->cur == NULL) {
1451 /* First iteration, initialize list on the first server */
1452 ctx->cur = ctx->px->srv;
1453 } else {
1454
1455 /* Next server (next ptr is always valid, even if current
1456 * server has the SRV_F_DELETED flag set)
1457 */
1458 ctx->cur = ctx->cur->next;
1459 }
1460
1461 /* next server is null, end of iteration */
1462 if (ctx->cur == NULL) {
1463 lua_pushnil(L);
1464 return 1;
1465 }
1466
1467 lua_pushstring(L, ctx->cur->id);
1468 hlua_fcn_new_server(L, ctx->cur);
1469 return 2;
1470}
1471
1472/* init the iterator context, return iterator function
1473 * with context as closure. The only argument is a
1474 * server list object.
1475 */
1476int hlua_listable_servers_pairs(lua_State *L)
1477{
1478 struct hlua_server_list_iterator_context *ctx;
1479 struct hlua_server_list *hlua_srv_list;
1480
1481 hlua_srv_list = hlua_check_server_list(L, 1);
1482
1483 ctx = lua_newuserdata(L, sizeof(*ctx));
1484 ctx->px = hlua_srv_list->px;
1485 ctx->cur = NULL;
1486
1487 lua_pushcclosure(L, hlua_listable_servers_pairs_iterator, 1);
1488 return 1;
1489}
1490
1491void hlua_listable_servers(lua_State *L, struct proxy *px)
1492{
1493 struct hlua_server_list *list;
1494
1495 lua_newtable(L);
1496 list = lua_newuserdata(L, sizeof(*list));
1497 list->px = px;
1498 lua_rawseti(L, -2, 0);
1499 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_list_ref);
1500 lua_setmetatable(L, -2);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001501}
1502
1503static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1504{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001505 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001506}
1507
Thierry Fournierb0467732022-10-07 12:07:24 +02001508int hlua_proxy_get_name(lua_State *L)
1509{
1510 struct proxy *px;
1511
1512 px = hlua_check_proxy(L, 1);
1513 lua_pushstring(L, px->id);
1514 return 1;
1515}
1516
1517int hlua_proxy_get_uuid(lua_State *L)
1518{
1519 struct proxy *px;
1520 char buffer[17];
1521
1522 px = hlua_check_proxy(L, 1);
1523 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1524 lua_pushstring(L, buffer);
1525 return 1;
1526}
1527
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001528/* __index metamethod for proxy class
1529 * support for additionnal keys that are missing from the main table
1530 * stack:1 = table (proxy class), stack:2 = requested key
1531 * Returns 1 if key is supported
1532 * else returns 0 to make lua return NIL value to the caller
1533 */
1534static int hlua_proxy_index(struct lua_State *L)
1535{
1536 const char *key = lua_tostring(L, 2);
1537
1538 if (!strcmp(key, "name")) {
1539 if (ONLY_ONCE())
1540 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, ", "));
1541 lua_pushvalue(L, 1);
1542 hlua_proxy_get_name(L);
1543 return 1;
1544 }
1545 if (!strcmp(key, "uuid")) {
1546 if (ONLY_ONCE())
1547 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, ", "));
1548 lua_pushvalue(L, 1);
1549 hlua_proxy_get_uuid(L);
1550 return 1;
1551 }
1552 /* unknown attribute */
1553 return 0;
1554}
1555
Thierry Fournierf61aa632016-02-19 20:56:00 +01001556int hlua_proxy_pause(lua_State *L)
1557{
1558 struct proxy *px;
1559
1560 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001561 /* safe to call without PROXY_LOCK - pause_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001562 pause_proxy(px);
1563 return 0;
1564}
1565
1566int hlua_proxy_resume(lua_State *L)
1567{
1568 struct proxy *px;
1569
1570 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001571 /* safe to call without PROXY_LOCK - resume_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001572 resume_proxy(px);
1573 return 0;
1574}
1575
1576int hlua_proxy_stop(lua_State *L)
1577{
1578 struct proxy *px;
1579
1580 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001581 /* safe to call without PROXY_LOCK - stop_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001582 stop_proxy(px);
1583 return 0;
1584}
1585
1586int hlua_proxy_get_cap(lua_State *L)
1587{
1588 struct proxy *px;
1589 const char *str;
1590
1591 px = hlua_check_proxy(L, 1);
1592 str = proxy_cap_str(px->cap);
1593 lua_pushstring(L, str);
1594 return 1;
1595}
1596
1597int hlua_proxy_get_stats(lua_State *L)
1598{
1599 struct proxy *px;
1600 int i;
1601
1602 px = hlua_check_proxy(L, 1);
1603 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001604 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001605 else
William Dauchy0ef54392021-01-17 18:27:45 +01001606 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001607 lua_newtable(L);
1608 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001609 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001610 hlua_fcn_pushfield(L, &stats[i]);
1611 lua_settable(L, -3);
1612 }
1613 return 1;
1614}
1615
1616int hlua_proxy_get_mode(lua_State *L)
1617{
1618 struct proxy *px;
1619 const char *str;
1620
1621 px = hlua_check_proxy(L, 1);
1622 str = proxy_mode_str(px->mode);
1623 lua_pushstring(L, str);
1624 return 1;
1625}
1626
1627int hlua_proxy_shut_bcksess(lua_State *L)
1628{
1629 struct proxy *px;
1630
1631 px = hlua_check_proxy(L, 1);
1632 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1633 return 0;
1634}
1635
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001636int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1637{
1638 struct listener *lst;
1639 int lid;
1640 char buffer[17];
1641
1642 lua_newtable(L);
1643
1644 /* Pop a class proxy metatable and affect it to the userdata. */
1645 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1646 lua_setmetatable(L, -2);
1647
1648 lua_pushlightuserdata(L, px);
1649 lua_rawseti(L, -2, 0);
1650
1651 /* set public methods */
1652 hlua_class_function(L, "get_name", hlua_proxy_get_name);
1653 hlua_class_function(L, "get_uuid", hlua_proxy_get_uuid);
1654 hlua_class_function(L, "pause", hlua_proxy_pause);
1655 hlua_class_function(L, "resume", hlua_proxy_resume);
1656 hlua_class_function(L, "stop", hlua_proxy_stop);
1657 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1658 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1659 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1660 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1661
1662 /* Browse and register servers. */
1663 lua_pushstring(L, "servers");
1664 hlua_listable_servers(L, px);
1665 lua_settable(L, -3);
1666
1667 /* Browse and register listeners. */
1668 lua_pushstring(L, "listeners");
1669 lua_newtable(L);
1670 lid = 1;
1671 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1672 if (lst->name)
1673 lua_pushstring(L, lst->name);
1674 else {
1675 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
1676 lid++;
1677 lua_pushstring(L, buffer);
1678 }
1679 hlua_fcn_new_listener(L, lst);
1680 lua_settable(L, -3);
1681 }
1682 lua_settable(L, -3);
1683
1684 if (px->table && px->table->id) {
1685 lua_pushstring(L, "stktable");
1686 hlua_fcn_new_stktable(L, px->table);
1687 lua_settable(L, -3);
1688 }
1689
1690 return 1;
1691}
1692
Thierry Fournier467913c2022-09-30 11:03:38 +02001693static struct hlua_proxy_list *hlua_check_proxy_list(lua_State *L, int ud)
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001694{
Thierry Fournier467913c2022-09-30 11:03:38 +02001695 return hlua_checkudata(L, ud, class_proxy_list_ref);
1696}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001697
Thierry Fournier467913c2022-09-30 11:03:38 +02001698/* does nothing and returns 0, only prevents insertions in the
1699 * table which represent list of proxies
1700 */
1701int hlua_listable_proxies_newindex(lua_State *L) {
1702 return 0;
1703}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001704
Thierry Fournier467913c2022-09-30 11:03:38 +02001705/* first arg is the table (struct hlua_proxy_list * in metadata)
1706 * second arg is the required index
1707 */
1708int hlua_listable_proxies_index(lua_State *L)
1709{
1710 struct hlua_proxy_list *hlua_px;
1711 const char *name;
1712 struct proxy *px;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001713
Thierry Fournier467913c2022-09-30 11:03:38 +02001714 hlua_px = hlua_check_proxy_list(L, 1);
1715 name = luaL_checkstring(L, 2);
1716
1717 px = NULL;
1718 if (hlua_px->capabilities & PR_CAP_FE) {
1719 px = proxy_find_by_name(name, PR_CAP_FE, 0);
1720 }
1721 if (!px && hlua_px->capabilities & PR_CAP_BE) {
1722 px = proxy_find_by_name(name, PR_CAP_BE, 0);
1723 }
1724 if (px == NULL) {
1725 lua_pushnil(L);
1726 return 1;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001727 }
1728
Thierry Fournier467913c2022-09-30 11:03:38 +02001729 hlua_fcn_new_proxy(L, px);
1730 return 1;
1731}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001732
Thierry Fournier467913c2022-09-30 11:03:38 +02001733static inline int hlua_listable_proxies_match(struct proxy *px, char cap) {
1734 return ((px->cap & cap) && !(px->cap & (PR_CAP_DEF | PR_CAP_INT)));
1735}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001736
Thierry Fournier467913c2022-09-30 11:03:38 +02001737/* iterator must return key as string and value as proxy
1738 * object, if we reach end of list, it returns nil
1739 */
1740int hlua_listable_proxies_pairs_iterator(lua_State *L)
1741{
1742 int context_index;
1743 struct hlua_proxy_list_iterator_context *ctx;
1744
1745 context_index = lua_upvalueindex(1);
1746 ctx = lua_touserdata(L, context_index);
1747
1748 if (ctx->next == NULL) {
1749 lua_pushnil(L);
1750 return 1;
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001751 }
1752
Thierry Fournier467913c2022-09-30 11:03:38 +02001753 lua_pushstring(L, ctx->next->id);
1754 hlua_fcn_new_proxy(L, ctx->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001755
Thierry Fournier467913c2022-09-30 11:03:38 +02001756 for (ctx->next = ctx->next->next;
1757 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
1758 ctx->next = ctx->next->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001759
Thierry Fournier467913c2022-09-30 11:03:38 +02001760 return 2;
1761}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001762
Thierry Fournier467913c2022-09-30 11:03:38 +02001763/* init the iterator context, return iterator function
1764 * with context as closure. The only argument is a
1765 * proxy object.
1766 */
1767int hlua_listable_proxies_pairs(lua_State *L)
1768{
1769 struct hlua_proxy_list_iterator_context *ctx;
1770 struct hlua_proxy_list *hlua_px;
1771
1772 hlua_px = hlua_check_proxy_list(L, 1);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001773
Thierry Fournier467913c2022-09-30 11:03:38 +02001774 ctx = lua_newuserdata(L, sizeof(*ctx));
1775
1776 ctx->capabilities = hlua_px->capabilities;
1777 for (ctx->next = proxies_list;
1778 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
1779 ctx->next = ctx->next->next);
1780 lua_pushcclosure(L, hlua_listable_proxies_pairs_iterator, 1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001781 return 1;
1782}
1783
Thierry Fournier467913c2022-09-30 11:03:38 +02001784void hlua_listable_proxies(lua_State *L, char capabilities)
1785{
1786 struct hlua_proxy_list *list;
1787
1788 lua_newtable(L);
1789 list = lua_newuserdata(L, sizeof(*list));
1790 list->capabilities = capabilities;
1791 lua_rawseti(L, -2, 0);
1792 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_list_ref);
1793 lua_setmetatable(L, -2);
1794}
1795
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001796/* This Lua function take a string, a list of separators.
1797 * It tokenize the input string using the list of separators
1798 * as separator.
1799 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001800 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001801 */
1802int hlua_tokenize(lua_State *L)
1803{
1804 const char *str;
1805 const char *sep;
1806 int index;
1807 const char *token;
1808 const char *p;
1809 const char *c;
1810 int ignore_empty;
1811
1812 ignore_empty = 0;
1813
1814 str = luaL_checkstring(L, 1);
1815 sep = luaL_checkstring(L, 2);
1816 if (lua_gettop(L) == 3)
1817 ignore_empty = hlua_checkboolean(L, 3);
1818
1819 lua_newtable(L);
1820 index = 1;
1821 token = str;
1822 p = str;
1823 while(1) {
1824 for (c = sep; *c != '\0'; c++)
1825 if (*p == *c)
1826 break;
1827 if (*p == *c) {
1828 if ((!ignore_empty) || (p - token > 0)) {
1829 lua_pushlstring(L, token, p - token);
1830 lua_rawseti(L, -2, index);
1831 index++;
1832 }
1833 token = p + 1;
1834 }
1835 if (*p == '\0')
1836 break;
1837 p++;
1838 }
1839
1840 return 1;
1841}
1842
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001843int hlua_parse_addr(lua_State *L)
1844{
Christopher Faulet29e93262021-02-26 09:39:05 +01001845 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001846 const char *str = luaL_checkstring(L, 1);
1847 unsigned char mask;
1848
Christopher Faulet29e93262021-02-26 09:39:05 +01001849 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001850 if (!addr) {
1851 lua_pushnil(L);
1852 return 1;
1853 }
1854
1855 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001856 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001857 return 1;
1858 }
1859
1860 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1861 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001862 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001863 return 1;
1864 }
1865
1866 lua_pop(L, 1);
1867 lua_pushnil(L);
1868 return 1;
1869}
1870
1871int hlua_match_addr(lua_State *L)
1872{
Christopher Faulet29e93262021-02-26 09:39:05 +01001873 struct net_addr *addr1;
1874 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001875
1876 if (!lua_isuserdata(L, 1) ||
1877 !lua_isuserdata(L, 2)) {
1878 lua_pushboolean(L, 0);
1879 return 1;
1880 }
1881
1882 addr1 = lua_touserdata(L, 1);
1883 addr2 = lua_touserdata(L, 2);
1884
Christopher Faulet29e93262021-02-26 09:39:05 +01001885 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001886 lua_pushboolean(L, 0);
1887 return 1;
1888 }
1889
Christopher Faulet29e93262021-02-26 09:39:05 +01001890 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001891 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1892 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1893 lua_pushboolean(L, 1);
1894 return 1;
1895 }
1896 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001897 int i;
1898
1899 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001900 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1901 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1902 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1903 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001904 break;
1905 }
1906 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001907 lua_pushboolean(L, 1);
1908 return 1;
1909 }
1910 }
1911
1912 lua_pushboolean(L, 0);
1913 return 1;
1914}
1915
Dragan Dosen26743032019-04-30 15:54:36 +02001916static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001917{
1918 return (hlua_checkudata(L, ud, class_regex_ref));
1919}
1920
1921static int hlua_regex_comp(struct lua_State *L)
1922{
Dragan Dosen26743032019-04-30 15:54:36 +02001923 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001924 const char *str;
1925 int cs;
1926 char *err;
1927
1928 str = luaL_checkstring(L, 1);
1929 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1930 cs = lua_toboolean(L, 2);
1931
1932 regex = lua_newuserdata(L, sizeof(*regex));
1933
1934 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001935 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001936 lua_pushboolean(L, 0); /* status error */
1937 lua_pushstring(L, err); /* Reason */
1938 free(err);
1939 return 2;
1940 }
1941
1942 lua_pushboolean(L, 1); /* Status ok */
1943
1944 /* Create object */
1945 lua_newtable(L);
1946 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1947 lua_rawseti(L, -2, 0);
1948 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1949 lua_setmetatable(L, -2);
1950 return 2;
1951}
1952
1953static int hlua_regex_exec(struct lua_State *L)
1954{
Dragan Dosen26743032019-04-30 15:54:36 +02001955 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001956 const char *str;
1957 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001958 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001959
1960 regex = hlua_check_regex(L, 1);
1961 str = luaL_checklstring(L, 2, &len);
1962
Dragan Dosen26743032019-04-30 15:54:36 +02001963 if (!*regex) {
1964 lua_pushboolean(L, 0);
1965 return 1;
1966 }
1967
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001968 /* Copy the string because regex_exec2 require a 'char *'
1969 * and not a 'const char *'.
1970 */
1971 tmp = get_trash_chunk();
1972 if (len >= tmp->size) {
1973 lua_pushboolean(L, 0);
1974 return 1;
1975 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001976 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001977
Dragan Dosen26743032019-04-30 15:54:36 +02001978 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001979
1980 return 1;
1981}
1982
1983static int hlua_regex_match(struct lua_State *L)
1984{
Dragan Dosen26743032019-04-30 15:54:36 +02001985 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001986 const char *str;
1987 size_t len;
1988 regmatch_t pmatch[20];
1989 int ret;
1990 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001991 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001992
1993 regex = hlua_check_regex(L, 1);
1994 str = luaL_checklstring(L, 2, &len);
1995
Dragan Dosen26743032019-04-30 15:54:36 +02001996 if (!*regex) {
1997 lua_pushboolean(L, 0);
1998 return 1;
1999 }
2000
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002001 /* Copy the string because regex_exec2 require a 'char *'
2002 * and not a 'const char *'.
2003 */
2004 tmp = get_trash_chunk();
2005 if (len >= tmp->size) {
2006 lua_pushboolean(L, 0);
2007 return 1;
2008 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002009 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002010
Dragan Dosen26743032019-04-30 15:54:36 +02002011 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02002012 lua_pushboolean(L, ret);
2013 lua_newtable(L);
2014 if (ret) {
2015 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
2016 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
2017 lua_rawseti(L, -2, i + 1);
2018 }
2019 }
2020 return 2;
2021}
2022
2023static int hlua_regex_free(struct lua_State *L)
2024{
Dragan Dosen26743032019-04-30 15:54:36 +02002025 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002026
2027 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02002028 regex_free(*regex);
2029 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002030 return 0;
2031}
2032
Thierry Fournier599f2312022-09-30 10:40:39 +02002033void hlua_fcn_reg_core_fcn(lua_State *L)
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002034{
Thierry Fournier599f2312022-09-30 10:40:39 +02002035 hlua_concat_init(L);
Thierry Fournier1de16592016-01-27 09:49:07 +01002036
Thierry Fournier4f99b272016-02-22 08:40:02 +01002037 hlua_class_function(L, "now", hlua_now);
2038 hlua_class_function(L, "http_date", hlua_http_date);
2039 hlua_class_function(L, "imf_date", hlua_imf_date);
2040 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
2041 hlua_class_function(L, "asctime_date", hlua_asctime_date);
2042 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01002043 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002044 hlua_class_function(L, "parse_addr", hlua_parse_addr);
2045 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002046 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01002047
Thierry FOURNIER31904272017-10-25 12:59:51 +02002048 /* Create regex object. */
2049 lua_newtable(L);
2050 hlua_class_function(L, "new", hlua_regex_comp);
2051
2052 lua_newtable(L); /* The metatable. */
2053 lua_pushstring(L, "__index");
2054 lua_newtable(L);
2055 hlua_class_function(L, "exec", hlua_regex_exec);
2056 hlua_class_function(L, "match", hlua_regex_match);
2057 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
2058 hlua_class_function(L, "__gc", hlua_regex_free);
2059
2060 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
2061 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
2062
2063 lua_setmetatable(L, -2);
2064 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
2065
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002066 /* Create stktable object. */
2067 lua_newtable(L);
2068 lua_pushstring(L, "__index");
2069 lua_newtable(L);
2070 hlua_class_function(L, "info", hlua_stktable_info);
2071 hlua_class_function(L, "lookup", hlua_stktable_lookup);
2072 hlua_class_function(L, "dump", hlua_stktable_dump);
2073 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2074 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
2075
Thierry Fournierff480422016-02-25 08:36:46 +01002076 /* Create listener object. */
2077 lua_newtable(L);
2078 lua_pushstring(L, "__index");
2079 lua_newtable(L);
2080 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
2081 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2082 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
2083
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002084 /* Create server object. */
2085 lua_newtable(L);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002086 hlua_class_function(L, "__gc", hlua_server_gc);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002087 hlua_class_function(L, "__index", hlua_server_index);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002088 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
2089
Thierry Fournierf61aa632016-02-19 20:56:00 +01002090 /* Create proxy object. */
2091 lua_newtable(L);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002092 hlua_class_function(L, "__index", hlua_proxy_index);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002093 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
2094
Thierry Fournier467913c2022-09-30 11:03:38 +02002095 /* list of proxy objects. Instead of having a static array
2096 * of proxies, we use special metamethods that rely on internal
2097 * proxies list so that the array is resolved at runtime.
2098 *
2099 * To emulate the same behavior than Lua array, we implement some
2100 * metatable functions:
2101 * - __newindex : prevent the insertion of a new item in the array
2102 * - __index : find a proxy in the list using "name" index
2103 * - __pairs : iterate through available proxies in the list
2104 */
2105 lua_newtable(L);
2106 hlua_class_function(L, "__index", hlua_listable_proxies_index);
2107 hlua_class_function(L, "__newindex", hlua_listable_proxies_newindex);
2108 hlua_class_function(L, "__pairs", hlua_listable_proxies_pairs);
2109 class_proxy_list_ref = hlua_register_metatable(L, CLASS_PROXY_LIST);
2110
2111 /* Create proxies entry. */
2112 lua_pushstring(L, "proxies");
2113 hlua_listable_proxies(L, PR_CAP_LISTEN);
2114 lua_settable(L, -3);
2115
2116 /* Create frontends entry. */
2117 lua_pushstring(L, "frontends");
2118 hlua_listable_proxies(L, PR_CAP_FE);
2119 lua_settable(L, -3);
2120
2121 /* Create backends entry. */
2122 lua_pushstring(L, "backends");
2123 hlua_listable_proxies(L, PR_CAP_BE);
2124 lua_settable(L, -3);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002125
2126 /* list of server. This object is similar to
2127 * CLASS_PROXY_LIST
2128 */
2129 lua_newtable(L);
2130 hlua_class_function(L, "__index", hlua_listable_servers_index);
2131 hlua_class_function(L, "__newindex", hlua_listable_servers_newindex);
2132 hlua_class_function(L, "__pairs", hlua_listable_servers_pairs);
2133 class_server_list_ref = hlua_register_metatable(L, CLASS_SERVER_LIST);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002134}