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 5813 2026-06-15 10:15:50Z 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"
95#define balua_create(p) _balua_create(p, BALUA_VERSION)
96
97#define baluaENV_getmetatable(L,mtId) lua_rawgeti(L, BA_ENV_IX, mtId)
98#define baluaENV_checkudata(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,TRUE)
99#define baluaENV_isudtype(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,FALSE)
100#define balua_pushbatab(L) lua_getfield(L,LUA_REGISTRYINDEX,BA_TABLE)
101/* Similar to luaL_newlibtable, but sets batab as upvalue */
102#define balua_newlib(L,l) \
103 (luaL_newlibtable(L,l), balua_pushbatab(L), luaL_setfuncs(L,l,1))
104#define baluaENV_getmutex(L) baluaENV_getparam(L)->mutex
106#define balua_getmutex(L) balua_getparam(L)->mutex
107#define GET_BAMUTEX ThreadMutex* m = baluaENV_getmutex(L)
109#define balua_releasemutex(m) if(m) ThreadMutex_release(m)
111#define balua_setmutex(m) if(m) ThreadMutex_set(m)
112
113#ifdef NDEBUG
114#define balua_check(x,y) x
115#else
116#define balua_check(x,y) baAssert(y == x)
117#endif
118
119#ifndef BALUA_XH
120#define BALUA_XH 0xA5
121#endif
122
123#define dmpstk1(L,x) \
124 HttpTrace_printf(0,"%d: %s %p\n", __LINE__,lua_typename(L,lua_type(L,(x))), \
125 lua_topointer(L,x))
126#define dmpstk2(L,x) { \
127 int ix=lua_absindex(L,x); \
128 HttpTrace_printf(0,"%4d: %2d %s\n",__LINE__,ix,luaL_tolstring(L,x,0)); \
129 lua_pop(L,1); \
130 }while(0)
131#define dmptop(L) \
132 HttpTrace_printf(0,"%d: top %d\n",__LINE__,lua_gettop(L))
133#define dmpTab(L, ix) do { \
134 HttpTrace_printf(0,"%d: TAB %d %p\n", \
135 __LINE__, lua_absindex(L,ix), lua_topointer(L,ix)); \
136 lua_pushnil(L); \
137 while(lua_next(L, ix < 0 ? ix-1: ix) != 0) \
138 { \
139 HttpTrace_printf(0,"\t%s = %s\n", \
140 luaL_tolstring(L,-3,0), \
141 luaL_tolstring(L,-1,0)); \
142 lua_pop(L,3); \
143 } \
144 } while(0)
145
146
147struct BaTimer;
148struct LoginTracker;
149
156typedef struct
157{
158 lua_State* L;
160 struct BaTimer* timer;
165 const U8* zipPubKey;
166 const U8* zipBinPwd;
168 BaBool pwdRequired;
170
171typedef struct
172{
173 IoIntf* i;
174 int destroy;
175} baio_t;
176
177
178#ifdef BA_LEAK_CHECK
179BA_API void* baLMallocL(lua_State* L, size_t size,const char* file, int line);
180#define baLMalloc(L,size) baLMallocL(L,size,__FILE__,__LINE__)
181#else
183BA_API void* baLMalloc(lua_State* L, size_t size);
184#endif
185
190BA_API lua_State* _balua_create(const BaLua_param* p, int version);
191
192/* Close the Barracuda Lua VM */
193BA_API void balua_close(lua_State* L);
194
207 lua_State* L, U32 noOfLoginTrackerNodes, U32 maxNumberOfLogins,
208 BaTime banTime);
209
210BA_API lua_State* balua_getmainthread(lua_State* L);
211BA_API int balua_typeerror(lua_State *L, int narg, const char *tname);
212#define balua_optboolean(L,narg,def) luaL_opt(L, balua_checkboolean, narg, def)
213BA_API int balua_checkboolean(lua_State *L, int narg);
214BA_API int baluaENV_newmetatable(lua_State *L, int mtid, int inheritmtid);
215BA_API void baluaENV_register(
216 lua_State *L, int mtid, int inheritmtid, const luaL_Reg* lib);
217BA_API void* baluaENV_newuserdata(lua_State *L, int mtid, size_t size);
218BA_API void* _baluaENV_isudtype(lua_State* L, int udIx, int mtid, int check);
219BA_API HttpCommand* baluaENV_checkcmd(lua_State* L, int ix);
220BA_API BaLua_param* balua_getparam(lua_State* L);
221BA_API BaLua_param* baluaENV_getparam(lua_State* L);
222BA_API int balua_errorhandler(lua_State* L);
223BA_API void balua_manageerr(
224 lua_State* L,const char* ewhere,const char* emsg,HttpCommand* cmd);
225BA_API void balua_resumeerr(lua_State* L,const char* ewhere);
226BA_API int balua_loadfile(
227 lua_State *L, const char *filename, struct IoIntf* io, int envix);
228
235BA_API void
236balua_getuservalue(lua_State* L, int index);
237
238
254BA_API int balua_wkRef(lua_State* L);
255
256BA_API int balua_wkRefIx(lua_State* L, int index);
257
263BA_API void balua_wkPush(lua_State* L, int reference);
264
269BA_API void balua_wkUnref(lua_State* L, int reference); /* Weak */
271
272
273
274#ifdef _DOXYGEN
283 lua_State* L, struct IoIntf* io, const char* filename);
284#else
285#define balua_loadconfig(L, io, filename) \
286 balua_loadconfigExt(L, io, filename, 0)
287#endif
288
300 lua_State* L, struct IoIntf* io, const char* filename, int nresults);
301
302
303BA_API IoIntf* baluaENV_checkIoIntf(lua_State *L, int udIx);
304
316 lua_State* L, const char* name, struct IoIntf* newio);
317BA_API struct IoIntf** balua_createiointf(lua_State* L);
318BA_API HttpDir* baluaENV_toDir(lua_State* L, int ix);
319BA_API HttpDir* baluaENV_createDir(lua_State* L,int mtId,size_t dirSize);
320BA_API const char* balua_getStringField(
321 lua_State *L, int ix, const char *k, const char *def);
322BA_API lua_Integer balua_getIntField(
323 lua_State *L, int ix, const char *k, lua_Integer defVal);
324BA_API BaBool balua_getBoolField(
325 lua_State *L, int ix, const char *k, BaBool defVal);
326BA_API int balua_getTabField(lua_State *L, int ix, const char *k);
327BA_API const char* balua_checkStringField(lua_State *L, int ix, const char *k);
328BA_API lua_Integer balua_checkIntField(lua_State *L, int ix, const char *k);
329
330
331/* Internally used by the xrc code */
332#ifndef NO_SHARKSSL
333typedef struct
334{
335 int (*pushCertificate)(lua_State *L, SoDispCon* con);
336 int (*pushCipher)(lua_State *L, SoDispCon* con);
337 void (*unlockSharkSSL)(lua_State *L,struct SharkSsl* super);
338 struct SharkSsl* (*lockSharkSSL)(
339 lua_State *L,int tabIx,SharkSsl_Role role,SharkSsl* lockedShark);
340} LSharkSSLFuncs;
341 /* Auto set if using xrc/lua/lsharkssl.c */
342extern const LSharkSSLFuncs* lSharkSSLFuncs;
343#else
344extern const void* lSharkSSLFuncs;
345#endif
346
347enum BaUserDataTypes {
348 BA_VMPTR=1,
349 BA_IOINTFTAB,
350 BA_DIRTAB,
351 BA_CMDTAB,
352 BA_JSONPARSER,
353 BA_TDIR,
354 BA_TDIR_RSRDR,
355 BA_TDIR_DAV,
356 BA_TRESINTF,
357 BA_TIOINTF,
358 BA_TDIRITER,
359 BA_THTTPCMD,
360 BA_TCOOKIE,
361 BA_TSESSION,
362 BA_TSETRESPONSE,
363 BA_TAUTHORIZERINTF,
364 BA_TLUA_AUTHORIZER,
365 BA_TUSERINTF,
366 BA_TLUA_USER,
367 BA_TJAUTHORIZER,
368 BA_TJUSER,
369 BA_TAUTHENTICATORINTF,
370 BA_TAUTHENTICATOR,
371 BA_TTIMER,
372 BA_TUPLOAD,
373 BA_TASYNCRESP,
374 BA_TDIR_REDIRECTOR,
375 BA_TDIR_TRACELOGGER,
376 BA_DBGMON_NEWIO_CB, /* ldbgmon.c */
377 BA_IOINTFPTRTAB /* balua_installZIO */
378};
379
380
393typedef struct LHttpDir
394{
395 lua_State *LM;
396 HttpDir_Service orgServiceFunc;
397 struct LHttpDir* parent;
398 int parentRef;
399 int funcRef; /* Reference to Lua service function, if any: In registry */
400 int authenticatorRef; /* authenticator if any: In ENV */
401 int authorizerRef; /* authorizer if any: In ENV */
402 int utabRef; /* Table associated with udata saved in BA_WKTABLE */
404
405
406BA_API int LHttpResRdr_loadLsp(
407 lua_State* L, IoIntf* io, const char* pathname, IoStat* st);
409BA_API void balua_ubjson(lua_State* L);
410BA_API void balua_luaio(lua_State* L);
411BA_API void luaopen_ba_redirector(lua_State *L);
412BA_API void ba_ldbgmon(
413 lua_State* L, void (*exitCb)(void*,BaBool), void* exitCbData);
414BA_API void balua_revcon(lua_State* L);
415BA_API void balua_mallinfo(lua_State* L);
416struct CspReader;
417
430 const U8* pubKey, U32 fileSize, struct CspReader* reader);
431struct ZipReader;
432
446BA_API void balua_installZIO(lua_State* L, const char* name, struct ZipReader* reader);
447
448
449#ifdef __cplusplus
450}
451#endif
452
453 /* LSP Plugin */
455
456#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)
Install 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)
Verify 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)
Create the BAS Lua VM.
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)
Allocate memory for Lua and run 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:2340
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.
Startup and runtime parameters for a BAS Lua VM.
Definition: balua.h:157
struct BaTimer * timer
Timer bindings activated if not NULL.
Definition: balua.h:160
struct LoginTracker * tracker
Optional login tracker.
Definition: balua.h:163
const U8 * zipBinPwd
Set the binary password for all ZIP files.
Definition: balua.h:166
ThreadMutex * mutex
Mutex used by the server's SoDisp.
Definition: balua.h:162
const U8 * zipPubKey
Set when zip signature check enabled.
Definition: balua.h:165
BaBool pwdRequired
Set to true to enforce password on all files.
Definition: balua.h:168
lua_State * L
Lua state created or used by this VM.
Definition: balua.h:158
IoIntf * vmio
Required VM I/O interface used for Lua resources.
Definition: balua.h:161
HttpServer * server
Server associated with this VM.
Definition: balua.h:159
U16 zipBinPwdLen
Binary password length.
Definition: balua.h:167
int errHndRef
Internal: The ba.seterrh(func) ref.
Definition: balua.h:164
Timer manager used for one-shot and periodic callbacks.
Definition: BaTimer.h:62
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:1824
An instance of the HttpDir class, which is a collection of zero or more resources,...
Definition: HttpServer.h:2370
The Web Server.
Definition: HttpServer.h:2867
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:394
The LoginTracker class is an optional security enhancement that can be installed in an instance of on...
Definition: AuthenticatedUser.h:809
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