blob: a357c9f7b0ab27570a7a305705e2dd6cc5eee8be [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua safe functions
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 *
12 * All the functions in this file runs with a Lua stack, and can
Thierry Fournierfb0b5462016-01-21 09:28:58 +010013 * return with a longjmp. All of these function must be launched
14 * in an environment able to catch a longjmp, otherwise a
15 * critical error can be raised.
16 */
Bertrand Jacquinf4c12d42021-01-21 21:14:07 +000017
18#define _GNU_SOURCE
19
Thierry Fournierfb0b5462016-01-21 09:28:58 +010020#include <lauxlib.h>
21#include <lua.h>
22#include <lualib.h>
23
Willy Tarreau63617db2021-10-06 18:23:40 +020024#include <import/ebmbtree.h>
25
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/errors.h>
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +010028#include <haproxy/hlua.h>
Tim Duesterhus6eded622022-05-14 22:17:25 +020029#include <haproxy/hlua_fcn.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020030#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/pattern-t.h>
33#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020037#include <haproxy/stick_table.h>
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010038#include <haproxy/event_hdl.h>
Willy Tarreau27539402021-10-06 09:12:44 +020039#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010041#include <haproxy/tools.h>
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +020042#include <haproxy/mailers.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010043
Thierry Fournier1de16592016-01-27 09:49:07 +010044/* Contains the class reference of the concat object. */
45static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010046static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010047static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010048static int class_listener_ref;
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010049static int class_event_sub_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020050static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020051static int class_stktable_ref;
Thierry Fournier467913c2022-09-30 11:03:38 +020052static int class_proxy_list_ref;
Thierry Fournier1edf36a2022-10-07 13:25:51 +020053static int class_server_list_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010054
Thierry Fournierf61aa632016-02-19 20:56:00 +010055#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010056
Thierry FOURNIERffbad792017-07-12 11:39:04 +020057static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010058
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010059int hlua_checkboolean(lua_State *L, int index)
60{
61 if (!lua_isboolean(L, index))
62 luaL_argerror(L, index, "boolean expected");
63 return lua_toboolean(L, index);
64}
65
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020066/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
67static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
68{
69#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
70 lua_pushinteger(L, val);
71#else
72 if (val > INT_MAX)
73 lua_pushnumber(L, (lua_Number)val);
74 else
75 lua_pushinteger(L, (int)val);
76#endif
77 return 1;
78}
79
80/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
81static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
82#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
83 /* 64 bits case, U64 is supported until LLONG_MAX */
84 if (val > LLONG_MAX)
85 lua_pushnumber(L, (lua_Number)val);
86 else
87 lua_pushinteger(L, val);
88#else
89 /* 32 bits case, U64 is supported until INT_MAX */
90 if (val > INT_MAX)
91 lua_pushnumber(L, (lua_Number)val);
92 else
93 lua_pushinteger(L, (int)val);
94#endif
95 return 1;
96}
97
Joseph Herlantb3d92e32018-11-15 09:35:04 -080098/* This function gets a struct field and converts it in Lua
99 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100100 */
101int hlua_fcn_pushfield(lua_State *L, struct field *field)
102{
103 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800104 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100105 * by some macros. Windows maros are not supported.
106 * If the number cannot be represented as integer, we try to
107 * convert to float.
108 */
109 switch (field_format(field, 0)) {
110
111 case FF_EMPTY:
112 lua_pushnil(L);
113 return 1;
114
115 case FF_S32:
116 /* S32 is always supported. */
117 lua_pushinteger(L, field->u.s32);
118 return 1;
119
120 case FF_U32:
121#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
122 /* 64 bits case, U32 is always supported */
123 lua_pushinteger(L, field->u.u32);
124#else
125 /* 32 bits case, U32 is supported until INT_MAX. */
126 if (field->u.u32 > INT_MAX)
127 lua_pushnumber(L, (lua_Number)field->u.u32);
128 else
129 lua_pushinteger(L, field->u.u32);
130#endif
131 return 1;
132
133 case FF_S64:
134#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
135 /* 64 bits case, S64 is always supported */
136 lua_pushinteger(L, field->u.s64);
137#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500138 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100139 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
140 lua_pushnumber(L, (lua_Number)field->u.s64);
141 else
142 lua_pushinteger(L, (int)field->u.s64);
143#endif
144 return 1;
145
146 case FF_U64:
147#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
148 /* 64 bits case, U64 is supported until LLONG_MAX */
149 if (field->u.u64 > LLONG_MAX)
150 lua_pushnumber(L, (lua_Number)field->u.u64);
151 else
152 lua_pushinteger(L, field->u.u64);
153#else
154 /* 64 bits case, U64 is supported until INT_MAX */
155 if (field->u.u64 > INT_MAX)
156 lua_pushnumber(L, (lua_Number)field->u.u64);
157 else
158 lua_pushinteger(L, (int)field->u.u64);
159#endif
160 return 1;
161
162 case FF_STR:
163 lua_pushstring(L, field->u.str);
164 return 1;
165
166 default:
167 break;
168 }
169
170 /* Default case, never reached. */
171 lua_pushnil(L);
172 return 1;
173}
174
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100175/* Some string are started or terminated by blank chars,
176 * this function removes the spaces, tabs, \r and
177 * \n at the begin and at the end of the string "str", and
178 * push the result in the lua stack.
179 * Returns a pointer to the Lua internal copy of the string.
180 */
181const char *hlua_pushstrippedstring(lua_State *L, const char *str)
182{
183 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100184 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100185
186 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100187
188 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100189
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100190 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100191}
192
Thierry Fournierddd89882016-02-22 19:52:08 +0100193/* The three following functions are useful for adding entries
194 * in a table. These functions takes a string and respectively an
195 * integer, a string or a function and add it to the table in the
196 * top of the stack.
197 *
198 * These functions throws an error if no more stack size is
199 * available.
200 */
201void hlua_class_const_int(lua_State *L, const char *name, int value)
202{
Thierry Fournierddd89882016-02-22 19:52:08 +0100203 lua_pushstring(L, name);
204 lua_pushinteger(L, value);
205 lua_rawset(L, -3);
206}
207void hlua_class_const_str(lua_State *L, const char *name, const char *value)
208{
Thierry Fournierddd89882016-02-22 19:52:08 +0100209 lua_pushstring(L, name);
210 lua_pushstring(L, value);
211 lua_rawset(L, -3);
212}
213void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
214{
Thierry Fournierddd89882016-02-22 19:52:08 +0100215 lua_pushstring(L, name);
216 lua_pushcclosure(L, function, 0);
217 lua_rawset(L, -3);
218}
219
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800220/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100221int hlua_dump_object(struct lua_State *L)
222{
223 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
224 lua_pushfstring(L, "HAProxy class %s", name);
225 return 1;
226}
227
Thierry Fournier45e78d72016-02-19 18:34:46 +0100228/* This function register a table as metatable and. It names
229 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500230 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100231 * "name" is the referenced class name.
232 */
233int hlua_register_metatable(struct lua_State *L, char *name)
234{
235 /* Check the type of the top element. it must be
236 * a table.
237 */
238 if (lua_type(L, -1) != LUA_TTABLE)
239 luaL_error(L, "hlua_register_metatable() requires a type Table "
240 "in the top of the stack");
241
242 /* Add the __tostring function which identify the
243 * created object.
244 */
245 lua_pushstring(L, "__tostring");
246 lua_pushstring(L, name);
247 lua_pushcclosure(L, hlua_dump_object, 1);
248 lua_rawset(L, -3);
249
250 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500251 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100252 * lua_setfield() pop the entry.
253 */
254 lua_pushvalue(L, -1);
255 lua_setfield(L, LUA_REGISTRYINDEX, name);
256
257 /* Creates the reference of the object. The
258 * function luaL_ref pop the top of the stack.
259 */
260 return luaL_ref(L, LUA_REGISTRYINDEX);
261}
262
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100263/* Return an object of the expected type, or throws an error. */
264void *hlua_checkudata(lua_State *L, int ud, int class_ref)
265{
266 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100267 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100268
269 /* Check if the stack entry is an array. */
270 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100271 luaL_argerror(L, ud, NULL);
272
273 /* pop the metatable of the referencecd object. */
274 if (!lua_getmetatable(L, ud))
275 luaL_argerror(L, ud, NULL);
276
277 /* pop the expected metatable. */
278 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
279
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100280 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100281 ret = lua_rawequal(L, -1, -2);
282 lua_pop(L, 2);
283 if (!ret)
284 luaL_argerror(L, ud, NULL);
285
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100286 /* Push on the stack at the entry [0] of the table. */
287 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100288
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100289 /* Check if this entry is userdata. */
290 p = lua_touserdata(L, -1);
291 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100292 luaL_argerror(L, ud, NULL);
293
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100294 /* Remove the entry returned by lua_rawgeti(). */
295 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100296
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100297 /* Return the associated struct. */
298 return p;
299}
300
Thierry Fournierb1f46562016-01-21 09:46:15 +0100301/* This function return the current date at epoch format in milliseconds. */
302int hlua_now(lua_State *L)
303{
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200304 /* WT: the doc says "returns the current time" and later says that it's
305 * monotonic. So the best fit is to use start_date+(now-start_time).
306 */
307 struct timeval tv;
308
Willy Tarreauc05d30e2023-04-28 14:50:29 +0200309 tv = NS_TO_TV(now_ns - start_time_ns);
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200310 tv_add(&tv, &tv, &start_date);
311
Thierry Fournierb1f46562016-01-21 09:46:15 +0100312 lua_newtable(L);
313 lua_pushstring(L, "sec");
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200314 lua_pushinteger(L, tv.tv_sec);
Thierry Fournierb1f46562016-01-21 09:46:15 +0100315 lua_rawset(L, -3);
316 lua_pushstring(L, "usec");
Willy Tarreaud2f61de2023-04-27 18:44:14 +0200317 lua_pushinteger(L, tv.tv_usec);
Thierry Fournierb1f46562016-01-21 09:46:15 +0100318 lua_rawset(L, -3);
319 return 1;
320}
321
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100322/* This functions expects a Lua string as HTTP date, parse it and
323 * returns an integer containing the epoch format of the date, or
324 * nil if the parsing fails.
325 */
326static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
327{
328 const char *str;
329 size_t len;
330 struct tm tm;
331 time_t time;
332
333 str = luaL_checklstring(L, 1, &len);
334
335 if (!fcn(str, len, &tm)) {
336 lua_pushnil(L);
337 return 1;
338 }
339
340 /* This function considers the content of the broken-down time
341 * is exprimed in the UTC timezone. timegm don't care about
342 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
343 * the timezone from the broken-down time, it must be fixed
344 * after the conversion.
345 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200346 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100347 if (time == -1) {
348 lua_pushnil(L);
349 return 1;
350 }
351
352 lua_pushinteger(L, (int)time);
353 return 1;
354}
355static int hlua_http_date(lua_State *L)
356{
357 return hlua_parse_date(L, parse_http_date);
358}
359static int hlua_imf_date(lua_State *L)
360{
361 return hlua_parse_date(L, parse_imf_date);
362}
363static int hlua_rfc850_date(lua_State *L)
364{
365 return hlua_parse_date(L, parse_rfc850_date);
366}
367static int hlua_asctime_date(lua_State *L)
368{
369 return hlua_parse_date(L, parse_asctime_date);
370}
371
Thierry Fourniereea77c02016-03-18 08:47:13 +0100372static int hlua_get_info(lua_State *L)
373{
374 int i;
375
Willy Tarreau0b26b382021-05-08 07:43:53 +0200376 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100377
378 lua_newtable(L);
379 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200380 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100381 hlua_fcn_pushfield(L, &stats[i]);
382 lua_settable(L, -3);
383 }
384 return 1;
385}
386
Thierry Fournier49d48422016-02-19 12:09:29 +0100387static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100388{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200389 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100390}
391
392static int hlua_concat_add(lua_State *L)
393{
Thierry Fournier49d48422016-02-19 12:09:29 +0100394 struct hlua_concat *b;
395 char *buffer;
396 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100397 const char *str;
398 size_t l;
399
400 /* First arg must be a concat object. */
401 b = hlua_check_concat(L, 1);
402
403 /* Second arg must be a string. */
404 str = luaL_checklstring(L, 2, &l);
405
Thierry Fournier49d48422016-02-19 12:09:29 +0100406 /* Get the buffer. */
407 lua_rawgeti(L, 1, 1);
408 buffer = lua_touserdata(L, -1);
409 lua_pop(L, 1);
410
411 /* Update the buffer size if it s required. The old buffer
412 * is crushed by the new in the object array, so it will
413 * be deleted by the GC.
414 * Note that in the first loop, the "new" variable is only
415 * used as a flag.
416 */
417 new = NULL;
418 while (b->size - b->len < l) {
419 b->size += HLUA_CONCAT_BLOCSZ;
420 new = buffer;
421 }
422 if (new) {
423 new = lua_newuserdata(L, b->size);
424 memcpy(new, buffer, b->len);
425 lua_rawseti(L, 1, 1);
426 buffer = new;
427 }
428
429 /* Copy string, and update metadata. */
430 memcpy(buffer + b->len, str, l);
431 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100432 return 0;
433}
434
435static int hlua_concat_dump(lua_State *L)
436{
Thierry Fournier49d48422016-02-19 12:09:29 +0100437 struct hlua_concat *b;
438 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100439
440 /* First arg must be a concat object. */
441 b = hlua_check_concat(L, 1);
442
Thierry Fournier49d48422016-02-19 12:09:29 +0100443 /* Get the buffer. */
444 lua_rawgeti(L, 1, 1);
445 buffer = lua_touserdata(L, -1);
446 lua_pop(L, 1);
447
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500448 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100449 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100450 return 1;
451}
452
453int hlua_concat_new(lua_State *L)
454{
Thierry Fournier49d48422016-02-19 12:09:29 +0100455 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100456
457 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200458 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100459 b->size = HLUA_CONCAT_BLOCSZ;
460 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100461 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100462 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
463 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100464
465 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
466 lua_setmetatable(L, -2);
467
Thierry Fournier1de16592016-01-27 09:49:07 +0100468 return 1;
469}
470
471static int concat_tostring(lua_State *L)
472{
473 const void *ptr = lua_topointer(L, 1);
474 lua_pushfstring(L, "Concat object: %p", ptr);
475 return 1;
476}
477
Thierry Fournier599f2312022-09-30 10:40:39 +0200478static void hlua_concat_init(lua_State *L)
Thierry Fournier1de16592016-01-27 09:49:07 +0100479{
480 /* Creates the buffered concat object. */
481 lua_newtable(L);
482
483 lua_pushstring(L, "__tostring");
484 lua_pushcclosure(L, concat_tostring, 0);
485 lua_settable(L, -3);
486
487 lua_pushstring(L, "__index"); /* Creates the index entry. */
488 lua_newtable(L); /* The "__index" content. */
489
490 lua_pushstring(L, "add");
491 lua_pushcclosure(L, hlua_concat_add, 0);
492 lua_settable(L, -3);
493
494 lua_pushstring(L, "dump");
495 lua_pushcclosure(L, hlua_concat_dump, 0);
496 lua_settable(L, -3);
497
498 lua_settable(L, -3); /* Sets the __index entry. */
499 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Thierry Fournier1de16592016-01-27 09:49:07 +0100500}
501
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200502int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
503{
504 lua_newtable(L);
505
506 /* Pop a class stktbl metatable and affect it to the userdata. */
507 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
508 lua_setmetatable(L, -2);
509
510 lua_pushlightuserdata(L, tbl);
511 lua_rawseti(L, -2, 0);
512 return 1;
513}
514
515static struct stktable *hlua_check_stktable(lua_State *L, int ud)
516{
517 return hlua_checkudata(L, ud, class_stktable_ref);
518}
519
520/* Extract stick table attributes into Lua table */
521int hlua_stktable_info(lua_State *L)
522{
523 struct stktable *tbl;
524 int dt;
525
526 tbl = hlua_check_stktable(L, 1);
527
528 if (!tbl->id) {
529 lua_pushnil(L);
530 return 1;
531 }
532
533 lua_newtable(L);
534
535 lua_pushstring(L, "type");
536 lua_pushstring(L, stktable_types[tbl->type].kw);
537 lua_settable(L, -3);
538
539 lua_pushstring(L, "length");
540 lua_pushinteger(L, tbl->key_size);
541 lua_settable(L, -3);
542
543 lua_pushstring(L, "size");
544 hlua_fcn_pushunsigned(L, tbl->size);
545 lua_settable(L, -3);
546
547 lua_pushstring(L, "used");
548 hlua_fcn_pushunsigned(L, tbl->current);
549 lua_settable(L, -3);
550
551 lua_pushstring(L, "nopurge");
552 lua_pushboolean(L, tbl->nopurge > 0);
553 lua_settable(L, -3);
554
555 lua_pushstring(L, "expire");
556 lua_pushinteger(L, tbl->expire);
557 lua_settable(L, -3);
558
559 /* Save data types periods (if applicable) in 'data' table */
560 lua_pushstring(L, "data");
561 lua_newtable(L);
562
563 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
564 if (tbl->data_ofs[dt] == 0)
565 continue;
566
567 lua_pushstring(L, stktable_data_types[dt].name);
568
569 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
570 lua_pushinteger(L, tbl->data_arg[dt].u);
571 else
572 lua_pushinteger(L, -1);
573
574 lua_settable(L, -3);
575 }
576
577 lua_settable(L, -3);
578
579 return 1;
580}
581
582/* Helper to get extract stick table entry into Lua table */
583static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
584{
585 int dt;
586 void *ptr;
587
588 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
589
590 if (t->data_ofs[dt] == 0)
591 continue;
592
593 lua_pushstring(L, stktable_data_types[dt].name);
594
595 ptr = stktable_data_ptr(t, ts, dt);
596 switch (stktable_data_types[dt].std_type) {
597 case STD_T_SINT:
598 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
599 break;
600 case STD_T_UINT:
601 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
602 break;
603 case STD_T_ULL:
604 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
605 break;
606 case STD_T_FRQP:
607 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
608 t->data_arg[dt].u));
609 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200610 case STD_T_DICT: {
611 struct dict_entry *de;
612 de = stktable_data_cast(ptr, std_t_dict);
613 lua_pushstring(L, de ? (char *)de->value.key : "-");
614 break;
615 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200616 }
617
618 lua_settable(L, -3);
619 }
620}
621
622/* Looks in table <t> for a sticky session matching key <key>
623 * Returns table with session data or nil
624 *
625 * The returned table always contains 'use' and 'expire' (integer) fields.
626 * For frequency/rate counters, each data entry is returned as table with
627 * 'value' and 'period' fields.
628 */
629int hlua_stktable_lookup(lua_State *L)
630{
631 struct stktable *t;
632 struct sample smp;
633 struct stktable_key *skey;
634 struct stksess *ts;
635
636 t = hlua_check_stktable(L, 1);
637 smp.data.type = SMP_T_STR;
638 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600639 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200640
641 skey = smp_to_stkey(&smp, t);
642 if (!skey) {
643 lua_pushnil(L);
644 return 1;
645 }
646
647 ts = stktable_lookup_key(t, skey);
648 if (!ts) {
649 lua_pushnil(L);
650 return 1;
651 }
652
653 lua_newtable(L);
654 lua_pushstring(L, "use");
655 lua_pushinteger(L, ts->ref_cnt - 1);
656 lua_settable(L, -3);
657
658 lua_pushstring(L, "expire");
659 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
660 lua_settable(L, -3);
661
662 hlua_stktable_entry(L, t, ts);
Willy Tarreau76642222022-10-11 12:02:50 +0200663 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200664 ts->ref_cnt--;
Willy Tarreau76642222022-10-11 12:02:50 +0200665 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200666
667 return 1;
668}
669
670struct stk_filter {
671 long long val;
672 int type;
673 int op;
674};
675
676
677/* Helper for returning errors to callers using Lua convention (nil, err) */
678static int hlua_error(lua_State *L, const char *fmt, ...) {
679 char buf[256];
680 int len;
681 va_list args;
682 va_start(args, fmt);
683 len = vsnprintf(buf, sizeof(buf), fmt, args);
684 va_end(args);
685
686 if (len < 0) {
687 ha_alert("hlua_error(): Could not write error message.\n");
688 lua_pushnil(L);
689 return 1;
690 } else if (len >= sizeof(buf))
691 ha_alert("hlua_error(): Error message was truncated.\n");
692
693 lua_pushnil(L);
694 lua_pushstring(L, buf);
695
696 return 2;
697}
698
699/* Dump the contents of stick table <t>*/
700int hlua_stktable_dump(lua_State *L)
701{
702 struct stktable *t;
703 struct ebmb_node *eb;
704 struct ebmb_node *n;
705 struct stksess *ts;
706 int type;
707 int op;
708 int dt;
709 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100710 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200711 int filter_count = 0;
712 int i;
713 int skip_entry;
714 void *ptr;
715
716 t = hlua_check_stktable(L, 1);
717 type = lua_type(L, 2);
718
719 switch (type) {
720 case LUA_TNONE:
721 case LUA_TNIL:
722 break;
723 case LUA_TTABLE:
724 lua_pushnil(L);
725 while (lua_next(L, 2) != 0) {
726 int entry_idx = 0;
727
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100728 if (filter_count >= STKTABLE_FILTER_LEN)
729 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200730
731 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
732 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
733
734 lua_pushnil(L);
735 while (lua_next(L, -2) != 0) {
736 switch (entry_idx) {
737 case 0:
738 if (lua_type(L, -1) != LUA_TSTRING)
739 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
740
741 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
742 if (dt < 0 || t->data_ofs[dt] == 0)
743 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
744 filter[filter_count].type = dt;
745 break;
746 case 1:
747 if (lua_type(L, -1) != LUA_TSTRING)
748 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
749
750 op = get_std_op(lua_tostring(L, -1));
751 if (op < 0)
752 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
753 filter[filter_count].op = op;
754 break;
755 case 2:
756 val = lua_tointeger(L, -1);
757 filter[filter_count].val = val;
758 filter_count++;
759 break;
760 default:
761 break;
762 }
763 entry_idx++;
764 lua_pop(L, 1);
765 }
766 lua_pop(L, 1);
767 }
768 break;
769 default:
770 return hlua_error(L, "filter table expected");
771 }
772
773 lua_newtable(L);
774
Willy Tarreau76642222022-10-11 12:02:50 +0200775 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200776 eb = ebmb_first(&t->keys);
777 for (n = eb; n; n = ebmb_next(n)) {
778 ts = ebmb_entry(n, struct stksess, key);
779 if (!ts) {
Willy Tarreau76642222022-10-11 12:02:50 +0200780 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200781 return 1;
782 }
783 ts->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +0200784 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200785
786 /* multi condition/value filter */
787 skip_entry = 0;
788 for (i = 0; i < filter_count; i++) {
789 if (t->data_ofs[filter[i].type] == 0)
790 continue;
791
792 ptr = stktable_data_ptr(t, ts, filter[i].type);
793
794 switch (stktable_data_types[filter[i].type].std_type) {
795 case STD_T_SINT:
796 val = stktable_data_cast(ptr, std_t_sint);
797 break;
798 case STD_T_UINT:
799 val = stktable_data_cast(ptr, std_t_uint);
800 break;
801 case STD_T_ULL:
802 val = stktable_data_cast(ptr, std_t_ull);
803 break;
804 case STD_T_FRQP:
805 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
806 t->data_arg[filter[i].type].u);
807 break;
808 default:
809 continue;
810 break;
811 }
812
813 op = filter[i].op;
814
815 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
816 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
817 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
818 skip_entry = 1;
819 break;
820 }
821 }
822
823 if (skip_entry) {
Willy Tarreau76642222022-10-11 12:02:50 +0200824 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200825 ts->ref_cnt--;
826 continue;
827 }
828
829 if (t->type == SMP_T_IPV4) {
830 char addr[INET_ADDRSTRLEN];
831 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
832 lua_pushstring(L, addr);
833 } else if (t->type == SMP_T_IPV6) {
834 char addr[INET6_ADDRSTRLEN];
835 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
836 lua_pushstring(L, addr);
837 } else if (t->type == SMP_T_SINT) {
838 lua_pushinteger(L, *ts->key.key);
839 } else if (t->type == SMP_T_STR) {
840 lua_pushstring(L, (const char *)ts->key.key);
841 } else {
842 return hlua_error(L, "Unsupported stick table key type");
843 }
844
845 lua_newtable(L);
846 hlua_stktable_entry(L, t, ts);
847 lua_settable(L, -3);
Willy Tarreau76642222022-10-11 12:02:50 +0200848 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200849 ts->ref_cnt--;
850 }
Willy Tarreau76642222022-10-11 12:02:50 +0200851 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200852
853 return 1;
854}
855
Thierry Fournierff480422016-02-25 08:36:46 +0100856int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
857{
858 lua_newtable(L);
859
860 /* Pop a class sesison metatable and affect it to the userdata. */
861 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
862 lua_setmetatable(L, -2);
863
864 lua_pushlightuserdata(L, lst);
865 lua_rawseti(L, -2, 0);
866 return 1;
867}
868
869static struct listener *hlua_check_listener(lua_State *L, int ud)
870{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200871 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100872}
873
874int hlua_listener_get_stats(lua_State *L)
875{
876 struct listener *li;
877 int i;
878
879 li = hlua_check_listener(L, 1);
880
Willy Tarreauc95bad52016-12-22 00:13:31 +0100881 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100882 lua_pushnil(L);
883 return 1;
884 }
885
William Dauchy655e14e2021-02-14 23:22:54 +0100886 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
887 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +0100888
889 lua_newtable(L);
890 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200891 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100892 hlua_fcn_pushfield(L, &stats[i]);
893 lua_settable(L, -3);
894 }
895 return 1;
896
897}
898
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200899int hlua_server_gc(lua_State *L)
900{
901 struct server *srv = hlua_checkudata(L, 1, class_server_ref);
902
903 srv_drop(srv); /* srv_drop allows NULL srv */
904 return 0;
905}
906
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100907static struct server *hlua_check_server(lua_State *L, int ud)
908{
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200909 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200910 if (srv->flags & SRV_F_DELETED) {
911 return NULL;
912 }
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200913 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100914}
915
916int hlua_server_get_stats(lua_State *L)
917{
918 struct server *srv;
919 int i;
920
921 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200922 if (srv == NULL) {
923 lua_pushnil(L);
924 return 1;
925 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100926
927 if (!srv->proxy) {
928 lua_pushnil(L);
929 return 1;
930 }
931
William Dauchyd3a9a492021-01-25 17:29:03 +0100932 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
933 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100934
935 lua_newtable(L);
936 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200937 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100938 hlua_fcn_pushfield(L, &stats[i]);
939 lua_settable(L, -3);
940 }
941 return 1;
942
943}
944
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +0200945int hlua_server_get_proxy(lua_State *L)
946{
947 struct server *srv;
948
949 srv = hlua_check_server(L, 1);
950 if (srv == NULL) {
951 lua_pushnil(L);
952 return 1;
953 }
954
955 if (!srv->proxy) {
956 lua_pushnil(L);
957 return 1;
958 }
959
960 hlua_fcn_new_proxy(L, srv->proxy);
961 return 1;
962}
963
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100964int hlua_server_get_addr(lua_State *L)
965{
966 struct server *srv;
967 char addr[INET6_ADDRSTRLEN];
968 luaL_Buffer b;
969
970 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +0200971 if (srv == NULL) {
972 lua_pushnil(L);
973 return 1;
974 }
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100975
976 luaL_buffinit(L, &b);
977
978 switch (srv->addr.ss_family) {
979 case AF_INET:
980 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
981 addr, INET_ADDRSTRLEN);
982 luaL_addstring(&b, addr);
983 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400984 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100985 luaL_addstring(&b, addr);
986 break;
987 case AF_INET6:
988 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400989 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100990 luaL_addstring(&b, addr);
991 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400992 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100993 luaL_addstring(&b, addr);
994 break;
995 case AF_UNIX:
996 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
997 break;
998 default:
999 luaL_addstring(&b, "<unknown>");
1000 break;
1001 }
1002
1003 luaL_pushresult(&b);
1004 return 1;
1005}
1006
Thierry Fournierb0467732022-10-07 12:07:24 +02001007int hlua_server_get_puid(lua_State *L)
1008{
1009 struct server *srv;
1010 char buffer[12];
1011
1012 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001013 if (srv == NULL) {
1014 lua_pushnil(L);
1015 return 1;
1016 }
Thierry Fournierb0467732022-10-07 12:07:24 +02001017
1018 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
1019 lua_pushstring(L, buffer);
1020 return 1;
1021}
1022
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001023int hlua_server_get_rid(lua_State *L)
1024{
1025 struct server *srv;
1026 char buffer[12];
1027
1028 srv = hlua_check_server(L, 1);
1029 if (srv == NULL) {
1030 lua_pushnil(L);
1031 return 1;
1032 }
1033
1034 snprintf(buffer, sizeof(buffer), "%d", srv->rid);
1035 lua_pushstring(L, buffer);
1036 return 1;
1037}
1038
Thierry Fournierb0467732022-10-07 12:07:24 +02001039int hlua_server_get_name(lua_State *L)
1040{
1041 struct server *srv;
1042
1043 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001044 if (srv == NULL) {
1045 lua_pushnil(L);
1046 return 1;
1047 }
1048
Thierry Fournierb0467732022-10-07 12:07:24 +02001049 lua_pushstring(L, srv->id);
1050 return 1;
1051}
1052
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001053/* __index metamethod for server class
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001054 * support for additional keys that are missing from the main table
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001055 * stack:1 = table (server class), stack:2 = requested key
1056 * Returns 1 if key is supported
1057 * else returns 0 to make lua return NIL value to the caller
1058 */
1059static int hlua_server_index(struct lua_State *L)
1060{
1061 const char *key = lua_tostring(L, 2);
1062
1063 if (!strcmp(key, "name")) {
1064 if (ONLY_ONCE())
1065 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, ", "));
1066 lua_pushvalue(L, 1);
1067 hlua_server_get_name(L);
1068 return 1;
1069 }
1070 if (!strcmp(key, "puid")) {
1071 if (ONLY_ONCE())
1072 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, ", "));
1073 lua_pushvalue(L, 1);
1074 hlua_server_get_puid(L);
1075 return 1;
1076 }
1077 /* unknown attribute */
1078 return 0;
1079}
1080
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001081int hlua_server_is_draining(lua_State *L)
1082{
1083 struct server *srv;
1084
1085 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001086 if (srv == NULL) {
1087 lua_pushnil(L);
1088 return 1;
1089 }
1090
Aurelien DARRAGON862a0fe2023-03-29 10:46:36 +02001091 lua_pushboolean(L, server_is_draining(srv));
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001092 return 1;
1093}
1094
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001095int hlua_server_is_backup(lua_State *L)
1096{
1097 struct server *srv;
1098
1099 srv = hlua_check_server(L, 1);
1100 if (srv == NULL) {
1101 lua_pushnil(L);
1102 return 1;
1103 }
1104
1105 lua_pushboolean(L, (srv->flags & SRV_F_BACKUP));
1106 return 1;
1107}
1108
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001109int hlua_server_is_dynamic(lua_State *L)
1110{
1111 struct server *srv;
1112
1113 srv = hlua_check_server(L, 1);
1114 if (srv == NULL) {
1115 lua_pushnil(L);
1116 return 1;
1117 }
1118
1119 lua_pushboolean(L, (srv->flags & SRV_F_DYNAMIC));
1120 return 1;
1121}
1122
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001123int hlua_server_get_cur_sess(lua_State *L)
1124{
1125 struct server *srv;
1126
1127 srv = hlua_check_server(L, 1);
1128 if (srv == NULL) {
1129 lua_pushnil(L);
1130 return 1;
1131 }
1132
1133 lua_pushinteger(L, srv->cur_sess);
1134 return 1;
1135}
1136
1137int hlua_server_get_pend_conn(lua_State *L)
1138{
1139 struct server *srv;
1140
1141 srv = hlua_check_server(L, 1);
1142 if (srv == NULL) {
1143 lua_pushnil(L);
1144 return 1;
1145 }
1146
1147 lua_pushinteger(L, srv->queue.length);
1148 return 1;
1149}
1150
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001151int hlua_server_set_maxconn(lua_State *L)
1152{
1153 struct server *srv;
1154 const char *maxconn;
1155 const char *err;
1156
1157 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001158 if (srv == NULL) {
1159 lua_pushnil(L);
1160 return 1;
1161 }
1162
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001163 maxconn = luaL_checkstring(L, 2);
1164
1165 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1166 err = server_parse_maxconn_change_request(srv, maxconn);
1167 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1168 if (!err)
1169 lua_pushnil(L);
1170 else
1171 hlua_pushstrippedstring(L, err);
1172 return 1;
1173}
1174
1175int hlua_server_get_maxconn(lua_State *L)
1176{
1177 struct server *srv;
1178
1179 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001180 if (srv == NULL) {
1181 lua_pushnil(L);
1182 return 1;
1183 }
1184
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001185 lua_pushinteger(L, srv->maxconn);
1186 return 1;
1187}
1188
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001189int hlua_server_set_weight(lua_State *L)
1190{
1191 struct server *srv;
1192 const char *weight;
1193 const char *err;
1194
1195 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001196 if (srv == NULL) {
1197 lua_pushnil(L);
1198 return 1;
1199 }
1200
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001201 weight = luaL_checkstring(L, 2);
1202
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001203 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001204 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001205 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001206 if (!err)
1207 lua_pushnil(L);
1208 else
1209 hlua_pushstrippedstring(L, err);
1210 return 1;
1211}
1212
1213int hlua_server_get_weight(lua_State *L)
1214{
1215 struct server *srv;
1216
1217 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001218 if (srv == NULL) {
1219 lua_pushnil(L);
1220 return 1;
1221 }
1222
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001223 lua_pushinteger(L, srv->uweight);
1224 return 1;
1225}
1226
1227int hlua_server_set_addr(lua_State *L)
1228{
1229 struct server *srv;
1230 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001231 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001232 const char *err;
1233
1234 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001235 if (srv == NULL) {
1236 lua_pushnil(L);
1237 return 1;
1238 }
1239
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001240 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001241 if (lua_gettop(L) >= 3)
1242 port = luaL_checkstring(L, 3);
1243 else
1244 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001245
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001246 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001247 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001248 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001249 if (!err)
1250 lua_pushnil(L);
1251 else
1252 hlua_pushstrippedstring(L, err);
1253 return 1;
1254}
1255
1256int hlua_server_shut_sess(lua_State *L)
1257{
1258 struct server *srv;
1259
1260 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001261 if (srv == NULL) {
1262 return 0;
1263 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001264 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001265 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001266 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001267 return 0;
1268}
1269
1270int hlua_server_set_drain(lua_State *L)
1271{
1272 struct server *srv;
1273
1274 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001275 if (srv == NULL) {
1276 return 0;
1277 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001278 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001279 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001280 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001281 return 0;
1282}
1283
1284int hlua_server_set_maint(lua_State *L)
1285{
1286 struct server *srv;
1287
1288 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001289 if (srv == NULL) {
1290 return 0;
1291 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001292 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001293 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001294 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001295 return 0;
1296}
1297
1298int hlua_server_set_ready(lua_State *L)
1299{
1300 struct server *srv;
1301
1302 srv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001303 if (srv == NULL) {
1304 return 0;
1305 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001306 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001307 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001308 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001309 return 0;
1310}
1311
1312int hlua_server_check_enable(lua_State *L)
1313{
1314 struct server *sv;
1315
1316 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001317 if (sv == NULL) {
1318 return 0;
1319 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001320 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001321 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001322 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001323 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001324 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001325 return 0;
1326}
1327
1328int hlua_server_check_disable(lua_State *L)
1329{
1330 struct server *sv;
1331
1332 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001333 if (sv == NULL) {
1334 return 0;
1335 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001336 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001337 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001338 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001339 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001340 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001341 return 0;
1342}
1343
1344int hlua_server_check_force_up(lua_State *L)
1345{
1346 struct server *sv;
1347
1348 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001349 if (sv == NULL) {
1350 return 0;
1351 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001352 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001353 if (!(sv->track)) {
1354 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001355 srv_set_running(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001356 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001357 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001358 return 0;
1359}
1360
1361int hlua_server_check_force_nolb(lua_State *L)
1362{
1363 struct server *sv;
1364
1365 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001366 if (sv == NULL) {
1367 return 0;
1368 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001369 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001370 if (!(sv->track)) {
1371 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001372 srv_set_stopping(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001373 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001374 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001375 return 0;
1376}
1377
1378int hlua_server_check_force_down(lua_State *L)
1379{
1380 struct server *sv;
1381
1382 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001383 if (sv == NULL) {
1384 return 0;
1385 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001386 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001387 if (!(sv->track)) {
1388 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001389 srv_set_stopped(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001390 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001391 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001392 return 0;
1393}
1394
1395int hlua_server_agent_enable(lua_State *L)
1396{
1397 struct server *sv;
1398
1399 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001400 if (sv == NULL) {
1401 return 0;
1402 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001403 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001404 if (sv->agent.state & CHK_ST_CONFIGURED) {
1405 sv->agent.state |= CHK_ST_ENABLED;
1406 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001407 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001408 return 0;
1409}
1410
1411int hlua_server_agent_disable(lua_State *L)
1412{
1413 struct server *sv;
1414
1415 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001416 if (sv == NULL) {
1417 return 0;
1418 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001419 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001420 if (sv->agent.state & CHK_ST_CONFIGURED) {
1421 sv->agent.state &= ~CHK_ST_ENABLED;
1422 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001423 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001424 return 0;
1425}
1426
1427int hlua_server_agent_force_up(lua_State *L)
1428{
1429 struct server *sv;
1430
1431 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001432 if (sv == NULL) {
1433 return 0;
1434 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001435 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001436 if (sv->agent.state & CHK_ST_ENABLED) {
1437 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001438 srv_set_running(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001439 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001440 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001441 return 0;
1442}
1443
1444int hlua_server_agent_force_down(lua_State *L)
1445{
1446 struct server *sv;
1447
1448 sv = hlua_check_server(L, 1);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001449 if (sv == NULL) {
1450 return 0;
1451 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001452 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001453 if (sv->agent.state & CHK_ST_ENABLED) {
1454 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001455 srv_set_stopped(sv, SRV_OP_STCHGC_LUA);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001456 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001457 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001458 return 0;
1459}
1460
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001461/* returns the tracked server, if any */
1462int hlua_server_tracking(lua_State *L)
1463{
1464 struct server *sv;
1465 struct server *tracked;
1466
1467 sv = hlua_check_server(L, 1);
1468 if (sv == NULL) {
1469 return 0;
1470 }
1471
1472 tracked = sv->track;
1473 if (tracked == NULL)
1474 lua_pushnil(L);
1475 else
1476 hlua_fcn_new_server(L, tracked);
1477
1478 return 1;
1479}
1480
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001481/* returns an array of servers tracking the current server */
1482int hlua_server_get_trackers(lua_State *L)
1483{
1484 struct server *sv;
1485 struct server *cur_tracker;
1486 int index;
1487
1488 sv = hlua_check_server(L, 1);
1489 if (sv == NULL) {
1490 return 0;
1491 }
1492
1493 lua_newtable(L);
1494 cur_tracker = sv->trackers;
1495 for (index = 1; cur_tracker; cur_tracker = cur_tracker->tracknext, index++) {
1496 if (!lua_checkstack(L, 5))
1497 luaL_error(L, "Lua out of memory error.");
1498 hlua_fcn_new_server(L, cur_tracker);
1499 /* array index starts at 1 in Lua */
1500 lua_rawseti(L, -2, index);
1501 }
1502 return 1;
1503}
1504
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001505/* hlua_event_sub wrapper for per-server subscription:
1506 *
1507 * hlua_event_sub() is called with sv->e_subs subscription list and
1508 * lua arguments are passed as-is (skipping the first argument which
1509 * is the server ctx)
1510 */
1511int hlua_server_event_sub(lua_State *L)
1512{
1513 struct server *sv;
1514
1515 sv = hlua_check_server(L, 1);
1516 if (sv == NULL) {
1517 return 0;
1518 }
1519 /* remove first argument from the stack (server) */
1520 lua_remove(L, 1);
1521
1522 /* try to subscribe within server's subscription list */
1523 return hlua_event_sub(L, &sv->e_subs);
1524}
1525
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001526int hlua_fcn_new_server(lua_State *L, struct server *srv)
1527{
1528 lua_newtable(L);
1529
1530 /* Pop a class server metatable and affect it to the userdata. */
1531 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
1532 lua_setmetatable(L, -2);
1533
1534 lua_pushlightuserdata(L, srv);
1535 lua_rawseti(L, -2, 0);
1536
1537 /* userdata is affected: increment server refcount */
1538 srv_take(srv);
1539
1540 /* set public methods */
1541 hlua_class_function(L, "get_name", hlua_server_get_name);
1542 hlua_class_function(L, "get_puid", hlua_server_get_puid);
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001543 hlua_class_function(L, "get_rid", hlua_server_get_rid);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001544 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001545 hlua_class_function(L, "is_backup", hlua_server_is_backup);
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001546 hlua_class_function(L, "is_dynamic", hlua_server_is_dynamic);
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001547 hlua_class_function(L, "get_cur_sess", hlua_server_get_cur_sess);
1548 hlua_class_function(L, "get_pend_conn", hlua_server_get_pend_conn);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001549 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1550 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
1551 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1552 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1553 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1554 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1555 hlua_class_function(L, "get_stats", hlua_server_get_stats);
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001556 hlua_class_function(L, "get_proxy", hlua_server_get_proxy);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001557 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1558 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1559 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1560 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1561 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1562 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1563 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1564 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1565 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1566 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1567 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1568 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1569 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001570 hlua_class_function(L, "tracking", hlua_server_tracking);
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001571 hlua_class_function(L, "get_trackers", hlua_server_get_trackers);
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001572 hlua_class_function(L, "event_sub", hlua_server_event_sub);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001573
1574 return 1;
1575}
1576
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001577static struct hlua_server_list *hlua_check_server_list(lua_State *L, int ud)
1578{
1579 return hlua_checkudata(L, ud, class_server_list_ref);
1580}
1581
1582/* does nothing and returns 0, only prevents insertions in the
1583 * table which represents the list of servers
1584 */
1585int hlua_listable_servers_newindex(lua_State *L) {
1586 return 0;
1587}
1588
1589/* first arg is the table (struct hlua_server_list * in metadata)
1590 * second arg is the required index
1591 */
1592int hlua_listable_servers_index(lua_State *L)
Thierry Fournierf61aa632016-02-19 20:56:00 +01001593{
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001594 struct hlua_server_list *hlua_srv;
1595 const char *name;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001596 struct server *srv;
Thierry Fournier1edf36a2022-10-07 13:25:51 +02001597
1598 hlua_srv = hlua_check_server_list(L, 1);
1599 name = luaL_checkstring(L, 2);
1600
1601 /* Perform a server lookup in px list */
1602 srv = server_find_by_name(hlua_srv->px, name);
1603 if (srv == NULL) {
1604 lua_pushnil(L);
1605 return 1;
1606 }
1607
1608 hlua_fcn_new_server(L, srv);
1609 return 1;
1610}
1611
1612/* iterator must return key as string and value as server
1613 * object, if we reach end of list, it returns nil.
1614 * The context knows the last returned server. if the
1615 * context contains srv == NULL, we start enumeration.
1616 * Then, use 'srv->next' ptr to iterate through the list
1617 */
1618int hlua_listable_servers_pairs_iterator(lua_State *L)
1619{
1620 int context_index;
1621 struct hlua_server_list_iterator_context *ctx;
1622
1623 context_index = lua_upvalueindex(1);
1624 ctx = lua_touserdata(L, context_index);
1625
1626 if (ctx->cur == NULL) {
1627 /* First iteration, initialize list on the first server */
1628 ctx->cur = ctx->px->srv;
1629 } else {
1630
1631 /* Next server (next ptr is always valid, even if current
1632 * server has the SRV_F_DELETED flag set)
1633 */
1634 ctx->cur = ctx->cur->next;
1635 }
1636
1637 /* next server is null, end of iteration */
1638 if (ctx->cur == NULL) {
1639 lua_pushnil(L);
1640 return 1;
1641 }
1642
1643 lua_pushstring(L, ctx->cur->id);
1644 hlua_fcn_new_server(L, ctx->cur);
1645 return 2;
1646}
1647
1648/* init the iterator context, return iterator function
1649 * with context as closure. The only argument is a
1650 * server list object.
1651 */
1652int hlua_listable_servers_pairs(lua_State *L)
1653{
1654 struct hlua_server_list_iterator_context *ctx;
1655 struct hlua_server_list *hlua_srv_list;
1656
1657 hlua_srv_list = hlua_check_server_list(L, 1);
1658
1659 ctx = lua_newuserdata(L, sizeof(*ctx));
1660 ctx->px = hlua_srv_list->px;
1661 ctx->cur = NULL;
1662
1663 lua_pushcclosure(L, hlua_listable_servers_pairs_iterator, 1);
1664 return 1;
1665}
1666
1667void hlua_listable_servers(lua_State *L, struct proxy *px)
1668{
1669 struct hlua_server_list *list;
1670
1671 lua_newtable(L);
1672 list = lua_newuserdata(L, sizeof(*list));
1673 list->px = px;
1674 lua_rawseti(L, -2, 0);
1675 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_list_ref);
1676 lua_setmetatable(L, -2);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001677}
1678
1679static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1680{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001681 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001682}
1683
Thierry Fournierb0467732022-10-07 12:07:24 +02001684int hlua_proxy_get_name(lua_State *L)
1685{
1686 struct proxy *px;
1687
1688 px = hlua_check_proxy(L, 1);
1689 lua_pushstring(L, px->id);
1690 return 1;
1691}
1692
1693int hlua_proxy_get_uuid(lua_State *L)
1694{
1695 struct proxy *px;
1696 char buffer[17];
1697
1698 px = hlua_check_proxy(L, 1);
1699 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1700 lua_pushstring(L, buffer);
1701 return 1;
1702}
1703
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001704/* __index metamethod for proxy class
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001705 * support for additional keys that are missing from the main table
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001706 * stack:1 = table (proxy class), stack:2 = requested key
1707 * Returns 1 if key is supported
1708 * else returns 0 to make lua return NIL value to the caller
1709 */
1710static int hlua_proxy_index(struct lua_State *L)
1711{
1712 const char *key = lua_tostring(L, 2);
1713
1714 if (!strcmp(key, "name")) {
1715 if (ONLY_ONCE())
1716 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, ", "));
1717 lua_pushvalue(L, 1);
1718 hlua_proxy_get_name(L);
1719 return 1;
1720 }
1721 if (!strcmp(key, "uuid")) {
1722 if (ONLY_ONCE())
1723 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, ", "));
1724 lua_pushvalue(L, 1);
1725 hlua_proxy_get_uuid(L);
1726 return 1;
1727 }
1728 /* unknown attribute */
1729 return 0;
1730}
1731
Thierry Fournierf61aa632016-02-19 20:56:00 +01001732int hlua_proxy_pause(lua_State *L)
1733{
1734 struct proxy *px;
1735
1736 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001737 /* safe to call without PROXY_LOCK - pause_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001738 pause_proxy(px);
1739 return 0;
1740}
1741
1742int hlua_proxy_resume(lua_State *L)
1743{
1744 struct proxy *px;
1745
1746 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001747 /* safe to call without PROXY_LOCK - resume_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001748 resume_proxy(px);
1749 return 0;
1750}
1751
1752int hlua_proxy_stop(lua_State *L)
1753{
1754 struct proxy *px;
1755
1756 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001757 /* safe to call without PROXY_LOCK - stop_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001758 stop_proxy(px);
1759 return 0;
1760}
1761
1762int hlua_proxy_get_cap(lua_State *L)
1763{
1764 struct proxy *px;
1765 const char *str;
1766
1767 px = hlua_check_proxy(L, 1);
1768 str = proxy_cap_str(px->cap);
1769 lua_pushstring(L, str);
1770 return 1;
1771}
1772
1773int hlua_proxy_get_stats(lua_State *L)
1774{
1775 struct proxy *px;
1776 int i;
1777
1778 px = hlua_check_proxy(L, 1);
1779 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001780 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001781 else
William Dauchy0ef54392021-01-17 18:27:45 +01001782 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001783 lua_newtable(L);
1784 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001785 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001786 hlua_fcn_pushfield(L, &stats[i]);
1787 lua_settable(L, -3);
1788 }
1789 return 1;
1790}
1791
1792int hlua_proxy_get_mode(lua_State *L)
1793{
1794 struct proxy *px;
1795 const char *str;
1796
1797 px = hlua_check_proxy(L, 1);
1798 str = proxy_mode_str(px->mode);
1799 lua_pushstring(L, str);
1800 return 1;
1801}
1802
1803int hlua_proxy_shut_bcksess(lua_State *L)
1804{
1805 struct proxy *px;
1806
1807 px = hlua_check_proxy(L, 1);
1808 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1809 return 0;
1810}
1811
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001812int hlua_proxy_get_srv_act(lua_State *L)
1813{
1814 struct proxy *px;
1815
1816 px = hlua_check_proxy(L, 1);
1817 lua_pushinteger(L, px->srv_act);
1818 return 1;
1819}
1820
1821int hlua_proxy_get_srv_bck(lua_State *L)
1822{
1823 struct proxy *px;
1824
1825 px = hlua_check_proxy(L, 1);
1826 lua_pushinteger(L, px->srv_bck);
1827 return 1;
1828}
1829
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001830/* Get mailers config info, used to implement email alert sending
1831 * according to mailers config from lua.
1832 */
1833int hlua_proxy_get_mailers(lua_State *L)
1834{
1835 struct proxy *px;
1836 int it;
1837 struct mailer *mailer;
1838
1839 px = hlua_check_proxy(L, 1);
1840
1841 if (!px->email_alert.mailers.m)
1842 return 0; /* email-alert mailers not found on proxy */
1843
1844 lua_newtable(L);
1845
1846 /* option log-health-checks */
1847 lua_pushstring(L, "track_server_health");
1848 lua_pushboolean(L, (px->options2 & PR_O2_LOGHCHKS));
1849 lua_settable(L, -3);
1850
1851 /* email-alert level */
1852 lua_pushstring(L, "log_level");
1853 lua_pushinteger(L, px->email_alert.level);
1854 lua_settable(L, -3);
1855
1856 /* email-alert mailers */
1857 lua_pushstring(L, "mailservers");
1858 lua_newtable(L);
1859 for (it = 0, mailer = px->email_alert.mailers.m->mailer_list;
1860 it < px->email_alert.mailers.m->count; it++, mailer = mailer->next) {
1861 char *srv_address;
1862
1863 lua_pushstring(L, mailer->id);
1864
1865 /* For now, we depend on mailer->addr to restore mailer's address which
1866 * was converted using str2sa_range() on startup.
1867 *
1868 * FIXME?:
1869 * It could be a good idea to pass the raw address (unparsed) to allow fqdn
1870 * to be resolved at runtime, unless we consider this as a pure legacy mode
1871 * and mailers config support is going to be removed in the future?
1872 */
1873 srv_address = sa2str(&mailer->addr, get_host_port(&mailer->addr), 0);
1874 if (srv_address) {
1875 lua_pushstring(L, srv_address);
1876 ha_free(&srv_address);
1877 lua_settable(L, -3);
1878 }
1879 }
1880 lua_settable(L, -3);
1881
1882 /* email-alert myhostname */
1883 lua_pushstring(L, "smtp_hostname");
1884 lua_pushstring(L, px->email_alert.myhostname);
1885 lua_settable(L, -3);
1886
1887 /* email-alert from */
1888 lua_pushstring(L, "smtp_from");
1889 lua_pushstring(L, px->email_alert.from);
1890 lua_settable(L, -3);
1891
1892 /* email-alert to */
1893 lua_pushstring(L, "smtp_to");
1894 lua_pushstring(L, px->email_alert.to);
1895 lua_settable(L, -3);
1896
1897 return 1;
1898}
1899
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001900int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1901{
1902 struct listener *lst;
1903 int lid;
1904 char buffer[17];
1905
1906 lua_newtable(L);
1907
1908 /* Pop a class proxy metatable and affect it to the userdata. */
1909 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1910 lua_setmetatable(L, -2);
1911
1912 lua_pushlightuserdata(L, px);
1913 lua_rawseti(L, -2, 0);
1914
1915 /* set public methods */
1916 hlua_class_function(L, "get_name", hlua_proxy_get_name);
1917 hlua_class_function(L, "get_uuid", hlua_proxy_get_uuid);
1918 hlua_class_function(L, "pause", hlua_proxy_pause);
1919 hlua_class_function(L, "resume", hlua_proxy_resume);
1920 hlua_class_function(L, "stop", hlua_proxy_stop);
1921 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1922 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1923 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001924 hlua_class_function(L, "get_srv_act", hlua_proxy_get_srv_act);
1925 hlua_class_function(L, "get_srv_bck", hlua_proxy_get_srv_bck);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001926 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001927 hlua_class_function(L, "get_mailers", hlua_proxy_get_mailers);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001928
1929 /* Browse and register servers. */
1930 lua_pushstring(L, "servers");
1931 hlua_listable_servers(L, px);
1932 lua_settable(L, -3);
1933
1934 /* Browse and register listeners. */
1935 lua_pushstring(L, "listeners");
1936 lua_newtable(L);
1937 lid = 1;
1938 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1939 if (lst->name)
1940 lua_pushstring(L, lst->name);
1941 else {
1942 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
1943 lid++;
1944 lua_pushstring(L, buffer);
1945 }
1946 hlua_fcn_new_listener(L, lst);
1947 lua_settable(L, -3);
1948 }
1949 lua_settable(L, -3);
1950
1951 if (px->table && px->table->id) {
1952 lua_pushstring(L, "stktable");
1953 hlua_fcn_new_stktable(L, px->table);
1954 lua_settable(L, -3);
1955 }
1956
1957 return 1;
1958}
1959
Thierry Fournier467913c2022-09-30 11:03:38 +02001960static struct hlua_proxy_list *hlua_check_proxy_list(lua_State *L, int ud)
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001961{
Thierry Fournier467913c2022-09-30 11:03:38 +02001962 return hlua_checkudata(L, ud, class_proxy_list_ref);
1963}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001964
Thierry Fournier467913c2022-09-30 11:03:38 +02001965/* does nothing and returns 0, only prevents insertions in the
1966 * table which represent list of proxies
1967 */
1968int hlua_listable_proxies_newindex(lua_State *L) {
1969 return 0;
1970}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001971
Thierry Fournier467913c2022-09-30 11:03:38 +02001972/* first arg is the table (struct hlua_proxy_list * in metadata)
1973 * second arg is the required index
1974 */
1975int hlua_listable_proxies_index(lua_State *L)
1976{
1977 struct hlua_proxy_list *hlua_px;
1978 const char *name;
1979 struct proxy *px;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001980
Thierry Fournier467913c2022-09-30 11:03:38 +02001981 hlua_px = hlua_check_proxy_list(L, 1);
1982 name = luaL_checkstring(L, 2);
1983
1984 px = NULL;
1985 if (hlua_px->capabilities & PR_CAP_FE) {
1986 px = proxy_find_by_name(name, PR_CAP_FE, 0);
1987 }
1988 if (!px && hlua_px->capabilities & PR_CAP_BE) {
1989 px = proxy_find_by_name(name, PR_CAP_BE, 0);
1990 }
1991 if (px == NULL) {
1992 lua_pushnil(L);
1993 return 1;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001994 }
1995
Thierry Fournier467913c2022-09-30 11:03:38 +02001996 hlua_fcn_new_proxy(L, px);
1997 return 1;
1998}
Thierry Fournierf61aa632016-02-19 20:56:00 +01001999
Thierry Fournier467913c2022-09-30 11:03:38 +02002000static inline int hlua_listable_proxies_match(struct proxy *px, char cap) {
2001 return ((px->cap & cap) && !(px->cap & (PR_CAP_DEF | PR_CAP_INT)));
2002}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002003
Thierry Fournier467913c2022-09-30 11:03:38 +02002004/* iterator must return key as string and value as proxy
2005 * object, if we reach end of list, it returns nil
2006 */
2007int hlua_listable_proxies_pairs_iterator(lua_State *L)
2008{
2009 int context_index;
2010 struct hlua_proxy_list_iterator_context *ctx;
2011
2012 context_index = lua_upvalueindex(1);
2013 ctx = lua_touserdata(L, context_index);
2014
2015 if (ctx->next == NULL) {
2016 lua_pushnil(L);
2017 return 1;
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002018 }
2019
Thierry Fournier467913c2022-09-30 11:03:38 +02002020 lua_pushstring(L, ctx->next->id);
2021 hlua_fcn_new_proxy(L, ctx->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002022
Thierry Fournier467913c2022-09-30 11:03:38 +02002023 for (ctx->next = ctx->next->next;
2024 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
2025 ctx->next = ctx->next->next);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002026
Thierry Fournier467913c2022-09-30 11:03:38 +02002027 return 2;
2028}
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002029
Thierry Fournier467913c2022-09-30 11:03:38 +02002030/* init the iterator context, return iterator function
2031 * with context as closure. The only argument is a
2032 * proxy object.
2033 */
2034int hlua_listable_proxies_pairs(lua_State *L)
2035{
2036 struct hlua_proxy_list_iterator_context *ctx;
2037 struct hlua_proxy_list *hlua_px;
2038
2039 hlua_px = hlua_check_proxy_list(L, 1);
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02002040
Thierry Fournier467913c2022-09-30 11:03:38 +02002041 ctx = lua_newuserdata(L, sizeof(*ctx));
2042
2043 ctx->capabilities = hlua_px->capabilities;
2044 for (ctx->next = proxies_list;
2045 ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities);
2046 ctx->next = ctx->next->next);
2047 lua_pushcclosure(L, hlua_listable_proxies_pairs_iterator, 1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01002048 return 1;
2049}
2050
Thierry Fournier467913c2022-09-30 11:03:38 +02002051void hlua_listable_proxies(lua_State *L, char capabilities)
2052{
2053 struct hlua_proxy_list *list;
2054
2055 lua_newtable(L);
2056 list = lua_newuserdata(L, sizeof(*list));
2057 list->capabilities = capabilities;
2058 lua_rawseti(L, -2, 0);
2059 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_list_ref);
2060 lua_setmetatable(L, -2);
2061}
2062
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01002063int hlua_event_sub_unsub(lua_State *L)
2064{
2065 struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref);
2066
2067 BUG_ON(!sub);
2068 event_hdl_take(sub); /* keep a reference on sub until the item is GCed */
2069 event_hdl_unsubscribe(sub); /* will automatically call event_hdl_drop() */
2070 return 0;
2071}
2072
2073int hlua_event_sub_gc(lua_State *L)
2074{
2075 struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref);
2076
2077 BUG_ON(!sub);
2078 event_hdl_drop(sub); /* final drop of the reference */
2079 return 0;
2080}
2081
2082int hlua_fcn_new_event_sub(lua_State *L, struct event_hdl_sub *sub)
2083{
2084 lua_newtable(L);
2085
2086 /* Pop a class event_sub metatable and affect it to the userdata. */
2087 lua_rawgeti(L, LUA_REGISTRYINDEX, class_event_sub_ref);
2088 lua_setmetatable(L, -2);
2089
2090 lua_pushlightuserdata(L, sub);
2091 lua_rawseti(L, -2, 0);
2092
2093 /* userdata is affected: increment sub refcount */
2094 event_hdl_take(sub);
2095
2096 /* set public methods */
2097 hlua_class_function(L, "unsub", hlua_event_sub_unsub);
2098
2099 return 1;
2100}
2101
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002102/* This Lua function take a string, a list of separators.
2103 * It tokenize the input string using the list of separators
2104 * as separator.
2105 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05002106 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002107 */
2108int hlua_tokenize(lua_State *L)
2109{
2110 const char *str;
2111 const char *sep;
2112 int index;
2113 const char *token;
2114 const char *p;
2115 const char *c;
2116 int ignore_empty;
2117
2118 ignore_empty = 0;
2119
2120 str = luaL_checkstring(L, 1);
2121 sep = luaL_checkstring(L, 2);
2122 if (lua_gettop(L) == 3)
2123 ignore_empty = hlua_checkboolean(L, 3);
2124
2125 lua_newtable(L);
2126 index = 1;
2127 token = str;
2128 p = str;
2129 while(1) {
2130 for (c = sep; *c != '\0'; c++)
2131 if (*p == *c)
2132 break;
2133 if (*p == *c) {
2134 if ((!ignore_empty) || (p - token > 0)) {
2135 lua_pushlstring(L, token, p - token);
2136 lua_rawseti(L, -2, index);
2137 index++;
2138 }
2139 token = p + 1;
2140 }
2141 if (*p == '\0')
2142 break;
2143 p++;
2144 }
2145
2146 return 1;
2147}
2148
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002149int hlua_parse_addr(lua_State *L)
2150{
Christopher Faulet29e93262021-02-26 09:39:05 +01002151 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002152 const char *str = luaL_checkstring(L, 1);
2153 unsigned char mask;
2154
Christopher Faulet29e93262021-02-26 09:39:05 +01002155 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002156 if (!addr) {
2157 lua_pushnil(L);
2158 return 1;
2159 }
2160
2161 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01002162 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002163 return 1;
2164 }
2165
2166 if (str62net(str, &addr->addr.v6.ip, &mask)) {
2167 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01002168 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002169 return 1;
2170 }
2171
2172 lua_pop(L, 1);
2173 lua_pushnil(L);
2174 return 1;
2175}
2176
2177int hlua_match_addr(lua_State *L)
2178{
Christopher Faulet29e93262021-02-26 09:39:05 +01002179 struct net_addr *addr1;
2180 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002181
2182 if (!lua_isuserdata(L, 1) ||
2183 !lua_isuserdata(L, 2)) {
2184 lua_pushboolean(L, 0);
2185 return 1;
2186 }
2187
2188 addr1 = lua_touserdata(L, 1);
2189 addr2 = lua_touserdata(L, 2);
2190
Christopher Faulet29e93262021-02-26 09:39:05 +01002191 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002192 lua_pushboolean(L, 0);
2193 return 1;
2194 }
2195
Christopher Faulet29e93262021-02-26 09:39:05 +01002196 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002197 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
2198 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
2199 lua_pushboolean(L, 1);
2200 return 1;
2201 }
2202 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01002203 int i;
2204
2205 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01002206 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
2207 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
2208 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
2209 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01002210 break;
2211 }
2212 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002213 lua_pushboolean(L, 1);
2214 return 1;
2215 }
2216 }
2217
2218 lua_pushboolean(L, 0);
2219 return 1;
2220}
2221
Dragan Dosen26743032019-04-30 15:54:36 +02002222static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02002223{
2224 return (hlua_checkudata(L, ud, class_regex_ref));
2225}
2226
2227static int hlua_regex_comp(struct lua_State *L)
2228{
Dragan Dosen26743032019-04-30 15:54:36 +02002229 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002230 const char *str;
2231 int cs;
2232 char *err;
2233
2234 str = luaL_checkstring(L, 1);
2235 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
2236 cs = lua_toboolean(L, 2);
2237
2238 regex = lua_newuserdata(L, sizeof(*regex));
2239
2240 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02002241 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02002242 lua_pushboolean(L, 0); /* status error */
2243 lua_pushstring(L, err); /* Reason */
2244 free(err);
2245 return 2;
2246 }
2247
2248 lua_pushboolean(L, 1); /* Status ok */
2249
2250 /* Create object */
2251 lua_newtable(L);
2252 lua_pushvalue(L, -3); /* Get the userdata pointer. */
2253 lua_rawseti(L, -2, 0);
2254 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
2255 lua_setmetatable(L, -2);
2256 return 2;
2257}
2258
2259static int hlua_regex_exec(struct lua_State *L)
2260{
Dragan Dosen26743032019-04-30 15:54:36 +02002261 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002262 const char *str;
2263 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02002264 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002265
2266 regex = hlua_check_regex(L, 1);
2267 str = luaL_checklstring(L, 2, &len);
2268
Dragan Dosen26743032019-04-30 15:54:36 +02002269 if (!*regex) {
2270 lua_pushboolean(L, 0);
2271 return 1;
2272 }
2273
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002274 /* Copy the string because regex_exec2 require a 'char *'
2275 * and not a 'const char *'.
2276 */
2277 tmp = get_trash_chunk();
2278 if (len >= tmp->size) {
2279 lua_pushboolean(L, 0);
2280 return 1;
2281 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002282 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002283
Dragan Dosen26743032019-04-30 15:54:36 +02002284 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02002285
2286 return 1;
2287}
2288
2289static int hlua_regex_match(struct lua_State *L)
2290{
Dragan Dosen26743032019-04-30 15:54:36 +02002291 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002292 const char *str;
2293 size_t len;
2294 regmatch_t pmatch[20];
2295 int ret;
2296 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02002297 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002298
2299 regex = hlua_check_regex(L, 1);
2300 str = luaL_checklstring(L, 2, &len);
2301
Dragan Dosen26743032019-04-30 15:54:36 +02002302 if (!*regex) {
2303 lua_pushboolean(L, 0);
2304 return 1;
2305 }
2306
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002307 /* Copy the string because regex_exec2 require a 'char *'
2308 * and not a 'const char *'.
2309 */
2310 tmp = get_trash_chunk();
2311 if (len >= tmp->size) {
2312 lua_pushboolean(L, 0);
2313 return 1;
2314 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002315 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02002316
Dragan Dosen26743032019-04-30 15:54:36 +02002317 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02002318 lua_pushboolean(L, ret);
2319 lua_newtable(L);
2320 if (ret) {
2321 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
2322 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
2323 lua_rawseti(L, -2, i + 1);
2324 }
2325 }
2326 return 2;
2327}
2328
2329static int hlua_regex_free(struct lua_State *L)
2330{
Dragan Dosen26743032019-04-30 15:54:36 +02002331 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002332
2333 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02002334 regex_free(*regex);
2335 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02002336 return 0;
2337}
2338
Thierry Fournier599f2312022-09-30 10:40:39 +02002339void hlua_fcn_reg_core_fcn(lua_State *L)
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002340{
Thierry Fournier599f2312022-09-30 10:40:39 +02002341 hlua_concat_init(L);
Thierry Fournier1de16592016-01-27 09:49:07 +01002342
Thierry Fournier4f99b272016-02-22 08:40:02 +01002343 hlua_class_function(L, "now", hlua_now);
2344 hlua_class_function(L, "http_date", hlua_http_date);
2345 hlua_class_function(L, "imf_date", hlua_imf_date);
2346 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
2347 hlua_class_function(L, "asctime_date", hlua_asctime_date);
2348 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01002349 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01002350 hlua_class_function(L, "parse_addr", hlua_parse_addr);
2351 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01002352 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01002353
Thierry FOURNIER31904272017-10-25 12:59:51 +02002354 /* Create regex object. */
2355 lua_newtable(L);
2356 hlua_class_function(L, "new", hlua_regex_comp);
2357
2358 lua_newtable(L); /* The metatable. */
2359 lua_pushstring(L, "__index");
2360 lua_newtable(L);
2361 hlua_class_function(L, "exec", hlua_regex_exec);
2362 hlua_class_function(L, "match", hlua_regex_match);
2363 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
2364 hlua_class_function(L, "__gc", hlua_regex_free);
2365
2366 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
2367 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
2368
2369 lua_setmetatable(L, -2);
2370 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
2371
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002372 /* Create stktable object. */
2373 lua_newtable(L);
2374 lua_pushstring(L, "__index");
2375 lua_newtable(L);
2376 hlua_class_function(L, "info", hlua_stktable_info);
2377 hlua_class_function(L, "lookup", hlua_stktable_lookup);
2378 hlua_class_function(L, "dump", hlua_stktable_dump);
2379 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2380 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
2381
Thierry Fournierff480422016-02-25 08:36:46 +01002382 /* Create listener object. */
2383 lua_newtable(L);
2384 lua_pushstring(L, "__index");
2385 lua_newtable(L);
2386 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
2387 lua_settable(L, -3); /* -> META["__index"] = TABLE */
2388 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
2389
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01002390 /* Create event_sub object. */
2391 lua_newtable(L);
2392 hlua_class_function(L, "__gc", hlua_event_sub_gc);
2393 class_event_sub_ref = hlua_register_metatable(L, CLASS_EVENT_SUB);
2394
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002395 /* Create server object. */
2396 lua_newtable(L);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002397 hlua_class_function(L, "__gc", hlua_server_gc);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002398 hlua_class_function(L, "__index", hlua_server_index);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01002399 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
2400
Thierry Fournierf61aa632016-02-19 20:56:00 +01002401 /* Create proxy object. */
2402 lua_newtable(L);
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01002403 hlua_class_function(L, "__index", hlua_proxy_index);
Thierry Fournierf61aa632016-02-19 20:56:00 +01002404 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
2405
Thierry Fournier467913c2022-09-30 11:03:38 +02002406 /* list of proxy objects. Instead of having a static array
2407 * of proxies, we use special metamethods that rely on internal
2408 * proxies list so that the array is resolved at runtime.
2409 *
2410 * To emulate the same behavior than Lua array, we implement some
2411 * metatable functions:
2412 * - __newindex : prevent the insertion of a new item in the array
2413 * - __index : find a proxy in the list using "name" index
2414 * - __pairs : iterate through available proxies in the list
2415 */
2416 lua_newtable(L);
2417 hlua_class_function(L, "__index", hlua_listable_proxies_index);
2418 hlua_class_function(L, "__newindex", hlua_listable_proxies_newindex);
2419 hlua_class_function(L, "__pairs", hlua_listable_proxies_pairs);
2420 class_proxy_list_ref = hlua_register_metatable(L, CLASS_PROXY_LIST);
2421
2422 /* Create proxies entry. */
2423 lua_pushstring(L, "proxies");
2424 hlua_listable_proxies(L, PR_CAP_LISTEN);
2425 lua_settable(L, -3);
2426
2427 /* Create frontends entry. */
2428 lua_pushstring(L, "frontends");
2429 hlua_listable_proxies(L, PR_CAP_FE);
2430 lua_settable(L, -3);
2431
2432 /* Create backends entry. */
2433 lua_pushstring(L, "backends");
2434 hlua_listable_proxies(L, PR_CAP_BE);
2435 lua_settable(L, -3);
Thierry Fournier1edf36a2022-10-07 13:25:51 +02002436
2437 /* list of server. This object is similar to
2438 * CLASS_PROXY_LIST
2439 */
2440 lua_newtable(L);
2441 hlua_class_function(L, "__index", hlua_listable_servers_index);
2442 hlua_class_function(L, "__newindex", hlua_listable_servers_newindex);
2443 hlua_class_function(L, "__pairs", hlua_listable_servers_pairs);
2444 class_server_list_ref = hlua_register_metatable(L, CLASS_SERVER_LIST);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01002445}