blob: 65857dc3200b6c693896af7dd6b59f122e256488 [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
Thierry Fournierb1f46562016-01-21 09:46:15 +010021#include <common/time.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010022#include <common/uri_auth.h>
Thierry Fournierb1f46562016-01-21 09:46:15 +010023
William Lallemand9ed62032016-11-21 17:49:11 +010024#include <types/cli.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010025#include <types/hlua.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010026#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010027#include <types/stats.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010028
Willy Tarreau35b51c62018-09-10 15:38:55 +020029#include <proto/h1.h>
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
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020043#define MAX_STK_FILTER_LEN 4
Thierry Fournierf61aa632016-02-19 20:56:00 +010044#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010045
Thierry FOURNIERffbad792017-07-12 11:39:04 +020046static THREAD_LOCAL struct field stats[STATS_LEN];
Thierry Fourniereea77c02016-03-18 08:47:13 +010047
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010048int hlua_checkboolean(lua_State *L, int index)
49{
50 if (!lua_isboolean(L, index))
51 luaL_argerror(L, index, "boolean expected");
52 return lua_toboolean(L, index);
53}
54
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020055/* Helper to push unsigned integers to Lua stack, respecting Lua limitations */
56static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
57{
58#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
59 lua_pushinteger(L, val);
60#else
61 if (val > INT_MAX)
62 lua_pushnumber(L, (lua_Number)val);
63 else
64 lua_pushinteger(L, (int)val);
65#endif
66 return 1;
67}
68
69/* Helper to push unsigned long long to Lua stack, respecting Lua limitations */
70static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
71#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
72 /* 64 bits case, U64 is supported until LLONG_MAX */
73 if (val > LLONG_MAX)
74 lua_pushnumber(L, (lua_Number)val);
75 else
76 lua_pushinteger(L, val);
77#else
78 /* 32 bits case, U64 is supported until INT_MAX */
79 if (val > INT_MAX)
80 lua_pushnumber(L, (lua_Number)val);
81 else
82 lua_pushinteger(L, (int)val);
83#endif
84 return 1;
85}
86
Thierry Fournier8b0d6e12016-03-16 18:29:13 +010087/* This function gets a struct field and convert it in Lua
88 * variable. The variable is pushed at the top of the stak.
89 */
90int hlua_fcn_pushfield(lua_State *L, struct field *field)
91{
92 /* The lua_Integer is always signed. Its length depends on
93 * compilation opions, so the followinfg code is conditionned
94 * by some macros. Windows maros are not supported.
95 * If the number cannot be represented as integer, we try to
96 * convert to float.
97 */
98 switch (field_format(field, 0)) {
99
100 case FF_EMPTY:
101 lua_pushnil(L);
102 return 1;
103
104 case FF_S32:
105 /* S32 is always supported. */
106 lua_pushinteger(L, field->u.s32);
107 return 1;
108
109 case FF_U32:
110#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
111 /* 64 bits case, U32 is always supported */
112 lua_pushinteger(L, field->u.u32);
113#else
114 /* 32 bits case, U32 is supported until INT_MAX. */
115 if (field->u.u32 > INT_MAX)
116 lua_pushnumber(L, (lua_Number)field->u.u32);
117 else
118 lua_pushinteger(L, field->u.u32);
119#endif
120 return 1;
121
122 case FF_S64:
123#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
124 /* 64 bits case, S64 is always supported */
125 lua_pushinteger(L, field->u.s64);
126#else
127 /* 64 bits case, S64 is supported beetween INT_MIN and INT_MAX */
128 if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
129 lua_pushnumber(L, (lua_Number)field->u.s64);
130 else
131 lua_pushinteger(L, (int)field->u.s64);
132#endif
133 return 1;
134
135 case FF_U64:
136#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
137 /* 64 bits case, U64 is supported until LLONG_MAX */
138 if (field->u.u64 > LLONG_MAX)
139 lua_pushnumber(L, (lua_Number)field->u.u64);
140 else
141 lua_pushinteger(L, field->u.u64);
142#else
143 /* 64 bits case, U64 is supported until INT_MAX */
144 if (field->u.u64 > INT_MAX)
145 lua_pushnumber(L, (lua_Number)field->u.u64);
146 else
147 lua_pushinteger(L, (int)field->u.u64);
148#endif
149 return 1;
150
151 case FF_STR:
152 lua_pushstring(L, field->u.str);
153 return 1;
154
155 default:
156 break;
157 }
158
159 /* Default case, never reached. */
160 lua_pushnil(L);
161 return 1;
162}
163
Thierry Fournier94ed1c12016-02-24 08:06:32 +0100164/* Some string are started or terminated by blank chars,
165 * this function removes the spaces, tabs, \r and
166 * \n at the begin and at the end of the string "str", and
167 * push the result in the lua stack.
168 * Returns a pointer to the Lua internal copy of the string.
169 */
170const char *hlua_pushstrippedstring(lua_State *L, const char *str)
171{
172 const char *p;
173 const char *e;
174
175 for (p = str; HTTP_IS_LWS(*p); p++);
176 for (e = p + strlen(p) - 1; e > p && HTTP_IS_LWS(*e); e--);
177
178 return lua_pushlstring(L, p, e - p);
179}
180
Thierry Fournierddd89882016-02-22 19:52:08 +0100181/* The three following functions are useful for adding entries
182 * in a table. These functions takes a string and respectively an
183 * integer, a string or a function and add it to the table in the
184 * top of the stack.
185 *
186 * These functions throws an error if no more stack size is
187 * available.
188 */
189void hlua_class_const_int(lua_State *L, const char *name, int value)
190{
Thierry Fournierddd89882016-02-22 19:52:08 +0100191 lua_pushstring(L, name);
192 lua_pushinteger(L, value);
193 lua_rawset(L, -3);
194}
195void hlua_class_const_str(lua_State *L, const char *name, const char *value)
196{
Thierry Fournierddd89882016-02-22 19:52:08 +0100197 lua_pushstring(L, name);
198 lua_pushstring(L, value);
199 lua_rawset(L, -3);
200}
201void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
202{
Thierry Fournierddd89882016-02-22 19:52:08 +0100203 lua_pushstring(L, name);
204 lua_pushcclosure(L, function, 0);
205 lua_rawset(L, -3);
206}
207
208/* This function returns a string containg the HAProxy object name. */
209int hlua_dump_object(struct lua_State *L)
210{
211 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
212 lua_pushfstring(L, "HAProxy class %s", name);
213 return 1;
214}
215
Thierry Fournier45e78d72016-02-19 18:34:46 +0100216/* This function register a table as metatable and. It names
217 * the metatable, and returns the associated reference.
218 * The original table is poped from the top of the stack.
219 * "name" is the referenced class name.
220 */
221int hlua_register_metatable(struct lua_State *L, char *name)
222{
223 /* Check the type of the top element. it must be
224 * a table.
225 */
226 if (lua_type(L, -1) != LUA_TTABLE)
227 luaL_error(L, "hlua_register_metatable() requires a type Table "
228 "in the top of the stack");
229
230 /* Add the __tostring function which identify the
231 * created object.
232 */
233 lua_pushstring(L, "__tostring");
234 lua_pushstring(L, name);
235 lua_pushcclosure(L, hlua_dump_object, 1);
236 lua_rawset(L, -3);
237
238 /* Register a named entry for the table. The table
239 * reference is copyed first because the function
240 * lua_setfield() pop the entry.
241 */
242 lua_pushvalue(L, -1);
243 lua_setfield(L, LUA_REGISTRYINDEX, name);
244
245 /* Creates the reference of the object. The
246 * function luaL_ref pop the top of the stack.
247 */
248 return luaL_ref(L, LUA_REGISTRYINDEX);
249}
250
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100251/* Return an object of the expected type, or throws an error. */
252void *hlua_checkudata(lua_State *L, int ud, int class_ref)
253{
254 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100255 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100256
257 /* Check if the stack entry is an array. */
258 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100259 luaL_argerror(L, ud, NULL);
260
261 /* pop the metatable of the referencecd object. */
262 if (!lua_getmetatable(L, ud))
263 luaL_argerror(L, ud, NULL);
264
265 /* pop the expected metatable. */
266 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
267
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100268 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100269 ret = lua_rawequal(L, -1, -2);
270 lua_pop(L, 2);
271 if (!ret)
272 luaL_argerror(L, ud, NULL);
273
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100274 /* Push on the stack at the entry [0] of the table. */
275 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100276
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100277 /* Check if this entry is userdata. */
278 p = lua_touserdata(L, -1);
279 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100280 luaL_argerror(L, ud, NULL);
281
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100282 /* Remove the entry returned by lua_rawgeti(). */
283 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100284
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100285 /* Return the associated struct. */
286 return p;
287}
288
Thierry Fournierb1f46562016-01-21 09:46:15 +0100289/* This function return the current date at epoch format in milliseconds. */
290int hlua_now(lua_State *L)
291{
292 lua_newtable(L);
293 lua_pushstring(L, "sec");
294 lua_pushinteger(L, now.tv_sec);
295 lua_rawset(L, -3);
296 lua_pushstring(L, "usec");
297 lua_pushinteger(L, now.tv_usec);
298 lua_rawset(L, -3);
299 return 1;
300}
301
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100302/* This functions expects a Lua string as HTTP date, parse it and
303 * returns an integer containing the epoch format of the date, or
304 * nil if the parsing fails.
305 */
306static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
307{
308 const char *str;
309 size_t len;
310 struct tm tm;
311 time_t time;
312
313 str = luaL_checklstring(L, 1, &len);
314
315 if (!fcn(str, len, &tm)) {
316 lua_pushnil(L);
317 return 1;
318 }
319
320 /* This function considers the content of the broken-down time
321 * is exprimed in the UTC timezone. timegm don't care about
322 * the gnu variable tm_gmtoff. If gmtoff is set, or if you know
323 * the timezone from the broken-down time, it must be fixed
324 * after the conversion.
325 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200326 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100327 if (time == -1) {
328 lua_pushnil(L);
329 return 1;
330 }
331
332 lua_pushinteger(L, (int)time);
333 return 1;
334}
335static int hlua_http_date(lua_State *L)
336{
337 return hlua_parse_date(L, parse_http_date);
338}
339static int hlua_imf_date(lua_State *L)
340{
341 return hlua_parse_date(L, parse_imf_date);
342}
343static int hlua_rfc850_date(lua_State *L)
344{
345 return hlua_parse_date(L, parse_rfc850_date);
346}
347static int hlua_asctime_date(lua_State *L)
348{
349 return hlua_parse_date(L, parse_asctime_date);
350}
351
Thierry Fourniereea77c02016-03-18 08:47:13 +0100352static int hlua_get_info(lua_State *L)
353{
354 int i;
355
356 stats_fill_info(stats, STATS_LEN);
357
358 lua_newtable(L);
359 for (i=0; i<INF_TOTAL_FIELDS; i++) {
360 lua_pushstring(L, info_field_names[i]);
361 hlua_fcn_pushfield(L, &stats[i]);
362 lua_settable(L, -3);
363 }
364 return 1;
365}
366
Thierry Fournier49d48422016-02-19 12:09:29 +0100367static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100368{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200369 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100370}
371
372static int hlua_concat_add(lua_State *L)
373{
Thierry Fournier49d48422016-02-19 12:09:29 +0100374 struct hlua_concat *b;
375 char *buffer;
376 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100377 const char *str;
378 size_t l;
379
380 /* First arg must be a concat object. */
381 b = hlua_check_concat(L, 1);
382
383 /* Second arg must be a string. */
384 str = luaL_checklstring(L, 2, &l);
385
Thierry Fournier49d48422016-02-19 12:09:29 +0100386 /* Get the buffer. */
387 lua_rawgeti(L, 1, 1);
388 buffer = lua_touserdata(L, -1);
389 lua_pop(L, 1);
390
391 /* Update the buffer size if it s required. The old buffer
392 * is crushed by the new in the object array, so it will
393 * be deleted by the GC.
394 * Note that in the first loop, the "new" variable is only
395 * used as a flag.
396 */
397 new = NULL;
398 while (b->size - b->len < l) {
399 b->size += HLUA_CONCAT_BLOCSZ;
400 new = buffer;
401 }
402 if (new) {
403 new = lua_newuserdata(L, b->size);
404 memcpy(new, buffer, b->len);
405 lua_rawseti(L, 1, 1);
406 buffer = new;
407 }
408
409 /* Copy string, and update metadata. */
410 memcpy(buffer + b->len, str, l);
411 b->len += l;
Thierry Fournier1de16592016-01-27 09:49:07 +0100412 return 0;
413}
414
415static int hlua_concat_dump(lua_State *L)
416{
Thierry Fournier49d48422016-02-19 12:09:29 +0100417 struct hlua_concat *b;
418 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100419
420 /* First arg must be a concat object. */
421 b = hlua_check_concat(L, 1);
422
Thierry Fournier49d48422016-02-19 12:09:29 +0100423 /* Get the buffer. */
424 lua_rawgeti(L, 1, 1);
425 buffer = lua_touserdata(L, -1);
426 lua_pop(L, 1);
427
Thierry Fournier1de16592016-01-27 09:49:07 +0100428 /* Push the soncatenated strng in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100429 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100430 return 1;
431}
432
433int hlua_concat_new(lua_State *L)
434{
Thierry Fournier49d48422016-02-19 12:09:29 +0100435 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100436
437 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200438 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100439 b->size = HLUA_CONCAT_BLOCSZ;
440 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100441 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100442 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
443 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100444
445 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
446 lua_setmetatable(L, -2);
447
Thierry Fournier1de16592016-01-27 09:49:07 +0100448 return 1;
449}
450
451static int concat_tostring(lua_State *L)
452{
453 const void *ptr = lua_topointer(L, 1);
454 lua_pushfstring(L, "Concat object: %p", ptr);
455 return 1;
456}
457
458static int hlua_concat_init(lua_State *L)
459{
460 /* Creates the buffered concat object. */
461 lua_newtable(L);
462
463 lua_pushstring(L, "__tostring");
464 lua_pushcclosure(L, concat_tostring, 0);
465 lua_settable(L, -3);
466
467 lua_pushstring(L, "__index"); /* Creates the index entry. */
468 lua_newtable(L); /* The "__index" content. */
469
470 lua_pushstring(L, "add");
471 lua_pushcclosure(L, hlua_concat_add, 0);
472 lua_settable(L, -3);
473
474 lua_pushstring(L, "dump");
475 lua_pushcclosure(L, hlua_concat_dump, 0);
476 lua_settable(L, -3);
477
478 lua_settable(L, -3); /* Sets the __index entry. */
479 class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
480
481 return 1;
482}
483
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200484int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
485{
486 lua_newtable(L);
487
488 /* Pop a class stktbl metatable and affect it to the userdata. */
489 lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
490 lua_setmetatable(L, -2);
491
492 lua_pushlightuserdata(L, tbl);
493 lua_rawseti(L, -2, 0);
494 return 1;
495}
496
497static struct stktable *hlua_check_stktable(lua_State *L, int ud)
498{
499 return hlua_checkudata(L, ud, class_stktable_ref);
500}
501
502/* Extract stick table attributes into Lua table */
503int hlua_stktable_info(lua_State *L)
504{
505 struct stktable *tbl;
506 int dt;
507
508 tbl = hlua_check_stktable(L, 1);
509
510 if (!tbl->id) {
511 lua_pushnil(L);
512 return 1;
513 }
514
515 lua_newtable(L);
516
517 lua_pushstring(L, "type");
518 lua_pushstring(L, stktable_types[tbl->type].kw);
519 lua_settable(L, -3);
520
521 lua_pushstring(L, "length");
522 lua_pushinteger(L, tbl->key_size);
523 lua_settable(L, -3);
524
525 lua_pushstring(L, "size");
526 hlua_fcn_pushunsigned(L, tbl->size);
527 lua_settable(L, -3);
528
529 lua_pushstring(L, "used");
530 hlua_fcn_pushunsigned(L, tbl->current);
531 lua_settable(L, -3);
532
533 lua_pushstring(L, "nopurge");
534 lua_pushboolean(L, tbl->nopurge > 0);
535 lua_settable(L, -3);
536
537 lua_pushstring(L, "expire");
538 lua_pushinteger(L, tbl->expire);
539 lua_settable(L, -3);
540
541 /* Save data types periods (if applicable) in 'data' table */
542 lua_pushstring(L, "data");
543 lua_newtable(L);
544
545 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
546 if (tbl->data_ofs[dt] == 0)
547 continue;
548
549 lua_pushstring(L, stktable_data_types[dt].name);
550
551 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
552 lua_pushinteger(L, tbl->data_arg[dt].u);
553 else
554 lua_pushinteger(L, -1);
555
556 lua_settable(L, -3);
557 }
558
559 lua_settable(L, -3);
560
561 return 1;
562}
563
564/* Helper to get extract stick table entry into Lua table */
565static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts)
566{
567 int dt;
568 void *ptr;
569
570 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
571
572 if (t->data_ofs[dt] == 0)
573 continue;
574
575 lua_pushstring(L, stktable_data_types[dt].name);
576
577 ptr = stktable_data_ptr(t, ts, dt);
578 switch (stktable_data_types[dt].std_type) {
579 case STD_T_SINT:
580 lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
581 break;
582 case STD_T_UINT:
583 hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint));
584 break;
585 case STD_T_ULL:
586 hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull));
587 break;
588 case STD_T_FRQP:
589 lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
590 t->data_arg[dt].u));
591 break;
592 }
593
594 lua_settable(L, -3);
595 }
596}
597
598/* Looks in table <t> for a sticky session matching key <key>
599 * Returns table with session data or nil
600 *
601 * The returned table always contains 'use' and 'expire' (integer) fields.
602 * For frequency/rate counters, each data entry is returned as table with
603 * 'value' and 'period' fields.
604 */
605int hlua_stktable_lookup(lua_State *L)
606{
607 struct stktable *t;
608 struct sample smp;
609 struct stktable_key *skey;
610 struct stksess *ts;
611
612 t = hlua_check_stktable(L, 1);
613 smp.data.type = SMP_T_STR;
614 smp.flags = SMP_F_CONST;
615 smp.data.u.str.area = (char *)luaL_checkstring(L, 2);
616
617 skey = smp_to_stkey(&smp, t);
618 if (!skey) {
619 lua_pushnil(L);
620 return 1;
621 }
622
623 ts = stktable_lookup_key(t, skey);
624 if (!ts) {
625 lua_pushnil(L);
626 return 1;
627 }
628
629 lua_newtable(L);
630 lua_pushstring(L, "use");
631 lua_pushinteger(L, ts->ref_cnt - 1);
632 lua_settable(L, -3);
633
634 lua_pushstring(L, "expire");
635 lua_pushinteger(L, tick_remain(now_ms, ts->expire));
636 lua_settable(L, -3);
637
638 hlua_stktable_entry(L, t, ts);
639 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
640 ts->ref_cnt--;
641 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
642
643 return 1;
644}
645
646struct stk_filter {
647 long long val;
648 int type;
649 int op;
650};
651
652
653/* Helper for returning errors to callers using Lua convention (nil, err) */
654static int hlua_error(lua_State *L, const char *fmt, ...) {
655 char buf[256];
656 int len;
657 va_list args;
658 va_start(args, fmt);
659 len = vsnprintf(buf, sizeof(buf), fmt, args);
660 va_end(args);
661
662 if (len < 0) {
663 ha_alert("hlua_error(): Could not write error message.\n");
664 lua_pushnil(L);
665 return 1;
666 } else if (len >= sizeof(buf))
667 ha_alert("hlua_error(): Error message was truncated.\n");
668
669 lua_pushnil(L);
670 lua_pushstring(L, buf);
671
672 return 2;
673}
674
675/* Dump the contents of stick table <t>*/
676int hlua_stktable_dump(lua_State *L)
677{
678 struct stktable *t;
679 struct ebmb_node *eb;
680 struct ebmb_node *n;
681 struct stksess *ts;
682 int type;
683 int op;
684 int dt;
685 long long val;
686 struct stk_filter filter[MAX_STK_FILTER_LEN];
687 int filter_count = 0;
688 int i;
689 int skip_entry;
690 void *ptr;
691
692 t = hlua_check_stktable(L, 1);
693 type = lua_type(L, 2);
694
695 switch (type) {
696 case LUA_TNONE:
697 case LUA_TNIL:
698 break;
699 case LUA_TTABLE:
700 lua_pushnil(L);
701 while (lua_next(L, 2) != 0) {
702 int entry_idx = 0;
703
704 if (filter_count >= MAX_STK_FILTER_LEN)
705 return hlua_error(L, "Filter table too large (len > %d)", MAX_STK_FILTER_LEN);
706
707 if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3)
708 return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1);
709
710 lua_pushnil(L);
711 while (lua_next(L, -2) != 0) {
712 switch (entry_idx) {
713 case 0:
714 if (lua_type(L, -1) != LUA_TSTRING)
715 return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1);
716
717 dt = stktable_get_data_type((char *)lua_tostring(L, -1));
718 if (dt < 0 || t->data_ofs[dt] == 0)
719 return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1);
720 filter[filter_count].type = dt;
721 break;
722 case 1:
723 if (lua_type(L, -1) != LUA_TSTRING)
724 return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1);
725
726 op = get_std_op(lua_tostring(L, -1));
727 if (op < 0)
728 return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1);
729 filter[filter_count].op = op;
730 break;
731 case 2:
732 val = lua_tointeger(L, -1);
733 filter[filter_count].val = val;
734 filter_count++;
735 break;
736 default:
737 break;
738 }
739 entry_idx++;
740 lua_pop(L, 1);
741 }
742 lua_pop(L, 1);
743 }
744 break;
745 default:
746 return hlua_error(L, "filter table expected");
747 }
748
749 lua_newtable(L);
750
751 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
752 eb = ebmb_first(&t->keys);
753 for (n = eb; n; n = ebmb_next(n)) {
754 ts = ebmb_entry(n, struct stksess, key);
755 if (!ts) {
756 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
757 return 1;
758 }
759 ts->ref_cnt++;
760 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
761
762 /* multi condition/value filter */
763 skip_entry = 0;
764 for (i = 0; i < filter_count; i++) {
765 if (t->data_ofs[filter[i].type] == 0)
766 continue;
767
768 ptr = stktable_data_ptr(t, ts, filter[i].type);
769
770 switch (stktable_data_types[filter[i].type].std_type) {
771 case STD_T_SINT:
772 val = stktable_data_cast(ptr, std_t_sint);
773 break;
774 case STD_T_UINT:
775 val = stktable_data_cast(ptr, std_t_uint);
776 break;
777 case STD_T_ULL:
778 val = stktable_data_cast(ptr, std_t_ull);
779 break;
780 case STD_T_FRQP:
781 val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
782 t->data_arg[filter[i].type].u);
783 break;
784 default:
785 continue;
786 break;
787 }
788
789 op = filter[i].op;
790
791 if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
792 (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
793 (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
794 skip_entry = 1;
795 break;
796 }
797 }
798
799 if (skip_entry) {
800 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
801 ts->ref_cnt--;
802 continue;
803 }
804
805 if (t->type == SMP_T_IPV4) {
806 char addr[INET_ADDRSTRLEN];
807 inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr));
808 lua_pushstring(L, addr);
809 } else if (t->type == SMP_T_IPV6) {
810 char addr[INET6_ADDRSTRLEN];
811 inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr));
812 lua_pushstring(L, addr);
813 } else if (t->type == SMP_T_SINT) {
814 lua_pushinteger(L, *ts->key.key);
815 } else if (t->type == SMP_T_STR) {
816 lua_pushstring(L, (const char *)ts->key.key);
817 } else {
818 return hlua_error(L, "Unsupported stick table key type");
819 }
820
821 lua_newtable(L);
822 hlua_stktable_entry(L, t, ts);
823 lua_settable(L, -3);
824 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
825 ts->ref_cnt--;
826 }
827 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
828
829 return 1;
830}
831
Thierry Fournierff480422016-02-25 08:36:46 +0100832int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
833{
834 lua_newtable(L);
835
836 /* Pop a class sesison metatable and affect it to the userdata. */
837 lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref);
838 lua_setmetatable(L, -2);
839
840 lua_pushlightuserdata(L, lst);
841 lua_rawseti(L, -2, 0);
842 return 1;
843}
844
845static struct listener *hlua_check_listener(lua_State *L, int ud)
846{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200847 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100848}
849
850int hlua_listener_get_stats(lua_State *L)
851{
852 struct listener *li;
853 int i;
854
855 li = hlua_check_listener(L, 1);
856
Willy Tarreauc95bad52016-12-22 00:13:31 +0100857 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100858 lua_pushnil(L);
859 return 1;
860 }
861
Willy Tarreauc95bad52016-12-22 00:13:31 +0100862 stats_fill_li_stats(li->bind_conf->frontend, li, ST_SHLGNDS, stats, STATS_LEN);
Thierry Fournierff480422016-02-25 08:36:46 +0100863
864 lua_newtable(L);
865 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
866 lua_pushstring(L, stat_field_names[i]);
867 hlua_fcn_pushfield(L, &stats[i]);
868 lua_settable(L, -3);
869 }
870 return 1;
871
872}
873
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100874int hlua_fcn_new_server(lua_State *L, struct server *srv)
875{
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400876 char buffer[12];
877
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100878 lua_newtable(L);
879
880 /* Pop a class sesison metatable and affect it to the userdata. */
881 lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref);
882 lua_setmetatable(L, -2);
883
884 lua_pushlightuserdata(L, srv);
885 lua_rawseti(L, -2, 0);
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400886
887 /* Add server name. */
888 lua_pushstring(L, "name");
889 lua_pushstring(L, srv->id);
890 lua_settable(L, -3);
891
892 /* Add server puid. */
893 lua_pushstring(L, "puid");
894 snprintf(buffer, sizeof(buffer), "%d", srv->puid);
895 lua_pushstring(L, buffer);
896 lua_settable(L, -3);
897
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100898 return 1;
899}
900
901static struct server *hlua_check_server(lua_State *L, int ud)
902{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200903 return hlua_checkudata(L, ud, class_server_ref);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100904}
905
906int hlua_server_get_stats(lua_State *L)
907{
908 struct server *srv;
909 int i;
910
911 srv = hlua_check_server(L, 1);
912
913 if (!srv->proxy) {
914 lua_pushnil(L);
915 return 1;
916 }
917
918 stats_fill_sv_stats(srv->proxy, srv, ST_SHLGNDS, stats, STATS_LEN);
919
920 lua_newtable(L);
921 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
922 lua_pushstring(L, stat_field_names[i]);
923 hlua_fcn_pushfield(L, &stats[i]);
924 lua_settable(L, -3);
925 }
926 return 1;
927
928}
929
930int hlua_server_get_addr(lua_State *L)
931{
932 struct server *srv;
933 char addr[INET6_ADDRSTRLEN];
934 luaL_Buffer b;
935
936 srv = hlua_check_server(L, 1);
937
938 luaL_buffinit(L, &b);
939
940 switch (srv->addr.ss_family) {
941 case AF_INET:
942 inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr,
943 addr, INET_ADDRSTRLEN);
944 luaL_addstring(&b, addr);
945 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400946 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100947 luaL_addstring(&b, addr);
948 break;
949 case AF_INET6:
950 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400951 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100952 luaL_addstring(&b, addr);
953 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400954 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100955 luaL_addstring(&b, addr);
956 break;
957 case AF_UNIX:
958 luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path);
959 break;
960 default:
961 luaL_addstring(&b, "<unknown>");
962 break;
963 }
964
965 luaL_pushresult(&b);
966 return 1;
967}
968
969int hlua_server_is_draining(lua_State *L)
970{
971 struct server *srv;
972
973 srv = hlua_check_server(L, 1);
974 lua_pushinteger(L, server_is_draining(srv));
975 return 1;
976}
977
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400978int hlua_server_set_maxconn(lua_State *L)
979{
980 struct server *srv;
981 const char *maxconn;
982 const char *err;
983
984 srv = hlua_check_server(L, 1);
985 maxconn = luaL_checkstring(L, 2);
986
987 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
988 err = server_parse_maxconn_change_request(srv, maxconn);
989 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
990 if (!err)
991 lua_pushnil(L);
992 else
993 hlua_pushstrippedstring(L, err);
994 return 1;
995}
996
997int hlua_server_get_maxconn(lua_State *L)
998{
999 struct server *srv;
1000
1001 srv = hlua_check_server(L, 1);
1002 lua_pushinteger(L, srv->maxconn);
1003 return 1;
1004}
1005
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001006int hlua_server_set_weight(lua_State *L)
1007{
1008 struct server *srv;
1009 const char *weight;
1010 const char *err;
1011
1012 srv = hlua_check_server(L, 1);
1013 weight = luaL_checkstring(L, 2);
1014
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001015 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001016 err = server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001017 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001018 if (!err)
1019 lua_pushnil(L);
1020 else
1021 hlua_pushstrippedstring(L, err);
1022 return 1;
1023}
1024
1025int hlua_server_get_weight(lua_State *L)
1026{
1027 struct server *srv;
1028
1029 srv = hlua_check_server(L, 1);
1030 lua_pushinteger(L, srv->uweight);
1031 return 1;
1032}
1033
1034int hlua_server_set_addr(lua_State *L)
1035{
1036 struct server *srv;
1037 const char *addr;
1038 const char *err;
1039
1040 srv = hlua_check_server(L, 1);
1041 addr = luaL_checkstring(L, 2);
1042
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001043 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001044 err = server_parse_addr_change_request(srv, addr, "Lua script");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001045 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001046 if (!err)
1047 lua_pushnil(L);
1048 else
1049 hlua_pushstrippedstring(L, err);
1050 return 1;
1051}
1052
1053int hlua_server_shut_sess(lua_State *L)
1054{
1055 struct server *srv;
1056
1057 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001058 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001059 srv_shutdown_streams(srv, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001060 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001061 return 0;
1062}
1063
1064int hlua_server_set_drain(lua_State *L)
1065{
1066 struct server *srv;
1067
1068 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001069 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001070 srv_adm_set_drain(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001071 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001072 return 0;
1073}
1074
1075int hlua_server_set_maint(lua_State *L)
1076{
1077 struct server *srv;
1078
1079 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001080 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001081 srv_adm_set_maint(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001082 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001083 return 0;
1084}
1085
1086int hlua_server_set_ready(lua_State *L)
1087{
1088 struct server *srv;
1089
1090 srv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001091 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001092 srv_adm_set_ready(srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001093 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001094 return 0;
1095}
1096
1097int hlua_server_check_enable(lua_State *L)
1098{
1099 struct server *sv;
1100
1101 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001102 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001103 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001104 sv->check.state |= CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001105 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001106 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001107 return 0;
1108}
1109
1110int hlua_server_check_disable(lua_State *L)
1111{
1112 struct server *sv;
1113
1114 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001115 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001116 if (sv->check.state & CHK_ST_CONFIGURED) {
Adis Nezirovicceee9332017-07-26 09:19:06 +02001117 sv->check.state &= ~CHK_ST_ENABLED;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001118 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001119 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001120 return 0;
1121}
1122
1123int hlua_server_check_force_up(lua_State *L)
1124{
1125 struct server *sv;
1126
1127 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001128 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001129 if (!(sv->track)) {
1130 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001131 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001132 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001133 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001134 return 0;
1135}
1136
1137int hlua_server_check_force_nolb(lua_State *L)
1138{
1139 struct server *sv;
1140
1141 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001142 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001143 if (!(sv->track)) {
1144 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001145 srv_set_stopping(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001146 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001147 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001148 return 0;
1149}
1150
1151int hlua_server_check_force_down(lua_State *L)
1152{
1153 struct server *sv;
1154
1155 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001156 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001157 if (!(sv->track)) {
1158 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001159 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001160 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001161 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001162 return 0;
1163}
1164
1165int hlua_server_agent_enable(lua_State *L)
1166{
1167 struct server *sv;
1168
1169 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001170 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001171 if (sv->agent.state & CHK_ST_CONFIGURED) {
1172 sv->agent.state |= CHK_ST_ENABLED;
1173 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001174 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001175 return 0;
1176}
1177
1178int hlua_server_agent_disable(lua_State *L)
1179{
1180 struct server *sv;
1181
1182 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001183 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001184 if (sv->agent.state & CHK_ST_CONFIGURED) {
1185 sv->agent.state &= ~CHK_ST_ENABLED;
1186 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001187 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001188 return 0;
1189}
1190
1191int hlua_server_agent_force_up(lua_State *L)
1192{
1193 struct server *sv;
1194
1195 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001196 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001197 if (sv->agent.state & CHK_ST_ENABLED) {
1198 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02001199 srv_set_running(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001200 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001201 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001202 return 0;
1203}
1204
1205int hlua_server_agent_force_down(lua_State *L)
1206{
1207 struct server *sv;
1208
1209 sv = hlua_check_server(L, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001210 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001211 if (sv->agent.state & CHK_ST_ENABLED) {
1212 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02001213 srv_set_stopped(sv, "changed from Lua script", NULL);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001214 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001215 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001216 return 0;
1217}
1218
Thierry Fournierf61aa632016-02-19 20:56:00 +01001219int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
1220{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001221 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +01001222 struct listener *lst;
1223 int lid;
Willy Tarreau29d69802018-05-06 14:50:09 +02001224 char buffer[17];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001225
Thierry Fournierf61aa632016-02-19 20:56:00 +01001226 lua_newtable(L);
1227
1228 /* Pop a class sesison metatable and affect it to the userdata. */
1229 lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref);
1230 lua_setmetatable(L, -2);
1231
1232 lua_pushlightuserdata(L, px);
1233 lua_rawseti(L, -2, 0);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001234
Thierry FOURNIERf2bbe382017-07-24 13:59:22 +02001235 /* Add proxy name. */
1236 lua_pushstring(L, "name");
1237 lua_pushstring(L, px->id);
1238 lua_settable(L, -3);
1239
Baptiste Assmann46c72552017-10-26 21:51:58 +02001240 /* Add proxy uuid. */
1241 lua_pushstring(L, "uuid");
1242 snprintf(buffer, sizeof(buffer), "%d", px->uuid);
1243 lua_pushstring(L, buffer);
1244 lua_settable(L, -3);
1245
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001246 /* Browse and register servers. */
1247 lua_pushstring(L, "servers");
1248 lua_newtable(L);
1249 for (srv = px->srv; srv; srv = srv->next) {
1250 lua_pushstring(L, srv->id);
1251 hlua_fcn_new_server(L, srv);
1252 lua_settable(L, -3);
1253 }
1254 lua_settable(L, -3);
1255
Thierry Fournierff480422016-02-25 08:36:46 +01001256 /* Browse and register listeners. */
1257 lua_pushstring(L, "listeners");
1258 lua_newtable(L);
1259 lid = 1;
1260 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
1261 if (lst->name)
1262 lua_pushstring(L, lst->name);
1263 else {
Willy Tarreau29d69802018-05-06 14:50:09 +02001264 snprintf(buffer, sizeof(buffer), "sock-%d", lid);
Thierry Fournierff480422016-02-25 08:36:46 +01001265 lid++;
1266 lua_pushstring(L, buffer);
1267 }
1268 hlua_fcn_new_listener(L, lst);
1269 lua_settable(L, -3);
1270 }
1271 lua_settable(L, -3);
1272
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001273 if (px->table.id) {
1274 lua_pushstring(L, "stktable");
1275 hlua_fcn_new_stktable(L, &px->table);
1276 lua_settable(L, -3);
1277 }
1278
Thierry Fournierf61aa632016-02-19 20:56:00 +01001279 return 1;
1280}
1281
1282static struct proxy *hlua_check_proxy(lua_State *L, int ud)
1283{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001284 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +01001285}
1286
1287int hlua_proxy_pause(lua_State *L)
1288{
1289 struct proxy *px;
1290
1291 px = hlua_check_proxy(L, 1);
1292 pause_proxy(px);
1293 return 0;
1294}
1295
1296int hlua_proxy_resume(lua_State *L)
1297{
1298 struct proxy *px;
1299
1300 px = hlua_check_proxy(L, 1);
1301 resume_proxy(px);
1302 return 0;
1303}
1304
1305int hlua_proxy_stop(lua_State *L)
1306{
1307 struct proxy *px;
1308
1309 px = hlua_check_proxy(L, 1);
1310 stop_proxy(px);
1311 return 0;
1312}
1313
1314int hlua_proxy_get_cap(lua_State *L)
1315{
1316 struct proxy *px;
1317 const char *str;
1318
1319 px = hlua_check_proxy(L, 1);
1320 str = proxy_cap_str(px->cap);
1321 lua_pushstring(L, str);
1322 return 1;
1323}
1324
1325int hlua_proxy_get_stats(lua_State *L)
1326{
1327 struct proxy *px;
1328 int i;
1329
1330 px = hlua_check_proxy(L, 1);
1331 if (px->cap & PR_CAP_BE)
1332 stats_fill_be_stats(px, ST_SHLGNDS, stats, STATS_LEN);
1333 else
1334 stats_fill_fe_stats(px, stats, STATS_LEN);
1335 lua_newtable(L);
1336 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
1337 lua_pushstring(L, stat_field_names[i]);
1338 hlua_fcn_pushfield(L, &stats[i]);
1339 lua_settable(L, -3);
1340 }
1341 return 1;
1342}
1343
1344int hlua_proxy_get_mode(lua_State *L)
1345{
1346 struct proxy *px;
1347 const char *str;
1348
1349 px = hlua_check_proxy(L, 1);
1350 str = proxy_mode_str(px->mode);
1351 lua_pushstring(L, str);
1352 return 1;
1353}
1354
1355int hlua_proxy_shut_bcksess(lua_State *L)
1356{
1357 struct proxy *px;
1358
1359 px = hlua_check_proxy(L, 1);
1360 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
1361 return 0;
1362}
1363
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001364int hlua_fcn_post_init(lua_State *L)
1365{
Thierry Fournierf61aa632016-02-19 20:56:00 +01001366 struct proxy *px;
1367
1368 /* get core array. */
1369 if (lua_getglobal(L, "core") != LUA_TTABLE)
1370 lua_error(L);
1371
1372 /* Create proxies entry. */
1373 lua_pushstring(L, "proxies");
1374 lua_newtable(L);
1375
1376 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001377 for (px = proxies_list; px; px = px->next) {
Thierry Fournierf61aa632016-02-19 20:56:00 +01001378 lua_pushstring(L, px->id);
1379 hlua_fcn_new_proxy(L, px);
1380 lua_settable(L, -3);
1381 }
1382
1383 /* push "proxies" in "core" */
1384 lua_settable(L, -3);
1385
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001386 /* Create proxies entry. */
1387 lua_pushstring(L, "frontends");
1388 lua_newtable(L);
1389
1390 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001391 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001392 if (!(px->cap & PR_CAP_FE))
1393 continue;
1394 lua_pushstring(L, px->id);
1395 hlua_fcn_new_proxy(L, px);
1396 lua_settable(L, -3);
1397 }
1398
1399 /* push "frontends" in "core" */
1400 lua_settable(L, -3);
1401
1402 /* Create proxies entry. */
1403 lua_pushstring(L, "backends");
1404 lua_newtable(L);
1405
1406 /* List all proxies. */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001407 for (px = proxies_list; px; px = px->next) {
Thierry FOURNIER9b82a582017-07-24 13:30:43 +02001408 if (!(px->cap & PR_CAP_BE))
1409 continue;
1410 lua_pushstring(L, px->id);
1411 hlua_fcn_new_proxy(L, px);
1412 lua_settable(L, -3);
1413 }
1414
1415 /* push "backend" in "core" */
1416 lua_settable(L, -3);
1417
Thierry Fournier3d4a6752016-02-19 20:53:30 +01001418 return 1;
1419}
1420
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001421/* This Lua function take a string, a list of separators.
1422 * It tokenize the input string using the list of separators
1423 * as separator.
1424 *
1425 * The functionreturns a tablle filled with tokens.
1426 */
1427int hlua_tokenize(lua_State *L)
1428{
1429 const char *str;
1430 const char *sep;
1431 int index;
1432 const char *token;
1433 const char *p;
1434 const char *c;
1435 int ignore_empty;
1436
1437 ignore_empty = 0;
1438
1439 str = luaL_checkstring(L, 1);
1440 sep = luaL_checkstring(L, 2);
1441 if (lua_gettop(L) == 3)
1442 ignore_empty = hlua_checkboolean(L, 3);
1443
1444 lua_newtable(L);
1445 index = 1;
1446 token = str;
1447 p = str;
1448 while(1) {
1449 for (c = sep; *c != '\0'; c++)
1450 if (*p == *c)
1451 break;
1452 if (*p == *c) {
1453 if ((!ignore_empty) || (p - token > 0)) {
1454 lua_pushlstring(L, token, p - token);
1455 lua_rawseti(L, -2, index);
1456 index++;
1457 }
1458 token = p + 1;
1459 }
1460 if (*p == '\0')
1461 break;
1462 p++;
1463 }
1464
1465 return 1;
1466}
1467
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001468int hlua_parse_addr(lua_State *L)
1469{
1470 struct hlua_addr *addr;
1471 const char *str = luaL_checkstring(L, 1);
1472 unsigned char mask;
1473
1474 addr = lua_newuserdata(L, sizeof(struct hlua_addr));
1475 if (!addr) {
1476 lua_pushnil(L);
1477 return 1;
1478 }
1479
1480 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
1481 addr->type = AF_INET;
1482 return 1;
1483 }
1484
1485 if (str62net(str, &addr->addr.v6.ip, &mask)) {
1486 len2mask6(mask, &addr->addr.v6.mask);
1487 addr->type = AF_INET6;
1488 return 1;
1489 }
1490
1491 lua_pop(L, 1);
1492 lua_pushnil(L);
1493 return 1;
1494}
1495
1496int hlua_match_addr(lua_State *L)
1497{
1498 struct hlua_addr *addr1;
1499 struct hlua_addr *addr2;
1500
1501 if (!lua_isuserdata(L, 1) ||
1502 !lua_isuserdata(L, 2)) {
1503 lua_pushboolean(L, 0);
1504 return 1;
1505 }
1506
1507 addr1 = lua_touserdata(L, 1);
1508 addr2 = lua_touserdata(L, 2);
1509
1510 if (addr1->type != addr2->type) {
1511 lua_pushboolean(L, 0);
1512 return 1;
1513 }
1514
1515 if (addr1->type == AF_INET) {
1516 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1517 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1518 lua_pushboolean(L, 1);
1519 return 1;
1520 }
1521 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001522 int i;
1523
1524 for (i = 0; i < 16; i += 4) {
1525 if ((*(uint32_t *)&addr1->addr.v6.ip.s6_addr[i] &
1526 *(uint32_t *)&addr2->addr.v6.mask.s6_addr[i]) !=
1527 (*(uint32_t *)&addr2->addr.v6.ip.s6_addr[i] &
1528 *(uint32_t *)&addr1->addr.v6.mask.s6_addr[i]))
1529 break;
1530 }
1531 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001532 lua_pushboolean(L, 1);
1533 return 1;
1534 }
1535 }
1536
1537 lua_pushboolean(L, 0);
1538 return 1;
1539}
1540
Thierry FOURNIER31904272017-10-25 12:59:51 +02001541static struct my_regex *hlua_check_regex(lua_State *L, int ud)
1542{
1543 return (hlua_checkudata(L, ud, class_regex_ref));
1544}
1545
1546static int hlua_regex_comp(struct lua_State *L)
1547{
1548 struct my_regex *regex;
1549 const char *str;
1550 int cs;
1551 char *err;
1552
1553 str = luaL_checkstring(L, 1);
1554 luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL);
1555 cs = lua_toboolean(L, 2);
1556
1557 regex = lua_newuserdata(L, sizeof(*regex));
1558
1559 err = NULL;
1560 if (!regex_comp(str, regex, cs, 1, &err)) {
1561 lua_pushboolean(L, 0); /* status error */
1562 lua_pushstring(L, err); /* Reason */
1563 free(err);
1564 return 2;
1565 }
1566
1567 lua_pushboolean(L, 1); /* Status ok */
1568
1569 /* Create object */
1570 lua_newtable(L);
1571 lua_pushvalue(L, -3); /* Get the userdata pointer. */
1572 lua_rawseti(L, -2, 0);
1573 lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref);
1574 lua_setmetatable(L, -2);
1575 return 2;
1576}
1577
1578static int hlua_regex_exec(struct lua_State *L)
1579{
1580 struct my_regex *regex;
1581 const char *str;
1582 size_t len;
Willy Tarreau83061a82018-07-13 11:56:34 +02001583 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001584
1585 regex = hlua_check_regex(L, 1);
1586 str = luaL_checklstring(L, 2, &len);
1587
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001588 /* Copy the string because regex_exec2 require a 'char *'
1589 * and not a 'const char *'.
1590 */
1591 tmp = get_trash_chunk();
1592 if (len >= tmp->size) {
1593 lua_pushboolean(L, 0);
1594 return 1;
1595 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001596 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001597
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001598 lua_pushboolean(L, regex_exec2(regex, tmp->area, len));
Thierry FOURNIER31904272017-10-25 12:59:51 +02001599
1600 return 1;
1601}
1602
1603static int hlua_regex_match(struct lua_State *L)
1604{
1605 struct my_regex *regex;
1606 const char *str;
1607 size_t len;
1608 regmatch_t pmatch[20];
1609 int ret;
1610 int i;
Willy Tarreau83061a82018-07-13 11:56:34 +02001611 struct buffer *tmp;
Thierry FOURNIER31904272017-10-25 12:59:51 +02001612
1613 regex = hlua_check_regex(L, 1);
1614 str = luaL_checklstring(L, 2, &len);
1615
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001616 /* Copy the string because regex_exec2 require a 'char *'
1617 * and not a 'const char *'.
1618 */
1619 tmp = get_trash_chunk();
1620 if (len >= tmp->size) {
1621 lua_pushboolean(L, 0);
1622 return 1;
1623 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001624 memcpy(tmp->area, str, len);
Thierry FOURNIER7c210e62017-10-27 14:13:51 +02001625
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001626 ret = regex_exec_match2(regex, tmp->area, len, 20, pmatch, 0);
Thierry FOURNIER31904272017-10-25 12:59:51 +02001627 lua_pushboolean(L, ret);
1628 lua_newtable(L);
1629 if (ret) {
1630 for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) {
1631 lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
1632 lua_rawseti(L, -2, i + 1);
1633 }
1634 }
1635 return 2;
1636}
1637
1638static int hlua_regex_free(struct lua_State *L)
1639{
1640 struct my_regex *regex;
1641
1642 regex = hlua_check_regex(L, 1);
1643 regex_free(regex);
1644 return 0;
1645}
1646
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001647int hlua_fcn_reg_core_fcn(lua_State *L)
1648{
Thierry Fournier1de16592016-01-27 09:49:07 +01001649 if (!hlua_concat_init(L))
1650 return 0;
1651
Thierry Fournier4f99b272016-02-22 08:40:02 +01001652 hlua_class_function(L, "now", hlua_now);
1653 hlua_class_function(L, "http_date", hlua_http_date);
1654 hlua_class_function(L, "imf_date", hlua_imf_date);
1655 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1656 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1657 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001658 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001659 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1660 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001661 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001662
Thierry FOURNIER31904272017-10-25 12:59:51 +02001663 /* Create regex object. */
1664 lua_newtable(L);
1665 hlua_class_function(L, "new", hlua_regex_comp);
1666
1667 lua_newtable(L); /* The metatable. */
1668 lua_pushstring(L, "__index");
1669 lua_newtable(L);
1670 hlua_class_function(L, "exec", hlua_regex_exec);
1671 hlua_class_function(L, "match", hlua_regex_match);
1672 lua_rawset(L, -3); /* -> META["__index"] = TABLE */
1673 hlua_class_function(L, "__gc", hlua_regex_free);
1674
1675 lua_pushvalue(L, -1); /* Duplicate the metatable reference. */
1676 class_regex_ref = hlua_register_metatable(L, CLASS_REGEX);
1677
1678 lua_setmetatable(L, -2);
1679 lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
1680
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001681 /* Create stktable object. */
1682 lua_newtable(L);
1683 lua_pushstring(L, "__index");
1684 lua_newtable(L);
1685 hlua_class_function(L, "info", hlua_stktable_info);
1686 hlua_class_function(L, "lookup", hlua_stktable_lookup);
1687 hlua_class_function(L, "dump", hlua_stktable_dump);
1688 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1689 class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
1690
Thierry Fournierff480422016-02-25 08:36:46 +01001691 /* Create listener object. */
1692 lua_newtable(L);
1693 lua_pushstring(L, "__index");
1694 lua_newtable(L);
1695 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1696 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1697 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1698
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001699 /* Create server object. */
1700 lua_newtable(L);
1701 lua_pushstring(L, "__index");
1702 lua_newtable(L);
1703 hlua_class_function(L, "is_draining", hlua_server_is_draining);
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001704 hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
1705 hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001706 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1707 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1708 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1709 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1710 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1711 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1712 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1713 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1714 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1715 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1716 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1717 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1718 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1719 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1720 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1721 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1722 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1723 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1724 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1725 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1726
Thierry Fournierf61aa632016-02-19 20:56:00 +01001727 /* Create proxy object. */
1728 lua_newtable(L);
1729 lua_pushstring(L, "__index");
1730 lua_newtable(L);
1731 hlua_class_function(L, "pause", hlua_proxy_pause);
1732 hlua_class_function(L, "resume", hlua_proxy_resume);
1733 hlua_class_function(L, "stop", hlua_proxy_stop);
1734 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1735 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1736 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1737 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1738 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1739 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1740
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001741 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001742}