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