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