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 | 63617db | 2021-10-06 18:23:40 +0200 | [diff] [blame] | 24 | #include <import/ebmbtree.h> |
| 25 | |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 26 | #include <haproxy/cli-t.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 27 | #include <haproxy/errors.h> |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 28 | #include <haproxy/hlua.h> |
Tim Duesterhus | 6eded62 | 2022-05-14 22:17:25 +0200 | [diff] [blame] | 29 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 30 | #include <haproxy/http.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 31 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 32 | #include <haproxy/pattern-t.h> |
| 33 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 34 | #include <haproxy/regex.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 35 | #include <haproxy/server.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 36 | #include <haproxy/stats.h> |
Willy Tarreau | 872f2ea | 2020-06-04 18:46:44 +0200 | [diff] [blame] | 37 | #include <haproxy/stick_table.h> |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 38 | #include <haproxy/event_hdl.h> |
Willy Tarreau | 2753940 | 2021-10-06 09:12:44 +0200 | [diff] [blame] | 39 | #include <haproxy/stream-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <haproxy/time.h> |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 41 | #include <haproxy/tools.h> |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 42 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 43 | /* Contains the class reference of the concat object. */ |
| 44 | static int class_concat_ref; |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 45 | static int class_proxy_ref; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 46 | static int class_server_ref; |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 47 | static int class_listener_ref; |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 48 | static int class_event_sub_ref; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 49 | static int class_regex_ref; |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 50 | static int class_stktable_ref; |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 51 | static int class_proxy_list_ref; |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 52 | static int class_server_list_ref; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 53 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 54 | #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] | 55 | |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 56 | static THREAD_LOCAL struct field stats[STATS_LEN]; |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 57 | |
Thierry FOURNIER / OZON.IO | 7f3aa8b | 2016-11-24 20:37:38 +0100 | [diff] [blame] | 58 | int hlua_checkboolean(lua_State *L, int index) |
| 59 | { |
| 60 | if (!lua_isboolean(L, index)) |
| 61 | luaL_argerror(L, index, "boolean expected"); |
| 62 | return lua_toboolean(L, index); |
| 63 | } |
| 64 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 65 | /* Helper to push unsigned integers to Lua stack, respecting Lua limitations */ |
| 66 | static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val) |
| 67 | { |
| 68 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 69 | lua_pushinteger(L, val); |
| 70 | #else |
| 71 | if (val > INT_MAX) |
| 72 | lua_pushnumber(L, (lua_Number)val); |
| 73 | else |
| 74 | lua_pushinteger(L, (int)val); |
| 75 | #endif |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | /* Helper to push unsigned long long to Lua stack, respecting Lua limitations */ |
| 80 | static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) { |
| 81 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 82 | /* 64 bits case, U64 is supported until LLONG_MAX */ |
| 83 | if (val > LLONG_MAX) |
| 84 | lua_pushnumber(L, (lua_Number)val); |
| 85 | else |
| 86 | lua_pushinteger(L, val); |
| 87 | #else |
| 88 | /* 32 bits case, U64 is supported until INT_MAX */ |
| 89 | if (val > INT_MAX) |
| 90 | lua_pushnumber(L, (lua_Number)val); |
| 91 | else |
| 92 | lua_pushinteger(L, (int)val); |
| 93 | #endif |
| 94 | return 1; |
| 95 | } |
| 96 | |
Joseph Herlant | b3d92e3 | 2018-11-15 09:35:04 -0800 | [diff] [blame] | 97 | /* This function gets a struct field and converts it in Lua |
| 98 | * variable. The variable is pushed at the top of the stack. |
Thierry Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 99 | */ |
| 100 | int hlua_fcn_pushfield(lua_State *L, struct field *field) |
| 101 | { |
| 102 | /* The lua_Integer is always signed. Its length depends on |
Joseph Herlant | b3d92e3 | 2018-11-15 09:35:04 -0800 | [diff] [blame] | 103 | * compilation options, so the following code is conditioned |
Thierry Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 104 | * by some macros. Windows maros are not supported. |
| 105 | * If the number cannot be represented as integer, we try to |
| 106 | * convert to float. |
| 107 | */ |
| 108 | switch (field_format(field, 0)) { |
| 109 | |
| 110 | case FF_EMPTY: |
| 111 | lua_pushnil(L); |
| 112 | return 1; |
| 113 | |
| 114 | case FF_S32: |
| 115 | /* S32 is always supported. */ |
| 116 | lua_pushinteger(L, field->u.s32); |
| 117 | return 1; |
| 118 | |
| 119 | case FF_U32: |
| 120 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 121 | /* 64 bits case, U32 is always supported */ |
| 122 | lua_pushinteger(L, field->u.u32); |
| 123 | #else |
| 124 | /* 32 bits case, U32 is supported until INT_MAX. */ |
| 125 | if (field->u.u32 > INT_MAX) |
| 126 | lua_pushnumber(L, (lua_Number)field->u.u32); |
| 127 | else |
| 128 | lua_pushinteger(L, field->u.u32); |
| 129 | #endif |
| 130 | return 1; |
| 131 | |
| 132 | case FF_S64: |
| 133 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 134 | /* 64 bits case, S64 is always supported */ |
| 135 | lua_pushinteger(L, field->u.s64); |
| 136 | #else |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 137 | /* 64 bits case, S64 is supported between INT_MIN and INT_MAX */ |
Thierry Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 138 | if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX) |
| 139 | lua_pushnumber(L, (lua_Number)field->u.s64); |
| 140 | else |
| 141 | lua_pushinteger(L, (int)field->u.s64); |
| 142 | #endif |
| 143 | return 1; |
| 144 | |
| 145 | case FF_U64: |
| 146 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 147 | /* 64 bits case, U64 is supported until LLONG_MAX */ |
| 148 | if (field->u.u64 > LLONG_MAX) |
| 149 | lua_pushnumber(L, (lua_Number)field->u.u64); |
| 150 | else |
| 151 | lua_pushinteger(L, field->u.u64); |
| 152 | #else |
| 153 | /* 64 bits case, U64 is supported until INT_MAX */ |
| 154 | if (field->u.u64 > INT_MAX) |
| 155 | lua_pushnumber(L, (lua_Number)field->u.u64); |
| 156 | else |
| 157 | lua_pushinteger(L, (int)field->u.u64); |
| 158 | #endif |
| 159 | return 1; |
| 160 | |
| 161 | case FF_STR: |
| 162 | lua_pushstring(L, field->u.str); |
| 163 | return 1; |
| 164 | |
| 165 | default: |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | /* Default case, never reached. */ |
| 170 | lua_pushnil(L); |
| 171 | return 1; |
| 172 | } |
| 173 | |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 174 | /* Some string are started or terminated by blank chars, |
| 175 | * this function removes the spaces, tabs, \r and |
| 176 | * \n at the begin and at the end of the string "str", and |
| 177 | * push the result in the lua stack. |
| 178 | * Returns a pointer to the Lua internal copy of the string. |
| 179 | */ |
| 180 | const char *hlua_pushstrippedstring(lua_State *L, const char *str) |
| 181 | { |
| 182 | const char *p; |
Christopher Faulet | 2ec4e3c | 2021-03-03 19:36:51 +0100 | [diff] [blame] | 183 | int l; |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 184 | |
| 185 | for (p = str; HTTP_IS_LWS(*p); p++); |
Christopher Faulet | 2ec4e3c | 2021-03-03 19:36:51 +0100 | [diff] [blame] | 186 | |
| 187 | for (l = strlen(p); l && HTTP_IS_LWS(p[l-1]); l--); |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 188 | |
Christopher Faulet | 2ec4e3c | 2021-03-03 19:36:51 +0100 | [diff] [blame] | 189 | return lua_pushlstring(L, p, l); |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 192 | /* The three following functions are useful for adding entries |
| 193 | * in a table. These functions takes a string and respectively an |
| 194 | * integer, a string or a function and add it to the table in the |
| 195 | * top of the stack. |
| 196 | * |
| 197 | * These functions throws an error if no more stack size is |
| 198 | * available. |
| 199 | */ |
| 200 | void hlua_class_const_int(lua_State *L, const char *name, int value) |
| 201 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 202 | lua_pushstring(L, name); |
| 203 | lua_pushinteger(L, value); |
| 204 | lua_rawset(L, -3); |
| 205 | } |
| 206 | void hlua_class_const_str(lua_State *L, const char *name, const char *value) |
| 207 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 208 | lua_pushstring(L, name); |
| 209 | lua_pushstring(L, value); |
| 210 | lua_rawset(L, -3); |
| 211 | } |
| 212 | void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L)) |
| 213 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 214 | lua_pushstring(L, name); |
| 215 | lua_pushcclosure(L, function, 0); |
| 216 | lua_rawset(L, -3); |
| 217 | } |
| 218 | |
Joseph Herlant | b3d92e3 | 2018-11-15 09:35:04 -0800 | [diff] [blame] | 219 | /* This function returns a string containing the HAProxy object name. */ |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 220 | int hlua_dump_object(struct lua_State *L) |
| 221 | { |
| 222 | const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1)); |
| 223 | lua_pushfstring(L, "HAProxy class %s", name); |
| 224 | return 1; |
| 225 | } |
| 226 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 227 | /* This function register a table as metatable and. It names |
| 228 | * the metatable, and returns the associated reference. |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 229 | * The original table is popped from the top of the stack. |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 230 | * "name" is the referenced class name. |
| 231 | */ |
| 232 | int hlua_register_metatable(struct lua_State *L, char *name) |
| 233 | { |
| 234 | /* Check the type of the top element. it must be |
| 235 | * a table. |
| 236 | */ |
| 237 | if (lua_type(L, -1) != LUA_TTABLE) |
| 238 | luaL_error(L, "hlua_register_metatable() requires a type Table " |
| 239 | "in the top of the stack"); |
| 240 | |
| 241 | /* Add the __tostring function which identify the |
| 242 | * created object. |
| 243 | */ |
| 244 | lua_pushstring(L, "__tostring"); |
| 245 | lua_pushstring(L, name); |
| 246 | lua_pushcclosure(L, hlua_dump_object, 1); |
| 247 | lua_rawset(L, -3); |
| 248 | |
| 249 | /* Register a named entry for the table. The table |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 250 | * reference is copied first because the function |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 251 | * lua_setfield() pop the entry. |
| 252 | */ |
| 253 | lua_pushvalue(L, -1); |
| 254 | lua_setfield(L, LUA_REGISTRYINDEX, name); |
| 255 | |
| 256 | /* Creates the reference of the object. The |
| 257 | * function luaL_ref pop the top of the stack. |
| 258 | */ |
| 259 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 260 | } |
| 261 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 262 | /* Return an object of the expected type, or throws an error. */ |
| 263 | void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 264 | { |
| 265 | void *p; |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 266 | int ret; |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 267 | |
| 268 | /* Check if the stack entry is an array. */ |
| 269 | if (!lua_istable(L, ud)) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 270 | luaL_argerror(L, ud, NULL); |
| 271 | |
| 272 | /* pop the metatable of the referencecd object. */ |
| 273 | if (!lua_getmetatable(L, ud)) |
| 274 | luaL_argerror(L, ud, NULL); |
| 275 | |
| 276 | /* pop the expected metatable. */ |
| 277 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 278 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 279 | /* Check if the metadata have the expected type. */ |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 280 | ret = lua_rawequal(L, -1, -2); |
| 281 | lua_pop(L, 2); |
| 282 | if (!ret) |
| 283 | luaL_argerror(L, ud, NULL); |
| 284 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 285 | /* Push on the stack at the entry [0] of the table. */ |
| 286 | lua_rawgeti(L, ud, 0); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 287 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 288 | /* Check if this entry is userdata. */ |
| 289 | p = lua_touserdata(L, -1); |
| 290 | if (!p) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 291 | luaL_argerror(L, ud, NULL); |
| 292 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 293 | /* Remove the entry returned by lua_rawgeti(). */ |
| 294 | lua_pop(L, 1); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 295 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 296 | /* Return the associated struct. */ |
| 297 | return p; |
| 298 | } |
| 299 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 300 | /* This function return the current date at epoch format in milliseconds. */ |
| 301 | int hlua_now(lua_State *L) |
| 302 | { |
Willy Tarreau | d2f61de | 2023-04-27 18:44:14 +0200 | [diff] [blame] | 303 | /* WT: the doc says "returns the current time" and later says that it's |
| 304 | * monotonic. So the best fit is to use start_date+(now-start_time). |
| 305 | */ |
| 306 | struct timeval tv; |
| 307 | |
Willy Tarreau | c05d30e | 2023-04-28 14:50:29 +0200 | [diff] [blame] | 308 | tv = NS_TO_TV(now_ns - start_time_ns); |
Willy Tarreau | d2f61de | 2023-04-27 18:44:14 +0200 | [diff] [blame] | 309 | tv_add(&tv, &tv, &start_date); |
| 310 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 311 | lua_newtable(L); |
| 312 | lua_pushstring(L, "sec"); |
Willy Tarreau | d2f61de | 2023-04-27 18:44:14 +0200 | [diff] [blame] | 313 | lua_pushinteger(L, tv.tv_sec); |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 314 | lua_rawset(L, -3); |
| 315 | lua_pushstring(L, "usec"); |
Willy Tarreau | d2f61de | 2023-04-27 18:44:14 +0200 | [diff] [blame] | 316 | lua_pushinteger(L, tv.tv_usec); |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 317 | lua_rawset(L, -3); |
| 318 | return 1; |
| 319 | } |
| 320 | |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 321 | /* This functions expects a Lua string as HTTP date, parse it and |
| 322 | * returns an integer containing the epoch format of the date, or |
| 323 | * nil if the parsing fails. |
| 324 | */ |
| 325 | static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*)) |
| 326 | { |
| 327 | const char *str; |
| 328 | size_t len; |
| 329 | struct tm tm; |
| 330 | time_t time; |
| 331 | |
| 332 | str = luaL_checklstring(L, 1, &len); |
| 333 | |
| 334 | if (!fcn(str, len, &tm)) { |
| 335 | lua_pushnil(L); |
| 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | /* This function considers the content of the broken-down time |
| 340 | * is exprimed in the UTC timezone. timegm don't care about |
| 341 | * the gnu variable tm_gmtoff. If gmtoff is set, or if you know |
| 342 | * the timezone from the broken-down time, it must be fixed |
| 343 | * after the conversion. |
| 344 | */ |
Willy Tarreau | abd9bb2 | 2017-07-19 19:08:48 +0200 | [diff] [blame] | 345 | time = my_timegm(&tm); |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 346 | if (time == -1) { |
| 347 | lua_pushnil(L); |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | lua_pushinteger(L, (int)time); |
| 352 | return 1; |
| 353 | } |
| 354 | static int hlua_http_date(lua_State *L) |
| 355 | { |
| 356 | return hlua_parse_date(L, parse_http_date); |
| 357 | } |
| 358 | static int hlua_imf_date(lua_State *L) |
| 359 | { |
| 360 | return hlua_parse_date(L, parse_imf_date); |
| 361 | } |
| 362 | static int hlua_rfc850_date(lua_State *L) |
| 363 | { |
| 364 | return hlua_parse_date(L, parse_rfc850_date); |
| 365 | } |
| 366 | static int hlua_asctime_date(lua_State *L) |
| 367 | { |
| 368 | return hlua_parse_date(L, parse_asctime_date); |
| 369 | } |
| 370 | |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 371 | static int hlua_get_info(lua_State *L) |
| 372 | { |
| 373 | int i; |
| 374 | |
Willy Tarreau | 0b26b38 | 2021-05-08 07:43:53 +0200 | [diff] [blame] | 375 | stats_fill_info(stats, STATS_LEN, 0); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 376 | |
| 377 | lua_newtable(L); |
| 378 | for (i=0; i<INF_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 379 | lua_pushstring(L, info_fields[i].name); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 380 | hlua_fcn_pushfield(L, &stats[i]); |
| 381 | lua_settable(L, -3); |
| 382 | } |
| 383 | return 1; |
| 384 | } |
| 385 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 386 | static struct hlua_concat *hlua_check_concat(lua_State *L, int ud) |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 387 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 388 | return (hlua_checkudata(L, ud, class_concat_ref)); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static int hlua_concat_add(lua_State *L) |
| 392 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 393 | struct hlua_concat *b; |
| 394 | char *buffer; |
| 395 | char *new; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 396 | const char *str; |
| 397 | size_t l; |
| 398 | |
| 399 | /* First arg must be a concat object. */ |
| 400 | b = hlua_check_concat(L, 1); |
| 401 | |
| 402 | /* Second arg must be a string. */ |
| 403 | str = luaL_checklstring(L, 2, &l); |
| 404 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 405 | /* Get the buffer. */ |
| 406 | lua_rawgeti(L, 1, 1); |
| 407 | buffer = lua_touserdata(L, -1); |
| 408 | lua_pop(L, 1); |
| 409 | |
| 410 | /* Update the buffer size if it s required. The old buffer |
| 411 | * is crushed by the new in the object array, so it will |
| 412 | * be deleted by the GC. |
| 413 | * Note that in the first loop, the "new" variable is only |
| 414 | * used as a flag. |
| 415 | */ |
| 416 | new = NULL; |
| 417 | while (b->size - b->len < l) { |
| 418 | b->size += HLUA_CONCAT_BLOCSZ; |
| 419 | new = buffer; |
| 420 | } |
| 421 | if (new) { |
| 422 | new = lua_newuserdata(L, b->size); |
| 423 | memcpy(new, buffer, b->len); |
| 424 | lua_rawseti(L, 1, 1); |
| 425 | buffer = new; |
| 426 | } |
| 427 | |
| 428 | /* Copy string, and update metadata. */ |
| 429 | memcpy(buffer + b->len, str, l); |
| 430 | b->len += l; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static int hlua_concat_dump(lua_State *L) |
| 435 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 436 | struct hlua_concat *b; |
| 437 | char *buffer; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 438 | |
| 439 | /* First arg must be a concat object. */ |
| 440 | b = hlua_check_concat(L, 1); |
| 441 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 442 | /* Get the buffer. */ |
| 443 | lua_rawgeti(L, 1, 1); |
| 444 | buffer = lua_touserdata(L, -1); |
| 445 | lua_pop(L, 1); |
| 446 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 447 | /* Push the soncatenated string in the stack. */ |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 448 | lua_pushlstring(L, buffer, b->len); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 449 | return 1; |
| 450 | } |
| 451 | |
| 452 | int hlua_concat_new(lua_State *L) |
| 453 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 454 | struct hlua_concat *b; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 455 | |
| 456 | lua_newtable(L); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 457 | b = lua_newuserdata(L, sizeof(*b)); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 458 | b->size = HLUA_CONCAT_BLOCSZ; |
| 459 | b->len = 0; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 460 | lua_rawseti(L, -2, 0); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 461 | lua_newuserdata(L, HLUA_CONCAT_BLOCSZ); |
| 462 | lua_rawseti(L, -2, 1); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 463 | |
| 464 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref); |
| 465 | lua_setmetatable(L, -2); |
| 466 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | static int concat_tostring(lua_State *L) |
| 471 | { |
| 472 | const void *ptr = lua_topointer(L, 1); |
| 473 | lua_pushfstring(L, "Concat object: %p", ptr); |
| 474 | return 1; |
| 475 | } |
| 476 | |
Thierry Fournier | 599f231 | 2022-09-30 10:40:39 +0200 | [diff] [blame] | 477 | static void hlua_concat_init(lua_State *L) |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 478 | { |
| 479 | /* Creates the buffered concat object. */ |
| 480 | lua_newtable(L); |
| 481 | |
| 482 | lua_pushstring(L, "__tostring"); |
| 483 | lua_pushcclosure(L, concat_tostring, 0); |
| 484 | lua_settable(L, -3); |
| 485 | |
| 486 | lua_pushstring(L, "__index"); /* Creates the index entry. */ |
| 487 | lua_newtable(L); /* The "__index" content. */ |
| 488 | |
| 489 | lua_pushstring(L, "add"); |
| 490 | lua_pushcclosure(L, hlua_concat_add, 0); |
| 491 | lua_settable(L, -3); |
| 492 | |
| 493 | lua_pushstring(L, "dump"); |
| 494 | lua_pushcclosure(L, hlua_concat_dump, 0); |
| 495 | lua_settable(L, -3); |
| 496 | |
| 497 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 498 | class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 501 | int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl) |
| 502 | { |
| 503 | lua_newtable(L); |
| 504 | |
| 505 | /* Pop a class stktbl metatable and affect it to the userdata. */ |
| 506 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref); |
| 507 | lua_setmetatable(L, -2); |
| 508 | |
| 509 | lua_pushlightuserdata(L, tbl); |
| 510 | lua_rawseti(L, -2, 0); |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | static struct stktable *hlua_check_stktable(lua_State *L, int ud) |
| 515 | { |
| 516 | return hlua_checkudata(L, ud, class_stktable_ref); |
| 517 | } |
| 518 | |
| 519 | /* Extract stick table attributes into Lua table */ |
| 520 | int hlua_stktable_info(lua_State *L) |
| 521 | { |
| 522 | struct stktable *tbl; |
| 523 | int dt; |
| 524 | |
| 525 | tbl = hlua_check_stktable(L, 1); |
| 526 | |
| 527 | if (!tbl->id) { |
| 528 | lua_pushnil(L); |
| 529 | return 1; |
| 530 | } |
| 531 | |
| 532 | lua_newtable(L); |
| 533 | |
| 534 | lua_pushstring(L, "type"); |
| 535 | lua_pushstring(L, stktable_types[tbl->type].kw); |
| 536 | lua_settable(L, -3); |
| 537 | |
| 538 | lua_pushstring(L, "length"); |
| 539 | lua_pushinteger(L, tbl->key_size); |
| 540 | lua_settable(L, -3); |
| 541 | |
| 542 | lua_pushstring(L, "size"); |
| 543 | hlua_fcn_pushunsigned(L, tbl->size); |
| 544 | lua_settable(L, -3); |
| 545 | |
| 546 | lua_pushstring(L, "used"); |
| 547 | hlua_fcn_pushunsigned(L, tbl->current); |
| 548 | lua_settable(L, -3); |
| 549 | |
| 550 | lua_pushstring(L, "nopurge"); |
| 551 | lua_pushboolean(L, tbl->nopurge > 0); |
| 552 | lua_settable(L, -3); |
| 553 | |
| 554 | lua_pushstring(L, "expire"); |
| 555 | lua_pushinteger(L, tbl->expire); |
| 556 | lua_settable(L, -3); |
| 557 | |
| 558 | /* Save data types periods (if applicable) in 'data' table */ |
| 559 | lua_pushstring(L, "data"); |
| 560 | lua_newtable(L); |
| 561 | |
| 562 | for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) { |
| 563 | if (tbl->data_ofs[dt] == 0) |
| 564 | continue; |
| 565 | |
| 566 | lua_pushstring(L, stktable_data_types[dt].name); |
| 567 | |
| 568 | if (stktable_data_types[dt].arg_type == ARG_T_DELAY) |
| 569 | lua_pushinteger(L, tbl->data_arg[dt].u); |
| 570 | else |
| 571 | lua_pushinteger(L, -1); |
| 572 | |
| 573 | lua_settable(L, -3); |
| 574 | } |
| 575 | |
| 576 | lua_settable(L, -3); |
| 577 | |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | /* Helper to get extract stick table entry into Lua table */ |
| 582 | static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct stksess *ts) |
| 583 | { |
| 584 | int dt; |
| 585 | void *ptr; |
| 586 | |
| 587 | for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) { |
| 588 | |
| 589 | if (t->data_ofs[dt] == 0) |
| 590 | continue; |
| 591 | |
| 592 | lua_pushstring(L, stktable_data_types[dt].name); |
| 593 | |
| 594 | ptr = stktable_data_ptr(t, ts, dt); |
| 595 | switch (stktable_data_types[dt].std_type) { |
| 596 | case STD_T_SINT: |
| 597 | lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint)); |
| 598 | break; |
| 599 | case STD_T_UINT: |
| 600 | hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, std_t_uint)); |
| 601 | break; |
| 602 | case STD_T_ULL: |
| 603 | hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, std_t_ull)); |
| 604 | break; |
| 605 | case STD_T_FRQP: |
| 606 | lua_pushinteger(L, read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 607 | t->data_arg[dt].u)); |
| 608 | break; |
Adis Nezirovic | ad9f9ed | 2020-05-05 13:57:28 +0200 | [diff] [blame] | 609 | case STD_T_DICT: { |
| 610 | struct dict_entry *de; |
| 611 | de = stktable_data_cast(ptr, std_t_dict); |
| 612 | lua_pushstring(L, de ? (char *)de->value.key : "-"); |
| 613 | break; |
| 614 | } |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | lua_settable(L, -3); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /* Looks in table <t> for a sticky session matching key <key> |
| 622 | * Returns table with session data or nil |
| 623 | * |
| 624 | * The returned table always contains 'use' and 'expire' (integer) fields. |
| 625 | * For frequency/rate counters, each data entry is returned as table with |
| 626 | * 'value' and 'period' fields. |
| 627 | */ |
| 628 | int hlua_stktable_lookup(lua_State *L) |
| 629 | { |
| 630 | struct stktable *t; |
| 631 | struct sample smp; |
| 632 | struct stktable_key *skey; |
| 633 | struct stksess *ts; |
| 634 | |
| 635 | t = hlua_check_stktable(L, 1); |
| 636 | smp.data.type = SMP_T_STR; |
| 637 | smp.flags = SMP_F_CONST; |
Nathan Neulinger | 31a841c | 2020-03-03 20:32:47 -0600 | [diff] [blame] | 638 | 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] | 639 | |
| 640 | skey = smp_to_stkey(&smp, t); |
| 641 | if (!skey) { |
| 642 | lua_pushnil(L); |
| 643 | return 1; |
| 644 | } |
| 645 | |
| 646 | ts = stktable_lookup_key(t, skey); |
| 647 | if (!ts) { |
| 648 | lua_pushnil(L); |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | lua_newtable(L); |
| 653 | lua_pushstring(L, "use"); |
| 654 | lua_pushinteger(L, ts->ref_cnt - 1); |
| 655 | lua_settable(L, -3); |
| 656 | |
| 657 | lua_pushstring(L, "expire"); |
| 658 | lua_pushinteger(L, tick_remain(now_ms, ts->expire)); |
| 659 | lua_settable(L, -3); |
| 660 | |
| 661 | hlua_stktable_entry(L, t, ts); |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 662 | HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 663 | ts->ref_cnt--; |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 664 | HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 665 | |
| 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | struct stk_filter { |
| 670 | long long val; |
| 671 | int type; |
| 672 | int op; |
| 673 | }; |
| 674 | |
| 675 | |
| 676 | /* Helper for returning errors to callers using Lua convention (nil, err) */ |
| 677 | static int hlua_error(lua_State *L, const char *fmt, ...) { |
| 678 | char buf[256]; |
| 679 | int len; |
| 680 | va_list args; |
| 681 | va_start(args, fmt); |
| 682 | len = vsnprintf(buf, sizeof(buf), fmt, args); |
| 683 | va_end(args); |
| 684 | |
| 685 | if (len < 0) { |
| 686 | ha_alert("hlua_error(): Could not write error message.\n"); |
| 687 | lua_pushnil(L); |
| 688 | return 1; |
| 689 | } else if (len >= sizeof(buf)) |
| 690 | ha_alert("hlua_error(): Error message was truncated.\n"); |
| 691 | |
| 692 | lua_pushnil(L); |
| 693 | lua_pushstring(L, buf); |
| 694 | |
| 695 | return 2; |
| 696 | } |
| 697 | |
| 698 | /* Dump the contents of stick table <t>*/ |
| 699 | int hlua_stktable_dump(lua_State *L) |
| 700 | { |
| 701 | struct stktable *t; |
| 702 | struct ebmb_node *eb; |
| 703 | struct ebmb_node *n; |
| 704 | struct stksess *ts; |
| 705 | int type; |
| 706 | int op; |
| 707 | int dt; |
| 708 | long long val; |
Adis Nezirovic | 1a693fc | 2020-01-16 15:19:29 +0100 | [diff] [blame] | 709 | struct stk_filter filter[STKTABLE_FILTER_LEN]; |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 710 | int filter_count = 0; |
| 711 | int i; |
| 712 | int skip_entry; |
| 713 | void *ptr; |
| 714 | |
| 715 | t = hlua_check_stktable(L, 1); |
| 716 | type = lua_type(L, 2); |
| 717 | |
| 718 | switch (type) { |
| 719 | case LUA_TNONE: |
| 720 | case LUA_TNIL: |
| 721 | break; |
| 722 | case LUA_TTABLE: |
| 723 | lua_pushnil(L); |
| 724 | while (lua_next(L, 2) != 0) { |
| 725 | int entry_idx = 0; |
| 726 | |
Adis Nezirovic | 1a693fc | 2020-01-16 15:19:29 +0100 | [diff] [blame] | 727 | if (filter_count >= STKTABLE_FILTER_LEN) |
| 728 | 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] | 729 | |
| 730 | if (lua_type(L, -1) != LUA_TTABLE || lua_rawlen(L, -1) != 3) |
| 731 | return hlua_error(L, "Filter table entry must be a triplet: {\"data_col\", \"op\", val} (entry #%d)", filter_count + 1); |
| 732 | |
| 733 | lua_pushnil(L); |
| 734 | while (lua_next(L, -2) != 0) { |
| 735 | switch (entry_idx) { |
| 736 | case 0: |
| 737 | if (lua_type(L, -1) != LUA_TSTRING) |
| 738 | return hlua_error(L, "Filter table data column must be string (entry #%d)", filter_count + 1); |
| 739 | |
| 740 | dt = stktable_get_data_type((char *)lua_tostring(L, -1)); |
| 741 | if (dt < 0 || t->data_ofs[dt] == 0) |
| 742 | return hlua_error(L, "Filter table data column not present in stick table (entry #%d)", filter_count + 1); |
| 743 | filter[filter_count].type = dt; |
| 744 | break; |
| 745 | case 1: |
| 746 | if (lua_type(L, -1) != LUA_TSTRING) |
| 747 | return hlua_error(L, "Filter table operator must be string (entry #%d)", filter_count + 1); |
| 748 | |
| 749 | op = get_std_op(lua_tostring(L, -1)); |
| 750 | if (op < 0) |
| 751 | return hlua_error(L, "Unknown operator in filter table (entry #%d)", filter_count + 1); |
| 752 | filter[filter_count].op = op; |
| 753 | break; |
| 754 | case 2: |
| 755 | val = lua_tointeger(L, -1); |
| 756 | filter[filter_count].val = val; |
| 757 | filter_count++; |
| 758 | break; |
| 759 | default: |
| 760 | break; |
| 761 | } |
| 762 | entry_idx++; |
| 763 | lua_pop(L, 1); |
| 764 | } |
| 765 | lua_pop(L, 1); |
| 766 | } |
| 767 | break; |
| 768 | default: |
| 769 | return hlua_error(L, "filter table expected"); |
| 770 | } |
| 771 | |
| 772 | lua_newtable(L); |
| 773 | |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 774 | HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 775 | eb = ebmb_first(&t->keys); |
| 776 | for (n = eb; n; n = ebmb_next(n)) { |
| 777 | ts = ebmb_entry(n, struct stksess, key); |
| 778 | if (!ts) { |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 779 | HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 780 | return 1; |
| 781 | } |
| 782 | ts->ref_cnt++; |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 783 | HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 784 | |
| 785 | /* multi condition/value filter */ |
| 786 | skip_entry = 0; |
| 787 | for (i = 0; i < filter_count; i++) { |
| 788 | if (t->data_ofs[filter[i].type] == 0) |
| 789 | continue; |
| 790 | |
| 791 | ptr = stktable_data_ptr(t, ts, filter[i].type); |
| 792 | |
| 793 | switch (stktable_data_types[filter[i].type].std_type) { |
| 794 | case STD_T_SINT: |
| 795 | val = stktable_data_cast(ptr, std_t_sint); |
| 796 | break; |
| 797 | case STD_T_UINT: |
| 798 | val = stktable_data_cast(ptr, std_t_uint); |
| 799 | break; |
| 800 | case STD_T_ULL: |
| 801 | val = stktable_data_cast(ptr, std_t_ull); |
| 802 | break; |
| 803 | case STD_T_FRQP: |
| 804 | val = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 805 | t->data_arg[filter[i].type].u); |
| 806 | break; |
| 807 | default: |
| 808 | continue; |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | op = filter[i].op; |
| 813 | |
| 814 | if ((val < filter[i].val && (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) || |
| 815 | (val == filter[i].val && (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) || |
| 816 | (val > filter[i].val && (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) { |
| 817 | skip_entry = 1; |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if (skip_entry) { |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 823 | HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 824 | ts->ref_cnt--; |
| 825 | continue; |
| 826 | } |
| 827 | |
| 828 | if (t->type == SMP_T_IPV4) { |
| 829 | char addr[INET_ADDRSTRLEN]; |
| 830 | inet_ntop(AF_INET, (const void *)&ts->key.key, addr, sizeof(addr)); |
| 831 | lua_pushstring(L, addr); |
| 832 | } else if (t->type == SMP_T_IPV6) { |
| 833 | char addr[INET6_ADDRSTRLEN]; |
| 834 | inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, sizeof(addr)); |
| 835 | lua_pushstring(L, addr); |
| 836 | } else if (t->type == SMP_T_SINT) { |
| 837 | lua_pushinteger(L, *ts->key.key); |
| 838 | } else if (t->type == SMP_T_STR) { |
| 839 | lua_pushstring(L, (const char *)ts->key.key); |
| 840 | } else { |
| 841 | return hlua_error(L, "Unsupported stick table key type"); |
| 842 | } |
| 843 | |
| 844 | lua_newtable(L); |
| 845 | hlua_stktable_entry(L, t, ts); |
| 846 | lua_settable(L, -3); |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 847 | HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 848 | ts->ref_cnt--; |
| 849 | } |
Willy Tarreau | 7664222 | 2022-10-11 12:02:50 +0200 | [diff] [blame] | 850 | HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock); |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 851 | |
| 852 | return 1; |
| 853 | } |
| 854 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 855 | int hlua_fcn_new_listener(lua_State *L, struct listener *lst) |
| 856 | { |
| 857 | lua_newtable(L); |
| 858 | |
| 859 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 860 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref); |
| 861 | lua_setmetatable(L, -2); |
| 862 | |
| 863 | lua_pushlightuserdata(L, lst); |
| 864 | lua_rawseti(L, -2, 0); |
| 865 | return 1; |
| 866 | } |
| 867 | |
| 868 | static struct listener *hlua_check_listener(lua_State *L, int ud) |
| 869 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 870 | return hlua_checkudata(L, ud, class_listener_ref); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | int hlua_listener_get_stats(lua_State *L) |
| 874 | { |
| 875 | struct listener *li; |
| 876 | int i; |
| 877 | |
| 878 | li = hlua_check_listener(L, 1); |
| 879 | |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 880 | if (!li->bind_conf->frontend) { |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 881 | lua_pushnil(L); |
| 882 | return 1; |
| 883 | } |
| 884 | |
William Dauchy | 655e14e | 2021-02-14 23:22:54 +0100 | [diff] [blame] | 885 | stats_fill_li_stats(li->bind_conf->frontend, li, STAT_SHLGNDS, stats, |
| 886 | STATS_LEN, NULL); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 887 | |
| 888 | lua_newtable(L); |
| 889 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 890 | lua_pushstring(L, stat_fields[i].name); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 891 | hlua_fcn_pushfield(L, &stats[i]); |
| 892 | lua_settable(L, -3); |
| 893 | } |
| 894 | return 1; |
| 895 | |
| 896 | } |
| 897 | |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 898 | int hlua_server_gc(lua_State *L) |
| 899 | { |
| 900 | struct server *srv = hlua_checkudata(L, 1, class_server_ref); |
| 901 | |
| 902 | srv_drop(srv); /* srv_drop allows NULL srv */ |
| 903 | return 0; |
| 904 | } |
| 905 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 906 | static struct server *hlua_check_server(lua_State *L, int ud) |
| 907 | { |
Amaury Denoyelle | 86f3707 | 2021-08-23 14:06:31 +0200 | [diff] [blame] | 908 | struct server *srv = hlua_checkudata(L, ud, class_server_ref); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 909 | if (srv->flags & SRV_F_DELETED) { |
| 910 | return NULL; |
| 911 | } |
Amaury Denoyelle | 86f3707 | 2021-08-23 14:06:31 +0200 | [diff] [blame] | 912 | return srv; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | int hlua_server_get_stats(lua_State *L) |
| 916 | { |
| 917 | struct server *srv; |
| 918 | int i; |
| 919 | |
| 920 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 921 | if (srv == NULL) { |
| 922 | lua_pushnil(L); |
| 923 | return 1; |
| 924 | } |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 925 | |
| 926 | if (!srv->proxy) { |
| 927 | lua_pushnil(L); |
| 928 | return 1; |
| 929 | } |
| 930 | |
William Dauchy | d3a9a49 | 2021-01-25 17:29:03 +0100 | [diff] [blame] | 931 | stats_fill_sv_stats(srv->proxy, srv, STAT_SHLGNDS, stats, |
| 932 | STATS_LEN, NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 933 | |
| 934 | lua_newtable(L); |
| 935 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 936 | lua_pushstring(L, stat_fields[i].name); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 937 | hlua_fcn_pushfield(L, &stats[i]); |
| 938 | lua_settable(L, -3); |
| 939 | } |
| 940 | return 1; |
| 941 | |
| 942 | } |
| 943 | |
| 944 | int hlua_server_get_addr(lua_State *L) |
| 945 | { |
| 946 | struct server *srv; |
| 947 | char addr[INET6_ADDRSTRLEN]; |
| 948 | luaL_Buffer b; |
| 949 | |
| 950 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 951 | if (srv == NULL) { |
| 952 | lua_pushnil(L); |
| 953 | return 1; |
| 954 | } |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 955 | |
| 956 | luaL_buffinit(L, &b); |
| 957 | |
| 958 | switch (srv->addr.ss_family) { |
| 959 | case AF_INET: |
| 960 | inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr, |
| 961 | addr, INET_ADDRSTRLEN); |
| 962 | luaL_addstring(&b, addr); |
| 963 | luaL_addstring(&b, ":"); |
Nenad Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 964 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 965 | luaL_addstring(&b, addr); |
| 966 | break; |
| 967 | case AF_INET6: |
| 968 | inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr, |
Nenad Merdanovic | a9f0404 | 2017-07-23 22:04:59 -0400 | [diff] [blame] | 969 | addr, INET6_ADDRSTRLEN); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 970 | luaL_addstring(&b, addr); |
| 971 | luaL_addstring(&b, ":"); |
Nenad Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 972 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 973 | luaL_addstring(&b, addr); |
| 974 | break; |
| 975 | case AF_UNIX: |
| 976 | luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path); |
| 977 | break; |
| 978 | default: |
| 979 | luaL_addstring(&b, "<unknown>"); |
| 980 | break; |
| 981 | } |
| 982 | |
| 983 | luaL_pushresult(&b); |
| 984 | return 1; |
| 985 | } |
| 986 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 987 | int hlua_server_get_puid(lua_State *L) |
| 988 | { |
| 989 | struct server *srv; |
| 990 | char buffer[12]; |
| 991 | |
| 992 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 993 | if (srv == NULL) { |
| 994 | lua_pushnil(L); |
| 995 | return 1; |
| 996 | } |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 997 | |
| 998 | snprintf(buffer, sizeof(buffer), "%d", srv->puid); |
| 999 | lua_pushstring(L, buffer); |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
Aurelien DARRAGON | 94ee663 | 2023-03-10 15:11:27 +0100 | [diff] [blame] | 1003 | int hlua_server_get_rid(lua_State *L) |
| 1004 | { |
| 1005 | struct server *srv; |
| 1006 | char buffer[12]; |
| 1007 | |
| 1008 | srv = hlua_check_server(L, 1); |
| 1009 | if (srv == NULL) { |
| 1010 | lua_pushnil(L); |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
| 1014 | snprintf(buffer, sizeof(buffer), "%d", srv->rid); |
| 1015 | lua_pushstring(L, buffer); |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1019 | int hlua_server_get_name(lua_State *L) |
| 1020 | { |
| 1021 | struct server *srv; |
| 1022 | |
| 1023 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1024 | if (srv == NULL) { |
| 1025 | lua_pushnil(L); |
| 1026 | return 1; |
| 1027 | } |
| 1028 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1029 | lua_pushstring(L, srv->id); |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1033 | /* __index metamethod for server class |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 1034 | * support for additional keys that are missing from the main table |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1035 | * stack:1 = table (server class), stack:2 = requested key |
| 1036 | * Returns 1 if key is supported |
| 1037 | * else returns 0 to make lua return NIL value to the caller |
| 1038 | */ |
| 1039 | static int hlua_server_index(struct lua_State *L) |
| 1040 | { |
| 1041 | const char *key = lua_tostring(L, 2); |
| 1042 | |
| 1043 | if (!strcmp(key, "name")) { |
| 1044 | if (ONLY_ONCE()) |
| 1045 | ha_warning("hlua: use of server 'name' attribute is deprecated and will eventually be removed, please use get_name() function instead: %s\n", hlua_traceback(L, ", ")); |
| 1046 | lua_pushvalue(L, 1); |
| 1047 | hlua_server_get_name(L); |
| 1048 | return 1; |
| 1049 | } |
| 1050 | if (!strcmp(key, "puid")) { |
| 1051 | if (ONLY_ONCE()) |
| 1052 | ha_warning("hlua: use of server 'puid' attribute is deprecated and will eventually be removed, please use get_puid() function instead: %s\n", hlua_traceback(L, ", ")); |
| 1053 | lua_pushvalue(L, 1); |
| 1054 | hlua_server_get_puid(L); |
| 1055 | return 1; |
| 1056 | } |
| 1057 | /* unknown attribute */ |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1061 | int hlua_server_is_draining(lua_State *L) |
| 1062 | { |
| 1063 | struct server *srv; |
| 1064 | |
| 1065 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1066 | if (srv == NULL) { |
| 1067 | lua_pushnil(L); |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
Aurelien DARRAGON | 862a0fe | 2023-03-29 10:46:36 +0200 | [diff] [blame] | 1071 | lua_pushboolean(L, server_is_draining(srv)); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1072 | return 1; |
| 1073 | } |
| 1074 | |
Aurelien DARRAGON | c72051d | 2023-03-29 10:44:38 +0200 | [diff] [blame] | 1075 | int hlua_server_is_backup(lua_State *L) |
| 1076 | { |
| 1077 | struct server *srv; |
| 1078 | |
| 1079 | srv = hlua_check_server(L, 1); |
| 1080 | if (srv == NULL) { |
| 1081 | lua_pushnil(L); |
| 1082 | return 1; |
| 1083 | } |
| 1084 | |
| 1085 | lua_pushboolean(L, (srv->flags & SRV_F_BACKUP)); |
| 1086 | return 1; |
| 1087 | } |
| 1088 | |
Aurelien DARRAGON | 7a03dee | 2023-03-29 10:49:30 +0200 | [diff] [blame] | 1089 | int hlua_server_is_dynamic(lua_State *L) |
| 1090 | { |
| 1091 | struct server *srv; |
| 1092 | |
| 1093 | srv = hlua_check_server(L, 1); |
| 1094 | if (srv == NULL) { |
| 1095 | lua_pushnil(L); |
| 1096 | return 1; |
| 1097 | } |
| 1098 | |
| 1099 | lua_pushboolean(L, (srv->flags & SRV_F_DYNAMIC)); |
| 1100 | return 1; |
| 1101 | } |
| 1102 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1103 | int hlua_server_set_maxconn(lua_State *L) |
| 1104 | { |
| 1105 | struct server *srv; |
| 1106 | const char *maxconn; |
| 1107 | const char *err; |
| 1108 | |
| 1109 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1110 | if (srv == NULL) { |
| 1111 | lua_pushnil(L); |
| 1112 | return 1; |
| 1113 | } |
| 1114 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1115 | maxconn = luaL_checkstring(L, 2); |
| 1116 | |
| 1117 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 1118 | err = server_parse_maxconn_change_request(srv, maxconn); |
| 1119 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 1120 | if (!err) |
| 1121 | lua_pushnil(L); |
| 1122 | else |
| 1123 | hlua_pushstrippedstring(L, err); |
| 1124 | return 1; |
| 1125 | } |
| 1126 | |
| 1127 | int hlua_server_get_maxconn(lua_State *L) |
| 1128 | { |
| 1129 | struct server *srv; |
| 1130 | |
| 1131 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1132 | if (srv == NULL) { |
| 1133 | lua_pushnil(L); |
| 1134 | return 1; |
| 1135 | } |
| 1136 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1137 | lua_pushinteger(L, srv->maxconn); |
| 1138 | return 1; |
| 1139 | } |
| 1140 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1141 | int hlua_server_set_weight(lua_State *L) |
| 1142 | { |
| 1143 | struct server *srv; |
| 1144 | const char *weight; |
| 1145 | const char *err; |
| 1146 | |
| 1147 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1148 | if (srv == NULL) { |
| 1149 | lua_pushnil(L); |
| 1150 | return 1; |
| 1151 | } |
| 1152 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1153 | weight = luaL_checkstring(L, 2); |
| 1154 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1155 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1156 | err = server_parse_weight_change_request(srv, weight); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1157 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1158 | if (!err) |
| 1159 | lua_pushnil(L); |
| 1160 | else |
| 1161 | hlua_pushstrippedstring(L, err); |
| 1162 | return 1; |
| 1163 | } |
| 1164 | |
| 1165 | int hlua_server_get_weight(lua_State *L) |
| 1166 | { |
| 1167 | struct server *srv; |
| 1168 | |
| 1169 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1170 | if (srv == NULL) { |
| 1171 | lua_pushnil(L); |
| 1172 | return 1; |
| 1173 | } |
| 1174 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1175 | lua_pushinteger(L, srv->uweight); |
| 1176 | return 1; |
| 1177 | } |
| 1178 | |
| 1179 | int hlua_server_set_addr(lua_State *L) |
| 1180 | { |
| 1181 | struct server *srv; |
| 1182 | const char *addr; |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 1183 | const char *port; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1184 | const char *err; |
| 1185 | |
| 1186 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1187 | if (srv == NULL) { |
| 1188 | lua_pushnil(L); |
| 1189 | return 1; |
| 1190 | } |
| 1191 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1192 | addr = luaL_checkstring(L, 2); |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 1193 | if (lua_gettop(L) >= 3) |
| 1194 | port = luaL_checkstring(L, 3); |
| 1195 | else |
| 1196 | port = NULL; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1197 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1198 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 1199 | err = srv_update_addr_port(srv, addr, port, "Lua script"); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1200 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1201 | if (!err) |
| 1202 | lua_pushnil(L); |
| 1203 | else |
| 1204 | hlua_pushstrippedstring(L, err); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | |
| 1208 | int hlua_server_shut_sess(lua_State *L) |
| 1209 | { |
| 1210 | struct server *srv; |
| 1211 | |
| 1212 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1213 | if (srv == NULL) { |
| 1214 | return 0; |
| 1215 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1216 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1217 | srv_shutdown_streams(srv, SF_ERR_KILLED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1218 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | int hlua_server_set_drain(lua_State *L) |
| 1223 | { |
| 1224 | struct server *srv; |
| 1225 | |
| 1226 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1227 | if (srv == NULL) { |
| 1228 | return 0; |
| 1229 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1230 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1231 | srv_adm_set_drain(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1232 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | int hlua_server_set_maint(lua_State *L) |
| 1237 | { |
| 1238 | struct server *srv; |
| 1239 | |
| 1240 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1241 | if (srv == NULL) { |
| 1242 | return 0; |
| 1243 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1244 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1245 | srv_adm_set_maint(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1246 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | int hlua_server_set_ready(lua_State *L) |
| 1251 | { |
| 1252 | struct server *srv; |
| 1253 | |
| 1254 | srv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1255 | if (srv == NULL) { |
| 1256 | return 0; |
| 1257 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1258 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1259 | srv_adm_set_ready(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1260 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | int hlua_server_check_enable(lua_State *L) |
| 1265 | { |
| 1266 | struct server *sv; |
| 1267 | |
| 1268 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1269 | if (sv == NULL) { |
| 1270 | return 0; |
| 1271 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1272 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1273 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 1274 | sv->check.state |= CHK_ST_ENABLED; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1275 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1276 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | int hlua_server_check_disable(lua_State *L) |
| 1281 | { |
| 1282 | struct server *sv; |
| 1283 | |
| 1284 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1285 | if (sv == NULL) { |
| 1286 | return 0; |
| 1287 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1288 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1289 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 1290 | sv->check.state &= ~CHK_ST_ENABLED; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1291 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1292 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | int hlua_server_check_force_up(lua_State *L) |
| 1297 | { |
| 1298 | struct server *sv; |
| 1299 | |
| 1300 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1301 | if (sv == NULL) { |
| 1302 | return 0; |
| 1303 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1304 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1305 | if (!(sv->track)) { |
| 1306 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1307 | srv_set_running(sv, SRV_OP_STCHGC_LUA); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1308 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1309 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | int hlua_server_check_force_nolb(lua_State *L) |
| 1314 | { |
| 1315 | struct server *sv; |
| 1316 | |
| 1317 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1318 | if (sv == NULL) { |
| 1319 | return 0; |
| 1320 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1321 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1322 | if (!(sv->track)) { |
| 1323 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1324 | srv_set_stopping(sv, SRV_OP_STCHGC_LUA); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1325 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1326 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | int hlua_server_check_force_down(lua_State *L) |
| 1331 | { |
| 1332 | struct server *sv; |
| 1333 | |
| 1334 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1335 | if (sv == NULL) { |
| 1336 | return 0; |
| 1337 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1338 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1339 | if (!(sv->track)) { |
| 1340 | sv->check.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1341 | srv_set_stopped(sv, SRV_OP_STCHGC_LUA); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1342 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1343 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | int hlua_server_agent_enable(lua_State *L) |
| 1348 | { |
| 1349 | struct server *sv; |
| 1350 | |
| 1351 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1352 | if (sv == NULL) { |
| 1353 | return 0; |
| 1354 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1355 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1356 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 1357 | sv->agent.state |= CHK_ST_ENABLED; |
| 1358 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1359 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | int hlua_server_agent_disable(lua_State *L) |
| 1364 | { |
| 1365 | struct server *sv; |
| 1366 | |
| 1367 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1368 | if (sv == NULL) { |
| 1369 | return 0; |
| 1370 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1371 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1372 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 1373 | sv->agent.state &= ~CHK_ST_ENABLED; |
| 1374 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1375 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | int hlua_server_agent_force_up(lua_State *L) |
| 1380 | { |
| 1381 | struct server *sv; |
| 1382 | |
| 1383 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1384 | if (sv == NULL) { |
| 1385 | return 0; |
| 1386 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1387 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1388 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 1389 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1390 | srv_set_running(sv, SRV_OP_STCHGC_LUA); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1391 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1392 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | int hlua_server_agent_force_down(lua_State *L) |
| 1397 | { |
| 1398 | struct server *sv; |
| 1399 | |
| 1400 | sv = hlua_check_server(L, 1); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1401 | if (sv == NULL) { |
| 1402 | return 0; |
| 1403 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1404 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1405 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 1406 | sv->agent.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1407 | srv_set_stopped(sv, SRV_OP_STCHGC_LUA); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1408 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1409 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1410 | return 0; |
| 1411 | } |
| 1412 | |
Aurelien DARRAGON | 406511a | 2023-03-29 11:30:36 +0200 | [diff] [blame^] | 1413 | /* returns the tracked server, if any */ |
| 1414 | int hlua_server_tracking(lua_State *L) |
| 1415 | { |
| 1416 | struct server *sv; |
| 1417 | struct server *tracked; |
| 1418 | |
| 1419 | sv = hlua_check_server(L, 1); |
| 1420 | if (sv == NULL) { |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | tracked = sv->track; |
| 1425 | if (tracked == NULL) |
| 1426 | lua_pushnil(L); |
| 1427 | else |
| 1428 | hlua_fcn_new_server(L, tracked); |
| 1429 | |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
Aurelien DARRAGON | 223770d | 2023-03-10 15:34:35 +0100 | [diff] [blame] | 1433 | /* hlua_event_sub wrapper for per-server subscription: |
| 1434 | * |
| 1435 | * hlua_event_sub() is called with sv->e_subs subscription list and |
| 1436 | * lua arguments are passed as-is (skipping the first argument which |
| 1437 | * is the server ctx) |
| 1438 | */ |
| 1439 | int hlua_server_event_sub(lua_State *L) |
| 1440 | { |
| 1441 | struct server *sv; |
| 1442 | |
| 1443 | sv = hlua_check_server(L, 1); |
| 1444 | if (sv == NULL) { |
| 1445 | return 0; |
| 1446 | } |
| 1447 | /* remove first argument from the stack (server) */ |
| 1448 | lua_remove(L, 1); |
| 1449 | |
| 1450 | /* try to subscribe within server's subscription list */ |
| 1451 | return hlua_event_sub(L, &sv->e_subs); |
| 1452 | } |
| 1453 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1454 | int hlua_fcn_new_server(lua_State *L, struct server *srv) |
| 1455 | { |
| 1456 | lua_newtable(L); |
| 1457 | |
| 1458 | /* Pop a class server metatable and affect it to the userdata. */ |
| 1459 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref); |
| 1460 | lua_setmetatable(L, -2); |
| 1461 | |
| 1462 | lua_pushlightuserdata(L, srv); |
| 1463 | lua_rawseti(L, -2, 0); |
| 1464 | |
| 1465 | /* userdata is affected: increment server refcount */ |
| 1466 | srv_take(srv); |
| 1467 | |
| 1468 | /* set public methods */ |
| 1469 | hlua_class_function(L, "get_name", hlua_server_get_name); |
| 1470 | hlua_class_function(L, "get_puid", hlua_server_get_puid); |
Aurelien DARRAGON | 94ee663 | 2023-03-10 15:11:27 +0100 | [diff] [blame] | 1471 | hlua_class_function(L, "get_rid", hlua_server_get_rid); |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1472 | hlua_class_function(L, "is_draining", hlua_server_is_draining); |
Aurelien DARRAGON | c72051d | 2023-03-29 10:44:38 +0200 | [diff] [blame] | 1473 | hlua_class_function(L, "is_backup", hlua_server_is_backup); |
Aurelien DARRAGON | 7a03dee | 2023-03-29 10:49:30 +0200 | [diff] [blame] | 1474 | hlua_class_function(L, "is_dynamic", hlua_server_is_dynamic); |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1475 | hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn); |
| 1476 | hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn); |
| 1477 | hlua_class_function(L, "set_weight", hlua_server_set_weight); |
| 1478 | hlua_class_function(L, "get_weight", hlua_server_get_weight); |
| 1479 | hlua_class_function(L, "set_addr", hlua_server_set_addr); |
| 1480 | hlua_class_function(L, "get_addr", hlua_server_get_addr); |
| 1481 | hlua_class_function(L, "get_stats", hlua_server_get_stats); |
| 1482 | hlua_class_function(L, "shut_sess", hlua_server_shut_sess); |
| 1483 | hlua_class_function(L, "set_drain", hlua_server_set_drain); |
| 1484 | hlua_class_function(L, "set_maint", hlua_server_set_maint); |
| 1485 | hlua_class_function(L, "set_ready", hlua_server_set_ready); |
| 1486 | hlua_class_function(L, "check_enable", hlua_server_check_enable); |
| 1487 | hlua_class_function(L, "check_disable", hlua_server_check_disable); |
| 1488 | hlua_class_function(L, "check_force_up", hlua_server_check_force_up); |
| 1489 | hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb); |
| 1490 | hlua_class_function(L, "check_force_down", hlua_server_check_force_down); |
| 1491 | hlua_class_function(L, "agent_enable", hlua_server_agent_enable); |
| 1492 | hlua_class_function(L, "agent_disable", hlua_server_agent_disable); |
| 1493 | hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up); |
| 1494 | hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down); |
Aurelien DARRAGON | 406511a | 2023-03-29 11:30:36 +0200 | [diff] [blame^] | 1495 | hlua_class_function(L, "tracking", hlua_server_tracking); |
Aurelien DARRAGON | 223770d | 2023-03-10 15:34:35 +0100 | [diff] [blame] | 1496 | hlua_class_function(L, "event_sub", hlua_server_event_sub); |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1497 | |
| 1498 | return 1; |
| 1499 | } |
| 1500 | |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1501 | static struct hlua_server_list *hlua_check_server_list(lua_State *L, int ud) |
| 1502 | { |
| 1503 | return hlua_checkudata(L, ud, class_server_list_ref); |
| 1504 | } |
| 1505 | |
| 1506 | /* does nothing and returns 0, only prevents insertions in the |
| 1507 | * table which represents the list of servers |
| 1508 | */ |
| 1509 | int hlua_listable_servers_newindex(lua_State *L) { |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | /* first arg is the table (struct hlua_server_list * in metadata) |
| 1514 | * second arg is the required index |
| 1515 | */ |
| 1516 | int hlua_listable_servers_index(lua_State *L) |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1517 | { |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1518 | struct hlua_server_list *hlua_srv; |
| 1519 | const char *name; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1520 | struct server *srv; |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 1521 | |
| 1522 | hlua_srv = hlua_check_server_list(L, 1); |
| 1523 | name = luaL_checkstring(L, 2); |
| 1524 | |
| 1525 | /* Perform a server lookup in px list */ |
| 1526 | srv = server_find_by_name(hlua_srv->px, name); |
| 1527 | if (srv == NULL) { |
| 1528 | lua_pushnil(L); |
| 1529 | return 1; |
| 1530 | } |
| 1531 | |
| 1532 | hlua_fcn_new_server(L, srv); |
| 1533 | return 1; |
| 1534 | } |
| 1535 | |
| 1536 | /* iterator must return key as string and value as server |
| 1537 | * object, if we reach end of list, it returns nil. |
| 1538 | * The context knows the last returned server. if the |
| 1539 | * context contains srv == NULL, we start enumeration. |
| 1540 | * Then, use 'srv->next' ptr to iterate through the list |
| 1541 | */ |
| 1542 | int hlua_listable_servers_pairs_iterator(lua_State *L) |
| 1543 | { |
| 1544 | int context_index; |
| 1545 | struct hlua_server_list_iterator_context *ctx; |
| 1546 | |
| 1547 | context_index = lua_upvalueindex(1); |
| 1548 | ctx = lua_touserdata(L, context_index); |
| 1549 | |
| 1550 | if (ctx->cur == NULL) { |
| 1551 | /* First iteration, initialize list on the first server */ |
| 1552 | ctx->cur = ctx->px->srv; |
| 1553 | } else { |
| 1554 | |
| 1555 | /* Next server (next ptr is always valid, even if current |
| 1556 | * server has the SRV_F_DELETED flag set) |
| 1557 | */ |
| 1558 | ctx->cur = ctx->cur->next; |
| 1559 | } |
| 1560 | |
| 1561 | /* next server is null, end of iteration */ |
| 1562 | if (ctx->cur == NULL) { |
| 1563 | lua_pushnil(L); |
| 1564 | return 1; |
| 1565 | } |
| 1566 | |
| 1567 | lua_pushstring(L, ctx->cur->id); |
| 1568 | hlua_fcn_new_server(L, ctx->cur); |
| 1569 | return 2; |
| 1570 | } |
| 1571 | |
| 1572 | /* init the iterator context, return iterator function |
| 1573 | * with context as closure. The only argument is a |
| 1574 | * server list object. |
| 1575 | */ |
| 1576 | int hlua_listable_servers_pairs(lua_State *L) |
| 1577 | { |
| 1578 | struct hlua_server_list_iterator_context *ctx; |
| 1579 | struct hlua_server_list *hlua_srv_list; |
| 1580 | |
| 1581 | hlua_srv_list = hlua_check_server_list(L, 1); |
| 1582 | |
| 1583 | ctx = lua_newuserdata(L, sizeof(*ctx)); |
| 1584 | ctx->px = hlua_srv_list->px; |
| 1585 | ctx->cur = NULL; |
| 1586 | |
| 1587 | lua_pushcclosure(L, hlua_listable_servers_pairs_iterator, 1); |
| 1588 | return 1; |
| 1589 | } |
| 1590 | |
| 1591 | void hlua_listable_servers(lua_State *L, struct proxy *px) |
| 1592 | { |
| 1593 | struct hlua_server_list *list; |
| 1594 | |
| 1595 | lua_newtable(L); |
| 1596 | list = lua_newuserdata(L, sizeof(*list)); |
| 1597 | list->px = px; |
| 1598 | lua_rawseti(L, -2, 0); |
| 1599 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_list_ref); |
| 1600 | lua_setmetatable(L, -2); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | static struct proxy *hlua_check_proxy(lua_State *L, int ud) |
| 1604 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1605 | return hlua_checkudata(L, ud, class_proxy_ref); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1608 | int hlua_proxy_get_name(lua_State *L) |
| 1609 | { |
| 1610 | struct proxy *px; |
| 1611 | |
| 1612 | px = hlua_check_proxy(L, 1); |
| 1613 | lua_pushstring(L, px->id); |
| 1614 | return 1; |
| 1615 | } |
| 1616 | |
| 1617 | int hlua_proxy_get_uuid(lua_State *L) |
| 1618 | { |
| 1619 | struct proxy *px; |
| 1620 | char buffer[17]; |
| 1621 | |
| 1622 | px = hlua_check_proxy(L, 1); |
| 1623 | snprintf(buffer, sizeof(buffer), "%d", px->uuid); |
| 1624 | lua_pushstring(L, buffer); |
| 1625 | return 1; |
| 1626 | } |
| 1627 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1628 | /* __index metamethod for proxy class |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 1629 | * support for additional keys that are missing from the main table |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1630 | * stack:1 = table (proxy class), stack:2 = requested key |
| 1631 | * Returns 1 if key is supported |
| 1632 | * else returns 0 to make lua return NIL value to the caller |
| 1633 | */ |
| 1634 | static int hlua_proxy_index(struct lua_State *L) |
| 1635 | { |
| 1636 | const char *key = lua_tostring(L, 2); |
| 1637 | |
| 1638 | if (!strcmp(key, "name")) { |
| 1639 | if (ONLY_ONCE()) |
| 1640 | ha_warning("hlua: use of proxy 'name' attribute is deprecated and will eventually be removed, please use get_name() function instead: %s\n", hlua_traceback(L, ", ")); |
| 1641 | lua_pushvalue(L, 1); |
| 1642 | hlua_proxy_get_name(L); |
| 1643 | return 1; |
| 1644 | } |
| 1645 | if (!strcmp(key, "uuid")) { |
| 1646 | if (ONLY_ONCE()) |
| 1647 | ha_warning("hlua: use of proxy 'uuid' attribute is deprecated and will eventually be removed, please use get_uuid() function instead: %s\n", hlua_traceback(L, ", ")); |
| 1648 | lua_pushvalue(L, 1); |
| 1649 | hlua_proxy_get_uuid(L); |
| 1650 | return 1; |
| 1651 | } |
| 1652 | /* unknown attribute */ |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1656 | int hlua_proxy_pause(lua_State *L) |
| 1657 | { |
| 1658 | struct proxy *px; |
| 1659 | |
| 1660 | px = hlua_check_proxy(L, 1); |
Aurelien DARRAGON | 7d00077 | 2022-09-08 14:35:35 +0200 | [diff] [blame] | 1661 | /* safe to call without PROXY_LOCK - pause_proxy takes it */ |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1662 | pause_proxy(px); |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | int hlua_proxy_resume(lua_State *L) |
| 1667 | { |
| 1668 | struct proxy *px; |
| 1669 | |
| 1670 | px = hlua_check_proxy(L, 1); |
Aurelien DARRAGON | 7d00077 | 2022-09-08 14:35:35 +0200 | [diff] [blame] | 1671 | /* safe to call without PROXY_LOCK - resume_proxy takes it */ |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1672 | resume_proxy(px); |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | int hlua_proxy_stop(lua_State *L) |
| 1677 | { |
| 1678 | struct proxy *px; |
| 1679 | |
| 1680 | px = hlua_check_proxy(L, 1); |
Aurelien DARRAGON | 7d00077 | 2022-09-08 14:35:35 +0200 | [diff] [blame] | 1681 | /* safe to call without PROXY_LOCK - stop_proxy takes it */ |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1682 | stop_proxy(px); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | int hlua_proxy_get_cap(lua_State *L) |
| 1687 | { |
| 1688 | struct proxy *px; |
| 1689 | const char *str; |
| 1690 | |
| 1691 | px = hlua_check_proxy(L, 1); |
| 1692 | str = proxy_cap_str(px->cap); |
| 1693 | lua_pushstring(L, str); |
| 1694 | return 1; |
| 1695 | } |
| 1696 | |
| 1697 | int hlua_proxy_get_stats(lua_State *L) |
| 1698 | { |
| 1699 | struct proxy *px; |
| 1700 | int i; |
| 1701 | |
| 1702 | px = hlua_check_proxy(L, 1); |
| 1703 | if (px->cap & PR_CAP_BE) |
William Dauchy | da3b466 | 2021-01-25 17:29:01 +0100 | [diff] [blame] | 1704 | stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1705 | else |
William Dauchy | 0ef5439 | 2021-01-17 18:27:45 +0100 | [diff] [blame] | 1706 | stats_fill_fe_stats(px, stats, STATS_LEN, NULL); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1707 | lua_newtable(L); |
| 1708 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 1709 | lua_pushstring(L, stat_fields[i].name); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1710 | hlua_fcn_pushfield(L, &stats[i]); |
| 1711 | lua_settable(L, -3); |
| 1712 | } |
| 1713 | return 1; |
| 1714 | } |
| 1715 | |
| 1716 | int hlua_proxy_get_mode(lua_State *L) |
| 1717 | { |
| 1718 | struct proxy *px; |
| 1719 | const char *str; |
| 1720 | |
| 1721 | px = hlua_check_proxy(L, 1); |
| 1722 | str = proxy_mode_str(px->mode); |
| 1723 | lua_pushstring(L, str); |
| 1724 | return 1; |
| 1725 | } |
| 1726 | |
| 1727 | int hlua_proxy_shut_bcksess(lua_State *L) |
| 1728 | { |
| 1729 | struct proxy *px; |
| 1730 | |
| 1731 | px = hlua_check_proxy(L, 1); |
| 1732 | srv_shutdown_backup_streams(px, SF_ERR_KILLED); |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1736 | int hlua_fcn_new_proxy(lua_State *L, struct proxy *px) |
| 1737 | { |
| 1738 | struct listener *lst; |
| 1739 | int lid; |
| 1740 | char buffer[17]; |
| 1741 | |
| 1742 | lua_newtable(L); |
| 1743 | |
| 1744 | /* Pop a class proxy metatable and affect it to the userdata. */ |
| 1745 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref); |
| 1746 | lua_setmetatable(L, -2); |
| 1747 | |
| 1748 | lua_pushlightuserdata(L, px); |
| 1749 | lua_rawseti(L, -2, 0); |
| 1750 | |
| 1751 | /* set public methods */ |
| 1752 | hlua_class_function(L, "get_name", hlua_proxy_get_name); |
| 1753 | hlua_class_function(L, "get_uuid", hlua_proxy_get_uuid); |
| 1754 | hlua_class_function(L, "pause", hlua_proxy_pause); |
| 1755 | hlua_class_function(L, "resume", hlua_proxy_resume); |
| 1756 | hlua_class_function(L, "stop", hlua_proxy_stop); |
| 1757 | hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess); |
| 1758 | hlua_class_function(L, "get_cap", hlua_proxy_get_cap); |
| 1759 | hlua_class_function(L, "get_mode", hlua_proxy_get_mode); |
| 1760 | hlua_class_function(L, "get_stats", hlua_proxy_get_stats); |
| 1761 | |
| 1762 | /* Browse and register servers. */ |
| 1763 | lua_pushstring(L, "servers"); |
| 1764 | hlua_listable_servers(L, px); |
| 1765 | lua_settable(L, -3); |
| 1766 | |
| 1767 | /* Browse and register listeners. */ |
| 1768 | lua_pushstring(L, "listeners"); |
| 1769 | lua_newtable(L); |
| 1770 | lid = 1; |
| 1771 | list_for_each_entry(lst, &px->conf.listeners, by_fe) { |
| 1772 | if (lst->name) |
| 1773 | lua_pushstring(L, lst->name); |
| 1774 | else { |
| 1775 | snprintf(buffer, sizeof(buffer), "sock-%d", lid); |
| 1776 | lid++; |
| 1777 | lua_pushstring(L, buffer); |
| 1778 | } |
| 1779 | hlua_fcn_new_listener(L, lst); |
| 1780 | lua_settable(L, -3); |
| 1781 | } |
| 1782 | lua_settable(L, -3); |
| 1783 | |
| 1784 | if (px->table && px->table->id) { |
| 1785 | lua_pushstring(L, "stktable"); |
| 1786 | hlua_fcn_new_stktable(L, px->table); |
| 1787 | lua_settable(L, -3); |
| 1788 | } |
| 1789 | |
| 1790 | return 1; |
| 1791 | } |
| 1792 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1793 | static struct hlua_proxy_list *hlua_check_proxy_list(lua_State *L, int ud) |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1794 | { |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1795 | return hlua_checkudata(L, ud, class_proxy_list_ref); |
| 1796 | } |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1797 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1798 | /* does nothing and returns 0, only prevents insertions in the |
| 1799 | * table which represent list of proxies |
| 1800 | */ |
| 1801 | int hlua_listable_proxies_newindex(lua_State *L) { |
| 1802 | return 0; |
| 1803 | } |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1804 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1805 | /* first arg is the table (struct hlua_proxy_list * in metadata) |
| 1806 | * second arg is the required index |
| 1807 | */ |
| 1808 | int hlua_listable_proxies_index(lua_State *L) |
| 1809 | { |
| 1810 | struct hlua_proxy_list *hlua_px; |
| 1811 | const char *name; |
| 1812 | struct proxy *px; |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1813 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1814 | hlua_px = hlua_check_proxy_list(L, 1); |
| 1815 | name = luaL_checkstring(L, 2); |
| 1816 | |
| 1817 | px = NULL; |
| 1818 | if (hlua_px->capabilities & PR_CAP_FE) { |
| 1819 | px = proxy_find_by_name(name, PR_CAP_FE, 0); |
| 1820 | } |
| 1821 | if (!px && hlua_px->capabilities & PR_CAP_BE) { |
| 1822 | px = proxy_find_by_name(name, PR_CAP_BE, 0); |
| 1823 | } |
| 1824 | if (px == NULL) { |
| 1825 | lua_pushnil(L); |
| 1826 | return 1; |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1829 | hlua_fcn_new_proxy(L, px); |
| 1830 | return 1; |
| 1831 | } |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1832 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1833 | static inline int hlua_listable_proxies_match(struct proxy *px, char cap) { |
| 1834 | return ((px->cap & cap) && !(px->cap & (PR_CAP_DEF | PR_CAP_INT))); |
| 1835 | } |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1836 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1837 | /* iterator must return key as string and value as proxy |
| 1838 | * object, if we reach end of list, it returns nil |
| 1839 | */ |
| 1840 | int hlua_listable_proxies_pairs_iterator(lua_State *L) |
| 1841 | { |
| 1842 | int context_index; |
| 1843 | struct hlua_proxy_list_iterator_context *ctx; |
| 1844 | |
| 1845 | context_index = lua_upvalueindex(1); |
| 1846 | ctx = lua_touserdata(L, context_index); |
| 1847 | |
| 1848 | if (ctx->next == NULL) { |
| 1849 | lua_pushnil(L); |
| 1850 | return 1; |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1853 | lua_pushstring(L, ctx->next->id); |
| 1854 | hlua_fcn_new_proxy(L, ctx->next); |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1855 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1856 | for (ctx->next = ctx->next->next; |
| 1857 | ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities); |
| 1858 | ctx->next = ctx->next->next); |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1859 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1860 | return 2; |
| 1861 | } |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1862 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1863 | /* init the iterator context, return iterator function |
| 1864 | * with context as closure. The only argument is a |
| 1865 | * proxy object. |
| 1866 | */ |
| 1867 | int hlua_listable_proxies_pairs(lua_State *L) |
| 1868 | { |
| 1869 | struct hlua_proxy_list_iterator_context *ctx; |
| 1870 | struct hlua_proxy_list *hlua_px; |
| 1871 | |
| 1872 | hlua_px = hlua_check_proxy_list(L, 1); |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1873 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1874 | ctx = lua_newuserdata(L, sizeof(*ctx)); |
| 1875 | |
| 1876 | ctx->capabilities = hlua_px->capabilities; |
| 1877 | for (ctx->next = proxies_list; |
| 1878 | ctx->next && !hlua_listable_proxies_match(ctx->next, ctx->capabilities); |
| 1879 | ctx->next = ctx->next->next); |
| 1880 | lua_pushcclosure(L, hlua_listable_proxies_pairs_iterator, 1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1881 | return 1; |
| 1882 | } |
| 1883 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 1884 | void hlua_listable_proxies(lua_State *L, char capabilities) |
| 1885 | { |
| 1886 | struct hlua_proxy_list *list; |
| 1887 | |
| 1888 | lua_newtable(L); |
| 1889 | list = lua_newuserdata(L, sizeof(*list)); |
| 1890 | list->capabilities = capabilities; |
| 1891 | lua_rawseti(L, -2, 0); |
| 1892 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_list_ref); |
| 1893 | lua_setmetatable(L, -2); |
| 1894 | } |
| 1895 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 1896 | int hlua_event_sub_unsub(lua_State *L) |
| 1897 | { |
| 1898 | struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref); |
| 1899 | |
| 1900 | BUG_ON(!sub); |
| 1901 | event_hdl_take(sub); /* keep a reference on sub until the item is GCed */ |
| 1902 | event_hdl_unsubscribe(sub); /* will automatically call event_hdl_drop() */ |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | int hlua_event_sub_gc(lua_State *L) |
| 1907 | { |
| 1908 | struct event_hdl_sub *sub = hlua_checkudata(L, 1, class_event_sub_ref); |
| 1909 | |
| 1910 | BUG_ON(!sub); |
| 1911 | event_hdl_drop(sub); /* final drop of the reference */ |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
| 1915 | int hlua_fcn_new_event_sub(lua_State *L, struct event_hdl_sub *sub) |
| 1916 | { |
| 1917 | lua_newtable(L); |
| 1918 | |
| 1919 | /* Pop a class event_sub metatable and affect it to the userdata. */ |
| 1920 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_event_sub_ref); |
| 1921 | lua_setmetatable(L, -2); |
| 1922 | |
| 1923 | lua_pushlightuserdata(L, sub); |
| 1924 | lua_rawseti(L, -2, 0); |
| 1925 | |
| 1926 | /* userdata is affected: increment sub refcount */ |
| 1927 | event_hdl_take(sub); |
| 1928 | |
| 1929 | /* set public methods */ |
| 1930 | hlua_class_function(L, "unsub", hlua_event_sub_unsub); |
| 1931 | |
| 1932 | return 1; |
| 1933 | } |
| 1934 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1935 | /* This Lua function take a string, a list of separators. |
| 1936 | * It tokenize the input string using the list of separators |
| 1937 | * as separator. |
| 1938 | * |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1939 | * The functionreturns a table filled with tokens. |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1940 | */ |
| 1941 | int hlua_tokenize(lua_State *L) |
| 1942 | { |
| 1943 | const char *str; |
| 1944 | const char *sep; |
| 1945 | int index; |
| 1946 | const char *token; |
| 1947 | const char *p; |
| 1948 | const char *c; |
| 1949 | int ignore_empty; |
| 1950 | |
| 1951 | ignore_empty = 0; |
| 1952 | |
| 1953 | str = luaL_checkstring(L, 1); |
| 1954 | sep = luaL_checkstring(L, 2); |
| 1955 | if (lua_gettop(L) == 3) |
| 1956 | ignore_empty = hlua_checkboolean(L, 3); |
| 1957 | |
| 1958 | lua_newtable(L); |
| 1959 | index = 1; |
| 1960 | token = str; |
| 1961 | p = str; |
| 1962 | while(1) { |
| 1963 | for (c = sep; *c != '\0'; c++) |
| 1964 | if (*p == *c) |
| 1965 | break; |
| 1966 | if (*p == *c) { |
| 1967 | if ((!ignore_empty) || (p - token > 0)) { |
| 1968 | lua_pushlstring(L, token, p - token); |
| 1969 | lua_rawseti(L, -2, index); |
| 1970 | index++; |
| 1971 | } |
| 1972 | token = p + 1; |
| 1973 | } |
| 1974 | if (*p == '\0') |
| 1975 | break; |
| 1976 | p++; |
| 1977 | } |
| 1978 | |
| 1979 | return 1; |
| 1980 | } |
| 1981 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1982 | int hlua_parse_addr(lua_State *L) |
| 1983 | { |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1984 | struct net_addr *addr; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1985 | const char *str = luaL_checkstring(L, 1); |
| 1986 | unsigned char mask; |
| 1987 | |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1988 | addr = lua_newuserdata(L, sizeof(struct net_addr)); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1989 | if (!addr) { |
| 1990 | lua_pushnil(L); |
| 1991 | return 1; |
| 1992 | } |
| 1993 | |
| 1994 | if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) { |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1995 | addr->family = AF_INET; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1996 | return 1; |
| 1997 | } |
| 1998 | |
| 1999 | if (str62net(str, &addr->addr.v6.ip, &mask)) { |
| 2000 | len2mask6(mask, &addr->addr.v6.mask); |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 2001 | addr->family = AF_INET6; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 2002 | return 1; |
| 2003 | } |
| 2004 | |
| 2005 | lua_pop(L, 1); |
| 2006 | lua_pushnil(L); |
| 2007 | return 1; |
| 2008 | } |
| 2009 | |
| 2010 | int hlua_match_addr(lua_State *L) |
| 2011 | { |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 2012 | struct net_addr *addr1; |
| 2013 | struct net_addr *addr2; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 2014 | |
| 2015 | if (!lua_isuserdata(L, 1) || |
| 2016 | !lua_isuserdata(L, 2)) { |
| 2017 | lua_pushboolean(L, 0); |
| 2018 | return 1; |
| 2019 | } |
| 2020 | |
| 2021 | addr1 = lua_touserdata(L, 1); |
| 2022 | addr2 = lua_touserdata(L, 2); |
| 2023 | |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 2024 | if (addr1->family != addr2->family) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 2025 | lua_pushboolean(L, 0); |
| 2026 | return 1; |
| 2027 | } |
| 2028 | |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 2029 | if (addr1->family == AF_INET) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 2030 | if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) == |
| 2031 | (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) { |
| 2032 | lua_pushboolean(L, 1); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | } else { |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 2036 | int i; |
| 2037 | |
| 2038 | for (i = 0; i < 16; i += 4) { |
Willy Tarreau | 26474c4 | 2020-02-25 10:02:51 +0100 | [diff] [blame] | 2039 | if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) & |
| 2040 | read_u32(&addr2->addr.v6.mask.s6_addr[i])) != |
| 2041 | (read_u32(&addr2->addr.v6.ip.s6_addr[i]) & |
| 2042 | read_u32(&addr1->addr.v6.mask.s6_addr[i]))) |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 2043 | break; |
| 2044 | } |
| 2045 | if (i == 16) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 2046 | lua_pushboolean(L, 1); |
| 2047 | return 1; |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | lua_pushboolean(L, 0); |
| 2052 | return 1; |
| 2053 | } |
| 2054 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2055 | static struct my_regex **hlua_check_regex(lua_State *L, int ud) |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2056 | { |
| 2057 | return (hlua_checkudata(L, ud, class_regex_ref)); |
| 2058 | } |
| 2059 | |
| 2060 | static int hlua_regex_comp(struct lua_State *L) |
| 2061 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2062 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2063 | const char *str; |
| 2064 | int cs; |
| 2065 | char *err; |
| 2066 | |
| 2067 | str = luaL_checkstring(L, 1); |
| 2068 | luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL); |
| 2069 | cs = lua_toboolean(L, 2); |
| 2070 | |
| 2071 | regex = lua_newuserdata(L, sizeof(*regex)); |
| 2072 | |
| 2073 | err = NULL; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2074 | if (!(*regex = regex_comp(str, cs, 1, &err))) { |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2075 | lua_pushboolean(L, 0); /* status error */ |
| 2076 | lua_pushstring(L, err); /* Reason */ |
| 2077 | free(err); |
| 2078 | return 2; |
| 2079 | } |
| 2080 | |
| 2081 | lua_pushboolean(L, 1); /* Status ok */ |
| 2082 | |
| 2083 | /* Create object */ |
| 2084 | lua_newtable(L); |
| 2085 | lua_pushvalue(L, -3); /* Get the userdata pointer. */ |
| 2086 | lua_rawseti(L, -2, 0); |
| 2087 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref); |
| 2088 | lua_setmetatable(L, -2); |
| 2089 | return 2; |
| 2090 | } |
| 2091 | |
| 2092 | static int hlua_regex_exec(struct lua_State *L) |
| 2093 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2094 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2095 | const char *str; |
| 2096 | size_t len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2097 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2098 | |
| 2099 | regex = hlua_check_regex(L, 1); |
| 2100 | str = luaL_checklstring(L, 2, &len); |
| 2101 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2102 | if (!*regex) { |
| 2103 | lua_pushboolean(L, 0); |
| 2104 | return 1; |
| 2105 | } |
| 2106 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 2107 | /* Copy the string because regex_exec2 require a 'char *' |
| 2108 | * and not a 'const char *'. |
| 2109 | */ |
| 2110 | tmp = get_trash_chunk(); |
| 2111 | if (len >= tmp->size) { |
| 2112 | lua_pushboolean(L, 0); |
| 2113 | return 1; |
| 2114 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2115 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 2116 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2117 | lua_pushboolean(L, regex_exec2(*regex, tmp->area, len)); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2118 | |
| 2119 | return 1; |
| 2120 | } |
| 2121 | |
| 2122 | static int hlua_regex_match(struct lua_State *L) |
| 2123 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2124 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2125 | const char *str; |
| 2126 | size_t len; |
| 2127 | regmatch_t pmatch[20]; |
| 2128 | int ret; |
| 2129 | int i; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2130 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2131 | |
| 2132 | regex = hlua_check_regex(L, 1); |
| 2133 | str = luaL_checklstring(L, 2, &len); |
| 2134 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2135 | if (!*regex) { |
| 2136 | lua_pushboolean(L, 0); |
| 2137 | return 1; |
| 2138 | } |
| 2139 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 2140 | /* Copy the string because regex_exec2 require a 'char *' |
| 2141 | * and not a 'const char *'. |
| 2142 | */ |
| 2143 | tmp = get_trash_chunk(); |
| 2144 | if (len >= tmp->size) { |
| 2145 | lua_pushboolean(L, 0); |
| 2146 | return 1; |
| 2147 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2148 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 2149 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2150 | ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2151 | lua_pushboolean(L, ret); |
| 2152 | lua_newtable(L); |
| 2153 | if (ret) { |
| 2154 | for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) { |
| 2155 | lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so); |
| 2156 | lua_rawseti(L, -2, i + 1); |
| 2157 | } |
| 2158 | } |
| 2159 | return 2; |
| 2160 | } |
| 2161 | |
| 2162 | static int hlua_regex_free(struct lua_State *L) |
| 2163 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2164 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2165 | |
| 2166 | regex = hlua_check_regex(L, 1); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 2167 | regex_free(*regex); |
| 2168 | *regex = NULL; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2169 | return 0; |
| 2170 | } |
| 2171 | |
Thierry Fournier | 599f231 | 2022-09-30 10:40:39 +0200 | [diff] [blame] | 2172 | void hlua_fcn_reg_core_fcn(lua_State *L) |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 2173 | { |
Thierry Fournier | 599f231 | 2022-09-30 10:40:39 +0200 | [diff] [blame] | 2174 | hlua_concat_init(L); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 2175 | |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 2176 | hlua_class_function(L, "now", hlua_now); |
| 2177 | hlua_class_function(L, "http_date", hlua_http_date); |
| 2178 | hlua_class_function(L, "imf_date", hlua_imf_date); |
| 2179 | hlua_class_function(L, "rfc850_date", hlua_rfc850_date); |
| 2180 | hlua_class_function(L, "asctime_date", hlua_asctime_date); |
| 2181 | hlua_class_function(L, "concat", hlua_concat_new); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 2182 | hlua_class_function(L, "get_info", hlua_get_info); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 2183 | hlua_class_function(L, "parse_addr", hlua_parse_addr); |
| 2184 | hlua_class_function(L, "match_addr", hlua_match_addr); |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 2185 | hlua_class_function(L, "tokenize", hlua_tokenize); |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 2186 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2187 | /* Create regex object. */ |
| 2188 | lua_newtable(L); |
| 2189 | hlua_class_function(L, "new", hlua_regex_comp); |
| 2190 | |
| 2191 | lua_newtable(L); /* The metatable. */ |
| 2192 | lua_pushstring(L, "__index"); |
| 2193 | lua_newtable(L); |
| 2194 | hlua_class_function(L, "exec", hlua_regex_exec); |
| 2195 | hlua_class_function(L, "match", hlua_regex_match); |
| 2196 | lua_rawset(L, -3); /* -> META["__index"] = TABLE */ |
| 2197 | hlua_class_function(L, "__gc", hlua_regex_free); |
| 2198 | |
| 2199 | lua_pushvalue(L, -1); /* Duplicate the metatable reference. */ |
| 2200 | class_regex_ref = hlua_register_metatable(L, CLASS_REGEX); |
| 2201 | |
| 2202 | lua_setmetatable(L, -2); |
| 2203 | lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */ |
| 2204 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 2205 | /* Create stktable object. */ |
| 2206 | lua_newtable(L); |
| 2207 | lua_pushstring(L, "__index"); |
| 2208 | lua_newtable(L); |
| 2209 | hlua_class_function(L, "info", hlua_stktable_info); |
| 2210 | hlua_class_function(L, "lookup", hlua_stktable_lookup); |
| 2211 | hlua_class_function(L, "dump", hlua_stktable_dump); |
| 2212 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 2213 | class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE); |
| 2214 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 2215 | /* Create listener object. */ |
| 2216 | lua_newtable(L); |
| 2217 | lua_pushstring(L, "__index"); |
| 2218 | lua_newtable(L); |
| 2219 | hlua_class_function(L, "get_stats", hlua_listener_get_stats); |
| 2220 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 2221 | class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER); |
| 2222 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 2223 | /* Create event_sub object. */ |
| 2224 | lua_newtable(L); |
| 2225 | hlua_class_function(L, "__gc", hlua_event_sub_gc); |
| 2226 | class_event_sub_ref = hlua_register_metatable(L, CLASS_EVENT_SUB); |
| 2227 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 2228 | /* Create server object. */ |
| 2229 | lua_newtable(L); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 2230 | hlua_class_function(L, "__gc", hlua_server_gc); |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 2231 | hlua_class_function(L, "__index", hlua_server_index); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 2232 | class_server_ref = hlua_register_metatable(L, CLASS_SERVER); |
| 2233 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 2234 | /* Create proxy object. */ |
| 2235 | lua_newtable(L); |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 2236 | hlua_class_function(L, "__index", hlua_proxy_index); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 2237 | class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY); |
| 2238 | |
Thierry Fournier | 467913c | 2022-09-30 11:03:38 +0200 | [diff] [blame] | 2239 | /* list of proxy objects. Instead of having a static array |
| 2240 | * of proxies, we use special metamethods that rely on internal |
| 2241 | * proxies list so that the array is resolved at runtime. |
| 2242 | * |
| 2243 | * To emulate the same behavior than Lua array, we implement some |
| 2244 | * metatable functions: |
| 2245 | * - __newindex : prevent the insertion of a new item in the array |
| 2246 | * - __index : find a proxy in the list using "name" index |
| 2247 | * - __pairs : iterate through available proxies in the list |
| 2248 | */ |
| 2249 | lua_newtable(L); |
| 2250 | hlua_class_function(L, "__index", hlua_listable_proxies_index); |
| 2251 | hlua_class_function(L, "__newindex", hlua_listable_proxies_newindex); |
| 2252 | hlua_class_function(L, "__pairs", hlua_listable_proxies_pairs); |
| 2253 | class_proxy_list_ref = hlua_register_metatable(L, CLASS_PROXY_LIST); |
| 2254 | |
| 2255 | /* Create proxies entry. */ |
| 2256 | lua_pushstring(L, "proxies"); |
| 2257 | hlua_listable_proxies(L, PR_CAP_LISTEN); |
| 2258 | lua_settable(L, -3); |
| 2259 | |
| 2260 | /* Create frontends entry. */ |
| 2261 | lua_pushstring(L, "frontends"); |
| 2262 | hlua_listable_proxies(L, PR_CAP_FE); |
| 2263 | lua_settable(L, -3); |
| 2264 | |
| 2265 | /* Create backends entry. */ |
| 2266 | lua_pushstring(L, "backends"); |
| 2267 | hlua_listable_proxies(L, PR_CAP_BE); |
| 2268 | lua_settable(L, -3); |
Thierry Fournier | 1edf36a | 2022-10-07 13:25:51 +0200 | [diff] [blame] | 2269 | |
| 2270 | /* list of server. This object is similar to |
| 2271 | * CLASS_PROXY_LIST |
| 2272 | */ |
| 2273 | lua_newtable(L); |
| 2274 | hlua_class_function(L, "__index", hlua_listable_servers_index); |
| 2275 | hlua_class_function(L, "__newindex", hlua_listable_servers_newindex); |
| 2276 | hlua_class_function(L, "__pairs", hlua_listable_servers_pairs); |
| 2277 | class_server_list_ref = hlua_register_metatable(L, CLASS_SERVER_LIST); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 2278 | } |