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