blob: 56923f0313a3e1bbf36010586dea5d17d8862a84 [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 Tarreau83487a82020-06-04 20:19:54 +020024#include <haproxy/cli-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020025#include <haproxy/errors.h>
Willy Tarreau86416052020-06-04 09:20:54 +020026#include <haproxy/hlua-t.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020028#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020029#include <haproxy/pattern-t.h>
30#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020034#include <haproxy/stick_table.h>
Willy Tarreau27539402021-10-06 09:12:44 +020035#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010037#include <haproxy/tools.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010038
Thierry Fournier1de16592016-01-27 09:49:07 +010039/* Contains the class reference of the concat object. */
40static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010041static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010042static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010043static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020044static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020045static int class_stktable_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010046
Thierry Fournierf61aa632016-02-19 20:56:00 +010047#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010048
Thierry FOURNIERffbad792017-07-12 11:39:04 +020049static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010050
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010051int hlua_checkboolean(lua_State *L, int index)
52{
53 if (!lua_isboolean(L, index))
54 luaL_argerror(L, index, "boolean expected");
55 return lua_toboolean(L, index);
56}
57
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020058/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
59static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
60{
61#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
62 lua_pushinteger(L, val);
63#else
64 if (val > INT_MAX)
65 lua_pushnumber(L, (lua_Number)val);
66 else
67 lua_pushinteger(L, (int)val);
68#endif
69 return 1;
70}
71
72/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
73static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
74#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
75 /* 64 bits case, U64 is supported until LLONG_MAX */
76 if (val > LLONG_MAX)
77 lua_pushnumber(L, (lua_Number)val);
78 else
79 lua_pushinteger(L, val);
80#else
81 /* 32 bits case, U64 is supported until INT_MAX */
82 if (val > INT_MAX)
83 lua_pushnumber(L, (lua_Number)val);
84 else
85 lua_pushinteger(L, (int)val);
86#endif
87 return 1;
88}
89
Joseph Herlantb3d92e32018-11-15 09:35:04 -080090/* This function gets a struct field and converts it in Lua
91 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010092 */
93int hlua_fcn_pushfield(lua_State *L, struct field *field)
94{
95 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -080096 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010097 * by some macros. Windows maros are not supported.
98 * If the number cannot be represented as integer, we try to
99 * convert to float.
100 */
101 switch (field_format(field, 0)) {
102
103 case FF_EMPTY:
104 lua_pushnil(L);
105 return 1;
106
107 case FF_S32:
108 /* S32 is always supported. */
109 lua_pushinteger(L, field->u.s32);
110 return 1;
111
112 case FF_U32:
113#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
114 /* 64 bits case, U32 is always supported */
115 lua_pushinteger(L, field->u.u32);
116#else
117 /* 32 bits case, U32 is supported until INT_MAX. */
118 if (field->u.u32 > INT_MAX)
119 lua_pushnumber(L, (lua_Number)field->u.u32);
120 else
121 lua_pushinteger(L, field->u.u32);
122#endif
123 return 1;
124
125 case FF_S64:
126#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
127 /* 64 bits case, S64 is always supported */
128 lua_pushinteger(L, field->u.s64);
129#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500130 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100131 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
132 lua_pushnumber(L, (lua_Number)field->u.s64);
133 else
134 lua_pushinteger(L, (int)field->u.s64);
135#endif
136 return 1;
137
138 case FF_U64:
139#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
140 /* 64 bits case, U64 is supported until LLONG_MAX */
141 if (field->u.u64 > LLONG_MAX)
142 lua_pushnumber(L, (lua_Number)field->u.u64);
143 else
144 lua_pushinteger(L, field->u.u64);
145#else
146 /* 64 bits case, U64 is supported until INT_MAX */
147 if (field->u.u64 > INT_MAX)
148 lua_pushnumber(L, (lua_Number)field->u.u64);
149 else
150 lua_pushinteger(L, (int)field->u.u64);
151#endif
152 return 1;
153
154 case FF_STR:
155 lua_pushstring(L, field->u.str);
156 return 1;
157
158 default:
159 break;
160 }
161
162 /* Default case, never reached. */
163 lua_pushnil(L);
164 return 1;
165}
166
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100167/* Some string are started or terminated by blank chars,
168 * this function removes the spaces, tabs, \r and
169 * \n at the begin and at the end of the string "str", and
170 * push the result in the lua stack.
171 * Returns a pointer to the Lua internal copy of the string.
172 */
173const char *hlua_pushstrippedstring(lua_State *L, const char *str)
174{
175 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100176 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100177
178 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100179
180 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100181
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100182 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100183}
184
Thierry Fournierddd89882016-02-22 19:52:08 +0100185/* The three following functions are useful for adding entries
186 * in a table. These functions takes a string and respectively an
187 * integer, a string or a function and add it to the table in the
188 * top of the stack.
189 *
190 * These functions throws an error if no more stack size is
191 * available.
192 */
193void hlua_class_const_int(lua_State *L, const char *name, int value)
194{
Thierry Fournierddd89882016-02-22 19:52:08 +0100195 lua_pushstring(L, name);
196 lua_pushinteger(L, value);
197 lua_rawset(L, -3);
198}
199void hlua_class_const_str(lua_State *L, const char *name, const char *value)
200{
Thierry Fournierddd89882016-02-22 19:52:08 +0100201 lua_pushstring(L, name);
202 lua_pushstring(L, value);
203 lua_rawset(L, -3);
204}
205void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
206{
Thierry Fournierddd89882016-02-22 19:52:08 +0100207 lua_pushstring(L, name);
208 lua_pushcclosure(L, function, 0);
209 lua_rawset(L, -3);
210}
211
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800212/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100213int hlua_dump_object(struct lua_State *L)
214{
215 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
216 lua_pushfstring(L, "HAProxy class %s", name);
217 return 1;
218}
219
Thierry Fournier45e78d72016-02-19 18:34:46 +0100220/* This function register a table as metatable and. It names
221 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500222 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100223 * "name" is the referenced class name.
224 */
225int hlua_register_metatable(struct lua_State *L, char *name)
226{
227 /* Check the type of the top element. it must be
228 * a table.
229 */
230 if (lua_type(L, -1) != LUA_TTABLE)
231 luaL_error(L, "hlua_register_metatable() requires a type Table "
232 "in the top of the stack");
233
234 /* Add the __tostring function which identify the
235 * created object.
236 */
237 lua_pushstring(L, "__tostring");
238 lua_pushstring(L, name);
239 lua_pushcclosure(L, hlua_dump_object, 1);
240 lua_rawset(L, -3);
241
242 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500243 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100244 * lua_setfield() pop the entry.
245 */
246 lua_pushvalue(L, -1);
247 lua_setfield(L, LUA_REGISTRYINDEX, name);
248
249 /* Creates the reference of the object. The
250 * function luaL_ref pop the top of the stack.
251 */
252 return luaL_ref(L, LUA_REGISTRYINDEX);
253}
254
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100255/* Return an object of the expected type, or throws an error. */
256void *hlua_checkudata(lua_State *L, int ud, int class_ref)
257{
258 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100259 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100260
261 /* Check if the stack entry is an array. */
262 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100263 luaL_argerror(L, ud, NULL);
264
265 /* pop the metatable of the referencecd object. */
266 if (!lua_getmetatable(L, ud))
267 luaL_argerror(L, ud, NULL);
268
269 /* pop the expected metatable. */
270 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
271
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100272 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100273 ret = lua_rawequal(L, -1, -2);
274 lua_pop(L, 2);
275 if (!ret)
276 luaL_argerror(L, ud, NULL);
277
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100278 /* Push on the stack at the entry [0] of the table. */
279 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100280
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100281 /* Check if this entry is userdata. */
282 p = lua_touserdata(L, -1);
283 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100284 luaL_argerror(L, ud, NULL);
285
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100286 /* Remove the entry returned by lua_rawgeti(). */
287 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100288
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100289 /* Return the associated struct. */
290 return p;
291}
292
Thierry Fournierb1f46562016-01-21 09:46:15 +0100293/* This function return the current date at epoch format in milliseconds. */
294int hlua_now(lua_State *L)
295{
296 lua_newtable(L);
297 lua_pushstring(L, "sec");
298 lua_pushinteger(L, now.tv_sec);
299 lua_rawset(L, -3);
300 lua_pushstring(L, "usec");
301 lua_pushinteger(L, now.tv_usec);
302 lua_rawset(L, -3);
303 return 1;
304}
305
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100306/* This functions expects a Lua string as HTTP date, parse it and
307 * returns an integer containing the epoch format of the date, or
308 * nil if the parsing fails.
309 */
310static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
311{
312 const char *str;
313 size_t len;
314 struct tm tm;
315 time_t time;
316
317 str = luaL_checklstring(L, 1, &len);
318
319 if (!fcn(str, len, &tm)) {
320 lua_pushnil(L);
321 return 1;
322 }
323
324 /* This function considers the content of the broken-down time
325 * is exprimed in the UTC timezone. timegm don't care about
326 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
327 * the timezone from the broken-down time, it must be fixed
328 * after the conversion.
329 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200330 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100331 if (time == -1) {
332 lua_pushnil(L);
333 return 1;
334 }
335
336 lua_pushinteger(L, (int)time);
337 return 1;
338}
339static int hlua_http_date(lua_State *L)
340{
341 return hlua_parse_date(L, parse_http_date);
342}
343static int hlua_imf_date(lua_State *L)
344{
345 return hlua_parse_date(L, parse_imf_date);
346}
347static int hlua_rfc850_date(lua_State *L)
348{
349 return hlua_parse_date(L, parse_rfc850_date);
350}
351static int hlua_asctime_date(lua_State *L)
352{
353 return hlua_parse_date(L, parse_asctime_date);
354}
355
Thierry Fourniereea77c02016-03-18 08:47:13 +0100356static int hlua_get_info(lua_State *L)
357{
358 int i;
359
Willy Tarreau0b26b382021-05-08 07:43:53 +0200360 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100361
362 lua_newtable(L);
363 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200364 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100365 hlua_fcn_pushfield(L, &stats[i]);
366 lua_settable(L, -3);
367 }
368 return 1;
369}
370
Thierry Fournier49d48422016-02-19 12:09:29 +0100371static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100372{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200373 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100374}
375
376static int hlua_concat_add(lua_State *L)
377{
Thierry Fournier49d48422016-02-19 12:09:29 +0100378 struct hlua_concat *b;
379 char *buffer;
380 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100381 const char *str;
382 size_t l;
383
384 /* First arg must be a concat object. */
385 b = hlua_check_concat(L, 1);
386
387 /* Second arg must be a string. */
388 str = luaL_checklstring(L, 2, &l);
389
Thierry Fournier49d48422016-02-19 12:09:29 +0100390 /* Get the buffer. */
391 lua_rawgeti(L, 1, 1);
392 buffer = lua_touserdata(L, -1);
393 lua_pop(L, 1);
394
395 /* Update the buffer size if it s required. The old buffer
396 * is crushed by the new in the object array, so it will
397 * be deleted by the GC.
398 * Note that in the first loop, the "new" variable is only
399 * used as a flag.
400 */
401 new = NULL;
402 while (b->size - b->len < l) {
403 b->size += HLUA_CONCAT_BLOCSZ;
404 new = buffer;
405 }
406 if (new) {
407 new = lua_newuserdata(L, b->size);
408 memcpy(new, buffer, b->len);
409 lua_rawseti(L, 1, 1);
410 buffer = new;
411 }
412
413 /* Copy string, and update metadata. */
414 memcpy(buffer + b->len, str, l);
415 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100416 return 0;
417}
418
419static int hlua_concat_dump(lua_State *L)
420{
Thierry Fournier49d48422016-02-19 12:09:29 +0100421 struct hlua_concat *b;
422 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100423
424 /* First arg must be a concat object. */
425 b = hlua_check_concat(L, 1);
426
Thierry Fournier49d48422016-02-19 12:09:29 +0100427 /* Get the buffer. */
428 lua_rawgeti(L, 1, 1);
429 buffer = lua_touserdata(L, -1);
430 lua_pop(L, 1);
431
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500432 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100433 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100434 return 1;
435}
436
437int hlua_concat_new(lua_State *L)
438{
Thierry Fournier49d48422016-02-19 12:09:29 +0100439 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100440
441 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200442 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100443 b->size = HLUA_CONCAT_BLOCSZ;
444 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100445 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100446 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
447 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100448
449 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
450 lua_setmetatable(L, -2);
451
Thierry Fournier1de16592016-01-27 09:49:07 +0100452 return 1;
453}
454
455static int concat_tostring(lua_State *L)
456{
457 const void *ptr = lua_topointer(L, 1);
458 lua_pushfstring(L, "Concat object: %p", ptr);
459 return 1;
460}
461
462static int hlua_concat_init(lua_State *L)
463{
464 /* Creates the buffered concat object. */
465 lua_newtable(L);
466
467 lua_pushstring(L, "__tostring");
468 lua_pushcclosure(L, concat_tostring, 0);
469 lua_settable(L, -3);
470
471 lua_pushstring(L, "__index"); /* Creates the index entry. */
472 lua_newtable(L); /* The "__index" content. */
473
474 lua_pushstring(L, "add");
475 lua_pushcclosure(L, hlua_concat_add, 0);
476 lua_settable(L, -3);
477
478 lua_pushstring(L, "dump");
479 lua_pushcclosure(L, hlua_concat_dump, 0);
480 lua_settable(L, -3);
481
482 lua_settable(L, -3); /* Sets the __index entry. */
483 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
484
485 return 1;
486}
487
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200488int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
489{
490 lua_newtable(L);
491
492 /* Pop a class stktbl metatable and affect it to the userdata. */
493 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
494 lua_setmetatable(L, -2);
495
496 lua_pushlightuserdata(L, tbl);
497 lua_rawseti(L, -2, 0);
498 return 1;
499}
500
501static struct stktable *hlua_check_stktable(lua_State *L, int ud)
502{
503 return hlua_checkudata(L, ud, class_stktable_ref);
504}
505
506/* Extract stick table attributes into Lua table */
507int hlua_stktable_info(lua_State *L)
508{
509 struct stktable *tbl;
510 int dt;
511
512 tbl = hlua_check_stktable(L, 1);
513
514 if (!tbl->id) {
515 lua_pushnil(L);
516 return 1;
517 }
518
519 lua_newtable(L);
520
521 lua_pushstring(L, "type");
522 lua_pushstring(L, stktable_types[tbl->type].kw);
523 lua_settable(L, -3);
524
525 lua_pushstring(L, "length");
526 lua_pushinteger(L, tbl->key_size);
527 lua_settable(L, -3);
528
529 lua_pushstring(L, "size");
530 hlua_fcn_pushunsigned(L, tbl->size);
531 lua_settable(L, -3);
532
533 lua_pushstring(L, "used");
534 hlua_fcn_pushunsigned(L, tbl->current);
535 lua_settable(L, -3);
536
537 lua_pushstring(L, "nopurge");
538 lua_pushboolean(L, tbl->nopurge > 0);
539 lua_settable(L, -3);
540
541 lua_pushstring(L, "expire");
542 lua_pushinteger(L, tbl->expire);
543 lua_settable(L, -3);
544
545 /* Save data types periods (if applicable) in 'data' table */
546 lua_pushstring(L, "data");
547 lua_newtable(L);
548
549 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
550 if (tbl->data_ofs[dt] == 0)
551 continue;
552
553 lua_pushstring(L, stktable_data_types[dt].name);
554
555 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
556 lua_pushinteger(L, tbl->data_arg[dt].u);
557 else
558 lua_pushinteger(L, -1);
559
560 lua_settable(L, -3);
561 }
562
563 lua_settable(L, -3);
564
565 return 1;
566}
567
568/* Helper to get extract stick table entry into Lua table */
569static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
570{
571 int dt;
572 void *ptr;
573
574 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
575
576 if (t->data_ofs[dt] == 0)
577 continue;
578
579 lua_pushstring(L, stktable_data_types[dt].name);
580
581 ptr = stktable_data_ptr(t, ts, dt);
582 switch (stktable_data_types[dt].std_type) {
583 case STD_T_SINT:
584 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
585 break;
586 case STD_T_UINT:
587 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
588 break;
589 case STD_T_ULL:
590 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
591 break;
592 case STD_T_FRQP:
593 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
594 t->data_arg[dt].u));
595 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200596 case STD_T_DICT: {
597 struct dict_entry *de;
598 de = stktable_data_cast(ptr, std_t_dict);
599 lua_pushstring(L, de ? (char *)de->value.key : "-");
600 break;
601 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200602 }
603
604 lua_settable(L, -3);
605 }
606}
607
608/* Looks in table <t> for a sticky session matching key <key>
609 * Returns table with session data or nil
610 *
611 * The returned table always contains 'use' and 'expire' (integer) fields.
612 * For frequency/rate counters, each data entry is returned as table with
613 * 'value' and 'period' fields.
614 */
615int hlua_stktable_lookup(lua_State *L)
616{
617 struct stktable *t;
618 struct sample smp;
619 struct stktable_key *skey;
620 struct stksess *ts;
621
622 t = hlua_check_stktable(L, 1);
623 smp.data.type = SMP_T_STR;
624 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600625 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200626
627 skey = smp_to_stkey(&smp, t);
628 if (!skey) {
629 lua_pushnil(L);
630 return 1;
631 }
632
633 ts = stktable_lookup_key(t, skey);
634 if (!ts) {
635 lua_pushnil(L);
636 return 1;
637 }
638
639 lua_newtable(L);
640 lua_pushstring(L, "use");
641 lua_pushinteger(L, ts->ref_cnt - 1);
642 lua_settable(L, -3);
643
644 lua_pushstring(L, "expire");
645 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
646 lua_settable(L, -3);
647
648 hlua_stktable_entry(L, t, ts);
649 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
650 ts->ref_cnt--;
651 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
652
653 return 1;
654}
655
656struct stk_filter {
657 long long val;
658 int type;
659 int op;
660};
661
662
663/* Helper for returning errors to callers using Lua convention (nil, err) */
664static int hlua_error(lua_State *L, const char *fmt, ...) {
665 char buf[256];
666 int len;
667 va_list args;
668 va_start(args, fmt);
669 len = vsnprintf(buf, sizeof(buf), fmt, args);
670 va_end(args);
671
672 if (len < 0) {
673 ha_alert("hlua_error(): Could not write error message.\n");
674 lua_pushnil(L);
675 return 1;
676 } else if (len >= sizeof(buf))
677 ha_alert("hlua_error(): Error message was truncated.\n");
678
679 lua_pushnil(L);
680 lua_pushstring(L, buf);
681
682 return 2;
683}
684
685/* Dump the contents of stick table <t>*/
686int hlua_stktable_dump(lua_State *L)
687{
688 struct stktable *t;
689 struct ebmb_node *eb;
690 struct ebmb_node *n;
691 struct stksess *ts;
692 int type;
693 int op;
694 int dt;
695 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100696 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200697 int filter_count = 0;
698 int i;
699 int skip_entry;
700 void *ptr;
701
702 t = hlua_check_stktable(L, 1);
703 type = lua_type(L, 2);
704
705 switch (type) {
706 case LUA_TNONE:
707 case LUA_TNIL:
708 break;
709 case LUA_TTABLE:
710 lua_pushnil(L);
711 while (lua_next(L, 2) != 0) {
712 int entry_idx = 0;
713
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100714 if (filter_count >= STKTABLE_FILTER_LEN)
715 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200716
717 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
718 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
719
720 lua_pushnil(L);
721 while (lua_next(L, -2) != 0) {
722 switch (entry_idx) {
723 case 0:
724 if (lua_type(L, -1) != LUA_TSTRING)
725 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
726
727 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
728 if (dt < 0 || t->data_ofs[dt] == 0)
729 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
730 filter[filter_count].type = dt;
731 break;
732 case 1:
733 if (lua_type(L, -1) != LUA_TSTRING)
734 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
735
736 op = get_std_op(lua_tostring(L, -1));
737 if (op < 0)
738 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
739 filter[filter_count].op = op;
740 break;
741 case 2:
742 val = lua_tointeger(L, -1);
743 filter[filter_count].val = val;
744 filter_count++;
745 break;
746 default:
747 break;
748 }
749 entry_idx++;
750 lua_pop(L, 1);
751 }
752 lua_pop(L, 1);
753 }
754 break;
755 default:
756 return hlua_error(L, "filter table expected");
757 }
758
759 lua_newtable(L);
760
761 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
762 eb = ebmb_first(&t->keys);
763 for (n = eb; n; n = ebmb_next(n)) {
764 ts = ebmb_entry(n, struct stksess, key);
765 if (!ts) {
766 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
767 return 1;
768 }
769 ts->ref_cnt++;
770 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
771
772 /* multi condition/value filter */
773 skip_entry = 0;
774 for (i = 0; i < filter_count; i++) {
775 if (t->data_ofs[filter[i].type] == 0)
776 continue;
777
778 ptr = stktable_data_ptr(t, ts, filter[i].type);
779
780 switch (stktable_data_types[filter[i].type].std_type) {
781 case STD_T_SINT:
782 val = stktable_data_cast(ptr, std_t_sint);
783 break;
784 case STD_T_UINT:
785 val = stktable_data_cast(ptr, std_t_uint);
786 break;
787 case STD_T_ULL:
788 val = stktable_data_cast(ptr, std_t_ull);
789 break;
790 case STD_T_FRQP:
791 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
792 t->data_arg[filter[i].type].u);
793 break;
794 default:
795 continue;
796 break;
797 }
798
799 op = filter[i].op;
800
801 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
802 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
803 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
804 skip_entry = 1;
805 break;
806 }
807 }
808
809 if (skip_entry) {
810 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
811 ts->ref_cnt--;
812 continue;
813 }
814
815 if (t->type == SMP_T_IPV4) {
816 char addr[INET_ADDRSTRLEN];
817 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
818 lua_pushstring(L, addr);
819 } else if (t->type == SMP_T_IPV6) {
820 char addr[INET6_ADDRSTRLEN];
821 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
822 lua_pushstring(L, addr);
823 } else if (t->type == SMP_T_SINT) {
824 lua_pushinteger(L, *ts->key.key);
825 } else if (t->type == SMP_T_STR) {
826 lua_pushstring(L, (const char *)ts->key.key);
827 } else {
828 return hlua_error(L, "Unsupported stick table key type");
829 }
830
831 lua_newtable(L);
832 hlua_stktable_entry(L, t, ts);
833 lua_settable(L, -3);
834 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
835 ts->ref_cnt--;
836 }
837 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
838
839 return 1;
840}
841
Thierry Fournierff480422016-02-25 08:36:46 +0100842int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
843{
844 lua_newtable(L);
845
846 /* Pop a class sesison metatable and affect it to the userdata. */
847 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
848 lua_setmetatable(L, -2);
849
850 lua_pushlightuserdata(L, lst);
851 lua_rawseti(L, -2, 0);
852 return 1;
853}
854
855static struct listener *hlua_check_listener(lua_State *L, int ud)
856{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200857 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100858}
859
860int hlua_listener_get_stats(lua_State *L)
861{
862 struct listener *li;
863 int i;
864
865 li = hlua_check_listener(L, 1);
866
Willy Tarreauc95bad52016-12-22 00:13:31 +0100867 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100868 lua_pushnil(L);
869 return 1;
870 }
871
William Dauchy655e14e2021-02-14 23:22:54 +0100872 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
873 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +0100874
875 lua_newtable(L);
876 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200877 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100878 hlua_fcn_pushfield(L, &stats[i]);
879 lua_settable(L, -3);
880 }
881 return 1;
882
883}
884
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100885int hlua_fcn_new_server(lua_State *L, struct server *srv)
886{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400887 char buffer[12];
888
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100889 lua_newtable(L);
890
891 /* Pop a class sesison metatable and affect it to the userdata. */
892 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
893 lua_setmetatable(L, -2);
894
895 lua_pushlightuserdata(L, srv);
896 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400897
898 /* Add server name. */
899 lua_pushstring(L, "name");
900 lua_pushstring(L, srv->id);
901 lua_settable(L, -3);
902
903 /* Add server puid. */
904 lua_pushstring(L, "puid");
905 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
906 lua_pushstring(L, buffer);
907 lua_settable(L, -3);
908
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100909 return 1;
910}
911
912static struct server *hlua_check_server(lua_State *L, int ud)
913{
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200914 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
915 srv->flags |= SRV_F_NON_PURGEABLE;
916 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100917}
918
919int hlua_server_get_stats(lua_State *L)
920{
921 struct server *srv;
922 int i;
923
924 srv = hlua_check_server(L, 1);
925
926 if (!srv->proxy) {
927 lua_pushnil(L);
928 return 1;
929 }
930
William Dauchyd3a9a492021-01-25 17:29:03 +0100931 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
932 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100933
934 lua_newtable(L);
935 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200936 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100937 hlua_fcn_pushfield(L, &stats[i]);
938 lua_settable(L, -3);
939 }
940 return 1;
941
942}
943
944int hlua_server_get_addr(lua_State *L)
945{
946 struct server *srv;
947 char addr[INET6_ADDRSTRLEN];
948 luaL_Buffer b;
949
950 srv = hlua_check_server(L, 1);
951
952 luaL_buffinit(L, &b);
953
954 switch (srv->addr.ss_family) {
955 case AF_INET:
956 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
957 addr, INET_ADDRSTRLEN);
958 luaL_addstring(&b, addr);
959 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400960 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100961 luaL_addstring(&b, addr);
962 break;
963 case AF_INET6:
964 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400965 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100966 luaL_addstring(&b, addr);
967 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400968 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100969 luaL_addstring(&b, addr);
970 break;
971 case AF_UNIX:
972 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
973 break;
974 default:
975 luaL_addstring(&b, "<unknown>");
976 break;
977 }
978
979 luaL_pushresult(&b);
980 return 1;
981}
982
983int hlua_server_is_draining(lua_State *L)
984{
985 struct server *srv;
986
987 srv = hlua_check_server(L, 1);
988 lua_pushinteger(L, server_is_draining(srv));
989 return 1;
990}
991
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400992int hlua_server_set_maxconn(lua_State *L)
993{
994 struct server *srv;
995 const char *maxconn;
996 const char *err;
997
998 srv = hlua_check_server(L, 1);
999 maxconn = luaL_checkstring(L, 2);
1000
1001 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1002 err = server_parse_maxconn_change_request(srv, maxconn);
1003 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1004 if (!err)
1005 lua_pushnil(L);
1006 else
1007 hlua_pushstrippedstring(L, err);
1008 return 1;
1009}
1010
1011int hlua_server_get_maxconn(lua_State *L)
1012{
1013 struct server *srv;
1014
1015 srv = hlua_check_server(L, 1);
1016 lua_pushinteger(L, srv->maxconn);
1017 return 1;
1018}
1019
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001020int hlua_server_set_weight(lua_State *L)
1021{
1022 struct server *srv;
1023 const char *weight;
1024 const char *err;
1025
1026 srv = hlua_check_server(L, 1);
1027 weight = luaL_checkstring(L, 2);
1028
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001029 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001030 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001031 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001032 if (!err)
1033 lua_pushnil(L);
1034 else
1035 hlua_pushstrippedstring(L, err);
1036 return 1;
1037}
1038
1039int hlua_server_get_weight(lua_State *L)
1040{
1041 struct server *srv;
1042
1043 srv = hlua_check_server(L, 1);
1044 lua_pushinteger(L, srv->uweight);
1045 return 1;
1046}
1047
1048int hlua_server_set_addr(lua_State *L)
1049{
1050 struct server *srv;
1051 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001052 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001053 const char *err;
1054
1055 srv = hlua_check_server(L, 1);
1056 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001057 if (lua_gettop(L) >= 3)
1058 port = luaL_checkstring(L, 3);
1059 else
1060 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001061
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001062 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001063 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001064 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001065 if (!err)
1066 lua_pushnil(L);
1067 else
1068 hlua_pushstrippedstring(L, err);
1069 return 1;
1070}
1071
1072int hlua_server_shut_sess(lua_State *L)
1073{
1074 struct server *srv;
1075
1076 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001077 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001078 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001079 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001080 return 0;
1081}
1082
1083int hlua_server_set_drain(lua_State *L)
1084{
1085 struct server *srv;
1086
1087 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001088 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001089 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001090 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001091 return 0;
1092}
1093
1094int hlua_server_set_maint(lua_State *L)
1095{
1096 struct server *srv;
1097
1098 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001099 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001100 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001101 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001102 return 0;
1103}
1104
1105int hlua_server_set_ready(lua_State *L)
1106{
1107 struct server *srv;
1108
1109 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001110 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001111 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001112 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001113 return 0;
1114}
1115
1116int hlua_server_check_enable(lua_State *L)
1117{
1118 struct server *sv;
1119
1120 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001121 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001122 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001123 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001124 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001125 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001126 return 0;
1127}
1128
1129int hlua_server_check_disable(lua_State *L)
1130{
1131 struct server *sv;
1132
1133 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001134 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001135 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001136 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001137 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001138 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001139 return 0;
1140}
1141
1142int hlua_server_check_force_up(lua_State *L)
1143{
1144 struct server *sv;
1145
1146 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001147 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001148 if (!(sv->track)) {
1149 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001150 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001151 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001152 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001153 return 0;
1154}
1155
1156int hlua_server_check_force_nolb(lua_State *L)
1157{
1158 struct server *sv;
1159
1160 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001161 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001162 if (!(sv->track)) {
1163 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001164 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001165 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001166 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001167 return 0;
1168}
1169
1170int hlua_server_check_force_down(lua_State *L)
1171{
1172 struct server *sv;
1173
1174 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001175 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001176 if (!(sv->track)) {
1177 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001178 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001179 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001180 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001181 return 0;
1182}
1183
1184int hlua_server_agent_enable(lua_State *L)
1185{
1186 struct server *sv;
1187
1188 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001189 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001190 if (sv->agent.state & CHK_ST_CONFIGURED) {
1191 sv->agent.state |= CHK_ST_ENABLED;
1192 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001193 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001194 return 0;
1195}
1196
1197int hlua_server_agent_disable(lua_State *L)
1198{
1199 struct server *sv;
1200
1201 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001202 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001203 if (sv->agent.state & CHK_ST_CONFIGURED) {
1204 sv->agent.state &= ~CHK_ST_ENABLED;
1205 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001206 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001207 return 0;
1208}
1209
1210int hlua_server_agent_force_up(lua_State *L)
1211{
1212 struct server *sv;
1213
1214 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001215 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001216 if (sv->agent.state & CHK_ST_ENABLED) {
1217 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001218 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001219 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001220 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001221 return 0;
1222}
1223
1224int hlua_server_agent_force_down(lua_State *L)
1225{
1226 struct server *sv;
1227
1228 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001229 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001230 if (sv->agent.state & CHK_ST_ENABLED) {
1231 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001232 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001233 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001234 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001235 return 0;
1236}
1237
Thierry Fournierf61aa632016-02-19 20:56:00 +01001238int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1239{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001240 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001241 struct listener *lst;
1242 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001243 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001244
Thierry Fournierf61aa632016-02-19 20:56:00 +01001245 lua_newtable(L);
1246
1247 /* Pop a class sesison metatable and affect it to the userdata. */
1248 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1249 lua_setmetatable(L, -2);
1250
1251 lua_pushlightuserdata(L, px);
1252 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001253
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001254 /* Add proxy name. */
1255 lua_pushstring(L, "name");
1256 lua_pushstring(L, px->id);
1257 lua_settable(L, -3);
1258
Baptiste Assmann46c72552017-10-26 21:51:58 +02001259 /* Add proxy uuid. */
1260 lua_pushstring(L, "uuid");
1261 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1262 lua_pushstring(L, buffer);
1263 lua_settable(L, -3);
1264
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001265 /* Browse and register servers. */
1266 lua_pushstring(L, "servers");
1267 lua_newtable(L);
1268 for (srv = px->srv; srv; srv = srv->next) {
1269 lua_pushstring(L, srv->id);
1270 hlua_fcn_new_server(L, srv);
1271 lua_settable(L, -3);
1272 }
1273 lua_settable(L, -3);
1274
Thierry Fournierff480422016-02-25 08:36:46 +01001275 /* Browse and register listeners. */
1276 lua_pushstring(L, "listeners");
1277 lua_newtable(L);
1278 lid = 1;
1279 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1280 if (lst->name)
1281 lua_pushstring(L, lst->name);
1282 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001283 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001284 lid++;
1285 lua_pushstring(L, buffer);
1286 }
1287 hlua_fcn_new_listener(L, lst);
1288 lua_settable(L, -3);
1289 }
1290 lua_settable(L, -3);
1291
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001292 if (px->table && px->table->id) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001293 lua_pushstring(L, "stktable");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001294 hlua_fcn_new_stktable(L, px->table);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001295 lua_settable(L, -3);
1296 }
1297
Thierry Fournierf61aa632016-02-19 20:56:00 +01001298 return 1;
1299}
1300
1301static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1302{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001303 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001304}
1305
1306int hlua_proxy_pause(lua_State *L)
1307{
1308 struct proxy *px;
1309
1310 px = hlua_check_proxy(L, 1);
1311 pause_proxy(px);
1312 return 0;
1313}
1314
1315int hlua_proxy_resume(lua_State *L)
1316{
1317 struct proxy *px;
1318
1319 px = hlua_check_proxy(L, 1);
1320 resume_proxy(px);
1321 return 0;
1322}
1323
1324int hlua_proxy_stop(lua_State *L)
1325{
1326 struct proxy *px;
1327
1328 px = hlua_check_proxy(L, 1);
1329 stop_proxy(px);
1330 return 0;
1331}
1332
1333int hlua_proxy_get_cap(lua_State *L)
1334{
1335 struct proxy *px;
1336 const char *str;
1337
1338 px = hlua_check_proxy(L, 1);
1339 str = proxy_cap_str(px->cap);
1340 lua_pushstring(L, str);
1341 return 1;
1342}
1343
1344int hlua_proxy_get_stats(lua_State *L)
1345{
1346 struct proxy *px;
1347 int i;
1348
1349 px = hlua_check_proxy(L, 1);
1350 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001351 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001352 else
William Dauchy0ef54392021-01-17 18:27:45 +01001353 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001354 lua_newtable(L);
1355 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001356 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001357 hlua_fcn_pushfield(L, &stats[i]);
1358 lua_settable(L, -3);
1359 }
1360 return 1;
1361}
1362
1363int hlua_proxy_get_mode(lua_State *L)
1364{
1365 struct proxy *px;
1366 const char *str;
1367
1368 px = hlua_check_proxy(L, 1);
1369 str = proxy_mode_str(px->mode);
1370 lua_pushstring(L, str);
1371 return 1;
1372}
1373
1374int hlua_proxy_shut_bcksess(lua_State *L)
1375{
1376 struct proxy *px;
1377
1378 px = hlua_check_proxy(L, 1);
1379 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1380 return 0;
1381}
1382
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001383int hlua_fcn_post_init(lua_State *L)
1384{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001385 struct proxy *px;
1386
1387 /* get core array. */
1388 if (lua_getglobal(L, "core") != LUA_TTABLE)
1389 lua_error(L);
1390
1391 /* Create proxies entry. */
1392 lua_pushstring(L, "proxies");
1393 lua_newtable(L);
1394
1395 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001396 for (px = proxies_list; px; px = px->next) {
Thierry Fournierf61aa632016-02-19 20:56:00 +01001397 lua_pushstring(L, px->id);
1398 hlua_fcn_new_proxy(L, px);
1399 lua_settable(L, -3);
1400 }
1401
1402 /* push "proxies" in "core" */
1403 lua_settable(L, -3);
1404
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001405 /* Create proxies entry. */
1406 lua_pushstring(L, "frontends");
1407 lua_newtable(L);
1408
1409 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001410 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001411 if (!(px->cap & PR_CAP_FE))
1412 continue;
1413 lua_pushstring(L, px->id);
1414 hlua_fcn_new_proxy(L, px);
1415 lua_settable(L, -3);
1416 }
1417
1418 /* push "frontends" in "core" */
1419 lua_settable(L, -3);
1420
1421 /* Create proxies entry. */
1422 lua_pushstring(L, "backends");
1423 lua_newtable(L);
1424
1425 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001426 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001427 if (!(px->cap & PR_CAP_BE))
1428 continue;
1429 lua_pushstring(L, px->id);
1430 hlua_fcn_new_proxy(L, px);
1431 lua_settable(L, -3);
1432 }
1433
1434 /* push "backend" in "core" */
1435 lua_settable(L, -3);
1436
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001437 return 1;
1438}
1439
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001440/* This Lua function take a string, a list of separators.
1441 * It tokenize the input string using the list of separators
1442 * as separator.
1443 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001444 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001445 */
1446int hlua_tokenize(lua_State *L)
1447{
1448 const char *str;
1449 const char *sep;
1450 int index;
1451 const char *token;
1452 const char *p;
1453 const char *c;
1454 int ignore_empty;
1455
1456 ignore_empty = 0;
1457
1458 str = luaL_checkstring(L, 1);
1459 sep = luaL_checkstring(L, 2);
1460 if (lua_gettop(L) == 3)
1461 ignore_empty = hlua_checkboolean(L, 3);
1462
1463 lua_newtable(L);
1464 index = 1;
1465 token = str;
1466 p = str;
1467 while(1) {
1468 for (c = sep; *c != '\0'; c++)
1469 if (*p == *c)
1470 break;
1471 if (*p == *c) {
1472 if ((!ignore_empty) || (p - token > 0)) {
1473 lua_pushlstring(L, token, p - token);
1474 lua_rawseti(L, -2, index);
1475 index++;
1476 }
1477 token = p + 1;
1478 }
1479 if (*p == '\0')
1480 break;
1481 p++;
1482 }
1483
1484 return 1;
1485}
1486
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001487int hlua_parse_addr(lua_State *L)
1488{
Christopher Faulet29e93262021-02-26 09:39:05 +01001489 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001490 const char *str = luaL_checkstring(L, 1);
1491 unsigned char mask;
1492
Christopher Faulet29e93262021-02-26 09:39:05 +01001493 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001494 if (!addr) {
1495 lua_pushnil(L);
1496 return 1;
1497 }
1498
1499 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001500 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001501 return 1;
1502 }
1503
1504 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1505 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001506 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001507 return 1;
1508 }
1509
1510 lua_pop(L, 1);
1511 lua_pushnil(L);
1512 return 1;
1513}
1514
1515int hlua_match_addr(lua_State *L)
1516{
Christopher Faulet29e93262021-02-26 09:39:05 +01001517 struct net_addr *addr1;
1518 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001519
1520 if (!lua_isuserdata(L, 1) ||
1521 !lua_isuserdata(L, 2)) {
1522 lua_pushboolean(L, 0);
1523 return 1;
1524 }
1525
1526 addr1 = lua_touserdata(L, 1);
1527 addr2 = lua_touserdata(L, 2);
1528
Christopher Faulet29e93262021-02-26 09:39:05 +01001529 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001530 lua_pushboolean(L, 0);
1531 return 1;
1532 }
1533
Christopher Faulet29e93262021-02-26 09:39:05 +01001534 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001535 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1536 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1537 lua_pushboolean(L, 1);
1538 return 1;
1539 }
1540 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001541 int i;
1542
1543 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001544 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1545 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1546 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1547 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001548 break;
1549 }
1550 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001551 lua_pushboolean(L, 1);
1552 return 1;
1553 }
1554 }
1555
1556 lua_pushboolean(L, 0);
1557 return 1;
1558}
1559
Dragan Dosen26743032019-04-30 15:54:36 +02001560static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001561{
1562 return (hlua_checkudata(L, ud, class_regex_ref));
1563}
1564
1565static int hlua_regex_comp(struct lua_State *L)
1566{
Dragan Dosen26743032019-04-30 15:54:36 +02001567 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001568 const char *str;
1569 int cs;
1570 char *err;
1571
1572 str = luaL_checkstring(L, 1);
1573 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1574 cs = lua_toboolean(L, 2);
1575
1576 regex = lua_newuserdata(L, sizeof(*regex));
1577
1578 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001579 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001580 lua_pushboolean(L, 0); /* status error */
1581 lua_pushstring(L, err); /* Reason */
1582 free(err);
1583 return 2;
1584 }
1585
1586 lua_pushboolean(L, 1); /* Status ok */
1587
1588 /* Create object */
1589 lua_newtable(L);
1590 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1591 lua_rawseti(L, -2, 0);
1592 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1593 lua_setmetatable(L, -2);
1594 return 2;
1595}
1596
1597static int hlua_regex_exec(struct lua_State *L)
1598{
Dragan Dosen26743032019-04-30 15:54:36 +02001599 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001600 const char *str;
1601 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001602 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001603
1604 regex = hlua_check_regex(L, 1);
1605 str = luaL_checklstring(L, 2, &len);
1606
Dragan Dosen26743032019-04-30 15:54:36 +02001607 if (!*regex) {
1608 lua_pushboolean(L, 0);
1609 return 1;
1610 }
1611
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001612 /* Copy the string because regex_exec2 require a 'char *'
1613 * and not a 'const char *'.
1614 */
1615 tmp = get_trash_chunk();
1616 if (len >= tmp->size) {
1617 lua_pushboolean(L, 0);
1618 return 1;
1619 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001620 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001621
Dragan Dosen26743032019-04-30 15:54:36 +02001622 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001623
1624 return 1;
1625}
1626
1627static int hlua_regex_match(struct lua_State *L)
1628{
Dragan Dosen26743032019-04-30 15:54:36 +02001629 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001630 const char *str;
1631 size_t len;
1632 regmatch_t pmatch[20];
1633 int ret;
1634 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001635 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001636
1637 regex = hlua_check_regex(L, 1);
1638 str = luaL_checklstring(L, 2, &len);
1639
Dragan Dosen26743032019-04-30 15:54:36 +02001640 if (!*regex) {
1641 lua_pushboolean(L, 0);
1642 return 1;
1643 }
1644
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001645 /* Copy the string because regex_exec2 require a 'char *'
1646 * and not a 'const char *'.
1647 */
1648 tmp = get_trash_chunk();
1649 if (len >= tmp->size) {
1650 lua_pushboolean(L, 0);
1651 return 1;
1652 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001653 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001654
Dragan Dosen26743032019-04-30 15:54:36 +02001655 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001656 lua_pushboolean(L, ret);
1657 lua_newtable(L);
1658 if (ret) {
1659 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1660 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1661 lua_rawseti(L, -2, i + 1);
1662 }
1663 }
1664 return 2;
1665}
1666
1667static int hlua_regex_free(struct lua_State *L)
1668{
Dragan Dosen26743032019-04-30 15:54:36 +02001669 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001670
1671 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001672 regex_free(*regex);
1673 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001674 return 0;
1675}
1676
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001677int hlua_fcn_reg_core_fcn(lua_State *L)
1678{
Thierry Fournier1de16592016-01-27 09:49:07 +01001679 if (!hlua_concat_init(L))
1680 return 0;
1681
Thierry Fournier4f99b272016-02-22 08:40:02 +01001682 hlua_class_function(L, "now", hlua_now);
1683 hlua_class_function(L, "http_date", hlua_http_date);
1684 hlua_class_function(L, "imf_date", hlua_imf_date);
1685 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1686 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1687 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001688 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001689 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1690 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001691 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001692
Thierry FOURNIER31904272017-10-25 12:59:51 +02001693 /* Create regex object. */
1694 lua_newtable(L);
1695 hlua_class_function(L, "new", hlua_regex_comp);
1696
1697 lua_newtable(L); /* The metatable. */
1698 lua_pushstring(L, "__index");
1699 lua_newtable(L);
1700 hlua_class_function(L, "exec", hlua_regex_exec);
1701 hlua_class_function(L, "match", hlua_regex_match);
1702 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1703 hlua_class_function(L, "__gc", hlua_regex_free);
1704
1705 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1706 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1707
1708 lua_setmetatable(L, -2);
1709 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1710
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001711 /* Create stktable object. */
1712 lua_newtable(L);
1713 lua_pushstring(L, "__index");
1714 lua_newtable(L);
1715 hlua_class_function(L, "info", hlua_stktable_info);
1716 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1717 hlua_class_function(L, "dump", hlua_stktable_dump);
1718 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1719 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1720
Thierry Fournierff480422016-02-25 08:36:46 +01001721 /* Create listener object. */
1722 lua_newtable(L);
1723 lua_pushstring(L, "__index");
1724 lua_newtable(L);
1725 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1726 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1727 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1728
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001729 /* Create server object. */
1730 lua_newtable(L);
1731 lua_pushstring(L, "__index");
1732 lua_newtable(L);
1733 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001734 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1735 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001736 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1737 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1738 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1739 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1740 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1741 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1742 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1743 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1744 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1745 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1746 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1747 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1748 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1749 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1750 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1751 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1752 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1753 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1754 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1755 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1756
Thierry Fournierf61aa632016-02-19 20:56:00 +01001757 /* Create proxy object. */
1758 lua_newtable(L);
1759 lua_pushstring(L, "__index");
1760 lua_newtable(L);
1761 hlua_class_function(L, "pause", hlua_proxy_pause);
1762 hlua_class_function(L, "resume", hlua_proxy_resume);
1763 hlua_class_function(L, "stop", hlua_proxy_stop);
1764 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1765 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1766 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1767 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1768 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1769 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1770
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001771 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001772}