Barracuda Application Server C/C++ Reference
NO
balua.h
1 /*
2  * ____ _________ __ _
3  * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4  * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5  * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6  * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7  * /____/
8  *
9  * Barracuda Embedded Web-Server
10  *
11  ****************************************************************************
12  * HEADER
13  *
14  * $Id: balua.h 5134 2022-04-27 22:40:57Z wini $
15  *
16  * COPYRIGHT: Real Time Logic LLC, 2008 - 2022
17  *
18  * This software is copyrighted by and is the sole property of Real
19  * Time Logic LLC. All rights, title, ownership, or other interests in
20  * the software remain the property of Real Time Logic LLC. This
21  * software may only be used in accordance with the terms and
22  * conditions stipulated in the corresponding license agreement under
23  * which the software has been supplied. Any unauthorized use,
24  * duplication, transmission, distribution, or disclosure of this
25  * software is expressly forbidden.
26  *
27  * This Copyright notice may not be removed or modified without prior
28  * written consent of Real Time Logic LLC.
29  *
30  * Real Time Logic LLC. reserves the right to modify this software
31  * without notice.
32  *
33  * http://www.realtimelogic.com
34  ****************************************************************************
35  *
36  *
37  */
38 
39 
40 #ifndef _balua_h
41 #define _balua_h
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #include "lua.h"
48 #include "lauxlib.h"
49 #include "SharkSSL.h"
50 
51 #ifdef __cplusplus
52 }
53 #endif
54 
55 #include "AuthenticatedUser.h"
56 #include "ThreadLib.h"
57 
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80 
81 #define BALUA_VERSION 20
82 #define BA_ENV_IX lua_upvalueindex(1)
83 #define BA_TABLE "_BA_"
84 #define BA_WKTABLE "_BAWK"
85 
86 #define balua_create(p) _balua_create(p, BALUA_VERSION)
87 
88 #define balua_optboolean(L,narg,def) luaL_opt(L, balua_checkboolean, narg, def)
89 #define baluaENV_getmetatable(L,mtId) lua_rawgeti(L, BA_ENV_IX, mtId)
90 #define baluaENV_checkudata(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,TRUE)
91 #define baluaENV_isudtype(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,FALSE)
92 #define balua_pushbatab(L) lua_getfield(L,LUA_REGISTRYINDEX,BA_TABLE)
93 #define balua_pushwktab(L) lua_getfield(L,LUA_REGISTRYINDEX,BA_WKTABLE)
94 #define balua_newlib(L,l) \
95  (luaL_newlibtable(L,l), balua_pushbatab(L), luaL_setfuncs(L,l,1))
96 #define baluaENV_getmutex(L) baluaENV_getparam(L)->mutex
97 
98 #define balua_getmutex(L) balua_getparam(L)->mutex
99 #define GET_BAMUTEX ThreadMutex* m = baluaENV_getmutex(L)
100 
101 #define balua_releasemutex(m) if(m) ThreadMutex_release(m)
102 
103 #define balua_setmutex(m) if(m) ThreadMutex_set(m)
104 
105 #ifdef NDEBUG
106 #define balua_check(x,y) x
107 #else
108 #define balua_check(x,y) baAssert(y == x)
109 #endif
110 
111 
112 #define dmpstk1(L,x) \
113  fprintf(stderr,"%d: %s %p\n", __LINE__, lua_typename(L,lua_type(L,(x))), \
114  lua_topointer(L,x))
115 #define dmpstk2(L,x) { \
116  int ix=lua_absindex(L,x); \
117  fprintf(stderr,"%4d: %2d %s\n", __LINE__, ix, luaL_tolstring(L,x,0)); \
118  lua_pop(L,1); \
119 }while(0)
120 #define dmptop(L) \
121  fprintf(stderr,"%d: top %d\n",__LINE__,lua_gettop(L))
122 #define dmpTab(L, ix) do { \
123  fprintf(stderr,"%d: TAB %d %p\n",\
124  __LINE__, lua_absindex(L,ix), lua_topointer(L,ix)); \
125  lua_pushnil(L); \
126  while(lua_next(L, ix < 0 ? ix-1: ix) != 0) \
127  { \
128  fprintf(stderr,"\t%s = %s\n", \
129  luaL_tolstring(L,-3,0), \
130  luaL_tolstring(L,-1,0)); \
131  lua_pop(L,3); \
132  } \
133 } while(0)
134 
135 
136 struct BaTimer;
137 struct LoginTracker;
138 
141 typedef struct
142 {
143  lua_State* L;
145  struct BaTimer* timer;
149  int errHndRef;
150 } BaLua_param;
151 
152 
153 typedef struct
154 {
155  IoIntf* i;
156  int destroy;
157 } baio_t;
158 
159 
160 #ifdef BA_LEAK_CHECK
161 BA_API void* baLMallocL(lua_State* L, size_t size,const char* file, int line);
162 #define baLMalloc(L,size) baLMallocL(L,size,__FILE__,__LINE__)
163 #else
164 
165 BA_API void* baLMalloc(lua_State* L, size_t size);
166 #endif
167 
171 BA_API lua_State* _balua_create(const BaLua_param* p, int version);
172 
181  lua_State* L, U32 noOfLoginTrackerNodes, U32 maxNumberOfLogins,
182  BaTime banTime);
183 
184 
185 BA_API lua_State* balua_getmainthread(lua_State* L);
186 BA_API int balua_typeerror(lua_State *L, int narg, const char *tname);
187 BA_API int balua_checkboolean(lua_State *L, int narg);
188 BA_API int baluaENV_newmetatable(lua_State *L, int mtid, int inheritmtid);
189 BA_API void baluaENV_register(
190  lua_State *L, int mtid, int inheritmtid, const luaL_Reg* lib);
191 BA_API void* baluaENV_newuserdata(lua_State *L, int mtid, size_t size);
192 BA_API void* _baluaENV_isudtype(lua_State* L, int udIx, int mtid, int check);
193 BA_API HttpCommand* baluaENV_checkcmd(lua_State* L, int ix);
194 BA_API BaLua_param* balua_getparam(lua_State* L);
195 BA_API BaLua_param* baluaENV_getparam(lua_State* L);
196 BA_API int balua_errorhandler(lua_State* L);
197 BA_API void balua_manageerr(
198  lua_State* L,const char* ewhere,const char* emsg,HttpCommand* cmd);
199 BA_API void balua_resumeerr(lua_State* L,const char* ewhere);
200 BA_API int balua_loadfile(
201  lua_State *L, const char *filename, struct IoIntf* io, int envix);
202 
203 #ifdef _DOXYGEN
204 
211 BA_API int balua_loadconfig(
212  lua_State* L, struct IoIntf* io, const char* filename);
213 #else
214 #define balua_loadconfig(L, io, filename) balua_loadconfigExt(L, io, filename, 0)
215 #endif
216 
228  lua_State* L, struct IoIntf* io, const char* filename, int nresults);
229 
230 
231 BA_API IoIntf* baluaENV_checkIoIntf(lua_State *L, int udIx);
232 
244  lua_State* L, const char* name, struct IoIntf* newio);
245 BA_API struct IoIntf** balua_createiointf(lua_State* L);
246 BA_API HttpDir* baluaENV_toDir(lua_State* L, int ix);
247 BA_API HttpDir* baluaENV_createDir(lua_State* L,int mtId,size_t dirSize);
248 BA_API const char* balua_getStringField(
249  lua_State *L, int ix, const char *k, const char *def);
250 BA_API lua_Integer balua_getIntField(
251  lua_State *L, int ix, const char *k, lua_Integer defVal);
252 BA_API BaBool balua_getBoolField(
253  lua_State *L, int ix, const char *k, BaBool defVal);
254 BA_API int balua_getTabField(lua_State *L, int ix, const char *k);
255 BA_API const char* balua_checkStringField(lua_State *L, int ix, const char *k);
256 BA_API lua_Integer balua_checkIntField(lua_State *L, int ix, const char *k);
257 
258 
259 /* Internally used by the xrc code */
260 typedef struct
261 {
262  int (*pushCertificate)(lua_State *L, SoDispCon* con);
263  int (*pushCipher)(lua_State *L, SoDispCon* con);
264  void (*unlockSharkSSL)(lua_State *L,struct SharkSsl* super);
265  struct SharkSsl* (*lockSharkSSL)(
266  lua_State *L,int tabIx,SharkSsl_Role role,SharkSsl* lockedShark);
268  /* Auto set if using xrc/lua/lsharkssl.c */
269 extern const LSharkSSLFuncs* lSharkSSLFuncs;
270 
271 
272 enum BaUserDataTypes {
273  BA_VMPTR=1,
274  BA_IOINTFTAB,
275  BA_DIRTAB,
276  BA_CMDTAB,
277  BA_JSONPARSER,
278  BA_TDIR,
279  BA_TDIR_RSRDR,
280  BA_TDIR_DAV,
281  BA_TRESINTF,
282  BA_TIOINTF,
283  BA_TDIRITER,
284  BA_THTTPCMD,
285  BA_TCOOKIE,
286  BA_TSESSION,
287  BA_TSETRESPONSE,
288  BA_TAUTHORIZERINTF,
289  BA_TLUA_AUTHORIZER,
290  BA_TUSERINTF,
291  BA_TLUA_USER,
292  BA_TJAUTHORIZER,
293  BA_TJUSER,
294  BA_TAUTHENTICATORINTF,
295  BA_TAUTHENTICATOR,
296  BA_TTIMER,
297  BA_TUPLOAD,
298  BA_TASYNCRESP,
299  BA_TDIR_REDIRECTOR,
300  BA_TDIR_TRACELOGGER,
301  BA_DBGMON_NEWIO_CB /* ldbgmon.c */
302 };
303 
304 
305 /*
306 
307 LHttpDir and Lua userdata memory layout:
308 +-----------+
309 | LHttpDir |
310 +-----------+
311 | HttpDir | <- Any type that inherits from HttpDir
312 +-----------+
313 */
314 typedef struct LHttpDir
315 {
316  lua_State *LM;
317  HttpDir_Service orgServiceFunc;
318  struct LHttpDir* parent;
319  int parentRef;
320  int funcRef; /* Reference to Lua service function, if any: In registry */
321  int authenticatorRef; /* authenticator if any: In ENV */
322  int authorizerRef; /* authorizer if any: In ENV */
323  int utabRef; /* Table associated with udata saved in BA_WKTABLE */
324 } LHttpDir;
325 
326 
327 BA_API int LHttpResRdr_loadLsp(
328  lua_State* L, IoIntf* io, const char* pathname, IoStat* st);
329 BA_API void balua_ubjson(lua_State* L);
330 BA_API void luaopen_ba_redirector(lua_State *L);
331 BA_API void ba_ldbgmon(
332  lua_State* L, void (*exitCb)(void*,BaBool), void* exitCbData);
333 BA_API void balua_revcon(lua_State* L);
334 BA_API void balua_mallinfo(lua_State* L);
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
340  /* LSP Plugin */
342 
343 #endif
BaTimer
The timer class makes it possible to create events that are activated at regular intervals or to crea...
Definition: BaTimer.h:59
baLMalloc
BA_API void * baLMalloc(lua_State *L, size_t size)
Same as baMalloc, but does an emergency GC if baMalloc returns NULL.
BaLua_param::L
lua_State * L
The lua universe.
Definition: balua.h:143
BaLua_param::mutex
ThreadMutex * mutex
The mutex used by the server's SoDisp.
Definition: balua.h:147
_balua_create
BA_API lua_State * _balua_create(const BaLua_param *p, int version)
Creates the Barracuda Lua VM; Note: use macro balua_create(BaLua_param).
BaLua_param::vmio
IoIntf * vmio
The required VM (Lua) IO.
Definition: balua.h:146
balua_iointf
BA_API IoIntf * balua_iointf(lua_State *L, const char *name, struct IoIntf *newio)
Register an IoIntf instance with the Lua VM.
HttpDir_Service
int(* HttpDir_Service)(struct HttpDir *o, const char *relPath, HttpCommand *cmd)
The HttpDir service callback function.
Definition: HttpServer.h:2350
LSharkSSLFuncs
Definition: balua.h:261
LoginTracker
The LoginTracker class is an optional security enhancement that can be installed in an instance of on...
Definition: AuthenticatedUser.h:810
BaTime
S64 BaTime
An arithmetic type representing calendar time with epoch of 1970-01-01 00:00:10 GMT – i....
Definition: GenPrimT.h:93
balua_loadconfig
BA_API int balua_loadconfig(lua_State *L, struct IoIntf *io, const char *filename)
Load a Lua script and run the script at startup.
balua_loadconfigExt
BA_API int balua_loadconfigExt(lua_State *L, struct IoIntf *io, const char *filename, int nresults)
Load a Lua script and run the script at startup.
HttpDir
An instance of the HttpDir class, which is a collection of zero or more resources,...
Definition: HttpServer.h:2384
BaLua_param::timer
struct BaTimer * timer
Timer bindings activated if not NULL.
Definition: balua.h:145
IoIntf
The IoIntf class specifies an abstract file API, implementations include ZipIo, DiskIo,...
Definition: IoIntf.h:377
BaLua_param
The startup and runtime parameters for a Barracuda Server Lua VM.
Definition: balua.h:142
balua_usertracker_create
BA_API int balua_usertracker_create(lua_State *L, U32 noOfLoginTrackerNodes, U32 maxNumberOfLogins, BaTime banTime)
Install Lua bindings for the default login tracker.
LHttpDir
Definition: balua.h:315
BaLua_param::server
HttpServer * server
Pointer to server for this vm.
Definition: balua.h:144
baio_t
Definition: balua.h:154
SoDispCon
Contains information about the physical socket connection.
Definition: SoDispCon.h:112
HttpCommand
The HttpCommand class is a container class for the HttpRequest and HttpResponse command pair.
Definition: HttpServer.h:1834
SharkSsl
Definition: selib.h:257
BaLua_param::tracker
struct LoginTracker * tracker
The optional tracker.
Definition: balua.h:148
BaLua_param::errHndRef
int errHndRef
Internal: The ba.seterrh(func) ref.
Definition: balua.h:149
ThreadMutex
A mutual exclusion class.
Definition: ThreadLib.h:186
IoStat
Resource information.
Definition: IoIntf.h:168
HttpServer
The Web Server.
Definition: HttpServer.h:2887