Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 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 Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 13 | * 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 Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 21 | #include <common/time.h> |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 22 | #include <common/uri_auth.h> |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 23 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 24 | #include <types/cli.h> |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 25 | #include <types/hlua.h> |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 26 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 27 | #include <types/stats.h> |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 28 | |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 29 | #include <proto/h1.h> |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 30 | #include <proto/proxy.h> |
| 31 | #include <proto/server.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 32 | #include <proto/stats.h> |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 33 | #include <proto/stick_table.h> |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 34 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 35 | /* Contains the class reference of the concat object. */ |
| 36 | static int class_concat_ref; |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 37 | static int class_proxy_ref; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 38 | static int class_server_ref; |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 39 | static int class_listener_ref; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 40 | static int class_regex_ref; |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 41 | static int class_stktable_ref; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 42 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 43 | #define MAX_STK_FILTER_LEN 4 |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 44 | #define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS)) |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 45 | |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 46 | static THREAD_LOCAL struct field stats[STATS_LEN]; |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 47 | |
Thierry FOURNIER / OZON.IO | 7f3aa8b | 2016-11-24 20:37:38 +0100 | [diff] [blame] | 48 | int 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 Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 55 | /* Helper to push unsigned integers to Lua stack, respecting Lua limitations */ |
| 56 | static 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 */ |
| 70 | static 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 Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 87 | /* This function gets a struct field and convert it in Lua |
| 88 | * variable. The variable is pushed at the top of the stak. |
| 89 | */ |
| 90 | int 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 Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 164 | /* 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 | */ |
| 170 | const 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 Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 181 | /* 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 | */ |
| 189 | void hlua_class_const_int(lua_State *L, const char *name, int value) |
| 190 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 191 | lua_pushstring(L, name); |
| 192 | lua_pushinteger(L, value); |
| 193 | lua_rawset(L, -3); |
| 194 | } |
| 195 | void hlua_class_const_str(lua_State *L, const char *name, const char *value) |
| 196 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 197 | lua_pushstring(L, name); |
| 198 | lua_pushstring(L, value); |
| 199 | lua_rawset(L, -3); |
| 200 | } |
| 201 | void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L)) |
| 202 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 203 | 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. */ |
| 209 | int 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 Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 216 | /* 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 | */ |
| 221 | int 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 Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 251 | /* Return an object of the expected type, or throws an error. */ |
| 252 | void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 253 | { |
| 254 | void *p; |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 255 | int ret; |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 256 | |
| 257 | /* Check if the stack entry is an array. */ |
| 258 | if (!lua_istable(L, ud)) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 259 | 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 Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 268 | /* Check if the metadata have the expected type. */ |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 269 | ret = lua_rawequal(L, -1, -2); |
| 270 | lua_pop(L, 2); |
| 271 | if (!ret) |
| 272 | luaL_argerror(L, ud, NULL); |
| 273 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 274 | /* Push on the stack at the entry [0] of the table. */ |
| 275 | lua_rawgeti(L, ud, 0); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 276 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 277 | /* Check if this entry is userdata. */ |
| 278 | p = lua_touserdata(L, -1); |
| 279 | if (!p) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 280 | luaL_argerror(L, ud, NULL); |
| 281 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 282 | /* Remove the entry returned by lua_rawgeti(). */ |
| 283 | lua_pop(L, 1); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 284 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 285 | /* Return the associated struct. */ |
| 286 | return p; |
| 287 | } |
| 288 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 289 | /* This function return the current date at epoch format in milliseconds. */ |
| 290 | int 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 Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 302 | /* 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 | */ |
| 306 | static 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 Tarreau | abd9bb2 | 2017-07-19 19:08:48 +0200 | [diff] [blame] | 326 | time = my_timegm(&tm); |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 327 | if (time == -1) { |
| 328 | lua_pushnil(L); |
| 329 | return 1; |
| 330 | } |
| 331 | |
| 332 | lua_pushinteger(L, (int)time); |
| 333 | return 1; |
| 334 | } |
| 335 | static int hlua_http_date(lua_State *L) |
| 336 | { |
| 337 | return hlua_parse_date(L, parse_http_date); |
| 338 | } |
| 339 | static int hlua_imf_date(lua_State *L) |
| 340 | { |
| 341 | return hlua_parse_date(L, parse_imf_date); |
| 342 | } |
| 343 | static int hlua_rfc850_date(lua_State *L) |
| 344 | { |
| 345 | return hlua_parse_date(L, parse_rfc850_date); |
| 346 | } |
| 347 | static int hlua_asctime_date(lua_State *L) |
| 348 | { |
| 349 | return hlua_parse_date(L, parse_asctime_date); |
| 350 | } |
| 351 | |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 352 | static 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 Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 367 | static struct hlua_concat *hlua_check_concat(lua_State *L, int ud) |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 368 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 369 | return (hlua_checkudata(L, ud, class_concat_ref)); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static int hlua_concat_add(lua_State *L) |
| 373 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 374 | struct hlua_concat *b; |
| 375 | char *buffer; |
| 376 | char *new; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 377 | 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 Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 386 | /* 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 Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int hlua_concat_dump(lua_State *L) |
| 416 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 417 | struct hlua_concat *b; |
| 418 | char *buffer; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 419 | |
| 420 | /* First arg must be a concat object. */ |
| 421 | b = hlua_check_concat(L, 1); |
| 422 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 423 | /* Get the buffer. */ |
| 424 | lua_rawgeti(L, 1, 1); |
| 425 | buffer = lua_touserdata(L, -1); |
| 426 | lua_pop(L, 1); |
| 427 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 428 | /* Push the soncatenated strng in the stack. */ |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 429 | lua_pushlstring(L, buffer, b->len); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | int hlua_concat_new(lua_State *L) |
| 434 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 435 | struct hlua_concat *b; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 436 | |
| 437 | lua_newtable(L); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 438 | b = lua_newuserdata(L, sizeof(*b)); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 439 | b->size = HLUA_CONCAT_BLOCSZ; |
| 440 | b->len = 0; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 441 | lua_rawseti(L, -2, 0); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 442 | lua_newuserdata(L, HLUA_CONCAT_BLOCSZ); |
| 443 | lua_rawseti(L, -2, 1); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 444 | |
| 445 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref); |
| 446 | lua_setmetatable(L, -2); |
| 447 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | static 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 | |
| 458 | static 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 Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 484 | int 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 | |
| 497 | static 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 */ |
| 503 | int 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 */ |
| 565 | static 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 | */ |
| 605 | int 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 | |
| 646 | struct 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) */ |
| 654 | static 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>*/ |
| 676 | int 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 Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 832 | int 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 | |
| 845 | static struct listener *hlua_check_listener(lua_State *L, int ud) |
| 846 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 847 | return hlua_checkudata(L, ud, class_listener_ref); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | int 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 Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 857 | if (!li->bind_conf->frontend) { |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 858 | lua_pushnil(L); |
| 859 | return 1; |
| 860 | } |
| 861 | |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 862 | stats_fill_li_stats(li->bind_conf->frontend, li, ST_SHLGNDS, stats, STATS_LEN); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 863 | |
| 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 Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 874 | int hlua_fcn_new_server(lua_State *L, struct server *srv) |
| 875 | { |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 876 | char buffer[12]; |
| 877 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 878 | 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 Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 886 | |
| 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 Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | static struct server *hlua_check_server(lua_State *L, int ud) |
| 902 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 903 | return hlua_checkudata(L, ud, class_server_ref); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | int 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 | |
| 930 | int 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 Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 946 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 947 | luaL_addstring(&b, addr); |
| 948 | break; |
| 949 | case AF_INET6: |
| 950 | inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr, |
Nenad Merdanovic | a9f0404 | 2017-07-23 22:04:59 -0400 | [diff] [blame] | 951 | addr, INET6_ADDRSTRLEN); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 952 | luaL_addstring(&b, addr); |
| 953 | luaL_addstring(&b, ":"); |
Nenad Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 954 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 955 | 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 | |
| 969 | int 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 Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 978 | int 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 | |
| 997 | int 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 Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1006 | int 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 Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1015 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1016 | err = server_parse_weight_change_request(srv, weight); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1017 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1018 | if (!err) |
| 1019 | lua_pushnil(L); |
| 1020 | else |
| 1021 | hlua_pushstrippedstring(L, err); |
| 1022 | return 1; |
| 1023 | } |
| 1024 | |
| 1025 | int 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 | |
| 1034 | int 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 Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1043 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1044 | err = server_parse_addr_change_request(srv, addr, "Lua script"); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1045 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1046 | if (!err) |
| 1047 | lua_pushnil(L); |
| 1048 | else |
| 1049 | hlua_pushstrippedstring(L, err); |
| 1050 | return 1; |
| 1051 | } |
| 1052 | |
| 1053 | int hlua_server_shut_sess(lua_State *L) |
| 1054 | { |
| 1055 | struct server *srv; |
| 1056 | |
| 1057 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1058 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1059 | srv_shutdown_streams(srv, SF_ERR_KILLED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1060 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | int hlua_server_set_drain(lua_State *L) |
| 1065 | { |
| 1066 | struct server *srv; |
| 1067 | |
| 1068 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1069 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1070 | srv_adm_set_drain(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1071 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | int hlua_server_set_maint(lua_State *L) |
| 1076 | { |
| 1077 | struct server *srv; |
| 1078 | |
| 1079 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1080 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1081 | srv_adm_set_maint(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1082 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | int hlua_server_set_ready(lua_State *L) |
| 1087 | { |
| 1088 | struct server *srv; |
| 1089 | |
| 1090 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1091 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1092 | srv_adm_set_ready(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1093 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | int hlua_server_check_enable(lua_State *L) |
| 1098 | { |
| 1099 | struct server *sv; |
| 1100 | |
| 1101 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1102 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1103 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 1104 | sv->check.state |= CHK_ST_ENABLED; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1105 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1106 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | int hlua_server_check_disable(lua_State *L) |
| 1111 | { |
| 1112 | struct server *sv; |
| 1113 | |
| 1114 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1115 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1116 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 1117 | sv->check.state &= ~CHK_ST_ENABLED; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1118 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1119 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | int hlua_server_check_force_up(lua_State *L) |
| 1124 | { |
| 1125 | struct server *sv; |
| 1126 | |
| 1127 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1128 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1129 | if (!(sv->track)) { |
| 1130 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1131 | srv_set_running(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1132 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1133 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | int hlua_server_check_force_nolb(lua_State *L) |
| 1138 | { |
| 1139 | struct server *sv; |
| 1140 | |
| 1141 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1142 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1143 | if (!(sv->track)) { |
| 1144 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1145 | srv_set_stopping(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1146 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1147 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | int hlua_server_check_force_down(lua_State *L) |
| 1152 | { |
| 1153 | struct server *sv; |
| 1154 | |
| 1155 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1156 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1157 | if (!(sv->track)) { |
| 1158 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1159 | srv_set_stopped(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1160 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1161 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | int hlua_server_agent_enable(lua_State *L) |
| 1166 | { |
| 1167 | struct server *sv; |
| 1168 | |
| 1169 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1170 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1171 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 1172 | sv->agent.state |= CHK_ST_ENABLED; |
| 1173 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1174 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | int hlua_server_agent_disable(lua_State *L) |
| 1179 | { |
| 1180 | struct server *sv; |
| 1181 | |
| 1182 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1183 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1184 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 1185 | sv->agent.state &= ~CHK_ST_ENABLED; |
| 1186 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1187 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | int hlua_server_agent_force_up(lua_State *L) |
| 1192 | { |
| 1193 | struct server *sv; |
| 1194 | |
| 1195 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1196 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1197 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 1198 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1199 | srv_set_running(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1200 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1201 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | int hlua_server_agent_force_down(lua_State *L) |
| 1206 | { |
| 1207 | struct server *sv; |
| 1208 | |
| 1209 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1210 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1211 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 1212 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1213 | srv_set_stopped(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1214 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1215 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1216 | return 0; |
| 1217 | } |
| 1218 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1219 | int hlua_fcn_new_proxy(lua_State *L, struct proxy *px) |
| 1220 | { |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1221 | struct server *srv; |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1222 | struct listener *lst; |
| 1223 | int lid; |
Willy Tarreau | 29d6980 | 2018-05-06 14:50:09 +0200 | [diff] [blame] | 1224 | char buffer[17]; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1225 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1226 | 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 Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1234 | |
Thierry FOURNIER | f2bbe38 | 2017-07-24 13:59:22 +0200 | [diff] [blame] | 1235 | /* Add proxy name. */ |
| 1236 | lua_pushstring(L, "name"); |
| 1237 | lua_pushstring(L, px->id); |
| 1238 | lua_settable(L, -3); |
| 1239 | |
Baptiste Assmann | 46c7255 | 2017-10-26 21:51:58 +0200 | [diff] [blame] | 1240 | /* 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 Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1246 | /* 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 Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1256 | /* 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 Tarreau | 29d6980 | 2018-05-06 14:50:09 +0200 | [diff] [blame] | 1264 | snprintf(buffer, sizeof(buffer), "sock-%d", lid); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1265 | 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 Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1273 | 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 Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1279 | return 1; |
| 1280 | } |
| 1281 | |
| 1282 | static struct proxy *hlua_check_proxy(lua_State *L, int ud) |
| 1283 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1284 | return hlua_checkudata(L, ud, class_proxy_ref); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | int 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 | |
| 1296 | int 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 | |
| 1305 | int 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 | |
| 1314 | int 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 | |
| 1325 | int 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 | |
| 1344 | int 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 | |
| 1355 | int 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 Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1364 | int hlua_fcn_post_init(lua_State *L) |
| 1365 | { |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1366 | 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 Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1377 | for (px = proxies_list; px; px = px->next) { |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1378 | 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 FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1386 | /* Create proxies entry. */ |
| 1387 | lua_pushstring(L, "frontends"); |
| 1388 | lua_newtable(L); |
| 1389 | |
| 1390 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1391 | for (px = proxies_list; px; px = px->next) { |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1392 | 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 Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1407 | for (px = proxies_list; px; px = px->next) { |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1408 | 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 Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1418 | return 1; |
| 1419 | } |
| 1420 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1421 | /* 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 | */ |
| 1427 | int 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.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1468 | int 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 | |
| 1496 | int 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 FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 1522 | 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.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1532 | lua_pushboolean(L, 1); |
| 1533 | return 1; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | lua_pushboolean(L, 0); |
| 1538 | return 1; |
| 1539 | } |
| 1540 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1541 | static struct my_regex *hlua_check_regex(lua_State *L, int ud) |
| 1542 | { |
| 1543 | return (hlua_checkudata(L, ud, class_regex_ref)); |
| 1544 | } |
| 1545 | |
| 1546 | static 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 | |
| 1578 | static int hlua_regex_exec(struct lua_State *L) |
| 1579 | { |
| 1580 | struct my_regex *regex; |
| 1581 | const char *str; |
| 1582 | size_t len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1583 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1584 | |
| 1585 | regex = hlua_check_regex(L, 1); |
| 1586 | str = luaL_checklstring(L, 2, &len); |
| 1587 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1588 | /* 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1596 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1597 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1598 | lua_pushboolean(L, regex_exec2(regex, tmp->area, len)); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1599 | |
| 1600 | return 1; |
| 1601 | } |
| 1602 | |
| 1603 | static 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 Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1611 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1612 | |
| 1613 | regex = hlua_check_regex(L, 1); |
| 1614 | str = luaL_checklstring(L, 2, &len); |
| 1615 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1616 | /* 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1624 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1625 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1626 | ret = regex_exec_match2(regex, tmp->area, len, 20, pmatch, 0); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1627 | 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 | |
| 1638 | static 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 Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1647 | int hlua_fcn_reg_core_fcn(lua_State *L) |
| 1648 | { |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1649 | if (!hlua_concat_init(L)) |
| 1650 | return 0; |
| 1651 | |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1652 | 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 Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 1658 | hlua_class_function(L, "get_info", hlua_get_info); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1659 | hlua_class_function(L, "parse_addr", hlua_parse_addr); |
| 1660 | hlua_class_function(L, "match_addr", hlua_match_addr); |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1661 | hlua_class_function(L, "tokenize", hlua_tokenize); |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1662 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1663 | /* 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 Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1681 | /* 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 Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1691 | /* 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 Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1699 | /* 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 Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1704 | hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn); |
| 1705 | hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1706 | 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 Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1727 | /* 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 Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 1741 | return 5; |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1742 | } |