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 | */ |
| 290 | time = timegm(&tm); |
| 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, ":"); |
| 548 | snprintf(addr, INET_ADDRSTRLEN, "%d", |
| 549 | ntohs(((struct sockaddr_in *)&srv->addr)->sin_port)); |
| 550 | luaL_addstring(&b, addr); |
| 551 | break; |
| 552 | case AF_INET6: |
| 553 | inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr, |
| 554 | addr, INET_ADDRSTRLEN); |
| 555 | luaL_addstring(&b, addr); |
| 556 | luaL_addstring(&b, ":"); |
| 557 | snprintf(addr, INET_ADDRSTRLEN, "%d", |
| 558 | ntohs(((struct sockaddr_in6 *)&srv->addr)->sin6_port)); |
| 559 | luaL_addstring(&b, addr); |
| 560 | break; |
| 561 | case AF_UNIX: |
| 562 | luaL_addstring(&b, (char *)((struct sockaddr_un *)&srv->addr)->sun_path); |
| 563 | break; |
| 564 | default: |
| 565 | luaL_addstring(&b, "<unknown>"); |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | luaL_pushresult(&b); |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | int hlua_server_is_draining(lua_State *L) |
| 574 | { |
| 575 | struct server *srv; |
| 576 | |
| 577 | srv = hlua_check_server(L, 1); |
| 578 | lua_pushinteger(L, server_is_draining(srv)); |
| 579 | return 1; |
| 580 | } |
| 581 | |
| 582 | int hlua_server_set_weight(lua_State *L) |
| 583 | { |
| 584 | struct server *srv; |
| 585 | const char *weight; |
| 586 | const char *err; |
| 587 | |
| 588 | srv = hlua_check_server(L, 1); |
| 589 | weight = luaL_checkstring(L, 2); |
| 590 | |
| 591 | err = server_parse_weight_change_request(srv, weight); |
| 592 | if (!err) |
| 593 | lua_pushnil(L); |
| 594 | else |
| 595 | hlua_pushstrippedstring(L, err); |
| 596 | return 1; |
| 597 | } |
| 598 | |
| 599 | int hlua_server_get_weight(lua_State *L) |
| 600 | { |
| 601 | struct server *srv; |
| 602 | |
| 603 | srv = hlua_check_server(L, 1); |
| 604 | lua_pushinteger(L, srv->uweight); |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | int hlua_server_set_addr(lua_State *L) |
| 609 | { |
| 610 | struct server *srv; |
| 611 | const char *addr; |
| 612 | const char *err; |
| 613 | |
| 614 | srv = hlua_check_server(L, 1); |
| 615 | addr = luaL_checkstring(L, 2); |
| 616 | |
| 617 | err = server_parse_addr_change_request(srv, addr, "Lua script"); |
| 618 | if (!err) |
| 619 | lua_pushnil(L); |
| 620 | else |
| 621 | hlua_pushstrippedstring(L, err); |
| 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | int hlua_server_shut_sess(lua_State *L) |
| 626 | { |
| 627 | struct server *srv; |
| 628 | |
| 629 | srv = hlua_check_server(L, 1); |
| 630 | srv_shutdown_streams(srv, SF_ERR_KILLED); |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | int hlua_server_set_drain(lua_State *L) |
| 635 | { |
| 636 | struct server *srv; |
| 637 | |
| 638 | srv = hlua_check_server(L, 1); |
| 639 | srv_adm_set_drain(srv); |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | int hlua_server_set_maint(lua_State *L) |
| 644 | { |
| 645 | struct server *srv; |
| 646 | |
| 647 | srv = hlua_check_server(L, 1); |
| 648 | srv_adm_set_maint(srv); |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | int hlua_server_set_ready(lua_State *L) |
| 653 | { |
| 654 | struct server *srv; |
| 655 | |
| 656 | srv = hlua_check_server(L, 1); |
| 657 | srv_adm_set_ready(srv); |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | int hlua_server_check_enable(lua_State *L) |
| 662 | { |
| 663 | struct server *sv; |
| 664 | |
| 665 | sv = hlua_check_server(L, 1); |
| 666 | if (sv->check.state & CHK_ST_CONFIGURED) { |
| 667 | sv->check.state &= ~CHK_ST_ENABLED; |
| 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | int hlua_server_check_disable(lua_State *L) |
| 673 | { |
| 674 | struct server *sv; |
| 675 | |
| 676 | sv = hlua_check_server(L, 1); |
| 677 | if (sv->check.state & CHK_ST_CONFIGURED) { |
| 678 | sv->check.state |= CHK_ST_ENABLED; |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | int hlua_server_check_force_up(lua_State *L) |
| 684 | { |
| 685 | struct server *sv; |
| 686 | |
| 687 | sv = hlua_check_server(L, 1); |
| 688 | if (!(sv->track)) { |
| 689 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
| 690 | srv_set_running(sv, "changed from Lua script"); |
| 691 | } |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | int hlua_server_check_force_nolb(lua_State *L) |
| 696 | { |
| 697 | struct server *sv; |
| 698 | |
| 699 | sv = hlua_check_server(L, 1); |
| 700 | if (!(sv->track)) { |
| 701 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
| 702 | srv_set_stopping(sv, "changed from Lua script"); |
| 703 | } |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | int hlua_server_check_force_down(lua_State *L) |
| 708 | { |
| 709 | struct server *sv; |
| 710 | |
| 711 | sv = hlua_check_server(L, 1); |
| 712 | if (!(sv->track)) { |
| 713 | sv->check.health = 0; |
| 714 | srv_set_stopped(sv, "changed from Lua script"); |
| 715 | } |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | int hlua_server_agent_enable(lua_State *L) |
| 720 | { |
| 721 | struct server *sv; |
| 722 | |
| 723 | sv = hlua_check_server(L, 1); |
| 724 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 725 | sv->agent.state |= CHK_ST_ENABLED; |
| 726 | } |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | int hlua_server_agent_disable(lua_State *L) |
| 731 | { |
| 732 | struct server *sv; |
| 733 | |
| 734 | sv = hlua_check_server(L, 1); |
| 735 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 736 | sv->agent.state &= ~CHK_ST_ENABLED; |
| 737 | } |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | int hlua_server_agent_force_up(lua_State *L) |
| 742 | { |
| 743 | struct server *sv; |
| 744 | |
| 745 | sv = hlua_check_server(L, 1); |
| 746 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 747 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
| 748 | srv_set_running(sv, "changed from Lua script"); |
| 749 | } |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | int hlua_server_agent_force_down(lua_State *L) |
| 754 | { |
| 755 | struct server *sv; |
| 756 | |
| 757 | sv = hlua_check_server(L, 1); |
| 758 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 759 | sv->agent.health = 0; |
| 760 | srv_set_stopped(sv, "changed from Lua script"); |
| 761 | } |
| 762 | return 0; |
| 763 | } |
| 764 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 765 | int hlua_fcn_new_proxy(lua_State *L, struct proxy *px) |
| 766 | { |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 767 | struct server *srv; |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 768 | struct listener *lst; |
| 769 | int lid; |
| 770 | char buffer[10]; |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 771 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 772 | lua_newtable(L); |
| 773 | |
| 774 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 775 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_proxy_ref); |
| 776 | lua_setmetatable(L, -2); |
| 777 | |
| 778 | lua_pushlightuserdata(L, px); |
| 779 | lua_rawseti(L, -2, 0); |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 780 | |
| 781 | /* Browse and register servers. */ |
| 782 | lua_pushstring(L, "servers"); |
| 783 | lua_newtable(L); |
| 784 | for (srv = px->srv; srv; srv = srv->next) { |
| 785 | lua_pushstring(L, srv->id); |
| 786 | hlua_fcn_new_server(L, srv); |
| 787 | lua_settable(L, -3); |
| 788 | } |
| 789 | lua_settable(L, -3); |
| 790 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 791 | /* Browse and register listeners. */ |
| 792 | lua_pushstring(L, "listeners"); |
| 793 | lua_newtable(L); |
| 794 | lid = 1; |
| 795 | list_for_each_entry(lst, &px->conf.listeners, by_fe) { |
| 796 | if (lst->name) |
| 797 | lua_pushstring(L, lst->name); |
| 798 | else { |
| 799 | snprintf(buffer, 10, "sock-%d", lid); |
| 800 | lid++; |
| 801 | lua_pushstring(L, buffer); |
| 802 | } |
| 803 | hlua_fcn_new_listener(L, lst); |
| 804 | lua_settable(L, -3); |
| 805 | } |
| 806 | lua_settable(L, -3); |
| 807 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 808 | return 1; |
| 809 | } |
| 810 | |
| 811 | static struct proxy *hlua_check_proxy(lua_State *L, int ud) |
| 812 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 813 | return hlua_checkudata(L, ud, class_proxy_ref); |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | int hlua_proxy_pause(lua_State *L) |
| 817 | { |
| 818 | struct proxy *px; |
| 819 | |
| 820 | px = hlua_check_proxy(L, 1); |
| 821 | pause_proxy(px); |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | int hlua_proxy_resume(lua_State *L) |
| 826 | { |
| 827 | struct proxy *px; |
| 828 | |
| 829 | px = hlua_check_proxy(L, 1); |
| 830 | resume_proxy(px); |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | int hlua_proxy_stop(lua_State *L) |
| 835 | { |
| 836 | struct proxy *px; |
| 837 | |
| 838 | px = hlua_check_proxy(L, 1); |
| 839 | stop_proxy(px); |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | int hlua_proxy_get_cap(lua_State *L) |
| 844 | { |
| 845 | struct proxy *px; |
| 846 | const char *str; |
| 847 | |
| 848 | px = hlua_check_proxy(L, 1); |
| 849 | str = proxy_cap_str(px->cap); |
| 850 | lua_pushstring(L, str); |
| 851 | return 1; |
| 852 | } |
| 853 | |
| 854 | int hlua_proxy_get_stats(lua_State *L) |
| 855 | { |
| 856 | struct proxy *px; |
| 857 | int i; |
| 858 | |
| 859 | px = hlua_check_proxy(L, 1); |
| 860 | if (px->cap & PR_CAP_BE) |
| 861 | stats_fill_be_stats(px, ST_SHLGNDS, stats, STATS_LEN); |
| 862 | else |
| 863 | stats_fill_fe_stats(px, stats, STATS_LEN); |
| 864 | lua_newtable(L); |
| 865 | for (i=0; i<ST_F_TOTAL_FIELDS; i++) { |
| 866 | lua_pushstring(L, stat_field_names[i]); |
| 867 | hlua_fcn_pushfield(L, &stats[i]); |
| 868 | lua_settable(L, -3); |
| 869 | } |
| 870 | return 1; |
| 871 | } |
| 872 | |
| 873 | int hlua_proxy_get_mode(lua_State *L) |
| 874 | { |
| 875 | struct proxy *px; |
| 876 | const char *str; |
| 877 | |
| 878 | px = hlua_check_proxy(L, 1); |
| 879 | str = proxy_mode_str(px->mode); |
| 880 | lua_pushstring(L, str); |
| 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | int hlua_proxy_shut_bcksess(lua_State *L) |
| 885 | { |
| 886 | struct proxy *px; |
| 887 | |
| 888 | px = hlua_check_proxy(L, 1); |
| 889 | srv_shutdown_backup_streams(px, SF_ERR_KILLED); |
| 890 | return 0; |
| 891 | } |
| 892 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 893 | int hlua_fcn_post_init(lua_State *L) |
| 894 | { |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 895 | struct proxy *px; |
| 896 | |
| 897 | /* get core array. */ |
| 898 | if (lua_getglobal(L, "core") != LUA_TTABLE) |
| 899 | lua_error(L); |
| 900 | |
| 901 | /* Create proxies entry. */ |
| 902 | lua_pushstring(L, "proxies"); |
| 903 | lua_newtable(L); |
| 904 | |
| 905 | /* List all proxies. */ |
| 906 | for (px = proxy; px; px = px->next) { |
| 907 | lua_pushstring(L, px->id); |
| 908 | hlua_fcn_new_proxy(L, px); |
| 909 | lua_settable(L, -3); |
| 910 | } |
| 911 | |
| 912 | /* push "proxies" in "core" */ |
| 913 | lua_settable(L, -3); |
| 914 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 915 | return 1; |
| 916 | } |
| 917 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 918 | /* This Lua function take a string, a list of separators. |
| 919 | * It tokenize the input string using the list of separators |
| 920 | * as separator. |
| 921 | * |
| 922 | * The functionreturns a tablle filled with tokens. |
| 923 | */ |
| 924 | int hlua_tokenize(lua_State *L) |
| 925 | { |
| 926 | const char *str; |
| 927 | const char *sep; |
| 928 | int index; |
| 929 | const char *token; |
| 930 | const char *p; |
| 931 | const char *c; |
| 932 | int ignore_empty; |
| 933 | |
| 934 | ignore_empty = 0; |
| 935 | |
| 936 | str = luaL_checkstring(L, 1); |
| 937 | sep = luaL_checkstring(L, 2); |
| 938 | if (lua_gettop(L) == 3) |
| 939 | ignore_empty = hlua_checkboolean(L, 3); |
| 940 | |
| 941 | lua_newtable(L); |
| 942 | index = 1; |
| 943 | token = str; |
| 944 | p = str; |
| 945 | while(1) { |
| 946 | for (c = sep; *c != '\0'; c++) |
| 947 | if (*p == *c) |
| 948 | break; |
| 949 | if (*p == *c) { |
| 950 | if ((!ignore_empty) || (p - token > 0)) { |
| 951 | lua_pushlstring(L, token, p - token); |
| 952 | lua_rawseti(L, -2, index); |
| 953 | index++; |
| 954 | } |
| 955 | token = p + 1; |
| 956 | } |
| 957 | if (*p == '\0') |
| 958 | break; |
| 959 | p++; |
| 960 | } |
| 961 | |
| 962 | return 1; |
| 963 | } |
| 964 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 965 | int hlua_parse_addr(lua_State *L) |
| 966 | { |
| 967 | struct hlua_addr *addr; |
| 968 | const char *str = luaL_checkstring(L, 1); |
| 969 | unsigned char mask; |
| 970 | |
| 971 | addr = lua_newuserdata(L, sizeof(struct hlua_addr)); |
| 972 | if (!addr) { |
| 973 | lua_pushnil(L); |
| 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) { |
| 978 | addr->type = AF_INET; |
| 979 | return 1; |
| 980 | } |
| 981 | |
| 982 | if (str62net(str, &addr->addr.v6.ip, &mask)) { |
| 983 | len2mask6(mask, &addr->addr.v6.mask); |
| 984 | addr->type = AF_INET6; |
| 985 | return 1; |
| 986 | } |
| 987 | |
| 988 | lua_pop(L, 1); |
| 989 | lua_pushnil(L); |
| 990 | return 1; |
| 991 | } |
| 992 | |
| 993 | int hlua_match_addr(lua_State *L) |
| 994 | { |
| 995 | struct hlua_addr *addr1; |
| 996 | struct hlua_addr *addr2; |
| 997 | |
| 998 | if (!lua_isuserdata(L, 1) || |
| 999 | !lua_isuserdata(L, 2)) { |
| 1000 | lua_pushboolean(L, 0); |
| 1001 | return 1; |
| 1002 | } |
| 1003 | |
| 1004 | addr1 = lua_touserdata(L, 1); |
| 1005 | addr2 = lua_touserdata(L, 2); |
| 1006 | |
| 1007 | if (addr1->type != addr2->type) { |
| 1008 | lua_pushboolean(L, 0); |
| 1009 | return 1; |
| 1010 | } |
| 1011 | |
| 1012 | if (addr1->type == AF_INET) { |
| 1013 | if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) == |
| 1014 | (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) { |
| 1015 | lua_pushboolean(L, 1); |
| 1016 | return 1; |
| 1017 | } |
| 1018 | } else { |
Thierry FOURNIER | de6925e | 2016-12-23 17:03:25 +0100 | [diff] [blame] | 1019 | int i; |
| 1020 | |
| 1021 | for (i = 0; i < 16; i += 4) { |
| 1022 | if ((*(uint32_t *)&addr1->addr.v6.ip.s6_addr[i] & |
| 1023 | *(uint32_t *)&addr2->addr.v6.mask.s6_addr[i]) != |
| 1024 | (*(uint32_t *)&addr2->addr.v6.ip.s6_addr[i] & |
| 1025 | *(uint32_t *)&addr1->addr.v6.mask.s6_addr[i])) |
| 1026 | break; |
| 1027 | } |
| 1028 | if (i == 16) { |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1029 | lua_pushboolean(L, 1); |
| 1030 | return 1; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | lua_pushboolean(L, 0); |
| 1035 | return 1; |
| 1036 | } |
| 1037 | |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1038 | int hlua_fcn_reg_core_fcn(lua_State *L) |
| 1039 | { |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1040 | if (!hlua_concat_init(L)) |
| 1041 | return 0; |
| 1042 | |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1043 | hlua_class_function(L, "now", hlua_now); |
| 1044 | hlua_class_function(L, "http_date", hlua_http_date); |
| 1045 | hlua_class_function(L, "imf_date", hlua_imf_date); |
| 1046 | hlua_class_function(L, "rfc850_date", hlua_rfc850_date); |
| 1047 | hlua_class_function(L, "asctime_date", hlua_asctime_date); |
| 1048 | hlua_class_function(L, "concat", hlua_concat_new); |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 1049 | hlua_class_function(L, "get_info", hlua_get_info); |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 1050 | hlua_class_function(L, "parse_addr", hlua_parse_addr); |
| 1051 | hlua_class_function(L, "match_addr", hlua_match_addr); |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 1052 | hlua_class_function(L, "tokenize", hlua_tokenize); |
Thierry Fournier | 4f99b27 | 2016-02-22 08:40:02 +0100 | [diff] [blame] | 1053 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1054 | /* Create listener object. */ |
| 1055 | lua_newtable(L); |
| 1056 | lua_pushstring(L, "__index"); |
| 1057 | lua_newtable(L); |
| 1058 | hlua_class_function(L, "get_stats", hlua_listener_get_stats); |
| 1059 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1060 | class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER); |
| 1061 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1062 | /* Create server object. */ |
| 1063 | lua_newtable(L); |
| 1064 | lua_pushstring(L, "__index"); |
| 1065 | lua_newtable(L); |
| 1066 | hlua_class_function(L, "is_draining", hlua_server_is_draining); |
| 1067 | hlua_class_function(L, "set_weight", hlua_server_set_weight); |
| 1068 | hlua_class_function(L, "get_weight", hlua_server_get_weight); |
| 1069 | hlua_class_function(L, "set_addr", hlua_server_set_addr); |
| 1070 | hlua_class_function(L, "get_addr", hlua_server_get_addr); |
| 1071 | hlua_class_function(L, "get_stats", hlua_server_get_stats); |
| 1072 | hlua_class_function(L, "shut_sess", hlua_server_shut_sess); |
| 1073 | hlua_class_function(L, "set_drain", hlua_server_set_drain); |
| 1074 | hlua_class_function(L, "set_maint", hlua_server_set_maint); |
| 1075 | hlua_class_function(L, "set_ready", hlua_server_set_ready); |
| 1076 | hlua_class_function(L, "check_enable", hlua_server_check_enable); |
| 1077 | hlua_class_function(L, "check_disable", hlua_server_check_disable); |
| 1078 | hlua_class_function(L, "check_force_up", hlua_server_check_force_up); |
| 1079 | hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb); |
| 1080 | hlua_class_function(L, "check_force_down", hlua_server_check_force_down); |
| 1081 | hlua_class_function(L, "agent_enable", hlua_server_agent_enable); |
| 1082 | hlua_class_function(L, "agent_disable", hlua_server_agent_disable); |
| 1083 | hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up); |
| 1084 | hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down); |
| 1085 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1086 | class_server_ref = hlua_register_metatable(L, CLASS_SERVER); |
| 1087 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1088 | /* Create proxy object. */ |
| 1089 | lua_newtable(L); |
| 1090 | lua_pushstring(L, "__index"); |
| 1091 | lua_newtable(L); |
| 1092 | hlua_class_function(L, "pause", hlua_proxy_pause); |
| 1093 | hlua_class_function(L, "resume", hlua_proxy_resume); |
| 1094 | hlua_class_function(L, "stop", hlua_proxy_stop); |
| 1095 | hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess); |
| 1096 | hlua_class_function(L, "get_cap", hlua_proxy_get_cap); |
| 1097 | hlua_class_function(L, "get_mode", hlua_proxy_get_mode); |
| 1098 | hlua_class_function(L, "get_stats", hlua_proxy_get_stats); |
| 1099 | lua_settable(L, -3); /* -> META["__index"] = TABLE */ |
| 1100 | class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY); |
| 1101 | |
Thierry Fournier | 1550d5d | 2016-01-21 09:35:41 +0100 | [diff] [blame] | 1102 | return 5; |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 1103 | } |