blob: 5ac533a00126834d7796d9c3a020cf01fdce6ecd [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
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 Fournierfb0b5462016-01-21 09:28:58 +010013 * 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 Fournierb1f46562016-01-21 09:46:15 +010021#include <common/time.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010022#include <common/uri_auth.h>
Thierry Fournierb1f46562016-01-21 09:46:15 +010023
William Lallemand9ed62032016-11-21 17:49:11 +010024#include <types/cli.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010025#include <types/hlua.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010026#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010027#include <types/stats.h>
Thierry Fournier49d48422016-02-19 12:09:29 +010028
Thierry Fournier94ed1c12016-02-24 08:06:32 +010029#include <proto/proto_http.h>
Thierry Fournierf61aa632016-02-19 20:56:00 +010030#include <proto/proxy.h>
31#include <proto/server.h>
William Lallemand9ed62032016-11-21 17:49:11 +010032#include <proto/stats.h>
Thierry Fournier94ed1c12016-02-24 08:06:32 +010033
Thierry Fournier1de16592016-01-27 09:49:07 +010034/* Contains the class reference of the concat object. */
35static int class_concat_ref;
Thierry Fournierf61aa632016-02-19 20:56:00 +010036static int class_proxy_ref;
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010037static int class_server_ref;
Thierry Fournierff480422016-02-25 08:36:46 +010038static int class_listener_ref;
Thierry Fournier1de16592016-01-27 09:49:07 +010039
Thierry Fournierf61aa632016-02-19 20:56:00 +010040#define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
Thierry Fourniereea77c02016-03-18 08:47:13 +010041
42static struct field stats[STATS_LEN];
43
Thierry FOURNIER / OZON.IO7f3aa8b2016-11-24 20:37:38 +010044int 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 Fournier8b0d6e12016-03-16 18:29:13 +010051/* This function gets a struct field and convert it in Lua
52 * variable. The variable is pushed at the top of the stak.
53 */
54int 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 Fournier94ed1c12016-02-24 08:06:32 +0100128/* 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 */
134const 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 Fournierddd89882016-02-22 19:52:08 +0100145/* 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 */
153void hlua_class_const_int(lua_State *L, const char *name, int value)
154{
Thierry Fournierddd89882016-02-22 19:52:08 +0100155 lua_pushstring(L, name);
156 lua_pushinteger(L, value);
157 lua_rawset(L, -3);
158}
159void hlua_class_const_str(lua_State *L, const char *name, const char *value)
160{
Thierry Fournierddd89882016-02-22 19:52:08 +0100161 lua_pushstring(L, name);
162 lua_pushstring(L, value);
163 lua_rawset(L, -3);
164}
165void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L))
166{
Thierry Fournierddd89882016-02-22 19:52:08 +0100167 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. */
173int 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 Fournier45e78d72016-02-19 18:34:46 +0100180/* 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 */
185int 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 Fournier9e7e3ea2016-01-27 09:55:30 +0100215/* Return an object of the expected type, or throws an error. */
216void *hlua_checkudata(lua_State *L, int ud, int class_ref)
217{
218 void *p;
Thierry Fournier53518272016-01-27 10:34:09 +0100219 int ret;
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100220
221 /* Check if the stack entry is an array. */
222 if (!lua_istable(L, ud))
Thierry Fournier53518272016-01-27 10:34:09 +0100223 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 Fournier9e7e3ea2016-01-27 09:55:30 +0100232 /* Check if the metadata have the expected type. */
Thierry Fournier53518272016-01-27 10:34:09 +0100233 ret = lua_rawequal(L, -1, -2);
234 lua_pop(L, 2);
235 if (!ret)
236 luaL_argerror(L, ud, NULL);
237
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100238 /* Push on the stack at the entry [0] of the table. */
239 lua_rawgeti(L, ud, 0);
Thierry Fournier53518272016-01-27 10:34:09 +0100240
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100241 /* Check if this entry is userdata. */
242 p = lua_touserdata(L, -1);
243 if (!p)
Thierry Fournier53518272016-01-27 10:34:09 +0100244 luaL_argerror(L, ud, NULL);
245
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100246 /* Remove the entry returned by lua_rawgeti(). */
247 lua_pop(L, 1);
Thierry Fournier53518272016-01-27 10:34:09 +0100248
Thierry Fournier9e7e3ea2016-01-27 09:55:30 +0100249 /* Return the associated struct. */
250 return p;
251}
252
Thierry Fournierb1f46562016-01-21 09:46:15 +0100253/* This function return the current date at epoch format in milliseconds. */
254int 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 Fournier1550d5d2016-01-21 09:35:41 +0100266/* 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 */
270static 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}
299static int hlua_http_date(lua_State *L)
300{
301 return hlua_parse_date(L, parse_http_date);
302}
303static int hlua_imf_date(lua_State *L)
304{
305 return hlua_parse_date(L, parse_imf_date);
306}
307static int hlua_rfc850_date(lua_State *L)
308{
309 return hlua_parse_date(L, parse_rfc850_date);
310}
311static int hlua_asctime_date(lua_State *L)
312{
313 return hlua_parse_date(L, parse_asctime_date);
314}
315
Thierry Fourniereea77c02016-03-18 08:47:13 +0100316static 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 Fournier49d48422016-02-19 12:09:29 +0100331static struct hlua_concat *hlua_check_concat(lua_State *L, int ud)
Thierry Fournier1de16592016-01-27 09:49:07 +0100332{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200333 return (hlua_checkudata(L, ud, class_concat_ref));
Thierry Fournier1de16592016-01-27 09:49:07 +0100334}
335
336static int hlua_concat_add(lua_State *L)
337{
Thierry Fournier49d48422016-02-19 12:09:29 +0100338 struct hlua_concat *b;
339 char *buffer;
340 char *new;
Thierry Fournier1de16592016-01-27 09:49:07 +0100341 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 Fournier49d48422016-02-19 12:09:29 +0100350 /* 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 Fournier1de16592016-01-27 09:49:07 +0100376 return 0;
377}
378
379static int hlua_concat_dump(lua_State *L)
380{
Thierry Fournier49d48422016-02-19 12:09:29 +0100381 struct hlua_concat *b;
382 char *buffer;
Thierry Fournier1de16592016-01-27 09:49:07 +0100383
384 /* First arg must be a concat object. */
385 b = hlua_check_concat(L, 1);
386
Thierry Fournier49d48422016-02-19 12:09:29 +0100387 /* Get the buffer. */
388 lua_rawgeti(L, 1, 1);
389 buffer = lua_touserdata(L, -1);
390 lua_pop(L, 1);
391
Thierry Fournier1de16592016-01-27 09:49:07 +0100392 /* Push the soncatenated strng in the stack. */
Thierry Fournier49d48422016-02-19 12:09:29 +0100393 lua_pushlstring(L, buffer, b->len);
Thierry Fournier1de16592016-01-27 09:49:07 +0100394 return 1;
395}
396
397int hlua_concat_new(lua_State *L)
398{
Thierry Fournier49d48422016-02-19 12:09:29 +0100399 struct hlua_concat *b;
Thierry Fournier1de16592016-01-27 09:49:07 +0100400
401 lua_newtable(L);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200402 b = lua_newuserdata(L, sizeof(*b));
Thierry Fournier49d48422016-02-19 12:09:29 +0100403 b->size = HLUA_CONCAT_BLOCSZ;
404 b->len = 0;
Thierry Fournier1de16592016-01-27 09:49:07 +0100405 lua_rawseti(L, -2, 0);
Thierry Fournier49d48422016-02-19 12:09:29 +0100406 lua_newuserdata(L, HLUA_CONCAT_BLOCSZ);
407 lua_rawseti(L, -2, 1);
Thierry Fournier1de16592016-01-27 09:49:07 +0100408
409 lua_rawgeti(L, LUA_REGISTRYINDEX, class_concat_ref);
410 lua_setmetatable(L, -2);
411
Thierry Fournier1de16592016-01-27 09:49:07 +0100412 return 1;
413}
414
415static 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
422static 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 Fournierff480422016-02-25 08:36:46 +0100448int 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
461static struct listener *hlua_check_listener(lua_State *L, int ud)
462{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200463 return hlua_checkudata(L, ud, class_listener_ref);
Thierry Fournierff480422016-02-25 08:36:46 +0100464}
465
466int hlua_listener_get_stats(lua_State *L)
467{
468 struct listener *li;
469 int i;
470
471 li = hlua_check_listener(L, 1);
472
473 if (!li->frontend) {
474 lua_pushnil(L);
475 return 1;
476 }
477
478 stats_fill_li_stats(li->frontend, li, ST_SHLGNDS, stats, STATS_LEN);
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 Fournierf2fdc9d2016-02-22 08:21:39 +0100490int 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
503static struct server *hlua_check_server(lua_State *L, int ud)
504{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200505 return hlua_checkudata(L, ud, class_server_ref);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100506}
507
508int 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
532int 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
573int 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
582int 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
599int 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
608int 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
625int 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
634int 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
643int 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
652int 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
661int 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
672int 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
683int 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
695int 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
707int 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
719int 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
730int 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
741int 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
753int 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 Fournierf61aa632016-02-19 20:56:00 +0100765int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
766{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100767 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +0100768 struct listener *lst;
769 int lid;
770 char buffer[10];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100771
Thierry Fournierf61aa632016-02-19 20:56:00 +0100772 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 Fournierf2fdc9d2016-02-22 08:21:39 +0100780
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 Fournierff480422016-02-25 08:36:46 +0100791 /* 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 Fournierf61aa632016-02-19 20:56:00 +0100808 return 1;
809}
810
811static struct proxy *hlua_check_proxy(lua_State *L, int ud)
812{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200813 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +0100814}
815
816int 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
825int 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
834int 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
843int 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
854int 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
873int 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
884int 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 Fournier3d4a6752016-02-19 20:53:30 +0100893int hlua_fcn_post_init(lua_State *L)
894{
Thierry Fournierf61aa632016-02-19 20:56:00 +0100895 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 Fournier3d4a6752016-02-19 20:53:30 +0100915 return 1;
916}
917
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100918/* 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 */
924int 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.IO62fec752016-11-10 20:38:11 +0100965int 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
993int 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 {
1019 if (((addr1->addr.v6.ip.s6_addr32[0] & addr2->addr.v6.mask.s6_addr32[0]) ==
1020 (addr2->addr.v6.ip.s6_addr32[0] & addr1->addr.v6.mask.s6_addr32[0])) &&
1021 ((addr1->addr.v6.ip.s6_addr32[1] & addr2->addr.v6.mask.s6_addr32[1]) ==
1022 (addr2->addr.v6.ip.s6_addr32[1] & addr1->addr.v6.mask.s6_addr32[1])) &&
1023 ((addr1->addr.v6.ip.s6_addr32[2] & addr2->addr.v6.mask.s6_addr32[2]) ==
1024 (addr2->addr.v6.ip.s6_addr32[2] & addr1->addr.v6.mask.s6_addr32[2])) &&
1025 ((addr1->addr.v6.ip.s6_addr32[3] & addr2->addr.v6.mask.s6_addr32[3]) ==
1026 (addr2->addr.v6.ip.s6_addr32[3] & addr1->addr.v6.mask.s6_addr32[3]))) {
1027 lua_pushboolean(L, 1);
1028 return 1;
1029 }
1030 }
1031
1032 lua_pushboolean(L, 0);
1033 return 1;
1034}
1035
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001036int hlua_fcn_reg_core_fcn(lua_State *L)
1037{
Thierry Fournier1de16592016-01-27 09:49:07 +01001038 if (!hlua_concat_init(L))
1039 return 0;
1040
Thierry Fournier4f99b272016-02-22 08:40:02 +01001041 hlua_class_function(L, "now", hlua_now);
1042 hlua_class_function(L, "http_date", hlua_http_date);
1043 hlua_class_function(L, "imf_date", hlua_imf_date);
1044 hlua_class_function(L, "rfc850_date", hlua_rfc850_date);
1045 hlua_class_function(L, "asctime_date", hlua_asctime_date);
1046 hlua_class_function(L, "concat", hlua_concat_new);
Thierry Fourniereea77c02016-03-18 08:47:13 +01001047 hlua_class_function(L, "get_info", hlua_get_info);
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001048 hlua_class_function(L, "parse_addr", hlua_parse_addr);
1049 hlua_class_function(L, "match_addr", hlua_match_addr);
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +01001050 hlua_class_function(L, "tokenize", hlua_tokenize);
Thierry Fournier4f99b272016-02-22 08:40:02 +01001051
Thierry Fournierff480422016-02-25 08:36:46 +01001052 /* Create listener object. */
1053 lua_newtable(L);
1054 lua_pushstring(L, "__index");
1055 lua_newtable(L);
1056 hlua_class_function(L, "get_stats", hlua_listener_get_stats);
1057 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1058 class_listener_ref = hlua_register_metatable(L, CLASS_LISTENER);
1059
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001060 /* Create server object. */
1061 lua_newtable(L);
1062 lua_pushstring(L, "__index");
1063 lua_newtable(L);
1064 hlua_class_function(L, "is_draining", hlua_server_is_draining);
1065 hlua_class_function(L, "set_weight", hlua_server_set_weight);
1066 hlua_class_function(L, "get_weight", hlua_server_get_weight);
1067 hlua_class_function(L, "set_addr", hlua_server_set_addr);
1068 hlua_class_function(L, "get_addr", hlua_server_get_addr);
1069 hlua_class_function(L, "get_stats", hlua_server_get_stats);
1070 hlua_class_function(L, "shut_sess", hlua_server_shut_sess);
1071 hlua_class_function(L, "set_drain", hlua_server_set_drain);
1072 hlua_class_function(L, "set_maint", hlua_server_set_maint);
1073 hlua_class_function(L, "set_ready", hlua_server_set_ready);
1074 hlua_class_function(L, "check_enable", hlua_server_check_enable);
1075 hlua_class_function(L, "check_disable", hlua_server_check_disable);
1076 hlua_class_function(L, "check_force_up", hlua_server_check_force_up);
1077 hlua_class_function(L, "check_force_nolb", hlua_server_check_force_nolb);
1078 hlua_class_function(L, "check_force_down", hlua_server_check_force_down);
1079 hlua_class_function(L, "agent_enable", hlua_server_agent_enable);
1080 hlua_class_function(L, "agent_disable", hlua_server_agent_disable);
1081 hlua_class_function(L, "agent_force_up", hlua_server_agent_force_up);
1082 hlua_class_function(L, "agent_force_down", hlua_server_agent_force_down);
1083 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1084 class_server_ref = hlua_register_metatable(L, CLASS_SERVER);
1085
Thierry Fournierf61aa632016-02-19 20:56:00 +01001086 /* Create proxy object. */
1087 lua_newtable(L);
1088 lua_pushstring(L, "__index");
1089 lua_newtable(L);
1090 hlua_class_function(L, "pause", hlua_proxy_pause);
1091 hlua_class_function(L, "resume", hlua_proxy_resume);
1092 hlua_class_function(L, "stop", hlua_proxy_stop);
1093 hlua_class_function(L, "shut_bcksess", hlua_proxy_shut_bcksess);
1094 hlua_class_function(L, "get_cap", hlua_proxy_get_cap);
1095 hlua_class_function(L, "get_mode", hlua_proxy_get_mode);
1096 hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
1097 lua_settable(L, -3); /* -> META["__index"] = TABLE */
1098 class_proxy_ref = hlua_register_metatable(L, CLASS_PROXY);
1099
Thierry Fournier1550d5d2016-01-21 09:35:41 +01001100 return 5;
Thierry Fournierfb0b5462016-01-21 09:28:58 +01001101}