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 | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 29 | #include <proto/proto_http.h> |
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> |
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; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 40 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 41 | #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] | 42 | |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 43 | static THREAD_LOCAL struct field stats[STATS_LEN]; |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 44 | |
Thierry FOURNIER / OZON.IO | 7f3aa8b | 2016-11-24 20:37:38 +0100 | [diff] [blame] | 45 | int hlua_checkboolean(lua_State *L, int index) |
| 46 | { |
| 47 | if (!lua_isboolean(L, index)) |
| 48 | luaL_argerror(L, index, "boolean expected"); |
| 49 | return lua_toboolean(L, index); |
| 50 | } |
| 51 | |
Thierry Fournier | 8b0d6e1 | 2016-03-16 18:29:13 +0100 | [diff] [blame] | 52 | /* This function gets a struct field and convert it in Lua |
| 53 | * variable. The variable is pushed at the top of the stak. |
| 54 | */ |
| 55 | int hlua_fcn_pushfield(lua_State *L, struct field *field) |
| 56 | { |
| 57 | /* The lua_Integer is always signed. Its length depends on |
| 58 | * compilation opions, so the followinfg code is conditionned |
| 59 | * by some macros. Windows maros are not supported. |
| 60 | * If the number cannot be represented as integer, we try to |
| 61 | * convert to float. |
| 62 | */ |
| 63 | switch (field_format(field, 0)) { |
| 64 | |
| 65 | case FF_EMPTY: |
| 66 | lua_pushnil(L); |
| 67 | return 1; |
| 68 | |
| 69 | case FF_S32: |
| 70 | /* S32 is always supported. */ |
| 71 | lua_pushinteger(L, field->u.s32); |
| 72 | return 1; |
| 73 | |
| 74 | case FF_U32: |
| 75 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 76 | /* 64 bits case, U32 is always supported */ |
| 77 | lua_pushinteger(L, field->u.u32); |
| 78 | #else |
| 79 | /* 32 bits case, U32 is supported until INT_MAX. */ |
| 80 | if (field->u.u32 > INT_MAX) |
| 81 | lua_pushnumber(L, (lua_Number)field->u.u32); |
| 82 | else |
| 83 | lua_pushinteger(L, field->u.u32); |
| 84 | #endif |
| 85 | return 1; |
| 86 | |
| 87 | case FF_S64: |
| 88 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 89 | /* 64 bits case, S64 is always supported */ |
| 90 | lua_pushinteger(L, field->u.s64); |
| 91 | #else |
| 92 | /* 64 bits case, S64 is supported beetween INT_MIN and INT_MAX */ |
| 93 | if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX) |
| 94 | lua_pushnumber(L, (lua_Number)field->u.s64); |
| 95 | else |
| 96 | lua_pushinteger(L, (int)field->u.s64); |
| 97 | #endif |
| 98 | return 1; |
| 99 | |
| 100 | case FF_U64: |
| 101 | #if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64))) |
| 102 | /* 64 bits case, U64 is supported until LLONG_MAX */ |
| 103 | if (field->u.u64 > LLONG_MAX) |
| 104 | lua_pushnumber(L, (lua_Number)field->u.u64); |
| 105 | else |
| 106 | lua_pushinteger(L, field->u.u64); |
| 107 | #else |
| 108 | /* 64 bits case, U64 is supported until INT_MAX */ |
| 109 | if (field->u.u64 > INT_MAX) |
| 110 | lua_pushnumber(L, (lua_Number)field->u.u64); |
| 111 | else |
| 112 | lua_pushinteger(L, (int)field->u.u64); |
| 113 | #endif |
| 114 | return 1; |
| 115 | |
| 116 | case FF_STR: |
| 117 | lua_pushstring(L, field->u.str); |
| 118 | return 1; |
| 119 | |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | /* Default case, never reached. */ |
| 125 | lua_pushnil(L); |
| 126 | return 1; |
| 127 | } |
| 128 | |
Thierry Fournier | 94ed1c1 | 2016-02-24 08:06:32 +0100 | [diff] [blame] | 129 | /* Some string are started or terminated by blank chars, |
| 130 | * this function removes the spaces, tabs, \r and |
| 131 | * \n at the begin and at the end of the string "str", and |
| 132 | * push the result in the lua stack. |
| 133 | * Returns a pointer to the Lua internal copy of the string. |
| 134 | */ |
| 135 | const char *hlua_pushstrippedstring(lua_State *L, const char *str) |
| 136 | { |
| 137 | const char *p; |
| 138 | const char *e; |
| 139 | |
| 140 | for (p = str; HTTP_IS_LWS(*p); p++); |
| 141 | for (e = p + strlen(p) - 1; e > p && HTTP_IS_LWS(*e); e--); |
| 142 | |
| 143 | return lua_pushlstring(L, p, e - p); |
| 144 | } |
| 145 | |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 146 | /* The three following functions are useful for adding entries |
| 147 | * in a table. These functions takes a string and respectively an |
| 148 | * integer, a string or a function and add it to the table in the |
| 149 | * top of the stack. |
| 150 | * |
| 151 | * These functions throws an error if no more stack size is |
| 152 | * available. |
| 153 | */ |
| 154 | void hlua_class_const_int(lua_State *L, const char *name, int value) |
| 155 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 156 | lua_pushstring(L, name); |
| 157 | lua_pushinteger(L, value); |
| 158 | lua_rawset(L, -3); |
| 159 | } |
| 160 | void hlua_class_const_str(lua_State *L, const char *name, const char *value) |
| 161 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 162 | lua_pushstring(L, name); |
| 163 | lua_pushstring(L, value); |
| 164 | lua_rawset(L, -3); |
| 165 | } |
| 166 | void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L)) |
| 167 | { |
Thierry Fournier | ddd8988 | 2016-02-22 19:52:08 +0100 | [diff] [blame] | 168 | lua_pushstring(L, name); |
| 169 | lua_pushcclosure(L, function, 0); |
| 170 | lua_rawset(L, -3); |
| 171 | } |
| 172 | |
| 173 | /* This function returns a string containg the HAProxy object name. */ |
| 174 | int hlua_dump_object(struct lua_State *L) |
| 175 | { |
| 176 | const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1)); |
| 177 | lua_pushfstring(L, "HAProxy class %s", name); |
| 178 | return 1; |
| 179 | } |
| 180 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 181 | /* This function register a table as metatable and. It names |
| 182 | * the metatable, and returns the associated reference. |
| 183 | * The original table is poped from the top of the stack. |
| 184 | * "name" is the referenced class name. |
| 185 | */ |
| 186 | int hlua_register_metatable(struct lua_State *L, char *name) |
| 187 | { |
| 188 | /* Check the type of the top element. it must be |
| 189 | * a table. |
| 190 | */ |
| 191 | if (lua_type(L, -1) != LUA_TTABLE) |
| 192 | luaL_error(L, "hlua_register_metatable() requires a type Table " |
| 193 | "in the top of the stack"); |
| 194 | |
| 195 | /* Add the __tostring function which identify the |
| 196 | * created object. |
| 197 | */ |
| 198 | lua_pushstring(L, "__tostring"); |
| 199 | lua_pushstring(L, name); |
| 200 | lua_pushcclosure(L, hlua_dump_object, 1); |
| 201 | lua_rawset(L, -3); |
| 202 | |
| 203 | /* Register a named entry for the table. The table |
| 204 | * reference is copyed first because the function |
| 205 | * lua_setfield() pop the entry. |
| 206 | */ |
| 207 | lua_pushvalue(L, -1); |
| 208 | lua_setfield(L, LUA_REGISTRYINDEX, name); |
| 209 | |
| 210 | /* Creates the reference of the object. The |
| 211 | * function luaL_ref pop the top of the stack. |
| 212 | */ |
| 213 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 214 | } |
| 215 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 216 | /* Return an object of the expected type, or throws an error. */ |
| 217 | void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 218 | { |
| 219 | void *p; |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 220 | int ret; |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 221 | |
| 222 | /* Check if the stack entry is an array. */ |
| 223 | if (!lua_istable(L, ud)) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 224 | luaL_argerror(L, ud, NULL); |
| 225 | |
| 226 | /* pop the metatable of the referencecd object. */ |
| 227 | if (!lua_getmetatable(L, ud)) |
| 228 | luaL_argerror(L, ud, NULL); |
| 229 | |
| 230 | /* pop the expected metatable. */ |
| 231 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 232 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 233 | /* Check if the metadata have the expected type. */ |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 234 | ret = lua_rawequal(L, -1, -2); |
| 235 | lua_pop(L, 2); |
| 236 | if (!ret) |
| 237 | luaL_argerror(L, ud, NULL); |
| 238 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 239 | /* Push on the stack at the entry [0] of the table. */ |
| 240 | lua_rawgeti(L, ud, 0); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 241 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 242 | /* Check if this entry is userdata. */ |
| 243 | p = lua_touserdata(L, -1); |
| 244 | if (!p) |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 245 | luaL_argerror(L, ud, NULL); |
| 246 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 247 | /* Remove the entry returned by lua_rawgeti(). */ |
| 248 | lua_pop(L, 1); |
Thierry Fournier | 5351827 | 2016-01-27 10:34:09 +0100 | [diff] [blame] | 249 | |
Thierry Fournier | 9e7e3ea | 2016-01-27 09:55:30 +0100 | [diff] [blame] | 250 | /* Return the associated struct. */ |
| 251 | return p; |
| 252 | } |
| 253 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 254 | /* This function return the current date at epoch format in milliseconds. */ |
| 255 | int hlua_now(lua_State *L) |
| 256 | { |
| 257 | lua_newtable(L); |
| 258 | lua_pushstring(L, "sec"); |
| 259 | lua_pushinteger(L, now.tv_sec); |
| 260 | lua_rawset(L, -3); |
| 261 | lua_pushstring(L, "usec"); |
| 262 | lua_pushinteger(L, now.tv_usec); |
| 263 | lua_rawset(L, -3); |
| 264 | return 1; |
| 265 | } |
| 266 | |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 267 | /* This functions expects a Lua string as HTTP date, parse it and |
| 268 | * returns an integer containing the epoch format of the date, or |
| 269 | * nil if the parsing fails. |
| 270 | */ |
| 271 | static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*)) |
| 272 | { |
| 273 | const char *str; |
| 274 | size_t len; |
| 275 | struct tm tm; |
| 276 | time_t time; |
| 277 | |
| 278 | str = luaL_checklstring(L, 1, &len); |
| 279 | |
| 280 | if (!fcn(str, len, &tm)) { |
| 281 | lua_pushnil(L); |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | /* This function considers the content of the broken-down time |
| 286 | * is exprimed in the UTC timezone. timegm don't care about |
| 287 | * the gnu variable tm_gmtoff. If gmtoff is set, or if you know |
| 288 | * the timezone from the broken-down time, it must be fixed |
| 289 | * after the conversion. |
| 290 | */ |
Willy Tarreau | abd9bb2 | 2017-07-19 19:08:48 +0200 | [diff] [blame] | 291 | time = my_timegm(&tm); |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 292 | if (time == -1) { |
| 293 | lua_pushnil(L); |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | lua_pushinteger(L, (int)time); |
| 298 | return 1; |
| 299 | } |
| 300 | static int hlua_http_date(lua_State *L) |
| 301 | { |
| 302 | return hlua_parse_date(L, parse_http_date); |
| 303 | } |
| 304 | static int hlua_imf_date(lua_State *L) |
| 305 | { |
| 306 | return hlua_parse_date(L, parse_imf_date); |
| 307 | } |
| 308 | static int hlua_rfc850_date(lua_State *L) |
| 309 | { |
| 310 | return hlua_parse_date(L, parse_rfc850_date); |
| 311 | } |
| 312 | static int hlua_asctime_date(lua_State *L) |
| 313 | { |
| 314 | return hlua_parse_date(L, parse_asctime_date); |
| 315 | } |
| 316 | |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 317 | static int hlua_get_info(lua_State *L) |
| 318 | { |
| 319 | int i; |
| 320 | |
| 321 | stats_fill_info(stats, STATS_LEN); |
| 322 | |
| 323 | lua_newtable(L); |
| 324 | for (i=0; i<INF_TOTAL_FIELDS; i++) { |
| 325 | lua_pushstring(L, info_field_names[i]); |
| 326 | hlua_fcn_pushfield(L, &stats[i]); |
| 327 | lua_settable(L, -3); |
| 328 | } |
| 329 | return 1; |
| 330 | } |
| 331 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 332 | static struct hlua_concat *hlua_check_concat(lua_State *L, int ud) |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 333 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 334 | return (hlua_checkudata(L, ud, class_concat_ref)); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static int hlua_concat_add(lua_State *L) |
| 338 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 339 | struct hlua_concat *b; |
| 340 | char *buffer; |
| 341 | char *new; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 342 | const char *str; |
| 343 | size_t l; |
| 344 | |
| 345 | /* First arg must be a concat object. */ |
| 346 | b = hlua_check_concat(L, 1); |
| 347 | |
| 348 | /* Second arg must be a string. */ |
| 349 | str = luaL_checklstring(L, 2, &l); |
| 350 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 351 | /* Get the buffer. */ |
| 352 | lua_rawgeti(L, 1, 1); |
| 353 | buffer = lua_touserdata(L, -1); |
| 354 | lua_pop(L, 1); |
| 355 | |
| 356 | /* Update the buffer size if it s required. The old buffer |
| 357 | * is crushed by the new in the object array, so it will |
| 358 | * be deleted by the GC. |
| 359 | * Note that in the first loop, the "new" variable is only |
| 360 | * used as a flag. |
| 361 | */ |
| 362 | new = NULL; |
| 363 | while (b->size - b->len < l) { |
| 364 | b->size += HLUA_CONCAT_BLOCSZ; |
| 365 | new = buffer; |
| 366 | } |
| 367 | if (new) { |
| 368 | new = lua_newuserdata(L, b->size); |
| 369 | memcpy(new, buffer, b->len); |
| 370 | lua_rawseti(L, 1, 1); |
| 371 | buffer = new; |
| 372 | } |
| 373 | |
| 374 | /* Copy string, and update metadata. */ |
| 375 | memcpy(buffer + b->len, str, l); |
| 376 | b->len += l; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int hlua_concat_dump(lua_State *L) |
| 381 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 382 | struct hlua_concat *b; |
| 383 | char *buffer; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 384 | |
| 385 | /* First arg must be a concat object. */ |
| 386 | b = hlua_check_concat(L, 1); |
| 387 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 388 | /* Get the buffer. */ |
| 389 | lua_rawgeti(L, 1, 1); |
| 390 | buffer = lua_touserdata(L, -1); |
| 391 | lua_pop(L, 1); |
| 392 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 393 | /* Push the soncatenated strng in the stack. */ |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 394 | lua_pushlstring(L, buffer, b->len); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | int hlua_concat_new(lua_State *L) |
| 399 | { |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 400 | struct hlua_concat *b; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 401 | |
| 402 | lua_newtable(L); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 403 | b = lua_newuserdata(L, sizeof(*b)); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 404 | b->size = HLUA_CONCAT_BLOCSZ; |
| 405 | b->len = 0; |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 406 | lua_rawseti(L, -2, 0); |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 407 | lua_newuserdata(L, HLUA_CONCAT_BLOCSZ); |
| 408 | lua_rawseti(L, -2, 1); |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 409 | |
| 410 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref); |
| 411 | lua_setmetatable(L, -2); |
| 412 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 413 | return 1; |
| 414 | } |
| 415 | |
| 416 | static int concat_tostring(lua_State *L) |
| 417 | { |
| 418 | const void *ptr = lua_topointer(L, 1); |
| 419 | lua_pushfstring(L, "Concat object: %p", ptr); |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | static int hlua_concat_init(lua_State *L) |
| 424 | { |
| 425 | /* Creates the buffered concat object. */ |
| 426 | lua_newtable(L); |
| 427 | |
| 428 | lua_pushstring(L, "__tostring"); |
| 429 | lua_pushcclosure(L, concat_tostring, 0); |
| 430 | lua_settable(L, -3); |
| 431 | |
| 432 | lua_pushstring(L, "__index"); /* Creates the index entry. */ |
| 433 | lua_newtable(L); /* The "__index" content. */ |
| 434 | |
| 435 | lua_pushstring(L, "add"); |
| 436 | lua_pushcclosure(L, hlua_concat_add, 0); |
| 437 | lua_settable(L, -3); |
| 438 | |
| 439 | lua_pushstring(L, "dump"); |
| 440 | lua_pushcclosure(L, hlua_concat_dump, 0); |
| 441 | lua_settable(L, -3); |
| 442 | |
| 443 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 444 | class_concat_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 445 | |
| 446 | return 1; |
| 447 | } |
| 448 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 449 | int hlua_fcn_new_listener(lua_State *L, struct listener *lst) |
| 450 | { |
| 451 | lua_newtable(L); |
| 452 | |
| 453 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 454 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_listener_ref); |
| 455 | lua_setmetatable(L, -2); |
| 456 | |
| 457 | lua_pushlightuserdata(L, lst); |
| 458 | lua_rawseti(L, -2, 0); |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | static struct listener *hlua_check_listener(lua_State *L, int ud) |
| 463 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 464 | return hlua_checkudata(L, ud, class_listener_ref); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | int hlua_listener_get_stats(lua_State *L) |
| 468 | { |
| 469 | struct listener *li; |
| 470 | int i; |
| 471 | |
| 472 | li = hlua_check_listener(L, 1); |
| 473 | |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 474 | if (!li->bind_conf->frontend) { |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 475 | lua_pushnil(L); |
| 476 | return 1; |
| 477 | } |
| 478 | |
Willy Tarreau | c95bad5 | 2016-12-22 00:13:31 +0100 | [diff] [blame] | 479 | stats_fill_li_stats(li->bind_conf->frontend, li, ST_SHLGNDS, stats, STATS_LEN); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 480 | |
| 481 | lua_newtable(L); |
| 482 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
| 483 | lua_pushstring(L, stat_field_names[i]); |
| 484 | hlua_fcn_pushfield(L, &stats[i]); |
| 485 | lua_settable(L, -3); |
| 486 | } |
| 487 | return 1; |
| 488 | |
| 489 | } |
| 490 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 491 | int hlua_fcn_new_server(lua_State *L, struct server *srv) |
| 492 | { |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 493 | char buffer[12]; |
| 494 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 495 | lua_newtable(L); |
| 496 | |
| 497 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 498 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_server_ref); |
| 499 | lua_setmetatable(L, -2); |
| 500 | |
| 501 | lua_pushlightuserdata(L, srv); |
| 502 | lua_rawseti(L, -2, 0); |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 503 | |
| 504 | /* Add server name. */ |
| 505 | lua_pushstring(L, "name"); |
| 506 | lua_pushstring(L, srv->id); |
| 507 | lua_settable(L, -3); |
| 508 | |
| 509 | /* Add server puid. */ |
| 510 | lua_pushstring(L, "puid"); |
| 511 | snprintf(buffer, sizeof(buffer), "%d", srv->puid); |
| 512 | lua_pushstring(L, buffer); |
| 513 | lua_settable(L, -3); |
| 514 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 515 | return 1; |
| 516 | } |
| 517 | |
| 518 | static struct server *hlua_check_server(lua_State *L, int ud) |
| 519 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 520 | return hlua_checkudata(L, ud, class_server_ref); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | int hlua_server_get_stats(lua_State *L) |
| 524 | { |
| 525 | struct server *srv; |
| 526 | int i; |
| 527 | |
| 528 | srv = hlua_check_server(L, 1); |
| 529 | |
| 530 | if (!srv->proxy) { |
| 531 | lua_pushnil(L); |
| 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | stats_fill_sv_stats(srv->proxy, srv, ST_SHLGNDS, stats, STATS_LEN); |
| 536 | |
| 537 | lua_newtable(L); |
| 538 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
| 539 | lua_pushstring(L, stat_field_names[i]); |
| 540 | hlua_fcn_pushfield(L, &stats[i]); |
| 541 | lua_settable(L, -3); |
| 542 | } |
| 543 | return 1; |
| 544 | |
| 545 | } |
| 546 | |
| 547 | int hlua_server_get_addr(lua_State *L) |
| 548 | { |
| 549 | struct server *srv; |
| 550 | char addr[INET6_ADDRSTRLEN]; |
| 551 | luaL_Buffer b; |
| 552 | |
| 553 | srv = hlua_check_server(L, 1); |
| 554 | |
| 555 | luaL_buffinit(L, &b); |
| 556 | |
| 557 | switch (srv->addr.ss_family) { |
| 558 | case AF_INET: |
| 559 | inet_ntop(AF_INET, &((struct sockaddr_in *)&srv->addr)->sin_addr, |
| 560 | addr, INET_ADDRSTRLEN); |
| 561 | luaL_addstring(&b, addr); |
| 562 | luaL_addstring(&b, ":"); |
Nenad Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 563 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 564 | luaL_addstring(&b, addr); |
| 565 | break; |
| 566 | case AF_INET6: |
| 567 | inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr, |
Nenad Merdanovic | a9f0404 | 2017-07-23 22:04:59 -0400 | [diff] [blame] | 568 | addr, INET6_ADDRSTRLEN); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 569 | luaL_addstring(&b, addr); |
| 570 | luaL_addstring(&b, ":"); |
Nenad Merdanovic | 3849473 | 2017-07-23 22:04:58 -0400 | [diff] [blame] | 571 | snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 572 | luaL_addstring(&b, addr); |
| 573 | break; |
| 574 | case AF_UNIX: |
| 575 | luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path); |
| 576 | break; |
| 577 | default: |
| 578 | luaL_addstring(&b, "<unknown>"); |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | luaL_pushresult(&b); |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | int hlua_server_is_draining(lua_State *L) |
| 587 | { |
| 588 | struct server *srv; |
| 589 | |
| 590 | srv = hlua_check_server(L, 1); |
| 591 | lua_pushinteger(L, server_is_draining(srv)); |
| 592 | return 1; |
| 593 | } |
| 594 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 595 | int hlua_server_set_maxconn(lua_State *L) |
| 596 | { |
| 597 | struct server *srv; |
| 598 | const char *maxconn; |
| 599 | const char *err; |
| 600 | |
| 601 | srv = hlua_check_server(L, 1); |
| 602 | maxconn = luaL_checkstring(L, 2); |
| 603 | |
| 604 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 605 | err = server_parse_maxconn_change_request(srv, maxconn); |
| 606 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 607 | if (!err) |
| 608 | lua_pushnil(L); |
| 609 | else |
| 610 | hlua_pushstrippedstring(L, err); |
| 611 | return 1; |
| 612 | } |
| 613 | |
| 614 | int hlua_server_get_maxconn(lua_State *L) |
| 615 | { |
| 616 | struct server *srv; |
| 617 | |
| 618 | srv = hlua_check_server(L, 1); |
| 619 | lua_pushinteger(L, srv->maxconn); |
| 620 | return 1; |
| 621 | } |
| 622 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 623 | int hlua_server_set_weight(lua_State *L) |
| 624 | { |
| 625 | struct server *srv; |
| 626 | const char *weight; |
| 627 | const char *err; |
| 628 | |
| 629 | srv = hlua_check_server(L, 1); |
| 630 | weight = luaL_checkstring(L, 2); |
| 631 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 632 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 633 | err = server_parse_weight_change_request(srv, weight); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 634 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 635 | if (!err) |
| 636 | lua_pushnil(L); |
| 637 | else |
| 638 | hlua_pushstrippedstring(L, err); |
| 639 | return 1; |
| 640 | } |
| 641 | |
| 642 | int hlua_server_get_weight(lua_State *L) |
| 643 | { |
| 644 | struct server *srv; |
| 645 | |
| 646 | srv = hlua_check_server(L, 1); |
| 647 | lua_pushinteger(L, srv->uweight); |
| 648 | return 1; |
| 649 | } |
| 650 | |
| 651 | int hlua_server_set_addr(lua_State *L) |
| 652 | { |
| 653 | struct server *srv; |
| 654 | const char *addr; |
| 655 | const char *err; |
| 656 | |
| 657 | srv = hlua_check_server(L, 1); |
| 658 | addr = luaL_checkstring(L, 2); |
| 659 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 660 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 661 | err = server_parse_addr_change_request(srv, addr, "Lua script"); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 662 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 663 | if (!err) |
| 664 | lua_pushnil(L); |
| 665 | else |
| 666 | hlua_pushstrippedstring(L, err); |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | int hlua_server_shut_sess(lua_State *L) |
| 671 | { |
| 672 | struct server *srv; |
| 673 | |
| 674 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 675 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 676 | srv_shutdown_streams(srv, SF_ERR_KILLED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 677 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | int hlua_server_set_drain(lua_State *L) |
| 682 | { |
| 683 | struct server *srv; |
| 684 | |
| 685 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 686 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 687 | srv_adm_set_drain(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 688 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | int hlua_server_set_maint(lua_State *L) |
| 693 | { |
| 694 | struct server *srv; |
| 695 | |
| 696 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 697 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 698 | srv_adm_set_maint(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 699 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | int hlua_server_set_ready(lua_State *L) |
| 704 | { |
| 705 | struct server *srv; |
| 706 | |
| 707 | srv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 708 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 709 | srv_adm_set_ready(srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 710 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | int hlua_server_check_enable(lua_State *L) |
| 715 | { |
| 716 | struct server *sv; |
| 717 | |
| 718 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 719 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 720 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 721 | sv->check.state |= CHK_ST_ENABLED; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 722 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 723 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | int hlua_server_check_disable(lua_State *L) |
| 728 | { |
| 729 | struct server *sv; |
| 730 | |
| 731 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 732 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 733 | if (sv->check.state & CHK_ST_CONFIGURED) { |
Adis Nezirovic | ceee933 | 2017-07-26 09:19:06 +0200 | [diff] [blame] | 734 | sv->check.state &= ~CHK_ST_ENABLED; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 735 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 736 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | int hlua_server_check_force_up(lua_State *L) |
| 741 | { |
| 742 | struct server *sv; |
| 743 | |
| 744 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 745 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 746 | if (!(sv->track)) { |
| 747 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 748 | srv_set_running(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 749 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 750 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | int hlua_server_check_force_nolb(lua_State *L) |
| 755 | { |
| 756 | struct server *sv; |
| 757 | |
| 758 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 759 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 760 | if (!(sv->track)) { |
| 761 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 762 | srv_set_stopping(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 763 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 764 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | int hlua_server_check_force_down(lua_State *L) |
| 769 | { |
| 770 | struct server *sv; |
| 771 | |
| 772 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 773 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 774 | if (!(sv->track)) { |
| 775 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 776 | srv_set_stopped(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 777 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 778 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | int hlua_server_agent_enable(lua_State *L) |
| 783 | { |
| 784 | struct server *sv; |
| 785 | |
| 786 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 787 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 788 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 789 | sv->agent.state |= CHK_ST_ENABLED; |
| 790 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 791 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | int hlua_server_agent_disable(lua_State *L) |
| 796 | { |
| 797 | struct server *sv; |
| 798 | |
| 799 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 800 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 801 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 802 | sv->agent.state &= ~CHK_ST_ENABLED; |
| 803 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 804 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | int hlua_server_agent_force_up(lua_State *L) |
| 809 | { |
| 810 | struct server *sv; |
| 811 | |
| 812 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 813 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 814 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 815 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 816 | srv_set_running(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 817 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 818 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | int hlua_server_agent_force_down(lua_State *L) |
| 823 | { |
| 824 | struct server *sv; |
| 825 | |
| 826 | sv = hlua_check_server(L, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 827 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 828 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 829 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 830 | srv_set_stopped(sv, "changed from Lua script", NULL); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 831 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 832 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 836 | int hlua_fcn_new_proxy(lua_State *L, struct proxy *px) |
| 837 | { |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 838 | struct server *srv; |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 839 | struct listener *lst; |
| 840 | int lid; |
Willy Tarreau | 29d6980 | 2018-05-06 14:50:09 +0200 | [diff] [blame] | 841 | char buffer[17]; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 842 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 843 | lua_newtable(L); |
| 844 | |
| 845 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 846 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref); |
| 847 | lua_setmetatable(L, -2); |
| 848 | |
| 849 | lua_pushlightuserdata(L, px); |
| 850 | lua_rawseti(L, -2, 0); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 851 | |
Thierry FOURNIER | f2bbe38 | 2017-07-24 13:59:22 +0200 | [diff] [blame] | 852 | /* Add proxy name. */ |
| 853 | lua_pushstring(L, "name"); |
| 854 | lua_pushstring(L, px->id); |
| 855 | lua_settable(L, -3); |
| 856 | |
Baptiste Assmann | 46c7255 | 2017-10-26 21:51:58 +0200 | [diff] [blame] | 857 | /* Add proxy uuid. */ |
| 858 | lua_pushstring(L, "uuid"); |
| 859 | snprintf(buffer, sizeof(buffer), "%d", px->uuid); |
| 860 | lua_pushstring(L, buffer); |
| 861 | lua_settable(L, -3); |
| 862 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 863 | /* Browse and register servers. */ |
| 864 | lua_pushstring(L, "servers"); |
| 865 | lua_newtable(L); |
| 866 | for (srv = px->srv; srv; srv = srv->next) { |
| 867 | lua_pushstring(L, srv->id); |
| 868 | hlua_fcn_new_server(L, srv); |
| 869 | lua_settable(L, -3); |
| 870 | } |
| 871 | lua_settable(L, -3); |
| 872 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 873 | /* Browse and register listeners. */ |
| 874 | lua_pushstring(L, "listeners"); |
| 875 | lua_newtable(L); |
| 876 | lid = 1; |
| 877 | list_for_each_entry(lst, &px->conf.listeners, by_fe) { |
| 878 | if (lst->name) |
| 879 | lua_pushstring(L, lst->name); |
| 880 | else { |
Willy Tarreau | 29d6980 | 2018-05-06 14:50:09 +0200 | [diff] [blame] | 881 | snprintf(buffer, sizeof(buffer), "sock-%d", lid); |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 882 | lid++; |
| 883 | lua_pushstring(L, buffer); |
| 884 | } |
| 885 | hlua_fcn_new_listener(L, lst); |
| 886 | lua_settable(L, -3); |
| 887 | } |
| 888 | lua_settable(L, -3); |
| 889 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 890 | return 1; |
| 891 | } |
| 892 | |
| 893 | static struct proxy *hlua_check_proxy(lua_State *L, int ud) |
| 894 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 895 | return hlua_checkudata(L, ud, class_proxy_ref); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | int hlua_proxy_pause(lua_State *L) |
| 899 | { |
| 900 | struct proxy *px; |
| 901 | |
| 902 | px = hlua_check_proxy(L, 1); |
| 903 | pause_proxy(px); |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | int hlua_proxy_resume(lua_State *L) |
| 908 | { |
| 909 | struct proxy *px; |
| 910 | |
| 911 | px = hlua_check_proxy(L, 1); |
| 912 | resume_proxy(px); |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | int hlua_proxy_stop(lua_State *L) |
| 917 | { |
| 918 | struct proxy *px; |
| 919 | |
| 920 | px = hlua_check_proxy(L, 1); |
| 921 | stop_proxy(px); |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | int hlua_proxy_get_cap(lua_State *L) |
| 926 | { |
| 927 | struct proxy *px; |
| 928 | const char *str; |
| 929 | |
| 930 | px = hlua_check_proxy(L, 1); |
| 931 | str = proxy_cap_str(px->cap); |
| 932 | lua_pushstring(L, str); |
| 933 | return 1; |
| 934 | } |
| 935 | |
| 936 | int hlua_proxy_get_stats(lua_State *L) |
| 937 | { |
| 938 | struct proxy *px; |
| 939 | int i; |
| 940 | |
| 941 | px = hlua_check_proxy(L, 1); |
| 942 | if (px->cap & PR_CAP_BE) |
| 943 | stats_fill_be_stats(px, ST_SHLGNDS, stats, STATS_LEN); |
| 944 | else |
| 945 | stats_fill_fe_stats(px, stats, STATS_LEN); |
| 946 | lua_newtable(L); |
| 947 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
| 948 | lua_pushstring(L, stat_field_names[i]); |
| 949 | hlua_fcn_pushfield(L, &stats[i]); |
| 950 | lua_settable(L, -3); |
| 951 | } |
| 952 | return 1; |
| 953 | } |
| 954 | |
| 955 | int hlua_proxy_get_mode(lua_State *L) |
| 956 | { |
| 957 | struct proxy *px; |
| 958 | const char *str; |
| 959 | |
| 960 | px = hlua_check_proxy(L, 1); |
| 961 | str = proxy_mode_str(px->mode); |
| 962 | lua_pushstring(L, str); |
| 963 | return 1; |
| 964 | } |
| 965 | |
| 966 | int hlua_proxy_shut_bcksess(lua_State *L) |
| 967 | { |
| 968 | struct proxy *px; |
| 969 | |
| 970 | px = hlua_check_proxy(L, 1); |
| 971 | srv_shutdown_backup_streams(px, SF_ERR_KILLED); |
| 972 | return 0; |
| 973 | } |
| 974 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 975 | int hlua_fcn_post_init(lua_State *L) |
| 976 | { |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 977 | struct proxy *px; |
| 978 | |
| 979 | /* get core array. */ |
| 980 | if (lua_getglobal(L, "core") != LUA_TTABLE) |
| 981 | lua_error(L); |
| 982 | |
| 983 | /* Create proxies entry. */ |
| 984 | lua_pushstring(L, "proxies"); |
| 985 | lua_newtable(L); |
| 986 | |
| 987 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 988 | for (px = proxies_list; px; px = px->next) { |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 989 | lua_pushstring(L, px->id); |
| 990 | hlua_fcn_new_proxy(L, px); |
| 991 | lua_settable(L, -3); |
| 992 | } |
| 993 | |
| 994 | /* push "proxies" in "core" */ |
| 995 | lua_settable(L, -3); |
| 996 | |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 997 | /* Create proxies entry. */ |
| 998 | lua_pushstring(L, "frontends"); |
| 999 | lua_newtable(L); |
| 1000 | |
| 1001 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1002 | for (px = proxies_list; px; px = px->next) { |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1003 | if (!(px->cap & PR_CAP_FE)) |
| 1004 | continue; |
| 1005 | lua_pushstring(L, px->id); |
| 1006 | hlua_fcn_new_proxy(L, px); |
| 1007 | lua_settable(L, -3); |
| 1008 | } |
| 1009 | |
| 1010 | /* push "frontends" in "core" */ |
| 1011 | lua_settable(L, -3); |
| 1012 | |
| 1013 | /* Create proxies entry. */ |
| 1014 | lua_pushstring(L, "backends"); |
| 1015 | lua_newtable(L); |
| 1016 | |
| 1017 | /* List all proxies. */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1018 | for (px = proxies_list; px; px = px->next) { |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 1019 | if (!(px->cap & PR_CAP_BE)) |
| 1020 | continue; |
| 1021 | lua_pushstring(L, px->id); |
| 1022 | hlua_fcn_new_proxy(L, px); |
| 1023 | lua_settable(L, -3); |
| 1024 | } |
| 1025 | |
| 1026 | /* push "backend" in "core" */ |
| 1027 | lua_settable(L, -3); |
| 1028 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 1029 | return 1; |
| 1030 | } |
| 1031 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1032 | /* This Lua function take a string, a list of separators. |
| 1033 | * It tokenize the input string using the list of separators |
| 1034 | * as separator. |
| 1035 | * |
| 1036 | * The functionreturns a tablle filled with tokens. |
| 1037 | */ |
| 1038 | int hlua_tokenize(lua_State *L) |
| 1039 | { |
| 1040 | const char *str; |
| 1041 | const char *sep; |
| 1042 | int index; |
| 1043 | const char *token; |
| 1044 | const char *p; |
| 1045 | const char *c; |
| 1046 | int ignore_empty; |
| 1047 | |
| 1048 | ignore_empty = 0; |
| 1049 | |
| 1050 | str = luaL_checkstring(L, 1); |
| 1051 | sep = luaL_checkstring(L, 2); |
| 1052 | if (lua_gettop(L) == 3) |
| 1053 | ignore_empty = hlua_checkboolean(L, 3); |
| 1054 | |
| 1055 | lua_newtable(L); |
| 1056 | index = 1; |
| 1057 | token = str; |
| 1058 | p = str; |
| 1059 | while(1) { |
| 1060 | for (c = sep; *c != '\0'; c++) |
| 1061 | if (*p == *c) |
| 1062 | break; |
| 1063 | if (*p == *c) { |
| 1064 | if ((!ignore_empty) || (p - token > 0)) { |
| 1065 | lua_pushlstring(L, token, p - token); |
| 1066 | lua_rawseti(L, -2, index); |
| 1067 | index++; |
| 1068 | } |
| 1069 | token = p + 1; |
| 1070 | } |
| 1071 | if (*p == '\0') |
| 1072 | break; |
| 1073 | p++; |
| 1074 | } |
| 1075 | |
| 1076 | return 1; |
| 1077 | } |
| 1078 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1079 | int hlua_parse_addr(lua_State *L) |
| 1080 | { |
| 1081 | struct hlua_addr *addr; |
| 1082 | const char *str = luaL_checkstring(L, 1); |
| 1083 | unsigned char mask; |
| 1084 | |
| 1085 | addr = lua_newuserdata(L, sizeof(struct hlua_addr)); |
| 1086 | if (!addr) { |
| 1087 | lua_pushnil(L); |
| 1088 | return 1; |
| 1089 | } |
| 1090 | |
| 1091 | if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) { |
| 1092 | addr->type = AF_INET; |
| 1093 | return 1; |
| 1094 | } |
| 1095 | |
| 1096 | if (str62net(str, &addr->addr.v6.ip, &mask)) { |
| 1097 | len2mask6(mask, &addr->addr.v6.mask); |
| 1098 | addr->type = AF_INET6; |
| 1099 | return 1; |
| 1100 | } |
| 1101 | |
| 1102 | lua_pop(L, 1); |
| 1103 | lua_pushnil(L); |
| 1104 | return 1; |
| 1105 | } |
| 1106 | |
| 1107 | int hlua_match_addr(lua_State *L) |
| 1108 | { |
| 1109 | struct hlua_addr *addr1; |
| 1110 | struct hlua_addr *addr2; |
| 1111 | |
| 1112 | if (!lua_isuserdata(L, 1) || |
| 1113 | !lua_isuserdata(L, 2)) { |
| 1114 | lua_pushboolean(L, 0); |
| 1115 | return 1; |
| 1116 | } |
| 1117 | |
| 1118 | addr1 = lua_touserdata(L, 1); |
| 1119 | addr2 = lua_touserdata(L, 2); |
| 1120 | |
| 1121 | if (addr1->type != addr2->type) { |
| 1122 | lua_pushboolean(L, 0); |
| 1123 | return 1; |
| 1124 | } |
| 1125 | |
| 1126 | if (addr1->type == AF_INET) { |
| 1127 | if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) == |
| 1128 | (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) { |
| 1129 | lua_pushboolean(L, 1); |
| 1130 | return 1; |
| 1131 | } |
| 1132 | } else { |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 1133 | int i; |
| 1134 | |
| 1135 | for (i = 0; i < 16; i += 4) { |
| 1136 | if ((*(uint32_t *)&addr1->addr.v6.ip.s6_addr[i] & |
| 1137 | *(uint32_t *)&addr2->addr.v6.mask.s6_addr[i]) != |
| 1138 | (*(uint32_t *)&addr2->addr.v6.ip.s6_addr[i] & |
| 1139 | *(uint32_t *)&addr1->addr.v6.mask.s6_addr[i])) |
| 1140 | break; |
| 1141 | } |
| 1142 | if (i == 16) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1143 | lua_pushboolean(L, 1); |
| 1144 | return 1; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | lua_pushboolean(L, 0); |
| 1149 | return 1; |
| 1150 | } |
| 1151 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1152 | static struct my_regex *hlua_check_regex(lua_State *L, int ud) |
| 1153 | { |
| 1154 | return (hlua_checkudata(L, ud, class_regex_ref)); |
| 1155 | } |
| 1156 | |
| 1157 | static int hlua_regex_comp(struct lua_State *L) |
| 1158 | { |
| 1159 | struct my_regex *regex; |
| 1160 | const char *str; |
| 1161 | int cs; |
| 1162 | char *err; |
| 1163 | |
| 1164 | str = luaL_checkstring(L, 1); |
| 1165 | luaL_argcheck(L, lua_isboolean(L, 2), 2, NULL); |
| 1166 | cs = lua_toboolean(L, 2); |
| 1167 | |
| 1168 | regex = lua_newuserdata(L, sizeof(*regex)); |
| 1169 | |
| 1170 | err = NULL; |
| 1171 | if (!regex_comp(str, regex, cs, 1, &err)) { |
| 1172 | lua_pushboolean(L, 0); /* status error */ |
| 1173 | lua_pushstring(L, err); /* Reason */ |
| 1174 | free(err); |
| 1175 | return 2; |
| 1176 | } |
| 1177 | |
| 1178 | lua_pushboolean(L, 1); /* Status ok */ |
| 1179 | |
| 1180 | /* Create object */ |
| 1181 | lua_newtable(L); |
| 1182 | lua_pushvalue(L, -3); /* Get the userdata pointer. */ |
| 1183 | lua_rawseti(L, -2, 0); |
| 1184 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_regex_ref); |
| 1185 | lua_setmetatable(L, -2); |
| 1186 | return 2; |
| 1187 | } |
| 1188 | |
| 1189 | static int hlua_regex_exec(struct lua_State *L) |
| 1190 | { |
| 1191 | struct my_regex *regex; |
| 1192 | const char *str; |
| 1193 | size_t len; |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1194 | struct chunk *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1195 | |
| 1196 | regex = hlua_check_regex(L, 1); |
| 1197 | str = luaL_checklstring(L, 2, &len); |
| 1198 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1199 | /* Copy the string because regex_exec2 require a 'char *' |
| 1200 | * and not a 'const char *'. |
| 1201 | */ |
| 1202 | tmp = get_trash_chunk(); |
| 1203 | if (len >= tmp->size) { |
| 1204 | lua_pushboolean(L, 0); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | memcpy(tmp->str, str, len); |
| 1208 | |
| 1209 | lua_pushboolean(L, regex_exec2(regex, tmp->str, len)); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1210 | |
| 1211 | return 1; |
| 1212 | } |
| 1213 | |
| 1214 | static int hlua_regex_match(struct lua_State *L) |
| 1215 | { |
| 1216 | struct my_regex *regex; |
| 1217 | const char *str; |
| 1218 | size_t len; |
| 1219 | regmatch_t pmatch[20]; |
| 1220 | int ret; |
| 1221 | int i; |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1222 | struct chunk *tmp; |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1223 | |
| 1224 | regex = hlua_check_regex(L, 1); |
| 1225 | str = luaL_checklstring(L, 2, &len); |
| 1226 | |
Thierry FOURNIER | 7c210e6 | 2017-10-27 14:13:51 +0200 | [diff] [blame] | 1227 | /* Copy the string because regex_exec2 require a 'char *' |
| 1228 | * and not a 'const char *'. |
| 1229 | */ |
| 1230 | tmp = get_trash_chunk(); |
| 1231 | if (len >= tmp->size) { |
| 1232 | lua_pushboolean(L, 0); |
| 1233 | return 1; |
| 1234 | } |
| 1235 | memcpy(tmp->str, str, len); |
| 1236 | |
| 1237 | ret = regex_exec_match2(regex, tmp->str, len, 20, pmatch, 0); |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1238 | lua_pushboolean(L, ret); |
| 1239 | lua_newtable(L); |
| 1240 | if (ret) { |
| 1241 | for (i = 0; i < 20 && pmatch[i].rm_so != -1; i++) { |
| 1242 | lua_pushlstring(L, str + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so); |
| 1243 | lua_rawseti(L, -2, i + 1); |
| 1244 | } |
| 1245 | } |
| 1246 | return 2; |
| 1247 | } |
| 1248 | |
| 1249 | static int hlua_regex_free(struct lua_State *L) |
| 1250 | { |
| 1251 | struct my_regex *regex; |
| 1252 | |
| 1253 | regex = hlua_check_regex(L, 1); |
| 1254 | regex_free(regex); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1258 | int hlua_fcn_reg_core_fcn(lua_State *L) |
| 1259 | { |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1260 | if (!hlua_concat_init(L)) |
| 1261 | return 0; |
| 1262 | |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1263 | hlua_class_function(L, "now", hlua_now); |
| 1264 | hlua_class_function(L, "http_date", hlua_http_date); |
| 1265 | hlua_class_function(L, "imf_date", hlua_imf_date); |
| 1266 | hlua_class_function(L, "rfc850_date", hlua_rfc850_date); |
| 1267 | hlua_class_function(L, "asctime_date", hlua_asctime_date); |
| 1268 | hlua_class_function(L, "concat", hlua_concat_new); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 1269 | hlua_class_function(L, "get_info", hlua_get_info); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1270 | hlua_class_function(L, "parse_addr", hlua_parse_addr); |
| 1271 | hlua_class_function(L, "match_addr", hlua_match_addr); |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1272 | hlua_class_function(L, "tokenize", hlua_tokenize); |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1273 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 1274 | /* Create regex object. */ |
| 1275 | lua_newtable(L); |
| 1276 | hlua_class_function(L, "new", hlua_regex_comp); |
| 1277 | |
| 1278 | lua_newtable(L); /* The metatable. */ |
| 1279 | lua_pushstring(L, "__index"); |
| 1280 | lua_newtable(L); |
| 1281 | hlua_class_function(L, "exec", hlua_regex_exec); |
| 1282 | hlua_class_function(L, "match", hlua_regex_match); |
| 1283 | lua_rawset(L, -3); /* -> META["__index"] = TABLE */ |
| 1284 | hlua_class_function(L, "__gc", hlua_regex_free); |
| 1285 | |
| 1286 | lua_pushvalue(L, -1); /* Duplicate the metatable reference. */ |
| 1287 | class_regex_ref = hlua_register_metatable(L, CLASS_REGEX); |
| 1288 | |
| 1289 | lua_setmetatable(L, -2); |
| 1290 | lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */ |
| 1291 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1292 | /* Create listener object. */ |
| 1293 | lua_newtable(L); |
| 1294 | lua_pushstring(L, "__index"); |
| 1295 | lua_newtable(L); |
| 1296 | hlua_class_function(L, "get_stats", hlua_listener_get_stats); |
| 1297 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1298 | class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER); |
| 1299 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1300 | /* Create server object. */ |
| 1301 | lua_newtable(L); |
| 1302 | lua_pushstring(L, "__index"); |
| 1303 | lua_newtable(L); |
| 1304 | hlua_class_function(L, "is_draining", hlua_server_is_draining); |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1305 | hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn); |
| 1306 | hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1307 | hlua_class_function(L, "set_weight", hlua_server_set_weight); |
| 1308 | hlua_class_function(L, "get_weight", hlua_server_get_weight); |
| 1309 | hlua_class_function(L, "set_addr", hlua_server_set_addr); |
| 1310 | hlua_class_function(L, "get_addr", hlua_server_get_addr); |
| 1311 | hlua_class_function(L, "get_stats", hlua_server_get_stats); |
| 1312 | hlua_class_function(L, "shut_sess", hlua_server_shut_sess); |
| 1313 | hlua_class_function(L, "set_drain", hlua_server_set_drain); |
| 1314 | hlua_class_function(L, "set_maint", hlua_server_set_maint); |
| 1315 | hlua_class_function(L, "set_ready", hlua_server_set_ready); |
| 1316 | hlua_class_function(L, "check_enable", hlua_server_check_enable); |
| 1317 | hlua_class_function(L, "check_disable", hlua_server_check_disable); |
| 1318 | hlua_class_function(L, "check_force_up", hlua_server_check_force_up); |
| 1319 | hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb); |
| 1320 | hlua_class_function(L, "check_force_down", hlua_server_check_force_down); |
| 1321 | hlua_class_function(L, "agent_enable", hlua_server_agent_enable); |
| 1322 | hlua_class_function(L, "agent_disable", hlua_server_agent_disable); |
| 1323 | hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up); |
| 1324 | hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down); |
| 1325 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1326 | class_server_ref = hlua_register_metatable(L, CLASS_SERVER); |
| 1327 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1328 | /* Create proxy object. */ |
| 1329 | lua_newtable(L); |
| 1330 | lua_pushstring(L, "__index"); |
| 1331 | lua_newtable(L); |
| 1332 | hlua_class_function(L, "pause", hlua_proxy_pause); |
| 1333 | hlua_class_function(L, "resume", hlua_proxy_resume); |
| 1334 | hlua_class_function(L, "stop", hlua_proxy_stop); |
| 1335 | hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess); |
| 1336 | hlua_class_function(L, "get_cap", hlua_proxy_get_cap); |
| 1337 | hlua_class_function(L, "get_mode", hlua_proxy_get_mode); |
| 1338 | hlua_class_function(L, "get_stats", hlua_proxy_get_stats); |
| 1339 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1340 | class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY); |
| 1341 | |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 1342 | return 5; |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1343 | } |