blob: 4c16d90a87cdebb0c65e7862e78b85c58adfc3ac [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>
Willy Tarreau86416052020-06-04 09:20:54 +020028#include <haproxy/hlua-t.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020029#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020030#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/pattern-t.h>
32#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020033#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020034#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020035#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020036#include <haproxy/stick_table.h>
Willy Tarreau27539402021-10-06 09:12:44 +020037#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010039#include <haproxy/tools.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010040
Thierry Fournier1de16592016-01-27 09:49:07 +010041/* Contains the class reference of the concat object. */
42static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010043static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010044static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010045static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020046static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020047static int class_stktable_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010048
Thierry Fournierf61aa632016-02-19 20:56:00 +010049#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010050
Thierry FOURNIERffbad792017-07-12 11:39:04 +020051static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010052
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010053int hlua_checkboolean(lua_State *L, int index)
54{
55 if (!lua_isboolean(L, index))
56 luaL_argerror(L, index, "boolean expected");
57 return lua_toboolean(L, index);
58}
59
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020060/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
61static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
62{
63#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
64 lua_pushinteger(L, val);
65#else
66 if (val > INT_MAX)
67 lua_pushnumber(L, (lua_Number)val);
68 else
69 lua_pushinteger(L, (int)val);
70#endif
71 return 1;
72}
73
74/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
75static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
76#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
77 /* 64 bits case, U64 is supported until LLONG_MAX */
78 if (val > LLONG_MAX)
79 lua_pushnumber(L, (lua_Number)val);
80 else
81 lua_pushinteger(L, val);
82#else
83 /* 32 bits case, U64 is supported until INT_MAX */
84 if (val > INT_MAX)
85 lua_pushnumber(L, (lua_Number)val);
86 else
87 lua_pushinteger(L, (int)val);
88#endif
89 return 1;
90}
91
Joseph Herlantb3d92e32018-11-15 09:35:04 -080092/* This function gets a struct field and converts it in Lua
93 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010094 */
95int hlua_fcn_pushfield(lua_State *L, struct field *field)
96{
97 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -080098 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010099 * by some macros. Windows maros are not supported.
100 * If the number cannot be represented as integer, we try to
101 * convert to float.
102 */
103 switch (field_format(field, 0)) {
104
105 case FF_EMPTY:
106 lua_pushnil(L);
107 return 1;
108
109 case FF_S32:
110 /* S32 is always supported. */
111 lua_pushinteger(L, field->u.s32);
112 return 1;
113
114 case FF_U32:
115#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
116 /* 64 bits case, U32 is always supported */
117 lua_pushinteger(L, field->u.u32);
118#else
119 /* 32 bits case, U32 is supported until INT_MAX. */
120 if (field->u.u32 > INT_MAX)
121 lua_pushnumber(L, (lua_Number)field->u.u32);
122 else
123 lua_pushinteger(L, field->u.u32);
124#endif
125 return 1;
126
127 case FF_S64:
128#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
129 /* 64 bits case, S64 is always supported */
130 lua_pushinteger(L, field->u.s64);
131#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500132 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100133 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
134 lua_pushnumber(L, (lua_Number)field->u.s64);
135 else
136 lua_pushinteger(L, (int)field->u.s64);
137#endif
138 return 1;
139
140 case FF_U64:
141#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
142 /* 64 bits case, U64 is supported until LLONG_MAX */
143 if (field->u.u64 > LLONG_MAX)
144 lua_pushnumber(L, (lua_Number)field->u.u64);
145 else
146 lua_pushinteger(L, field->u.u64);
147#else
148 /* 64 bits case, U64 is supported until INT_MAX */
149 if (field->u.u64 > INT_MAX)
150 lua_pushnumber(L, (lua_Number)field->u.u64);
151 else
152 lua_pushinteger(L, (int)field->u.u64);
153#endif
154 return 1;
155
156 case FF_STR:
157 lua_pushstring(L, field->u.str);
158 return 1;
159
160 default:
161 break;
162 }
163
164 /* Default case, never reached. */
165 lua_pushnil(L);
166 return 1;
167}
168
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100169/* Some string are started or terminated by blank chars,
170 * this function removes the spaces, tabs, \r and
171 * \n at the begin and at the end of the string "str", and
172 * push the result in the lua stack.
173 * Returns a pointer to the Lua internal copy of the string.
174 */
175const char *hlua_pushstrippedstring(lua_State *L, const char *str)
176{
177 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100178 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100179
180 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100181
182 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100183
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100184 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100185}
186
Thierry Fournierddd89882016-02-22 19:52:08 +0100187/* The three following functions are useful for adding entries
188 * in a table. These functions takes a string and respectively an
189 * integer, a string or a function and add it to the table in the
190 * top of the stack.
191 *
192 * These functions throws an error if no more stack size is
193 * available.
194 */
195void hlua_class_const_int(lua_State *L, const char *name, int value)
196{
Thierry Fournierddd89882016-02-22 19:52:08 +0100197 lua_pushstring(L, name);
198 lua_pushinteger(L, value);
199 lua_rawset(L, -3);
200}
201void hlua_class_const_str(lua_State *L, const char *name, const char *value)
202{
Thierry Fournierddd89882016-02-22 19:52:08 +0100203 lua_pushstring(L, name);
204 lua_pushstring(L, value);
205 lua_rawset(L, -3);
206}
207void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
208{
Thierry Fournierddd89882016-02-22 19:52:08 +0100209 lua_pushstring(L, name);
210 lua_pushcclosure(L, function, 0);
211 lua_rawset(L, -3);
212}
213
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800214/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100215int hlua_dump_object(struct lua_State *L)
216{
217 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
218 lua_pushfstring(L, "HAProxy class %s", name);
219 return 1;
220}
221
Thierry Fournier45e78d72016-02-19 18:34:46 +0100222/* This function register a table as metatable and. It names
223 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500224 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100225 * "name" is the referenced class name.
226 */
227int hlua_register_metatable(struct lua_State *L, char *name)
228{
229 /* Check the type of the top element. it must be
230 * a table.
231 */
232 if (lua_type(L, -1) != LUA_TTABLE)
233 luaL_error(L, "hlua_register_metatable() requires a type Table "
234 "in the top of the stack");
235
236 /* Add the __tostring function which identify the
237 * created object.
238 */
239 lua_pushstring(L, "__tostring");
240 lua_pushstring(L, name);
241 lua_pushcclosure(L, hlua_dump_object, 1);
242 lua_rawset(L, -3);
243
244 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500245 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100246 * lua_setfield() pop the entry.
247 */
248 lua_pushvalue(L, -1);
249 lua_setfield(L, LUA_REGISTRYINDEX, name);
250
251 /* Creates the reference of the object. The
252 * function luaL_ref pop the top of the stack.
253 */
254 return luaL_ref(L, LUA_REGISTRYINDEX);
255}
256
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100257/* Return an object of the expected type, or throws an error. */
258void *hlua_checkudata(lua_State *L, int ud, int class_ref)
259{
260 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100261 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100262
263 /* Check if the stack entry is an array. */
264 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100265 luaL_argerror(L, ud, NULL);
266
267 /* pop the metatable of the referencecd object. */
268 if (!lua_getmetatable(L, ud))
269 luaL_argerror(L, ud, NULL);
270
271 /* pop the expected metatable. */
272 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
273
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100274 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100275 ret = lua_rawequal(L, -1, -2);
276 lua_pop(L, 2);
277 if (!ret)
278 luaL_argerror(L, ud, NULL);
279
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100280 /* Push on the stack at the entry [0] of the table. */
281 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100282
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100283 /* Check if this entry is userdata. */
284 p = lua_touserdata(L, -1);
285 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100286 luaL_argerror(L, ud, NULL);
287
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100288 /* Remove the entry returned by lua_rawgeti(). */
289 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100290
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100291 /* Return the associated struct. */
292 return p;
293}
294
Thierry Fournierb1f46562016-01-21 09:46:15 +0100295/* This function return the current date at epoch format in milliseconds. */
296int hlua_now(lua_State *L)
297{
298 lua_newtable(L);
299 lua_pushstring(L, "sec");
300 lua_pushinteger(L, now.tv_sec);
301 lua_rawset(L, -3);
302 lua_pushstring(L, "usec");
303 lua_pushinteger(L, now.tv_usec);
304 lua_rawset(L, -3);
305 return 1;
306}
307
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100308/* This functions expects a Lua string as HTTP date, parse it and
309 * returns an integer containing the epoch format of the date, or
310 * nil if the parsing fails.
311 */
312static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
313{
314 const char *str;
315 size_t len;
316 struct tm tm;
317 time_t time;
318
319 str = luaL_checklstring(L, 1, &len);
320
321 if (!fcn(str, len, &tm)) {
322 lua_pushnil(L);
323 return 1;
324 }
325
326 /* This function considers the content of the broken-down time
327 * is exprimed in the UTC timezone. timegm don't care about
328 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
329 * the timezone from the broken-down time, it must be fixed
330 * after the conversion.
331 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200332 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100333 if (time == -1) {
334 lua_pushnil(L);
335 return 1;
336 }
337
338 lua_pushinteger(L, (int)time);
339 return 1;
340}
341static int hlua_http_date(lua_State *L)
342{
343 return hlua_parse_date(L, parse_http_date);
344}
345static int hlua_imf_date(lua_State *L)
346{
347 return hlua_parse_date(L, parse_imf_date);
348}
349static int hlua_rfc850_date(lua_State *L)
350{
351 return hlua_parse_date(L, parse_rfc850_date);
352}
353static int hlua_asctime_date(lua_State *L)
354{
355 return hlua_parse_date(L, parse_asctime_date);
356}
357
Thierry Fourniereea77c02016-03-18 08:47:13 +0100358static int hlua_get_info(lua_State *L)
359{
360 int i;
361
Willy Tarreau0b26b382021-05-08 07:43:53 +0200362 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100363
364 lua_newtable(L);
365 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200366 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100367 hlua_fcn_pushfield(L, &stats[i]);
368 lua_settable(L, -3);
369 }
370 return 1;
371}
372
Thierry Fournier49d48422016-02-19 12:09:29 +0100373static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100374{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200375 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100376}
377
378static int hlua_concat_add(lua_State *L)
379{
Thierry Fournier49d48422016-02-19 12:09:29 +0100380 struct hlua_concat *b;
381 char *buffer;
382 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100383 const char *str;
384 size_t l;
385
386 /* First arg must be a concat object. */
387 b = hlua_check_concat(L, 1);
388
389 /* Second arg must be a string. */
390 str = luaL_checklstring(L, 2, &l);
391
Thierry Fournier49d48422016-02-19 12:09:29 +0100392 /* Get the buffer. */
393 lua_rawgeti(L, 1, 1);
394 buffer = lua_touserdata(L, -1);
395 lua_pop(L, 1);
396
397 /* Update the buffer size if it s required. The old buffer
398 * is crushed by the new in the object array, so it will
399 * be deleted by the GC.
400 * Note that in the first loop, the "new" variable is only
401 * used as a flag.
402 */
403 new = NULL;
404 while (b->size - b->len < l) {
405 b->size += HLUA_CONCAT_BLOCSZ;
406 new = buffer;
407 }
408 if (new) {
409 new = lua_newuserdata(L, b->size);
410 memcpy(new, buffer, b->len);
411 lua_rawseti(L, 1, 1);
412 buffer = new;
413 }
414
415 /* Copy string, and update metadata. */
416 memcpy(buffer + b->len, str, l);
417 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100418 return 0;
419}
420
421static int hlua_concat_dump(lua_State *L)
422{
Thierry Fournier49d48422016-02-19 12:09:29 +0100423 struct hlua_concat *b;
424 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100425
426 /* First arg must be a concat object. */
427 b = hlua_check_concat(L, 1);
428
Thierry Fournier49d48422016-02-19 12:09:29 +0100429 /* Get the buffer. */
430 lua_rawgeti(L, 1, 1);
431 buffer = lua_touserdata(L, -1);
432 lua_pop(L, 1);
433
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500434 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100435 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100436 return 1;
437}
438
439int hlua_concat_new(lua_State *L)
440{
Thierry Fournier49d48422016-02-19 12:09:29 +0100441 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100442
443 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200444 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100445 b->size = HLUA_CONCAT_BLOCSZ;
446 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100447 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100448 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
449 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100450
451 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
452 lua_setmetatable(L, -2);
453
Thierry Fournier1de16592016-01-27 09:49:07 +0100454 return 1;
455}
456
457static int concat_tostring(lua_State *L)
458{
459 const void *ptr = lua_topointer(L, 1);
460 lua_pushfstring(L, "Concat object: %p", ptr);
461 return 1;
462}
463
464static int hlua_concat_init(lua_State *L)
465{
466 /* Creates the buffered concat object. */
467 lua_newtable(L);
468
469 lua_pushstring(L, "__tostring");
470 lua_pushcclosure(L, concat_tostring, 0);
471 lua_settable(L, -3);
472
473 lua_pushstring(L, "__index"); /* Creates the index entry. */
474 lua_newtable(L); /* The "__index" content. */
475
476 lua_pushstring(L, "add");
477 lua_pushcclosure(L, hlua_concat_add, 0);
478 lua_settable(L, -3);
479
480 lua_pushstring(L, "dump");
481 lua_pushcclosure(L, hlua_concat_dump, 0);
482 lua_settable(L, -3);
483
484 lua_settable(L, -3); /* Sets the __index entry. */
485 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
486
487 return 1;
488}
489
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200490int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
491{
492 lua_newtable(L);
493
494 /* Pop a class stktbl metatable and affect it to the userdata. */
495 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
496 lua_setmetatable(L, -2);
497
498 lua_pushlightuserdata(L, tbl);
499 lua_rawseti(L, -2, 0);
500 return 1;
501}
502
503static struct stktable *hlua_check_stktable(lua_State *L, int ud)
504{
505 return hlua_checkudata(L, ud, class_stktable_ref);
506}
507
508/* Extract stick table attributes into Lua table */
509int hlua_stktable_info(lua_State *L)
510{
511 struct stktable *tbl;
512 int dt;
513
514 tbl = hlua_check_stktable(L, 1);
515
516 if (!tbl->id) {
517 lua_pushnil(L);
518 return 1;
519 }
520
521 lua_newtable(L);
522
523 lua_pushstring(L, "type");
524 lua_pushstring(L, stktable_types[tbl->type].kw);
525 lua_settable(L, -3);
526
527 lua_pushstring(L, "length");
528 lua_pushinteger(L, tbl->key_size);
529 lua_settable(L, -3);
530
531 lua_pushstring(L, "size");
532 hlua_fcn_pushunsigned(L, tbl->size);
533 lua_settable(L, -3);
534
535 lua_pushstring(L, "used");
536 hlua_fcn_pushunsigned(L, tbl->current);
537 lua_settable(L, -3);
538
539 lua_pushstring(L, "nopurge");
540 lua_pushboolean(L, tbl->nopurge > 0);
541 lua_settable(L, -3);
542
543 lua_pushstring(L, "expire");
544 lua_pushinteger(L, tbl->expire);
545 lua_settable(L, -3);
546
547 /* Save data types periods (if applicable) in 'data' table */
548 lua_pushstring(L, "data");
549 lua_newtable(L);
550
551 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
552 if (tbl->data_ofs[dt] == 0)
553 continue;
554
555 lua_pushstring(L, stktable_data_types[dt].name);
556
557 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
558 lua_pushinteger(L, tbl->data_arg[dt].u);
559 else
560 lua_pushinteger(L, -1);
561
562 lua_settable(L, -3);
563 }
564
565 lua_settable(L, -3);
566
567 return 1;
568}
569
570/* Helper to get extract stick table entry into Lua table */
571static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
572{
573 int dt;
574 void *ptr;
575
576 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
577
578 if (t->data_ofs[dt] == 0)
579 continue;
580
581 lua_pushstring(L, stktable_data_types[dt].name);
582
583 ptr = stktable_data_ptr(t, ts, dt);
584 switch (stktable_data_types[dt].std_type) {
585 case STD_T_SINT:
586 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
587 break;
588 case STD_T_UINT:
589 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
590 break;
591 case STD_T_ULL:
592 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
593 break;
594 case STD_T_FRQP:
595 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
596 t->data_arg[dt].u));
597 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200598 case STD_T_DICT: {
599 struct dict_entry *de;
600 de = stktable_data_cast(ptr, std_t_dict);
601 lua_pushstring(L, de ? (char *)de->value.key : "-");
602 break;
603 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200604 }
605
606 lua_settable(L, -3);
607 }
608}
609
610/* Looks in table <t> for a sticky session matching key <key>
611 * Returns table with session data or nil
612 *
613 * The returned table always contains 'use' and 'expire' (integer) fields.
614 * For frequency/rate counters, each data entry is returned as table with
615 * 'value' and 'period' fields.
616 */
617int hlua_stktable_lookup(lua_State *L)
618{
619 struct stktable *t;
620 struct sample smp;
621 struct stktable_key *skey;
622 struct stksess *ts;
623
624 t = hlua_check_stktable(L, 1);
625 smp.data.type = SMP_T_STR;
626 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600627 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200628
629 skey = smp_to_stkey(&smp, t);
630 if (!skey) {
631 lua_pushnil(L);
632 return 1;
633 }
634
635 ts = stktable_lookup_key(t, skey);
636 if (!ts) {
637 lua_pushnil(L);
638 return 1;
639 }
640
641 lua_newtable(L);
642 lua_pushstring(L, "use");
643 lua_pushinteger(L, ts->ref_cnt - 1);
644 lua_settable(L, -3);
645
646 lua_pushstring(L, "expire");
647 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
648 lua_settable(L, -3);
649
650 hlua_stktable_entry(L, t, ts);
651 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
652 ts->ref_cnt--;
653 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
654
655 return 1;
656}
657
658struct stk_filter {
659 long long val;
660 int type;
661 int op;
662};
663
664
665/* Helper for returning errors to callers using Lua convention (nil, err) */
666static int hlua_error(lua_State *L, const char *fmt, ...) {
667 char buf[256];
668 int len;
669 va_list args;
670 va_start(args, fmt);
671 len = vsnprintf(buf, sizeof(buf), fmt, args);
672 va_end(args);
673
674 if (len < 0) {
675 ha_alert("hlua_error(): Could not write error message.\n");
676 lua_pushnil(L);
677 return 1;
678 } else if (len >= sizeof(buf))
679 ha_alert("hlua_error(): Error message was truncated.\n");
680
681 lua_pushnil(L);
682 lua_pushstring(L, buf);
683
684 return 2;
685}
686
687/* Dump the contents of stick table <t>*/
688int hlua_stktable_dump(lua_State *L)
689{
690 struct stktable *t;
691 struct ebmb_node *eb;
692 struct ebmb_node *n;
693 struct stksess *ts;
694 int type;
695 int op;
696 int dt;
697 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100698 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200699 int filter_count = 0;
700 int i;
701 int skip_entry;
702 void *ptr;
703
704 t = hlua_check_stktable(L, 1);
705 type = lua_type(L, 2);
706
707 switch (type) {
708 case LUA_TNONE:
709 case LUA_TNIL:
710 break;
711 case LUA_TTABLE:
712 lua_pushnil(L);
713 while (lua_next(L, 2) != 0) {
714 int entry_idx = 0;
715
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100716 if (filter_count >= STKTABLE_FILTER_LEN)
717 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200718
719 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
720 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
721
722 lua_pushnil(L);
723 while (lua_next(L, -2) != 0) {
724 switch (entry_idx) {
725 case 0:
726 if (lua_type(L, -1) != LUA_TSTRING)
727 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
728
729 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
730 if (dt < 0 || t->data_ofs[dt] == 0)
731 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
732 filter[filter_count].type = dt;
733 break;
734 case 1:
735 if (lua_type(L, -1) != LUA_TSTRING)
736 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
737
738 op = get_std_op(lua_tostring(L, -1));
739 if (op < 0)
740 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
741 filter[filter_count].op = op;
742 break;
743 case 2:
744 val = lua_tointeger(L, -1);
745 filter[filter_count].val = val;
746 filter_count++;
747 break;
748 default:
749 break;
750 }
751 entry_idx++;
752 lua_pop(L, 1);
753 }
754 lua_pop(L, 1);
755 }
756 break;
757 default:
758 return hlua_error(L, "filter table expected");
759 }
760
761 lua_newtable(L);
762
763 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
764 eb = ebmb_first(&t->keys);
765 for (n = eb; n; n = ebmb_next(n)) {
766 ts = ebmb_entry(n, struct stksess, key);
767 if (!ts) {
768 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
769 return 1;
770 }
771 ts->ref_cnt++;
772 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
773
774 /* multi condition/value filter */
775 skip_entry = 0;
776 for (i = 0; i < filter_count; i++) {
777 if (t->data_ofs[filter[i].type] == 0)
778 continue;
779
780 ptr = stktable_data_ptr(t, ts, filter[i].type);
781
782 switch (stktable_data_types[filter[i].type].std_type) {
783 case STD_T_SINT:
784 val = stktable_data_cast(ptr, std_t_sint);
785 break;
786 case STD_T_UINT:
787 val = stktable_data_cast(ptr, std_t_uint);
788 break;
789 case STD_T_ULL:
790 val = stktable_data_cast(ptr, std_t_ull);
791 break;
792 case STD_T_FRQP:
793 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
794 t->data_arg[filter[i].type].u);
795 break;
796 default:
797 continue;
798 break;
799 }
800
801 op = filter[i].op;
802
803 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
804 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
805 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
806 skip_entry = 1;
807 break;
808 }
809 }
810
811 if (skip_entry) {
812 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
813 ts->ref_cnt--;
814 continue;
815 }
816
817 if (t->type == SMP_T_IPV4) {
818 char addr[INET_ADDRSTRLEN];
819 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
820 lua_pushstring(L, addr);
821 } else if (t->type == SMP_T_IPV6) {
822 char addr[INET6_ADDRSTRLEN];
823 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
824 lua_pushstring(L, addr);
825 } else if (t->type == SMP_T_SINT) {
826 lua_pushinteger(L, *ts->key.key);
827 } else if (t->type == SMP_T_STR) {
828 lua_pushstring(L, (const char *)ts->key.key);
829 } else {
830 return hlua_error(L, "Unsupported stick table key type");
831 }
832
833 lua_newtable(L);
834 hlua_stktable_entry(L, t, ts);
835 lua_settable(L, -3);
836 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
837 ts->ref_cnt--;
838 }
839 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
840
841 return 1;
842}
843
Thierry Fournierff480422016-02-25 08:36:46 +0100844int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
845{
846 lua_newtable(L);
847
848 /* Pop a class sesison metatable and affect it to the userdata. */
849 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
850 lua_setmetatable(L, -2);
851
852 lua_pushlightuserdata(L, lst);
853 lua_rawseti(L, -2, 0);
854 return 1;
855}
856
857static struct listener *hlua_check_listener(lua_State *L, int ud)
858{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200859 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100860}
861
862int hlua_listener_get_stats(lua_State *L)
863{
864 struct listener *li;
865 int i;
866
867 li = hlua_check_listener(L, 1);
868
Willy Tarreauc95bad52016-12-22 00:13:31 +0100869 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100870 lua_pushnil(L);
871 return 1;
872 }
873
William Dauchy655e14e2021-02-14 23:22:54 +0100874 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
875 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +0100876
877 lua_newtable(L);
878 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200879 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100880 hlua_fcn_pushfield(L, &stats[i]);
881 lua_settable(L, -3);
882 }
883 return 1;
884
885}
886
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100887int hlua_fcn_new_server(lua_State *L, struct server *srv)
888{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400889 char buffer[12];
890
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100891 lua_newtable(L);
892
893 /* Pop a class sesison metatable and affect it to the userdata. */
894 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
895 lua_setmetatable(L, -2);
896
897 lua_pushlightuserdata(L, srv);
898 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400899
900 /* Add server name. */
901 lua_pushstring(L, "name");
902 lua_pushstring(L, srv->id);
903 lua_settable(L, -3);
904
905 /* Add server puid. */
906 lua_pushstring(L, "puid");
907 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
908 lua_pushstring(L, buffer);
909 lua_settable(L, -3);
910
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100911 return 1;
912}
913
914static struct server *hlua_check_server(lua_State *L, int ud)
915{
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200916 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
917 srv->flags |= SRV_F_NON_PURGEABLE;
918 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100919}
920
921int hlua_server_get_stats(lua_State *L)
922{
923 struct server *srv;
924 int i;
925
926 srv = hlua_check_server(L, 1);
927
928 if (!srv->proxy) {
929 lua_pushnil(L);
930 return 1;
931 }
932
William Dauchyd3a9a492021-01-25 17:29:03 +0100933 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
934 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100935
936 lua_newtable(L);
937 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200938 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100939 hlua_fcn_pushfield(L, &stats[i]);
940 lua_settable(L, -3);
941 }
942 return 1;
943
944}
945
946int hlua_server_get_addr(lua_State *L)
947{
948 struct server *srv;
949 char addr[INET6_ADDRSTRLEN];
950 luaL_Buffer b;
951
952 srv = hlua_check_server(L, 1);
953
954 luaL_buffinit(L, &b);
955
956 switch (srv->addr.ss_family) {
957 case AF_INET:
958 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
959 addr, INET_ADDRSTRLEN);
960 luaL_addstring(&b, addr);
961 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400962 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100963 luaL_addstring(&b, addr);
964 break;
965 case AF_INET6:
966 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400967 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100968 luaL_addstring(&b, addr);
969 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400970 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100971 luaL_addstring(&b, addr);
972 break;
973 case AF_UNIX:
974 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
975 break;
976 default:
977 luaL_addstring(&b, "<unknown>");
978 break;
979 }
980
981 luaL_pushresult(&b);
982 return 1;
983}
984
985int hlua_server_is_draining(lua_State *L)
986{
987 struct server *srv;
988
989 srv = hlua_check_server(L, 1);
990 lua_pushinteger(L, server_is_draining(srv));
991 return 1;
992}
993
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400994int hlua_server_set_maxconn(lua_State *L)
995{
996 struct server *srv;
997 const char *maxconn;
998 const char *err;
999
1000 srv = hlua_check_server(L, 1);
1001 maxconn = luaL_checkstring(L, 2);
1002
1003 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1004 err = server_parse_maxconn_change_request(srv, maxconn);
1005 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1006 if (!err)
1007 lua_pushnil(L);
1008 else
1009 hlua_pushstrippedstring(L, err);
1010 return 1;
1011}
1012
1013int hlua_server_get_maxconn(lua_State *L)
1014{
1015 struct server *srv;
1016
1017 srv = hlua_check_server(L, 1);
1018 lua_pushinteger(L, srv->maxconn);
1019 return 1;
1020}
1021
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001022int hlua_server_set_weight(lua_State *L)
1023{
1024 struct server *srv;
1025 const char *weight;
1026 const char *err;
1027
1028 srv = hlua_check_server(L, 1);
1029 weight = luaL_checkstring(L, 2);
1030
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001031 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001032 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001033 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001034 if (!err)
1035 lua_pushnil(L);
1036 else
1037 hlua_pushstrippedstring(L, err);
1038 return 1;
1039}
1040
1041int hlua_server_get_weight(lua_State *L)
1042{
1043 struct server *srv;
1044
1045 srv = hlua_check_server(L, 1);
1046 lua_pushinteger(L, srv->uweight);
1047 return 1;
1048}
1049
1050int hlua_server_set_addr(lua_State *L)
1051{
1052 struct server *srv;
1053 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001054 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001055 const char *err;
1056
1057 srv = hlua_check_server(L, 1);
1058 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001059 if (lua_gettop(L) >= 3)
1060 port = luaL_checkstring(L, 3);
1061 else
1062 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001063
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001064 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001065 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001066 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001067 if (!err)
1068 lua_pushnil(L);
1069 else
1070 hlua_pushstrippedstring(L, err);
1071 return 1;
1072}
1073
1074int hlua_server_shut_sess(lua_State *L)
1075{
1076 struct server *srv;
1077
1078 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001079 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001080 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001081 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001082 return 0;
1083}
1084
1085int hlua_server_set_drain(lua_State *L)
1086{
1087 struct server *srv;
1088
1089 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001090 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001091 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001092 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001093 return 0;
1094}
1095
1096int hlua_server_set_maint(lua_State *L)
1097{
1098 struct server *srv;
1099
1100 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001101 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001102 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001103 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001104 return 0;
1105}
1106
1107int hlua_server_set_ready(lua_State *L)
1108{
1109 struct server *srv;
1110
1111 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001112 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001113 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001114 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001115 return 0;
1116}
1117
1118int hlua_server_check_enable(lua_State *L)
1119{
1120 struct server *sv;
1121
1122 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001123 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001124 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001125 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001126 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001127 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001128 return 0;
1129}
1130
1131int hlua_server_check_disable(lua_State *L)
1132{
1133 struct server *sv;
1134
1135 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001136 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001137 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001138 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001139 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001140 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001141 return 0;
1142}
1143
1144int hlua_server_check_force_up(lua_State *L)
1145{
1146 struct server *sv;
1147
1148 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001149 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001150 if (!(sv->track)) {
1151 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001152 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001153 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001154 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001155 return 0;
1156}
1157
1158int hlua_server_check_force_nolb(lua_State *L)
1159{
1160 struct server *sv;
1161
1162 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001163 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001164 if (!(sv->track)) {
1165 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001166 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001167 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001168 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001169 return 0;
1170}
1171
1172int hlua_server_check_force_down(lua_State *L)
1173{
1174 struct server *sv;
1175
1176 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001177 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001178 if (!(sv->track)) {
1179 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001180 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001181 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001182 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001183 return 0;
1184}
1185
1186int hlua_server_agent_enable(lua_State *L)
1187{
1188 struct server *sv;
1189
1190 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001191 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001192 if (sv->agent.state & CHK_ST_CONFIGURED) {
1193 sv->agent.state |= CHK_ST_ENABLED;
1194 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001195 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001196 return 0;
1197}
1198
1199int hlua_server_agent_disable(lua_State *L)
1200{
1201 struct server *sv;
1202
1203 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001204 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001205 if (sv->agent.state & CHK_ST_CONFIGURED) {
1206 sv->agent.state &= ~CHK_ST_ENABLED;
1207 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001208 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001209 return 0;
1210}
1211
1212int hlua_server_agent_force_up(lua_State *L)
1213{
1214 struct server *sv;
1215
1216 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001217 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001218 if (sv->agent.state & CHK_ST_ENABLED) {
1219 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001220 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001221 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001223 return 0;
1224}
1225
1226int hlua_server_agent_force_down(lua_State *L)
1227{
1228 struct server *sv;
1229
1230 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001231 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001232 if (sv->agent.state & CHK_ST_ENABLED) {
1233 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001234 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001235 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001236 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001237 return 0;
1238}
1239
Thierry Fournierf61aa632016-02-19 20:56:00 +01001240int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1241{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001242 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001243 struct listener *lst;
1244 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001245 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001246
Thierry Fournierf61aa632016-02-19 20:56:00 +01001247 lua_newtable(L);
1248
1249 /* Pop a class sesison metatable and affect it to the userdata. */
1250 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1251 lua_setmetatable(L, -2);
1252
1253 lua_pushlightuserdata(L, px);
1254 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001255
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001256 /* Add proxy name. */
1257 lua_pushstring(L, "name");
1258 lua_pushstring(L, px->id);
1259 lua_settable(L, -3);
1260
Baptiste Assmann46c72552017-10-26 21:51:58 +02001261 /* Add proxy uuid. */
1262 lua_pushstring(L, "uuid");
1263 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1264 lua_pushstring(L, buffer);
1265 lua_settable(L, -3);
1266
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001267 /* Browse and register servers. */
1268 lua_pushstring(L, "servers");
1269 lua_newtable(L);
1270 for (srv = px->srv; srv; srv = srv->next) {
1271 lua_pushstring(L, srv->id);
1272 hlua_fcn_new_server(L, srv);
1273 lua_settable(L, -3);
1274 }
1275 lua_settable(L, -3);
1276
Thierry Fournierff480422016-02-25 08:36:46 +01001277 /* Browse and register listeners. */
1278 lua_pushstring(L, "listeners");
1279 lua_newtable(L);
1280 lid = 1;
1281 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1282 if (lst->name)
1283 lua_pushstring(L, lst->name);
1284 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001285 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001286 lid++;
1287 lua_pushstring(L, buffer);
1288 }
1289 hlua_fcn_new_listener(L, lst);
1290 lua_settable(L, -3);
1291 }
1292 lua_settable(L, -3);
1293
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001294 if (px->table && px->table->id) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001295 lua_pushstring(L, "stktable");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001296 hlua_fcn_new_stktable(L, px->table);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001297 lua_settable(L, -3);
1298 }
1299
Thierry Fournierf61aa632016-02-19 20:56:00 +01001300 return 1;
1301}
1302
1303static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1304{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001305 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001306}
1307
1308int hlua_proxy_pause(lua_State *L)
1309{
1310 struct proxy *px;
1311
1312 px = hlua_check_proxy(L, 1);
1313 pause_proxy(px);
1314 return 0;
1315}
1316
1317int hlua_proxy_resume(lua_State *L)
1318{
1319 struct proxy *px;
1320
1321 px = hlua_check_proxy(L, 1);
1322 resume_proxy(px);
1323 return 0;
1324}
1325
1326int hlua_proxy_stop(lua_State *L)
1327{
1328 struct proxy *px;
1329
1330 px = hlua_check_proxy(L, 1);
1331 stop_proxy(px);
1332 return 0;
1333}
1334
1335int hlua_proxy_get_cap(lua_State *L)
1336{
1337 struct proxy *px;
1338 const char *str;
1339
1340 px = hlua_check_proxy(L, 1);
1341 str = proxy_cap_str(px->cap);
1342 lua_pushstring(L, str);
1343 return 1;
1344}
1345
1346int hlua_proxy_get_stats(lua_State *L)
1347{
1348 struct proxy *px;
1349 int i;
1350
1351 px = hlua_check_proxy(L, 1);
1352 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001353 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001354 else
William Dauchy0ef54392021-01-17 18:27:45 +01001355 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001356 lua_newtable(L);
1357 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001358 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001359 hlua_fcn_pushfield(L, &stats[i]);
1360 lua_settable(L, -3);
1361 }
1362 return 1;
1363}
1364
1365int hlua_proxy_get_mode(lua_State *L)
1366{
1367 struct proxy *px;
1368 const char *str;
1369
1370 px = hlua_check_proxy(L, 1);
1371 str = proxy_mode_str(px->mode);
1372 lua_pushstring(L, str);
1373 return 1;
1374}
1375
1376int hlua_proxy_shut_bcksess(lua_State *L)
1377{
1378 struct proxy *px;
1379
1380 px = hlua_check_proxy(L, 1);
1381 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1382 return 0;
1383}
1384
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001385int hlua_fcn_post_init(lua_State *L)
1386{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001387 struct proxy *px;
1388
1389 /* get core array. */
1390 if (lua_getglobal(L, "core") != LUA_TTABLE)
1391 lua_error(L);
1392
1393 /* Create proxies entry. */
1394 lua_pushstring(L, "proxies");
1395 lua_newtable(L);
1396
1397 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001398 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001399 if (px->cap & PR_CAP_INT)
1400 continue;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001401 lua_pushstring(L, px->id);
1402 hlua_fcn_new_proxy(L, px);
1403 lua_settable(L, -3);
1404 }
1405
1406 /* push "proxies" in "core" */
1407 lua_settable(L, -3);
1408
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001409 /* Create proxies entry. */
1410 lua_pushstring(L, "frontends");
1411 lua_newtable(L);
1412
1413 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001414 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001415 if (!(px->cap & PR_CAP_FE) || (px->cap & PR_CAP_INT))
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001416 continue;
1417 lua_pushstring(L, px->id);
1418 hlua_fcn_new_proxy(L, px);
1419 lua_settable(L, -3);
1420 }
1421
1422 /* push "frontends" in "core" */
1423 lua_settable(L, -3);
1424
1425 /* Create proxies entry. */
1426 lua_pushstring(L, "backends");
1427 lua_newtable(L);
1428
1429 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001430 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001431 if (!(px->cap & PR_CAP_BE) || (px->cap & PR_CAP_INT))
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001432 continue;
1433 lua_pushstring(L, px->id);
1434 hlua_fcn_new_proxy(L, px);
1435 lua_settable(L, -3);
1436 }
1437
1438 /* push "backend" in "core" */
1439 lua_settable(L, -3);
1440
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001441 return 1;
1442}
1443
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001444/* This Lua function take a string, a list of separators.
1445 * It tokenize the input string using the list of separators
1446 * as separator.
1447 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001448 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001449 */
1450int hlua_tokenize(lua_State *L)
1451{
1452 const char *str;
1453 const char *sep;
1454 int index;
1455 const char *token;
1456 const char *p;
1457 const char *c;
1458 int ignore_empty;
1459
1460 ignore_empty = 0;
1461
1462 str = luaL_checkstring(L, 1);
1463 sep = luaL_checkstring(L, 2);
1464 if (lua_gettop(L) == 3)
1465 ignore_empty = hlua_checkboolean(L, 3);
1466
1467 lua_newtable(L);
1468 index = 1;
1469 token = str;
1470 p = str;
1471 while(1) {
1472 for (c = sep; *c != '\0'; c++)
1473 if (*p == *c)
1474 break;
1475 if (*p == *c) {
1476 if ((!ignore_empty) || (p - token > 0)) {
1477 lua_pushlstring(L, token, p - token);
1478 lua_rawseti(L, -2, index);
1479 index++;
1480 }
1481 token = p + 1;
1482 }
1483 if (*p == '\0')
1484 break;
1485 p++;
1486 }
1487
1488 return 1;
1489}
1490
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001491int hlua_parse_addr(lua_State *L)
1492{
Christopher Faulet29e93262021-02-26 09:39:05 +01001493 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001494 const char *str = luaL_checkstring(L, 1);
1495 unsigned char mask;
1496
Christopher Faulet29e93262021-02-26 09:39:05 +01001497 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001498 if (!addr) {
1499 lua_pushnil(L);
1500 return 1;
1501 }
1502
1503 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001504 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001505 return 1;
1506 }
1507
1508 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1509 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001510 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001511 return 1;
1512 }
1513
1514 lua_pop(L, 1);
1515 lua_pushnil(L);
1516 return 1;
1517}
1518
1519int hlua_match_addr(lua_State *L)
1520{
Christopher Faulet29e93262021-02-26 09:39:05 +01001521 struct net_addr *addr1;
1522 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001523
1524 if (!lua_isuserdata(L, 1) ||
1525 !lua_isuserdata(L, 2)) {
1526 lua_pushboolean(L, 0);
1527 return 1;
1528 }
1529
1530 addr1 = lua_touserdata(L, 1);
1531 addr2 = lua_touserdata(L, 2);
1532
Christopher Faulet29e93262021-02-26 09:39:05 +01001533 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001534 lua_pushboolean(L, 0);
1535 return 1;
1536 }
1537
Christopher Faulet29e93262021-02-26 09:39:05 +01001538 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001539 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1540 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1541 lua_pushboolean(L, 1);
1542 return 1;
1543 }
1544 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001545 int i;
1546
1547 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001548 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1549 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1550 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1551 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001552 break;
1553 }
1554 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001555 lua_pushboolean(L, 1);
1556 return 1;
1557 }
1558 }
1559
1560 lua_pushboolean(L, 0);
1561 return 1;
1562}
1563
Dragan Dosen26743032019-04-30 15:54:36 +02001564static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001565{
1566 return (hlua_checkudata(L, ud, class_regex_ref));
1567}
1568
1569static int hlua_regex_comp(struct lua_State *L)
1570{
Dragan Dosen26743032019-04-30 15:54:36 +02001571 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001572 const char *str;
1573 int cs;
1574 char *err;
1575
1576 str = luaL_checkstring(L, 1);
1577 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1578 cs = lua_toboolean(L, 2);
1579
1580 regex = lua_newuserdata(L, sizeof(*regex));
1581
1582 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001583 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001584 lua_pushboolean(L, 0); /* status error */
1585 lua_pushstring(L, err); /* Reason */
1586 free(err);
1587 return 2;
1588 }
1589
1590 lua_pushboolean(L, 1); /* Status ok */
1591
1592 /* Create object */
1593 lua_newtable(L);
1594 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1595 lua_rawseti(L, -2, 0);
1596 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1597 lua_setmetatable(L, -2);
1598 return 2;
1599}
1600
1601static int hlua_regex_exec(struct lua_State *L)
1602{
Dragan Dosen26743032019-04-30 15:54:36 +02001603 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001604 const char *str;
1605 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001606 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001607
1608 regex = hlua_check_regex(L, 1);
1609 str = luaL_checklstring(L, 2, &len);
1610
Dragan Dosen26743032019-04-30 15:54:36 +02001611 if (!*regex) {
1612 lua_pushboolean(L, 0);
1613 return 1;
1614 }
1615
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001616 /* Copy the string because regex_exec2 require a 'char *'
1617 * and not a 'const char *'.
1618 */
1619 tmp = get_trash_chunk();
1620 if (len >= tmp->size) {
1621 lua_pushboolean(L, 0);
1622 return 1;
1623 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001624 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001625
Dragan Dosen26743032019-04-30 15:54:36 +02001626 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001627
1628 return 1;
1629}
1630
1631static int hlua_regex_match(struct lua_State *L)
1632{
Dragan Dosen26743032019-04-30 15:54:36 +02001633 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001634 const char *str;
1635 size_t len;
1636 regmatch_t pmatch[20];
1637 int ret;
1638 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001639 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001640
1641 regex = hlua_check_regex(L, 1);
1642 str = luaL_checklstring(L, 2, &len);
1643
Dragan Dosen26743032019-04-30 15:54:36 +02001644 if (!*regex) {
1645 lua_pushboolean(L, 0);
1646 return 1;
1647 }
1648
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001649 /* Copy the string because regex_exec2 require a 'char *'
1650 * and not a 'const char *'.
1651 */
1652 tmp = get_trash_chunk();
1653 if (len >= tmp->size) {
1654 lua_pushboolean(L, 0);
1655 return 1;
1656 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001657 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001658
Dragan Dosen26743032019-04-30 15:54:36 +02001659 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001660 lua_pushboolean(L, ret);
1661 lua_newtable(L);
1662 if (ret) {
1663 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1664 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1665 lua_rawseti(L, -2, i + 1);
1666 }
1667 }
1668 return 2;
1669}
1670
1671static int hlua_regex_free(struct lua_State *L)
1672{
Dragan Dosen26743032019-04-30 15:54:36 +02001673 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001674
1675 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001676 regex_free(*regex);
1677 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001678 return 0;
1679}
1680
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001681int hlua_fcn_reg_core_fcn(lua_State *L)
1682{
Thierry Fournier1de16592016-01-27 09:49:07 +01001683 if (!hlua_concat_init(L))
1684 return 0;
1685
Thierry Fournier4f99b272016-02-22 08:40:02 +01001686 hlua_class_function(L, "now", hlua_now);
1687 hlua_class_function(L, "http_date", hlua_http_date);
1688 hlua_class_function(L, "imf_date", hlua_imf_date);
1689 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1690 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1691 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001692 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001693 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1694 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001695 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001696
Thierry FOURNIER31904272017-10-25 12:59:51 +02001697 /* Create regex object. */
1698 lua_newtable(L);
1699 hlua_class_function(L, "new", hlua_regex_comp);
1700
1701 lua_newtable(L); /* The metatable. */
1702 lua_pushstring(L, "__index");
1703 lua_newtable(L);
1704 hlua_class_function(L, "exec", hlua_regex_exec);
1705 hlua_class_function(L, "match", hlua_regex_match);
1706 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1707 hlua_class_function(L, "__gc", hlua_regex_free);
1708
1709 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1710 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1711
1712 lua_setmetatable(L, -2);
1713 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1714
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001715 /* Create stktable object. */
1716 lua_newtable(L);
1717 lua_pushstring(L, "__index");
1718 lua_newtable(L);
1719 hlua_class_function(L, "info", hlua_stktable_info);
1720 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1721 hlua_class_function(L, "dump", hlua_stktable_dump);
1722 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1723 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1724
Thierry Fournierff480422016-02-25 08:36:46 +01001725 /* Create listener object. */
1726 lua_newtable(L);
1727 lua_pushstring(L, "__index");
1728 lua_newtable(L);
1729 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1730 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1731 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1732
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001733 /* Create server object. */
1734 lua_newtable(L);
1735 lua_pushstring(L, "__index");
1736 lua_newtable(L);
1737 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001738 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1739 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001740 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1741 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1742 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1743 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1744 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1745 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1746 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1747 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1748 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1749 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1750 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1751 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1752 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1753 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1754 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1755 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1756 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1757 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1758 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1759 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1760
Thierry Fournierf61aa632016-02-19 20:56:00 +01001761 /* Create proxy object. */
1762 lua_newtable(L);
1763 lua_pushstring(L, "__index");
1764 lua_newtable(L);
1765 hlua_class_function(L, "pause", hlua_proxy_pause);
1766 hlua_class_function(L, "resume", hlua_proxy_resume);
1767 hlua_class_function(L, "stop", hlua_proxy_stop);
1768 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1769 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1770 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1771 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1772 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1773 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1774
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001775 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001776}