blob: ed016c0b85c4a94f032ad7a3587a08c55b02a285 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua safe functions
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 *
12 * All the functions in this file runs with a Lua stack, and can
Thierry Fournierfb0b5462016-01-21 09:28:58 +010013 * return with a longjmp. All of these function must be launched
14 * in an environment able to catch a longjmp, otherwise a
15 * critical error can be raised.
16 */
Bertrand Jacquinf4c12d42021-01-21 21:14:07 +000017
18#define _GNU_SOURCE
19
Thierry Fournierfb0b5462016-01-21 09:28:58 +010020#include <lauxlib.h>
21#include <lua.h>
22#include <lualib.h>
23
Willy Tarreau63617db2021-10-06 18:23:40 +020024#include <import/ebmbtree.h>
25
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/errors.h>
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +010028#include <haproxy/hlua.h>
Tim Duesterhus6eded622022-05-14 22:17:25 +020029#include <haproxy/hlua_fcn.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020030#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/pattern-t.h>
33#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020037#include <haproxy/stick_table.h>
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010038#include <haproxy/event_hdl.h>
Willy Tarreau27539402021-10-06 09:12:44 +020039#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010041#include <haproxy/tools.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010042
Thierry Fournier1de16592016-01-27 09:49:07 +010043/* Contains the class reference of the concat object. */
44static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010045static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010046static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010047static int class_listener_ref;
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010048static int class_event_sub_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020049static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020050static int class_stktable_ref;
Thierry Fournier467913c2022-09-30 11:03:38 +020051static int class_proxy_list_ref;
Thierry Fournier1edf36a2022-10-07 13:25:51 +020052static int class_server_list_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010053
Thierry Fournierf61aa632016-02-19 20:56:00 +010054#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010055
Thierry FOURNIERffbad792017-07-12 11:39:04 +020056static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010057
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010058int hlua_checkboolean(lua_State *L, int index)
59{
60 if (!lua_isboolean(L, index))
61 luaL_argerror(L, index, "boolean expected");
62 return lua_toboolean(L, index);
63}
64
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020065/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
66static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
67{
68#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
69 lua_pushinteger(L, val);
70#else
71 if (val > INT_MAX)
72 lua_pushnumber(L, (lua_Number)val);
73 else
74 lua_pushinteger(L, (int)val);
75#endif
76 return 1;
77}
78
79/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
80static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
81#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
82 /* 64 bits case, U64 is supported until LLONG_MAX */
83 if (val > LLONG_MAX)
84 lua_pushnumber(L, (lua_Number)val);
85 else
86 lua_pushinteger(L, val);
87#else
88 /* 32 bits case, U64 is supported until INT_MAX */
89 if (val > INT_MAX)
90 lua_pushnumber(L, (lua_Number)val);
91 else
92 lua_pushinteger(L, (int)val);
93#endif
94 return 1;
95}
96
Joseph Herlantb3d92e32018-11-15 09:35:04 -080097/* This function gets a struct field and converts it in Lua
98 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010099 */
100int hlua_fcn_pushfield(lua_State *L, struct field *field)
101{
102 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800103 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100104 * by some macros. Windows maros are not supported.
105 * If the number cannot be represented as integer, we try to
106 * convert to float.
107 */
108 switch (field_format(field, 0)) {
109
110 case FF_EMPTY:
111 lua_pushnil(L);
112 return 1;
113
114 case FF_S32:
115 /* S32 is always supported. */
116 lua_pushinteger(L, field->u.s32);
117 return 1;
118
119 case FF_U32:
120#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
121 /* 64 bits case, U32 is always supported */
122 lua_pushinteger(L, field->u.u32);
123#else
124 /* 32 bits case, U32 is supported until INT_MAX. */
125 if (field->u.u32 > INT_MAX)
126 lua_pushnumber(L, (lua_Number)field->u.u32);
127 else
128 lua_pushinteger(L, field->u.u32);
129#endif
130 return 1;
131
132 case FF_S64:
133#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
134 /* 64 bits case, S64 is always supported */
135 lua_pushinteger(L, field->u.s64);
136#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500137 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100138 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
139 lua_pushnumber(L, (lua_Number)field->u.s64);
140 else
141 lua_pushinteger(L, (int)field->u.s64);
142#endif
143 return 1;
144
145 case FF_U64:
146#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
147 /* 64 bits case, U64 is supported until LLONG_MAX */
148 if (field->u.u64 > LLONG_MAX)
149 lua_pushnumber(L, (lua_Number)field->u.u64);
150 else
151 lua_pushinteger(L, field->u.u64);
152#else
153 /* 64 bits case, U64 is supported until INT_MAX */
154 if (field->u.u64 > INT_MAX)
155 lua_pushnumber(L, (lua_Number)field->u.u64);
156 else
157 lua_pushinteger(L, (int)field->u.u64);
158#endif
159 return 1;
160
161 case FF_STR:
162 lua_pushstring(L, field->u.str);
163 return 1;
164
165 default:
166 break;
167 }
168
169 /* Default case, never reached. */
170 lua_pushnil(L);
171 return 1;
172}
173
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100174/* Some string are started or terminated by blank chars,
175 * this function removes the spaces, tabs, \r and
176 * \n at the begin and at the end of the string "str", and
177 * push the result in the lua stack.
178 * Returns a pointer to the Lua internal copy of the string.
179 */
180const char *hlua_pushstrippedstring(lua_State *L, const char *str)
181{
182 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100183 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100184
185 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100186
187 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100188
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100189 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100190}
191
Thierry Fournierddd89882016-02-22 19:52:08 +0100192/* The three following functions are useful for adding entries
193 * in a table. These functions takes a string and respectively an
194 * integer, a string or a function and add it to the table in the
195 * top of the stack.
196 *
197 * These functions throws an error if no more stack size is
198 * available.
199 */
200void hlua_class_const_int(lua_State *L, const char *name, int value)
201{
Thierry Fournierddd89882016-02-22 19:52:08 +0100202 lua_pushstring(L, name);
203 lua_pushinteger(L, value);
204 lua_rawset(L, -3);
205}
206void hlua_class_const_str(lua_State *L, const char *name, const char *value)
207{
Thierry Fournierddd89882016-02-22 19:52:08 +0100208 lua_pushstring(L, name);
209 lua_pushstring(L, value);
210 lua_rawset(L, -3);
211}
212void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
213{
Thierry Fournierddd89882016-02-22 19:52:08 +0100214 lua_pushstring(L, name);
215 lua_pushcclosure(L, function, 0);
216 lua_rawset(L, -3);
217}
218
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800219/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100220int hlua_dump_object(struct lua_State *L)
221{
222 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
223 lua_pushfstring(L, "HAProxy class %s", name);
224 return 1;
225}
226
Thierry Fournier45e78d72016-02-19 18:34:46 +0100227/* This function register a table as metatable and. It names
228 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500229 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100230 * "name" is the referenced class name.
231 */
232int hlua_register_metatable(struct lua_State *L, char *name)
233{
234 /* Check the type of the top element. it must be
235 * a table.
236 */
237 if (lua_type(L, -1) != LUA_TTABLE)
238 luaL_error(L, "hlua_register_metatable() requires a type Table "
239 "in the top of the stack");
240
241 /* Add the __tostring function which identify the
242 * created object.
243 */
244 lua_pushstring(L, "__tostring");
245 lua_pushstring(L, name);
246 lua_pushcclosure(L, hlua_dump_object, 1);
247 lua_rawset(L, -3);
248
249 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500250 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100251 * lua_setfield() pop the entry.
252 */
253 lua_pushvalue(L, -1);
254 lua_setfield(L, LUA_REGISTRYINDEX, name);
255
256 /* Creates the reference of the object. The
257 * function luaL_ref pop the top of the stack.
258 */
259 return luaL_ref(L, LUA_REGISTRYINDEX);
260}
261
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100262/* Return an object of the expected type, or throws an error. */
263void *hlua_checkudata(lua_State *L, int ud, int class_ref)
264{
265 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100266 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100267
268 /* Check if the stack entry is an array. */
269 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100270 luaL_argerror(L, ud, NULL);
271
272 /* pop the metatable of the referencecd object. */
273 if (!lua_getmetatable(L, ud))
274 luaL_argerror(L, ud, NULL);
275
276 /* pop the expected metatable. */
277 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
278
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100279 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100280 ret = lua_rawequal(L, -1, -2);
281 lua_pop(L, 2);
282 if (!ret)
283 luaL_argerror(L, ud, NULL);
284
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100285 /* Push on the stack at the entry [0] of the table. */
286 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100287
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100288 /* Check if this entry is userdata. */
289 p = lua_touserdata(L, -1);
290 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100291 luaL_argerror(L, ud, NULL);
292
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100293 /* Remove the entry returned by lua_rawgeti(). */
294 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100295
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100296 /* Return the associated struct. */
297 return p;
298}
299
Thierry Fournierb1f46562016-01-21 09:46:15 +0100300/* This function return the current date at epoch format in milliseconds. */
301int hlua_now(lua_State *L)
302{
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200303 /* WT: the doc says "returns the current time" and later says that it's
304 * monotonic. So the best fit is to use start_date+(now-start_time).
305 */
306 struct timeval tv;
307
Willy Tarreauc05d30e2023-04-28 14:50:29 +0200308 tv = NS_TO_TV(now_ns - start_time_ns);
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200309 tv_add(&tv, &tv, &start_date);
310
Thierry Fournierb1f46562016-01-21 09:46:15 +0100311 lua_newtable(L);
312 lua_pushstring(L, "sec");
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200313 lua_pushinteger(L, tv.tv_sec);
Thierry Fournierb1f46562016-01-21 09:46:15 +0100314 lua_rawset(L, -3);
315 lua_pushstring(L, "usec");
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200316 lua_pushinteger(L, tv.tv_usec);
Thierry Fournierb1f46562016-01-21 09:46:15 +0100317 lua_rawset(L, -3);
318 return 1;
319}
320
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100321/* This functions expects a Lua string as HTTP date, parse it and
322 * returns an integer containing the epoch format of the date, or
323 * nil if the parsing fails.
324 */
325static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
326{
327 const char *str;
328 size_t len;
329 struct tm tm;
330 time_t time;
331
332 str = luaL_checklstring(L, 1, &len);
333
334 if (!fcn(str, len, &tm)) {
335 lua_pushnil(L);
336 return 1;
337 }
338
339 /* This function considers the content of the broken-down time
340 * is exprimed in the UTC timezone. timegm don't care about
341 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
342 * the timezone from the broken-down time, it must be fixed
343 * after the conversion.
344 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200345 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100346 if (time == -1) {
347 lua_pushnil(L);
348 return 1;
349 }
350
351 lua_pushinteger(L, (int)time);
352 return 1;
353}
354static int hlua_http_date(lua_State *L)
355{
356 return hlua_parse_date(L, parse_http_date);
357}
358static int hlua_imf_date(lua_State *L)
359{
360 return hlua_parse_date(L, parse_imf_date);
361}
362static int hlua_rfc850_date(lua_State *L)
363{
364 return hlua_parse_date(L, parse_rfc850_date);
365}
366static int hlua_asctime_date(lua_State *L)
367{
368 return hlua_parse_date(L, parse_asctime_date);
369}
370
Thierry Fourniereea77c02016-03-18 08:47:13 +0100371static int hlua_get_info(lua_State *L)
372{
373 int i;
374
Willy Tarreau0b26b382021-05-08 07:43:53 +0200375 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100376
377 lua_newtable(L);
378 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200379 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100380 hlua_fcn_pushfield(L, &stats[i]);
381 lua_settable(L, -3);
382 }
383 return 1;
384}
385
Thierry Fournier49d48422016-02-19 12:09:29 +0100386static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100387{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200388 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100389}
390
391static int hlua_concat_add(lua_State *L)
392{
Thierry Fournier49d48422016-02-19 12:09:29 +0100393 struct hlua_concat *b;
394 char *buffer;
395 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100396 const char *str;
397 size_t l;
398
399 /* First arg must be a concat object. */
400 b = hlua_check_concat(L, 1);
401
402 /* Second arg must be a string. */
403 str = luaL_checklstring(L, 2, &l);
404
Thierry Fournier49d48422016-02-19 12:09:29 +0100405 /* Get the buffer. */
406 lua_rawgeti(L, 1, 1);
407 buffer = lua_touserdata(L, -1);
408 lua_pop(L, 1);
409
410 /* Update the buffer size if it s required. The old buffer
411 * is crushed by the new in the object array, so it will
412 * be deleted by the GC.
413 * Note that in the first loop, the "new" variable is only
414 * used as a flag.
415 */
416 new = NULL;
417 while (b->size - b->len < l) {
418 b->size += HLUA_CONCAT_BLOCSZ;
419 new = buffer;
420 }
421 if (new) {
422 new = lua_newuserdata(L, b->size);
423 memcpy(new, buffer, b->len);
424 lua_rawseti(L, 1, 1);
425 buffer = new;
426 }
427
428 /* Copy string, and update metadata. */
429 memcpy(buffer + b->len, str, l);
430 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100431 return 0;
432}
433
434static int hlua_concat_dump(lua_State *L)
435{
Thierry Fournier49d48422016-02-19 12:09:29 +0100436 struct hlua_concat *b;
437 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100438
439 /* First arg must be a concat object. */
440 b = hlua_check_concat(L, 1);
441
Thierry Fournier49d48422016-02-19 12:09:29 +0100442 /* Get the buffer. */
443 lua_rawgeti(L, 1, 1);
444 buffer = lua_touserdata(L, -1);
445 lua_pop(L, 1);
446
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500447 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100448 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100449 return 1;
450}
451
452int hlua_concat_new(lua_State *L)
453{
Thierry Fournier49d48422016-02-19 12:09:29 +0100454 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100455
456 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200457 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100458 b->size = HLUA_CONCAT_BLOCSZ;
459 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100460 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100461 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
462 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100463
464 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
465 lua_setmetatable(L, -2);
466
Thierry Fournier1de16592016-01-27 09:49:07 +0100467 return 1;
468}
469
470static int concat_tostring(lua_State *L)
471{
472 const void *ptr = lua_topointer(L, 1);
473 lua_pushfstring(L, "Concat object: %p", ptr);
474 return 1;
475}
476
Thierry Fournier599f2312022-09-30 10:40:39 +0200477static void hlua_concat_init(lua_State *L)
Thierry Fournier1de16592016-01-27 09:49:07 +0100478{
479 /* Creates the buffered concat object. */
480 lua_newtable(L);
481
482 lua_pushstring(L, "__tostring");
483 lua_pushcclosure(L, concat_tostring, 0);
484 lua_settable(L, -3);
485
486 lua_pushstring(L, "__index"); /* Creates the index entry. */
487 lua_newtable(L); /* The "__index" content. */
488
489 lua_pushstring(L, "add");
490 lua_pushcclosure(L, hlua_concat_add, 0);
491 lua_settable(L, -3);
492
493 lua_pushstring(L, "dump");
494 lua_pushcclosure(L, hlua_concat_dump, 0);
495 lua_settable(L, -3);
496
497 lua_settable(L, -3); /* Sets the __index entry. */
498 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Thierry Fournier1de16592016-01-27 09:49:07 +0100499}
500
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200501int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
502{
503 lua_newtable(L);
504
505 /* Pop a class stktbl metatable and affect it to the userdata. */
506 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
507 lua_setmetatable(L, -2);
508
509 lua_pushlightuserdata(L, tbl);
510 lua_rawseti(L, -2, 0);
511 return 1;
512}
513
514static struct stktable *hlua_check_stktable(lua_State *L, int ud)
515{
516 return hlua_checkudata(L, ud, class_stktable_ref);
517}
518
519/* Extract stick table attributes into Lua table */
520int hlua_stktable_info(lua_State *L)
521{
522 struct stktable *tbl;
523 int dt;
524
525 tbl = hlua_check_stktable(L, 1);
526
527 if (!tbl->id) {
528 lua_pushnil(L);
529 return 1;
530 }
531
532 lua_newtable(L);
533
534 lua_pushstring(L, "type");
535 lua_pushstring(L, stktable_types[tbl->type].kw);
536 lua_settable(L, -3);
537
538 lua_pushstring(L, "length");
539 lua_pushinteger(L, tbl->key_size);
540 lua_settable(L, -3);
541
542 lua_pushstring(L, "size");
543 hlua_fcn_pushunsigned(L, tbl->size);
544 lua_settable(L, -3);
545
546 lua_pushstring(L, "used");
547 hlua_fcn_pushunsigned(L, tbl->current);
548 lua_settable(L, -3);
549
550 lua_pushstring(L, "nopurge");
551 lua_pushboolean(L, tbl->nopurge > 0);
552 lua_settable(L, -3);
553
554 lua_pushstring(L, "expire");
555 lua_pushinteger(L, tbl->expire);
556 lua_settable(L, -3);
557
558 /* Save data types periods (if applicable) in 'data' table */
559 lua_pushstring(L, "data");
560 lua_newtable(L);
561
562 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
563 if (tbl->data_ofs[dt] == 0)
564 continue;
565
566 lua_pushstring(L, stktable_data_types[dt].name);
567
568 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
569 lua_pushinteger(L, tbl->data_arg[dt].u);
570 else
571 lua_pushinteger(L, -1);
572
573 lua_settable(L, -3);
574 }
575
576 lua_settable(L, -3);
577
578 return 1;
579}
580
581/* Helper to get extract stick table entry into Lua table */
582static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
583{
584 int dt;
585 void *ptr;
586
587 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
588
589 if (t->data_ofs[dt] == 0)
590 continue;
591
592 lua_pushstring(L, stktable_data_types[dt].name);
593
594 ptr = stktable_data_ptr(t, ts, dt);
595 switch (stktable_data_types[dt].std_type) {
596 case STD_T_SINT:
597 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
598 break;
599 case STD_T_UINT:
600 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
601 break;
602 case STD_T_ULL:
603 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
604 break;
605 case STD_T_FRQP:
606 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
607 t->data_arg[dt].u));
608 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200609 case STD_T_DICT: {
610 struct dict_entry *de;
611 de = stktable_data_cast(ptr, std_t_dict);
612 lua_pushstring(L, de ? (char *)de->value.key : "-");
613 break;
614 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200615 }
616
617 lua_settable(L, -3);
618 }
619}
620
621/* Looks in table <t> for a sticky session matching key <key>
622 * Returns table with session data or nil
623 *
624 * The returned table always contains 'use' and 'expire' (integer) fields.
625 * For frequency/rate counters, each data entry is returned as table with
626 * 'value' and 'period' fields.
627 */
628int hlua_stktable_lookup(lua_State *L)
629{
630 struct stktable *t;
631 struct sample smp;
632 struct stktable_key *skey;
633 struct stksess *ts;
634
635 t = hlua_check_stktable(L, 1);
636 smp.data.type = SMP_T_STR;
637 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600638 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200639
640 skey = smp_to_stkey(&smp, t);
641 if (!skey) {
642 lua_pushnil(L);
643 return 1;
644 }
645
646 ts = stktable_lookup_key(t, skey);
647 if (!ts) {
648 lua_pushnil(L);
649 return 1;
650 }
651
652 lua_newtable(L);
653 lua_pushstring(L, "use");
654 lua_pushinteger(L, ts->ref_cnt - 1);
655 lua_settable(L, -3);
656
657 lua_pushstring(L, "expire");
658 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
659 lua_settable(L, -3);
660
661 hlua_stktable_entry(L, t, ts);
Willy Tarreau76642222022-10-11 12:02:50 +0200662 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200663 ts->ref_cnt--;
Willy Tarreau76642222022-10-11 12:02:50 +0200664 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200665
666 return 1;
667}
668
669struct stk_filter {
670 long long val;
671 int type;
672 int op;
673};
674
675
676/* Helper for returning errors to callers using Lua convention (nil, err) */
677static int hlua_error(lua_State *L, const char *fmt, ...) {
678 char buf[256];
679 int len;
680 va_list args;
681 va_start(args, fmt);
682 len = vsnprintf(buf, sizeof(buf), fmt, args);
683 va_end(args);
684
685 if (len < 0) {
686 ha_alert("hlua_error(): Could not write error message.\n");
687 lua_pushnil(L);
688 return 1;
689 } else if (len >= sizeof(buf))
690 ha_alert("hlua_error(): Error message was truncated.\n");
691
692 lua_pushnil(L);
693 lua_pushstring(L, buf);
694
695 return 2;
696}
697
698/* Dump the contents of stick table <t>*/
699int hlua_stktable_dump(lua_State *L)
700{
701 struct stktable *t;
702 struct ebmb_node *eb;
703 struct ebmb_node *n;
704 struct stksess *ts;
705 int type;
706 int op;
707 int dt;
708 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100709 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200710 int filter_count = 0;
711 int i;
712 int skip_entry;
713 void *ptr;
714
715 t = hlua_check_stktable(L, 1);
716 type = lua_type(L, 2);
717
718 switch (type) {
719 case LUA_TNONE:
720 case LUA_TNIL:
721 break;
722 case LUA_TTABLE:
723 lua_pushnil(L);
724 while (lua_next(L, 2) != 0) {
725 int entry_idx = 0;
726
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100727 if (filter_count >= STKTABLE_FILTER_LEN)
728 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200729
730 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
731 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
732
733 lua_pushnil(L);
734 while (lua_next(L, -2) != 0) {
735 switch (entry_idx) {
736 case 0:
737 if (lua_type(L, -1) != LUA_TSTRING)
738 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
739
740 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
741 if (dt < 0 || t->data_ofs[dt] == 0)
742 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
743 filter[filter_count].type = dt;
744 break;
745 case 1:
746 if (lua_type(L, -1) != LUA_TSTRING)
747 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
748
749 op = get_std_op(lua_tostring(L, -1));
750 if (op < 0)
751 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
752 filter[filter_count].op = op;
753 break;
754 case 2:
755 val = lua_tointeger(L, -1);
756 filter[filter_count].val = val;
757 filter_count++;
758 break;
759 default:
760 break;
761 }
762 entry_idx++;
763 lua_pop(L, 1);
764 }
765 lua_pop(L, 1);
766 }
767 break;
768 default:
769 return hlua_error(L, "filter table expected");
770 }
771
772 lua_newtable(L);
773
Willy Tarreau76642222022-10-11 12:02:50 +0200774 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200775 eb = ebmb_first(&t->keys);
776 for (n = eb; n; n = ebmb_next(n)) {
777 ts = ebmb_entry(n, struct stksess, key);
778 if (!ts) {
Willy Tarreau76642222022-10-11 12:02:50 +0200779 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200780 return 1;
781 }
782 ts->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +0200783 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200784
785 /* multi condition/value filter */
786 skip_entry = 0;
787 for (i = 0; i < filter_count; i++) {
788 if (t->data_ofs[filter[i].type] == 0)
789 continue;
790
791 ptr = stktable_data_ptr(t, ts, filter[i].type);
792
793 switch (stktable_data_types[filter[i].type].std_type) {
794 case STD_T_SINT:
795 val = stktable_data_cast(ptr, std_t_sint);
796 break;
797 case STD_T_UINT:
798 val = stktable_data_cast(ptr, std_t_uint);
799 break;
800 case STD_T_ULL:
801 val = stktable_data_cast(ptr, std_t_ull);
802 break;
803 case STD_T_FRQP:
804 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
805 t->data_arg[filter[i].type].u);
806 break;
807 default:
808 continue;
809 break;
810 }
811
812 op = filter[i].op;
813
814 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
815 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
816 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
817 skip_entry = 1;
818 break;
819 }
820 }
821
822 if (skip_entry) {
Willy Tarreau76642222022-10-11 12:02:50 +0200823 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200824 ts->ref_cnt--;
825 continue;
826 }
827
828 if (t->type == SMP_T_IPV4) {
829 char addr[INET_ADDRSTRLEN];
830 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
831 lua_pushstring(L, addr);
832 } else if (t->type == SMP_T_IPV6) {
833 char addr[INET6_ADDRSTRLEN];
834 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
835 lua_pushstring(L, addr);
836 } else if (t->type == SMP_T_SINT) {
837 lua_pushinteger(L, *ts->key.key);
838 } else if (t->type == SMP_T_STR) {
839 lua_pushstring(L, (const char *)ts->key.key);
840 } else {
841 return hlua_error(L, "Unsupported stick table key type");
842 }
843
844 lua_newtable(L);
845 hlua_stktable_entry(L, t, ts);
846 lua_settable(L, -3);
Willy Tarreau76642222022-10-11 12:02:50 +0200847 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200848 ts->ref_cnt--;
849 }
Willy Tarreau76642222022-10-11 12:02:50 +0200850 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200851
852 return 1;
853}
854
Thierry Fournierff480422016-02-25 08:36:46 +0100855int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
856{
857 lua_newtable(L);
858
859 /* Pop a class sesison metatable and affect it to the userdata. */
860 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
861 lua_setmetatable(L, -2);
862
863 lua_pushlightuserdata(L, lst);
864 lua_rawseti(L, -2, 0);
865 return 1;
866}
867
868static struct listener *hlua_check_listener(lua_State *L, int ud)
869{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200870 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100871}
872
873int hlua_listener_get_stats(lua_State *L)
874{
875 struct listener *li;
876 int i;
877
878 li = hlua_check_listener(L, 1);
879
Willy Tarreauc95bad52016-12-22 00:13:31 +0100880 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100881 lua_pushnil(L);
882 return 1;
883 }
884
William Dauchy655e14e2021-02-14 23:22:54 +0100885 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
886 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +0100887
888 lua_newtable(L);
889 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200890 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100891 hlua_fcn_pushfield(L, &stats[i]);
892 lua_settable(L, -3);
893 }
894 return 1;
895
896}
897
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200898int hlua_server_gc(lua_State *L)
899{
900 struct server *srv = hlua_checkudata(L, 1, class_server_ref);
901
902 srv_drop(srv); /* srv_drop allows NULL srv */
903 return 0;
904}
905
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100906static struct server *hlua_check_server(lua_State *L, int ud)
907{
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200908 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200909 if (srv->flags & SRV_F_DELETED) {
910 return NULL;
911 }
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200912 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100913}
914
915int hlua_server_get_stats(lua_State *L)
916{
917 struct server *srv;
918 int i;
919
920 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200921 if (srv == NULL) {
922 lua_pushnil(L);
923 return 1;
924 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100925
926 if (!srv->proxy) {
927 lua_pushnil(L);
928 return 1;
929 }
930
William Dauchyd3a9a492021-01-25 17:29:03 +0100931 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
932 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100933
934 lua_newtable(L);
935 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200936 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100937 hlua_fcn_pushfield(L, &stats[i]);
938 lua_settable(L, -3);
939 }
940 return 1;
941
942}
943
944int hlua_server_get_addr(lua_State *L)
945{
946 struct server *srv;
947 char addr[INET6_ADDRSTRLEN];
948 luaL_Buffer b;
949
950 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200951 if (srv == NULL) {
952 lua_pushnil(L);
953 return 1;
954 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100955
956 luaL_buffinit(L, &b);
957
958 switch (srv->addr.ss_family) {
959 case AF_INET:
960 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
961 addr, INET_ADDRSTRLEN);
962 luaL_addstring(&b, addr);
963 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400964 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100965 luaL_addstring(&b, addr);
966 break;
967 case AF_INET6:
968 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400969 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100970 luaL_addstring(&b, addr);
971 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400972 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100973 luaL_addstring(&b, addr);
974 break;
975 case AF_UNIX:
976 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
977 break;
978 default:
979 luaL_addstring(&b, "<unknown>");
980 break;
981 }
982
983 luaL_pushresult(&b);
984 return 1;
985}
986
Thierry Fournierb0467732022-10-07 12:07:24 +0200987int hlua_server_get_puid(lua_State *L)
988{
989 struct server *srv;
990 char buffer[12];
991
992 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200993 if (srv == NULL) {
994 lua_pushnil(L);
995 return 1;
996 }
Thierry Fournierb0467732022-10-07 12:07:24 +0200997
998 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
999 lua_pushstring(L, buffer);
1000 return 1;
1001}
1002
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001003int hlua_server_get_rid(lua_State *L)
1004{
1005 struct server *srv;
1006 char buffer[12];
1007
1008 srv = hlua_check_server(L, 1);
1009 if (srv == NULL) {
1010 lua_pushnil(L);
1011 return 1;
1012 }
1013
1014 snprintf(buffer, sizeof(buffer), "%d", srv->rid);
1015 lua_pushstring(L, buffer);
1016 return 1;
1017}
1018
Thierry Fournierb0467732022-10-07 12:07:24 +02001019int hlua_server_get_name(lua_State *L)
1020{
1021 struct server *srv;
1022
1023 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001024 if (srv == NULL) {
1025 lua_pushnil(L);
1026 return 1;
1027 }
1028
Thierry Fournierb0467732022-10-07 12:07:24 +02001029 lua_pushstring(L, srv->id);
1030 return 1;
1031}
1032
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001033/* __index metamethod for server class
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001034 * support for additional keys that are missing from the main table
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001035 * stack:1 = table (server class), stack:2 = requested key
1036 * Returns 1 if key is supported
1037 * else returns 0 to make lua return NIL value to the caller
1038 */
1039static int hlua_server_index(struct lua_State *L)
1040{
1041 const char *key = lua_tostring(L, 2);
1042
1043 if (!strcmp(key, "name")) {
1044 if (ONLY_ONCE())
1045 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, ", "));
1046 lua_pushvalue(L, 1);
1047 hlua_server_get_name(L);
1048 return 1;
1049 }
1050 if (!strcmp(key, "puid")) {
1051 if (ONLY_ONCE())
1052 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, ", "));
1053 lua_pushvalue(L, 1);
1054 hlua_server_get_puid(L);
1055 return 1;
1056 }
1057 /* unknown attribute */
1058 return 0;
1059}
1060
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001061int hlua_server_is_draining(lua_State *L)
1062{
1063 struct server *srv;
1064
1065 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001066 if (srv == NULL) {
1067 lua_pushnil(L);
1068 return 1;
1069 }
1070
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001071 lua_pushinteger(L, server_is_draining(srv));
1072 return 1;
1073}
1074
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001075int hlua_server_set_maxconn(lua_State *L)
1076{
1077 struct server *srv;
1078 const char *maxconn;
1079 const char *err;
1080
1081 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001082 if (srv == NULL) {
1083 lua_pushnil(L);
1084 return 1;
1085 }
1086
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001087 maxconn = luaL_checkstring(L, 2);
1088
1089 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1090 err = server_parse_maxconn_change_request(srv, maxconn);
1091 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1092 if (!err)
1093 lua_pushnil(L);
1094 else
1095 hlua_pushstrippedstring(L, err);
1096 return 1;
1097}
1098
1099int hlua_server_get_maxconn(lua_State *L)
1100{
1101 struct server *srv;
1102
1103 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001104 if (srv == NULL) {
1105 lua_pushnil(L);
1106 return 1;
1107 }
1108
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001109 lua_pushinteger(L, srv->maxconn);
1110 return 1;
1111}
1112
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001113int hlua_server_set_weight(lua_State *L)
1114{
1115 struct server *srv;
1116 const char *weight;
1117 const char *err;
1118
1119 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001120 if (srv == NULL) {
1121 lua_pushnil(L);
1122 return 1;
1123 }
1124
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001125 weight = luaL_checkstring(L, 2);
1126
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001127 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001128 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001129 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001130 if (!err)
1131 lua_pushnil(L);
1132 else
1133 hlua_pushstrippedstring(L, err);
1134 return 1;
1135}
1136
1137int hlua_server_get_weight(lua_State *L)
1138{
1139 struct server *srv;
1140
1141 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001142 if (srv == NULL) {
1143 lua_pushnil(L);
1144 return 1;
1145 }
1146
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001147 lua_pushinteger(L, srv->uweight);
1148 return 1;
1149}
1150
1151int hlua_server_set_addr(lua_State *L)
1152{
1153 struct server *srv;
1154 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001155 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001156 const char *err;
1157
1158 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001159 if (srv == NULL) {
1160 lua_pushnil(L);
1161 return 1;
1162 }
1163
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001164 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001165 if (lua_gettop(L) >= 3)
1166 port = luaL_checkstring(L, 3);
1167 else
1168 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001169
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001170 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001171 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001172 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001173 if (!err)
1174 lua_pushnil(L);
1175 else
1176 hlua_pushstrippedstring(L, err);
1177 return 1;
1178}
1179
1180int hlua_server_shut_sess(lua_State *L)
1181{
1182 struct server *srv;
1183
1184 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001185 if (srv == NULL) {
1186 return 0;
1187 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001188 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001189 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001190 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001191 return 0;
1192}
1193
1194int hlua_server_set_drain(lua_State *L)
1195{
1196 struct server *srv;
1197
1198 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001199 if (srv == NULL) {
1200 return 0;
1201 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001202 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001203 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001204 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001205 return 0;
1206}
1207
1208int hlua_server_set_maint(lua_State *L)
1209{
1210 struct server *srv;
1211
1212 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001213 if (srv == NULL) {
1214 return 0;
1215 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001216 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001217 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001219 return 0;
1220}
1221
1222int hlua_server_set_ready(lua_State *L)
1223{
1224 struct server *srv;
1225
1226 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001227 if (srv == NULL) {
1228 return 0;
1229 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001230 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001231 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001232 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001233 return 0;
1234}
1235
1236int hlua_server_check_enable(lua_State *L)
1237{
1238 struct server *sv;
1239
1240 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001241 if (sv == NULL) {
1242 return 0;
1243 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001244 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001245 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001246 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001247 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001248 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001249 return 0;
1250}
1251
1252int hlua_server_check_disable(lua_State *L)
1253{
1254 struct server *sv;
1255
1256 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001257 if (sv == NULL) {
1258 return 0;
1259 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001260 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001261 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001262 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001263 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001264 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001265 return 0;
1266}
1267
1268int hlua_server_check_force_up(lua_State *L)
1269{
1270 struct server *sv;
1271
1272 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001273 if (sv == NULL) {
1274 return 0;
1275 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001276 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001277 if (!(sv->track)) {
1278 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001279 srv_set_running(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001280 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001281 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001282 return 0;
1283}
1284
1285int hlua_server_check_force_nolb(lua_State *L)
1286{
1287 struct server *sv;
1288
1289 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001290 if (sv == NULL) {
1291 return 0;
1292 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001293 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001294 if (!(sv->track)) {
1295 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001296 srv_set_stopping(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001297 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001298 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001299 return 0;
1300}
1301
1302int hlua_server_check_force_down(lua_State *L)
1303{
1304 struct server *sv;
1305
1306 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001307 if (sv == NULL) {
1308 return 0;
1309 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001310 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001311 if (!(sv->track)) {
1312 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001313 srv_set_stopped(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001314 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001315 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001316 return 0;
1317}
1318
1319int hlua_server_agent_enable(lua_State *L)
1320{
1321 struct server *sv;
1322
1323 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001324 if (sv == NULL) {
1325 return 0;
1326 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001327 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001328 if (sv->agent.state & CHK_ST_CONFIGURED) {
1329 sv->agent.state |= CHK_ST_ENABLED;
1330 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001331 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001332 return 0;
1333}
1334
1335int hlua_server_agent_disable(lua_State *L)
1336{
1337 struct server *sv;
1338
1339 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001340 if (sv == NULL) {
1341 return 0;
1342 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001343 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001344 if (sv->agent.state & CHK_ST_CONFIGURED) {
1345 sv->agent.state &= ~CHK_ST_ENABLED;
1346 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001347 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001348 return 0;
1349}
1350
1351int hlua_server_agent_force_up(lua_State *L)
1352{
1353 struct server *sv;
1354
1355 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001356 if (sv == NULL) {
1357 return 0;
1358 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001359 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001360 if (sv->agent.state & CHK_ST_ENABLED) {
1361 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001362 srv_set_running(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001363 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001364 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001365 return 0;
1366}
1367
1368int hlua_server_agent_force_down(lua_State *L)
1369{
1370 struct server *sv;
1371
1372 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001373 if (sv == NULL) {
1374 return 0;
1375 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001376 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001377 if (sv->agent.state & CHK_ST_ENABLED) {
1378 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001379 srv_set_stopped(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001380 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001381 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001382 return 0;
1383}
1384
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001385/* hlua_event_sub wrapper for per-server subscription:
1386 *
1387 * hlua_event_sub() is called with sv->e_subs subscription list and
1388 * lua arguments are passed as-is (skipping the first argument which
1389 * is the server ctx)
1390 */
1391int hlua_server_event_sub(lua_State *L)
1392{
1393 struct server *sv;
1394
1395 sv = hlua_check_server(L, 1);
1396 if (sv == NULL) {
1397 return 0;
1398 }
1399 /* remove first argument from the stack (server) */
1400 lua_remove(L, 1);
1401
1402 /* try to subscribe within server's subscription list */
1403 return hlua_event_sub(L, &sv->e_subs);
1404}
1405
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001406int hlua_fcn_new_server(lua_State *L, struct server *srv)
1407{
1408 lua_newtable(L);
1409
1410 /* Pop a class server metatable and affect it to the userdata. */
1411 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
1412 lua_setmetatable(L, -2);
1413
1414 lua_pushlightuserdata(L, srv);
1415 lua_rawseti(L, -2, 0);
1416
1417 /* userdata is affected: increment server refcount */
1418 srv_take(srv);
1419
1420 /* set public methods */
1421 hlua_class_function(L, "get_name", hlua_server_get_name);
1422 hlua_class_function(L, "get_puid", hlua_server_get_puid);
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001423 hlua_class_function(L, "get_rid", hlua_server_get_rid);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001424 hlua_class_function(L, "is_draining", hlua_server_is_draining);
1425 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1426 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
1427 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1428 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1429 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1430 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1431 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1432 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1433 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1434 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1435 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1436 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1437 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1438 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1439 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1440 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1441 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1442 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1443 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1444 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001445 hlua_class_function(L, "event_sub", hlua_server_event_sub);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001446
1447 return 1;
1448}
1449
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001450static struct hlua_server_list *hlua_check_server_list(lua_State *L, int ud)
1451{
1452 return hlua_checkudata(L, ud, class_server_list_ref);
1453}
1454
1455/* does nothing and returns 0, only prevents insertions in the
1456 * table which represents the list of servers
1457 */
1458int hlua_listable_servers_newindex(lua_State *L) {
1459 return 0;
1460}
1461
1462/* first arg is the table (struct hlua_server_list * in metadata)
1463 * second arg is the required index
1464 */
1465int hlua_listable_servers_index(lua_State *L)
Thierry Fournierf61aa632016-02-19 20:56:00 +01001466{
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001467 struct hlua_server_list *hlua_srv;
1468 const char *name;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001469 struct server *srv;
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001470
1471 hlua_srv = hlua_check_server_list(L, 1);
1472 name = luaL_checkstring(L, 2);
1473
1474 /* Perform a server lookup in px list */
1475 srv = server_find_by_name(hlua_srv->px, name);
1476 if (srv == NULL) {
1477 lua_pushnil(L);
1478 return 1;
1479 }
1480
1481 hlua_fcn_new_server(L, srv);
1482 return 1;
1483}
1484
1485/* iterator must return key as string and value as server
1486 * object, if we reach end of list, it returns nil.
1487 * The context knows the last returned server. if the
1488 * context contains srv == NULL, we start enumeration.
1489 * Then, use 'srv->next' ptr to iterate through the list
1490 */
1491int hlua_listable_servers_pairs_iterator(lua_State *L)
1492{
1493 int context_index;
1494 struct hlua_server_list_iterator_context *ctx;
1495
1496 context_index = lua_upvalueindex(1);
1497 ctx = lua_touserdata(L, context_index);
1498
1499 if (ctx->cur == NULL) {
1500 /* First iteration, initialize list on the first server */
1501 ctx->cur = ctx->px->srv;
1502 } else {
1503
1504 /* Next server (next ptr is always valid, even if current
1505 * server has the SRV_F_DELETED flag set)
1506 */
1507 ctx->cur = ctx->cur->next;
1508 }
1509
1510 /* next server is null, end of iteration */
1511 if (ctx->cur == NULL) {
1512 lua_pushnil(L);
1513 return 1;
1514 }
1515
1516 lua_pushstring(L, ctx->cur->id);
1517 hlua_fcn_new_server(L, ctx->cur);
1518 return 2;
1519}
1520
1521/* init the iterator context, return iterator function
1522 * with context as closure. The only argument is a
1523 * server list object.
1524 */
1525int hlua_listable_servers_pairs(lua_State *L)
1526{
1527 struct hlua_server_list_iterator_context *ctx;
1528 struct hlua_server_list *hlua_srv_list;
1529
1530 hlua_srv_list = hlua_check_server_list(L, 1);
1531
1532 ctx = lua_newuserdata(L, sizeof(*ctx));
1533 ctx->px = hlua_srv_list->px;
1534 ctx->cur = NULL;
1535
1536 lua_pushcclosure(L, hlua_listable_servers_pairs_iterator, 1);
1537 return 1;
1538}
1539
1540void hlua_listable_servers(lua_State *L, struct proxy *px)
1541{
1542 struct hlua_server_list *list;
1543
1544 lua_newtable(L);
1545 list = lua_newuserdata(L, sizeof(*list));
1546 list->px = px;
1547 lua_rawseti(L, -2, 0);
1548 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_list_ref);
1549 lua_setmetatable(L, -2);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001550}
1551
1552static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1553{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001554 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001555}
1556
Thierry Fournierb0467732022-10-07 12:07:24 +02001557int hlua_proxy_get_name(lua_State *L)
1558{
1559 struct proxy *px;
1560
1561 px = hlua_check_proxy(L, 1);
1562 lua_pushstring(L, px->id);
1563 return 1;
1564}
1565
1566int hlua_proxy_get_uuid(lua_State *L)
1567{
1568 struct proxy *px;
1569 char buffer[17];
1570
1571 px = hlua_check_proxy(L, 1);
1572 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1573 lua_pushstring(L, buffer);
1574 return 1;
1575}
1576
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001577/* __index metamethod for proxy class
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001578 * support for additional keys that are missing from the main table
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001579 * stack:1 = table (proxy class), stack:2 = requested key
1580 * Returns 1 if key is supported
1581 * else returns 0 to make lua return NIL value to the caller
1582 */
1583static int hlua_proxy_index(struct lua_State *L)
1584{
1585 const char *key = lua_tostring(L, 2);
1586
1587 if (!strcmp(key, "name")) {
1588 if (ONLY_ONCE())
1589 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, ", "));
1590 lua_pushvalue(L, 1);
1591 hlua_proxy_get_name(L);
1592 return 1;
1593 }
1594 if (!strcmp(key, "uuid")) {
1595 if (ONLY_ONCE())
1596 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, ", "));
1597 lua_pushvalue(L, 1);
1598 hlua_proxy_get_uuid(L);
1599 return 1;
1600 }
1601 /* unknown attribute */
1602 return 0;
1603}
1604
Thierry Fournierf61aa632016-02-19 20:56:00 +01001605int hlua_proxy_pause(lua_State *L)
1606{
1607 struct proxy *px;
1608
1609 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001610 /* safe to call without PROXY_LOCK - pause_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001611 pause_proxy(px);
1612 return 0;
1613}
1614
1615int hlua_proxy_resume(lua_State *L)
1616{
1617 struct proxy *px;
1618
1619 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001620 /* safe to call without PROXY_LOCK - resume_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001621 resume_proxy(px);
1622 return 0;
1623}
1624
1625int hlua_proxy_stop(lua_State *L)
1626{
1627 struct proxy *px;
1628
1629 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001630 /* safe to call without PROXY_LOCK - stop_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001631 stop_proxy(px);
1632 return 0;
1633}
1634
1635int hlua_proxy_get_cap(lua_State *L)
1636{
1637 struct proxy *px;
1638 const char *str;
1639
1640 px = hlua_check_proxy(L, 1);
1641 str = proxy_cap_str(px->cap);
1642 lua_pushstring(L, str);
1643 return 1;
1644}
1645
1646int hlua_proxy_get_stats(lua_State *L)
1647{
1648 struct proxy *px;
1649 int i;
1650
1651 px = hlua_check_proxy(L, 1);
1652 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001653 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001654 else
William Dauchy0ef54392021-01-17 18:27:45 +01001655 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001656 lua_newtable(L);
1657 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001658 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001659 hlua_fcn_pushfield(L, &stats[i]);
1660 lua_settable(L, -3);
1661 }
1662 return 1;
1663}
1664
1665int hlua_proxy_get_mode(lua_State *L)
1666{
1667 struct proxy *px;
1668 const char *str;
1669
1670 px = hlua_check_proxy(L, 1);
1671 str = proxy_mode_str(px->mode);
1672 lua_pushstring(L, str);
1673 return 1;
1674}
1675
1676int hlua_proxy_shut_bcksess(lua_State *L)
1677{
1678 struct proxy *px;
1679
1680 px = hlua_check_proxy(L, 1);
1681 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1682 return 0;
1683}
1684
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001685int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1686{
1687 struct listener *lst;
1688 int lid;
1689 char buffer[17];
1690
1691 lua_newtable(L);
1692
1693 /* Pop a class proxy metatable and affect it to the userdata. */
1694 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1695 lua_setmetatable(L, -2);
1696
1697 lua_pushlightuserdata(L, px);
1698 lua_rawseti(L, -2, 0);
1699
1700 /* set public methods */
1701 hlua_class_function(L, "get_name", hlua_proxy_get_name);
1702 hlua_class_function(L, "get_uuid", hlua_proxy_get_uuid);
1703 hlua_class_function(L, "pause", hlua_proxy_pause);
1704 hlua_class_function(L, "resume", hlua_proxy_resume);
1705 hlua_class_function(L, "stop", hlua_proxy_stop);
1706 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1707 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1708 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1709 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1710
1711 /* Browse and register servers. */
1712 lua_pushstring(L, "servers");
1713 hlua_listable_servers(L, px);
1714 lua_settable(L, -3);
1715
1716 /* Browse and register listeners. */
1717 lua_pushstring(L, "listeners");
1718 lua_newtable(L);
1719 lid = 1;
1720 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1721 if (lst->name)
1722 lua_pushstring(L, lst->name);
1723 else {
1724 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
1725 lid++;
1726 lua_pushstring(L, buffer);
1727 }
1728 hlua_fcn_new_listener(L, lst);
1729 lua_settable(L, -3);
1730 }
1731 lua_settable(L, -3);
1732
1733 if (px->table && px->table->id) {
1734 lua_pushstring(L, "stktable");
1735 hlua_fcn_new_stktable(L, px->table);
1736 lua_settable(L, -3);
1737 }
1738
1739 return 1;
1740}
1741
Thierry Fournier467913c2022-09-30 11:03:38 +02001742static struct hlua_proxy_list *hlua_check_proxy_list(lua_State *L, int ud)
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001743{
Thierry Fournier467913c2022-09-30 11:03:38 +02001744 return hlua_checkudata(L, ud, class_proxy_list_ref);
1745}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001746
Thierry Fournier467913c2022-09-30 11:03:38 +02001747/* does nothing and returns 0, only prevents insertions in the
1748 * table which represent list of proxies
1749 */
1750int hlua_listable_proxies_newindex(lua_State *L) {
1751 return 0;
1752}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001753
Thierry Fournier467913c2022-09-30 11:03:38 +02001754/* first arg is the table (struct hlua_proxy_list * in metadata)
1755 * second arg is the required index
1756 */
1757int hlua_listable_proxies_index(lua_State *L)
1758{
1759 struct hlua_proxy_list *hlua_px;
1760 const char *name;
1761 struct proxy *px;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001762
Thierry Fournier467913c2022-09-30 11:03:38 +02001763 hlua_px = hlua_check_proxy_list(L, 1);
1764 name = luaL_checkstring(L, 2);
1765
1766 px = NULL;
1767 if (hlua_px->capabilities & PR_CAP_FE) {
1768 px = proxy_find_by_name(name, PR_CAP_FE, 0);
1769 }
1770 if (!px && hlua_px->capabilities & PR_CAP_BE) {
1771 px = proxy_find_by_name(name, PR_CAP_BE, 0);
1772 }
1773 if (px == NULL) {
1774 lua_pushnil(L);
1775 return 1;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001776 }
1777
Thierry Fournier467913c2022-09-30 11:03:38 +02001778 hlua_fcn_new_proxy(L, px);
1779 return 1;
1780}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001781
Thierry Fournier467913c2022-09-30 11:03:38 +02001782static inline int hlua_listable_proxies_match(struct proxy *px, char cap) {
1783 return ((px->cap & cap) && !(px->cap & (PR_CAP_DEF | PR_CAP_INT)));
1784}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001785
Thierry Fournier467913c2022-09-30 11:03:38 +02001786/* iterator must return key as string and value as proxy
1787 * object, if we reach end of list, it returns nil
1788 */
1789int hlua_listable_proxies_pairs_iterator(lua_State *L)
1790{
1791 int context_index;
1792 struct hlua_proxy_list_iterator_context *ctx;
1793
1794 context_index = lua_upvalueindex(1);
1795 ctx = lua_touserdata(L, context_index);
1796
1797 if (ctx->next == NULL) {
1798 lua_pushnil(L);
1799 return 1;
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001800 }
1801
Thierry Fournier467913c2022-09-30 11:03:38 +02001802 lua_pushstring(L, ctx->next->id);
1803 hlua_fcn_new_proxy(L, ctx->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001804
Thierry Fournier467913c2022-09-30 11:03:38 +02001805 for (ctx->next = ctx->next->next;
1806 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
1807 ctx->next = ctx->next->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001808
Thierry Fournier467913c2022-09-30 11:03:38 +02001809 return 2;
1810}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001811
Thierry Fournier467913c2022-09-30 11:03:38 +02001812/* init the iterator context, return iterator function
1813 * with context as closure. The only argument is a
1814 * proxy object.
1815 */
1816int hlua_listable_proxies_pairs(lua_State *L)
1817{
1818 struct hlua_proxy_list_iterator_context *ctx;
1819 struct hlua_proxy_list *hlua_px;
1820
1821 hlua_px = hlua_check_proxy_list(L, 1);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001822
Thierry Fournier467913c2022-09-30 11:03:38 +02001823 ctx = lua_newuserdata(L, sizeof(*ctx));
1824
1825 ctx->capabilities = hlua_px->capabilities;
1826 for (ctx->next = proxies_list;
1827 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
1828 ctx->next = ctx->next->next);
1829 lua_pushcclosure(L, hlua_listable_proxies_pairs_iterator, 1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001830 return 1;
1831}
1832
Thierry Fournier467913c2022-09-30 11:03:38 +02001833void hlua_listable_proxies(lua_State *L, char capabilities)
1834{
1835 struct hlua_proxy_list *list;
1836
1837 lua_newtable(L);
1838 list = lua_newuserdata(L, sizeof(*list));
1839 list->capabilities = capabilities;
1840 lua_rawseti(L, -2, 0);
1841 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_list_ref);
1842 lua_setmetatable(L, -2);
1843}
1844
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001845int hlua_event_sub_unsub(lua_State *L)
1846{
1847 struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref);
1848
1849 BUG_ON(!sub);
1850 event_hdl_take(sub); /* keep a reference on sub until the item is GCed */
1851 event_hdl_unsubscribe(sub); /* will automatically call event_hdl_drop() */
1852 return 0;
1853}
1854
1855int hlua_event_sub_gc(lua_State *L)
1856{
1857 struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref);
1858
1859 BUG_ON(!sub);
1860 event_hdl_drop(sub); /* final drop of the reference */
1861 return 0;
1862}
1863
1864int hlua_fcn_new_event_sub(lua_State *L, struct event_hdl_sub *sub)
1865{
1866 lua_newtable(L);
1867
1868 /* Pop a class event_sub metatable and affect it to the userdata. */
1869 lua_rawgeti(L, LUA_REGISTRYINDEX, class_event_sub_ref);
1870 lua_setmetatable(L, -2);
1871
1872 lua_pushlightuserdata(L, sub);
1873 lua_rawseti(L, -2, 0);
1874
1875 /* userdata is affected: increment sub refcount */
1876 event_hdl_take(sub);
1877
1878 /* set public methods */
1879 hlua_class_function(L, "unsub", hlua_event_sub_unsub);
1880
1881 return 1;
1882}
1883
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001884/* This Lua function take a string, a list of separators.
1885 * It tokenize the input string using the list of separators
1886 * as separator.
1887 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001888 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001889 */
1890int hlua_tokenize(lua_State *L)
1891{
1892 const char *str;
1893 const char *sep;
1894 int index;
1895 const char *token;
1896 const char *p;
1897 const char *c;
1898 int ignore_empty;
1899
1900 ignore_empty = 0;
1901
1902 str = luaL_checkstring(L, 1);
1903 sep = luaL_checkstring(L, 2);
1904 if (lua_gettop(L) == 3)
1905 ignore_empty = hlua_checkboolean(L, 3);
1906
1907 lua_newtable(L);
1908 index = 1;
1909 token = str;
1910 p = str;
1911 while(1) {
1912 for (c = sep; *c != '\0'; c++)
1913 if (*p == *c)
1914 break;
1915 if (*p == *c) {
1916 if ((!ignore_empty) || (p - token > 0)) {
1917 lua_pushlstring(L, token, p - token);
1918 lua_rawseti(L, -2, index);
1919 index++;
1920 }
1921 token = p + 1;
1922 }
1923 if (*p == '\0')
1924 break;
1925 p++;
1926 }
1927
1928 return 1;
1929}
1930
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001931int hlua_parse_addr(lua_State *L)
1932{
Christopher Faulet29e93262021-02-26 09:39:05 +01001933 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001934 const char *str = luaL_checkstring(L, 1);
1935 unsigned char mask;
1936
Christopher Faulet29e93262021-02-26 09:39:05 +01001937 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001938 if (!addr) {
1939 lua_pushnil(L);
1940 return 1;
1941 }
1942
1943 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001944 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001945 return 1;
1946 }
1947
1948 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1949 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001950 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001951 return 1;
1952 }
1953
1954 lua_pop(L, 1);
1955 lua_pushnil(L);
1956 return 1;
1957}
1958
1959int hlua_match_addr(lua_State *L)
1960{
Christopher Faulet29e93262021-02-26 09:39:05 +01001961 struct net_addr *addr1;
1962 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001963
1964 if (!lua_isuserdata(L, 1) ||
1965 !lua_isuserdata(L, 2)) {
1966 lua_pushboolean(L, 0);
1967 return 1;
1968 }
1969
1970 addr1 = lua_touserdata(L, 1);
1971 addr2 = lua_touserdata(L, 2);
1972
Christopher Faulet29e93262021-02-26 09:39:05 +01001973 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001974 lua_pushboolean(L, 0);
1975 return 1;
1976 }
1977
Christopher Faulet29e93262021-02-26 09:39:05 +01001978 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001979 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1980 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1981 lua_pushboolean(L, 1);
1982 return 1;
1983 }
1984 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001985 int i;
1986
1987 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001988 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1989 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1990 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1991 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001992 break;
1993 }
1994 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001995 lua_pushboolean(L, 1);
1996 return 1;
1997 }
1998 }
1999
2000 lua_pushboolean(L, 0);
2001 return 1;
2002}
2003
Dragan Dosen26743032019-04-30 15:54:36 +02002004static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02002005{
2006 return (hlua_checkudata(L, ud, class_regex_ref));
2007}
2008
2009static int hlua_regex_comp(struct lua_State *L)
2010{
Dragan Dosen26743032019-04-30 15:54:36 +02002011 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002012 const char *str;
2013 int cs;
2014 char *err;
2015
2016 str = luaL_checkstring(L, 1);
2017 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
2018 cs = lua_toboolean(L, 2);
2019
2020 regex = lua_newuserdata(L, sizeof(*regex));
2021
2022 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02002023 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02002024 lua_pushboolean(L, 0); /* status error */
2025 lua_pushstring(L, err); /* Reason */
2026 free(err);
2027 return 2;
2028 }
2029
2030 lua_pushboolean(L, 1); /* Status ok */
2031
2032 /* Create object */
2033 lua_newtable(L);
2034 lua_pushvalue(L, -3); /* Get the userdata pointer. */
2035 lua_rawseti(L, -2, 0);
2036 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
2037 lua_setmetatable(L, -2);
2038 return 2;
2039}
2040
2041static int hlua_regex_exec(struct lua_State *L)
2042{
Dragan Dosen26743032019-04-30 15:54:36 +02002043 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002044 const char *str;
2045 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02002046 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002047
2048 regex = hlua_check_regex(L, 1);
2049 str = luaL_checklstring(L, 2, &len);
2050
Dragan Dosen26743032019-04-30 15:54:36 +02002051 if (!*regex) {
2052 lua_pushboolean(L, 0);
2053 return 1;
2054 }
2055
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002056 /* Copy the string because regex_exec2 require a 'char *'
2057 * and not a 'const char *'.
2058 */
2059 tmp = get_trash_chunk();
2060 if (len >= tmp->size) {
2061 lua_pushboolean(L, 0);
2062 return 1;
2063 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002064 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002065
Dragan Dosen26743032019-04-30 15:54:36 +02002066 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02002067
2068 return 1;
2069}
2070
2071static int hlua_regex_match(struct lua_State *L)
2072{
Dragan Dosen26743032019-04-30 15:54:36 +02002073 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002074 const char *str;
2075 size_t len;
2076 regmatch_t pmatch[20];
2077 int ret;
2078 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02002079 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002080
2081 regex = hlua_check_regex(L, 1);
2082 str = luaL_checklstring(L, 2, &len);
2083
Dragan Dosen26743032019-04-30 15:54:36 +02002084 if (!*regex) {
2085 lua_pushboolean(L, 0);
2086 return 1;
2087 }
2088
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002089 /* Copy the string because regex_exec2 require a 'char *'
2090 * and not a 'const char *'.
2091 */
2092 tmp = get_trash_chunk();
2093 if (len >= tmp->size) {
2094 lua_pushboolean(L, 0);
2095 return 1;
2096 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002097 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002098
Dragan Dosen26743032019-04-30 15:54:36 +02002099 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02002100 lua_pushboolean(L, ret);
2101 lua_newtable(L);
2102 if (ret) {
2103 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
2104 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
2105 lua_rawseti(L, -2, i + 1);
2106 }
2107 }
2108 return 2;
2109}
2110
2111static int hlua_regex_free(struct lua_State *L)
2112{
Dragan Dosen26743032019-04-30 15:54:36 +02002113 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002114
2115 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02002116 regex_free(*regex);
2117 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002118 return 0;
2119}
2120
Thierry Fournier599f2312022-09-30 10:40:39 +02002121void hlua_fcn_reg_core_fcn(lua_State *L)
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002122{
Thierry Fournier599f2312022-09-30 10:40:39 +02002123 hlua_concat_init(L);
Thierry Fournier1de16592016-01-27 09:49:07 +01002124
Thierry Fournier4f99b272016-02-22 08:40:02 +01002125 hlua_class_function(L, "now", hlua_now);
2126 hlua_class_function(L, "http_date", hlua_http_date);
2127 hlua_class_function(L, "imf_date", hlua_imf_date);
2128 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
2129 hlua_class_function(L, "asctime_date", hlua_asctime_date);
2130 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01002131 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002132 hlua_class_function(L, "parse_addr", hlua_parse_addr);
2133 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002134 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01002135
Thierry FOURNIER31904272017-10-25 12:59:51 +02002136 /* Create regex object. */
2137 lua_newtable(L);
2138 hlua_class_function(L, "new", hlua_regex_comp);
2139
2140 lua_newtable(L); /* The metatable. */
2141 lua_pushstring(L, "__index");
2142 lua_newtable(L);
2143 hlua_class_function(L, "exec", hlua_regex_exec);
2144 hlua_class_function(L, "match", hlua_regex_match);
2145 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
2146 hlua_class_function(L, "__gc", hlua_regex_free);
2147
2148 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
2149 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
2150
2151 lua_setmetatable(L, -2);
2152 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
2153
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002154 /* Create stktable object. */
2155 lua_newtable(L);
2156 lua_pushstring(L, "__index");
2157 lua_newtable(L);
2158 hlua_class_function(L, "info", hlua_stktable_info);
2159 hlua_class_function(L, "lookup", hlua_stktable_lookup);
2160 hlua_class_function(L, "dump", hlua_stktable_dump);
2161 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2162 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
2163
Thierry Fournierff480422016-02-25 08:36:46 +01002164 /* Create listener object. */
2165 lua_newtable(L);
2166 lua_pushstring(L, "__index");
2167 lua_newtable(L);
2168 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
2169 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2170 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
2171
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01002172 /* Create event_sub object. */
2173 lua_newtable(L);
2174 hlua_class_function(L, "__gc", hlua_event_sub_gc);
2175 class_event_sub_ref = hlua_register_metatable(L, CLASS_EVENT_SUB);
2176
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002177 /* Create server object. */
2178 lua_newtable(L);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002179 hlua_class_function(L, "__gc", hlua_server_gc);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002180 hlua_class_function(L, "__index", hlua_server_index);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002181 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
2182
Thierry Fournierf61aa632016-02-19 20:56:00 +01002183 /* Create proxy object. */
2184 lua_newtable(L);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002185 hlua_class_function(L, "__index", hlua_proxy_index);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002186 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
2187
Thierry Fournier467913c2022-09-30 11:03:38 +02002188 /* list of proxy objects. Instead of having a static array
2189 * of proxies, we use special metamethods that rely on internal
2190 * proxies list so that the array is resolved at runtime.
2191 *
2192 * To emulate the same behavior than Lua array, we implement some
2193 * metatable functions:
2194 * - __newindex : prevent the insertion of a new item in the array
2195 * - __index : find a proxy in the list using "name" index
2196 * - __pairs : iterate through available proxies in the list
2197 */
2198 lua_newtable(L);
2199 hlua_class_function(L, "__index", hlua_listable_proxies_index);
2200 hlua_class_function(L, "__newindex", hlua_listable_proxies_newindex);
2201 hlua_class_function(L, "__pairs", hlua_listable_proxies_pairs);
2202 class_proxy_list_ref = hlua_register_metatable(L, CLASS_PROXY_LIST);
2203
2204 /* Create proxies entry. */
2205 lua_pushstring(L, "proxies");
2206 hlua_listable_proxies(L, PR_CAP_LISTEN);
2207 lua_settable(L, -3);
2208
2209 /* Create frontends entry. */
2210 lua_pushstring(L, "frontends");
2211 hlua_listable_proxies(L, PR_CAP_FE);
2212 lua_settable(L, -3);
2213
2214 /* Create backends entry. */
2215 lua_pushstring(L, "backends");
2216 hlua_listable_proxies(L, PR_CAP_BE);
2217 lua_settable(L, -3);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002218
2219 /* list of server. This object is similar to
2220 * CLASS_PROXY_LIST
2221 */
2222 lua_newtable(L);
2223 hlua_class_function(L, "__index", hlua_listable_servers_index);
2224 hlua_class_function(L, "__newindex", hlua_listable_servers_newindex);
2225 hlua_class_function(L, "__pairs", hlua_listable_servers_pairs);
2226 class_server_list_ref = hlua_register_metatable(L, CLASS_SERVER_LIST);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002227}