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