1 #ifndef _TYPES_HLUA_H
2 #define _TYPES_HLUA_H
3 
4 #ifdef USE_LUA
5 
6 #include <lua.h>
7 #include <lauxlib.h>
8 
9 #include <common/regex.h>
10 #include <common/xref.h>
11 
12 #include <types/proto_http.h>
13 #include <types/proxy.h>
14 #include <types/server.h>
15 #include <types/stick_table.h>
16 
17 #define CLASS_CORE         "Core"
18 #define CLASS_TXN          "TXN"
19 #define CLASS_FETCHES      "Fetches"
20 #define CLASS_CONVERTERS   "Converters"
21 #define CLASS_SOCKET       "Socket"
22 #define CLASS_CHANNEL      "Channel"
23 #define CLASS_HTTP         "HTTP"
24 #define CLASS_MAP          "Map"
25 #define CLASS_APPLET_TCP   "AppletTCP"
26 #define CLASS_APPLET_HTTP  "AppletHTTP"
27 #define CLASS_PROXY        "Proxy"
28 #define CLASS_SERVER       "Server"
29 #define CLASS_LISTENER     "Listener"
30 #define CLASS_REGEX        "Regex"
31 #define CLASS_STKTABLE     "StickTable"
32 
33 struct stream;
34 
35 #define HLUA_RUN       0x00000001
36 #define HLUA_CTRLYIELD 0x00000002
37 #define HLUA_WAKERESWR 0x00000004
38 #define HLUA_WAKEREQWR 0x00000008
39 #define HLUA_EXIT      0x00000010
40 #define HLUA_MUST_GC   0x00000020
41 #define HLUA_STOP      0x00000040
42 
43 #define HLUA_F_AS_STRING    0x01
44 #define HLUA_F_MAY_USE_HTTP 0x02
45 
46 #define HLUA_TXN_NOTERM   0x00000001
47 #define HLUA_TXN_HTTP_RDY 0x00000002 /* Set if the txn is HTTP ready for the defined direction */
48 
49 #define HLUA_CONCAT_BLOCSZ 2048
50 
51 enum hlua_exec {
52 	HLUA_E_OK = 0,
53 	HLUA_E_AGAIN,  /* LUA yield, must resume the stack execution later, when
54 	                  the associatedtask is waked. */
55 	HLUA_E_ETMOUT, /* Execution timeout */
56 	HLUA_E_NOMEM,  /* Out of memory error */
57 	HLUA_E_YIELD,  /* LUA code try to yield, and this is not allowed */
58 	HLUA_E_ERRMSG, /* LUA stack execution failed with a string error message
59 	                  in the top of stack. */
60 	HLUA_E_ERR,    /* LUA stack execution failed without error message. */
61 };
62 
63 /* This struct is use for storing HAProxy parsers state
64  * before executing some Lua code. The goal is we can
65  * check and compare the parser state a the end of Lua
66  * execution. If the state is changed by Lua towards
67  * an unexpected state, we can abort the transaction.
68  */
69 struct hlua_consistency {
70 	enum pr_mode mode;
71 	union {
72 		struct {
73 			int dir;
74 			enum h1_state state;
75 		} http;
76 	} data;
77 };
78 
79 struct hlua {
80 	lua_State *T; /* The LUA stack. */
81 	int Tref; /* The reference of the stack in coroutine case.
82 	             -1 for the main lua stack. */
83 	int Mref; /* The reference of the memory context in coroutine case.
84 	             -1 if the memory context is not used. */
85 	int nargs; /* The number of arguments in the stack at the start of execution. */
86 	unsigned int flags; /* The current execution flags. */
87 	int wake_time; /* The lua wants to be waked at this time, or before. */
88 	unsigned int max_time; /* The max amount of execution time for an Lua process, in ms. */
89 	unsigned int start_time; /* The ms time when the Lua starts the last execution. */
90 	unsigned int run_time; /* Lua total execution time in ms. */
91 	struct task *task; /* The task associated with the lua stack execution.
92 	                      We must wake this task to continue the task execution */
93 	struct list com; /* The list head of the signals attached to this task. */
94 	struct ebpt_node node;
95 	struct hlua_consistency cons; /* Store data consistency check. */
96 };
97 
98 /* This is a part of the list containing references to functions
99  * called at the initialisation time.
100  */
101 struct hlua_init_function {
102 	struct list l;
103 	int function_ref;
104 };
105 
106 /* This struct contains the lua data used to bind
107  * Lua function on HAProxy hook like sample-fetches
108  * or actions.
109  */
110 struct hlua_function {
111 	char *name;
112 	int function_ref;
113 	int nargs;
114 };
115 
116 /* This struct is used with the structs:
117  *  - http_req_rule
118  *  - http_res_rule
119  *  - tcp_rule
120  * It contains the lua execution configuration.
121  */
122 struct hlua_rule {
123 	struct hlua_function fcn;
124 	char **args;
125 };
126 
127 /* This struct contains the pointer provided on the most
128  * of internal HAProxy calls during the processing of
129  * rules, converters and sample-fetches. This struct is
130  * associated with the lua object called "TXN".
131  */
132 struct hlua_txn {
133 	struct stream *s;
134 	struct proxy *p;
135 	int dir;                /* SMP_OPT_DIR_{REQ,RES} */
136 	int flags;
137 };
138 
139 /* This struct contains the applet context. */
140 struct hlua_appctx {
141 	struct appctx *appctx;
142 	luaL_Buffer b; /* buffer used to prepare strings. */
143 	struct hlua_txn htxn;
144 };
145 
146 /* This struc is used with sample fetches and sample converters. */
147 struct hlua_smp {
148 	struct stream *s;
149 	struct proxy *p;
150 	unsigned int flags;     /* LUA_F_OPT_* */
151 	int dir;                /* SMP_OPT_DIR_{REQ,RES} */
152 };
153 
154 /* This struct contains data used with sleep functions. */
155 struct hlua_sleep {
156 	struct task *task; /* task associated with sleep. */
157 	struct list com; /* list of signal to wake at the end of sleep. */
158 	unsigned int wakeup_ms; /* hour to wakeup. */
159 };
160 
161 /* This struct is used to create coprocess doing TCP or
162  * SSL I/O. It uses a fake stream.
163  */
164 struct hlua_socket {
165 	struct xref xref; /* cross reference with the stream used for socket I/O. */
166 	luaL_Buffer b; /* buffer used to prepare strings. */
167 	unsigned long tid; /* Store the thread id which creates the socket. */
168 };
169 
170 struct hlua_concat {
171 	int size;
172 	int len;
173 };
174 
175 struct hlua_addr {
176 	union {
177 		struct {
178 			struct in_addr ip;
179 			struct in_addr mask;
180 		} v4;
181 		struct {
182 			struct in6_addr ip;
183 			struct in6_addr mask;
184 		} v6;
185 	} addr;
186 	int type;
187 };
188 
189 #else /* USE_LUA */
190 
191 /* Empty struct for compilation compatibility */
192 struct hlua { };
193 struct hlua_socket { };
194 struct hlua_rule { };
195 
196 #endif /* USE_LUA */
197 
198 #endif /* _TYPES_HLUA_H */
199