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