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