blob: da975c0752286b5414787c5f1cf114ec01db2bcc [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 Tarreau26474c42020-02-25 10:02:51 +010021#include <common/net_helper.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020022#include <haproxy/time.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010023#include <common/uri_auth.h>
Thierry Fournierb1f46562016-01-21 09:46:15 +010024
William Lallemand9ed62032016-11-21 17:49:11 +010025#include <types/cli.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010026#include <types/hlua.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010027#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010028#include <types/stats.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010029
Thierry Fournierf61aa632016-02-19 20:56:00 +010030#include <proto/proxy.h>
31#include <proto/server.h>
William Lallemand9ed62032016-11-21 17:49:11 +010032#include <proto/stats.h>
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020033#include <proto/stick_table.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010034
Thierry Fournier1de16592016-01-27 09:49:07 +010035/* Contains the class reference of the concat object. */
36static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010037static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010038static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010039static int class_listener_ref;
Thierry FOURNIER31904272017-10-25 12:59:51 +020040static int class_regex_ref;
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020041static int class_stktable_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010042
Thierry Fournierf61aa632016-02-19 20:56:00 +010043#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010044
Thierry FOURNIERffbad792017-07-12 11:39:04 +020045static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010046
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010047int hlua_checkboolean(lua_State *L, int index)
48{
49 if (!lua_isboolean(L, index))
50 luaL_argerror(L, index, "boolean expected");
51 return lua_toboolean(L, index);
52}
53
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020054/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
55static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
56{
57#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
58 lua_pushinteger(L, val);
59#else
60 if (val > INT_MAX)
61 lua_pushnumber(L, (lua_Number)val);
62 else
63 lua_pushinteger(L, (int)val);
64#endif
65 return 1;
66}
67
68/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
69static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
70#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
71 /* 64 bits case, U64 is supported until LLONG_MAX */
72 if (val > LLONG_MAX)
73 lua_pushnumber(L, (lua_Number)val);
74 else
75 lua_pushinteger(L, val);
76#else
77 /* 32 bits case, U64 is supported until INT_MAX */
78 if (val > INT_MAX)
79 lua_pushnumber(L, (lua_Number)val);
80 else
81 lua_pushinteger(L, (int)val);
82#endif
83 return 1;
84}
85
Joseph Herlantb3d92e32018-11-15 09:35:04 -080086/* This function gets a struct field and converts it in Lua
87 * variable. The variable is pushed at the top of the stack.
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010088 */
89int hlua_fcn_pushfield(lua_State *L, struct field *field)
90{
91 /* The lua_Integer is always signed. Its length depends on
Joseph Herlantb3d92e32018-11-15 09:35:04 -080092 * compilation options, so the following code is conditioned
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010093 * by some macros. Windows maros are not supported.
94 * If the number cannot be represented as integer, we try to
95 * convert to float.
96 */
97 switch (field_format(field, 0)) {
98
99 case FF_EMPTY:
100 lua_pushnil(L);
101 return 1;
102
103 case FF_S32:
104 /* S32 is always supported. */
105 lua_pushinteger(L, field->u.s32);
106 return 1;
107
108 case FF_U32:
109#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
110 /* 64 bits case, U32 is always supported */
111 lua_pushinteger(L, field->u.u32);
112#else
113 /* 32 bits case, U32 is supported until INT_MAX. */
114 if (field->u.u32 > INT_MAX)
115 lua_pushnumber(L, (lua_Number)field->u.u32);
116 else
117 lua_pushinteger(L, field->u.u32);
118#endif
119 return 1;
120
121 case FF_S64:
122#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
123 /* 64 bits case, S64 is always supported */
124 lua_pushinteger(L, field->u.s64);
125#else
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500126 /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */
Thierry Fournier8b0d6e12016-03-16 18:29:13 +0100127 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
128 lua_pushnumber(L, (lua_Number)field->u.s64);
129 else
130 lua_pushinteger(L, (int)field->u.s64);
131#endif
132 return 1;
133
134 case FF_U64:
135#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
136 /* 64 bits case, U64 is supported until LLONG_MAX */
137 if (field->u.u64 > LLONG_MAX)
138 lua_pushnumber(L, (lua_Number)field->u.u64);
139 else
140 lua_pushinteger(L, field->u.u64);
141#else
142 /* 64 bits case, U64 is supported until INT_MAX */
143 if (field->u.u64 > INT_MAX)
144 lua_pushnumber(L, (lua_Number)field->u.u64);
145 else
146 lua_pushinteger(L, (int)field->u.u64);
147#endif
148 return 1;
149
150 case FF_STR:
151 lua_pushstring(L, field->u.str);
152 return 1;
153
154 default:
155 break;
156 }
157
158 /* Default case, never reached. */
159 lua_pushnil(L);
160 return 1;
161}
162
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100163/* Some string are started or terminated by blank chars,
164 * this function removes the spaces, tabs, \r and
165 * \n at the begin and at the end of the string "str", and
166 * push the result in the lua stack.
167 * Returns a pointer to the Lua internal copy of the string.
168 */
169const char *hlua_pushstrippedstring(lua_State *L, const char *str)
170{
171 const char *p;
172 const char *e;
173
174 for (p = str; HTTP_IS_LWS(*p); p++);
175 for (e = p + strlen(p) - 1; e > p && HTTP_IS_LWS(*e); e--);
176
177 return lua_pushlstring(L, p, e - p);
178}
179
Thierry Fournierddd89882016-02-22 19:52:08 +0100180/* The three following functions are useful for adding entries
181 * in a table. These functions takes a string and respectively an
182 * integer, a string or a function and add it to the table in the
183 * top of the stack.
184 *
185 * These functions throws an error if no more stack size is
186 * available.
187 */
188void hlua_class_const_int(lua_State *L, const char *name, int value)
189{
Thierry Fournierddd89882016-02-22 19:52:08 +0100190 lua_pushstring(L, name);
191 lua_pushinteger(L, value);
192 lua_rawset(L, -3);
193}
194void hlua_class_const_str(lua_State *L, const char *name, const char *value)
195{
Thierry Fournierddd89882016-02-22 19:52:08 +0100196 lua_pushstring(L, name);
197 lua_pushstring(L, value);
198 lua_rawset(L, -3);
199}
200void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
201{
Thierry Fournierddd89882016-02-22 19:52:08 +0100202 lua_pushstring(L, name);
203 lua_pushcclosure(L, function, 0);
204 lua_rawset(L, -3);
205}
206
Joseph Herlantb3d92e32018-11-15 09:35:04 -0800207/* This function returns a string containing the HAProxy object name. */
Thierry Fournierddd89882016-02-22 19:52:08 +0100208int hlua_dump_object(struct lua_State *L)
209{
210 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
211 lua_pushfstring(L, "HAProxy class %s", name);
212 return 1;
213}
214
Thierry Fournier45e78d72016-02-19 18:34:46 +0100215/* This function register a table as metatable and. It names
216 * the metatable, and returns the associated reference.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500217 * The original table is popped from the top of the stack.
Thierry Fournier45e78d72016-02-19 18:34:46 +0100218 * "name" is the referenced class name.
219 */
220int hlua_register_metatable(struct lua_State *L, char *name)
221{
222 /* Check the type of the top element. it must be
223 * a table.
224 */
225 if (lua_type(L, -1) != LUA_TTABLE)
226 luaL_error(L, "hlua_register_metatable() requires a type Table "
227 "in the top of the stack");
228
229 /* Add the __tostring function which identify the
230 * created object.
231 */
232 lua_pushstring(L, "__tostring");
233 lua_pushstring(L, name);
234 lua_pushcclosure(L, hlua_dump_object, 1);
235 lua_rawset(L, -3);
236
237 /* Register a named entry for the table. The table
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500238 * reference is copied first because the function
Thierry Fournier45e78d72016-02-19 18:34:46 +0100239 * lua_setfield() pop the entry.
240 */
241 lua_pushvalue(L, -1);
242 lua_setfield(L, LUA_REGISTRYINDEX, name);
243
244 /* Creates the reference of the object. The
245 * function luaL_ref pop the top of the stack.
246 */
247 return luaL_ref(L, LUA_REGISTRYINDEX);
248}
249
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100250/* Return an object of the expected type, or throws an error. */
251void *hlua_checkudata(lua_State *L, int ud, int class_ref)
252{
253 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100254 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100255
256 /* Check if the stack entry is an array. */
257 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100258 luaL_argerror(L, ud, NULL);
259
260 /* pop the metatable of the referencecd object. */
261 if (!lua_getmetatable(L, ud))
262 luaL_argerror(L, ud, NULL);
263
264 /* pop the expected metatable. */
265 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
266
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100267 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100268 ret = lua_rawequal(L, -1, -2);
269 lua_pop(L, 2);
270 if (!ret)
271 luaL_argerror(L, ud, NULL);
272
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100273 /* Push on the stack at the entry [0] of the table. */
274 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100275
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100276 /* Check if this entry is userdata. */
277 p = lua_touserdata(L, -1);
278 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100279 luaL_argerror(L, ud, NULL);
280
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100281 /* Remove the entry returned by lua_rawgeti(). */
282 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100283
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100284 /* Return the associated struct. */
285 return p;
286}
287
Thierry Fournierb1f46562016-01-21 09:46:15 +0100288/* This function return the current date at epoch format in milliseconds. */
289int hlua_now(lua_State *L)
290{
291 lua_newtable(L);
292 lua_pushstring(L, "sec");
293 lua_pushinteger(L, now.tv_sec);
294 lua_rawset(L, -3);
295 lua_pushstring(L, "usec");
296 lua_pushinteger(L, now.tv_usec);
297 lua_rawset(L, -3);
298 return 1;
299}
300
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100301/* This functions expects a Lua string as HTTP date, parse it and
302 * returns an integer containing the epoch format of the date, or
303 * nil if the parsing fails.
304 */
305static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
306{
307 const char *str;
308 size_t len;
309 struct tm tm;
310 time_t time;
311
312 str = luaL_checklstring(L, 1, &len);
313
314 if (!fcn(str, len, &tm)) {
315 lua_pushnil(L);
316 return 1;
317 }
318
319 /* This function considers the content of the broken-down time
320 * is exprimed in the UTC timezone. timegm don't care about
321 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
322 * the timezone from the broken-down time, it must be fixed
323 * after the conversion.
324 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200325 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100326 if (time == -1) {
327 lua_pushnil(L);
328 return 1;
329 }
330
331 lua_pushinteger(L, (int)time);
332 return 1;
333}
334static int hlua_http_date(lua_State *L)
335{
336 return hlua_parse_date(L, parse_http_date);
337}
338static int hlua_imf_date(lua_State *L)
339{
340 return hlua_parse_date(L, parse_imf_date);
341}
342static int hlua_rfc850_date(lua_State *L)
343{
344 return hlua_parse_date(L, parse_rfc850_date);
345}
346static int hlua_asctime_date(lua_State *L)
347{
348 return hlua_parse_date(L, parse_asctime_date);
349}
350
Thierry Fourniereea77c02016-03-18 08:47:13 +0100351static int hlua_get_info(lua_State *L)
352{
353 int i;
354
355 stats_fill_info(stats, STATS_LEN);
356
357 lua_newtable(L);
358 for (i=0; i<INF_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200359 lua_pushstring(L, info_fields[i].name);
Thierry Fourniereea77c02016-03-18 08:47:13 +0100360 hlua_fcn_pushfield(L, &stats[i]);
361 lua_settable(L, -3);
362 }
363 return 1;
364}
365
Thierry Fournier49d48422016-02-19 12:09:29 +0100366static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100367{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200368 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100369}
370
371static int hlua_concat_add(lua_State *L)
372{
Thierry Fournier49d48422016-02-19 12:09:29 +0100373 struct hlua_concat *b;
374 char *buffer;
375 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100376 const char *str;
377 size_t l;
378
379 /* First arg must be a concat object. */
380 b = hlua_check_concat(L, 1);
381
382 /* Second arg must be a string. */
383 str = luaL_checklstring(L, 2, &l);
384
Thierry Fournier49d48422016-02-19 12:09:29 +0100385 /* Get the buffer. */
386 lua_rawgeti(L, 1, 1);
387 buffer = lua_touserdata(L, -1);
388 lua_pop(L, 1);
389
390 /* Update the buffer size if it s required. The old buffer
391 * is crushed by the new in the object array, so it will
392 * be deleted by the GC.
393 * Note that in the first loop, the "new" variable is only
394 * used as a flag.
395 */
396 new = NULL;
397 while (b->size - b->len < l) {
398 b->size += HLUA_CONCAT_BLOCSZ;
399 new = buffer;
400 }
401 if (new) {
402 new = lua_newuserdata(L, b->size);
403 memcpy(new, buffer, b->len);
404 lua_rawseti(L, 1, 1);
405 buffer = new;
406 }
407
408 /* Copy string, and update metadata. */
409 memcpy(buffer + b->len, str, l);
410 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100411 return 0;
412}
413
414static int hlua_concat_dump(lua_State *L)
415{
Thierry Fournier49d48422016-02-19 12:09:29 +0100416 struct hlua_concat *b;
417 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100418
419 /* First arg must be a concat object. */
420 b = hlua_check_concat(L, 1);
421
Thierry Fournier49d48422016-02-19 12:09:29 +0100422 /* Get the buffer. */
423 lua_rawgeti(L, 1, 1);
424 buffer = lua_touserdata(L, -1);
425 lua_pop(L, 1);
426
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500427 /* Push the soncatenated string in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100428 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100429 return 1;
430}
431
432int hlua_concat_new(lua_State *L)
433{
Thierry Fournier49d48422016-02-19 12:09:29 +0100434 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100435
436 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200437 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100438 b->size = HLUA_CONCAT_BLOCSZ;
439 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100440 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100441 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
442 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100443
444 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
445 lua_setmetatable(L, -2);
446
Thierry Fournier1de16592016-01-27 09:49:07 +0100447 return 1;
448}
449
450static int concat_tostring(lua_State *L)
451{
452 const void *ptr = lua_topointer(L, 1);
453 lua_pushfstring(L, "Concat object: %p", ptr);
454 return 1;
455}
456
457static int hlua_concat_init(lua_State *L)
458{
459 /* Creates the buffered concat object. */
460 lua_newtable(L);
461
462 lua_pushstring(L, "__tostring");
463 lua_pushcclosure(L, concat_tostring, 0);
464 lua_settable(L, -3);
465
466 lua_pushstring(L, "__index"); /* Creates the index entry. */
467 lua_newtable(L); /* The "__index" content. */
468
469 lua_pushstring(L, "add");
470 lua_pushcclosure(L, hlua_concat_add, 0);
471 lua_settable(L, -3);
472
473 lua_pushstring(L, "dump");
474 lua_pushcclosure(L, hlua_concat_dump, 0);
475 lua_settable(L, -3);
476
477 lua_settable(L, -3); /* Sets the __index entry. */
478 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
479
480 return 1;
481}
482
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200483int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
484{
485 lua_newtable(L);
486
487 /* Pop a class stktbl metatable and affect it to the userdata. */
488 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
489 lua_setmetatable(L, -2);
490
491 lua_pushlightuserdata(L, tbl);
492 lua_rawseti(L, -2, 0);
493 return 1;
494}
495
496static struct stktable *hlua_check_stktable(lua_State *L, int ud)
497{
498 return hlua_checkudata(L, ud, class_stktable_ref);
499}
500
501/* Extract stick table attributes into Lua table */
502int hlua_stktable_info(lua_State *L)
503{
504 struct stktable *tbl;
505 int dt;
506
507 tbl = hlua_check_stktable(L, 1);
508
509 if (!tbl->id) {
510 lua_pushnil(L);
511 return 1;
512 }
513
514 lua_newtable(L);
515
516 lua_pushstring(L, "type");
517 lua_pushstring(L, stktable_types[tbl->type].kw);
518 lua_settable(L, -3);
519
520 lua_pushstring(L, "length");
521 lua_pushinteger(L, tbl->key_size);
522 lua_settable(L, -3);
523
524 lua_pushstring(L, "size");
525 hlua_fcn_pushunsigned(L, tbl->size);
526 lua_settable(L, -3);
527
528 lua_pushstring(L, "used");
529 hlua_fcn_pushunsigned(L, tbl->current);
530 lua_settable(L, -3);
531
532 lua_pushstring(L, "nopurge");
533 lua_pushboolean(L, tbl->nopurge > 0);
534 lua_settable(L, -3);
535
536 lua_pushstring(L, "expire");
537 lua_pushinteger(L, tbl->expire);
538 lua_settable(L, -3);
539
540 /* Save data types periods (if applicable) in 'data' table */
541 lua_pushstring(L, "data");
542 lua_newtable(L);
543
544 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
545 if (tbl->data_ofs[dt] == 0)
546 continue;
547
548 lua_pushstring(L, stktable_data_types[dt].name);
549
550 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
551 lua_pushinteger(L, tbl->data_arg[dt].u);
552 else
553 lua_pushinteger(L, -1);
554
555 lua_settable(L, -3);
556 }
557
558 lua_settable(L, -3);
559
560 return 1;
561}
562
563/* Helper to get extract stick table entry into Lua table */
564static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
565{
566 int dt;
567 void *ptr;
568
569 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
570
571 if (t->data_ofs[dt] == 0)
572 continue;
573
574 lua_pushstring(L, stktable_data_types[dt].name);
575
576 ptr = stktable_data_ptr(t, ts, dt);
577 switch (stktable_data_types[dt].std_type) {
578 case STD_T_SINT:
579 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
580 break;
581 case STD_T_UINT:
582 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
583 break;
584 case STD_T_ULL:
585 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
586 break;
587 case STD_T_FRQP:
588 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
589 t->data_arg[dt].u));
590 break;
Adis Nezirovicad9f9ed2020-05-05 13:57:28 +0200591 case STD_T_DICT: {
592 struct dict_entry *de;
593 de = stktable_data_cast(ptr, std_t_dict);
594 lua_pushstring(L, de ? (char *)de->value.key : "-");
595 break;
596 }
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200597 }
598
599 lua_settable(L, -3);
600 }
601}
602
603/* Looks in table <t> for a sticky session matching key <key>
604 * Returns table with session data or nil
605 *
606 * The returned table always contains 'use' and 'expire' (integer) fields.
607 * For frequency/rate counters, each data entry is returned as table with
608 * 'value' and 'period' fields.
609 */
610int hlua_stktable_lookup(lua_State *L)
611{
612 struct stktable *t;
613 struct sample smp;
614 struct stktable_key *skey;
615 struct stksess *ts;
616
617 t = hlua_check_stktable(L, 1);
618 smp.data.type = SMP_T_STR;
619 smp.flags = SMP_F_CONST;
Nathan Neulinger31a841c2020-03-03 20:32:47 -0600620 smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200621
622 skey = smp_to_stkey(&smp, t);
623 if (!skey) {
624 lua_pushnil(L);
625 return 1;
626 }
627
628 ts = stktable_lookup_key(t, skey);
629 if (!ts) {
630 lua_pushnil(L);
631 return 1;
632 }
633
634 lua_newtable(L);
635 lua_pushstring(L, "use");
636 lua_pushinteger(L, ts->ref_cnt - 1);
637 lua_settable(L, -3);
638
639 lua_pushstring(L, "expire");
640 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
641 lua_settable(L, -3);
642
643 hlua_stktable_entry(L, t, ts);
644 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
645 ts->ref_cnt--;
646 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
647
648 return 1;
649}
650
651struct stk_filter {
652 long long val;
653 int type;
654 int op;
655};
656
657
658/* Helper for returning errors to callers using Lua convention (nil, err) */
659static int hlua_error(lua_State *L, const char *fmt, ...) {
660 char buf[256];
661 int len;
662 va_list args;
663 va_start(args, fmt);
664 len = vsnprintf(buf, sizeof(buf), fmt, args);
665 va_end(args);
666
667 if (len < 0) {
668 ha_alert("hlua_error(): Could not write error message.\n");
669 lua_pushnil(L);
670 return 1;
671 } else if (len >= sizeof(buf))
672 ha_alert("hlua_error(): Error message was truncated.\n");
673
674 lua_pushnil(L);
675 lua_pushstring(L, buf);
676
677 return 2;
678}
679
680/* Dump the contents of stick table <t>*/
681int hlua_stktable_dump(lua_State *L)
682{
683 struct stktable *t;
684 struct ebmb_node *eb;
685 struct ebmb_node *n;
686 struct stksess *ts;
687 int type;
688 int op;
689 int dt;
690 long long val;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100691 struct stk_filter filter[STKTABLE_FILTER_LEN];
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200692 int filter_count = 0;
693 int i;
694 int skip_entry;
695 void *ptr;
696
697 t = hlua_check_stktable(L, 1);
698 type = lua_type(L, 2);
699
700 switch (type) {
701 case LUA_TNONE:
702 case LUA_TNIL:
703 break;
704 case LUA_TTABLE:
705 lua_pushnil(L);
706 while (lua_next(L, 2) != 0) {
707 int entry_idx = 0;
708
Adis Nezirovic1a693fc2020-01-16 15:19:29 +0100709 if (filter_count >= STKTABLE_FILTER_LEN)
710 return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200711
712 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
713 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
714
715 lua_pushnil(L);
716 while (lua_next(L, -2) != 0) {
717 switch (entry_idx) {
718 case 0:
719 if (lua_type(L, -1) != LUA_TSTRING)
720 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
721
722 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
723 if (dt < 0 || t->data_ofs[dt] == 0)
724 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
725 filter[filter_count].type = dt;
726 break;
727 case 1:
728 if (lua_type(L, -1) != LUA_TSTRING)
729 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
730
731 op = get_std_op(lua_tostring(L, -1));
732 if (op < 0)
733 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
734 filter[filter_count].op = op;
735 break;
736 case 2:
737 val = lua_tointeger(L, -1);
738 filter[filter_count].val = val;
739 filter_count++;
740 break;
741 default:
742 break;
743 }
744 entry_idx++;
745 lua_pop(L, 1);
746 }
747 lua_pop(L, 1);
748 }
749 break;
750 default:
751 return hlua_error(L, "filter table expected");
752 }
753
754 lua_newtable(L);
755
756 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
757 eb = ebmb_first(&t->keys);
758 for (n = eb; n; n = ebmb_next(n)) {
759 ts = ebmb_entry(n, struct stksess, key);
760 if (!ts) {
761 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
762 return 1;
763 }
764 ts->ref_cnt++;
765 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
766
767 /* multi condition/value filter */
768 skip_entry = 0;
769 for (i = 0; i < filter_count; i++) {
770 if (t->data_ofs[filter[i].type] == 0)
771 continue;
772
773 ptr = stktable_data_ptr(t, ts, filter[i].type);
774
775 switch (stktable_data_types[filter[i].type].std_type) {
776 case STD_T_SINT:
777 val = stktable_data_cast(ptr, std_t_sint);
778 break;
779 case STD_T_UINT:
780 val = stktable_data_cast(ptr, std_t_uint);
781 break;
782 case STD_T_ULL:
783 val = stktable_data_cast(ptr, std_t_ull);
784 break;
785 case STD_T_FRQP:
786 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
787 t->data_arg[filter[i].type].u);
788 break;
789 default:
790 continue;
791 break;
792 }
793
794 op = filter[i].op;
795
796 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
797 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
798 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
799 skip_entry = 1;
800 break;
801 }
802 }
803
804 if (skip_entry) {
805 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
806 ts->ref_cnt--;
807 continue;
808 }
809
810 if (t->type == SMP_T_IPV4) {
811 char addr[INET_ADDRSTRLEN];
812 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
813 lua_pushstring(L, addr);
814 } else if (t->type == SMP_T_IPV6) {
815 char addr[INET6_ADDRSTRLEN];
816 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
817 lua_pushstring(L, addr);
818 } else if (t->type == SMP_T_SINT) {
819 lua_pushinteger(L, *ts->key.key);
820 } else if (t->type == SMP_T_STR) {
821 lua_pushstring(L, (const char *)ts->key.key);
822 } else {
823 return hlua_error(L, "Unsupported stick table key type");
824 }
825
826 lua_newtable(L);
827 hlua_stktable_entry(L, t, ts);
828 lua_settable(L, -3);
829 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
830 ts->ref_cnt--;
831 }
832 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
833
834 return 1;
835}
836
Thierry Fournierff480422016-02-25 08:36:46 +0100837int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
838{
839 lua_newtable(L);
840
841 /* Pop a class sesison metatable and affect it to the userdata. */
842 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
843 lua_setmetatable(L, -2);
844
845 lua_pushlightuserdata(L, lst);
846 lua_rawseti(L, -2, 0);
847 return 1;
848}
849
850static struct listener *hlua_check_listener(lua_State *L, int ud)
851{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200852 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100853}
854
855int hlua_listener_get_stats(lua_State *L)
856{
857 struct listener *li;
858 int i;
859
860 li = hlua_check_listener(L, 1);
861
Willy Tarreauc95bad52016-12-22 00:13:31 +0100862 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100863 lua_pushnil(L);
864 return 1;
865 }
866
Willy Tarreau708c4162019-10-09 10:19:16 +0200867 stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats, STATS_LEN);
Thierry Fournierff480422016-02-25 08:36:46 +0100868
869 lua_newtable(L);
870 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200871 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierff480422016-02-25 08:36:46 +0100872 hlua_fcn_pushfield(L, &stats[i]);
873 lua_settable(L, -3);
874 }
875 return 1;
876
877}
878
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100879int hlua_fcn_new_server(lua_State *L, struct server *srv)
880{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400881 char buffer[12];
882
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100883 lua_newtable(L);
884
885 /* Pop a class sesison metatable and affect it to the userdata. */
886 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
887 lua_setmetatable(L, -2);
888
889 lua_pushlightuserdata(L, srv);
890 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400891
892 /* Add server name. */
893 lua_pushstring(L, "name");
894 lua_pushstring(L, srv->id);
895 lua_settable(L, -3);
896
897 /* Add server puid. */
898 lua_pushstring(L, "puid");
899 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
900 lua_pushstring(L, buffer);
901 lua_settable(L, -3);
902
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100903 return 1;
904}
905
906static struct server *hlua_check_server(lua_State *L, int ud)
907{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200908 return hlua_checkudata(L, ud, class_server_ref);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100909}
910
911int hlua_server_get_stats(lua_State *L)
912{
913 struct server *srv;
914 int i;
915
916 srv = hlua_check_server(L, 1);
917
918 if (!srv->proxy) {
919 lua_pushnil(L);
920 return 1;
921 }
922
Willy Tarreau708c4162019-10-09 10:19:16 +0200923 stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats, STATS_LEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100924
925 lua_newtable(L);
926 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +0200927 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100928 hlua_fcn_pushfield(L, &stats[i]);
929 lua_settable(L, -3);
930 }
931 return 1;
932
933}
934
935int hlua_server_get_addr(lua_State *L)
936{
937 struct server *srv;
938 char addr[INET6_ADDRSTRLEN];
939 luaL_Buffer b;
940
941 srv = hlua_check_server(L, 1);
942
943 luaL_buffinit(L, &b);
944
945 switch (srv->addr.ss_family) {
946 case AF_INET:
947 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
948 addr, INET_ADDRSTRLEN);
949 luaL_addstring(&b, addr);
950 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400951 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100952 luaL_addstring(&b, addr);
953 break;
954 case AF_INET6:
955 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400956 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100957 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_UNIX:
963 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
964 break;
965 default:
966 luaL_addstring(&b, "<unknown>");
967 break;
968 }
969
970 luaL_pushresult(&b);
971 return 1;
972}
973
974int hlua_server_is_draining(lua_State *L)
975{
976 struct server *srv;
977
978 srv = hlua_check_server(L, 1);
979 lua_pushinteger(L, server_is_draining(srv));
980 return 1;
981}
982
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400983int hlua_server_set_maxconn(lua_State *L)
984{
985 struct server *srv;
986 const char *maxconn;
987 const char *err;
988
989 srv = hlua_check_server(L, 1);
990 maxconn = luaL_checkstring(L, 2);
991
992 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
993 err = server_parse_maxconn_change_request(srv, maxconn);
994 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
995 if (!err)
996 lua_pushnil(L);
997 else
998 hlua_pushstrippedstring(L, err);
999 return 1;
1000}
1001
1002int hlua_server_get_maxconn(lua_State *L)
1003{
1004 struct server *srv;
1005
1006 srv = hlua_check_server(L, 1);
1007 lua_pushinteger(L, srv->maxconn);
1008 return 1;
1009}
1010
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001011int hlua_server_set_weight(lua_State *L)
1012{
1013 struct server *srv;
1014 const char *weight;
1015 const char *err;
1016
1017 srv = hlua_check_server(L, 1);
1018 weight = luaL_checkstring(L, 2);
1019
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001020 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001021 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001022 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001023 if (!err)
1024 lua_pushnil(L);
1025 else
1026 hlua_pushstrippedstring(L, err);
1027 return 1;
1028}
1029
1030int hlua_server_get_weight(lua_State *L)
1031{
1032 struct server *srv;
1033
1034 srv = hlua_check_server(L, 1);
1035 lua_pushinteger(L, srv->uweight);
1036 return 1;
1037}
1038
1039int hlua_server_set_addr(lua_State *L)
1040{
1041 struct server *srv;
1042 const char *addr;
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001043 const char *port;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001044 const char *err;
1045
1046 srv = hlua_check_server(L, 1);
1047 addr = luaL_checkstring(L, 2);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001048 if (lua_gettop(L) >= 3)
1049 port = luaL_checkstring(L, 3);
1050 else
1051 port = NULL;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001052
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001053 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001054 err = update_server_addr_port(srv, addr, port, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001055 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001056 if (!err)
1057 lua_pushnil(L);
1058 else
1059 hlua_pushstrippedstring(L, err);
1060 return 1;
1061}
1062
1063int hlua_server_shut_sess(lua_State *L)
1064{
1065 struct server *srv;
1066
1067 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001068 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001069 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001070 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001071 return 0;
1072}
1073
1074int hlua_server_set_drain(lua_State *L)
1075{
1076 struct server *srv;
1077
1078 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001079 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001080 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001081 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001082 return 0;
1083}
1084
1085int hlua_server_set_maint(lua_State *L)
1086{
1087 struct server *srv;
1088
1089 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001090 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001091 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001092 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001093 return 0;
1094}
1095
1096int hlua_server_set_ready(lua_State *L)
1097{
1098 struct server *srv;
1099
1100 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001101 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001102 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001103 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001104 return 0;
1105}
1106
1107int hlua_server_check_enable(lua_State *L)
1108{
1109 struct server *sv;
1110
1111 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001112 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001113 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001114 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001115 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001116 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001117 return 0;
1118}
1119
1120int hlua_server_check_disable(lua_State *L)
1121{
1122 struct server *sv;
1123
1124 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001125 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001126 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001127 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001128 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001129 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001130 return 0;
1131}
1132
1133int hlua_server_check_force_up(lua_State *L)
1134{
1135 struct server *sv;
1136
1137 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001138 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001139 if (!(sv->track)) {
1140 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001141 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001142 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001143 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001144 return 0;
1145}
1146
1147int hlua_server_check_force_nolb(lua_State *L)
1148{
1149 struct server *sv;
1150
1151 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001152 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001153 if (!(sv->track)) {
1154 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001155 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001156 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001157 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001158 return 0;
1159}
1160
1161int hlua_server_check_force_down(lua_State *L)
1162{
1163 struct server *sv;
1164
1165 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001166 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001167 if (!(sv->track)) {
1168 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001169 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001170 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001171 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001172 return 0;
1173}
1174
1175int hlua_server_agent_enable(lua_State *L)
1176{
1177 struct server *sv;
1178
1179 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001180 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001181 if (sv->agent.state & CHK_ST_CONFIGURED) {
1182 sv->agent.state |= CHK_ST_ENABLED;
1183 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001184 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001185 return 0;
1186}
1187
1188int hlua_server_agent_disable(lua_State *L)
1189{
1190 struct server *sv;
1191
1192 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001193 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001194 if (sv->agent.state & CHK_ST_CONFIGURED) {
1195 sv->agent.state &= ~CHK_ST_ENABLED;
1196 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001197 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001198 return 0;
1199}
1200
1201int hlua_server_agent_force_up(lua_State *L)
1202{
1203 struct server *sv;
1204
1205 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001206 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001207 if (sv->agent.state & CHK_ST_ENABLED) {
1208 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001209 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001210 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001211 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001212 return 0;
1213}
1214
1215int hlua_server_agent_force_down(lua_State *L)
1216{
1217 struct server *sv;
1218
1219 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001220 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001221 if (sv->agent.state & CHK_ST_ENABLED) {
1222 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001223 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001224 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001225 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001226 return 0;
1227}
1228
Thierry Fournierf61aa632016-02-19 20:56:00 +01001229int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1230{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001231 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001232 struct listener *lst;
1233 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001234 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001235
Thierry Fournierf61aa632016-02-19 20:56:00 +01001236 lua_newtable(L);
1237
1238 /* Pop a class sesison metatable and affect it to the userdata. */
1239 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1240 lua_setmetatable(L, -2);
1241
1242 lua_pushlightuserdata(L, px);
1243 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001244
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001245 /* Add proxy name. */
1246 lua_pushstring(L, "name");
1247 lua_pushstring(L, px->id);
1248 lua_settable(L, -3);
1249
Baptiste Assmann46c72552017-10-26 21:51:58 +02001250 /* Add proxy uuid. */
1251 lua_pushstring(L, "uuid");
1252 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1253 lua_pushstring(L, buffer);
1254 lua_settable(L, -3);
1255
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001256 /* Browse and register servers. */
1257 lua_pushstring(L, "servers");
1258 lua_newtable(L);
1259 for (srv = px->srv; srv; srv = srv->next) {
1260 lua_pushstring(L, srv->id);
1261 hlua_fcn_new_server(L, srv);
1262 lua_settable(L, -3);
1263 }
1264 lua_settable(L, -3);
1265
Thierry Fournierff480422016-02-25 08:36:46 +01001266 /* Browse and register listeners. */
1267 lua_pushstring(L, "listeners");
1268 lua_newtable(L);
1269 lid = 1;
1270 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1271 if (lst->name)
1272 lua_pushstring(L, lst->name);
1273 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001274 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001275 lid++;
1276 lua_pushstring(L, buffer);
1277 }
1278 hlua_fcn_new_listener(L, lst);
1279 lua_settable(L, -3);
1280 }
1281 lua_settable(L, -3);
1282
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001283 if (px->table && px->table->id) {
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001284 lua_pushstring(L, "stktable");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001285 hlua_fcn_new_stktable(L, px->table);
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001286 lua_settable(L, -3);
1287 }
1288
Thierry Fournierf61aa632016-02-19 20:56:00 +01001289 return 1;
1290}
1291
1292static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1293{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001294 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001295}
1296
1297int hlua_proxy_pause(lua_State *L)
1298{
1299 struct proxy *px;
1300
1301 px = hlua_check_proxy(L, 1);
1302 pause_proxy(px);
1303 return 0;
1304}
1305
1306int hlua_proxy_resume(lua_State *L)
1307{
1308 struct proxy *px;
1309
1310 px = hlua_check_proxy(L, 1);
1311 resume_proxy(px);
1312 return 0;
1313}
1314
1315int hlua_proxy_stop(lua_State *L)
1316{
1317 struct proxy *px;
1318
1319 px = hlua_check_proxy(L, 1);
1320 stop_proxy(px);
1321 return 0;
1322}
1323
1324int hlua_proxy_get_cap(lua_State *L)
1325{
1326 struct proxy *px;
1327 const char *str;
1328
1329 px = hlua_check_proxy(L, 1);
1330 str = proxy_cap_str(px->cap);
1331 lua_pushstring(L, str);
1332 return 1;
1333}
1334
1335int hlua_proxy_get_stats(lua_State *L)
1336{
1337 struct proxy *px;
1338 int i;
1339
1340 px = hlua_check_proxy(L, 1);
1341 if (px->cap & PR_CAP_BE)
Willy Tarreau708c4162019-10-09 10:19:16 +02001342 stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001343 else
1344 stats_fill_fe_stats(px, stats, STATS_LEN);
1345 lua_newtable(L);
1346 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
Willy Tarreaueaa55372019-10-09 07:39:11 +02001347 lua_pushstring(L, stat_fields[i].name);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001348 hlua_fcn_pushfield(L, &stats[i]);
1349 lua_settable(L, -3);
1350 }
1351 return 1;
1352}
1353
1354int hlua_proxy_get_mode(lua_State *L)
1355{
1356 struct proxy *px;
1357 const char *str;
1358
1359 px = hlua_check_proxy(L, 1);
1360 str = proxy_mode_str(px->mode);
1361 lua_pushstring(L, str);
1362 return 1;
1363}
1364
1365int hlua_proxy_shut_bcksess(lua_State *L)
1366{
1367 struct proxy *px;
1368
1369 px = hlua_check_proxy(L, 1);
1370 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1371 return 0;
1372}
1373
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001374int hlua_fcn_post_init(lua_State *L)
1375{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001376 struct proxy *px;
1377
1378 /* get core array. */
1379 if (lua_getglobal(L, "core") != LUA_TTABLE)
1380 lua_error(L);
1381
1382 /* Create proxies entry. */
1383 lua_pushstring(L, "proxies");
1384 lua_newtable(L);
1385
1386 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001387 for (px = proxies_list; px; px = px->next) {
Thierry Fournierf61aa632016-02-19 20:56:00 +01001388 lua_pushstring(L, px->id);
1389 hlua_fcn_new_proxy(L, px);
1390 lua_settable(L, -3);
1391 }
1392
1393 /* push "proxies" in "core" */
1394 lua_settable(L, -3);
1395
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001396 /* Create proxies entry. */
1397 lua_pushstring(L, "frontends");
1398 lua_newtable(L);
1399
1400 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001401 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001402 if (!(px->cap & PR_CAP_FE))
1403 continue;
1404 lua_pushstring(L, px->id);
1405 hlua_fcn_new_proxy(L, px);
1406 lua_settable(L, -3);
1407 }
1408
1409 /* push "frontends" in "core" */
1410 lua_settable(L, -3);
1411
1412 /* Create proxies entry. */
1413 lua_pushstring(L, "backends");
1414 lua_newtable(L);
1415
1416 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001417 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001418 if (!(px->cap & PR_CAP_BE))
1419 continue;
1420 lua_pushstring(L, px->id);
1421 hlua_fcn_new_proxy(L, px);
1422 lua_settable(L, -3);
1423 }
1424
1425 /* push "backend" in "core" */
1426 lua_settable(L, -3);
1427
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001428 return 1;
1429}
1430
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001431/* This Lua function take a string, a list of separators.
1432 * It tokenize the input string using the list of separators
1433 * as separator.
1434 *
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001435 * The functionreturns a table filled with tokens.
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001436 */
1437int hlua_tokenize(lua_State *L)
1438{
1439 const char *str;
1440 const char *sep;
1441 int index;
1442 const char *token;
1443 const char *p;
1444 const char *c;
1445 int ignore_empty;
1446
1447 ignore_empty = 0;
1448
1449 str = luaL_checkstring(L, 1);
1450 sep = luaL_checkstring(L, 2);
1451 if (lua_gettop(L) == 3)
1452 ignore_empty = hlua_checkboolean(L, 3);
1453
1454 lua_newtable(L);
1455 index = 1;
1456 token = str;
1457 p = str;
1458 while(1) {
1459 for (c = sep; *c != '\0'; c++)
1460 if (*p == *c)
1461 break;
1462 if (*p == *c) {
1463 if ((!ignore_empty) || (p - token > 0)) {
1464 lua_pushlstring(L, token, p - token);
1465 lua_rawseti(L, -2, index);
1466 index++;
1467 }
1468 token = p + 1;
1469 }
1470 if (*p == '\0')
1471 break;
1472 p++;
1473 }
1474
1475 return 1;
1476}
1477
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001478int hlua_parse_addr(lua_State *L)
1479{
1480 struct hlua_addr *addr;
1481 const char *str = luaL_checkstring(L, 1);
1482 unsigned char mask;
1483
1484 addr = lua_newuserdata(L, sizeof(struct hlua_addr));
1485 if (!addr) {
1486 lua_pushnil(L);
1487 return 1;
1488 }
1489
1490 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
1491 addr->type = AF_INET;
1492 return 1;
1493 }
1494
1495 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1496 len2mask6(mask, &addr->addr.v6.mask);
1497 addr->type = AF_INET6;
1498 return 1;
1499 }
1500
1501 lua_pop(L, 1);
1502 lua_pushnil(L);
1503 return 1;
1504}
1505
1506int hlua_match_addr(lua_State *L)
1507{
1508 struct hlua_addr *addr1;
1509 struct hlua_addr *addr2;
1510
1511 if (!lua_isuserdata(L, 1) ||
1512 !lua_isuserdata(L, 2)) {
1513 lua_pushboolean(L, 0);
1514 return 1;
1515 }
1516
1517 addr1 = lua_touserdata(L, 1);
1518 addr2 = lua_touserdata(L, 2);
1519
1520 if (addr1->type != addr2->type) {
1521 lua_pushboolean(L, 0);
1522 return 1;
1523 }
1524
1525 if (addr1->type == AF_INET) {
1526 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1527 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1528 lua_pushboolean(L, 1);
1529 return 1;
1530 }
1531 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001532 int i;
1533
1534 for (i = 0; i < 16; i += 4) {
Willy Tarreau26474c42020-02-25 10:02:51 +01001535 if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) &
1536 read_u32(&addr2->addr.v6.mask.s6_addr[i])) !=
1537 (read_u32(&addr2->addr.v6.ip.s6_addr[i]) &
1538 read_u32(&addr1->addr.v6.mask.s6_addr[i])))
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001539 break;
1540 }
1541 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001542 lua_pushboolean(L, 1);
1543 return 1;
1544 }
1545 }
1546
1547 lua_pushboolean(L, 0);
1548 return 1;
1549}
1550
Dragan Dosen26743032019-04-30 15:54:36 +02001551static struct my_regex **hlua_check_regex(lua_State *L, int ud)
Thierry FOURNIER31904272017-10-25 12:59:51 +02001552{
1553 return (hlua_checkudata(L, ud, class_regex_ref));
1554}
1555
1556static int hlua_regex_comp(struct lua_State *L)
1557{
Dragan Dosen26743032019-04-30 15:54:36 +02001558 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001559 const char *str;
1560 int cs;
1561 char *err;
1562
1563 str = luaL_checkstring(L, 1);
1564 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1565 cs = lua_toboolean(L, 2);
1566
1567 regex = lua_newuserdata(L, sizeof(*regex));
1568
1569 err = NULL;
Dragan Dosen26743032019-04-30 15:54:36 +02001570 if (!(*regex = regex_comp(str, cs, 1, &err))) {
Thierry FOURNIER31904272017-10-25 12:59:51 +02001571 lua_pushboolean(L, 0); /* status error */
1572 lua_pushstring(L, err); /* Reason */
1573 free(err);
1574 return 2;
1575 }
1576
1577 lua_pushboolean(L, 1); /* Status ok */
1578
1579 /* Create object */
1580 lua_newtable(L);
1581 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1582 lua_rawseti(L, -2, 0);
1583 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1584 lua_setmetatable(L, -2);
1585 return 2;
1586}
1587
1588static int hlua_regex_exec(struct lua_State *L)
1589{
Dragan Dosen26743032019-04-30 15:54:36 +02001590 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001591 const char *str;
1592 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001593 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001594
1595 regex = hlua_check_regex(L, 1);
1596 str = luaL_checklstring(L, 2, &len);
1597
Dragan Dosen26743032019-04-30 15:54:36 +02001598 if (!*regex) {
1599 lua_pushboolean(L, 0);
1600 return 1;
1601 }
1602
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001603 /* Copy the string because regex_exec2 require a 'char *'
1604 * and not a 'const char *'.
1605 */
1606 tmp = get_trash_chunk();
1607 if (len >= tmp->size) {
1608 lua_pushboolean(L, 0);
1609 return 1;
1610 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001611 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001612
Dragan Dosen26743032019-04-30 15:54:36 +02001613 lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001614
1615 return 1;
1616}
1617
1618static int hlua_regex_match(struct lua_State *L)
1619{
Dragan Dosen26743032019-04-30 15:54:36 +02001620 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001621 const char *str;
1622 size_t len;
1623 regmatch_t pmatch[20];
1624 int ret;
1625 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001626 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001627
1628 regex = hlua_check_regex(L, 1);
1629 str = luaL_checklstring(L, 2, &len);
1630
Dragan Dosen26743032019-04-30 15:54:36 +02001631 if (!*regex) {
1632 lua_pushboolean(L, 0);
1633 return 1;
1634 }
1635
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001636 /* Copy the string because regex_exec2 require a 'char *'
1637 * and not a 'const char *'.
1638 */
1639 tmp = get_trash_chunk();
1640 if (len >= tmp->size) {
1641 lua_pushboolean(L, 0);
1642 return 1;
1643 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001644 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001645
Dragan Dosen26743032019-04-30 15:54:36 +02001646 ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001647 lua_pushboolean(L, ret);
1648 lua_newtable(L);
1649 if (ret) {
1650 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1651 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1652 lua_rawseti(L, -2, i + 1);
1653 }
1654 }
1655 return 2;
1656}
1657
1658static int hlua_regex_free(struct lua_State *L)
1659{
Dragan Dosen26743032019-04-30 15:54:36 +02001660 struct my_regex **regex;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001661
1662 regex = hlua_check_regex(L, 1);
Dragan Dosen26743032019-04-30 15:54:36 +02001663 regex_free(*regex);
1664 *regex = NULL;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001665 return 0;
1666}
1667
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001668int hlua_fcn_reg_core_fcn(lua_State *L)
1669{
Thierry Fournier1de16592016-01-27 09:49:07 +01001670 if (!hlua_concat_init(L))
1671 return 0;
1672
Thierry Fournier4f99b272016-02-22 08:40:02 +01001673 hlua_class_function(L, "now", hlua_now);
1674 hlua_class_function(L, "http_date", hlua_http_date);
1675 hlua_class_function(L, "imf_date", hlua_imf_date);
1676 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1677 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1678 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001679 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001680 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1681 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001682 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001683
Thierry FOURNIER31904272017-10-25 12:59:51 +02001684 /* Create regex object. */
1685 lua_newtable(L);
1686 hlua_class_function(L, "new", hlua_regex_comp);
1687
1688 lua_newtable(L); /* The metatable. */
1689 lua_pushstring(L, "__index");
1690 lua_newtable(L);
1691 hlua_class_function(L, "exec", hlua_regex_exec);
1692 hlua_class_function(L, "match", hlua_regex_match);
1693 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1694 hlua_class_function(L, "__gc", hlua_regex_free);
1695
1696 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1697 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1698
1699 lua_setmetatable(L, -2);
1700 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1701
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001702 /* Create stktable object. */
1703 lua_newtable(L);
1704 lua_pushstring(L, "__index");
1705 lua_newtable(L);
1706 hlua_class_function(L, "info", hlua_stktable_info);
1707 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1708 hlua_class_function(L, "dump", hlua_stktable_dump);
1709 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1710 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1711
Thierry Fournierff480422016-02-25 08:36:46 +01001712 /* Create listener object. */
1713 lua_newtable(L);
1714 lua_pushstring(L, "__index");
1715 lua_newtable(L);
1716 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1717 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1718 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1719
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001720 /* Create server object. */
1721 lua_newtable(L);
1722 lua_pushstring(L, "__index");
1723 lua_newtable(L);
1724 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001725 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1726 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001727 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1728 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1729 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1730 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1731 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1732 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1733 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1734 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1735 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1736 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1737 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1738 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1739 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1740 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1741 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1742 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1743 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1744 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1745 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1746 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1747
Thierry Fournierf61aa632016-02-19 20:56:00 +01001748 /* Create proxy object. */
1749 lua_newtable(L);
1750 lua_pushstring(L, "__index");
1751 lua_newtable(L);
1752 hlua_class_function(L, "pause", hlua_proxy_pause);
1753 hlua_class_function(L, "resume", hlua_proxy_resume);
1754 hlua_class_function(L, "stop", hlua_proxy_stop);
1755 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1756 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1757 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1758 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1759 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1760 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1761
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001762 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001763}