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