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