blob: ed094b7e378c2475878311c1a274554c0114db43 [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 */
17#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Willy Tarreau83487a82020-06-04 20:19:54 +020021#include <haproxy/cli-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreau86416052020-06-04 09:20:54 +020023#include <haproxy/hlua-t.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020025#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020026#include <haproxy/pattern-t.h>
27#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020028#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020029#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020030#include <haproxy/stats.h>
Willy Tarreau872f2ea2020-06-04 18:46:44 +020031#include <haproxy/stick_table.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/time.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010033
Thierry Fournier1de16592016-01-27 09:49:07 +010034/* Contains the class reference of the concat object. */
35static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010036static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010037static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010038static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020039static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020040static int class_stktable_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010041
Thierry Fournierf61aa632016-02-19 20:56:00 +010042#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010043
Thierry FOURNIERffbad792017-07-12 11:39:04 +020044static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010045
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010046int hlua_checkboolean(lua_State *L, int index)
47{
48 if (!lua_isboolean(L, index))
49 luaL_argerror(L, index, "boolean expected");
50 return lua_toboolean(L, index);
51}
52
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020053/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
54static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
55{
56#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
57 lua_pushinteger(L, val);
58#else
59 if (val > INT_MAX)
60 lua_pushnumber(L, (lua_Number)val);
61 else
62 lua_pushinteger(L, (int)val);
63#endif
64 return 1;
65}
66
67/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
68static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
69#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
70 /* 64 bits case, U64 is supported until LLONG_MAX */
71 if (val > LLONG_MAX)
72 lua_pushnumber(L, (lua_Number)val);
73 else
74 lua_pushinteger(L, val);
75#else
76 /* 32 bits case, U64 is supported until INT_MAX */
77 if (val > INT_MAX)
78 lua_pushnumber(L, (lua_Number)val);
79 else
80 lua_pushinteger(L, (int)val);
81#endif
82 return 1;
83}
84
Joseph Herlantb3d92e32018-11-15 09:35:04 -080085/* This function gets a struct field and converts it in Lua
86 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010087 */
88int hlua_fcn_pushfield(lua_State *L, struct field *field)
89{
90 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -080091 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010092 * by some macros. Windows maros are not supported.
93 * If the number cannot be represented as integer, we try to
94 * convert to float.
95 */
96 switch (field_format(field, 0)) {
97
98 case FF_EMPTY:
99 lua_pushnil(L);
100 return 1;
101
102 case FF_S32:
103 /* S32 is always supported. */
104 lua_pushinteger(L, field->u.s32);
105 return 1;
106
107 case FF_U32:
108#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
109 /* 64 bits case, U32 is always supported */
110 lua_pushinteger(L, field->u.u32);
111#else
112 /* 32 bits case, U32 is supported until INT_MAX. */
113 if (field->u.u32 > INT_MAX)
114 lua_pushnumber(L, (lua_Number)field->u.u32);
115 else
116 lua_pushinteger(L, field->u.u32);
117#endif
118 return 1;
119
120 case FF_S64:
121#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
122 /* 64 bits case, S64 is always supported */
123 lua_pushinteger(L, field->u.s64);
124#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500125 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100126 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
127 lua_pushnumber(L, (lua_Number)field->u.s64);
128 else
129 lua_pushinteger(L, (int)field->u.s64);
130#endif
131 return 1;
132
133 case FF_U64:
134#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
135 /* 64 bits case, U64 is supported until LLONG_MAX */
136 if (field->u.u64 > LLONG_MAX)
137 lua_pushnumber(L, (lua_Number)field->u.u64);
138 else
139 lua_pushinteger(L, field->u.u64);
140#else
141 /* 64 bits case, U64 is supported until INT_MAX */
142 if (field->u.u64 > INT_MAX)
143 lua_pushnumber(L, (lua_Number)field->u.u64);
144 else
145 lua_pushinteger(L, (int)field->u.u64);
146#endif
147 return 1;
148
149 case FF_STR:
150 lua_pushstring(L, field->u.str);
151 return 1;
152
153 default:
154 break;
155 }
156
157 /* Default case, never reached. */
158 lua_pushnil(L);
159 return 1;
160}
161
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100162/* Some string are started or terminated by blank chars,
163 * this function removes the spaces, tabs, \r and
164 * \n at the begin and at the end of the string "str", and
165 * push the result in the lua stack.
166 * Returns a pointer to the Lua internal copy of the string.
167 */
168const char *hlua_pushstrippedstring(lua_State *L, const char *str)
169{
170 const char *p;
171 const char *e;
172
173 for (p = str; HTTP_IS_LWS(*p); p++);
174 for (e = p + strlen(p) - 1; e > p && HTTP_IS_LWS(*e); e--);
175
176 return lua_pushlstring(L, p, e - p);
177}
178
Thierry Fournierddd89882016-02-22 19:52:08 +0100179/* The three following functions are useful for adding entries
180 * in a table. These functions takes a string and respectively an
181 * integer, a string or a function and add it to the table in the
182 * top of the stack.
183 *
184 * These functions throws an error if no more stack size is
185 * available.
186 */
187void hlua_class_const_int(lua_State *L, const char *name, int value)
188{
Thierry Fournierddd89882016-02-22 19:52:08 +0100189 lua_pushstring(L, name);
190 lua_pushinteger(L, value);
191 lua_rawset(L, -3);
192}
193void hlua_class_const_str(lua_State *L, const char *name, const char *value)
194{
Thierry Fournierddd89882016-02-22 19:52:08 +0100195 lua_pushstring(L, name);
196 lua_pushstring(L, value);
197 lua_rawset(L, -3);
198}
199void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
200{
Thierry Fournierddd89882016-02-22 19:52:08 +0100201 lua_pushstring(L, name);
202 lua_pushcclosure(L, function, 0);
203 lua_rawset(L, -3);
204}
205
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800206/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100207int hlua_dump_object(struct lua_State *L)
208{
209 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
210 lua_pushfstring(L, "HAProxy class %s", name);
211 return 1;
212}
213
Thierry Fournier45e78d72016-02-19 18:34:46 +0100214/* This function register a table as metatable and. It names
215 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500216 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100217 * "name" is the referenced class name.
218 */
219int hlua_register_metatable(struct lua_State *L, char *name)
220{
221 /* Check the type of the top element. it must be
222 * a table.
223 */
224 if (lua_type(L, -1) != LUA_TTABLE)
225 luaL_error(L, "hlua_register_metatable() requires a type Table "
226 "in the top of the stack");
227
228 /* Add the __tostring function which identify the
229 * created object.
230 */
231 lua_pushstring(L, "__tostring");
232 lua_pushstring(L, name);
233 lua_pushcclosure(L, hlua_dump_object, 1);
234 lua_rawset(L, -3);
235
236 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500237 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100238 * lua_setfield() pop the entry.
239 */
240 lua_pushvalue(L, -1);
241 lua_setfield(L, LUA_REGISTRYINDEX, name);
242
243 /* Creates the reference of the object. The
244 * function luaL_ref pop the top of the stack.
245 */
246 return luaL_ref(L, LUA_REGISTRYINDEX);
247}
248
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100249/* Return an object of the expected type, or throws an error. */
250void *hlua_checkudata(lua_State *L, int ud, int class_ref)
251{
252 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100253 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100254
255 /* Check if the stack entry is an array. */
256 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100257 luaL_argerror(L, ud, NULL);
258
259 /* pop the metatable of the referencecd object. */
260 if (!lua_getmetatable(L, ud))
261 luaL_argerror(L, ud, NULL);
262
263 /* pop the expected metatable. */
264 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
265
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100266 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100267 ret = lua_rawequal(L, -1, -2);
268 lua_pop(L, 2);
269 if (!ret)
270 luaL_argerror(L, ud, NULL);
271
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100272 /* Push on the stack at the entry [0] of the table. */
273 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100274
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100275 /* Check if this entry is userdata. */
276 p = lua_touserdata(L, -1);
277 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100278 luaL_argerror(L, ud, NULL);
279
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100280 /* Remove the entry returned by lua_rawgeti(). */
281 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100282
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100283 /* Return the associated struct. */
284 return p;
285}
286
Thierry Fournierb1f46562016-01-21 09:46:15 +0100287/* This function return the current date at epoch format in milliseconds. */
288int hlua_now(lua_State *L)
289{
290 lua_newtable(L);
291 lua_pushstring(L, "sec");
292 lua_pushinteger(L, now.tv_sec);
293 lua_rawset(L, -3);
294 lua_pushstring(L, "usec");
295 lua_pushinteger(L, now.tv_usec);
296 lua_rawset(L, -3);
297 return 1;
298}
299
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100300/* This functions expects a Lua string as HTTP date, parse it and
301 * returns an integer containing the epoch format of the date, or
302 * nil if the parsing fails.
303 */
304static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
305{
306 const char *str;
307 size_t len;
308 struct tm tm;
309 time_t time;
310
311 str = luaL_checklstring(L, 1, &len);
312
313 if (!fcn(str, len, &tm)) {
314 lua_pushnil(L);
315 return 1;
316 }
317
318 /* This function considers the content of the broken-down time
319 * is exprimed in the UTC timezone. timegm don't care about
320 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
321 * the timezone from the broken-down time, it must be fixed
322 * after the conversion.
323 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200324 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100325 if (time == -1) {
326 lua_pushnil(L);
327 return 1;
328 }
329
330 lua_pushinteger(L, (int)time);
331 return 1;
332}
333static int hlua_http_date(lua_State *L)
334{
335 return hlua_parse_date(L, parse_http_date);
336}
337static int hlua_imf_date(lua_State *L)
338{
339 return hlua_parse_date(L, parse_imf_date);
340}
341static int hlua_rfc850_date(lua_State *L)
342{
343 return hlua_parse_date(L, parse_rfc850_date);
344}
345static int hlua_asctime_date(lua_State *L)
346{
347 return hlua_parse_date(L, parse_asctime_date);
348}
349
Thierry Fourniereea77c02016-03-18 08:47:13 +0100350static int hlua_get_info(lua_State *L)
351{
352 int i;
353
354 stats_fill_info(stats, STATS_LEN);
355
356 lua_newtable(L);
357 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200358 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100359 hlua_fcn_pushfield(L, &stats[i]);
360 lua_settable(L, -3);
361 }
362 return 1;
363}
364
Thierry Fournier49d48422016-02-19 12:09:29 +0100365static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100366{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200367 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100368}
369
370static int hlua_concat_add(lua_State *L)
371{
Thierry Fournier49d48422016-02-19 12:09:29 +0100372 struct hlua_concat *b;
373 char *buffer;
374 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100375 const char *str;
376 size_t l;
377
378 /* First arg must be a concat object. */
379 b = hlua_check_concat(L, 1);
380
381 /* Second arg must be a string. */
382 str = luaL_checklstring(L, 2, &l);
383
Thierry Fournier49d48422016-02-19 12:09:29 +0100384 /* Get the buffer. */
385 lua_rawgeti(L, 1, 1);
386 buffer = lua_touserdata(L, -1);
387 lua_pop(L, 1);
388
389 /* Update the buffer size if it s required. The old buffer
390 * is crushed by the new in the object array, so it will
391 * be deleted by the GC.
392 * Note that in the first loop, the "new" variable is only
393 * used as a flag.
394 */
395 new = NULL;
396 while (b->size - b->len < l) {
397 b->size += HLUA_CONCAT_BLOCSZ;
398 new = buffer;
399 }
400 if (new) {
401 new = lua_newuserdata(L, b->size);
402 memcpy(new, buffer, b->len);
403 lua_rawseti(L, 1, 1);
404 buffer = new;
405 }
406
407 /* Copy string, and update metadata. */
408 memcpy(buffer + b->len, str, l);
409 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100410 return 0;
411}
412
413static int hlua_concat_dump(lua_State *L)
414{
Thierry Fournier49d48422016-02-19 12:09:29 +0100415 struct hlua_concat *b;
416 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100417
418 /* First arg must be a concat object. */
419 b = hlua_check_concat(L, 1);
420
Thierry Fournier49d48422016-02-19 12:09:29 +0100421 /* Get the buffer. */
422 lua_rawgeti(L, 1, 1);
423 buffer = lua_touserdata(L, -1);
424 lua_pop(L, 1);
425
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500426 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100427 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100428 return 1;
429}
430
431int hlua_concat_new(lua_State *L)
432{
Thierry Fournier49d48422016-02-19 12:09:29 +0100433 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100434
435 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200436 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100437 b->size = HLUA_CONCAT_BLOCSZ;
438 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100439 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100440 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
441 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100442
443 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
444 lua_setmetatable(L, -2);
445
Thierry Fournier1de16592016-01-27 09:49:07 +0100446 return 1;
447}
448
449static int concat_tostring(lua_State *L)
450{
451 const void *ptr = lua_topointer(L, 1);
452 lua_pushfstring(L, "Concat object: %p", ptr);
453 return 1;
454}
455
456static int hlua_concat_init(lua_State *L)
457{
458 /* Creates the buffered concat object. */
459 lua_newtable(L);
460
461 lua_pushstring(L, "__tostring");
462 lua_pushcclosure(L, concat_tostring, 0);
463 lua_settable(L, -3);
464
465 lua_pushstring(L, "__index"); /* Creates the index entry. */
466 lua_newtable(L); /* The "__index" content. */
467
468 lua_pushstring(L, "add");
469 lua_pushcclosure(L, hlua_concat_add, 0);
470 lua_settable(L, -3);
471
472 lua_pushstring(L, "dump");
473 lua_pushcclosure(L, hlua_concat_dump, 0);
474 lua_settable(L, -3);
475
476 lua_settable(L, -3); /* Sets the __index entry. */
477 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
478
479 return 1;
480}
481
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200482int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
483{
484 lua_newtable(L);
485
486 /* Pop a class stktbl metatable and affect it to the userdata. */
487 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
488 lua_setmetatable(L, -2);
489
490 lua_pushlightuserdata(L, tbl);
491 lua_rawseti(L, -2, 0);
492 return 1;
493}
494
495static struct stktable *hlua_check_stktable(lua_State *L, int ud)
496{
497 return hlua_checkudata(L, ud, class_stktable_ref);
498}
499
500/* Extract stick table attributes into Lua table */
501int hlua_stktable_info(lua_State *L)
502{
503 struct stktable *tbl;
504 int dt;
505
506 tbl = hlua_check_stktable(L, 1);
507
508 if (!tbl->id) {
509 lua_pushnil(L);
510 return 1;
511 }
512
513 lua_newtable(L);
514
515 lua_pushstring(L, "type");
516 lua_pushstring(L, stktable_types[tbl->type].kw);
517 lua_settable(L, -3);
518
519 lua_pushstring(L, "length");
520 lua_pushinteger(L, tbl->key_size);
521 lua_settable(L, -3);
522
523 lua_pushstring(L, "size");
524 hlua_fcn_pushunsigned(L, tbl->size);
525 lua_settable(L, -3);
526
527 lua_pushstring(L, "used");
528 hlua_fcn_pushunsigned(L, tbl->current);
529 lua_settable(L, -3);
530
531 lua_pushstring(L, "nopurge");
532 lua_pushboolean(L, tbl->nopurge > 0);
533 lua_settable(L, -3);
534
535 lua_pushstring(L, "expire");
536 lua_pushinteger(L, tbl->expire);
537 lua_settable(L, -3);
538
539 /* Save data types periods (if applicable) in 'data' table */
540 lua_pushstring(L, "data");
541 lua_newtable(L);
542
543 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
544 if (tbl->data_ofs[dt] == 0)
545 continue;
546
547 lua_pushstring(L, stktable_data_types[dt].name);
548
549 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
550 lua_pushinteger(L, tbl->data_arg[dt].u);
551 else
552 lua_pushinteger(L, -1);
553
554 lua_settable(L, -3);
555 }
556
557 lua_settable(L, -3);
558
559 return 1;
560}
561
562/* Helper to get extract stick table entry into Lua table */
563static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
564{
565 int dt;
566 void *ptr;
567
568 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
569
570 if (t->data_ofs[dt] == 0)
571 continue;
572
573 lua_pushstring(L, stktable_data_types[dt].name);
574
575 ptr = stktable_data_ptr(t, ts, dt);
576 switch (stktable_data_types[dt].std_type) {
577 case STD_T_SINT:
578 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
579 break;
580 case STD_T_UINT:
581 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
582 break;
583 case STD_T_ULL:
584 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
585 break;
586 case STD_T_FRQP:
587 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
588 t->data_arg[dt].u));
589 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200590 case STD_T_DICT: {
591 struct dict_entry *de;
592 de = stktable_data_cast(ptr, std_t_dict);
593 lua_pushstring(L, de ? (char *)de->value.key : "-");
594 break;
595 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200596 }
597
598 lua_settable(L, -3);
599 }
600}
601
602/* Looks in table <t> for a sticky session matching key <key>
603 * Returns table with session data or nil
604 *
605 * The returned table always contains 'use' and 'expire' (integer) fields.
606 * For frequency/rate counters, each data entry is returned as table with
607 * 'value' and 'period' fields.
608 */
609int hlua_stktable_lookup(lua_State *L)
610{
611 struct stktable *t;
612 struct sample smp;
613 struct stktable_key *skey;
614 struct stksess *ts;
615
616 t = hlua_check_stktable(L, 1);
617 smp.data.type = SMP_T_STR;
618 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600619 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200620
621 skey = smp_to_stkey(&smp, t);
622 if (!skey) {
623 lua_pushnil(L);
624 return 1;
625 }
626
627 ts = stktable_lookup_key(t, skey);
628 if (!ts) {
629 lua_pushnil(L);
630 return 1;
631 }
632
633 lua_newtable(L);
634 lua_pushstring(L, "use");
635 lua_pushinteger(L, ts->ref_cnt - 1);
636 lua_settable(L, -3);
637
638 lua_pushstring(L, "expire");
639 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
640 lua_settable(L, -3);
641
642 hlua_stktable_entry(L, t, ts);
643 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
644 ts->ref_cnt--;
645 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
646
647 return 1;
648}
649
650struct stk_filter {
651 long long val;
652 int type;
653 int op;
654};
655
656
657/* Helper for returning errors to callers using Lua convention (nil, err) */
658static int hlua_error(lua_State *L, const char *fmt, ...) {
659 char buf[256];
660 int len;
661 va_list args;
662 va_start(args, fmt);
663 len = vsnprintf(buf, sizeof(buf), fmt, args);
664 va_end(args);
665
666 if (len < 0) {
667 ha_alert("hlua_error(): Could not write error message.\n");
668 lua_pushnil(L);
669 return 1;
670 } else if (len >= sizeof(buf))
671 ha_alert("hlua_error(): Error message was truncated.\n");
672
673 lua_pushnil(L);
674 lua_pushstring(L, buf);
675
676 return 2;
677}
678
679/* Dump the contents of stick table <t>*/
680int hlua_stktable_dump(lua_State *L)
681{
682 struct stktable *t;
683 struct ebmb_node *eb;
684 struct ebmb_node *n;
685 struct stksess *ts;
686 int type;
687 int op;
688 int dt;
689 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100690 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200691 int filter_count = 0;
692 int i;
693 int skip_entry;
694 void *ptr;
695
696 t = hlua_check_stktable(L, 1);
697 type = lua_type(L, 2);
698
699 switch (type) {
700 case LUA_TNONE:
701 case LUA_TNIL:
702 break;
703 case LUA_TTABLE:
704 lua_pushnil(L);
705 while (lua_next(L, 2) != 0) {
706 int entry_idx = 0;
707
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100708 if (filter_count >= STKTABLE_FILTER_LEN)
709 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200710
711 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
712 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
713
714 lua_pushnil(L);
715 while (lua_next(L, -2) != 0) {
716 switch (entry_idx) {
717 case 0:
718 if (lua_type(L, -1) != LUA_TSTRING)
719 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
720
721 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
722 if (dt < 0 || t->data_ofs[dt] == 0)
723 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
724 filter[filter_count].type = dt;
725 break;
726 case 1:
727 if (lua_type(L, -1) != LUA_TSTRING)
728 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
729
730 op = get_std_op(lua_tostring(L, -1));
731 if (op < 0)
732 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
733 filter[filter_count].op = op;
734 break;
735 case 2:
736 val = lua_tointeger(L, -1);
737 filter[filter_count].val = val;
738 filter_count++;
739 break;
740 default:
741 break;
742 }
743 entry_idx++;
744 lua_pop(L, 1);
745 }
746 lua_pop(L, 1);
747 }
748 break;
749 default:
750 return hlua_error(L, "filter table expected");
751 }
752
753 lua_newtable(L);
754
755 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
756 eb = ebmb_first(&t->keys);
757 for (n = eb; n; n = ebmb_next(n)) {
758 ts = ebmb_entry(n, struct stksess, key);
759 if (!ts) {
760 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
761 return 1;
762 }
763 ts->ref_cnt++;
764 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
765
766 /* multi condition/value filter */
767 skip_entry = 0;
768 for (i = 0; i < filter_count; i++) {
769 if (t->data_ofs[filter[i].type] == 0)
770 continue;
771
772 ptr = stktable_data_ptr(t, ts, filter[i].type);
773
774 switch (stktable_data_types[filter[i].type].std_type) {
775 case STD_T_SINT:
776 val = stktable_data_cast(ptr, std_t_sint);
777 break;
778 case STD_T_UINT:
779 val = stktable_data_cast(ptr, std_t_uint);
780 break;
781 case STD_T_ULL:
782 val = stktable_data_cast(ptr, std_t_ull);
783 break;
784 case STD_T_FRQP:
785 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
786 t->data_arg[filter[i].type].u);
787 break;
788 default:
789 continue;
790 break;
791 }
792
793 op = filter[i].op;
794
795 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
796 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
797 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
798 skip_entry = 1;
799 break;
800 }
801 }
802
803 if (skip_entry) {
804 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
805 ts->ref_cnt--;
806 continue;
807 }
808
809 if (t->type == SMP_T_IPV4) {
810 char addr[INET_ADDRSTRLEN];
811 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
812 lua_pushstring(L, addr);
813 } else if (t->type == SMP_T_IPV6) {
814 char addr[INET6_ADDRSTRLEN];
815 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
816 lua_pushstring(L, addr);
817 } else if (t->type == SMP_T_SINT) {
818 lua_pushinteger(L, *ts->key.key);
819 } else if (t->type == SMP_T_STR) {
820 lua_pushstring(L, (const char *)ts->key.key);
821 } else {
822 return hlua_error(L, "Unsupported stick table key type");
823 }
824
825 lua_newtable(L);
826 hlua_stktable_entry(L, t, ts);
827 lua_settable(L, -3);
828 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
829 ts->ref_cnt--;
830 }
831 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
832
833 return 1;
834}
835
Thierry Fournierff480422016-02-25 08:36:46 +0100836int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
837{
838 lua_newtable(L);
839
840 /* Pop a class sesison metatable and affect it to the userdata. */
841 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
842 lua_setmetatable(L, -2);
843
844 lua_pushlightuserdata(L, lst);
845 lua_rawseti(L, -2, 0);
846 return 1;
847}
848
849static struct listener *hlua_check_listener(lua_State *L, int ud)
850{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200851 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100852}
853
854int hlua_listener_get_stats(lua_State *L)
855{
856 struct listener *li;
857 int i;
858
859 li = hlua_check_listener(L, 1);
860
Willy Tarreauc95bad52016-12-22 00:13:31 +0100861 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100862 lua_pushnil(L);
863 return 1;
864 }
865
Willy Tarreau708c4162019-10-09 10:19:16 +0200866 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats, STATS_LEN);
Thierry Fournierff480422016-02-25 08:36:46 +0100867
868 lua_newtable(L);
869 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200870 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100871 hlua_fcn_pushfield(L, &stats[i]);
872 lua_settable(L, -3);
873 }
874 return 1;
875
876}
877
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100878int hlua_fcn_new_server(lua_State *L, struct server *srv)
879{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400880 char buffer[12];
881
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100882 lua_newtable(L);
883
884 /* Pop a class sesison metatable and affect it to the userdata. */
885 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
886 lua_setmetatable(L, -2);
887
888 lua_pushlightuserdata(L, srv);
889 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400890
891 /* Add server name. */
892 lua_pushstring(L, "name");
893 lua_pushstring(L, srv->id);
894 lua_settable(L, -3);
895
896 /* Add server puid. */
897 lua_pushstring(L, "puid");
898 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
899 lua_pushstring(L, buffer);
900 lua_settable(L, -3);
901
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100902 return 1;
903}
904
905static struct server *hlua_check_server(lua_State *L, int ud)
906{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200907 return hlua_checkudata(L, ud, class_server_ref);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100908}
909
910int hlua_server_get_stats(lua_State *L)
911{
912 struct server *srv;
913 int i;
914
915 srv = hlua_check_server(L, 1);
916
917 if (!srv->proxy) {
918 lua_pushnil(L);
919 return 1;
920 }
921
Willy Tarreau708c4162019-10-09 10:19:16 +0200922 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats, STATS_LEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100923
924 lua_newtable(L);
925 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200926 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100927 hlua_fcn_pushfield(L, &stats[i]);
928 lua_settable(L, -3);
929 }
930 return 1;
931
932}
933
934int hlua_server_get_addr(lua_State *L)
935{
936 struct server *srv;
937 char addr[INET6_ADDRSTRLEN];
938 luaL_Buffer b;
939
940 srv = hlua_check_server(L, 1);
941
942 luaL_buffinit(L, &b);
943
944 switch (srv->addr.ss_family) {
945 case AF_INET:
946 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
947 addr, INET_ADDRSTRLEN);
948 luaL_addstring(&b, addr);
949 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400950 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100951 luaL_addstring(&b, addr);
952 break;
953 case AF_INET6:
954 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400955 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100956 luaL_addstring(&b, addr);
957 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400958 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100959 luaL_addstring(&b, addr);
960 break;
961 case AF_UNIX:
962 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
963 break;
964 default:
965 luaL_addstring(&b, "<unknown>");
966 break;
967 }
968
969 luaL_pushresult(&b);
970 return 1;
971}
972
973int hlua_server_is_draining(lua_State *L)
974{
975 struct server *srv;
976
977 srv = hlua_check_server(L, 1);
978 lua_pushinteger(L, server_is_draining(srv));
979 return 1;
980}
981
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400982int hlua_server_set_maxconn(lua_State *L)
983{
984 struct server *srv;
985 const char *maxconn;
986 const char *err;
987
988 srv = hlua_check_server(L, 1);
989 maxconn = luaL_checkstring(L, 2);
990
991 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
992 err = server_parse_maxconn_change_request(srv, maxconn);
993 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
994 if (!err)
995 lua_pushnil(L);
996 else
997 hlua_pushstrippedstring(L, err);
998 return 1;
999}
1000
1001int hlua_server_get_maxconn(lua_State *L)
1002{
1003 struct server *srv;
1004
1005 srv = hlua_check_server(L, 1);
1006 lua_pushinteger(L, srv->maxconn);
1007 return 1;
1008}
1009
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001010int hlua_server_set_weight(lua_State *L)
1011{
1012 struct server *srv;
1013 const char *weight;
1014 const char *err;
1015
1016 srv = hlua_check_server(L, 1);
1017 weight = luaL_checkstring(L, 2);
1018
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001019 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001020 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001021 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001022 if (!err)
1023 lua_pushnil(L);
1024 else
1025 hlua_pushstrippedstring(L, err);
1026 return 1;
1027}
1028
1029int hlua_server_get_weight(lua_State *L)
1030{
1031 struct server *srv;
1032
1033 srv = hlua_check_server(L, 1);
1034 lua_pushinteger(L, srv->uweight);
1035 return 1;
1036}
1037
1038int hlua_server_set_addr(lua_State *L)
1039{
1040 struct server *srv;
1041 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001042 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001043 const char *err;
1044
1045 srv = hlua_check_server(L, 1);
1046 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001047 if (lua_gettop(L) >= 3)
1048 port = luaL_checkstring(L, 3);
1049 else
1050 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001051
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001052 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001053 err = update_server_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001054 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001055 if (!err)
1056 lua_pushnil(L);
1057 else
1058 hlua_pushstrippedstring(L, err);
1059 return 1;
1060}
1061
1062int hlua_server_shut_sess(lua_State *L)
1063{
1064 struct server *srv;
1065
1066 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001067 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001068 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001069 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001070 return 0;
1071}
1072
1073int hlua_server_set_drain(lua_State *L)
1074{
1075 struct server *srv;
1076
1077 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001078 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001079 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001080 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001081 return 0;
1082}
1083
1084int hlua_server_set_maint(lua_State *L)
1085{
1086 struct server *srv;
1087
1088 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001089 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001090 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001091 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001092 return 0;
1093}
1094
1095int hlua_server_set_ready(lua_State *L)
1096{
1097 struct server *srv;
1098
1099 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001100 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001101 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001102 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001103 return 0;
1104}
1105
1106int hlua_server_check_enable(lua_State *L)
1107{
1108 struct server *sv;
1109
1110 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001111 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001112 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001113 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001114 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001115 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001116 return 0;
1117}
1118
1119int hlua_server_check_disable(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_force_up(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->track)) {
1139 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001140 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001141 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001142 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001143 return 0;
1144}
1145
1146int hlua_server_check_force_nolb(lua_State *L)
1147{
1148 struct server *sv;
1149
1150 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001151 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001152 if (!(sv->track)) {
1153 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001154 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001155 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001156 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001157 return 0;
1158}
1159
1160int hlua_server_check_force_down(lua_State *L)
1161{
1162 struct server *sv;
1163
1164 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001165 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001166 if (!(sv->track)) {
1167 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001168 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001169 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001170 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001171 return 0;
1172}
1173
1174int hlua_server_agent_enable(lua_State *L)
1175{
1176 struct server *sv;
1177
1178 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001179 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001180 if (sv->agent.state & CHK_ST_CONFIGURED) {
1181 sv->agent.state |= CHK_ST_ENABLED;
1182 }
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_disable(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_force_up(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_ENABLED) {
1207 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001208 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001209 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001210 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001211 return 0;
1212}
1213
1214int hlua_server_agent_force_down(lua_State *L)
1215{
1216 struct server *sv;
1217
1218 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001219 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001220 if (sv->agent.state & CHK_ST_ENABLED) {
1221 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001222 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001223 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001224 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001225 return 0;
1226}
1227
Thierry Fournierf61aa632016-02-19 20:56:00 +01001228int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1229{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001230 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001231 struct listener *lst;
1232 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001233 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001234
Thierry Fournierf61aa632016-02-19 20:56:00 +01001235 lua_newtable(L);
1236
1237 /* Pop a class sesison metatable and affect it to the userdata. */
1238 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1239 lua_setmetatable(L, -2);
1240
1241 lua_pushlightuserdata(L, px);
1242 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001243
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001244 /* Add proxy name. */
1245 lua_pushstring(L, "name");
1246 lua_pushstring(L, px->id);
1247 lua_settable(L, -3);
1248
Baptiste Assmann46c72552017-10-26 21:51:58 +02001249 /* Add proxy uuid. */
1250 lua_pushstring(L, "uuid");
1251 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1252 lua_pushstring(L, buffer);
1253 lua_settable(L, -3);
1254
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001255 /* Browse and register servers. */
1256 lua_pushstring(L, "servers");
1257 lua_newtable(L);
1258 for (srv = px->srv; srv; srv = srv->next) {
1259 lua_pushstring(L, srv->id);
1260 hlua_fcn_new_server(L, srv);
1261 lua_settable(L, -3);
1262 }
1263 lua_settable(L, -3);
1264
Thierry Fournierff480422016-02-25 08:36:46 +01001265 /* Browse and register listeners. */
1266 lua_pushstring(L, "listeners");
1267 lua_newtable(L);
1268 lid = 1;
1269 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1270 if (lst->name)
1271 lua_pushstring(L, lst->name);
1272 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001273 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001274 lid++;
1275 lua_pushstring(L, buffer);
1276 }
1277 hlua_fcn_new_listener(L, lst);
1278 lua_settable(L, -3);
1279 }
1280 lua_settable(L, -3);
1281
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001282 if (px->table && px->table->id) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001283 lua_pushstring(L, "stktable");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001284 hlua_fcn_new_stktable(L, px->table);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001285 lua_settable(L, -3);
1286 }
1287
Thierry Fournierf61aa632016-02-19 20:56:00 +01001288 return 1;
1289}
1290
1291static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1292{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001293 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001294}
1295
1296int hlua_proxy_pause(lua_State *L)
1297{
1298 struct proxy *px;
1299
1300 px = hlua_check_proxy(L, 1);
1301 pause_proxy(px);
1302 return 0;
1303}
1304
1305int hlua_proxy_resume(lua_State *L)
1306{
1307 struct proxy *px;
1308
1309 px = hlua_check_proxy(L, 1);
1310 resume_proxy(px);
1311 return 0;
1312}
1313
1314int hlua_proxy_stop(lua_State *L)
1315{
1316 struct proxy *px;
1317
1318 px = hlua_check_proxy(L, 1);
1319 stop_proxy(px);
1320 return 0;
1321}
1322
1323int hlua_proxy_get_cap(lua_State *L)
1324{
1325 struct proxy *px;
1326 const char *str;
1327
1328 px = hlua_check_proxy(L, 1);
1329 str = proxy_cap_str(px->cap);
1330 lua_pushstring(L, str);
1331 return 1;
1332}
1333
1334int hlua_proxy_get_stats(lua_State *L)
1335{
1336 struct proxy *px;
1337 int i;
1338
1339 px = hlua_check_proxy(L, 1);
1340 if (px->cap & PR_CAP_BE)
Willy Tarreau708c4162019-10-09 10:19:16 +02001341 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001342 else
1343 stats_fill_fe_stats(px, stats, STATS_LEN);
1344 lua_newtable(L);
1345 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001346 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001347 hlua_fcn_pushfield(L, &stats[i]);
1348 lua_settable(L, -3);
1349 }
1350 return 1;
1351}
1352
1353int hlua_proxy_get_mode(lua_State *L)
1354{
1355 struct proxy *px;
1356 const char *str;
1357
1358 px = hlua_check_proxy(L, 1);
1359 str = proxy_mode_str(px->mode);
1360 lua_pushstring(L, str);
1361 return 1;
1362}
1363
1364int hlua_proxy_shut_bcksess(lua_State *L)
1365{
1366 struct proxy *px;
1367
1368 px = hlua_check_proxy(L, 1);
1369 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1370 return 0;
1371}
1372
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001373int hlua_fcn_post_init(lua_State *L)
1374{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001375 struct proxy *px;
1376
1377 /* get core array. */
1378 if (lua_getglobal(L, "core") != LUA_TTABLE)
1379 lua_error(L);
1380
1381 /* Create proxies entry. */
1382 lua_pushstring(L, "proxies");
1383 lua_newtable(L);
1384
1385 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001386 for (px = proxies_list; px; px = px->next) {
Thierry Fournierf61aa632016-02-19 20:56:00 +01001387 lua_pushstring(L, px->id);
1388 hlua_fcn_new_proxy(L, px);
1389 lua_settable(L, -3);
1390 }
1391
1392 /* push "proxies" in "core" */
1393 lua_settable(L, -3);
1394
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001395 /* Create proxies entry. */
1396 lua_pushstring(L, "frontends");
1397 lua_newtable(L);
1398
1399 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001400 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001401 if (!(px->cap & PR_CAP_FE))
1402 continue;
1403 lua_pushstring(L, px->id);
1404 hlua_fcn_new_proxy(L, px);
1405 lua_settable(L, -3);
1406 }
1407
1408 /* push "frontends" in "core" */
1409 lua_settable(L, -3);
1410
1411 /* Create proxies entry. */
1412 lua_pushstring(L, "backends");
1413 lua_newtable(L);
1414
1415 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001416 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001417 if (!(px->cap & PR_CAP_BE))
1418 continue;
1419 lua_pushstring(L, px->id);
1420 hlua_fcn_new_proxy(L, px);
1421 lua_settable(L, -3);
1422 }
1423
1424 /* push "backend" in "core" */
1425 lua_settable(L, -3);
1426
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001427 return 1;
1428}
1429
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001430/* This Lua function take a string, a list of separators.
1431 * It tokenize the input string using the list of separators
1432 * as separator.
1433 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001434 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001435 */
1436int hlua_tokenize(lua_State *L)
1437{
1438 const char *str;
1439 const char *sep;
1440 int index;
1441 const char *token;
1442 const char *p;
1443 const char *c;
1444 int ignore_empty;
1445
1446 ignore_empty = 0;
1447
1448 str = luaL_checkstring(L, 1);
1449 sep = luaL_checkstring(L, 2);
1450 if (lua_gettop(L) == 3)
1451 ignore_empty = hlua_checkboolean(L, 3);
1452
1453 lua_newtable(L);
1454 index = 1;
1455 token = str;
1456 p = str;
1457 while(1) {
1458 for (c = sep; *c != '\0'; c++)
1459 if (*p == *c)
1460 break;
1461 if (*p == *c) {
1462 if ((!ignore_empty) || (p - token > 0)) {
1463 lua_pushlstring(L, token, p - token);
1464 lua_rawseti(L, -2, index);
1465 index++;
1466 }
1467 token = p + 1;
1468 }
1469 if (*p == '\0')
1470 break;
1471 p++;
1472 }
1473
1474 return 1;
1475}
1476
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001477int hlua_parse_addr(lua_State *L)
1478{
1479 struct hlua_addr *addr;
1480 const char *str = luaL_checkstring(L, 1);
1481 unsigned char mask;
1482
1483 addr = lua_newuserdata(L, sizeof(struct hlua_addr));
1484 if (!addr) {
1485 lua_pushnil(L);
1486 return 1;
1487 }
1488
1489 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
1490 addr->type = AF_INET;
1491 return 1;
1492 }
1493
1494 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1495 len2mask6(mask, &addr->addr.v6.mask);
1496 addr->type = AF_INET6;
1497 return 1;
1498 }
1499
1500 lua_pop(L, 1);
1501 lua_pushnil(L);
1502 return 1;
1503}
1504
1505int hlua_match_addr(lua_State *L)
1506{
1507 struct hlua_addr *addr1;
1508 struct hlua_addr *addr2;
1509
1510 if (!lua_isuserdata(L, 1) ||
1511 !lua_isuserdata(L, 2)) {
1512 lua_pushboolean(L, 0);
1513 return 1;
1514 }
1515
1516 addr1 = lua_touserdata(L, 1);
1517 addr2 = lua_touserdata(L, 2);
1518
1519 if (addr1->type != addr2->type) {
1520 lua_pushboolean(L, 0);
1521 return 1;
1522 }
1523
1524 if (addr1->type == AF_INET) {
1525 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1526 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1527 lua_pushboolean(L, 1);
1528 return 1;
1529 }
1530 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001531 int i;
1532
1533 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001534 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1535 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1536 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1537 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001538 break;
1539 }
1540 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001541 lua_pushboolean(L, 1);
1542 return 1;
1543 }
1544 }
1545
1546 lua_pushboolean(L, 0);
1547 return 1;
1548}
1549
Dragan Dosen26743032019-04-30 15:54:36 +02001550static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001551{
1552 return (hlua_checkudata(L, ud, class_regex_ref));
1553}
1554
1555static int hlua_regex_comp(struct lua_State *L)
1556{
Dragan Dosen26743032019-04-30 15:54:36 +02001557 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001558 const char *str;
1559 int cs;
1560 char *err;
1561
1562 str = luaL_checkstring(L, 1);
1563 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1564 cs = lua_toboolean(L, 2);
1565
1566 regex = lua_newuserdata(L, sizeof(*regex));
1567
1568 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001569 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001570 lua_pushboolean(L, 0); /* status error */
1571 lua_pushstring(L, err); /* Reason */
1572 free(err);
1573 return 2;
1574 }
1575
1576 lua_pushboolean(L, 1); /* Status ok */
1577
1578 /* Create object */
1579 lua_newtable(L);
1580 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1581 lua_rawseti(L, -2, 0);
1582 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1583 lua_setmetatable(L, -2);
1584 return 2;
1585}
1586
1587static int hlua_regex_exec(struct lua_State *L)
1588{
Dragan Dosen26743032019-04-30 15:54:36 +02001589 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001590 const char *str;
1591 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001592 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001593
1594 regex = hlua_check_regex(L, 1);
1595 str = luaL_checklstring(L, 2, &len);
1596
Dragan Dosen26743032019-04-30 15:54:36 +02001597 if (!*regex) {
1598 lua_pushboolean(L, 0);
1599 return 1;
1600 }
1601
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001602 /* Copy the string because regex_exec2 require a 'char *'
1603 * and not a 'const char *'.
1604 */
1605 tmp = get_trash_chunk();
1606 if (len >= tmp->size) {
1607 lua_pushboolean(L, 0);
1608 return 1;
1609 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001610 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001611
Dragan Dosen26743032019-04-30 15:54:36 +02001612 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001613
1614 return 1;
1615}
1616
1617static int hlua_regex_match(struct lua_State *L)
1618{
Dragan Dosen26743032019-04-30 15:54:36 +02001619 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001620 const char *str;
1621 size_t len;
1622 regmatch_t pmatch[20];
1623 int ret;
1624 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001625 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001626
1627 regex = hlua_check_regex(L, 1);
1628 str = luaL_checklstring(L, 2, &len);
1629
Dragan Dosen26743032019-04-30 15:54:36 +02001630 if (!*regex) {
1631 lua_pushboolean(L, 0);
1632 return 1;
1633 }
1634
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001635 /* Copy the string because regex_exec2 require a 'char *'
1636 * and not a 'const char *'.
1637 */
1638 tmp = get_trash_chunk();
1639 if (len >= tmp->size) {
1640 lua_pushboolean(L, 0);
1641 return 1;
1642 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001643 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001644
Dragan Dosen26743032019-04-30 15:54:36 +02001645 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001646 lua_pushboolean(L, ret);
1647 lua_newtable(L);
1648 if (ret) {
1649 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1650 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1651 lua_rawseti(L, -2, i + 1);
1652 }
1653 }
1654 return 2;
1655}
1656
1657static int hlua_regex_free(struct lua_State *L)
1658{
Dragan Dosen26743032019-04-30 15:54:36 +02001659 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001660
1661 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001662 regex_free(*regex);
1663 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001664 return 0;
1665}
1666
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001667int hlua_fcn_reg_core_fcn(lua_State *L)
1668{
Thierry Fournier1de16592016-01-27 09:49:07 +01001669 if (!hlua_concat_init(L))
1670 return 0;
1671
Thierry Fournier4f99b272016-02-22 08:40:02 +01001672 hlua_class_function(L, "now", hlua_now);
1673 hlua_class_function(L, "http_date", hlua_http_date);
1674 hlua_class_function(L, "imf_date", hlua_imf_date);
1675 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1676 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1677 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001678 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001679 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1680 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001681 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001682
Thierry FOURNIER31904272017-10-25 12:59:51 +02001683 /* Create regex object. */
1684 lua_newtable(L);
1685 hlua_class_function(L, "new", hlua_regex_comp);
1686
1687 lua_newtable(L); /* The metatable. */
1688 lua_pushstring(L, "__index");
1689 lua_newtable(L);
1690 hlua_class_function(L, "exec", hlua_regex_exec);
1691 hlua_class_function(L, "match", hlua_regex_match);
1692 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1693 hlua_class_function(L, "__gc", hlua_regex_free);
1694
1695 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1696 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1697
1698 lua_setmetatable(L, -2);
1699 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1700
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001701 /* Create stktable object. */
1702 lua_newtable(L);
1703 lua_pushstring(L, "__index");
1704 lua_newtable(L);
1705 hlua_class_function(L, "info", hlua_stktable_info);
1706 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1707 hlua_class_function(L, "dump", hlua_stktable_dump);
1708 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1709 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1710
Thierry Fournierff480422016-02-25 08:36:46 +01001711 /* Create listener object. */
1712 lua_newtable(L);
1713 lua_pushstring(L, "__index");
1714 lua_newtable(L);
1715 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1716 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1717 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1718
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001719 /* Create server object. */
1720 lua_newtable(L);
1721 lua_pushstring(L, "__index");
1722 lua_newtable(L);
1723 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001724 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1725 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001726 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1727 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1728 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1729 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1730 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1731 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1732 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1733 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1734 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1735 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1736 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1737 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1738 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1739 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1740 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1741 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1742 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1743 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1744 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1745 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1746
Thierry Fournierf61aa632016-02-19 20:56:00 +01001747 /* Create proxy object. */
1748 lua_newtable(L);
1749 lua_pushstring(L, "__index");
1750 lua_newtable(L);
1751 hlua_class_function(L, "pause", hlua_proxy_pause);
1752 hlua_class_function(L, "resume", hlua_proxy_resume);
1753 hlua_class_function(L, "stop", hlua_proxy_stop);
1754 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1755 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1756 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1757 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1758 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1759 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1760
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001761 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001762}