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 5712 2025-12-22 18:14:12Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2008 - 2025
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
44extern "C" {
45#endif
46
47#include "lua.h"
48#include "lauxlib.h"
49#ifndef NO_SHARKSSL
50#include "SharkSSL.h"
51#endif
52#ifdef __cplusplus
53}
54#endif
55
56#include "AuthenticatedUser.h"
57#include "ThreadLib.h"
58#include "DoubleList.h"
59#include "HttpCmdThreadPoolIntf.h"
60
61
80#ifdef __cplusplus
81extern "C" {
82#endif
83
84#ifdef BASLIB_VER_NO
85#define BALUA_VERSION BASLIB_VER_NO
86#else
87#define BALUA_VERSION 1
88#endif
89#define BA_ENV_IX lua_upvalueindex(1)
90#define BA_TABLE "_BA"
92#define balua_create(p) _balua_create(p, BALUA_VERSION)
93
94#define baluaENV_getmetatable(L,mtId) lua_rawgeti(L, BA_ENV_IX, mtId)
95#define baluaENV_checkudata(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,TRUE)
96#define baluaENV_isudtype(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,FALSE)
97#define balua_pushbatab(L) lua_getfield(L,LUA_REGISTRYINDEX,BA_TABLE)
98/* Similar to luaL_newlibtable, but sets batab as upvalue */
99#define balua_newlib(L,l) \
100 (luaL_newlibtable(L,l), balua_pushbatab(L), luaL_setfuncs(L,l,1))
101#define baluaENV_getmutex(L) baluaENV_getparam(L)->mutex
103#define balua_getmutex(L) balua_getparam(L)->mutex
104#define GET_BAMUTEX ThreadMutex* m = baluaENV_getmutex(L)
106#define balua_releasemutex(m) if(m) ThreadMutex_release(m)
108#define balua_setmutex(m) if(m) ThreadMutex_set(m)
109
110#ifdef NDEBUG
111#define balua_check(x,y) x
112#else
113#define balua_check(x,y) baAssert(y == x)
114#endif
115
116#ifndef BALUA_XH
117#define BALUA_XH 0xA5
118#endif
119
120#define dmpstk1(L,x) \
121 HttpTrace_printf(0,"%d: %s %p\n", __LINE__,lua_typename(L,lua_type(L,(x))), \
122 lua_topointer(L,x))
123#define dmpstk2(L,x) { \
124 int ix=lua_absindex(L,x); \
125 HttpTrace_printf(0,"%4d: %2d %s\n",__LINE__,ix,luaL_tolstring(L,x,0)); \
126 lua_pop(L,1); \
127 }while(0)
128#define dmptop(L) \
129 HttpTrace_printf(0,"%d: top %d\n",__LINE__,lua_gettop(L))
130#define dmpTab(L, ix) do { \
131 HttpTrace_printf(0,"%d: TAB %d %p\n", \
132 __LINE__, lua_absindex(L,ix), lua_topointer(L,ix)); \
133 lua_pushnil(L); \
134 while(lua_next(L, ix < 0 ? ix-1: ix) != 0) \
135 { \
136 HttpTrace_printf(0,"\t%s = %s\n", \
137 luaL_tolstring(L,-3,0), \
138 luaL_tolstring(L,-1,0)); \
139 lua_pop(L,3); \
140 } \
141 } while(0)
142
143
144struct BaTimer;
145struct LoginTracker;
146
153typedef struct
154{
155 lua_State* L;
157 struct BaTimer* timer;
162 const U8* zipPubKey;
163 const U8* zipBinPwd;
165 BaBool pwdRequired;
167
168typedef struct
169{
170 IoIntf* i;
171 int destroy;
172} baio_t;
173
174
175#ifdef BA_LEAK_CHECK
176BA_API void* baLMallocL(lua_State* L, size_t size,const char* file, int line);
177#define baLMalloc(L,size) baLMallocL(L,size,__FILE__,__LINE__)
178#else
180BA_API void* baLMalloc(lua_State* L, size_t size);
181#endif
182
186BA_API lua_State* _balua_create(const BaLua_param* p, int version);
187
188/* Close the Barracuda Lua VM */
189BA_API void balua_close(lua_State* L);
190
203 lua_State* L, U32 noOfLoginTrackerNodes, U32 maxNumberOfLogins,
204 BaTime banTime);
205
206BA_API lua_State* balua_getmainthread(lua_State* L);
207BA_API int balua_typeerror(lua_State *L, int narg, const char *tname);
208#define balua_optboolean(L,narg,def) luaL_opt(L, balua_checkboolean, narg, def)
209BA_API int balua_checkboolean(lua_State *L, int narg);
210BA_API int baluaENV_newmetatable(lua_State *L, int mtid, int inheritmtid);
211BA_API void baluaENV_register(
212 lua_State *L, int mtid, int inheritmtid, const luaL_Reg* lib);
213BA_API void* baluaENV_newuserdata(lua_State *L, int mtid, size_t size);
214BA_API void* _baluaENV_isudtype(lua_State* L, int udIx, int mtid, int check);
215BA_API HttpCommand* baluaENV_checkcmd(lua_State* L, int ix);
216BA_API BaLua_param* balua_getparam(lua_State* L);
217BA_API BaLua_param* baluaENV_getparam(lua_State* L);
218BA_API int balua_errorhandler(lua_State* L);
219BA_API void balua_manageerr(
220 lua_State* L,const char* ewhere,const char* emsg,HttpCommand* cmd);
221BA_API void balua_resumeerr(lua_State* L,const char* ewhere);
222BA_API int balua_loadfile(
223 lua_State *L, const char *filename, struct IoIntf* io, int envix);
224
231BA_API void
232balua_getuservalue(lua_State* L, int index);
233
234
250BA_API int balua_wkRef(lua_State* L);
251
252BA_API int balua_wkRefIx(lua_State* L, int index);
253
259BA_API void balua_wkPush(lua_State* L, int reference);
260
265BA_API void balua_wkUnref(lua_State* L, int reference); /* Weak */
267
268
269
270#ifdef _DOXYGEN
279 lua_State* L, struct IoIntf* io, const char* filename);
280#else
281#define balua_loadconfig(L, io, filename) \
282 balua_loadconfigExt(L, io, filename, 0)
283#endif
284
296 lua_State* L, struct IoIntf* io, const char* filename, int nresults);
297
298
299BA_API IoIntf* baluaENV_checkIoIntf(lua_State *L, int udIx);
300
312 lua_State* L, const char* name, struct IoIntf* newio);
313BA_API struct IoIntf** balua_createiointf(lua_State* L);
314BA_API HttpDir* baluaENV_toDir(lua_State* L, int ix);
315BA_API HttpDir* baluaENV_createDir(lua_State* L,int mtId,size_t dirSize);
316BA_API const char* balua_getStringField(
317 lua_State *L, int ix, const char *k, const char *def);
318BA_API lua_Integer balua_getIntField(
319 lua_State *L, int ix, const char *k, lua_Integer defVal);
320BA_API BaBool balua_getBoolField(
321 lua_State *L, int ix, const char *k, BaBool defVal);
322BA_API int balua_getTabField(lua_State *L, int ix, const char *k);
323BA_API const char* balua_checkStringField(lua_State *L, int ix, const char *k);
324BA_API lua_Integer balua_checkIntField(lua_State *L, int ix, const char *k);
325
326
327/* Internally used by the xrc code */
328#ifndef NO_SHARKSSL
329typedef struct
330{
331 int (*pushCertificate)(lua_State *L, SoDispCon* con);
332 int (*pushCipher)(lua_State *L, SoDispCon* con);
333 void (*unlockSharkSSL)(lua_State *L,struct SharkSsl* super);
334 struct SharkSsl* (*lockSharkSSL)(
335 lua_State *L,int tabIx,SharkSsl_Role role,SharkSsl* lockedShark);
336} LSharkSSLFuncs;
337 /* Auto set if using xrc/lua/lsharkssl.c */
338extern const LSharkSSLFuncs* lSharkSSLFuncs;
339#else
340extern const void* lSharkSSLFuncs;
341#endif
342
343enum BaUserDataTypes {
344 BA_VMPTR=1,
345 BA_IOINTFTAB,
346 BA_DIRTAB,
347 BA_CMDTAB,
348 BA_JSONPARSER,
349 BA_TDIR,
350 BA_TDIR_RSRDR,
351 BA_TDIR_DAV,
352 BA_TRESINTF,
353 BA_TIOINTF,
354 BA_TDIRITER,
355 BA_THTTPCMD,
356 BA_TCOOKIE,
357 BA_TSESSION,
358 BA_TSETRESPONSE,
359 BA_TAUTHORIZERINTF,
360 BA_TLUA_AUTHORIZER,
361 BA_TUSERINTF,
362 BA_TLUA_USER,
363 BA_TJAUTHORIZER,
364 BA_TJUSER,
365 BA_TAUTHENTICATORINTF,
366 BA_TAUTHENTICATOR,
367 BA_TTIMER,
368 BA_TUPLOAD,
369 BA_TASYNCRESP,
370 BA_TDIR_REDIRECTOR,
371 BA_TDIR_TRACELOGGER,
372 BA_DBGMON_NEWIO_CB, /* ldbgmon.c */
373 BA_IOINTFPTRTAB /* balua_installZIO */
374};
375
376
389typedef struct LHttpDir
390{
391 lua_State *LM;
392 HttpDir_Service orgServiceFunc;
393 struct LHttpDir* parent;
394 int parentRef;
395 int funcRef; /* Reference to Lua service function, if any: In registry */
396 int authenticatorRef; /* authenticator if any: In ENV */
397 int authorizerRef; /* authorizer if any: In ENV */
398 int utabRef; /* Table associated with udata saved in BA_WKTABLE */
400
401
402BA_API int LHttpResRdr_loadLsp(
403 lua_State* L, IoIntf* io, const char* pathname, IoStat* st);
405BA_API void balua_ubjson(lua_State* L);
406BA_API void balua_luaio(lua_State* L);
407BA_API void luaopen_ba_redirector(lua_State *L);
408BA_API void ba_ldbgmon(
409 lua_State* L, void (*exitCb)(void*,BaBool), void* exitCbData);
410BA_API void balua_revcon(lua_State* L);
411BA_API void balua_mallinfo(lua_State* L);
412struct CspReader;
413
426 const U8* pubKey, U32 fileSize, struct CspReader* reader);
427struct ZipReader;
428
442BA_API void balua_installZIO(lua_State* L, const char* name, struct ZipReader* reader);
443
444
445#ifdef __cplusplus
446}
447#endif
448
449 /* LSP Plugin */
451
452#endif
void balua_luaio(lua_State *L)
Install the LuaIo Lua bindings.
BA_API int balua_usertracker_create(lua_State *L, U32 noOfLoginTrackerNodes, U32 maxNumberOfLogins, BaTime banTime)
Install Lua bindings for the default login tracker.
BA_API void balua_ubjson(lua_State *L)
Install the UBJSON lua api.
BA_API void balua_installZIO(lua_State *L, const char *name, struct ZipReader *reader)
Installs a Zip I/O interface into the Lua environment.
struct LHttpDir LHttpDir
LHttpDir is the HttpDir instance used by Lua bindings and can be used by advanced Lua bindings creati...
BA_API int baCheckZipSignature(const U8 *pubKey, U32 fileSize, struct CspReader *reader)
Verifies the signature of a ZIP file using a public key.
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.
BA_API lua_State * _balua_create(const BaLua_param *p, int version)
Creates the Barracuda Lua VM; Note: use macro balua_create(BaLua_param).
BA_API void balua_getuservalue(lua_State *L, int index)
Pushes onto the stack the first user value associated with the full userdata at the given index.
BA_API IoIntf * balua_iointf(lua_State *L, const char *name, struct IoIntf *newio)
Register an IoIntf instance with the Lua VM.
BA_API void * baLMalloc(lua_State *L, size_t size)
Same as baMalloc, but does an emergency GC if baMalloc returns NULL.
BA_API int balua_loadconfig(lua_State *L, struct IoIntf *io, const char *filename)
Load a Lua script and run the script at startup.
int(* HttpDir_Service)(struct HttpDir *o, const char *relPath, HttpCommand *cmd)
The HttpDir service callback function.
Definition: HttpServer.h:2334
S64 BaTime
An arithmetic type representing calendar time with epoch of 1970-01-01 00:00:10 GMT – i....
Definition: GenPrimT.h:93
BA_API void balua_wkUnref(lua_State *L, int reference)
Releases the reference ref from the weak table.
BA_API int balua_wkRef(lua_State *L)
Creates and returns a reference in the weak table, for the object on the top of the stack (and pops t...
BA_API void balua_wkPush(lua_State *L, int reference)
Pushes the value associated with the key 'index' on top of the stack.
The startup and runtime parameters for a Barracuda Server Lua VM.
Definition: balua.h:154
struct BaTimer * timer
Timer bindings activated if not NULL.
Definition: balua.h:157
struct LoginTracker * tracker
The optional tracker.
Definition: balua.h:160
const U8 * zipBinPwd
Set the binary password for all ZIP files.
Definition: balua.h:163
ThreadMutex * mutex
The mutex used by the server's SoDisp.
Definition: balua.h:159
const U8 * zipPubKey
Set when zip signature check enabled.
Definition: balua.h:162
BaBool pwdRequired
Set to true to enforce password on all files.
Definition: balua.h:165
lua_State * L
The lua universe.
Definition: balua.h:155
IoIntf * vmio
The required VM (Lua) IO.
Definition: balua.h:158
HttpServer * server
Pointer to server for this vm.
Definition: balua.h:156
U16 zipBinPwdLen
Binary password length.
Definition: balua.h:164
int errHndRef
Internal: The ba.seterrh(func) ref.
Definition: balua.h:161
The timer class makes it possible to create events that are activated at regular intervals or to crea...
Definition: BaTimer.h:59
Abstract interface class for reading the "dat" file generated by HttpLink.
Definition: CspRunTm.h:118
The HttpCommand class is a container class for the HttpRequest and HttpResponse command pair.
Definition: HttpServer.h:1818
An instance of the HttpDir class, which is a collection of zero or more resources,...
Definition: HttpServer.h:2364
The Web Server.
Definition: HttpServer.h:2861
The IoIntf class specifies an abstract file API, implementations include ZipIo, DiskIo,...
Definition: IoIntf.h:377
Resource information.
Definition: IoIntf.h:168
LHttpDir is the HttpDir instance used by Lua bindings and can be used by advanced Lua bindings creati...
Definition: balua.h:390
The LoginTracker class is an optional security enhancement that can be installed in an instance of on...
Definition: AuthenticatedUser.h:806
Contains information about the physical socket connection.
Definition: SoDispCon.h:112
A mutual exclusion class.
Definition: ThreadLib.h:186
Abstract interface class for reading a ZipFile.
Definition: ZipFileIterator.h:72