blob: 5907d48557bad17848f8097d9f2b434dc965f0d4 [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);
652 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
653 ts->ref_cnt--;
654 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
655
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
764 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
765 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) {
769 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
770 return 1;
771 }
772 ts->ref_cnt++;
773 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
774
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) {
813 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
814 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);
837 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
838 ts->ref_cnt--;
839 }
840 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
841
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);
1314 pause_proxy(px);
1315 return 0;
1316}
1317
1318int hlua_proxy_resume(lua_State *L)
1319{
1320 struct proxy *px;
1321
1322 px = hlua_check_proxy(L, 1);
1323 resume_proxy(px);
1324 return 0;
1325}
1326
1327int hlua_proxy_stop(lua_State *L)
1328{
1329 struct proxy *px;
1330
1331 px = hlua_check_proxy(L, 1);
1332 stop_proxy(px);
1333 return 0;
1334}
1335
1336int hlua_proxy_get_cap(lua_State *L)
1337{
1338 struct proxy *px;
1339 const char *str;
1340
1341 px = hlua_check_proxy(L, 1);
1342 str = proxy_cap_str(px->cap);
1343 lua_pushstring(L, str);
1344 return 1;
1345}
1346
1347int hlua_proxy_get_stats(lua_State *L)
1348{
1349 struct proxy *px;
1350 int i;
1351
1352 px = hlua_check_proxy(L, 1);
1353 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001354 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001355 else
William Dauchy0ef54392021-01-17 18:27:45 +01001356 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001357 lua_newtable(L);
1358 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001359 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001360 hlua_fcn_pushfield(L, &stats[i]);
1361 lua_settable(L, -3);
1362 }
1363 return 1;
1364}
1365
1366int hlua_proxy_get_mode(lua_State *L)
1367{
1368 struct proxy *px;
1369 const char *str;
1370
1371 px = hlua_check_proxy(L, 1);
1372 str = proxy_mode_str(px->mode);
1373 lua_pushstring(L, str);
1374 return 1;
1375}
1376
1377int hlua_proxy_shut_bcksess(lua_State *L)
1378{
1379 struct proxy *px;
1380
1381 px = hlua_check_proxy(L, 1);
1382 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1383 return 0;
1384}
1385
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001386int hlua_fcn_post_init(lua_State *L)
1387{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001388 struct proxy *px;
1389
1390 /* get core array. */
1391 if (lua_getglobal(L, "core") != LUA_TTABLE)
1392 lua_error(L);
1393
1394 /* Create proxies entry. */
1395 lua_pushstring(L, "proxies");
1396 lua_newtable(L);
1397
1398 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001399 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001400 if (px->cap & PR_CAP_INT)
1401 continue;
Thierry Fournierf61aa632016-02-19 20:56:00 +01001402 lua_pushstring(L, px->id);
1403 hlua_fcn_new_proxy(L, px);
1404 lua_settable(L, -3);
1405 }
1406
1407 /* push "proxies" in "core" */
1408 lua_settable(L, -3);
1409
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001410 /* Create proxies entry. */
1411 lua_pushstring(L, "frontends");
1412 lua_newtable(L);
1413
1414 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001415 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001416 if (!(px->cap & PR_CAP_FE) || (px->cap & PR_CAP_INT))
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001417 continue;
1418 lua_pushstring(L, px->id);
1419 hlua_fcn_new_proxy(L, px);
1420 lua_settable(L, -3);
1421 }
1422
1423 /* push "frontends" in "core" */
1424 lua_settable(L, -3);
1425
1426 /* Create proxies entry. */
1427 lua_pushstring(L, "backends");
1428 lua_newtable(L);
1429
1430 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001431 for (px = proxies_list; px; px = px->next) {
William Lallemand82d5f012021-11-24 16:14:24 +01001432 if (!(px->cap & PR_CAP_BE) || (px->cap & PR_CAP_INT))
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001433 continue;
1434 lua_pushstring(L, px->id);
1435 hlua_fcn_new_proxy(L, px);
1436 lua_settable(L, -3);
1437 }
1438
1439 /* push "backend" in "core" */
1440 lua_settable(L, -3);
1441
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001442 return 1;
1443}
1444
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001445/* This Lua function take a string, a list of separators.
1446 * It tokenize the input string using the list of separators
1447 * as separator.
1448 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001449 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001450 */
1451int hlua_tokenize(lua_State *L)
1452{
1453 const char *str;
1454 const char *sep;
1455 int index;
1456 const char *token;
1457 const char *p;
1458 const char *c;
1459 int ignore_empty;
1460
1461 ignore_empty = 0;
1462
1463 str = luaL_checkstring(L, 1);
1464 sep = luaL_checkstring(L, 2);
1465 if (lua_gettop(L) == 3)
1466 ignore_empty = hlua_checkboolean(L, 3);
1467
1468 lua_newtable(L);
1469 index = 1;
1470 token = str;
1471 p = str;
1472 while(1) {
1473 for (c = sep; *c != '\0'; c++)
1474 if (*p == *c)
1475 break;
1476 if (*p == *c) {
1477 if ((!ignore_empty) || (p - token > 0)) {
1478 lua_pushlstring(L, token, p - token);
1479 lua_rawseti(L, -2, index);
1480 index++;
1481 }
1482 token = p + 1;
1483 }
1484 if (*p == '\0')
1485 break;
1486 p++;
1487 }
1488
1489 return 1;
1490}
1491
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001492int hlua_parse_addr(lua_State *L)
1493{
Christopher Faulet29e93262021-02-26 09:39:05 +01001494 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001495 const char *str = luaL_checkstring(L, 1);
1496 unsigned char mask;
1497
Christopher Faulet29e93262021-02-26 09:39:05 +01001498 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001499 if (!addr) {
1500 lua_pushnil(L);
1501 return 1;
1502 }
1503
1504 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001505 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001506 return 1;
1507 }
1508
1509 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1510 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001511 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001512 return 1;
1513 }
1514
1515 lua_pop(L, 1);
1516 lua_pushnil(L);
1517 return 1;
1518}
1519
1520int hlua_match_addr(lua_State *L)
1521{
Christopher Faulet29e93262021-02-26 09:39:05 +01001522 struct net_addr *addr1;
1523 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001524
1525 if (!lua_isuserdata(L, 1) ||
1526 !lua_isuserdata(L, 2)) {
1527 lua_pushboolean(L, 0);
1528 return 1;
1529 }
1530
1531 addr1 = lua_touserdata(L, 1);
1532 addr2 = lua_touserdata(L, 2);
1533
Christopher Faulet29e93262021-02-26 09:39:05 +01001534 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001535 lua_pushboolean(L, 0);
1536 return 1;
1537 }
1538
Christopher Faulet29e93262021-02-26 09:39:05 +01001539 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001540 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1541 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1542 lua_pushboolean(L, 1);
1543 return 1;
1544 }
1545 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001546 int i;
1547
1548 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001549 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1550 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1551 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1552 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001553 break;
1554 }
1555 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001556 lua_pushboolean(L, 1);
1557 return 1;
1558 }
1559 }
1560
1561 lua_pushboolean(L, 0);
1562 return 1;
1563}
1564
Dragan Dosen26743032019-04-30 15:54:36 +02001565static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001566{
1567 return (hlua_checkudata(L, ud, class_regex_ref));
1568}
1569
1570static int hlua_regex_comp(struct lua_State *L)
1571{
Dragan Dosen26743032019-04-30 15:54:36 +02001572 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001573 const char *str;
1574 int cs;
1575 char *err;
1576
1577 str = luaL_checkstring(L, 1);
1578 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1579 cs = lua_toboolean(L, 2);
1580
1581 regex = lua_newuserdata(L, sizeof(*regex));
1582
1583 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001584 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001585 lua_pushboolean(L, 0); /* status error */
1586 lua_pushstring(L, err); /* Reason */
1587 free(err);
1588 return 2;
1589 }
1590
1591 lua_pushboolean(L, 1); /* Status ok */
1592
1593 /* Create object */
1594 lua_newtable(L);
1595 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1596 lua_rawseti(L, -2, 0);
1597 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1598 lua_setmetatable(L, -2);
1599 return 2;
1600}
1601
1602static int hlua_regex_exec(struct lua_State *L)
1603{
Dragan Dosen26743032019-04-30 15:54:36 +02001604 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001605 const char *str;
1606 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001607 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001608
1609 regex = hlua_check_regex(L, 1);
1610 str = luaL_checklstring(L, 2, &len);
1611
Dragan Dosen26743032019-04-30 15:54:36 +02001612 if (!*regex) {
1613 lua_pushboolean(L, 0);
1614 return 1;
1615 }
1616
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001617 /* Copy the string because regex_exec2 require a 'char *'
1618 * and not a 'const char *'.
1619 */
1620 tmp = get_trash_chunk();
1621 if (len >= tmp->size) {
1622 lua_pushboolean(L, 0);
1623 return 1;
1624 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001625 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001626
Dragan Dosen26743032019-04-30 15:54:36 +02001627 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001628
1629 return 1;
1630}
1631
1632static int hlua_regex_match(struct lua_State *L)
1633{
Dragan Dosen26743032019-04-30 15:54:36 +02001634 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001635 const char *str;
1636 size_t len;
1637 regmatch_t pmatch[20];
1638 int ret;
1639 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001640 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001641
1642 regex = hlua_check_regex(L, 1);
1643 str = luaL_checklstring(L, 2, &len);
1644
Dragan Dosen26743032019-04-30 15:54:36 +02001645 if (!*regex) {
1646 lua_pushboolean(L, 0);
1647 return 1;
1648 }
1649
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001650 /* Copy the string because regex_exec2 require a 'char *'
1651 * and not a 'const char *'.
1652 */
1653 tmp = get_trash_chunk();
1654 if (len >= tmp->size) {
1655 lua_pushboolean(L, 0);
1656 return 1;
1657 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001658 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001659
Dragan Dosen26743032019-04-30 15:54:36 +02001660 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001661 lua_pushboolean(L, ret);
1662 lua_newtable(L);
1663 if (ret) {
1664 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1665 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1666 lua_rawseti(L, -2, i + 1);
1667 }
1668 }
1669 return 2;
1670}
1671
1672static int hlua_regex_free(struct lua_State *L)
1673{
Dragan Dosen26743032019-04-30 15:54:36 +02001674 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001675
1676 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001677 regex_free(*regex);
1678 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001679 return 0;
1680}
1681
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001682int hlua_fcn_reg_core_fcn(lua_State *L)
1683{
Thierry Fournier1de16592016-01-27 09:49:07 +01001684 if (!hlua_concat_init(L))
1685 return 0;
1686
Thierry Fournier4f99b272016-02-22 08:40:02 +01001687 hlua_class_function(L, "now", hlua_now);
1688 hlua_class_function(L, "http_date", hlua_http_date);
1689 hlua_class_function(L, "imf_date", hlua_imf_date);
1690 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1691 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1692 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001693 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001694 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1695 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001696 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001697
Thierry FOURNIER31904272017-10-25 12:59:51 +02001698 /* Create regex object. */
1699 lua_newtable(L);
1700 hlua_class_function(L, "new", hlua_regex_comp);
1701
1702 lua_newtable(L); /* The metatable. */
1703 lua_pushstring(L, "__index");
1704 lua_newtable(L);
1705 hlua_class_function(L, "exec", hlua_regex_exec);
1706 hlua_class_function(L, "match", hlua_regex_match);
1707 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1708 hlua_class_function(L, "__gc", hlua_regex_free);
1709
1710 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1711 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1712
1713 lua_setmetatable(L, -2);
1714 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1715
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001716 /* Create stktable object. */
1717 lua_newtable(L);
1718 lua_pushstring(L, "__index");
1719 lua_newtable(L);
1720 hlua_class_function(L, "info", hlua_stktable_info);
1721 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1722 hlua_class_function(L, "dump", hlua_stktable_dump);
1723 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1724 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1725
Thierry Fournierff480422016-02-25 08:36:46 +01001726 /* Create listener object. */
1727 lua_newtable(L);
1728 lua_pushstring(L, "__index");
1729 lua_newtable(L);
1730 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1731 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1732 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1733
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001734 /* Create server object. */
1735 lua_newtable(L);
1736 lua_pushstring(L, "__index");
1737 lua_newtable(L);
1738 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001739 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1740 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001741 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1742 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1743 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1744 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1745 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1746 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1747 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1748 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1749 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1750 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1751 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1752 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1753 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1754 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1755 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1756 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1757 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1758 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1759 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1760 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1761
Thierry Fournierf61aa632016-02-19 20:56:00 +01001762 /* Create proxy object. */
1763 lua_newtable(L);
1764 lua_pushstring(L, "__index");
1765 lua_newtable(L);
1766 hlua_class_function(L, "pause", hlua_proxy_pause);
1767 hlua_class_function(L, "resume", hlua_proxy_resume);
1768 hlua_class_function(L, "stop", hlua_proxy_stop);
1769 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1770 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1771 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1772 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1773 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1774 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1775
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001776 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001777}