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