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); |
| 652 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 653 | ts->ref_cnt--; |
| 654 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 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 | |
| 764 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 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) { |
| 769 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 770 | return 1; |
| 771 | } |
| 772 | ts->ref_cnt++; |
| 773 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 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) { |
| 813 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 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); |
| 837 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
| 838 | ts->ref_cnt--; |
| 839 | } |
| 840 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
| 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); |
| 1314 | pause_proxy(px); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | int hlua_proxy_resume(lua_State *L) |
| 1319 | { |
| 1320 | struct proxy *px; |
| 1321 | |
| 1322 | px = hlua_check_proxy(L, 1); |
| 1323 | resume_proxy(px); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | int hlua_proxy_stop(lua_State *L) |
| 1328 | { |
| 1329 | struct proxy *px; |
| 1330 | |
| 1331 | px = hlua_check_proxy(L, 1); |
| 1332 | stop_proxy(px); |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | int hlua_proxy_get_cap(lua_State *L) |
| 1337 | { |
| 1338 | struct proxy *px; |
| 1339 | const char *str; |
| 1340 | |
| 1341 | px = hlua_check_proxy(L, 1); |
| 1342 | str = proxy_cap_str(px->cap); |
| 1343 | lua_pushstring(L, str); |
| 1344 | return 1; |
| 1345 | } |
| 1346 | |
| 1347 | int hlua_proxy_get_stats(lua_State *L) |
| 1348 | { |
| 1349 | struct proxy *px; |
| 1350 | int i; |
| 1351 | |
| 1352 | px = hlua_check_proxy(L, 1); |
| 1353 | if (px->cap & PR_CAP_BE) |
William Dauchy | da3b466 | 2021-01-25 17:29:01 +0100 | [diff] [blame] | 1354 | stats_fill_be_stats(px, STAT_SHLGNDS, stats, STATS_LEN, NULL); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1355 | else |
William Dauchy | 0ef5439 | 2021-01-17 18:27:45 +0100 | [diff] [blame] | 1356 | stats_fill_fe_stats(px, stats, STATS_LEN, NULL); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1357 | lua_newtable(L); |
| 1358 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
Willy Tarreau | eaa5537 | 2019-10-09 07:39:11 +0200 | [diff] [blame] | 1359 | lua_pushstring(L, stat_fields[i].name); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1360 | hlua_fcn_pushfield(L, &stats[i]); |
| 1361 | lua_settable(L, -3); |
| 1362 | } |
| 1363 | return 1; |
| 1364 | } |
| 1365 | |
| 1366 | int hlua_proxy_get_mode(lua_State *L) |
| 1367 | { |
| 1368 | struct proxy *px; |
| 1369 | const char *str; |
| 1370 | |
| 1371 | px = hlua_check_proxy(L, 1); |
| 1372 | str = proxy_mode_str(px->mode); |
| 1373 | lua_pushstring(L, str); |
| 1374 | return 1; |
| 1375 | } |
| 1376 | |
| 1377 | int hlua_proxy_shut_bcksess(lua_State *L) |
| 1378 | { |
| 1379 | struct proxy *px; |
| 1380 | |
| 1381 | px = hlua_check_proxy(L, 1); |
| 1382 | srv_shutdown_backup_streams(px, SF_ERR_KILLED); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1386 | int hlua_fcn_post_init(lua_State *L) |
| 1387 | { |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1388 | struct proxy *px; |
| 1389 | |
| 1390 | /* get core array. */ |
| 1391 | if (lua_getglobal(L, "core") != LUA_TTABLE) |
| 1392 | lua_error(L); |
| 1393 | |
| 1394 | /* Create proxies entry. */ |
| 1395 | lua_pushstring(L, "proxies"); |
| 1396 | lua_newtable(L); |
| 1397 | |
| 1398 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1399 | for (px = proxies_list; px; px = px->next) { |
William Lallemand | 82d5f01 | 2021-11-24 16:14:24 +0100 | [diff] [blame] | 1400 | if (px->cap & PR_CAP_INT) |
| 1401 | continue; |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1402 | lua_pushstring(L, px->id); |
| 1403 | hlua_fcn_new_proxy(L, px); |
| 1404 | lua_settable(L, -3); |
| 1405 | } |
| 1406 | |
| 1407 | /* push "proxies" in "core" */ |
| 1408 | lua_settable(L, -3); |
| 1409 | |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1410 | /* Create proxies entry. */ |
| 1411 | lua_pushstring(L, "frontends"); |
| 1412 | lua_newtable(L); |
| 1413 | |
| 1414 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1415 | for (px = proxies_list; px; px = px->next) { |
William Lallemand | 82d5f01 | 2021-11-24 16:14:24 +0100 | [diff] [blame] | 1416 | if (!(px->cap & PR_CAP_FE) || (px->cap & PR_CAP_INT)) |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1417 | continue; |
| 1418 | lua_pushstring(L, px->id); |
| 1419 | hlua_fcn_new_proxy(L, px); |
| 1420 | lua_settable(L, -3); |
| 1421 | } |
| 1422 | |
| 1423 | /* push "frontends" in "core" */ |
| 1424 | lua_settable(L, -3); |
| 1425 | |
| 1426 | /* Create proxies entry. */ |
| 1427 | lua_pushstring(L, "backends"); |
| 1428 | lua_newtable(L); |
| 1429 | |
| 1430 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1431 | for (px = proxies_list; px; px = px->next) { |
William Lallemand | 82d5f01 | 2021-11-24 16:14:24 +0100 | [diff] [blame] | 1432 | if (!(px->cap & PR_CAP_BE) || (px->cap & PR_CAP_INT)) |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1433 | continue; |
| 1434 | lua_pushstring(L, px->id); |
| 1435 | hlua_fcn_new_proxy(L, px); |
| 1436 | lua_settable(L, -3); |
| 1437 | } |
| 1438 | |
| 1439 | /* push "backend" in "core" */ |
| 1440 | lua_settable(L, -3); |
| 1441 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1442 | return 1; |
| 1443 | } |
| 1444 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1445 | /* This Lua function take a string, a list of separators. |
| 1446 | * It tokenize the input string using the list of separators |
| 1447 | * as separator. |
| 1448 | * |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1449 | * The functionreturns a table filled with tokens. |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1450 | */ |
| 1451 | int hlua_tokenize(lua_State *L) |
| 1452 | { |
| 1453 | const char *str; |
| 1454 | const char *sep; |
| 1455 | int index; |
| 1456 | const char *token; |
| 1457 | const char *p; |
| 1458 | const char *c; |
| 1459 | int ignore_empty; |
| 1460 | |
| 1461 | ignore_empty = 0; |
| 1462 | |
| 1463 | str = luaL_checkstring(L, 1); |
| 1464 | sep = luaL_checkstring(L, 2); |
| 1465 | if (lua_gettop(L) == 3) |
| 1466 | ignore_empty = hlua_checkboolean(L, 3); |
| 1467 | |
| 1468 | lua_newtable(L); |
| 1469 | index = 1; |
| 1470 | token = str; |
| 1471 | p = str; |
| 1472 | while(1) { |
| 1473 | for (c = sep; *c != '\0'; c++) |
| 1474 | if (*p == *c) |
| 1475 | break; |
| 1476 | if (*p == *c) { |
| 1477 | if ((!ignore_empty) || (p - token > 0)) { |
| 1478 | lua_pushlstring(L, token, p - token); |
| 1479 | lua_rawseti(L, -2, index); |
| 1480 | index++; |
| 1481 | } |
| 1482 | token = p + 1; |
| 1483 | } |
| 1484 | if (*p == '\0') |
| 1485 | break; |
| 1486 | p++; |
| 1487 | } |
| 1488 | |
| 1489 | return 1; |
| 1490 | } |
| 1491 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1492 | int hlua_parse_addr(lua_State *L) |
| 1493 | { |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1494 | struct net_addr *addr; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1495 | const char *str = luaL_checkstring(L, 1); |
| 1496 | unsigned char mask; |
| 1497 | |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1498 | addr = lua_newuserdata(L, sizeof(struct net_addr)); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1499 | if (!addr) { |
| 1500 | lua_pushnil(L); |
| 1501 | return 1; |
| 1502 | } |
| 1503 | |
| 1504 | 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] | 1505 | addr->family = AF_INET; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1506 | return 1; |
| 1507 | } |
| 1508 | |
| 1509 | if (str62net(str, &addr->addr.v6.ip, &mask)) { |
| 1510 | len2mask6(mask, &addr->addr.v6.mask); |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1511 | addr->family = AF_INET6; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1512 | return 1; |
| 1513 | } |
| 1514 | |
| 1515 | lua_pop(L, 1); |
| 1516 | lua_pushnil(L); |
| 1517 | return 1; |
| 1518 | } |
| 1519 | |
| 1520 | int hlua_match_addr(lua_State *L) |
| 1521 | { |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1522 | struct net_addr *addr1; |
| 1523 | struct net_addr *addr2; |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1524 | |
| 1525 | if (!lua_isuserdata(L, 1) || |
| 1526 | !lua_isuserdata(L, 2)) { |
| 1527 | lua_pushboolean(L, 0); |
| 1528 | return 1; |
| 1529 | } |
| 1530 | |
| 1531 | addr1 = lua_touserdata(L, 1); |
| 1532 | addr2 = lua_touserdata(L, 2); |
| 1533 | |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1534 | if (addr1->family != addr2->family) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1535 | lua_pushboolean(L, 0); |
| 1536 | return 1; |
| 1537 | } |
| 1538 | |
Christopher Faulet | 29e9326 | 2021-02-26 09:39:05 +0100 | [diff] [blame] | 1539 | if (addr1->family == AF_INET) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1540 | if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) == |
| 1541 | (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) { |
| 1542 | lua_pushboolean(L, 1); |
| 1543 | return 1; |
| 1544 | } |
| 1545 | } else { |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 1546 | int i; |
| 1547 | |
| 1548 | for (i = 0; i < 16; i += 4) { |
Willy Tarreau | 26474c4 | 2020-02-25 10:02:51 +0100 | [diff] [blame] | 1549 | if ((read_u32(&addr1->addr.v6.ip.s6_addr[i]) & |
| 1550 | read_u32(&addr2->addr.v6.mask.s6_addr[i])) != |
| 1551 | (read_u32(&addr2->addr.v6.ip.s6_addr[i]) & |
| 1552 | read_u32(&addr1->addr.v6.mask.s6_addr[i]))) |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 1553 | break; |
| 1554 | } |
| 1555 | if (i == 16) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1556 | lua_pushboolean(L, 1); |
| 1557 | return 1; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | lua_pushboolean(L, 0); |
| 1562 | return 1; |
| 1563 | } |
| 1564 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1565 | static struct my_regex **hlua_check_regex(lua_State *L, int ud) |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1566 | { |
| 1567 | return (hlua_checkudata(L, ud, class_regex_ref)); |
| 1568 | } |
| 1569 | |
| 1570 | static int hlua_regex_comp(struct lua_State *L) |
| 1571 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1572 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1573 | const char *str; |
| 1574 | int cs; |
| 1575 | char *err; |
| 1576 | |
| 1577 | str = luaL_checkstring(L, 1); |
| 1578 | luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL); |
| 1579 | cs = lua_toboolean(L, 2); |
| 1580 | |
| 1581 | regex = lua_newuserdata(L, sizeof(*regex)); |
| 1582 | |
| 1583 | err = NULL; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1584 | if (!(*regex = regex_comp(str, cs, 1, &err))) { |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1585 | lua_pushboolean(L, 0); /* status error */ |
| 1586 | lua_pushstring(L, err); /* Reason */ |
| 1587 | free(err); |
| 1588 | return 2; |
| 1589 | } |
| 1590 | |
| 1591 | lua_pushboolean(L, 1); /* Status ok */ |
| 1592 | |
| 1593 | /* Create object */ |
| 1594 | lua_newtable(L); |
| 1595 | lua_pushvalue(L, -3); /* Get the userdata pointer. */ |
| 1596 | lua_rawseti(L, -2, 0); |
| 1597 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref); |
| 1598 | lua_setmetatable(L, -2); |
| 1599 | return 2; |
| 1600 | } |
| 1601 | |
| 1602 | static int hlua_regex_exec(struct lua_State *L) |
| 1603 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1604 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1605 | const char *str; |
| 1606 | size_t len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1607 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1608 | |
| 1609 | regex = hlua_check_regex(L, 1); |
| 1610 | str = luaL_checklstring(L, 2, &len); |
| 1611 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1612 | if (!*regex) { |
| 1613 | lua_pushboolean(L, 0); |
| 1614 | return 1; |
| 1615 | } |
| 1616 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1617 | /* Copy the string because regex_exec2 require a 'char *' |
| 1618 | * and not a 'const char *'. |
| 1619 | */ |
| 1620 | tmp = get_trash_chunk(); |
| 1621 | if (len >= tmp->size) { |
| 1622 | lua_pushboolean(L, 0); |
| 1623 | return 1; |
| 1624 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1625 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1626 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1627 | lua_pushboolean(L, regex_exec2(*regex, tmp->area, len)); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1628 | |
| 1629 | return 1; |
| 1630 | } |
| 1631 | |
| 1632 | static int hlua_regex_match(struct lua_State *L) |
| 1633 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1634 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1635 | const char *str; |
| 1636 | size_t len; |
| 1637 | regmatch_t pmatch[20]; |
| 1638 | int ret; |
| 1639 | int i; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1640 | struct buffer *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1641 | |
| 1642 | regex = hlua_check_regex(L, 1); |
| 1643 | str = luaL_checklstring(L, 2, &len); |
| 1644 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1645 | if (!*regex) { |
| 1646 | lua_pushboolean(L, 0); |
| 1647 | return 1; |
| 1648 | } |
| 1649 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1650 | /* Copy the string because regex_exec2 require a 'char *' |
| 1651 | * and not a 'const char *'. |
| 1652 | */ |
| 1653 | tmp = get_trash_chunk(); |
| 1654 | if (len >= tmp->size) { |
| 1655 | lua_pushboolean(L, 0); |
| 1656 | return 1; |
| 1657 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1658 | memcpy(tmp->area, str, len); |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1659 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1660 | ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1661 | lua_pushboolean(L, ret); |
| 1662 | lua_newtable(L); |
| 1663 | if (ret) { |
| 1664 | for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) { |
| 1665 | lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so); |
| 1666 | lua_rawseti(L, -2, i + 1); |
| 1667 | } |
| 1668 | } |
| 1669 | return 2; |
| 1670 | } |
| 1671 | |
| 1672 | static int hlua_regex_free(struct lua_State *L) |
| 1673 | { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1674 | struct my_regex **regex; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1675 | |
| 1676 | regex = hlua_check_regex(L, 1); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1677 | regex_free(*regex); |
| 1678 | *regex = NULL; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
| 1681 | |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1682 | int hlua_fcn_reg_core_fcn(lua_State *L) |
| 1683 | { |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1684 | if (!hlua_concat_init(L)) |
| 1685 | return 0; |
| 1686 | |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1687 | hlua_class_function(L, "now", hlua_now); |
| 1688 | hlua_class_function(L, "http_date", hlua_http_date); |
| 1689 | hlua_class_function(L, "imf_date", hlua_imf_date); |
| 1690 | hlua_class_function(L, "rfc850_date", hlua_rfc850_date); |
| 1691 | hlua_class_function(L, "asctime_date", hlua_asctime_date); |
| 1692 | hlua_class_function(L, "concat", hlua_concat_new); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 1693 | hlua_class_function(L, "get_info", hlua_get_info); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1694 | hlua_class_function(L, "parse_addr", hlua_parse_addr); |
| 1695 | hlua_class_function(L, "match_addr", hlua_match_addr); |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1696 | hlua_class_function(L, "tokenize", hlua_tokenize); |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1697 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1698 | /* Create regex object. */ |
| 1699 | lua_newtable(L); |
| 1700 | hlua_class_function(L, "new", hlua_regex_comp); |
| 1701 | |
| 1702 | lua_newtable(L); /* The metatable. */ |
| 1703 | lua_pushstring(L, "__index"); |
| 1704 | lua_newtable(L); |
| 1705 | hlua_class_function(L, "exec", hlua_regex_exec); |
| 1706 | hlua_class_function(L, "match", hlua_regex_match); |
| 1707 | lua_rawset(L, -3); /* -> META["__index"] = TABLE */ |
| 1708 | hlua_class_function(L, "__gc", hlua_regex_free); |
| 1709 | |
| 1710 | lua_pushvalue(L, -1); /* Duplicate the metatable reference. */ |
| 1711 | class_regex_ref = hlua_register_metatable(L, CLASS_REGEX); |
| 1712 | |
| 1713 | lua_setmetatable(L, -2); |
| 1714 | lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */ |
| 1715 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1716 | /* Create stktable object. */ |
| 1717 | lua_newtable(L); |
| 1718 | lua_pushstring(L, "__index"); |
| 1719 | lua_newtable(L); |
| 1720 | hlua_class_function(L, "info", hlua_stktable_info); |
| 1721 | hlua_class_function(L, "lookup", hlua_stktable_lookup); |
| 1722 | hlua_class_function(L, "dump", hlua_stktable_dump); |
| 1723 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1724 | class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE); |
| 1725 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1726 | /* Create listener object. */ |
| 1727 | lua_newtable(L); |
| 1728 | lua_pushstring(L, "__index"); |
| 1729 | lua_newtable(L); |
| 1730 | hlua_class_function(L, "get_stats", hlua_listener_get_stats); |
| 1731 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1732 | class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER); |
| 1733 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1734 | /* Create server object. */ |
| 1735 | lua_newtable(L); |
| 1736 | lua_pushstring(L, "__index"); |
| 1737 | lua_newtable(L); |
| 1738 | hlua_class_function(L, "is_draining", hlua_server_is_draining); |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1739 | hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn); |
| 1740 | hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1741 | hlua_class_function(L, "set_weight", hlua_server_set_weight); |
| 1742 | hlua_class_function(L, "get_weight", hlua_server_get_weight); |
| 1743 | hlua_class_function(L, "set_addr", hlua_server_set_addr); |
| 1744 | hlua_class_function(L, "get_addr", hlua_server_get_addr); |
| 1745 | hlua_class_function(L, "get_stats", hlua_server_get_stats); |
| 1746 | hlua_class_function(L, "shut_sess", hlua_server_shut_sess); |
| 1747 | hlua_class_function(L, "set_drain", hlua_server_set_drain); |
| 1748 | hlua_class_function(L, "set_maint", hlua_server_set_maint); |
| 1749 | hlua_class_function(L, "set_ready", hlua_server_set_ready); |
| 1750 | hlua_class_function(L, "check_enable", hlua_server_check_enable); |
| 1751 | hlua_class_function(L, "check_disable", hlua_server_check_disable); |
| 1752 | hlua_class_function(L, "check_force_up", hlua_server_check_force_up); |
| 1753 | hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb); |
| 1754 | hlua_class_function(L, "check_force_down", hlua_server_check_force_down); |
| 1755 | hlua_class_function(L, "agent_enable", hlua_server_agent_enable); |
| 1756 | hlua_class_function(L, "agent_disable", hlua_server_agent_disable); |
| 1757 | hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up); |
| 1758 | hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down); |
| 1759 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1760 | class_server_ref = hlua_register_metatable(L, CLASS_SERVER); |
| 1761 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1762 | /* Create proxy object. */ |
| 1763 | lua_newtable(L); |
| 1764 | lua_pushstring(L, "__index"); |
| 1765 | lua_newtable(L); |
| 1766 | hlua_class_function(L, "pause", hlua_proxy_pause); |
| 1767 | hlua_class_function(L, "resume", hlua_proxy_resume); |
| 1768 | hlua_class_function(L, "stop", hlua_proxy_stop); |
| 1769 | hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess); |
| 1770 | hlua_class_function(L, "get_cap", hlua_proxy_get_cap); |
| 1771 | hlua_class_function(L, "get_mode", hlua_proxy_get_mode); |
| 1772 | hlua_class_function(L, "get_stats", hlua_proxy_get_stats); |
| 1773 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1774 | class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY); |
| 1775 | |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 1776 | return 5; |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1777 | } |