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 | */ |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 20 | #include <lauxlib.h> |
| 21 | #include <lua.h> |
| 22 | #include <lualib.h> |
| 23 | |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 24 | #include <haproxy/cli-t.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 25 | #include <haproxy/errors.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 26 | #include <haproxy/hlua-t.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 27 | #include <haproxy/http.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 28 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 29 | #include <haproxy/pattern-t.h> |
| 30 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 31 | #include <haproxy/regex.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 32 | #include <haproxy/server.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 33 | #include <haproxy/stats.h> |
Willy Tarreau | 872f2ea | 2020-06-04 18:46:44 +0200 | [diff] [blame] | 34 | #include <haproxy/stick_table.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 35 | #include <haproxy/time.h> |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 36 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 37 | /* Contains the class reference of the concat object. */ |
| 38 | static int class_concat_ref; |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 39 | static int class_proxy_ref; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 40 | static int class_server_ref; |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 41 | static int class_listener_ref; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 42 | static int class_regex_ref; |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 43 | static int class_stktable_ref; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 44 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 45 | #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] | 46 | |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 47 | static THREAD_LOCAL struct field stats[STATS_LEN]; |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 48 | |
Thierry FOURNIER / OZON.IO | 7f3aa8b | 2016-11-24 20:37:38 +0100 | [diff] [blame] | 49 | int hlua_checkboolean(lua_State *L, int index) |
| 50 | { |
| 51 | if (!lua_isboolean(L, index)) |
| 52 | luaL_argerror(L, index, "boolean expected"); |
| 53 | return lua_toboolean(L, index); |
| 54 | } |
| 55 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 56 | /* Helper to push unsigned integers to Lua stack, respecting Lua limitations */ |
| 57 | static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val) |
| 58 | { |
| 59 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 60 | lua_pushinteger(L, val); |
| 61 | #else |
| 62 | if (val > INT_MAX) |
| 63 | lua_pushnumber(L, (lua_Number)val); |
| 64 | else |
| 65 | lua_pushinteger(L, (int)val); |
| 66 | #endif |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | /* Helper to push unsigned long long to Lua stack, respecting Lua limitations */ |
| 71 | static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) { |
| 72 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 73 | /* 64 bits case, U64 is supported until LLONG_MAX */ |
| 74 | if (val > LLONG_MAX) |
| 75 | lua_pushnumber(L, (lua_Number)val); |
| 76 | else |
| 77 | lua_pushinteger(L, val); |
| 78 | #else |
| 79 | /* 32 bits case, U64 is supported until INT_MAX */ |
| 80 | if (val > INT_MAX) |
| 81 | lua_pushnumber(L, (lua_Number)val); |
| 82 | else |
| 83 | lua_pushinteger(L, (int)val); |
| 84 | #endif |
| 85 | return 1; |
| 86 | } |
| 87 | |
Joseph Herlant | b3d92e3 | 2018-11-15 09:35:04 -0800 | [diff] [blame] | 88 | /* This function gets a struct field and converts it in Lua |
| 89 | * variable. The variable is pushed at the top of the stack. |
Thierry Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 90 | */ |
| 91 | int hlua_fcn_pushfield(lua_State *L, struct field *field) |
| 92 | { |
| 93 | /* The lua_Integer is always signed. Its length depends on |
Joseph Herlant | b3d92e3 | 2018-11-15 09:35:04 -0800 | [diff] [blame] | 94 | * compilation options, so the following code is conditioned |
Thierry Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 95 | * by some macros. Windows maros are not supported. |
| 96 | * If the number cannot be represented as integer, we try to |
| 97 | * convert to float. |
| 98 | */ |
| 99 | switch (field_format(field, 0)) { |
| 100 | |
| 101 | case FF_EMPTY: |
| 102 | lua_pushnil(L); |
| 103 | return 1; |
| 104 | |
| 105 | case FF_S32: |
| 106 | /* S32 is always supported. */ |
| 107 | lua_pushinteger(L, field->u.s32); |
| 108 | return 1; |
| 109 | |
| 110 | case FF_U32: |
| 111 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 112 | /* 64 bits case, U32 is always supported */ |
| 113 | lua_pushinteger(L, field->u.u32); |
| 114 | #else |
| 115 | /* 32 bits case, U32 is supported until INT_MAX. */ |
| 116 | if (field->u.u32 > INT_MAX) |
| 117 | lua_pushnumber(L, (lua_Number)field->u.u32); |
| 118 | else |
| 119 | lua_pushinteger(L, field->u.u32); |
| 120 | #endif |
| 121 | return 1; |
| 122 | |
| 123 | case FF_S64: |
| 124 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 125 | /* 64 bits case, S64 is always supported */ |
| 126 | lua_pushinteger(L, field->u.s64); |
| 127 | #else |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 128 | /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */ |
Thierry Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 129 | if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX) |
| 130 | lua_pushnumber(L, (lua_Number)field->u.s64); |
| 131 | else |
| 132 | lua_pushinteger(L, (int)field->u.s64); |
| 133 | #endif |
| 134 | return 1; |
| 135 | |
| 136 | case FF_U64: |
| 137 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 138 | /* 64 bits case, U64 is supported until LLONG_MAX */ |
| 139 | if (field->u.u64 > LLONG_MAX) |
| 140 | lua_pushnumber(L, (lua_Number)field->u.u64); |
| 141 | else |
| 142 | lua_pushinteger(L, field->u.u64); |
| 143 | #else |
| 144 | /* 64 bits case, U64 is supported until INT_MAX */ |
| 145 | if (field->u.u64 > INT_MAX) |
| 146 | lua_pushnumber(L, (lua_Number)field->u.u64); |
| 147 | else |
| 148 | lua_pushinteger(L, (int)field->u.u64); |
| 149 | #endif |
| 150 | return 1; |
| 151 | |
| 152 | case FF_STR: |
| 153 | lua_pushstring(L, field->u.str); |
| 154 | return 1; |
| 155 | |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | /* Default case, never reached. */ |
| 161 | lua_pushnil(L); |
| 162 | return 1; |
| 163 | } |
| 164 | |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 165 | /* Some string are started or terminated by blank chars, |
| 166 | * this function removes the spaces, tabs, \r and |
| 167 | * \n at the begin and at the end of the string "str", and |
| 168 | * push the result in the lua stack. |
| 169 | * Returns a pointer to the Lua internal copy of the string. |
| 170 | */ |
| 171 | const char *hlua_pushstrippedstring(lua_State *L, const char *str) |
| 172 | { |
| 173 | const char *p; |
| 174 | const char *e; |
| 175 | |
| 176 | for (p = str; HTTP_IS_LWS(*p); p++); |
| 177 | for (e = p + strlen(p) - 1; e > p && HTTP_IS_LWS(*e); e--); |
| 178 | |
| 179 | return lua_pushlstring(L, p, e - p); |
| 180 | } |
| 181 | |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 182 | /* The three following functions are useful for adding entries |
| 183 | * in a table. These functions takes a string and respectively an |
| 184 | * integer, a string or a function and add it to the table in the |
| 185 | * top of the stack. |
| 186 | * |
| 187 | * These functions throws an error if no more stack size is |
| 188 | * available. |
| 189 | */ |
| 190 | void hlua_class_const_int(lua_State *L, const char *name, int value) |
| 191 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 192 | lua_pushstring(L, name); |
| 193 | lua_pushinteger(L, value); |
| 194 | lua_rawset(L, -3); |
| 195 | } |
| 196 | void hlua_class_const_str(lua_State *L, const char *name, const char *value) |
| 197 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 198 | lua_pushstring(L, name); |
| 199 | lua_pushstring(L, value); |
| 200 | lua_rawset(L, -3); |
| 201 | } |
| 202 | void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L)) |
| 203 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 204 | lua_pushstring(L, name); |
| 205 | lua_pushcclosure(L, function, 0); |
| 206 | lua_rawset(L, -3); |
| 207 | } |
| 208 | |
Joseph Herlant | b3d92e3 | 2018-11-15 09:35:04 -0800 | [diff] [blame] | 209 | /* This function returns a string containing the HAProxy object name. */ |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 210 | int hlua_dump_object(struct lua_State *L) |
| 211 | { |
| 212 | const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1)); |
| 213 | lua_pushfstring(L, "HAProxy class %s", name); |
| 214 | return 1; |
| 215 | } |
| 216 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 217 | /* This function register a table as metatable and. It names |
| 218 | * the metatable, and returns the associated reference. |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 219 | * The original table is popped from the top of the stack. |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 220 | * "name" is the referenced class name. |
| 221 | */ |
| 222 | int hlua_register_metatable(struct lua_State *L, char *name) |
| 223 | { |
| 224 | /* Check the type of the top element. it must be |
| 225 | * a table. |
| 226 | */ |
| 227 | if (lua_type(L, -1) != LUA_TTABLE) |
| 228 | luaL_error(L, "hlua_register_metatable() requires a type Table " |
| 229 | "in the top of the stack"); |
| 230 | |
| 231 | /* Add the __tostring function which identify the |
| 232 | * created object. |
| 233 | */ |
| 234 | lua_pushstring(L, "__tostring"); |
| 235 | lua_pushstring(L, name); |
| 236 | lua_pushcclosure(L, hlua_dump_object, 1); |
| 237 | lua_rawset(L, -3); |
| 238 | |
| 239 | /* Register a named entry for the table. The table |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 240 | * reference is copied first because the function |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 241 | * lua_setfield() pop the entry. |
| 242 | */ |
| 243 | lua_pushvalue(L, -1); |
| 244 | lua_setfield(L, LUA_REGISTRYINDEX, name); |
| 245 | |
| 246 | /* Creates the reference of the object. The |
| 247 | * function luaL_ref pop the top of the stack. |
| 248 | */ |
| 249 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 250 | } |
| 251 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 252 | /* Return an object of the expected type, or throws an error. */ |
| 253 | void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 254 | { |
| 255 | void *p; |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 256 | int ret; |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 257 | |
| 258 | /* Check if the stack entry is an array. */ |
| 259 | if (!lua_istable(L, ud)) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 260 | luaL_argerror(L, ud, NULL); |
| 261 | |
| 262 | /* pop the metatable of the referencecd object. */ |
| 263 | if (!lua_getmetatable(L, ud)) |
| 264 | luaL_argerror(L, ud, NULL); |
| 265 | |
| 266 | /* pop the expected metatable. */ |
| 267 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 268 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 269 | /* Check if the metadata have the expected type. */ |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 270 | ret = lua_rawequal(L, -1, -2); |
| 271 | lua_pop(L, 2); |
| 272 | if (!ret) |
| 273 | luaL_argerror(L, ud, NULL); |
| 274 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 275 | /* Push on the stack at the entry [0] of the table. */ |
| 276 | lua_rawgeti(L, ud, 0); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 277 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 278 | /* Check if this entry is userdata. */ |
| 279 | p = lua_touserdata(L, -1); |
| 280 | if (!p) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 281 | luaL_argerror(L, ud, NULL); |
| 282 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 283 | /* Remove the entry returned by lua_rawgeti(). */ |
| 284 | lua_pop(L, 1); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 285 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 286 | /* Return the associated struct. */ |
| 287 | return p; |
| 288 | } |
| 289 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 290 | /* This function return the current date at epoch format in milliseconds. */ |
| 291 | int hlua_now(lua_State *L) |
| 292 | { |
| 293 | lua_newtable(L); |
| 294 | lua_pushstring(L, "sec"); |
| 295 | lua_pushinteger(L, now.tv_sec); |
| 296 | lua_rawset(L, -3); |
| 297 | lua_pushstring(L, "usec"); |
| 298 | lua_pushinteger(L, now.tv_usec); |
| 299 | lua_rawset(L, -3); |
| 300 | return 1; |
| 301 | } |
| 302 | |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 303 | /* This functions expects a Lua string as HTTP date, parse it and |
| 304 | * returns an integer containing the epoch format of the date, or |
| 305 | * nil if the parsing fails. |
| 306 | */ |
| 307 | static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*)) |
| 308 | { |
| 309 | const char *str; |
| 310 | size_t len; |
| 311 | struct tm tm; |
| 312 | time_t time; |
| 313 | |
| 314 | str = luaL_checklstring(L, 1, &len); |
| 315 | |
| 316 | if (!fcn(str, len, &tm)) { |
| 317 | lua_pushnil(L); |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | /* This function considers the content of the broken-down time |
| 322 | * is exprimed in the UTC timezone. timegm don't care about |
| 323 | * the gnu variable tm_gmtoff. If gmtoff is set, or if you know |
| 324 | * the timezone from the broken-down time, it must be fixed |
| 325 | * after the conversion. |
| 326 | */ |
Willy Tarreau | abd9bb2 | 2017-07-19 19:08:48 +0200 | [diff] [blame] | 327 | time = my_timegm(&tm); |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 328 | if (time == -1) { |
| 329 | lua_pushnil(L); |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | lua_pushinteger(L, (int)time); |
| 334 | return 1; |
| 335 | } |
| 336 | static int hlua_http_date(lua_State *L) |
| 337 | { |
| 338 | return hlua_parse_date(L, parse_http_date); |
| 339 | } |
| 340 | static int hlua_imf_date(lua_State *L) |
| 341 | { |
| 342 | return hlua_parse_date(L, parse_imf_date); |
| 343 | } |
| 344 | static int hlua_rfc850_date(lua_State *L) |
| 345 | { |
| 346 | return hlua_parse_date(L, parse_rfc850_date); |
| 347 | } |
| 348 | static int hlua_asctime_date(lua_State *L) |
| 349 | { |
| 350 | return hlua_parse_date(L, parse_asctime_date); |
| 351 | } |
| 352 | |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 353 | static int hlua_get_info(lua_State *L) |
| 354 | { |
| 355 | int i; |
| 356 | |
| 357 | stats_fill_info(stats, STATS_LEN); |
| 358 | |
| 359 | lua_newtable(L); |
| 360 | for (i=0; i<INF_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 361 | lua_pushstring(L, info_fields[i].name); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 362 | hlua_fcn_pushfield(L, &stats[i]); |
| 363 | lua_settable(L, -3); |
| 364 | } |
| 365 | return 1; |
| 366 | } |
| 367 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 368 | static struct hlua_concat *hlua_check_concat(lua_State *L, int ud) |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 369 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 370 | return (hlua_checkudata(L, ud, class_concat_ref)); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static int hlua_concat_add(lua_State *L) |
| 374 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 375 | struct hlua_concat *b; |
| 376 | char *buffer; |
| 377 | char *new; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 378 | const char *str; |
| 379 | size_t l; |
| 380 | |
| 381 | /* First arg must be a concat object. */ |
| 382 | b = hlua_check_concat(L, 1); |
| 383 | |
| 384 | /* Second arg must be a string. */ |
| 385 | str = luaL_checklstring(L, 2, &l); |
| 386 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 387 | /* Get the buffer. */ |
| 388 | lua_rawgeti(L, 1, 1); |
| 389 | buffer = lua_touserdata(L, -1); |
| 390 | lua_pop(L, 1); |
| 391 | |
| 392 | /* Update the buffer size if it s required. The old buffer |
| 393 | * is crushed by the new in the object array, so it will |
| 394 | * be deleted by the GC. |
| 395 | * Note that in the first loop, the "new" variable is only |
| 396 | * used as a flag. |
| 397 | */ |
| 398 | new = NULL; |
| 399 | while (b->size - b->len < l) { |
| 400 | b->size += HLUA_CONCAT_BLOCSZ; |
| 401 | new = buffer; |
| 402 | } |
| 403 | if (new) { |
| 404 | new = lua_newuserdata(L, b->size); |
| 405 | memcpy(new, buffer, b->len); |
| 406 | lua_rawseti(L, 1, 1); |
| 407 | buffer = new; |
| 408 | } |
| 409 | |
| 410 | /* Copy string, and update metadata. */ |
| 411 | memcpy(buffer + b->len, str, l); |
| 412 | b->len += l; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int hlua_concat_dump(lua_State *L) |
| 417 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 418 | struct hlua_concat *b; |
| 419 | char *buffer; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 420 | |
| 421 | /* First arg must be a concat object. */ |
| 422 | b = hlua_check_concat(L, 1); |
| 423 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 424 | /* Get the buffer. */ |
| 425 | lua_rawgeti(L, 1, 1); |
| 426 | buffer = lua_touserdata(L, -1); |
| 427 | lua_pop(L, 1); |
| 428 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 429 | /* Push the soncatenated string in the stack. */ |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 430 | lua_pushlstring(L, buffer, b->len); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | int hlua_concat_new(lua_State *L) |
| 435 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 436 | struct hlua_concat *b; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 437 | |
| 438 | lua_newtable(L); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 439 | b = lua_newuserdata(L, sizeof(*b)); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 440 | b->size = HLUA_CONCAT_BLOCSZ; |
| 441 | b->len = 0; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 442 | lua_rawseti(L, -2, 0); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 443 | lua_newuserdata(L, HLUA_CONCAT_BLOCSZ); |
| 444 | lua_rawseti(L, -2, 1); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 445 | |
| 446 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref); |
| 447 | lua_setmetatable(L, -2); |
| 448 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 449 | return 1; |
| 450 | } |
| 451 | |
| 452 | static int concat_tostring(lua_State *L) |
| 453 | { |
| 454 | const void *ptr = lua_topointer(L, 1); |
| 455 | lua_pushfstring(L, "Concat object: %p", ptr); |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | static int hlua_concat_init(lua_State *L) |
| 460 | { |
| 461 | /* Creates the buffered concat object. */ |
| 462 | lua_newtable(L); |
| 463 | |
| 464 | lua_pushstring(L, "__tostring"); |
| 465 | lua_pushcclosure(L, concat_tostring, 0); |
| 466 | lua_settable(L, -3); |
| 467 | |
| 468 | lua_pushstring(L, "__index"); /* Creates the index entry. */ |
| 469 | lua_newtable(L); /* The "__index" content. */ |
| 470 | |
| 471 | lua_pushstring(L, "add"); |
| 472 | lua_pushcclosure(L, hlua_concat_add, 0); |
| 473 | lua_settable(L, -3); |
| 474 | |
| 475 | lua_pushstring(L, "dump"); |
| 476 | lua_pushcclosure(L, hlua_concat_dump, 0); |
| 477 | lua_settable(L, -3); |
| 478 | |
| 479 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 480 | class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 481 | |
| 482 | return 1; |
| 483 | } |
| 484 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 485 | int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl) |
| 486 | { |
| 487 | lua_newtable(L); |
| 488 | |
| 489 | /* Pop a class stktbl metatable and affect it to the userdata. */ |
| 490 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref); |
| 491 | lua_setmetatable(L, -2); |
| 492 | |
| 493 | lua_pushlightuserdata(L, tbl); |
| 494 | lua_rawseti(L, -2, 0); |
| 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | static struct stktable *hlua_check_stktable(lua_State *L, int ud) |
| 499 | { |
| 500 | return hlua_checkudata(L, ud, class_stktable_ref); |
| 501 | } |
| 502 | |
| 503 | /* Extract stick table attributes into Lua table */ |
| 504 | int hlua_stktable_info(lua_State *L) |
| 505 | { |
| 506 | struct stktable *tbl; |
| 507 | int dt; |
| 508 | |
| 509 | tbl = hlua_check_stktable(L, 1); |
| 510 | |
| 511 | if (!tbl->id) { |
| 512 | lua_pushnil(L); |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | lua_newtable(L); |
| 517 | |
| 518 | lua_pushstring(L, "type"); |
| 519 | lua_pushstring(L, stktable_types[tbl->type].kw); |
| 520 | lua_settable(L, -3); |
| 521 | |
| 522 | lua_pushstring(L, "length"); |
| 523 | lua_pushinteger(L, tbl->key_size); |
| 524 | lua_settable(L, -3); |
| 525 | |
| 526 | lua_pushstring(L, "size"); |
| 527 | hlua_fcn_pushunsigned(L, tbl->size); |
| 528 | lua_settable(L, -3); |
| 529 | |
| 530 | lua_pushstring(L, "used"); |
| 531 | hlua_fcn_pushunsigned(L, tbl->current); |
| 532 | lua_settable(L, -3); |
| 533 | |
| 534 | lua_pushstring(L, "nopurge"); |
| 535 | lua_pushboolean(L, tbl->nopurge > 0); |
| 536 | lua_settable(L, -3); |
| 537 | |
| 538 | lua_pushstring(L, "expire"); |
| 539 | lua_pushinteger(L, tbl->expire); |
| 540 | lua_settable(L, -3); |
| 541 | |
| 542 | /* Save data types periods (if applicable) in 'data' table */ |
| 543 | lua_pushstring(L, "data"); |
| 544 | lua_newtable(L); |
| 545 | |
| 546 | for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) { |
| 547 | if (tbl->data_ofs[dt] == 0) |
| 548 | continue; |
| 549 | |
| 550 | lua_pushstring(L, stktable_data_types[dt].name); |
| 551 | |
| 552 | if (stktable_data_types[dt].arg_type == ARG_T_DELAY) |
| 553 | lua_pushinteger(L, tbl->data_arg[dt].u); |
| 554 | else |
| 555 | lua_pushinteger(L, -1); |
| 556 | |
| 557 | lua_settable(L, -3); |
| 558 | } |
| 559 | |
| 560 | lua_settable(L, -3); |
| 561 | |
| 562 | return 1; |
| 563 | } |
| 564 | |
| 565 | /* Helper to get extract stick table entry into Lua table */ |
| 566 | static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts) |
| 567 | { |
| 568 | int dt; |
| 569 | void *ptr; |
| 570 | |
| 571 | for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) { |
| 572 | |
| 573 | if (t->data_ofs[dt] == 0) |
| 574 | continue; |
| 575 | |
| 576 | lua_pushstring(L, stktable_data_types[dt].name); |
| 577 | |
| 578 | ptr = stktable_data_ptr(t, ts, dt); |
| 579 | switch (stktable_data_types[dt].std_type) { |
| 580 | case STD_T_SINT: |
| 581 | lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint)); |
| 582 | break; |
| 583 | case STD_T_UINT: |
| 584 | hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint)); |
| 585 | break; |
| 586 | case STD_T_ULL: |
| 587 | hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull)); |
| 588 | break; |
| 589 | case STD_T_FRQP: |
| 590 | lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 591 | t->data_arg[dt].u)); |
| 592 | break; |
Adis Nezirovic | ad9f9ed | 2020-05-05 13:57:28 +0200 | [diff] [blame] | 593 | case STD_T_DICT: { |
| 594 | struct dict_entry *de; |
| 595 | de = stktable_data_cast(ptr, std_t_dict); |
| 596 | lua_pushstring(L, de ? (char *)de->value.key : "-"); |
| 597 | break; |
| 598 | } |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | lua_settable(L, -3); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /* Looks in table <t> for a sticky session matching key <key> |
| 606 | * Returns table with session data or nil |
| 607 | * |
| 608 | * The returned table always contains 'use' and 'expire' (integer) fields. |
| 609 | * For frequency/rate counters, each data entry is returned as table with |
| 610 | * 'value' and 'period' fields. |
| 611 | */ |
| 612 | int hlua_stktable_lookup(lua_State *L) |
| 613 | { |
| 614 | struct stktable *t; |
| 615 | struct sample smp; |
| 616 | struct stktable_key *skey; |
| 617 | struct stksess *ts; |
| 618 | |
| 619 | t = hlua_check_stktable(L, 1); |
| 620 | smp.data.type = SMP_T_STR; |
| 621 | smp.flags = SMP_F_CONST; |
Nathan Neulinger | 31a841c | 2020-03-03 20:32:47 -0600 | [diff] [blame] | 622 | smp.data.u.str.area = (char *)lua_tolstring(L, 2, &smp.data.u.str.data); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 623 | |
| 624 | skey = smp_to_stkey(&smp, t); |
| 625 | if (!skey) { |
| 626 | lua_pushnil(L); |
| 627 | return 1; |
| 628 | } |
| 629 | |
| 630 | ts = stktable_lookup_key(t, skey); |
| 631 | if (!ts) { |
| 632 | lua_pushnil(L); |
| 633 | return 1; |
| 634 | } |
| 635 | |
| 636 | lua_newtable(L); |
| 637 | lua_pushstring(L, "use"); |
| 638 | lua_pushinteger(L, ts->ref_cnt - 1); |
| 639 | lua_settable(L, -3); |
| 640 | |
| 641 | lua_pushstring(L, "expire"); |
| 642 | lua_pushinteger(L, tick_remain(now_ms, ts->expire)); |
| 643 | lua_settable(L, -3); |
| 644 | |
| 645 | hlua_stktable_entry(L, t, ts); |
| 646 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 647 | ts->ref_cnt--; |
| 648 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 649 | |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | struct stk_filter { |
| 654 | long long val; |
| 655 | int type; |
| 656 | int op; |
| 657 | }; |
| 658 | |
| 659 | |
| 660 | /* Helper for returning errors to callers using Lua convention (nil, err) */ |
| 661 | static int hlua_error(lua_State *L, const char *fmt, ...) { |
| 662 | char buf[256]; |
| 663 | int len; |
| 664 | va_list args; |
| 665 | va_start(args, fmt); |
| 666 | len = vsnprintf(buf, sizeof(buf), fmt, args); |
| 667 | va_end(args); |
| 668 | |
| 669 | if (len < 0) { |
| 670 | ha_alert("hlua_error(): Could not write error message.\n"); |
| 671 | lua_pushnil(L); |
| 672 | return 1; |
| 673 | } else if (len >= sizeof(buf)) |
| 674 | ha_alert("hlua_error(): Error message was truncated.\n"); |
| 675 | |
| 676 | lua_pushnil(L); |
| 677 | lua_pushstring(L, buf); |
| 678 | |
| 679 | return 2; |
| 680 | } |
| 681 | |
| 682 | /* Dump the contents of stick table <t>*/ |
| 683 | int hlua_stktable_dump(lua_State *L) |
| 684 | { |
| 685 | struct stktable *t; |
| 686 | struct ebmb_node *eb; |
| 687 | struct ebmb_node *n; |
| 688 | struct stksess *ts; |
| 689 | int type; |
| 690 | int op; |
| 691 | int dt; |
| 692 | long long val; |
Adis Nezirovic | 1a693fc | 2020-01-16 15:19:29 +0100 | [diff] [blame] | 693 | struct stk_filter filter[STKTABLE_FILTER_LEN]; |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 694 | int filter_count = 0; |
| 695 | int i; |
| 696 | int skip_entry; |
| 697 | void *ptr; |
| 698 | |
| 699 | t = hlua_check_stktable(L, 1); |
| 700 | type = lua_type(L, 2); |
| 701 | |
| 702 | switch (type) { |
| 703 | case LUA_TNONE: |
| 704 | case LUA_TNIL: |
| 705 | break; |
| 706 | case LUA_TTABLE: |
| 707 | lua_pushnil(L); |
| 708 | while (lua_next(L, 2) != 0) { |
| 709 | int entry_idx = 0; |
| 710 | |
Adis Nezirovic | 1a693fc | 2020-01-16 15:19:29 +0100 | [diff] [blame] | 711 | if (filter_count >= STKTABLE_FILTER_LEN) |
| 712 | return hlua_error(L, "Filter table too large (len > %d)", STKTABLE_FILTER_LEN); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 713 | |
| 714 | if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3) |
| 715 | return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1); |
| 716 | |
| 717 | lua_pushnil(L); |
| 718 | while (lua_next(L, -2) != 0) { |
| 719 | switch (entry_idx) { |
| 720 | case 0: |
| 721 | if (lua_type(L, -1) != LUA_TSTRING) |
| 722 | return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1); |
| 723 | |
| 724 | dt = stktable_get_data_type((char *)lua_tostring(L, -1)); |
| 725 | if (dt < 0 || t->data_ofs[dt] == 0) |
| 726 | return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1); |
| 727 | filter[filter_count].type = dt; |
| 728 | break; |
| 729 | case 1: |
| 730 | if (lua_type(L, -1) != LUA_TSTRING) |
| 731 | return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1); |
| 732 | |
| 733 | op = get_std_op(lua_tostring(L, -1)); |
| 734 | if (op < 0) |
| 735 | return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1); |
| 736 | filter[filter_count].op = op; |
| 737 | break; |
| 738 | case 2: |
| 739 | val = lua_tointeger(L, -1); |
| 740 | filter[filter_count].val = val; |
| 741 | filter_count++; |
| 742 | break; |
| 743 | default: |
| 744 | break; |
| 745 | } |
| 746 | entry_idx++; |
| 747 | lua_pop(L, 1); |
| 748 | } |
| 749 | lua_pop(L, 1); |
| 750 | } |
| 751 | break; |
| 752 | default: |
| 753 | return hlua_error(L, "filter table expected"); |
| 754 | } |
| 755 | |
| 756 | lua_newtable(L); |
| 757 | |
| 758 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 759 | eb = ebmb_first(&t->keys); |
| 760 | for (n = eb; n; n = ebmb_next(n)) { |
| 761 | ts = ebmb_entry(n, struct stksess, key); |
| 762 | if (!ts) { |
| 763 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 764 | return 1; |
| 765 | } |
| 766 | ts->ref_cnt++; |
| 767 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 768 | |
| 769 | /* multi condition/value filter */ |
| 770 | skip_entry = 0; |
| 771 | for (i = 0; i < filter_count; i++) { |
| 772 | if (t->data_ofs[filter[i].type] == 0) |
| 773 | continue; |
| 774 | |
| 775 | ptr = stktable_data_ptr(t, ts, filter[i].type); |
| 776 | |
| 777 | switch (stktable_data_types[filter[i].type].std_type) { |
| 778 | case STD_T_SINT: |
| 779 | val = stktable_data_cast(ptr, std_t_sint); |
| 780 | break; |
| 781 | case STD_T_UINT: |
| 782 | val = stktable_data_cast(ptr, std_t_uint); |
| 783 | break; |
| 784 | case STD_T_ULL: |
| 785 | val = stktable_data_cast(ptr, std_t_ull); |
| 786 | break; |
| 787 | case STD_T_FRQP: |
| 788 | val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 789 | t->data_arg[filter[i].type].u); |
| 790 | break; |
| 791 | default: |
| 792 | continue; |
| 793 | break; |
| 794 | } |
| 795 | |
| 796 | op = filter[i].op; |
| 797 | |
| 798 | if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) || |
| 799 | (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) || |
| 800 | (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) { |
| 801 | skip_entry = 1; |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (skip_entry) { |
| 807 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 808 | ts->ref_cnt--; |
| 809 | continue; |
| 810 | } |
| 811 | |
| 812 | if (t->type == SMP_T_IPV4) { |
| 813 | char addr[INET_ADDRSTRLEN]; |
| 814 | inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr)); |
| 815 | lua_pushstring(L, addr); |
| 816 | } else if (t->type == SMP_T_IPV6) { |
| 817 | char addr[INET6_ADDRSTRLEN]; |
| 818 | inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr)); |
| 819 | lua_pushstring(L, addr); |
| 820 | } else if (t->type == SMP_T_SINT) { |
| 821 | lua_pushinteger(L, *ts->key.key); |
| 822 | } else if (t->type == SMP_T_STR) { |
| 823 | lua_pushstring(L, (const char *)ts->key.key); |
| 824 | } else { |
| 825 | return hlua_error(L, "Unsupported stick table key type"); |
| 826 | } |
| 827 | |
| 828 | lua_newtable(L); |
| 829 | hlua_stktable_entry(L, t, ts); |
| 830 | lua_settable(L, -3); |
| 831 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 832 | ts->ref_cnt--; |
| 833 | } |
| 834 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 835 | |
| 836 | return 1; |
| 837 | } |
| 838 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 839 | int hlua_fcn_new_listener(lua_State *L, struct listener *lst) |
| 840 | { |
| 841 | lua_newtable(L); |
| 842 | |
| 843 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 844 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref); |
| 845 | lua_setmetatable(L, -2); |
| 846 | |
| 847 | lua_pushlightuserdata(L, lst); |
| 848 | lua_rawseti(L, -2, 0); |
| 849 | return 1; |
| 850 | } |
| 851 | |
| 852 | static struct listener *hlua_check_listener(lua_State *L, int ud) |
| 853 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 854 | return hlua_checkudata(L, ud, class_listener_ref); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | int hlua_listener_get_stats(lua_State *L) |
| 858 | { |
| 859 | struct listener *li; |
| 860 | int i; |
| 861 | |
| 862 | li = hlua_check_listener(L, 1); |
| 863 | |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 864 | if (!li->bind_conf->frontend) { |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 865 | lua_pushnil(L); |
| 866 | return 1; |
| 867 | } |
| 868 | |
William Dauchy | 655e14e | 2021-02-14 23:22:54 +0100 | [diff] [blame] | 869 | stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats, |
| 870 | STATS_LEN, NULL); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 871 | |
| 872 | lua_newtable(L); |
| 873 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 874 | lua_pushstring(L, stat_fields[i].name); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 875 | hlua_fcn_pushfield(L, &stats[i]); |
| 876 | lua_settable(L, -3); |
| 877 | } |
| 878 | return 1; |
| 879 | |
| 880 | } |
| 881 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 882 | int hlua_fcn_new_server(lua_State *L, struct server *srv) |
| 883 | { |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 884 | char buffer[12]; |
| 885 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 886 | lua_newtable(L); |
| 887 | |
| 888 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 889 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref); |
| 890 | lua_setmetatable(L, -2); |
| 891 | |
| 892 | lua_pushlightuserdata(L, srv); |
| 893 | lua_rawseti(L, -2, 0); |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 894 | |
| 895 | /* Add server name. */ |
| 896 | lua_pushstring(L, "name"); |
| 897 | lua_pushstring(L, srv->id); |
| 898 | lua_settable(L, -3); |
| 899 | |
| 900 | /* Add server puid. */ |
| 901 | lua_pushstring(L, "puid"); |
| 902 | snprintf(buffer, sizeof(buffer), "%d", srv->puid); |
| 903 | lua_pushstring(L, buffer); |
| 904 | lua_settable(L, -3); |
| 905 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 906 | return 1; |
| 907 | } |
| 908 | |
| 909 | static struct server *hlua_check_server(lua_State *L, int ud) |
| 910 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 911 | return hlua_checkudata(L, ud, class_server_ref); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | int hlua_server_get_stats(lua_State *L) |
| 915 | { |
| 916 | struct server *srv; |
| 917 | int i; |
| 918 | |
| 919 | srv = hlua_check_server(L, 1); |
| 920 | |
| 921 | if (!srv->proxy) { |
| 922 | lua_pushnil(L); |
| 923 | return 1; |
| 924 | } |
| 925 | |
William Dauchy | d3a9a49 | 2021-01-25 17:29:03 +0100 | [diff] [blame] | 926 | stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats, |
| 927 | STATS_LEN, NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 928 | |
| 929 | lua_newtable(L); |
| 930 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 931 | lua_pushstring(L, stat_fields[i].name); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 932 | hlua_fcn_pushfield(L, &stats[i]); |
| 933 | lua_settable(L, -3); |
| 934 | } |
| 935 | return 1; |
| 936 | |
| 937 | } |
| 938 | |
| 939 | int hlua_server_get_addr(lua_State *L) |
| 940 | { |
| 941 | struct server *srv; |
| 942 | char addr[INET6_ADDRSTRLEN]; |
| 943 | luaL_Buffer b; |
| 944 | |
| 945 | srv = hlua_check_server(L, 1); |
| 946 | |
| 947 | luaL_buffinit(L, &b); |
| 948 | |
| 949 | switch (srv->addr.ss_family) { |
| 950 | case AF_INET: |
| 951 | inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr, |
| 952 | addr, INET_ADDRSTRLEN); |
| 953 | luaL_addstring(&b, addr); |
| 954 | luaL_addstring(&b, ":"); |
Nenad Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 955 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 956 | luaL_addstring(&b, addr); |
| 957 | break; |
| 958 | case AF_INET6: |
| 959 | inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr, |
Nenad Merdanovic | a9f0404 | 2017-07-23 22:04:59 -0400 | [diff] [blame] | 960 | addr, INET6_ADDRSTRLEN); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 961 | luaL_addstring(&b, addr); |
| 962 | luaL_addstring(&b, ":"); |
Nenad Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 963 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 964 | luaL_addstring(&b, addr); |
| 965 | break; |
| 966 | case AF_UNIX: |
| 967 | luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path); |
| 968 | break; |
| 969 | default: |
| 970 | luaL_addstring(&b, "<unknown>"); |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | luaL_pushresult(&b); |
| 975 | return 1; |
| 976 | } |
| 977 | |
| 978 | int hlua_server_is_draining(lua_State *L) |
| 979 | { |
| 980 | struct server *srv; |
| 981 | |
| 982 | srv = hlua_check_server(L, 1); |
| 983 | lua_pushinteger(L, server_is_draining(srv)); |
| 984 | return 1; |
| 985 | } |
| 986 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 987 | int hlua_server_set_maxconn(lua_State *L) |
| 988 | { |
| 989 | struct server *srv; |
| 990 | const char *maxconn; |
| 991 | const char *err; |
| 992 | |
| 993 | srv = hlua_check_server(L, 1); |
| 994 | maxconn = luaL_checkstring(L, 2); |
| 995 | |
| 996 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 997 | err = server_parse_maxconn_change_request(srv, maxconn); |
| 998 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 999 | if (!err) |
| 1000 | lua_pushnil(L); |
| 1001 | else |
| 1002 | hlua_pushstrippedstring(L, err); |
| 1003 | return 1; |
| 1004 | } |
| 1005 | |
| 1006 | int hlua_server_get_maxconn(lua_State *L) |
| 1007 | { |
| 1008 | struct server *srv; |
| 1009 | |
| 1010 | srv = hlua_check_server(L, 1); |
| 1011 | lua_pushinteger(L, srv->maxconn); |
| 1012 | return 1; |
| 1013 | } |
| 1014 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1015 | int hlua_server_set_weight(lua_State *L) |
| 1016 | { |
| 1017 | struct server *srv; |
| 1018 | const char *weight; |
| 1019 | const char *err; |
| 1020 | |
| 1021 | srv = hlua_check_server(L, 1); |
| 1022 | weight = luaL_checkstring(L, 2); |
| 1023 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1024 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1025 | err = server_parse_weight_change_request(srv, weight); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1026 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1027 | if (!err) |
| 1028 | lua_pushnil(L); |
| 1029 | else |
| 1030 | hlua_pushstrippedstring(L, err); |
| 1031 | return 1; |
| 1032 | } |
| 1033 | |
| 1034 | int hlua_server_get_weight(lua_State *L) |
| 1035 | { |
| 1036 | struct server *srv; |
| 1037 | |
| 1038 | srv = hlua_check_server(L, 1); |
| 1039 | lua_pushinteger(L, srv->uweight); |
| 1040 | return 1; |
| 1041 | } |
| 1042 | |
| 1043 | int hlua_server_set_addr(lua_State *L) |
| 1044 | { |
| 1045 | struct server *srv; |
| 1046 | const char *addr; |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 1047 | const char *port; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1048 | const char *err; |
| 1049 | |
| 1050 | srv = hlua_check_server(L, 1); |
| 1051 | addr = luaL_checkstring(L, 2); |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 1052 | if (lua_gettop(L) >= 3) |
| 1053 | port = luaL_checkstring(L, 3); |
| 1054 | else |
| 1055 | port = NULL; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1056 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1057 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 1058 | err = update_server_addr_port(srv, addr, port, "Lua script"); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1059 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1060 | if (!err) |
| 1061 | lua_pushnil(L); |
| 1062 | else |
| 1063 | hlua_pushstrippedstring(L, err); |
| 1064 | return 1; |
| 1065 | } |
| 1066 | |
| 1067 | int hlua_server_shut_sess(lua_State *L) |
| 1068 | { |
| 1069 | struct server *srv; |
| 1070 | |
| 1071 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1072 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1073 | srv_shutdown_streams(srv, SF_ERR_KILLED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1074 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | int hlua_server_set_drain(lua_State *L) |
| 1079 | { |
| 1080 | struct server *srv; |
| 1081 | |
| 1082 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1083 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1084 | srv_adm_set_drain(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1085 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | int hlua_server_set_maint(lua_State *L) |
| 1090 | { |
| 1091 | struct server *srv; |
| 1092 | |
| 1093 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1094 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1095 | srv_adm_set_maint(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1096 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | int hlua_server_set_ready(lua_State *L) |
| 1101 | { |
| 1102 | struct server *srv; |
| 1103 | |
| 1104 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1105 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1106 | srv_adm_set_ready(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1107 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | int hlua_server_check_enable(lua_State *L) |
| 1112 | { |
| 1113 | struct server *sv; |
| 1114 | |
| 1115 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1116 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1117 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 1118 | sv->check.state |= CHK_ST_ENABLED; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1119 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1120 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | int hlua_server_check_disable(lua_State *L) |
| 1125 | { |
| 1126 | struct server *sv; |
| 1127 | |
| 1128 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1129 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1130 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 1131 | sv->check.state &= ~CHK_ST_ENABLED; |
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_up(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_running(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_nolb(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 = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1159 | srv_set_stopping(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_check_force_down(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->track)) { |
| 1172 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1173 | srv_set_stopped(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1174 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1175 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | int hlua_server_agent_enable(lua_State *L) |
| 1180 | { |
| 1181 | struct server *sv; |
| 1182 | |
| 1183 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1184 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1185 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 1186 | sv->agent.state |= CHK_ST_ENABLED; |
| 1187 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1188 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | int hlua_server_agent_disable(lua_State *L) |
| 1193 | { |
| 1194 | struct server *sv; |
| 1195 | |
| 1196 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1197 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1198 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 1199 | sv->agent.state &= ~CHK_ST_ENABLED; |
| 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_up(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 = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1213 | srv_set_running(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 | |
| 1219 | int hlua_server_agent_force_down(lua_State *L) |
| 1220 | { |
| 1221 | struct server *sv; |
| 1222 | |
| 1223 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1224 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1225 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 1226 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1227 | srv_set_stopped(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1228 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1229 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1230 | return 0; |
| 1231 | } |
| 1232 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1233 | int hlua_fcn_new_proxy(lua_State *L, struct proxy *px) |
| 1234 | { |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1235 | struct server *srv; |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1236 | struct listener *lst; |
| 1237 | int lid; |
Willy Tarreau | 29d6980 | 2018-05-06 14:50:09 +0200 | [diff] [blame] | 1238 | char buffer[17]; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1239 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1240 | lua_newtable(L); |
| 1241 | |
| 1242 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 1243 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref); |
| 1244 | lua_setmetatable(L, -2); |
| 1245 | |
| 1246 | lua_pushlightuserdata(L, px); |
| 1247 | lua_rawseti(L, -2, 0); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1248 | |
Thierry FOURNIER | f2bbe38 | 2017-07-24 13:59:22 +0200 | [diff] [blame] | 1249 | /* Add proxy name. */ |
| 1250 | lua_pushstring(L, "name"); |
| 1251 | lua_pushstring(L, px->id); |
| 1252 | lua_settable(L, -3); |
| 1253 | |
Baptiste Assmann | 46c7255 | 2017-10-26 21:51:58 +0200 | [diff] [blame] | 1254 | /* Add proxy uuid. */ |
| 1255 | lua_pushstring(L, "uuid"); |
| 1256 | snprintf(buffer, sizeof(buffer), "%d", px->uuid); |
| 1257 | lua_pushstring(L, buffer); |
| 1258 | lua_settable(L, -3); |
| 1259 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1260 | /* Browse and register servers. */ |
| 1261 | lua_pushstring(L, "servers"); |
| 1262 | lua_newtable(L); |
| 1263 | for (srv = px->srv; srv; srv = srv->next) { |
| 1264 | lua_pushstring(L, srv->id); |
| 1265 | hlua_fcn_new_server(L, srv); |
| 1266 | lua_settable(L, -3); |
| 1267 | } |
| 1268 | lua_settable(L, -3); |
| 1269 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1270 | /* Browse and register listeners. */ |
| 1271 | lua_pushstring(L, "listeners"); |
| 1272 | lua_newtable(L); |
| 1273 | lid = 1; |
| 1274 | list_for_each_entry(lst, &px->conf.listeners, by_fe) { |
| 1275 | if (lst->name) |
| 1276 | lua_pushstring(L, lst->name); |
| 1277 | else { |
Willy Tarreau | 29d6980 | 2018-05-06 14:50:09 +0200 | [diff] [blame] | 1278 | snprintf(buffer, sizeof(buffer), "sock-%d", lid); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1279 | lid++; |
| 1280 | lua_pushstring(L, buffer); |
| 1281 | } |
| 1282 | hlua_fcn_new_listener(L, lst); |
| 1283 | lua_settable(L, -3); |
| 1284 | } |
| 1285 | lua_settable(L, -3); |
| 1286 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1287 | if (px->table && px->table->id) { |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1288 | lua_pushstring(L, "stktable"); |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1289 | hlua_fcn_new_stktable(L, px->table); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1290 | lua_settable(L, -3); |
| 1291 | } |
| 1292 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1293 | return 1; |
| 1294 | } |
| 1295 | |
| 1296 | static struct proxy *hlua_check_proxy(lua_State *L, int ud) |
| 1297 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1298 | return hlua_checkudata(L, ud, class_proxy_ref); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | int hlua_proxy_pause(lua_State *L) |
| 1302 | { |
| 1303 | struct proxy *px; |
| 1304 | |
| 1305 | px = hlua_check_proxy(L, 1); |
| 1306 | pause_proxy(px); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | int hlua_proxy_resume(lua_State *L) |
| 1311 | { |
| 1312 | struct proxy *px; |
| 1313 | |
| 1314 | px = hlua_check_proxy(L, 1); |
| 1315 | resume_proxy(px); |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | int hlua_proxy_stop(lua_State *L) |
| 1320 | { |
| 1321 | struct proxy *px; |
| 1322 | |
| 1323 | px = hlua_check_proxy(L, 1); |
| 1324 | stop_proxy(px); |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | int hlua_proxy_get_cap(lua_State *L) |
| 1329 | { |
| 1330 | struct proxy *px; |
| 1331 | const char *str; |
| 1332 | |
| 1333 | px = hlua_check_proxy(L, 1); |
| 1334 | str = proxy_cap_str(px->cap); |
| 1335 | lua_pushstring(L, str); |
| 1336 | return 1; |
| 1337 | } |
| 1338 | |
| 1339 | int hlua_proxy_get_stats(lua_State *L) |
| 1340 | { |
| 1341 | struct proxy *px; |
| 1342 | int i; |
| 1343 | |
| 1344 | px = hlua_check_proxy(L, 1); |
| 1345 | if (px->cap & PR_CAP_BE) |
William Dauchy | da3b466 | 2021-01-25 17:29:01 +0100 | [diff] [blame] | 1346 | stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1347 | else |
William Dauchy | 0ef5439 | 2021-01-17 18:27:45 +0100 | [diff] [blame] | 1348 | stats_fill_fe_stats(px, stats, STATS_LEN, NULL); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1349 | lua_newtable(L); |
| 1350 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 1351 | lua_pushstring(L, stat_fields[i].name); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1352 | hlua_fcn_pushfield(L, &stats[i]); |
| 1353 | lua_settable(L, -3); |
| 1354 | } |
| 1355 | return 1; |
| 1356 | } |
| 1357 | |
| 1358 | int hlua_proxy_get_mode(lua_State *L) |
| 1359 | { |
| 1360 | struct proxy *px; |
| 1361 | const char *str; |
| 1362 | |
| 1363 | px = hlua_check_proxy(L, 1); |
| 1364 | str = proxy_mode_str(px->mode); |
| 1365 | lua_pushstring(L, str); |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
| 1369 | int hlua_proxy_shut_bcksess(lua_State *L) |
| 1370 | { |
| 1371 | struct proxy *px; |
| 1372 | |
| 1373 | px = hlua_check_proxy(L, 1); |
| 1374 | srv_shutdown_backup_streams(px, SF_ERR_KILLED); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1378 | int hlua_fcn_post_init(lua_State *L) |
| 1379 | { |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1380 | struct proxy *px; |
| 1381 | |
| 1382 | /* get core array. */ |
| 1383 | if (lua_getglobal(L, "core") != LUA_TTABLE) |
| 1384 | lua_error(L); |
| 1385 | |
| 1386 | /* Create proxies entry. */ |
| 1387 | lua_pushstring(L, "proxies"); |
| 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 | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1392 | lua_pushstring(L, px->id); |
| 1393 | hlua_fcn_new_proxy(L, px); |
| 1394 | lua_settable(L, -3); |
| 1395 | } |
| 1396 | |
| 1397 | /* push "proxies" in "core" */ |
| 1398 | lua_settable(L, -3); |
| 1399 | |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1400 | /* Create proxies entry. */ |
| 1401 | lua_pushstring(L, "frontends"); |
| 1402 | lua_newtable(L); |
| 1403 | |
| 1404 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1405 | for (px = proxies_list; px; px = px->next) { |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1406 | if (!(px->cap & PR_CAP_FE)) |
| 1407 | continue; |
| 1408 | lua_pushstring(L, px->id); |
| 1409 | hlua_fcn_new_proxy(L, px); |
| 1410 | lua_settable(L, -3); |
| 1411 | } |
| 1412 | |
| 1413 | /* push "frontends" in "core" */ |
| 1414 | lua_settable(L, -3); |
| 1415 | |
| 1416 | /* Create proxies entry. */ |
| 1417 | lua_pushstring(L, "backends"); |
| 1418 | lua_newtable(L); |
| 1419 | |
| 1420 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1421 | for (px = proxies_list; px; px = px->next) { |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1422 | if (!(px->cap & PR_CAP_BE)) |
| 1423 | continue; |
| 1424 | lua_pushstring(L, px->id); |
| 1425 | hlua_fcn_new_proxy(L, px); |
| 1426 | lua_settable(L, -3); |
| 1427 | } |
| 1428 | |
| 1429 | /* push "backend" in "core" */ |
| 1430 | lua_settable(L, -3); |
| 1431 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1432 | return 1; |
| 1433 | } |
| 1434 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1435 | /* This Lua function take a string, a list of separators. |
| 1436 | * It tokenize the input string using the list of separators |
| 1437 | * as separator. |
| 1438 | * |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1439 | * The functionreturns a table filled with tokens. |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1440 | */ |
| 1441 | int hlua_tokenize(lua_State *L) |
| 1442 | { |
| 1443 | const char *str; |
| 1444 | const char *sep; |
| 1445 | int index; |
| 1446 | const char *token; |
| 1447 | const char *p; |
| 1448 | const char *c; |
| 1449 | int ignore_empty; |
| 1450 | |
| 1451 | ignore_empty = 0; |
| 1452 | |
| 1453 | str = luaL_checkstring(L, 1); |
| 1454 | sep = luaL_checkstring(L, 2); |
| 1455 | if (lua_gettop(L) == 3) |
| 1456 | ignore_empty = hlua_checkboolean(L, 3); |
| 1457 | |
| 1458 | lua_newtable(L); |
| 1459 | index = 1; |
| 1460 | token = str; |
| 1461 | p = str; |
| 1462 | while(1) { |
| 1463 | for (c = sep; *c != '\0'; c++) |
| 1464 | if (*p == *c) |
| 1465 | break; |
| 1466 | if (*p == *c) { |
| 1467 | if ((!ignore_empty) || (p - token > 0)) { |
| 1468 | lua_pushlstring(L, token, p - token); |
| 1469 | lua_rawseti(L, -2, index); |
| 1470 | index++; |
| 1471 | } |
| 1472 | token = p + 1; |
| 1473 | } |
| 1474 | if (*p == '\0') |
| 1475 | break; |
| 1476 | p++; |
| 1477 | } |
| 1478 | |
| 1479 | return 1; |
| 1480 | } |
| 1481 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1482 | int hlua_parse_addr(lua_State *L) |
| 1483 | { |
| 1484 | struct hlua_addr *addr; |
| 1485 | const char *str = luaL_checkstring(L, 1); |
| 1486 | unsigned char mask; |
| 1487 | |
| 1488 | addr = lua_newuserdata(L, sizeof(struct hlua_addr)); |
| 1489 | if (!addr) { |
| 1490 | lua_pushnil(L); |
| 1491 | return 1; |
| 1492 | } |
| 1493 | |
| 1494 | if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) { |
| 1495 | addr->type = AF_INET; |
| 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | if (str62net(str, &addr->addr.v6.ip, &mask)) { |
| 1500 | len2mask6(mask, &addr->addr.v6.mask); |
| 1501 | addr->type = AF_INET6; |
| 1502 | return 1; |
| 1503 | } |
| 1504 | |
| 1505 | lua_pop(L, 1); |
| 1506 | lua_pushnil(L); |
| 1507 | return 1; |
| 1508 | } |
| 1509 | |
| 1510 | int hlua_match_addr(lua_State *L) |
| 1511 | { |
| 1512 | struct hlua_addr *addr1; |
| 1513 | struct hlua_addr *addr2; |
| 1514 | |
| 1515 | if (!lua_isuserdata(L, 1) || |
| 1516 | !lua_isuserdata(L, 2)) { |
| 1517 | lua_pushboolean(L, 0); |
| 1518 | return 1; |
| 1519 | } |
| 1520 | |
| 1521 | addr1 = lua_touserdata(L, 1); |
| 1522 | addr2 = lua_touserdata(L, 2); |
| 1523 | |
| 1524 | if (addr1->type != addr2->type) { |
| 1525 | lua_pushboolean(L, 0); |
| 1526 | return 1; |
| 1527 | } |
| 1528 | |
| 1529 | if (addr1->type == AF_INET) { |
| 1530 | if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) == |
| 1531 | (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) { |
| 1532 | lua_pushboolean(L, 1); |
| 1533 | return 1; |
| 1534 | } |
| 1535 | } else { |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 1536 | int i; |
| 1537 | |
| 1538 | for (i = 0; i < 16; i += 4) { |
Willy Tarreau | 26474c4 | 2020-02-25 10:02:51 +0100 | [diff] [blame] | 1539 | if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) & |
| 1540 | read_u32(&addr2->addr.v6.mask.s6_addr[i])) != |
| 1541 | (read_u32(&addr2->addr.v6.ip.s6_addr[i]) & |
| 1542 | read_u32(&addr1->addr.v6.mask.s6_addr[i]))) |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 1543 | break; |
| 1544 | } |
| 1545 | if (i == 16) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1546 | lua_pushboolean(L, 1); |
| 1547 | return 1; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | lua_pushboolean(L, 0); |
| 1552 | return 1; |
| 1553 | } |
| 1554 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1555 | static struct my_regex **hlua_check_regex(lua_State *L, int ud) |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1556 | { |
| 1557 | return (hlua_checkudata(L, ud, class_regex_ref)); |
| 1558 | } |
| 1559 | |
| 1560 | static int hlua_regex_comp(struct lua_State *L) |
| 1561 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1562 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1563 | const char *str; |
| 1564 | int cs; |
| 1565 | char *err; |
| 1566 | |
| 1567 | str = luaL_checkstring(L, 1); |
| 1568 | luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL); |
| 1569 | cs = lua_toboolean(L, 2); |
| 1570 | |
| 1571 | regex = lua_newuserdata(L, sizeof(*regex)); |
| 1572 | |
| 1573 | err = NULL; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1574 | if (!(*regex = regex_comp(str, cs, 1, &err))) { |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1575 | lua_pushboolean(L, 0); /* status error */ |
| 1576 | lua_pushstring(L, err); /* Reason */ |
| 1577 | free(err); |
| 1578 | return 2; |
| 1579 | } |
| 1580 | |
| 1581 | lua_pushboolean(L, 1); /* Status ok */ |
| 1582 | |
| 1583 | /* Create object */ |
| 1584 | lua_newtable(L); |
| 1585 | lua_pushvalue(L, -3); /* Get the userdata pointer. */ |
| 1586 | lua_rawseti(L, -2, 0); |
| 1587 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref); |
| 1588 | lua_setmetatable(L, -2); |
| 1589 | return 2; |
| 1590 | } |
| 1591 | |
| 1592 | static int hlua_regex_exec(struct lua_State *L) |
| 1593 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1594 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1595 | const char *str; |
| 1596 | size_t len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1597 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1598 | |
| 1599 | regex = hlua_check_regex(L, 1); |
| 1600 | str = luaL_checklstring(L, 2, &len); |
| 1601 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1602 | if (!*regex) { |
| 1603 | lua_pushboolean(L, 0); |
| 1604 | return 1; |
| 1605 | } |
| 1606 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1607 | /* Copy the string because regex_exec2 require a 'char *' |
| 1608 | * and not a 'const char *'. |
| 1609 | */ |
| 1610 | tmp = get_trash_chunk(); |
| 1611 | if (len >= tmp->size) { |
| 1612 | lua_pushboolean(L, 0); |
| 1613 | return 1; |
| 1614 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1615 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1616 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1617 | lua_pushboolean(L, regex_exec2(*regex, tmp->area, len)); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1618 | |
| 1619 | return 1; |
| 1620 | } |
| 1621 | |
| 1622 | static int hlua_regex_match(struct lua_State *L) |
| 1623 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1624 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1625 | const char *str; |
| 1626 | size_t len; |
| 1627 | regmatch_t pmatch[20]; |
| 1628 | int ret; |
| 1629 | int i; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1630 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1631 | |
| 1632 | regex = hlua_check_regex(L, 1); |
| 1633 | str = luaL_checklstring(L, 2, &len); |
| 1634 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1635 | if (!*regex) { |
| 1636 | lua_pushboolean(L, 0); |
| 1637 | return 1; |
| 1638 | } |
| 1639 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1640 | /* Copy the string because regex_exec2 require a 'char *' |
| 1641 | * and not a 'const char *'. |
| 1642 | */ |
| 1643 | tmp = get_trash_chunk(); |
| 1644 | if (len >= tmp->size) { |
| 1645 | lua_pushboolean(L, 0); |
| 1646 | return 1; |
| 1647 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1648 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1649 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1650 | ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1651 | lua_pushboolean(L, ret); |
| 1652 | lua_newtable(L); |
| 1653 | if (ret) { |
| 1654 | for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) { |
| 1655 | lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so); |
| 1656 | lua_rawseti(L, -2, i + 1); |
| 1657 | } |
| 1658 | } |
| 1659 | return 2; |
| 1660 | } |
| 1661 | |
| 1662 | static int hlua_regex_free(struct lua_State *L) |
| 1663 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1664 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1665 | |
| 1666 | regex = hlua_check_regex(L, 1); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1667 | regex_free(*regex); |
| 1668 | *regex = NULL; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1669 | return 0; |
| 1670 | } |
| 1671 | |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1672 | int hlua_fcn_reg_core_fcn(lua_State *L) |
| 1673 | { |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1674 | if (!hlua_concat_init(L)) |
| 1675 | return 0; |
| 1676 | |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1677 | hlua_class_function(L, "now", hlua_now); |
| 1678 | hlua_class_function(L, "http_date", hlua_http_date); |
| 1679 | hlua_class_function(L, "imf_date", hlua_imf_date); |
| 1680 | hlua_class_function(L, "rfc850_date", hlua_rfc850_date); |
| 1681 | hlua_class_function(L, "asctime_date", hlua_asctime_date); |
| 1682 | hlua_class_function(L, "concat", hlua_concat_new); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 1683 | hlua_class_function(L, "get_info", hlua_get_info); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1684 | hlua_class_function(L, "parse_addr", hlua_parse_addr); |
| 1685 | hlua_class_function(L, "match_addr", hlua_match_addr); |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1686 | hlua_class_function(L, "tokenize", hlua_tokenize); |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1687 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1688 | /* Create regex object. */ |
| 1689 | lua_newtable(L); |
| 1690 | hlua_class_function(L, "new", hlua_regex_comp); |
| 1691 | |
| 1692 | lua_newtable(L); /* The metatable. */ |
| 1693 | lua_pushstring(L, "__index"); |
| 1694 | lua_newtable(L); |
| 1695 | hlua_class_function(L, "exec", hlua_regex_exec); |
| 1696 | hlua_class_function(L, "match", hlua_regex_match); |
| 1697 | lua_rawset(L, -3); /* -> META["__index"] = TABLE */ |
| 1698 | hlua_class_function(L, "__gc", hlua_regex_free); |
| 1699 | |
| 1700 | lua_pushvalue(L, -1); /* Duplicate the metatable reference. */ |
| 1701 | class_regex_ref = hlua_register_metatable(L, CLASS_REGEX); |
| 1702 | |
| 1703 | lua_setmetatable(L, -2); |
| 1704 | lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */ |
| 1705 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1706 | /* Create stktable object. */ |
| 1707 | lua_newtable(L); |
| 1708 | lua_pushstring(L, "__index"); |
| 1709 | lua_newtable(L); |
| 1710 | hlua_class_function(L, "info", hlua_stktable_info); |
| 1711 | hlua_class_function(L, "lookup", hlua_stktable_lookup); |
| 1712 | hlua_class_function(L, "dump", hlua_stktable_dump); |
| 1713 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1714 | class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE); |
| 1715 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1716 | /* Create listener object. */ |
| 1717 | lua_newtable(L); |
| 1718 | lua_pushstring(L, "__index"); |
| 1719 | lua_newtable(L); |
| 1720 | hlua_class_function(L, "get_stats", hlua_listener_get_stats); |
| 1721 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1722 | class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER); |
| 1723 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1724 | /* Create server object. */ |
| 1725 | lua_newtable(L); |
| 1726 | lua_pushstring(L, "__index"); |
| 1727 | lua_newtable(L); |
| 1728 | hlua_class_function(L, "is_draining", hlua_server_is_draining); |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1729 | hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn); |
| 1730 | hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1731 | hlua_class_function(L, "set_weight", hlua_server_set_weight); |
| 1732 | hlua_class_function(L, "get_weight", hlua_server_get_weight); |
| 1733 | hlua_class_function(L, "set_addr", hlua_server_set_addr); |
| 1734 | hlua_class_function(L, "get_addr", hlua_server_get_addr); |
| 1735 | hlua_class_function(L, "get_stats", hlua_server_get_stats); |
| 1736 | hlua_class_function(L, "shut_sess", hlua_server_shut_sess); |
| 1737 | hlua_class_function(L, "set_drain", hlua_server_set_drain); |
| 1738 | hlua_class_function(L, "set_maint", hlua_server_set_maint); |
| 1739 | hlua_class_function(L, "set_ready", hlua_server_set_ready); |
| 1740 | hlua_class_function(L, "check_enable", hlua_server_check_enable); |
| 1741 | hlua_class_function(L, "check_disable", hlua_server_check_disable); |
| 1742 | hlua_class_function(L, "check_force_up", hlua_server_check_force_up); |
| 1743 | hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb); |
| 1744 | hlua_class_function(L, "check_force_down", hlua_server_check_force_down); |
| 1745 | hlua_class_function(L, "agent_enable", hlua_server_agent_enable); |
| 1746 | hlua_class_function(L, "agent_disable", hlua_server_agent_disable); |
| 1747 | hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up); |
| 1748 | hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down); |
| 1749 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1750 | class_server_ref = hlua_register_metatable(L, CLASS_SERVER); |
| 1751 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1752 | /* Create proxy object. */ |
| 1753 | lua_newtable(L); |
| 1754 | lua_pushstring(L, "__index"); |
| 1755 | lua_newtable(L); |
| 1756 | hlua_class_function(L, "pause", hlua_proxy_pause); |
| 1757 | hlua_class_function(L, "resume", hlua_proxy_resume); |
| 1758 | hlua_class_function(L, "stop", hlua_proxy_stop); |
| 1759 | hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess); |
| 1760 | hlua_class_function(L, "get_cap", hlua_proxy_get_cap); |
| 1761 | hlua_class_function(L, "get_mode", hlua_proxy_get_mode); |
| 1762 | hlua_class_function(L, "get_stats", hlua_proxy_get_stats); |
| 1763 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1764 | class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY); |
| 1765 | |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 1766 | return 5; |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1767 | } |