blob: f7120f23faa01ae9fd6385829055b3aa662f6936 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua safe functions
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 *
12 * All the functions in this file runs with a Lua stack, and can
Thierry Fournierfb0b5462016-01-21 09:28:58 +010013 * return with a longjmp. All of these function must be launched
14 * in an environment able to catch a longjmp, otherwise a
15 * critical error can be raised.
16 */
Bertrand Jacquinf4c12d42021-01-21 21:14:07 +000017
18#define _GNU_SOURCE
19
Thierry Fournierfb0b5462016-01-21 09:28:58 +010020#include <lauxlib.h>
21#include <lua.h>
22#include <lualib.h>
23
Willy Tarreau83487a82020-06-04 20:19:54 +020024#include <haproxy/cli-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020025#include <haproxy/errors.h>
Willy Tarreau86416052020-06-04 09:20:54 +020026#include <haproxy/hlua-t.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020028#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020029#include <haproxy/pattern-t.h>
30#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020034#include <haproxy/stick_table.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/time.h>
Christopher Faulet29e93262021-02-26 09:39:05 +010036#include <haproxy/tools.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010037
Thierry Fournier1de16592016-01-27 09:49:07 +010038/* Contains the class reference of the concat object. */
39static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010040static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010041static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010042static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020043static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020044static int class_stktable_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010045
Thierry Fournierf61aa632016-02-19 20:56:00 +010046#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010047
Thierry FOURNIERffbad792017-07-12 11:39:04 +020048static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010049
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010050int hlua_checkboolean(lua_State *L, int index)
51{
52 if (!lua_isboolean(L, index))
53 luaL_argerror(L, index, "boolean expected");
54 return lua_toboolean(L, index);
55}
56
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020057/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
58static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
59{
60#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
61 lua_pushinteger(L, val);
62#else
63 if (val > INT_MAX)
64 lua_pushnumber(L, (lua_Number)val);
65 else
66 lua_pushinteger(L, (int)val);
67#endif
68 return 1;
69}
70
71/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
72static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
73#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
74 /* 64 bits case, U64 is supported until LLONG_MAX */
75 if (val > LLONG_MAX)
76 lua_pushnumber(L, (lua_Number)val);
77 else
78 lua_pushinteger(L, val);
79#else
80 /* 32 bits case, U64 is supported until INT_MAX */
81 if (val > INT_MAX)
82 lua_pushnumber(L, (lua_Number)val);
83 else
84 lua_pushinteger(L, (int)val);
85#endif
86 return 1;
87}
88
Joseph Herlantb3d92e32018-11-15 09:35:04 -080089/* This function gets a struct field and converts it in Lua
90 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010091 */
92int hlua_fcn_pushfield(lua_State *L, struct field *field)
93{
94 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -080095 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010096 * by some macros. Windows maros are not supported.
97 * If the number cannot be represented as integer, we try to
98 * convert to float.
99 */
100 switch (field_format(field, 0)) {
101
102 case FF_EMPTY:
103 lua_pushnil(L);
104 return 1;
105
106 case FF_S32:
107 /* S32 is always supported. */
108 lua_pushinteger(L, field->u.s32);
109 return 1;
110
111 case FF_U32:
112#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
113 /* 64 bits case, U32 is always supported */
114 lua_pushinteger(L, field->u.u32);
115#else
116 /* 32 bits case, U32 is supported until INT_MAX. */
117 if (field->u.u32 > INT_MAX)
118 lua_pushnumber(L, (lua_Number)field->u.u32);
119 else
120 lua_pushinteger(L, field->u.u32);
121#endif
122 return 1;
123
124 case FF_S64:
125#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
126 /* 64 bits case, S64 is always supported */
127 lua_pushinteger(L, field->u.s64);
128#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500129 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100130 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
131 lua_pushnumber(L, (lua_Number)field->u.s64);
132 else
133 lua_pushinteger(L, (int)field->u.s64);
134#endif
135 return 1;
136
137 case FF_U64:
138#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
139 /* 64 bits case, U64 is supported until LLONG_MAX */
140 if (field->u.u64 > LLONG_MAX)
141 lua_pushnumber(L, (lua_Number)field->u.u64);
142 else
143 lua_pushinteger(L, field->u.u64);
144#else
145 /* 64 bits case, U64 is supported until INT_MAX */
146 if (field->u.u64 > INT_MAX)
147 lua_pushnumber(L, (lua_Number)field->u.u64);
148 else
149 lua_pushinteger(L, (int)field->u.u64);
150#endif
151 return 1;
152
153 case FF_STR:
154 lua_pushstring(L, field->u.str);
155 return 1;
156
157 default:
158 break;
159 }
160
161 /* Default case, never reached. */
162 lua_pushnil(L);
163 return 1;
164}
165
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100166/* Some string are started or terminated by blank chars,
167 * this function removes the spaces, tabs, \r and
168 * \n at the begin and at the end of the string "str", and
169 * push the result in the lua stack.
170 * Returns a pointer to the Lua internal copy of the string.
171 */
172const char *hlua_pushstrippedstring(lua_State *L, const char *str)
173{
174 const char *p;
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100175 int l;
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100176
177 for (p = str; HTTP_IS_LWS(*p); p++);
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100178
179 for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100180
Christopher Faulet2ec4e3c2021-03-03 19:36:51 +0100181 return lua_pushlstring(L, p, l);
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100182}
183
Thierry Fournierddd89882016-02-22 19:52:08 +0100184/* The three following functions are useful for adding entries
185 * in a table. These functions takes a string and respectively an
186 * integer, a string or a function and add it to the table in the
187 * top of the stack.
188 *
189 * These functions throws an error if no more stack size is
190 * available.
191 */
192void hlua_class_const_int(lua_State *L, const char *name, int value)
193{
Thierry Fournierddd89882016-02-22 19:52:08 +0100194 lua_pushstring(L, name);
195 lua_pushinteger(L, value);
196 lua_rawset(L, -3);
197}
198void hlua_class_const_str(lua_State *L, const char *name, const char *value)
199{
Thierry Fournierddd89882016-02-22 19:52:08 +0100200 lua_pushstring(L, name);
201 lua_pushstring(L, value);
202 lua_rawset(L, -3);
203}
204void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
205{
Thierry Fournierddd89882016-02-22 19:52:08 +0100206 lua_pushstring(L, name);
207 lua_pushcclosure(L, function, 0);
208 lua_rawset(L, -3);
209}
210
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800211/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100212int hlua_dump_object(struct lua_State *L)
213{
214 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
215 lua_pushfstring(L, "HAProxy class %s", name);
216 return 1;
217}
218
Thierry Fournier45e78d72016-02-19 18:34:46 +0100219/* This function register a table as metatable and. It names
220 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500221 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100222 * "name" is the referenced class name.
223 */
224int hlua_register_metatable(struct lua_State *L, char *name)
225{
226 /* Check the type of the top element. it must be
227 * a table.
228 */
229 if (lua_type(L, -1) != LUA_TTABLE)
230 luaL_error(L, "hlua_register_metatable() requires a type Table "
231 "in the top of the stack");
232
233 /* Add the __tostring function which identify the
234 * created object.
235 */
236 lua_pushstring(L, "__tostring");
237 lua_pushstring(L, name);
238 lua_pushcclosure(L, hlua_dump_object, 1);
239 lua_rawset(L, -3);
240
241 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500242 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100243 * lua_setfield() pop the entry.
244 */
245 lua_pushvalue(L, -1);
246 lua_setfield(L, LUA_REGISTRYINDEX, name);
247
248 /* Creates the reference of the object. The
249 * function luaL_ref pop the top of the stack.
250 */
251 return luaL_ref(L, LUA_REGISTRYINDEX);
252}
253
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100254/* Return an object of the expected type, or throws an error. */
255void *hlua_checkudata(lua_State *L, int ud, int class_ref)
256{
257 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100258 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100259
260 /* Check if the stack entry is an array. */
261 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100262 luaL_argerror(L, ud, NULL);
263
264 /* pop the metatable of the referencecd object. */
265 if (!lua_getmetatable(L, ud))
266 luaL_argerror(L, ud, NULL);
267
268 /* pop the expected metatable. */
269 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
270
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100271 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100272 ret = lua_rawequal(L, -1, -2);
273 lua_pop(L, 2);
274 if (!ret)
275 luaL_argerror(L, ud, NULL);
276
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100277 /* Push on the stack at the entry [0] of the table. */
278 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100279
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100280 /* Check if this entry is userdata. */
281 p = lua_touserdata(L, -1);
282 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100283 luaL_argerror(L, ud, NULL);
284
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100285 /* Remove the entry returned by lua_rawgeti(). */
286 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100287
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100288 /* Return the associated struct. */
289 return p;
290}
291
Thierry Fournierb1f46562016-01-21 09:46:15 +0100292/* This function return the current date at epoch format in milliseconds. */
293int hlua_now(lua_State *L)
294{
295 lua_newtable(L);
296 lua_pushstring(L, "sec");
297 lua_pushinteger(L, now.tv_sec);
298 lua_rawset(L, -3);
299 lua_pushstring(L, "usec");
300 lua_pushinteger(L, now.tv_usec);
301 lua_rawset(L, -3);
302 return 1;
303}
304
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100305/* This functions expects a Lua string as HTTP date, parse it and
306 * returns an integer containing the epoch format of the date, or
307 * nil if the parsing fails.
308 */
309static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
310{
311 const char *str;
312 size_t len;
313 struct tm tm;
314 time_t time;
315
316 str = luaL_checklstring(L, 1, &len);
317
318 if (!fcn(str, len, &tm)) {
319 lua_pushnil(L);
320 return 1;
321 }
322
323 /* This function considers the content of the broken-down time
324 * is exprimed in the UTC timezone. timegm don't care about
325 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
326 * the timezone from the broken-down time, it must be fixed
327 * after the conversion.
328 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200329 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100330 if (time == -1) {
331 lua_pushnil(L);
332 return 1;
333 }
334
335 lua_pushinteger(L, (int)time);
336 return 1;
337}
338static int hlua_http_date(lua_State *L)
339{
340 return hlua_parse_date(L, parse_http_date);
341}
342static int hlua_imf_date(lua_State *L)
343{
344 return hlua_parse_date(L, parse_imf_date);
345}
346static int hlua_rfc850_date(lua_State *L)
347{
348 return hlua_parse_date(L, parse_rfc850_date);
349}
350static int hlua_asctime_date(lua_State *L)
351{
352 return hlua_parse_date(L, parse_asctime_date);
353}
354
Thierry Fourniereea77c02016-03-18 08:47:13 +0100355static int hlua_get_info(lua_State *L)
356{
357 int i;
358
Willy Tarreau0b26b382021-05-08 07:43:53 +0200359 stats_fill_info(stats, STATS_LEN, 0);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100360
361 lua_newtable(L);
362 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200363 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100364 hlua_fcn_pushfield(L, &stats[i]);
365 lua_settable(L, -3);
366 }
367 return 1;
368}
369
Thierry Fournier49d48422016-02-19 12:09:29 +0100370static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100371{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200372 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100373}
374
375static int hlua_concat_add(lua_State *L)
376{
Thierry Fournier49d48422016-02-19 12:09:29 +0100377 struct hlua_concat *b;
378 char *buffer;
379 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100380 const char *str;
381 size_t l;
382
383 /* First arg must be a concat object. */
384 b = hlua_check_concat(L, 1);
385
386 /* Second arg must be a string. */
387 str = luaL_checklstring(L, 2, &l);
388
Thierry Fournier49d48422016-02-19 12:09:29 +0100389 /* Get the buffer. */
390 lua_rawgeti(L, 1, 1);
391 buffer = lua_touserdata(L, -1);
392 lua_pop(L, 1);
393
394 /* Update the buffer size if it s required. The old buffer
395 * is crushed by the new in the object array, so it will
396 * be deleted by the GC.
397 * Note that in the first loop, the "new" variable is only
398 * used as a flag.
399 */
400 new = NULL;
401 while (b->size - b->len < l) {
402 b->size += HLUA_CONCAT_BLOCSZ;
403 new = buffer;
404 }
405 if (new) {
406 new = lua_newuserdata(L, b->size);
407 memcpy(new, buffer, b->len);
408 lua_rawseti(L, 1, 1);
409 buffer = new;
410 }
411
412 /* Copy string, and update metadata. */
413 memcpy(buffer + b->len, str, l);
414 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100415 return 0;
416}
417
418static int hlua_concat_dump(lua_State *L)
419{
Thierry Fournier49d48422016-02-19 12:09:29 +0100420 struct hlua_concat *b;
421 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100422
423 /* First arg must be a concat object. */
424 b = hlua_check_concat(L, 1);
425
Thierry Fournier49d48422016-02-19 12:09:29 +0100426 /* Get the buffer. */
427 lua_rawgeti(L, 1, 1);
428 buffer = lua_touserdata(L, -1);
429 lua_pop(L, 1);
430
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500431 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100432 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100433 return 1;
434}
435
436int hlua_concat_new(lua_State *L)
437{
Thierry Fournier49d48422016-02-19 12:09:29 +0100438 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100439
440 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200441 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100442 b->size = HLUA_CONCAT_BLOCSZ;
443 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100444 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100445 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
446 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100447
448 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
449 lua_setmetatable(L, -2);
450
Thierry Fournier1de16592016-01-27 09:49:07 +0100451 return 1;
452}
453
454static int concat_tostring(lua_State *L)
455{
456 const void *ptr = lua_topointer(L, 1);
457 lua_pushfstring(L, "Concat object: %p", ptr);
458 return 1;
459}
460
461static int hlua_concat_init(lua_State *L)
462{
463 /* Creates the buffered concat object. */
464 lua_newtable(L);
465
466 lua_pushstring(L, "__tostring");
467 lua_pushcclosure(L, concat_tostring, 0);
468 lua_settable(L, -3);
469
470 lua_pushstring(L, "__index"); /* Creates the index entry. */
471 lua_newtable(L); /* The "__index" content. */
472
473 lua_pushstring(L, "add");
474 lua_pushcclosure(L, hlua_concat_add, 0);
475 lua_settable(L, -3);
476
477 lua_pushstring(L, "dump");
478 lua_pushcclosure(L, hlua_concat_dump, 0);
479 lua_settable(L, -3);
480
481 lua_settable(L, -3); /* Sets the __index entry. */
482 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
483
484 return 1;
485}
486
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200487int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
488{
489 lua_newtable(L);
490
491 /* Pop a class stktbl metatable and affect it to the userdata. */
492 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
493 lua_setmetatable(L, -2);
494
495 lua_pushlightuserdata(L, tbl);
496 lua_rawseti(L, -2, 0);
497 return 1;
498}
499
500static struct stktable *hlua_check_stktable(lua_State *L, int ud)
501{
502 return hlua_checkudata(L, ud, class_stktable_ref);
503}
504
505/* Extract stick table attributes into Lua table */
506int hlua_stktable_info(lua_State *L)
507{
508 struct stktable *tbl;
509 int dt;
510
511 tbl = hlua_check_stktable(L, 1);
512
513 if (!tbl->id) {
514 lua_pushnil(L);
515 return 1;
516 }
517
518 lua_newtable(L);
519
520 lua_pushstring(L, "type");
521 lua_pushstring(L, stktable_types[tbl->type].kw);
522 lua_settable(L, -3);
523
524 lua_pushstring(L, "length");
525 lua_pushinteger(L, tbl->key_size);
526 lua_settable(L, -3);
527
528 lua_pushstring(L, "size");
529 hlua_fcn_pushunsigned(L, tbl->size);
530 lua_settable(L, -3);
531
532 lua_pushstring(L, "used");
533 hlua_fcn_pushunsigned(L, tbl->current);
534 lua_settable(L, -3);
535
536 lua_pushstring(L, "nopurge");
537 lua_pushboolean(L, tbl->nopurge > 0);
538 lua_settable(L, -3);
539
540 lua_pushstring(L, "expire");
541 lua_pushinteger(L, tbl->expire);
542 lua_settable(L, -3);
543
544 /* Save data types periods (if applicable) in 'data' table */
545 lua_pushstring(L, "data");
546 lua_newtable(L);
547
548 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
549 if (tbl->data_ofs[dt] == 0)
550 continue;
551
552 lua_pushstring(L, stktable_data_types[dt].name);
553
554 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
555 lua_pushinteger(L, tbl->data_arg[dt].u);
556 else
557 lua_pushinteger(L, -1);
558
559 lua_settable(L, -3);
560 }
561
562 lua_settable(L, -3);
563
564 return 1;
565}
566
567/* Helper to get extract stick table entry into Lua table */
568static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
569{
570 int dt;
571 void *ptr;
572
573 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
574
575 if (t->data_ofs[dt] == 0)
576 continue;
577
578 lua_pushstring(L, stktable_data_types[dt].name);
579
580 ptr = stktable_data_ptr(t, ts, dt);
581 switch (stktable_data_types[dt].std_type) {
582 case STD_T_SINT:
583 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
584 break;
585 case STD_T_UINT:
586 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
587 break;
588 case STD_T_ULL:
589 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
590 break;
591 case STD_T_FRQP:
592 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
593 t->data_arg[dt].u));
594 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200595 case STD_T_DICT: {
596 struct dict_entry *de;
597 de = stktable_data_cast(ptr, std_t_dict);
598 lua_pushstring(L, de ? (char *)de->value.key : "-");
599 break;
600 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200601 }
602
603 lua_settable(L, -3);
604 }
605}
606
607/* Looks in table <t> for a sticky session matching key <key>
608 * Returns table with session data or nil
609 *
610 * The returned table always contains 'use' and 'expire' (integer) fields.
611 * For frequency/rate counters, each data entry is returned as table with
612 * 'value' and 'period' fields.
613 */
614int hlua_stktable_lookup(lua_State *L)
615{
616 struct stktable *t;
617 struct sample smp;
618 struct stktable_key *skey;
619 struct stksess *ts;
620
621 t = hlua_check_stktable(L, 1);
622 smp.data.type = SMP_T_STR;
623 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600624 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200625
626 skey = smp_to_stkey(&smp, t);
627 if (!skey) {
628 lua_pushnil(L);
629 return 1;
630 }
631
632 ts = stktable_lookup_key(t, skey);
633 if (!ts) {
634 lua_pushnil(L);
635 return 1;
636 }
637
638 lua_newtable(L);
639 lua_pushstring(L, "use");
640 lua_pushinteger(L, ts->ref_cnt - 1);
641 lua_settable(L, -3);
642
643 lua_pushstring(L, "expire");
644 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
645 lua_settable(L, -3);
646
647 hlua_stktable_entry(L, t, ts);
648 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
649 ts->ref_cnt--;
650 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
651
652 return 1;
653}
654
655struct stk_filter {
656 long long val;
657 int type;
658 int op;
659};
660
661
662/* Helper for returning errors to callers using Lua convention (nil, err) */
663static int hlua_error(lua_State *L, const char *fmt, ...) {
664 char buf[256];
665 int len;
666 va_list args;
667 va_start(args, fmt);
668 len = vsnprintf(buf, sizeof(buf), fmt, args);
669 va_end(args);
670
671 if (len < 0) {
672 ha_alert("hlua_error(): Could not write error message.\n");
673 lua_pushnil(L);
674 return 1;
675 } else if (len >= sizeof(buf))
676 ha_alert("hlua_error(): Error message was truncated.\n");
677
678 lua_pushnil(L);
679 lua_pushstring(L, buf);
680
681 return 2;
682}
683
684/* Dump the contents of stick table <t>*/
685int hlua_stktable_dump(lua_State *L)
686{
687 struct stktable *t;
688 struct ebmb_node *eb;
689 struct ebmb_node *n;
690 struct stksess *ts;
691 int type;
692 int op;
693 int dt;
694 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100695 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200696 int filter_count = 0;
697 int i;
698 int skip_entry;
699 void *ptr;
700
701 t = hlua_check_stktable(L, 1);
702 type = lua_type(L, 2);
703
704 switch (type) {
705 case LUA_TNONE:
706 case LUA_TNIL:
707 break;
708 case LUA_TTABLE:
709 lua_pushnil(L);
710 while (lua_next(L, 2) != 0) {
711 int entry_idx = 0;
712
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100713 if (filter_count >= STKTABLE_FILTER_LEN)
714 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200715
716 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
717 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
718
719 lua_pushnil(L);
720 while (lua_next(L, -2) != 0) {
721 switch (entry_idx) {
722 case 0:
723 if (lua_type(L, -1) != LUA_TSTRING)
724 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
725
726 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
727 if (dt < 0 || t->data_ofs[dt] == 0)
728 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
729 filter[filter_count].type = dt;
730 break;
731 case 1:
732 if (lua_type(L, -1) != LUA_TSTRING)
733 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
734
735 op = get_std_op(lua_tostring(L, -1));
736 if (op < 0)
737 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
738 filter[filter_count].op = op;
739 break;
740 case 2:
741 val = lua_tointeger(L, -1);
742 filter[filter_count].val = val;
743 filter_count++;
744 break;
745 default:
746 break;
747 }
748 entry_idx++;
749 lua_pop(L, 1);
750 }
751 lua_pop(L, 1);
752 }
753 break;
754 default:
755 return hlua_error(L, "filter table expected");
756 }
757
758 lua_newtable(L);
759
760 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
761 eb = ebmb_first(&t->keys);
762 for (n = eb; n; n = ebmb_next(n)) {
763 ts = ebmb_entry(n, struct stksess, key);
764 if (!ts) {
765 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
766 return 1;
767 }
768 ts->ref_cnt++;
769 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
770
771 /* multi condition/value filter */
772 skip_entry = 0;
773 for (i = 0; i < filter_count; i++) {
774 if (t->data_ofs[filter[i].type] == 0)
775 continue;
776
777 ptr = stktable_data_ptr(t, ts, filter[i].type);
778
779 switch (stktable_data_types[filter[i].type].std_type) {
780 case STD_T_SINT:
781 val = stktable_data_cast(ptr, std_t_sint);
782 break;
783 case STD_T_UINT:
784 val = stktable_data_cast(ptr, std_t_uint);
785 break;
786 case STD_T_ULL:
787 val = stktable_data_cast(ptr, std_t_ull);
788 break;
789 case STD_T_FRQP:
790 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
791 t->data_arg[filter[i].type].u);
792 break;
793 default:
794 continue;
795 break;
796 }
797
798 op = filter[i].op;
799
800 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
801 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
802 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
803 skip_entry = 1;
804 break;
805 }
806 }
807
808 if (skip_entry) {
809 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
810 ts->ref_cnt--;
811 continue;
812 }
813
814 if (t->type == SMP_T_IPV4) {
815 char addr[INET_ADDRSTRLEN];
816 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
817 lua_pushstring(L, addr);
818 } else if (t->type == SMP_T_IPV6) {
819 char addr[INET6_ADDRSTRLEN];
820 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
821 lua_pushstring(L, addr);
822 } else if (t->type == SMP_T_SINT) {
823 lua_pushinteger(L, *ts->key.key);
824 } else if (t->type == SMP_T_STR) {
825 lua_pushstring(L, (const char *)ts->key.key);
826 } else {
827 return hlua_error(L, "Unsupported stick table key type");
828 }
829
830 lua_newtable(L);
831 hlua_stktable_entry(L, t, ts);
832 lua_settable(L, -3);
833 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
834 ts->ref_cnt--;
835 }
836 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
837
838 return 1;
839}
840
Thierry Fournierff480422016-02-25 08:36:46 +0100841int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
842{
843 lua_newtable(L);
844
845 /* Pop a class sesison metatable and affect it to the userdata. */
846 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
847 lua_setmetatable(L, -2);
848
849 lua_pushlightuserdata(L, lst);
850 lua_rawseti(L, -2, 0);
851 return 1;
852}
853
854static struct listener *hlua_check_listener(lua_State *L, int ud)
855{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200856 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100857}
858
859int hlua_listener_get_stats(lua_State *L)
860{
861 struct listener *li;
862 int i;
863
864 li = hlua_check_listener(L, 1);
865
Willy Tarreauc95bad52016-12-22 00:13:31 +0100866 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100867 lua_pushnil(L);
868 return 1;
869 }
870
William Dauchy655e14e2021-02-14 23:22:54 +0100871 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats,
872 STATS_LEN, NULL);
Thierry Fournierff480422016-02-25 08:36:46 +0100873
874 lua_newtable(L);
875 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200876 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100877 hlua_fcn_pushfield(L, &stats[i]);
878 lua_settable(L, -3);
879 }
880 return 1;
881
882}
883
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100884int hlua_fcn_new_server(lua_State *L, struct server *srv)
885{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400886 char buffer[12];
887
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100888 lua_newtable(L);
889
890 /* Pop a class sesison metatable and affect it to the userdata. */
891 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
892 lua_setmetatable(L, -2);
893
894 lua_pushlightuserdata(L, srv);
895 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400896
897 /* Add server name. */
898 lua_pushstring(L, "name");
899 lua_pushstring(L, srv->id);
900 lua_settable(L, -3);
901
902 /* Add server puid. */
903 lua_pushstring(L, "puid");
904 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
905 lua_pushstring(L, buffer);
906 lua_settable(L, -3);
907
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100908 return 1;
909}
910
911static struct server *hlua_check_server(lua_State *L, int ud)
912{
Amaury Denoyelle86f37072021-08-23 14:06:31 +0200913 struct server *srv = hlua_checkudata(L, ud, class_server_ref);
914 srv->flags |= SRV_F_NON_PURGEABLE;
915 return srv;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100916}
917
918int hlua_server_get_stats(lua_State *L)
919{
920 struct server *srv;
921 int i;
922
923 srv = hlua_check_server(L, 1);
924
925 if (!srv->proxy) {
926 lua_pushnil(L);
927 return 1;
928 }
929
William Dauchyd3a9a492021-01-25 17:29:03 +0100930 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats,
931 STATS_LEN, NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100932
933 lua_newtable(L);
934 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200935 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100936 hlua_fcn_pushfield(L, &stats[i]);
937 lua_settable(L, -3);
938 }
939 return 1;
940
941}
942
943int hlua_server_get_addr(lua_State *L)
944{
945 struct server *srv;
946 char addr[INET6_ADDRSTRLEN];
947 luaL_Buffer b;
948
949 srv = hlua_check_server(L, 1);
950
951 luaL_buffinit(L, &b);
952
953 switch (srv->addr.ss_family) {
954 case AF_INET:
955 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
956 addr, INET_ADDRSTRLEN);
957 luaL_addstring(&b, addr);
958 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400959 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100960 luaL_addstring(&b, addr);
961 break;
962 case AF_INET6:
963 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400964 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100965 luaL_addstring(&b, addr);
966 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400967 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100968 luaL_addstring(&b, addr);
969 break;
970 case AF_UNIX:
971 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
972 break;
973 default:
974 luaL_addstring(&b, "<unknown>");
975 break;
976 }
977
978 luaL_pushresult(&b);
979 return 1;
980}
981
982int hlua_server_is_draining(lua_State *L)
983{
984 struct server *srv;
985
986 srv = hlua_check_server(L, 1);
987 lua_pushinteger(L, server_is_draining(srv));
988 return 1;
989}
990
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400991int hlua_server_set_maxconn(lua_State *L)
992{
993 struct server *srv;
994 const char *maxconn;
995 const char *err;
996
997 srv = hlua_check_server(L, 1);
998 maxconn = luaL_checkstring(L, 2);
999
1000 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1001 err = server_parse_maxconn_change_request(srv, maxconn);
1002 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1003 if (!err)
1004 lua_pushnil(L);
1005 else
1006 hlua_pushstrippedstring(L, err);
1007 return 1;
1008}
1009
1010int hlua_server_get_maxconn(lua_State *L)
1011{
1012 struct server *srv;
1013
1014 srv = hlua_check_server(L, 1);
1015 lua_pushinteger(L, srv->maxconn);
1016 return 1;
1017}
1018
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001019int hlua_server_set_weight(lua_State *L)
1020{
1021 struct server *srv;
1022 const char *weight;
1023 const char *err;
1024
1025 srv = hlua_check_server(L, 1);
1026 weight = luaL_checkstring(L, 2);
1027
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001028 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001029 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001030 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001031 if (!err)
1032 lua_pushnil(L);
1033 else
1034 hlua_pushstrippedstring(L, err);
1035 return 1;
1036}
1037
1038int hlua_server_get_weight(lua_State *L)
1039{
1040 struct server *srv;
1041
1042 srv = hlua_check_server(L, 1);
1043 lua_pushinteger(L, srv->uweight);
1044 return 1;
1045}
1046
1047int hlua_server_set_addr(lua_State *L)
1048{
1049 struct server *srv;
1050 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001051 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001052 const char *err;
1053
1054 srv = hlua_check_server(L, 1);
1055 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001056 if (lua_gettop(L) >= 3)
1057 port = luaL_checkstring(L, 3);
1058 else
1059 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001060
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001061 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01001062 err = srv_update_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001063 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001064 if (!err)
1065 lua_pushnil(L);
1066 else
1067 hlua_pushstrippedstring(L, err);
1068 return 1;
1069}
1070
1071int hlua_server_shut_sess(lua_State *L)
1072{
1073 struct server *srv;
1074
1075 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001076 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001077 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001078 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001079 return 0;
1080}
1081
1082int hlua_server_set_drain(lua_State *L)
1083{
1084 struct server *srv;
1085
1086 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001087 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001088 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001089 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001090 return 0;
1091}
1092
1093int hlua_server_set_maint(lua_State *L)
1094{
1095 struct server *srv;
1096
1097 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001098 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001099 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001100 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001101 return 0;
1102}
1103
1104int hlua_server_set_ready(lua_State *L)
1105{
1106 struct server *srv;
1107
1108 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001109 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001110 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001111 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001112 return 0;
1113}
1114
1115int hlua_server_check_enable(lua_State *L)
1116{
1117 struct server *sv;
1118
1119 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001120 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001121 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001122 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001123 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001124 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001125 return 0;
1126}
1127
1128int hlua_server_check_disable(lua_State *L)
1129{
1130 struct server *sv;
1131
1132 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001133 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001134 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001135 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001136 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001137 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001138 return 0;
1139}
1140
1141int hlua_server_check_force_up(lua_State *L)
1142{
1143 struct server *sv;
1144
1145 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001146 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001147 if (!(sv->track)) {
1148 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001149 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001150 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001151 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001152 return 0;
1153}
1154
1155int hlua_server_check_force_nolb(lua_State *L)
1156{
1157 struct server *sv;
1158
1159 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001160 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001161 if (!(sv->track)) {
1162 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001163 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001164 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001165 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001166 return 0;
1167}
1168
1169int hlua_server_check_force_down(lua_State *L)
1170{
1171 struct server *sv;
1172
1173 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001174 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001175 if (!(sv->track)) {
1176 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001177 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001178 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001179 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001180 return 0;
1181}
1182
1183int hlua_server_agent_enable(lua_State *L)
1184{
1185 struct server *sv;
1186
1187 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001188 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001189 if (sv->agent.state & CHK_ST_CONFIGURED) {
1190 sv->agent.state |= CHK_ST_ENABLED;
1191 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001192 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001193 return 0;
1194}
1195
1196int hlua_server_agent_disable(lua_State *L)
1197{
1198 struct server *sv;
1199
1200 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001201 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001202 if (sv->agent.state & CHK_ST_CONFIGURED) {
1203 sv->agent.state &= ~CHK_ST_ENABLED;
1204 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001205 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001206 return 0;
1207}
1208
1209int hlua_server_agent_force_up(lua_State *L)
1210{
1211 struct server *sv;
1212
1213 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001214 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001215 if (sv->agent.state & CHK_ST_ENABLED) {
1216 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001217 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001218 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001219 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001220 return 0;
1221}
1222
1223int hlua_server_agent_force_down(lua_State *L)
1224{
1225 struct server *sv;
1226
1227 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001228 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001229 if (sv->agent.state & CHK_ST_ENABLED) {
1230 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001231 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001232 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001233 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001234 return 0;
1235}
1236
Thierry Fournierf61aa632016-02-19 20:56:00 +01001237int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1238{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001239 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001240 struct listener *lst;
1241 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001242 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001243
Thierry Fournierf61aa632016-02-19 20:56:00 +01001244 lua_newtable(L);
1245
1246 /* Pop a class sesison metatable and affect it to the userdata. */
1247 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1248 lua_setmetatable(L, -2);
1249
1250 lua_pushlightuserdata(L, px);
1251 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001252
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001253 /* Add proxy name. */
1254 lua_pushstring(L, "name");
1255 lua_pushstring(L, px->id);
1256 lua_settable(L, -3);
1257
Baptiste Assmann46c72552017-10-26 21:51:58 +02001258 /* Add proxy uuid. */
1259 lua_pushstring(L, "uuid");
1260 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1261 lua_pushstring(L, buffer);
1262 lua_settable(L, -3);
1263
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001264 /* Browse and register servers. */
1265 lua_pushstring(L, "servers");
1266 lua_newtable(L);
1267 for (srv = px->srv; srv; srv = srv->next) {
1268 lua_pushstring(L, srv->id);
1269 hlua_fcn_new_server(L, srv);
1270 lua_settable(L, -3);
1271 }
1272 lua_settable(L, -3);
1273
Thierry Fournierff480422016-02-25 08:36:46 +01001274 /* Browse and register listeners. */
1275 lua_pushstring(L, "listeners");
1276 lua_newtable(L);
1277 lid = 1;
1278 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1279 if (lst->name)
1280 lua_pushstring(L, lst->name);
1281 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001282 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001283 lid++;
1284 lua_pushstring(L, buffer);
1285 }
1286 hlua_fcn_new_listener(L, lst);
1287 lua_settable(L, -3);
1288 }
1289 lua_settable(L, -3);
1290
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001291 if (px->table && px->table->id) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001292 lua_pushstring(L, "stktable");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001293 hlua_fcn_new_stktable(L, px->table);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001294 lua_settable(L, -3);
1295 }
1296
Thierry Fournierf61aa632016-02-19 20:56:00 +01001297 return 1;
1298}
1299
1300static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1301{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001302 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001303}
1304
1305int hlua_proxy_pause(lua_State *L)
1306{
1307 struct proxy *px;
1308
1309 px = hlua_check_proxy(L, 1);
1310 pause_proxy(px);
1311 return 0;
1312}
1313
1314int hlua_proxy_resume(lua_State *L)
1315{
1316 struct proxy *px;
1317
1318 px = hlua_check_proxy(L, 1);
1319 resume_proxy(px);
1320 return 0;
1321}
1322
1323int hlua_proxy_stop(lua_State *L)
1324{
1325 struct proxy *px;
1326
1327 px = hlua_check_proxy(L, 1);
1328 stop_proxy(px);
1329 return 0;
1330}
1331
1332int hlua_proxy_get_cap(lua_State *L)
1333{
1334 struct proxy *px;
1335 const char *str;
1336
1337 px = hlua_check_proxy(L, 1);
1338 str = proxy_cap_str(px->cap);
1339 lua_pushstring(L, str);
1340 return 1;
1341}
1342
1343int hlua_proxy_get_stats(lua_State *L)
1344{
1345 struct proxy *px;
1346 int i;
1347
1348 px = hlua_check_proxy(L, 1);
1349 if (px->cap & PR_CAP_BE)
William Dauchyda3b4662021-01-25 17:29:01 +01001350 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001351 else
William Dauchy0ef54392021-01-17 18:27:45 +01001352 stats_fill_fe_stats(px, stats, STATS_LEN, NULL);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001353 lua_newtable(L);
1354 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001355 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001356 hlua_fcn_pushfield(L, &stats[i]);
1357 lua_settable(L, -3);
1358 }
1359 return 1;
1360}
1361
1362int hlua_proxy_get_mode(lua_State *L)
1363{
1364 struct proxy *px;
1365 const char *str;
1366
1367 px = hlua_check_proxy(L, 1);
1368 str = proxy_mode_str(px->mode);
1369 lua_pushstring(L, str);
1370 return 1;
1371}
1372
1373int hlua_proxy_shut_bcksess(lua_State *L)
1374{
1375 struct proxy *px;
1376
1377 px = hlua_check_proxy(L, 1);
1378 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1379 return 0;
1380}
1381
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001382int hlua_fcn_post_init(lua_State *L)
1383{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001384 struct proxy *px;
1385
1386 /* get core array. */
1387 if (lua_getglobal(L, "core") != LUA_TTABLE)
1388 lua_error(L);
1389
1390 /* Create proxies entry. */
1391 lua_pushstring(L, "proxies");
1392 lua_newtable(L);
1393
1394 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001395 for (px = proxies_list; px; px = px->next) {
Thierry Fournierf61aa632016-02-19 20:56:00 +01001396 lua_pushstring(L, px->id);
1397 hlua_fcn_new_proxy(L, px);
1398 lua_settable(L, -3);
1399 }
1400
1401 /* push "proxies" in "core" */
1402 lua_settable(L, -3);
1403
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001404 /* Create proxies entry. */
1405 lua_pushstring(L, "frontends");
1406 lua_newtable(L);
1407
1408 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001409 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001410 if (!(px->cap & PR_CAP_FE))
1411 continue;
1412 lua_pushstring(L, px->id);
1413 hlua_fcn_new_proxy(L, px);
1414 lua_settable(L, -3);
1415 }
1416
1417 /* push "frontends" in "core" */
1418 lua_settable(L, -3);
1419
1420 /* Create proxies entry. */
1421 lua_pushstring(L, "backends");
1422 lua_newtable(L);
1423
1424 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001425 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001426 if (!(px->cap & PR_CAP_BE))
1427 continue;
1428 lua_pushstring(L, px->id);
1429 hlua_fcn_new_proxy(L, px);
1430 lua_settable(L, -3);
1431 }
1432
1433 /* push "backend" in "core" */
1434 lua_settable(L, -3);
1435
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001436 return 1;
1437}
1438
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001439/* This Lua function take a string, a list of separators.
1440 * It tokenize the input string using the list of separators
1441 * as separator.
1442 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001443 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001444 */
1445int hlua_tokenize(lua_State *L)
1446{
1447 const char *str;
1448 const char *sep;
1449 int index;
1450 const char *token;
1451 const char *p;
1452 const char *c;
1453 int ignore_empty;
1454
1455 ignore_empty = 0;
1456
1457 str = luaL_checkstring(L, 1);
1458 sep = luaL_checkstring(L, 2);
1459 if (lua_gettop(L) == 3)
1460 ignore_empty = hlua_checkboolean(L, 3);
1461
1462 lua_newtable(L);
1463 index = 1;
1464 token = str;
1465 p = str;
1466 while(1) {
1467 for (c = sep; *c != '\0'; c++)
1468 if (*p == *c)
1469 break;
1470 if (*p == *c) {
1471 if ((!ignore_empty) || (p - token > 0)) {
1472 lua_pushlstring(L, token, p - token);
1473 lua_rawseti(L, -2, index);
1474 index++;
1475 }
1476 token = p + 1;
1477 }
1478 if (*p == '\0')
1479 break;
1480 p++;
1481 }
1482
1483 return 1;
1484}
1485
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001486int hlua_parse_addr(lua_State *L)
1487{
Christopher Faulet29e93262021-02-26 09:39:05 +01001488 struct net_addr *addr;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001489 const char *str = luaL_checkstring(L, 1);
1490 unsigned char mask;
1491
Christopher Faulet29e93262021-02-26 09:39:05 +01001492 addr = lua_newuserdata(L, sizeof(struct net_addr));
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001493 if (!addr) {
1494 lua_pushnil(L);
1495 return 1;
1496 }
1497
1498 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
Christopher Faulet29e93262021-02-26 09:39:05 +01001499 addr->family = AF_INET;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001500 return 1;
1501 }
1502
1503 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1504 len2mask6(mask, &addr->addr.v6.mask);
Christopher Faulet29e93262021-02-26 09:39:05 +01001505 addr->family = AF_INET6;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001506 return 1;
1507 }
1508
1509 lua_pop(L, 1);
1510 lua_pushnil(L);
1511 return 1;
1512}
1513
1514int hlua_match_addr(lua_State *L)
1515{
Christopher Faulet29e93262021-02-26 09:39:05 +01001516 struct net_addr *addr1;
1517 struct net_addr *addr2;
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001518
1519 if (!lua_isuserdata(L, 1) ||
1520 !lua_isuserdata(L, 2)) {
1521 lua_pushboolean(L, 0);
1522 return 1;
1523 }
1524
1525 addr1 = lua_touserdata(L, 1);
1526 addr2 = lua_touserdata(L, 2);
1527
Christopher Faulet29e93262021-02-26 09:39:05 +01001528 if (addr1->family != addr2->family) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001529 lua_pushboolean(L, 0);
1530 return 1;
1531 }
1532
Christopher Faulet29e93262021-02-26 09:39:05 +01001533 if (addr1->family == AF_INET) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001534 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1535 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1536 lua_pushboolean(L, 1);
1537 return 1;
1538 }
1539 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001540 int i;
1541
1542 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001543 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1544 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1545 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1546 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001547 break;
1548 }
1549 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001550 lua_pushboolean(L, 1);
1551 return 1;
1552 }
1553 }
1554
1555 lua_pushboolean(L, 0);
1556 return 1;
1557}
1558
Dragan Dosen26743032019-04-30 15:54:36 +02001559static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001560{
1561 return (hlua_checkudata(L, ud, class_regex_ref));
1562}
1563
1564static int hlua_regex_comp(struct lua_State *L)
1565{
Dragan Dosen26743032019-04-30 15:54:36 +02001566 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001567 const char *str;
1568 int cs;
1569 char *err;
1570
1571 str = luaL_checkstring(L, 1);
1572 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1573 cs = lua_toboolean(L, 2);
1574
1575 regex = lua_newuserdata(L, sizeof(*regex));
1576
1577 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001578 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001579 lua_pushboolean(L, 0); /* status error */
1580 lua_pushstring(L, err); /* Reason */
1581 free(err);
1582 return 2;
1583 }
1584
1585 lua_pushboolean(L, 1); /* Status ok */
1586
1587 /* Create object */
1588 lua_newtable(L);
1589 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1590 lua_rawseti(L, -2, 0);
1591 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1592 lua_setmetatable(L, -2);
1593 return 2;
1594}
1595
1596static int hlua_regex_exec(struct lua_State *L)
1597{
Dragan Dosen26743032019-04-30 15:54:36 +02001598 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001599 const char *str;
1600 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001601 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001602
1603 regex = hlua_check_regex(L, 1);
1604 str = luaL_checklstring(L, 2, &len);
1605
Dragan Dosen26743032019-04-30 15:54:36 +02001606 if (!*regex) {
1607 lua_pushboolean(L, 0);
1608 return 1;
1609 }
1610
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001611 /* Copy the string because regex_exec2 require a 'char *'
1612 * and not a 'const char *'.
1613 */
1614 tmp = get_trash_chunk();
1615 if (len >= tmp->size) {
1616 lua_pushboolean(L, 0);
1617 return 1;
1618 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001619 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001620
Dragan Dosen26743032019-04-30 15:54:36 +02001621 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001622
1623 return 1;
1624}
1625
1626static int hlua_regex_match(struct lua_State *L)
1627{
Dragan Dosen26743032019-04-30 15:54:36 +02001628 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001629 const char *str;
1630 size_t len;
1631 regmatch_t pmatch[20];
1632 int ret;
1633 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001634 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001635
1636 regex = hlua_check_regex(L, 1);
1637 str = luaL_checklstring(L, 2, &len);
1638
Dragan Dosen26743032019-04-30 15:54:36 +02001639 if (!*regex) {
1640 lua_pushboolean(L, 0);
1641 return 1;
1642 }
1643
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001644 /* Copy the string because regex_exec2 require a 'char *'
1645 * and not a 'const char *'.
1646 */
1647 tmp = get_trash_chunk();
1648 if (len >= tmp->size) {
1649 lua_pushboolean(L, 0);
1650 return 1;
1651 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001652 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001653
Dragan Dosen26743032019-04-30 15:54:36 +02001654 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001655 lua_pushboolean(L, ret);
1656 lua_newtable(L);
1657 if (ret) {
1658 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1659 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1660 lua_rawseti(L, -2, i + 1);
1661 }
1662 }
1663 return 2;
1664}
1665
1666static int hlua_regex_free(struct lua_State *L)
1667{
Dragan Dosen26743032019-04-30 15:54:36 +02001668 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001669
1670 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001671 regex_free(*regex);
1672 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001673 return 0;
1674}
1675
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001676int hlua_fcn_reg_core_fcn(lua_State *L)
1677{
Thierry Fournier1de16592016-01-27 09:49:07 +01001678 if (!hlua_concat_init(L))
1679 return 0;
1680
Thierry Fournier4f99b272016-02-22 08:40:02 +01001681 hlua_class_function(L, "now", hlua_now);
1682 hlua_class_function(L, "http_date", hlua_http_date);
1683 hlua_class_function(L, "imf_date", hlua_imf_date);
1684 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1685 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1686 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001687 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001688 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1689 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001690 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001691
Thierry FOURNIER31904272017-10-25 12:59:51 +02001692 /* Create regex object. */
1693 lua_newtable(L);
1694 hlua_class_function(L, "new", hlua_regex_comp);
1695
1696 lua_newtable(L); /* The metatable. */
1697 lua_pushstring(L, "__index");
1698 lua_newtable(L);
1699 hlua_class_function(L, "exec", hlua_regex_exec);
1700 hlua_class_function(L, "match", hlua_regex_match);
1701 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1702 hlua_class_function(L, "__gc", hlua_regex_free);
1703
1704 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1705 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1706
1707 lua_setmetatable(L, -2);
1708 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1709
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001710 /* Create stktable object. */
1711 lua_newtable(L);
1712 lua_pushstring(L, "__index");
1713 lua_newtable(L);
1714 hlua_class_function(L, "info", hlua_stktable_info);
1715 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1716 hlua_class_function(L, "dump", hlua_stktable_dump);
1717 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1718 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1719
Thierry Fournierff480422016-02-25 08:36:46 +01001720 /* Create listener object. */
1721 lua_newtable(L);
1722 lua_pushstring(L, "__index");
1723 lua_newtable(L);
1724 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1725 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1726 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1727
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001728 /* Create server object. */
1729 lua_newtable(L);
1730 lua_pushstring(L, "__index");
1731 lua_newtable(L);
1732 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001733 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1734 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001735 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1736 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1737 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1738 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1739 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1740 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1741 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1742 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1743 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1744 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1745 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1746 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1747 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1748 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1749 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1750 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1751 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1752 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1753 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1754 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1755
Thierry Fournierf61aa632016-02-19 20:56:00 +01001756 /* Create proxy object. */
1757 lua_newtable(L);
1758 lua_pushstring(L, "__index");
1759 lua_newtable(L);
1760 hlua_class_function(L, "pause", hlua_proxy_pause);
1761 hlua_class_function(L, "resume", hlua_proxy_resume);
1762 hlua_class_function(L, "stop", hlua_proxy_stop);
1763 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1764 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1765 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1766 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1767 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1768 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1769
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001770 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001771}