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