blob: 975ed246ea2c7654b9cd530534294b52a575b7e1 [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>
Tim Duesterhus6eded622022-05-14 22:17:25 +020029#include <haproxy/hlua_fcn.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020030#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/pattern-t.h>
33#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020037#include <haproxy/stick_table.h>
Willy Tarreau27539402021-10-06 09:12:44 +020038#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010040#include <haproxy/tools.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010041
Thierry Fournier1de16592016-01-27 09:49:07 +010042/* Contains the class reference of the concat object. */
43static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010044static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010045static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010046static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020047static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020048static int class_stktable_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010049
Thierry Fournierf61aa632016-02-19 20:56:00 +010050#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010051
Thierry FOURNIERffbad792017-07-12 11:39:04 +020052static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010053
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010054int hlua_checkboolean(lua_State *L, int index)
55{
56 if (!lua_isboolean(L, index))
57 luaL_argerror(L, index, "boolean expected");
58 return lua_toboolean(L, index);
59}
60
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020061/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
62static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
63{
64#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
65 lua_pushinteger(L, val);
66#else
67 if (val > INT_MAX)
68 lua_pushnumber(L, (lua_Number)val);
69 else
70 lua_pushinteger(L, (int)val);
71#endif
72 return 1;
73}
74
75/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
76static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
77#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
78 /* 64 bits case, U64 is supported until LLONG_MAX */
79 if (val > LLONG_MAX)
80 lua_pushnumber(L, (lua_Number)val);
81 else
82 lua_pushinteger(L, val);
83#else
84 /* 32 bits case, U64 is supported until INT_MAX */
85 if (val > INT_MAX)
86 lua_pushnumber(L, (lua_Number)val);
87 else
88 lua_pushinteger(L, (int)val);
89#endif
90 return 1;
91}
92
Joseph Herlantb3d92e32018-11-15 09:35:04 -080093/* This function gets a struct field and converts it in Lua
94 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010095 */
96int hlua_fcn_pushfield(lua_State *L, struct field *field)
97{
98 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -080099 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100100 * by some macros. Windows maros are not supported.
101 * If the number cannot be represented as integer, we try to
102 * convert to float.
103 */
104 switch (field_format(field, 0)) {
105
106 case FF_EMPTY:
107 lua_pushnil(L);
108 return 1;
109
110 case FF_S32:
111 /* S32 is always supported. */
112 lua_pushinteger(L, field->u.s32);
113 return 1;
114
115 case FF_U32:
116#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
117 /* 64 bits case, U32 is always supported */
118 lua_pushinteger(L, field->u.u32);
119#else
120 /* 32 bits case, U32 is supported until INT_MAX. */
121 if (field->u.u32 > INT_MAX)
122 lua_pushnumber(L, (lua_Number)field->u.u32);
123 else
124 lua_pushinteger(L, field->u.u32);
125#endif
126 return 1;
127
128 case FF_S64:
129#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
130 /* 64 bits case, S64 is always supported */
131 lua_pushinteger(L, field->u.s64);
132#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500133 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100134 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
135 lua_pushnumber(L, (lua_Number)field->u.s64);
136 else
137 lua_pushinteger(L, (int)field->u.s64);
138#endif
139 return 1;
140
141 case FF_U64:
142#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
143 /* 64 bits case, U64 is supported until LLONG_MAX */
144 if (field->u.u64 > LLONG_MAX)
145 lua_pushnumber(L, (lua_Number)field->u.u64);
146 else
147 lua_pushinteger(L, field->u.u64);
148#else
149 /* 64 bits case, U64 is supported until INT_MAX */
150 if (field->u.u64 > INT_MAX)
151 lua_pushnumber(L, (lua_Number)field->u.u64);
152 else
153 lua_pushinteger(L, (int)field->u.u64);
154#endif
155 return 1;
156
157 case FF_STR:
158 lua_pushstring(L, field->u.str);
159 return 1;
160
161 default:
162 break;
163 }
164
165 /* Default case, never reached. */
166 lua_pushnil(L);
167 return 1;
168}
169
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100170/* Some string are started or terminated by blank chars,
171 * this function removes the spaces, tabs, \r and
172 * \n at the begin and at the end of the string "str", and
173 * push the result in the lua stack.
174 * Returns a pointer to the Lua internal copy of the string.
175 */
176const char *hlua_pushstrippedstring(lua_State *L, const char *str)
177{
178 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100179 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100180
181 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100182
183 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100184
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100185 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100186}
187
Thierry Fournierddd89882016-02-22 19:52:08 +0100188/* The three following functions are useful for adding entries
189 * in a table. These functions takes a string and respectively an
190 * integer, a string or a function and add it to the table in the
191 * top of the stack.
192 *
193 * These functions throws an error if no more stack size is
194 * available.
195 */
196void hlua_class_const_int(lua_State *L, const char *name, int value)
197{
Thierry Fournierddd89882016-02-22 19:52:08 +0100198 lua_pushstring(L, name);
199 lua_pushinteger(L, value);
200 lua_rawset(L, -3);
201}
202void hlua_class_const_str(lua_State *L, const char *name, const char *value)
203{
Thierry Fournierddd89882016-02-22 19:52:08 +0100204 lua_pushstring(L, name);
205 lua_pushstring(L, value);
206 lua_rawset(L, -3);
207}
208void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
209{
Thierry Fournierddd89882016-02-22 19:52:08 +0100210 lua_pushstring(L, name);
211 lua_pushcclosure(L, function, 0);
212 lua_rawset(L, -3);
213}
214
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800215/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100216int hlua_dump_object(struct lua_State *L)
217{
218 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
219 lua_pushfstring(L, "HAProxy class %s", name);
220 return 1;
221}
222
Thierry Fournier45e78d72016-02-19 18:34:46 +0100223/* This function register a table as metatable and. It names
224 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500225 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100226 * "name" is the referenced class name.
227 */
228int hlua_register_metatable(struct lua_State *L, char *name)
229{
230 /* Check the type of the top element. it must be
231 * a table.
232 */
233 if (lua_type(L, -1) != LUA_TTABLE)
234 luaL_error(L, "hlua_register_metatable() requires a type Table "
235 "in the top of the stack");
236
237 /* Add the __tostring function which identify the
238 * created object.
239 */
240 lua_pushstring(L, "__tostring");
241 lua_pushstring(L, name);
242 lua_pushcclosure(L, hlua_dump_object, 1);
243 lua_rawset(L, -3);
244
245 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500246 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100247 * lua_setfield() pop the entry.
248 */
249 lua_pushvalue(L, -1);
250 lua_setfield(L, LUA_REGISTRYINDEX, name);
251
252 /* Creates the reference of the object. The
253 * function luaL_ref pop the top of the stack.
254 */
255 return luaL_ref(L, LUA_REGISTRYINDEX);
256}
257
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100258/* Return an object of the expected type, or throws an error. */
259void *hlua_checkudata(lua_State *L, int ud, int class_ref)
260{
261 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100262 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100263
264 /* Check if the stack entry is an array. */
265 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100266 luaL_argerror(L, ud, NULL);
267
268 /* pop the metatable of the referencecd object. */
269 if (!lua_getmetatable(L, ud))
270 luaL_argerror(L, ud, NULL);
271
272 /* pop the expected metatable. */
273 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
274
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100275 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100276 ret = lua_rawequal(L, -1, -2);
277 lua_pop(L, 2);
278 if (!ret)
279 luaL_argerror(L, ud, NULL);
280
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100281 /* Push on the stack at the entry [0] of the table. */
282 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100283
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100284 /* Check if this entry is userdata. */
285 p = lua_touserdata(L, -1);
286 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100287 luaL_argerror(L, ud, NULL);
288
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100289 /* Remove the entry returned by lua_rawgeti(). */
290 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100291
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100292 /* Return the associated struct. */
293 return p;
294}
295
Thierry Fournierb1f46562016-01-21 09:46:15 +0100296/* This function return the current date at epoch format in milliseconds. */
297int hlua_now(lua_State *L)
298{
299 lua_newtable(L);
300 lua_pushstring(L, "sec");
301 lua_pushinteger(L, now.tv_sec);
302 lua_rawset(L, -3);
303 lua_pushstring(L, "usec");
304 lua_pushinteger(L, now.tv_usec);
305 lua_rawset(L, -3);
306 return 1;
307}
308
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100309/* This functions expects a Lua string as HTTP date, parse it and
310 * returns an integer containing the epoch format of the date, or
311 * nil if the parsing fails.
312 */
313static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
314{
315 const char *str;
316 size_t len;
317 struct tm tm;
318 time_t time;
319
320 str = luaL_checklstring(L, 1, &len);
321
322 if (!fcn(str, len, &tm)) {
323 lua_pushnil(L);
324 return 1;
325 }
326
327 /* This function considers the content of the broken-down time
328 * is exprimed in the UTC timezone. timegm don't care about
329 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
330 * the timezone from the broken-down time, it must be fixed
331 * after the conversion.
332 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200333 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100334 if (time == -1) {
335 lua_pushnil(L);
336 return 1;
337 }
338
339 lua_pushinteger(L, (int)time);
340 return 1;
341}
342static int hlua_http_date(lua_State *L)
343{
344 return hlua_parse_date(L, parse_http_date);
345}
346static int hlua_imf_date(lua_State *L)
347{
348 return hlua_parse_date(L, parse_imf_date);
349}
350static int hlua_rfc850_date(lua_State *L)
351{
352 return hlua_parse_date(L, parse_rfc850_date);
353}
354static int hlua_asctime_date(lua_State *L)
355{
356 return hlua_parse_date(L, parse_asctime_date);
357}
358
Thierry Fourniereea77c02016-03-18 08:47:13 +0100359static int hlua_get_info(lua_State *L)
360{
361 int i;
362
Willy Tarreau0b26b382021-05-08 07:43:53 +0200363 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100364
365 lua_newtable(L);
366 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200367 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100368 hlua_fcn_pushfield(L, &stats[i]);
369 lua_settable(L, -3);
370 }
371 return 1;
372}
373
Thierry Fournier49d48422016-02-19 12:09:29 +0100374static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100375{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200376 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100377}
378
379static int hlua_concat_add(lua_State *L)
380{
Thierry Fournier49d48422016-02-19 12:09:29 +0100381 struct hlua_concat *b;
382 char *buffer;
383 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100384 const char *str;
385 size_t l;
386
387 /* First arg must be a concat object. */
388 b = hlua_check_concat(L, 1);
389
390 /* Second arg must be a string. */
391 str = luaL_checklstring(L, 2, &l);
392
Thierry Fournier49d48422016-02-19 12:09:29 +0100393 /* Get the buffer. */
394 lua_rawgeti(L, 1, 1);
395 buffer = lua_touserdata(L, -1);
396 lua_pop(L, 1);
397
398 /* Update the buffer size if it s required. The old buffer
399 * is crushed by the new in the object array, so it will
400 * be deleted by the GC.
401 * Note that in the first loop, the "new" variable is only
402 * used as a flag.
403 */
404 new = NULL;
405 while (b->size - b->len < l) {
406 b->size += HLUA_CONCAT_BLOCSZ;
407 new = buffer;
408 }
409 if (new) {
410 new = lua_newuserdata(L, b->size);
411 memcpy(new, buffer, b->len);
412 lua_rawseti(L, 1, 1);
413 buffer = new;
414 }
415
416 /* Copy string, and update metadata. */
417 memcpy(buffer + b->len, str, l);
418 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100419 return 0;
420}
421
422static int hlua_concat_dump(lua_State *L)
423{
Thierry Fournier49d48422016-02-19 12:09:29 +0100424 struct hlua_concat *b;
425 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100426
427 /* First arg must be a concat object. */
428 b = hlua_check_concat(L, 1);
429
Thierry Fournier49d48422016-02-19 12:09:29 +0100430 /* Get the buffer. */
431 lua_rawgeti(L, 1, 1);
432 buffer = lua_touserdata(L, -1);
433 lua_pop(L, 1);
434
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500435 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100436 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100437 return 1;
438}
439
440int hlua_concat_new(lua_State *L)
441{
Thierry Fournier49d48422016-02-19 12:09:29 +0100442 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100443
444 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200445 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100446 b->size = HLUA_CONCAT_BLOCSZ;
447 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100448 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100449 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
450 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100451
452 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
453 lua_setmetatable(L, -2);
454
Thierry Fournier1de16592016-01-27 09:49:07 +0100455 return 1;
456}
457
458static int concat_tostring(lua_State *L)
459{
460 const void *ptr = lua_topointer(L, 1);
461 lua_pushfstring(L, "Concat object: %p", ptr);
462 return 1;
463}
464
465static int hlua_concat_init(lua_State *L)
466{
467 /* Creates the buffered concat object. */
468 lua_newtable(L);
469
470 lua_pushstring(L, "__tostring");
471 lua_pushcclosure(L, concat_tostring, 0);
472 lua_settable(L, -3);
473
474 lua_pushstring(L, "__index"); /* Creates the index entry. */
475 lua_newtable(L); /* The "__index" content. */
476
477 lua_pushstring(L, "add");
478 lua_pushcclosure(L, hlua_concat_add, 0);
479 lua_settable(L, -3);
480
481 lua_pushstring(L, "dump");
482 lua_pushcclosure(L, hlua_concat_dump, 0);
483 lua_settable(L, -3);
484
485 lua_settable(L, -3); /* Sets the __index entry. */
486 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
487
488 return 1;
489}
490
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200491int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
492{
493 lua_newtable(L);
494
495 /* Pop a class stktbl metatable and affect it to the userdata. */
496 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
497 lua_setmetatable(L, -2);
498
499 lua_pushlightuserdata(L, tbl);
500 lua_rawseti(L, -2, 0);
501 return 1;
502}
503
504static struct stktable *hlua_check_stktable(lua_State *L, int ud)
505{
506 return hlua_checkudata(L, ud, class_stktable_ref);
507}
508
509/* Extract stick table attributes into Lua table */
510int hlua_stktable_info(lua_State *L)
511{
512 struct stktable *tbl;
513 int dt;
514
515 tbl = hlua_check_stktable(L, 1);
516
517 if (!tbl->id) {
518 lua_pushnil(L);
519 return 1;
520 }
521
522 lua_newtable(L);
523
524 lua_pushstring(L, "type");
525 lua_pushstring(L, stktable_types[tbl->type].kw);
526 lua_settable(L, -3);
527
528 lua_pushstring(L, "length");
529 lua_pushinteger(L, tbl->key_size);
530 lua_settable(L, -3);
531
532 lua_pushstring(L, "size");
533 hlua_fcn_pushunsigned(L, tbl->size);
534 lua_settable(L, -3);
535
536 lua_pushstring(L, "used");
537 hlua_fcn_pushunsigned(L, tbl->current);
538 lua_settable(L, -3);
539
540 lua_pushstring(L, "nopurge");
541 lua_pushboolean(L, tbl->nopurge > 0);
542 lua_settable(L, -3);
543
544 lua_pushstring(L, "expire");
545 lua_pushinteger(L, tbl->expire);
546 lua_settable(L, -3);
547
548 /* Save data types periods (if applicable) in 'data' table */
549 lua_pushstring(L, "data");
550 lua_newtable(L);
551
552 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
553 if (tbl->data_ofs[dt] == 0)
554 continue;
555
556 lua_pushstring(L, stktable_data_types[dt].name);
557
558 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
559 lua_pushinteger(L, tbl->data_arg[dt].u);
560 else
561 lua_pushinteger(L, -1);
562
563 lua_settable(L, -3);
564 }
565
566 lua_settable(L, -3);
567
568 return 1;
569}
570
571/* Helper to get extract stick table entry into Lua table */
572static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
573{
574 int dt;
575 void *ptr;
576
577 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
578
579 if (t->data_ofs[dt] == 0)
580 continue;
581
582 lua_pushstring(L, stktable_data_types[dt].name);
583
584 ptr = stktable_data_ptr(t, ts, dt);
585 switch (stktable_data_types[dt].std_type) {
586 case STD_T_SINT:
587 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
588 break;
589 case STD_T_UINT:
590 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
591 break;
592 case STD_T_ULL:
593 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
594 break;
595 case STD_T_FRQP:
596 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
597 t->data_arg[dt].u));
598 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200599 case STD_T_DICT: {
600 struct dict_entry *de;
601 de = stktable_data_cast(ptr, std_t_dict);
602 lua_pushstring(L, de ? (char *)de->value.key : "-");
603 break;
604 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200605 }
606
607 lua_settable(L, -3);
608 }
609}
610
611/* Looks in table <t> for a sticky session matching key <key>
612 * Returns table with session data or nil
613 *
614 * The returned table always contains 'use' and 'expire' (integer) fields.
615 * For frequency/rate counters, each data entry is returned as table with
616 * 'value' and 'period' fields.
617 */
618int hlua_stktable_lookup(lua_State *L)
619{
620 struct stktable *t;
621 struct sample smp;
622 struct stktable_key *skey;
623 struct stksess *ts;
624
625 t = hlua_check_stktable(L, 1);
626 smp.data.type = SMP_T_STR;
627 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600628 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200629
630 skey = smp_to_stkey(&smp, t);
631 if (!skey) {
632 lua_pushnil(L);
633 return 1;
634 }
635
636 ts = stktable_lookup_key(t, skey);
637 if (!ts) {
638 lua_pushnil(L);
639 return 1;
640 }
641
642 lua_newtable(L);
643 lua_pushstring(L, "use");
644 lua_pushinteger(L, ts->ref_cnt - 1);
645 lua_settable(L, -3);
646
647 lua_pushstring(L, "expire");
648 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
649 lua_settable(L, -3);
650
651 hlua_stktable_entry(L, t, ts);
Willy Tarreau76642222022-10-11 12:02:50 +0200652 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200653 ts->ref_cnt--;
Willy Tarreau76642222022-10-11 12:02:50 +0200654 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200655
656 return 1;
657}
658
659struct stk_filter {
660 long long val;
661 int type;
662 int op;
663};
664
665
666/* Helper for returning errors to callers using Lua convention (nil, err) */
667static int hlua_error(lua_State *L, const char *fmt, ...) {
668 char buf[256];
669 int len;
670 va_list args;
671 va_start(args, fmt);
672 len = vsnprintf(buf, sizeof(buf), fmt, args);
673 va_end(args);
674
675 if (len < 0) {
676 ha_alert("hlua_error(): Could not write error message.\n");
677 lua_pushnil(L);
678 return 1;
679 } else if (len >= sizeof(buf))
680 ha_alert("hlua_error(): Error message was truncated.\n");
681
682 lua_pushnil(L);
683 lua_pushstring(L, buf);
684
685 return 2;
686}
687
688/* Dump the contents of stick table <t>*/
689int hlua_stktable_dump(lua_State *L)
690{
691 struct stktable *t;
692 struct ebmb_node *eb;
693 struct ebmb_node *n;
694 struct stksess *ts;
695 int type;
696 int op;
697 int dt;
698 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100699 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200700 int filter_count = 0;
701 int i;
702 int skip_entry;
703 void *ptr;
704
705 t = hlua_check_stktable(L, 1);
706 type = lua_type(L, 2);
707
708 switch (type) {
709 case LUA_TNONE:
710 case LUA_TNIL:
711 break;
712 case LUA_TTABLE:
713 lua_pushnil(L);
714 while (lua_next(L, 2) != 0) {
715 int entry_idx = 0;
716
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100717 if (filter_count >= STKTABLE_FILTER_LEN)
718 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200719
720 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
721 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
722
723 lua_pushnil(L);
724 while (lua_next(L, -2) != 0) {
725 switch (entry_idx) {
726 case 0:
727 if (lua_type(L, -1) != LUA_TSTRING)
728 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
729
730 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
731 if (dt < 0 || t->data_ofs[dt] == 0)
732 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
733 filter[filter_count].type = dt;
734 break;
735 case 1:
736 if (lua_type(L, -1) != LUA_TSTRING)
737 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
738
739 op = get_std_op(lua_tostring(L, -1));
740 if (op < 0)
741 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
742 filter[filter_count].op = op;
743 break;
744 case 2:
745 val = lua_tointeger(L, -1);
746 filter[filter_count].val = val;
747 filter_count++;
748 break;
749 default:
750 break;
751 }
752 entry_idx++;
753 lua_pop(L, 1);
754 }
755 lua_pop(L, 1);
756 }
757 break;
758 default:
759 return hlua_error(L, "filter table expected");
760 }
761
762 lua_newtable(L);
763
Willy Tarreau76642222022-10-11 12:02:50 +0200764 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200765 eb = ebmb_first(&t->keys);
766 for (n = eb; n; n = ebmb_next(n)) {
767 ts = ebmb_entry(n, struct stksess, key);
768 if (!ts) {
Willy Tarreau76642222022-10-11 12:02:50 +0200769 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200770 return 1;
771 }
772 ts->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +0200773 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200774
775 /* multi condition/value filter */
776 skip_entry = 0;
777 for (i = 0; i < filter_count; i++) {
778 if (t->data_ofs[filter[i].type] == 0)
779 continue;
780
781 ptr = stktable_data_ptr(t, ts, filter[i].type);
782
783 switch (stktable_data_types[filter[i].type].std_type) {
784 case STD_T_SINT:
785 val = stktable_data_cast(ptr, std_t_sint);
786 break;
787 case STD_T_UINT:
788 val = stktable_data_cast(ptr, std_t_uint);
789 break;
790 case STD_T_ULL:
791 val = stktable_data_cast(ptr, std_t_ull);
792 break;
793 case STD_T_FRQP:
794 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
795 t->data_arg[filter[i].type].u);
796 break;
797 default:
798 continue;
799 break;
800 }
801
802 op = filter[i].op;
803
804 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
805 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
806 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
807 skip_entry = 1;
808 break;
809 }
810 }
811
812 if (skip_entry) {
Willy Tarreau76642222022-10-11 12:02:50 +0200813 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200814 ts->ref_cnt--;
815 continue;
816 }
817
818 if (t->type == SMP_T_IPV4) {
819 char addr[INET_ADDRSTRLEN];
820 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
821 lua_pushstring(L, addr);
822 } else if (t->type == SMP_T_IPV6) {
823 char addr[INET6_ADDRSTRLEN];
824 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
825 lua_pushstring(L, addr);
826 } else if (t->type == SMP_T_SINT) {
827 lua_pushinteger(L, *ts->key.key);
828 } else if (t->type == SMP_T_STR) {
829 lua_pushstring(L, (const char *)ts->key.key);
830 } else {
831 return hlua_error(L, "Unsupported stick table key type");
832 }
833
834 lua_newtable(L);
835 hlua_stktable_entry(L, t, ts);
836 lua_settable(L, -3);
Willy Tarreau76642222022-10-11 12:02:50 +0200837 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200838 ts->ref_cnt--;
839 }
Willy Tarreau76642222022-10-11 12:02:50 +0200840 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200841
842 return 1;
843}
844
Thierry Fournierff480422016-02-25 08:36:46 +0100845int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
846{
847 lua_newtable(L);
848
849 /* Pop a class sesison metatable and affect it to the userdata. */
850 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
851 lua_setmetatable(L, -2);
852
853 lua_pushlightuserdata(L, lst);
854 lua_rawseti(L, -2, 0);
855 return 1;
856}
857
858static struct listener *hlua_check_listener(lua_State *L, int ud)
859{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200860 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100861}
862
863int hlua_listener_get_stats(lua_State *L)
864{
865 struct listener *li;
866 int i;
867
868 li = hlua_check_listener(L, 1);
869
Willy Tarreauc95bad52016-12-22 00:13:31 +0100870 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100871 lua_pushnil(L);
872 return 1;
873 }
874
William Dauchy655e14e2021-02-14 23:22:54 +0100875 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
876 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +0100877
878 lua_newtable(L);
879 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200880 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100881 hlua_fcn_pushfield(L, &stats[i]);
882 lua_settable(L, -3);
883 }
884 return 1;
885
886}
887
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100888int hlua_fcn_new_server(lua_State *L, struct server *srv)
889{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400890 char buffer[12];
891
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100892 lua_newtable(L);
893
894 /* Pop a class sesison metatable and affect it to the userdata. */
895 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
896 lua_setmetatable(L, -2);
897
898 lua_pushlightuserdata(L, srv);
899 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400900
901 /* Add server name. */
902 lua_pushstring(L, "name");
903 lua_pushstring(L, srv->id);
904 lua_settable(L, -3);
905
906 /* Add server puid. */
907 lua_pushstring(L, "puid");
908 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
909 lua_pushstring(L, buffer);
910 lua_settable(L, -3);
911
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100912 return 1;
913}
914
915static struct server *hlua_check_server(lua_State *L, int ud)
916{
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200917 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
918 srv->flags |= SRV_F_NON_PURGEABLE;
919 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100920}
921
922int hlua_server_get_stats(lua_State *L)
923{
924 struct server *srv;
925 int i;
926
927 srv = hlua_check_server(L, 1);
928
929 if (!srv->proxy) {
930 lua_pushnil(L);
931 return 1;
932 }
933
William Dauchyd3a9a492021-01-25 17:29:03 +0100934 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
935 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100936
937 lua_newtable(L);
938 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200939 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100940 hlua_fcn_pushfield(L, &stats[i]);
941 lua_settable(L, -3);
942 }
943 return 1;
944
945}
946
947int hlua_server_get_addr(lua_State *L)
948{
949 struct server *srv;
950 char addr[INET6_ADDRSTRLEN];
951 luaL_Buffer b;
952
953 srv = hlua_check_server(L, 1);
954
955 luaL_buffinit(L, &b);
956
957 switch (srv->addr.ss_family) {
958 case AF_INET:
959 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
960 addr, INET_ADDRSTRLEN);
961 luaL_addstring(&b, addr);
962 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400963 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100964 luaL_addstring(&b, addr);
965 break;
966 case AF_INET6:
967 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400968 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100969 luaL_addstring(&b, addr);
970 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400971 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100972 luaL_addstring(&b, addr);
973 break;
974 case AF_UNIX:
975 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
976 break;
977 default:
978 luaL_addstring(&b, "<unknown>");
979 break;
980 }
981
982 luaL_pushresult(&b);
983 return 1;
984}
985
986int hlua_server_is_draining(lua_State *L)
987{
988 struct server *srv;
989
990 srv = hlua_check_server(L, 1);
991 lua_pushinteger(L, server_is_draining(srv));
992 return 1;
993}
994
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400995int hlua_server_set_maxconn(lua_State *L)
996{
997 struct server *srv;
998 const char *maxconn;
999 const char *err;
1000
1001 srv = hlua_check_server(L, 1);
1002 maxconn = luaL_checkstring(L, 2);
1003
1004 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1005 err = server_parse_maxconn_change_request(srv, maxconn);
1006 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1007 if (!err)
1008 lua_pushnil(L);
1009 else
1010 hlua_pushstrippedstring(L, err);
1011 return 1;
1012}
1013
1014int hlua_server_get_maxconn(lua_State *L)
1015{
1016 struct server *srv;
1017
1018 srv = hlua_check_server(L, 1);
1019 lua_pushinteger(L, srv->maxconn);
1020 return 1;
1021}
1022
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001023int hlua_server_set_weight(lua_State *L)
1024{
1025 struct server *srv;
1026 const char *weight;
1027 const char *err;
1028
1029 srv = hlua_check_server(L, 1);
1030 weight = luaL_checkstring(L, 2);
1031
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001032 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001033 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001034 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001035 if (!err)
1036 lua_pushnil(L);
1037 else
1038 hlua_pushstrippedstring(L, err);
1039 return 1;
1040}
1041
1042int hlua_server_get_weight(lua_State *L)
1043{
1044 struct server *srv;
1045
1046 srv = hlua_check_server(L, 1);
1047 lua_pushinteger(L, srv->uweight);
1048 return 1;
1049}
1050
1051int hlua_server_set_addr(lua_State *L)
1052{
1053 struct server *srv;
1054 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001055 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001056 const char *err;
1057
1058 srv = hlua_check_server(L, 1);
1059 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001060 if (lua_gettop(L) >= 3)
1061 port = luaL_checkstring(L, 3);
1062 else
1063 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001064
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001066 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001067 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001068 if (!err)
1069 lua_pushnil(L);
1070 else
1071 hlua_pushstrippedstring(L, err);
1072 return 1;
1073}
1074
1075int hlua_server_shut_sess(lua_State *L)
1076{
1077 struct server *srv;
1078
1079 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001080 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001081 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001082 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001083 return 0;
1084}
1085
1086int hlua_server_set_drain(lua_State *L)
1087{
1088 struct server *srv;
1089
1090 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001091 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001092 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001093 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001094 return 0;
1095}
1096
1097int hlua_server_set_maint(lua_State *L)
1098{
1099 struct server *srv;
1100
1101 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001102 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001103 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001104 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001105 return 0;
1106}
1107
1108int hlua_server_set_ready(lua_State *L)
1109{
1110 struct server *srv;
1111
1112 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001113 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001114 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001115 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001116 return 0;
1117}
1118
1119int hlua_server_check_enable(lua_State *L)
1120{
1121 struct server *sv;
1122
1123 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001124 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001125 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001126 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001127 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001128 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001129 return 0;
1130}
1131
1132int hlua_server_check_disable(lua_State *L)
1133{
1134 struct server *sv;
1135
1136 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001137 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001138 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001139 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001140 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001141 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001142 return 0;
1143}
1144
1145int hlua_server_check_force_up(lua_State *L)
1146{
1147 struct server *sv;
1148
1149 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001150 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001151 if (!(sv->track)) {
1152 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001153 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001154 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001155 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001156 return 0;
1157}
1158
1159int hlua_server_check_force_nolb(lua_State *L)
1160{
1161 struct server *sv;
1162
1163 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001164 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001165 if (!(sv->track)) {
1166 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001167 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001168 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001169 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001170 return 0;
1171}
1172
1173int hlua_server_check_force_down(lua_State *L)
1174{
1175 struct server *sv;
1176
1177 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001178 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001179 if (!(sv->track)) {
1180 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001181 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001182 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001183 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001184 return 0;
1185}
1186
1187int hlua_server_agent_enable(lua_State *L)
1188{
1189 struct server *sv;
1190
1191 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001192 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001193 if (sv->agent.state & CHK_ST_CONFIGURED) {
1194 sv->agent.state |= CHK_ST_ENABLED;
1195 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001196 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001197 return 0;
1198}
1199
1200int hlua_server_agent_disable(lua_State *L)
1201{
1202 struct server *sv;
1203
1204 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001205 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001206 if (sv->agent.state & CHK_ST_CONFIGURED) {
1207 sv->agent.state &= ~CHK_ST_ENABLED;
1208 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001209 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001210 return 0;
1211}
1212
1213int hlua_server_agent_force_up(lua_State *L)
1214{
1215 struct server *sv;
1216
1217 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001219 if (sv->agent.state & CHK_ST_ENABLED) {
1220 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001221 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001222 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001223 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001224 return 0;
1225}
1226
1227int hlua_server_agent_force_down(lua_State *L)
1228{
1229 struct server *sv;
1230
1231 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001232 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001233 if (sv->agent.state & CHK_ST_ENABLED) {
1234 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001235 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001236 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001237 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001238 return 0;
1239}
1240
Thierry Fournierf61aa632016-02-19 20:56:00 +01001241int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1242{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001243 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001244 struct listener *lst;
1245 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001246 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001247
Thierry Fournierf61aa632016-02-19 20:56:00 +01001248 lua_newtable(L);
1249
1250 /* Pop a class sesison metatable and affect it to the userdata. */
1251 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1252 lua_setmetatable(L, -2);
1253
1254 lua_pushlightuserdata(L, px);
1255 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001256
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001257 /* Add proxy name. */
1258 lua_pushstring(L, "name");
1259 lua_pushstring(L, px->id);
1260 lua_settable(L, -3);
1261
Baptiste Assmann46c72552017-10-26 21:51:58 +02001262 /* Add proxy uuid. */
1263 lua_pushstring(L, "uuid");
1264 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1265 lua_pushstring(L, buffer);
1266 lua_settable(L, -3);
1267
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001268 /* Browse and register servers. */
1269 lua_pushstring(L, "servers");
1270 lua_newtable(L);
1271 for (srv = px->srv; srv; srv = srv->next) {
1272 lua_pushstring(L, srv->id);
1273 hlua_fcn_new_server(L, srv);
1274 lua_settable(L, -3);
1275 }
1276 lua_settable(L, -3);
1277
Thierry Fournierff480422016-02-25 08:36:46 +01001278 /* Browse and register listeners. */
1279 lua_pushstring(L, "listeners");
1280 lua_newtable(L);
1281 lid = 1;
1282 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1283 if (lst->name)
1284 lua_pushstring(L, lst->name);
1285 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001286 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001287 lid++;
1288 lua_pushstring(L, buffer);
1289 }
1290 hlua_fcn_new_listener(L, lst);
1291 lua_settable(L, -3);
1292 }
1293 lua_settable(L, -3);
1294
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001295 if (px->table && px->table->id) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001296 lua_pushstring(L, "stktable");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001297 hlua_fcn_new_stktable(L, px->table);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001298 lua_settable(L, -3);
1299 }
1300
Thierry Fournierf61aa632016-02-19 20:56:00 +01001301 return 1;
1302}
1303
1304static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1305{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001306 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001307}
1308
1309int hlua_proxy_pause(lua_State *L)
1310{
1311 struct proxy *px;
1312
1313 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001314 /* safe to call without PROXY_LOCK - pause_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001315 pause_proxy(px);
1316 return 0;
1317}
1318
1319int hlua_proxy_resume(lua_State *L)
1320{
1321 struct proxy *px;
1322
1323 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001324 /* safe to call without PROXY_LOCK - resume_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001325 resume_proxy(px);
1326 return 0;
1327}
1328
1329int hlua_proxy_stop(lua_State *L)
1330{
1331 struct proxy *px;
1332
1333 px = hlua_check_proxy(L, 1);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02001334 /* safe to call without PROXY_LOCK - stop_proxy takes it */
Thierry Fournierf61aa632016-02-19 20:56:00 +01001335 stop_proxy(px);
1336 return 0;
1337}
1338
1339int hlua_proxy_get_cap(lua_State *L)
1340{
1341 struct proxy *px;
1342 const char *str;
1343
1344 px = hlua_check_proxy(L, 1);
1345 str = proxy_cap_str(px->cap);
1346 lua_pushstring(L, str);
1347 return 1;
1348}
1349
1350int hlua_proxy_get_stats(lua_State *L)
1351{
1352 struct proxy *px;
1353 int i;
1354
1355 px = hlua_check_proxy(L, 1);
1356 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001357 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001358 else
William Dauchy0ef54392021-01-17 18:27:45 +01001359 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001360 lua_newtable(L);
1361 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001362 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001363 hlua_fcn_pushfield(L, &stats[i]);
1364 lua_settable(L, -3);
1365 }
1366 return 1;
1367}
1368
1369int hlua_proxy_get_mode(lua_State *L)
1370{
1371 struct proxy *px;
1372 const char *str;
1373
1374 px = hlua_check_proxy(L, 1);
1375 str = proxy_mode_str(px->mode);
1376 lua_pushstring(L, str);
1377 return 1;
1378}
1379
1380int hlua_proxy_shut_bcksess(lua_State *L)
1381{
1382 struct proxy *px;
1383
1384 px = hlua_check_proxy(L, 1);
1385 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1386 return 0;
1387}
1388
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001389int hlua_fcn_post_init(lua_State *L)
1390{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001391 struct proxy *px;
1392
1393 /* get core array. */
1394 if (lua_getglobal(L, "core") != LUA_TTABLE)
1395 lua_error(L);
1396
1397 /* Create proxies entry. */
1398 lua_pushstring(L, "proxies");
1399 lua_newtable(L);
1400
1401 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001402 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001403 if (px->cap & PR_CAP_INT)
1404 continue;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001405 lua_pushstring(L, px->id);
1406 hlua_fcn_new_proxy(L, px);
1407 lua_settable(L, -3);
1408 }
1409
1410 /* push "proxies" in "core" */
1411 lua_settable(L, -3);
1412
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001413 /* Create proxies entry. */
1414 lua_pushstring(L, "frontends");
1415 lua_newtable(L);
1416
1417 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001418 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001419 if (!(px->cap & PR_CAP_FE) || (px->cap & PR_CAP_INT))
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001420 continue;
1421 lua_pushstring(L, px->id);
1422 hlua_fcn_new_proxy(L, px);
1423 lua_settable(L, -3);
1424 }
1425
1426 /* push "frontends" in "core" */
1427 lua_settable(L, -3);
1428
1429 /* Create proxies entry. */
1430 lua_pushstring(L, "backends");
1431 lua_newtable(L);
1432
1433 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001434 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001435 if (!(px->cap & PR_CAP_BE) || (px->cap & PR_CAP_INT))
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001436 continue;
1437 lua_pushstring(L, px->id);
1438 hlua_fcn_new_proxy(L, px);
1439 lua_settable(L, -3);
1440 }
1441
1442 /* push "backend" in "core" */
1443 lua_settable(L, -3);
1444
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001445 return 1;
1446}
1447
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001448/* This Lua function take a string, a list of separators.
1449 * It tokenize the input string using the list of separators
1450 * as separator.
1451 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001452 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001453 */
1454int hlua_tokenize(lua_State *L)
1455{
1456 const char *str;
1457 const char *sep;
1458 int index;
1459 const char *token;
1460 const char *p;
1461 const char *c;
1462 int ignore_empty;
1463
1464 ignore_empty = 0;
1465
1466 str = luaL_checkstring(L, 1);
1467 sep = luaL_checkstring(L, 2);
1468 if (lua_gettop(L) == 3)
1469 ignore_empty = hlua_checkboolean(L, 3);
1470
1471 lua_newtable(L);
1472 index = 1;
1473 token = str;
1474 p = str;
1475 while(1) {
1476 for (c = sep; *c != '\0'; c++)
1477 if (*p == *c)
1478 break;
1479 if (*p == *c) {
1480 if ((!ignore_empty) || (p - token > 0)) {
1481 lua_pushlstring(L, token, p - token);
1482 lua_rawseti(L, -2, index);
1483 index++;
1484 }
1485 token = p + 1;
1486 }
1487 if (*p == '\0')
1488 break;
1489 p++;
1490 }
1491
1492 return 1;
1493}
1494
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001495int hlua_parse_addr(lua_State *L)
1496{
Christopher Faulet29e93262021-02-26 09:39:05 +01001497 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001498 const char *str = luaL_checkstring(L, 1);
1499 unsigned char mask;
1500
Christopher Faulet29e93262021-02-26 09:39:05 +01001501 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001502 if (!addr) {
1503 lua_pushnil(L);
1504 return 1;
1505 }
1506
1507 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001508 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001509 return 1;
1510 }
1511
1512 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1513 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001514 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001515 return 1;
1516 }
1517
1518 lua_pop(L, 1);
1519 lua_pushnil(L);
1520 return 1;
1521}
1522
1523int hlua_match_addr(lua_State *L)
1524{
Christopher Faulet29e93262021-02-26 09:39:05 +01001525 struct net_addr *addr1;
1526 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001527
1528 if (!lua_isuserdata(L, 1) ||
1529 !lua_isuserdata(L, 2)) {
1530 lua_pushboolean(L, 0);
1531 return 1;
1532 }
1533
1534 addr1 = lua_touserdata(L, 1);
1535 addr2 = lua_touserdata(L, 2);
1536
Christopher Faulet29e93262021-02-26 09:39:05 +01001537 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001538 lua_pushboolean(L, 0);
1539 return 1;
1540 }
1541
Christopher Faulet29e93262021-02-26 09:39:05 +01001542 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001543 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1544 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1545 lua_pushboolean(L, 1);
1546 return 1;
1547 }
1548 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001549 int i;
1550
1551 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001552 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1553 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1554 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1555 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001556 break;
1557 }
1558 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001559 lua_pushboolean(L, 1);
1560 return 1;
1561 }
1562 }
1563
1564 lua_pushboolean(L, 0);
1565 return 1;
1566}
1567
Dragan Dosen26743032019-04-30 15:54:36 +02001568static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001569{
1570 return (hlua_checkudata(L, ud, class_regex_ref));
1571}
1572
1573static int hlua_regex_comp(struct lua_State *L)
1574{
Dragan Dosen26743032019-04-30 15:54:36 +02001575 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001576 const char *str;
1577 int cs;
1578 char *err;
1579
1580 str = luaL_checkstring(L, 1);
1581 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1582 cs = lua_toboolean(L, 2);
1583
1584 regex = lua_newuserdata(L, sizeof(*regex));
1585
1586 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001587 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001588 lua_pushboolean(L, 0); /* status error */
1589 lua_pushstring(L, err); /* Reason */
1590 free(err);
1591 return 2;
1592 }
1593
1594 lua_pushboolean(L, 1); /* Status ok */
1595
1596 /* Create object */
1597 lua_newtable(L);
1598 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1599 lua_rawseti(L, -2, 0);
1600 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1601 lua_setmetatable(L, -2);
1602 return 2;
1603}
1604
1605static int hlua_regex_exec(struct lua_State *L)
1606{
Dragan Dosen26743032019-04-30 15:54:36 +02001607 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001608 const char *str;
1609 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001610 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001611
1612 regex = hlua_check_regex(L, 1);
1613 str = luaL_checklstring(L, 2, &len);
1614
Dragan Dosen26743032019-04-30 15:54:36 +02001615 if (!*regex) {
1616 lua_pushboolean(L, 0);
1617 return 1;
1618 }
1619
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001620 /* Copy the string because regex_exec2 require a 'char *'
1621 * and not a 'const char *'.
1622 */
1623 tmp = get_trash_chunk();
1624 if (len >= tmp->size) {
1625 lua_pushboolean(L, 0);
1626 return 1;
1627 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001628 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001629
Dragan Dosen26743032019-04-30 15:54:36 +02001630 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001631
1632 return 1;
1633}
1634
1635static int hlua_regex_match(struct lua_State *L)
1636{
Dragan Dosen26743032019-04-30 15:54:36 +02001637 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001638 const char *str;
1639 size_t len;
1640 regmatch_t pmatch[20];
1641 int ret;
1642 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001643 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001644
1645 regex = hlua_check_regex(L, 1);
1646 str = luaL_checklstring(L, 2, &len);
1647
Dragan Dosen26743032019-04-30 15:54:36 +02001648 if (!*regex) {
1649 lua_pushboolean(L, 0);
1650 return 1;
1651 }
1652
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001653 /* Copy the string because regex_exec2 require a 'char *'
1654 * and not a 'const char *'.
1655 */
1656 tmp = get_trash_chunk();
1657 if (len >= tmp->size) {
1658 lua_pushboolean(L, 0);
1659 return 1;
1660 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001661 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001662
Dragan Dosen26743032019-04-30 15:54:36 +02001663 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001664 lua_pushboolean(L, ret);
1665 lua_newtable(L);
1666 if (ret) {
1667 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1668 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1669 lua_rawseti(L, -2, i + 1);
1670 }
1671 }
1672 return 2;
1673}
1674
1675static int hlua_regex_free(struct lua_State *L)
1676{
Dragan Dosen26743032019-04-30 15:54:36 +02001677 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001678
1679 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001680 regex_free(*regex);
1681 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001682 return 0;
1683}
1684
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001685int hlua_fcn_reg_core_fcn(lua_State *L)
1686{
Thierry Fournier1de16592016-01-27 09:49:07 +01001687 if (!hlua_concat_init(L))
1688 return 0;
1689
Thierry Fournier4f99b272016-02-22 08:40:02 +01001690 hlua_class_function(L, "now", hlua_now);
1691 hlua_class_function(L, "http_date", hlua_http_date);
1692 hlua_class_function(L, "imf_date", hlua_imf_date);
1693 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1694 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1695 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001696 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001697 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1698 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001699 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001700
Thierry FOURNIER31904272017-10-25 12:59:51 +02001701 /* Create regex object. */
1702 lua_newtable(L);
1703 hlua_class_function(L, "new", hlua_regex_comp);
1704
1705 lua_newtable(L); /* The metatable. */
1706 lua_pushstring(L, "__index");
1707 lua_newtable(L);
1708 hlua_class_function(L, "exec", hlua_regex_exec);
1709 hlua_class_function(L, "match", hlua_regex_match);
1710 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1711 hlua_class_function(L, "__gc", hlua_regex_free);
1712
1713 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1714 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1715
1716 lua_setmetatable(L, -2);
1717 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1718
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001719 /* Create stktable object. */
1720 lua_newtable(L);
1721 lua_pushstring(L, "__index");
1722 lua_newtable(L);
1723 hlua_class_function(L, "info", hlua_stktable_info);
1724 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1725 hlua_class_function(L, "dump", hlua_stktable_dump);
1726 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1727 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1728
Thierry Fournierff480422016-02-25 08:36:46 +01001729 /* Create listener object. */
1730 lua_newtable(L);
1731 lua_pushstring(L, "__index");
1732 lua_newtable(L);
1733 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1734 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1735 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1736
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001737 /* Create server object. */
1738 lua_newtable(L);
1739 lua_pushstring(L, "__index");
1740 lua_newtable(L);
1741 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001742 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1743 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001744 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1745 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1746 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1747 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1748 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1749 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1750 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1751 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1752 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1753 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1754 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1755 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1756 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1757 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1758 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1759 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1760 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1761 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1762 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1763 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1764
Thierry Fournierf61aa632016-02-19 20:56:00 +01001765 /* Create proxy object. */
1766 lua_newtable(L);
1767 lua_pushstring(L, "__index");
1768 lua_newtable(L);
1769 hlua_class_function(L, "pause", hlua_proxy_pause);
1770 hlua_class_function(L, "resume", hlua_proxy_resume);
1771 hlua_class_function(L, "stop", hlua_proxy_stop);
1772 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1773 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1774 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1775 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1776 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1777 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1778
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001779 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001780}