blob: d44f2bd4fc540b1a864a213a09b418ee23f5b6ae [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 */
Willy Tarreauabd9bb22017-07-19 19:08:48 +0200290 time = my_timegm(&tm);
Thierry Fournier1550d5d2016-01-21 09:35:41 +0100291 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
Willy Tarreauc95bad52016-12-22 00:13:31 +0100473 if (!li->bind_conf->frontend) {
Thierry Fournierff480422016-02-25 08:36:46 +0100474 lua_pushnil(L);
475 return 1;
476 }
477
Willy Tarreauc95bad52016-12-22 00:13:31 +0100478 stats_fill_li_stats(li->bind_conf->frontend, li, ST_SHLGNDS, stats, STATS_LEN);
Thierry Fournierff480422016-02-25 08:36:46 +0100479
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, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400548 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100549 luaL_addstring(&b, addr);
550 break;
551 case AF_INET6:
552 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
Nenad Merdanovica9f04042017-07-23 22:04:59 -0400553 addr, INET6_ADDRSTRLEN);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100554 luaL_addstring(&b, addr);
555 luaL_addstring(&b, ":");
Nenad Merdanovic38494732017-07-23 22:04:58 -0400556 snprintf(addr, INET_ADDRSTRLEN, "%d", srv->svc_port);
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100557 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
571int 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
580int 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
597int 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
606int 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
623int 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
632int 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
641int 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
650int 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
659int 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
670int 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
681int 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
693int 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
705int 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
717int 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
728int 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
739int 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
751int 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 Fournierf61aa632016-02-19 20:56:00 +0100763int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
764{
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100765 struct server *srv;
Thierry Fournierff480422016-02-25 08:36:46 +0100766 struct listener *lst;
767 int lid;
768 char buffer[10];
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100769
Thierry Fournierf61aa632016-02-19 20:56:00 +0100770 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 Fournierf2fdc9d2016-02-22 08:21:39 +0100778
779 /* Browse and register servers. */
780 lua_pushstring(L, "servers");
781 lua_newtable(L);
782 for (srv = px->srv; srv; srv = srv->next) {
783 lua_pushstring(L, srv->id);
784 hlua_fcn_new_server(L, srv);
785 lua_settable(L, -3);
786 }
787 lua_settable(L, -3);
788
Thierry Fournierff480422016-02-25 08:36:46 +0100789 /* Browse and register listeners. */
790 lua_pushstring(L, "listeners");
791 lua_newtable(L);
792 lid = 1;
793 list_for_each_entry(lst, &px->conf.listeners, by_fe) {
794 if (lst->name)
795 lua_pushstring(L, lst->name);
796 else {
797 snprintf(buffer, 10, "sock-%d", lid);
798 lid++;
799 lua_pushstring(L, buffer);
800 }
801 hlua_fcn_new_listener(L, lst);
802 lua_settable(L, -3);
803 }
804 lua_settable(L, -3);
805
Thierry Fournierf61aa632016-02-19 20:56:00 +0100806 return 1;
807}
808
809static struct proxy *hlua_check_proxy(lua_State *L, int ud)
810{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200811 return hlua_checkudata(L, ud, class_proxy_ref);
Thierry Fournierf61aa632016-02-19 20:56:00 +0100812}
813
814int hlua_proxy_pause(lua_State *L)
815{
816 struct proxy *px;
817
818 px = hlua_check_proxy(L, 1);
819 pause_proxy(px);
820 return 0;
821}
822
823int hlua_proxy_resume(lua_State *L)
824{
825 struct proxy *px;
826
827 px = hlua_check_proxy(L, 1);
828 resume_proxy(px);
829 return 0;
830}
831
832int hlua_proxy_stop(lua_State *L)
833{
834 struct proxy *px;
835
836 px = hlua_check_proxy(L, 1);
837 stop_proxy(px);
838 return 0;
839}
840
841int hlua_proxy_get_cap(lua_State *L)
842{
843 struct proxy *px;
844 const char *str;
845
846 px = hlua_check_proxy(L, 1);
847 str = proxy_cap_str(px->cap);
848 lua_pushstring(L, str);
849 return 1;
850}
851
852int hlua_proxy_get_stats(lua_State *L)
853{
854 struct proxy *px;
855 int i;
856
857 px = hlua_check_proxy(L, 1);
858 if (px->cap & PR_CAP_BE)
859 stats_fill_be_stats(px, ST_SHLGNDS, stats, STATS_LEN);
860 else
861 stats_fill_fe_stats(px, stats, STATS_LEN);
862 lua_newtable(L);
863 for (i=0; i<ST_F_TOTAL_FIELDS; i++) {
864 lua_pushstring(L, stat_field_names[i]);
865 hlua_fcn_pushfield(L, &stats[i]);
866 lua_settable(L, -3);
867 }
868 return 1;
869}
870
871int hlua_proxy_get_mode(lua_State *L)
872{
873 struct proxy *px;
874 const char *str;
875
876 px = hlua_check_proxy(L, 1);
877 str = proxy_mode_str(px->mode);
878 lua_pushstring(L, str);
879 return 1;
880}
881
882int hlua_proxy_shut_bcksess(lua_State *L)
883{
884 struct proxy *px;
885
886 px = hlua_check_proxy(L, 1);
887 srv_shutdown_backup_streams(px, SF_ERR_KILLED);
888 return 0;
889}
890
Thierry Fournier3d4a6752016-02-19 20:53:30 +0100891int hlua_fcn_post_init(lua_State *L)
892{
Thierry Fournierf61aa632016-02-19 20:56:00 +0100893 struct proxy *px;
894
895 /* get core array. */
896 if (lua_getglobal(L, "core") != LUA_TTABLE)
897 lua_error(L);
898
899 /* Create proxies entry. */
900 lua_pushstring(L, "proxies");
901 lua_newtable(L);
902
903 /* List all proxies. */
904 for (px = proxy; px; px = px->next) {
905 lua_pushstring(L, px->id);
906 hlua_fcn_new_proxy(L, px);
907 lua_settable(L, -3);
908 }
909
910 /* push "proxies" in "core" */
911 lua_settable(L, -3);
912
Thierry Fournier3d4a6752016-02-19 20:53:30 +0100913 return 1;
914}
915
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100916/* This Lua function take a string, a list of separators.
917 * It tokenize the input string using the list of separators
918 * as separator.
919 *
920 * The functionreturns a tablle filled with tokens.
921 */
922int hlua_tokenize(lua_State *L)
923{
924 const char *str;
925 const char *sep;
926 int index;
927 const char *token;
928 const char *p;
929 const char *c;
930 int ignore_empty;
931
932 ignore_empty = 0;
933
934 str = luaL_checkstring(L, 1);
935 sep = luaL_checkstring(L, 2);
936 if (lua_gettop(L) == 3)
937 ignore_empty = hlua_checkboolean(L, 3);
938
939 lua_newtable(L);
940 index = 1;
941 token = str;
942 p = str;
943 while(1) {
944 for (c = sep; *c != '\0'; c++)
945 if (*p == *c)
946 break;
947 if (*p == *c) {
948 if ((!ignore_empty) || (p - token > 0)) {
949 lua_pushlstring(L, token, p - token);
950 lua_rawseti(L, -2, index);
951 index++;
952 }
953 token = p + 1;
954 }
955 if (*p == '\0')
956 break;
957 p++;
958 }
959
960 return 1;
961}
962
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100963int hlua_parse_addr(lua_State *L)
964{
965 struct hlua_addr *addr;
966 const char *str = luaL_checkstring(L, 1);
967 unsigned char mask;
968
969 addr = lua_newuserdata(L, sizeof(struct hlua_addr));
970 if (!addr) {
971 lua_pushnil(L);
972 return 1;
973 }
974
975 if (str2net(str, PAT_MF_NO_DNS, &addr->addr.v4.ip, &addr->addr.v4.mask)) {
976 addr->type = AF_INET;
977 return 1;
978 }
979
980 if (str62net(str, &addr->addr.v6.ip, &mask)) {
981 len2mask6(mask, &addr->addr.v6.mask);
982 addr->type = AF_INET6;
983 return 1;
984 }
985
986 lua_pop(L, 1);
987 lua_pushnil(L);
988 return 1;
989}
990
991int hlua_match_addr(lua_State *L)
992{
993 struct hlua_addr *addr1;
994 struct hlua_addr *addr2;
995
996 if (!lua_isuserdata(L, 1) ||
997 !lua_isuserdata(L, 2)) {
998 lua_pushboolean(L, 0);
999 return 1;
1000 }
1001
1002 addr1 = lua_touserdata(L, 1);
1003 addr2 = lua_touserdata(L, 2);
1004
1005 if (addr1->type != addr2->type) {
1006 lua_pushboolean(L, 0);
1007 return 1;
1008 }
1009
1010 if (addr1->type == AF_INET) {
1011 if ((addr1->addr.v4.ip.s_addr & addr2->addr.v4.mask.s_addr) ==
1012 (addr2->addr.v4.ip.s_addr & addr1->addr.v4.mask.s_addr)) {
1013 lua_pushboolean(L, 1);
1014 return 1;
1015 }
1016 } else {
Thierry FOURNIERde6925e2016-12-23 17:03:25 +01001017 int i;
1018
1019 for (i = 0; i < 16; i += 4) {
1020 if ((*(uint32_t *)&addr1->addr.v6.ip.s6_addr[i] &
1021 *(uint32_t *)&addr2->addr.v6.mask.s6_addr[i]) !=
1022 (*(uint32_t *)&addr2->addr.v6.ip.s6_addr[i] &
1023 *(uint32_t *)&addr1->addr.v6.mask.s6_addr[i]))
1024 break;
1025 }
1026 if (i == 16) {
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +01001027 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}