Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
balua.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Embedded Web-Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: balua.h 5978 2026-09-11 16:13:48Z 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
42#ifndef _balua_h
43#define _balua_h
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#include "lua.h"
50#include "lauxlib.h"
51#ifndef NO_SHARKSSL
52#include "SharkSSL.h"
53#endif
54#ifdef __cplusplus
55}
56#endif
57
58#include "AuthenticatedUser.h"
59#include "ThreadLib.h"
60#include "DoubleList.h"
62
63
87#ifdef __cplusplus
88extern "C" {
89#endif
90
91#ifdef BASLIB_VER_NO
92#define BALUA_VERSION BASLIB_VER_NO
93#else
94#define BALUA_VERSION 1
95#endif
96#define BA_ENV_IX lua_upvalueindex(1)
97#define BA_TABLE "_BA"
108#define balua_create(p) _balua_create(p, BALUA_VERSION)
109
110#define baluaENV_getmetatable(L,mtId) lua_rawgeti(L, BA_ENV_IX, mtId)
111#define baluaENV_checkudata(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,TRUE)
112#define baluaENV_isudtype(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,FALSE)
113#define balua_pushbatab(L) lua_getfield(L,LUA_REGISTRYINDEX,BA_TABLE)
114/* Similar to luaL_newlibtable, but sets batab as upvalue */
115#define balua_newlib(L,l) \
116 (luaL_newlibtable(L,l), balua_pushbatab(L), luaL_setfuncs(L,l,1))
117#define baluaENV_getmutex(L) baluaENV_getparam(L)->mutex
122#define balua_getmutex(L) balua_getparam(L)->mutex
123#define GET_BAMUTEX ThreadMutex* m = baluaENV_getmutex(L)
128#define balua_releasemutex(m) if(m) ThreadMutex_release(m)
132#define balua_setmutex(m) if(m) ThreadMutex_set(m)
133
134#ifdef NDEBUG
135#define balua_check(x,y) x
136#else
137#define balua_check(x,y) baAssert(y == x)
138#endif
139
140#ifndef BALUA_XH
141#define BALUA_XH 0xA5
142#endif
143
144#define dmpstk1(L,x) \
145 HttpTrace_printf(0,"%d: %s %p\n", __LINE__,lua_typename(L,lua_type(L,(x))), \
146 lua_topointer(L,x))
147#define dmpstk2(L,x) { \
148 int ix=lua_absindex(L,x); \
149 HttpTrace_printf(0,"%4d: %2d %s\n",__LINE__,ix,luaL_tolstring(L,x,0)); \
150 lua_pop(L,1); \
151 }while(0)
152#define dmptop(L) \
153 HttpTrace_printf(0,"%d: top %d\n",__LINE__,lua_gettop(L))
154#define dmpTab(L, ix) do { \
155 HttpTrace_printf(0,"%d: TAB %d %p\n", \
156 __LINE__, lua_absindex(L,ix), lua_topointer(L,ix)); \
157 lua_pushnil(L); \
158 while(lua_next(L, ix < 0 ? ix-1: ix) != 0) \
159 { \
160 HttpTrace_printf(0,"\t%s = %s\n", \
161 luaL_tolstring(L,-3,0), \
162 luaL_tolstring(L,-1,0)); \
163 lua_pop(L,3); \
164 } \
165 } while(0)
166
167
168struct BaTimer;
169struct LoginTracker;
170
177typedef struct
178{
179 lua_State* L;
181 struct BaTimer* timer;
186 const U8* zipPubKey;
187 const U8* zipBinPwd;
191
192typedef struct
193{
194 IoIntf* i;
195 int destroy;
196} baio_t;
197
198
199#ifdef BA_LEAK_CHECK
200BA_API void* baLMallocL(lua_State* L, size_t size,const char* file, int line);
201#define baLMalloc(L,size) baLMallocL(L,size,__FILE__,__LINE__)
202#else
209BA_API void* baLMalloc(lua_State* L, size_t size);
210#endif
211
215BA_API lua_State* _balua_create(const BaLua_param* p, int version);
216
223BA_API void balua_close(lua_State* L);
224
240 lua_State* L, U32 noOfLoginTrackerNodes, U32 maxNumberOfLogins,
241 BaTime banTime);
242
247BA_API lua_State* balua_getmainthread(lua_State* L);
248BA_API int balua_typeerror(lua_State *L, int narg, const char *tname);
249#define balua_optboolean(L,narg,def) luaL_opt(L, balua_checkboolean, narg, def)
250BA_API int balua_checkboolean(lua_State *L, int narg);
251BA_API int baluaENV_newmetatable(lua_State *L, int mtid, int inheritmtid);
252BA_API void baluaENV_register(
253 lua_State *L, int mtid, int inheritmtid, const luaL_Reg* lib);
254BA_API void* baluaENV_newuserdata(lua_State *L, int mtid, size_t size);
255BA_API void* _baluaENV_isudtype(lua_State* L, int udIx, int mtid, int check);
256BA_API HttpCommand* baluaENV_checkcmd(lua_State* L, int ix);
261BA_API BaLua_param* balua_getparam(lua_State* L);
262BA_API BaLua_param* baluaENV_getparam(lua_State* L);
263BA_API int balua_errorhandler(lua_State* L);
264BA_API void balua_manageerr(
265 lua_State* L,const char* ewhere,const char* emsg,HttpCommand* cmd);
266BA_API void balua_resumeerr(lua_State* L,const char* ewhere);
278BA_API int balua_loadfile(
279 lua_State *L, const char *filename, struct IoIntf* io, int envix);
280
290BA_API void
291balua_getuservalue(lua_State* L, int index);
292
293
310BA_API int balua_wkRef(lua_State* L);
311
318BA_API int balua_wkRefIx(lua_State* L, int index);
319
325BA_API void balua_wkPush(lua_State* L, int reference);
326
331BA_API void balua_wkUnref(lua_State* L, int reference); /* Weak */
333
334
335
336#ifdef _DOXYGEN
348 lua_State* L, struct IoIntf* io, const char* filename);
349#else
350#define balua_loadconfig(L, io, filename) \
351 balua_loadconfigExt(L, io, filename, 0)
352#endif
353
368 lua_State* L, struct IoIntf* io, const char* filename, int nresults);
369
370
371BA_API IoIntf* baluaENV_checkIoIntf(lua_State *L, int udIx);
372
389 lua_State* L, const char* name, struct IoIntf* newio);
397BA_API struct IoIntf** balua_createiointf(lua_State* L);
398BA_API HttpDir* baluaENV_toDir(lua_State* L, int ix);
399BA_API HttpDir* baluaENV_createDir(lua_State* L,int mtId,size_t dirSize);
400BA_API const char* balua_getStringField(
401 lua_State *L, int ix, const char *k, const char *def);
402BA_API lua_Integer balua_getIntField(
403 lua_State *L, int ix, const char *k, lua_Integer defVal);
404BA_API BaBool balua_getBoolField(
405 lua_State *L, int ix, const char *k, BaBool defVal);
406BA_API int balua_getTabField(lua_State *L, int ix, const char *k);
407BA_API const char* balua_checkStringField(lua_State *L, int ix, const char *k);
408BA_API lua_Integer balua_checkIntField(lua_State *L, int ix, const char *k);
409
410
411/* Internally used by the xrc code */
412#ifndef NO_SHARKSSL
413typedef struct
414{
415 int (*pushCertificate)(lua_State *L, SoDispCon* con);
416 int (*pushCipher)(lua_State *L, SoDispCon* con);
417 void (*unlockSharkSSL)(lua_State *L,struct SharkSsl* super);
418 struct SharkSsl* (*lockSharkSSL)(
419 lua_State *L,int tabIx,SharkSsl_Role role,SharkSsl* lockedShark);
420} LSharkSSLFuncs;
421 /* Auto set if using xrc/lua/lsharkssl.c */
422extern const LSharkSSLFuncs* lSharkSSLFuncs;
423#else
424extern const void* lSharkSSLFuncs;
425#endif
426
427enum BaUserDataTypes {
428 BA_VMPTR=1,
429 BA_IOINTFTAB,
430 BA_DIRTAB,
431 BA_CMDTAB,
432 BA_JSONPARSER,
433 BA_TDIR,
434 BA_TDIR_RSRDR,
435 BA_TDIR_DAV,
436 BA_TRESINTF,
437 BA_TIOINTF,
438 BA_TDIRITER,
439 BA_THTTPCMD,
440 BA_TCOOKIE,
441 BA_TSESSION,
442 BA_TSETRESPONSE,
443 BA_TAUTHORIZERINTF,
444 BA_TLUA_AUTHORIZER,
445 BA_TUSERINTF,
446 BA_TLUA_USER,
447 BA_TJAUTHORIZER,
448 BA_TJUSER,
449 BA_TAUTHENTICATORINTF,
450 BA_TAUTHENTICATOR,
451 BA_TTIMER,
452 BA_TUPLOAD,
453 BA_TASYNCRESP,
454 BA_TDIR_REDIRECTOR,
455 BA_TDIR_TRACELOGGER,
456 BA_DBGMON_NEWIO_CB, /* ldbgmon.c */
457 BA_IOINTFPTRTAB /* balua_installZIO */
458};
459
460
473typedef struct LHttpDir
474{
475 lua_State *LM;
476 HttpDir_Service orgServiceFunc;
477 struct LHttpDir* parent;
478 int parentRef;
479 int funcRef; /* Reference to Lua service function, if any: In registry */
480 int authenticatorRef; /* authenticator if any: In ENV */
481 int authorizerRef; /* authorizer if any: In ENV */
482 int utabRef; /* Table associated with udata saved in BA_WKTABLE */
484
485
486BA_API int LHttpResRdr_loadLsp(
487 lua_State* L, IoIntf* io, const char* pathname, IoStat* st);
491BA_API void balua_ubjson(lua_State* L);
492BA_API void balua_luaio(lua_State* L);
493BA_API void luaopen_ba_redirector(lua_State *L);
494BA_API void ba_ldbgmon(
495 lua_State* L, void (*exitCb)(void*,BaBool), void* exitCbData);
496BA_API void balua_revcon(lua_State* L);
497BA_API void balua_mallinfo(lua_State* L);
498struct CspReader;
499
515 const U8* pubKey, U32 fileSize, struct CspReader* reader);
516struct ZipReader;
517
533BA_API void balua_installZIO(lua_State* L, const char* name, struct ZipReader* reader);
534
535
536#ifdef __cplusplus
537}
538#endif
539
540 /* LSP Plugin */
542
543#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_close(lua_State *L)
Close the BAS Lua VM, run finalizers, and free its copied parameters.
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.
BA_API struct IoIntf ** balua_createiointf(lua_State *L)
Push a Lua I/O userdata whose underlying interface is owned by Lua.
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 BaLua_param * balua_getparam(lua_State *L)
Get the VM's stored parameter copy without changing the stack.
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 a BAS Lua VM.
BA_API lua_State * balua_getmainthread(lua_State *L)
Get the main state without changing the stack.
BA_API int balua_loadfile(lua_State *L, const char *filename, struct IoIntf *io, int envix)
Compile a Lua source or bytecode file without executing it.
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 application memory, retrying after a full Lua GC on allocation failure.
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:2747
uint16_t U16
Unsigned 16-bit integer.
Definition: GenPrimT.h:91
S64 BaTime
An arithmetic type representing calendar time with epoch of 1970-01-01 00:00:00 UTC,...
Definition: GenPrimT.h:103
uint32_t U32
Unsigned 32-bit integer.
Definition: GenPrimT.h:93
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:89
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:118
BA_API int balua_wkRefIx(lua_State *L, int index)
Create a weak reference without popping the referenced value.
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:178
struct BaTimer * timer
Timer bindings activated if not NULL.
Definition: balua.h:181
struct LoginTracker * tracker
Optional login tracker.
Definition: balua.h:184
const U8 * zipBinPwd
Borrowed password bytes, or NULL for no default ZIP password.
Definition: balua.h:187
ThreadMutex * mutex
Derived from server in normal builds; supplied directly in NO_BA_SERVER.
Definition: balua.h:183
const U8 * zipPubKey
Borrowed SharkSSL ECC public key, or NULL to disable ZIP signature checks.
Definition: balua.h:186
BaBool pwdRequired
Nonzero requires encryption on all files in ZIP I/O using this policy.
Definition: balua.h:189
lua_State * L
Input NULL in server builds; existing state in NO_BA_SERVER.
Definition: balua.h:179
IoIntf * vmio
Required VM I/O interface used for Lua resources.
Definition: balua.h:182
HttpServer * server
Borrowed server, or NULL when server bindings are not needed.
Definition: balua.h:180
U16 zipBinPwdLen
Password byte count, matching zipBinPwd.
Definition: balua.h:188
int errHndRef
Initialize to zero; internal ba.seterrh reference.
Definition: balua.h:185
Timer manager used for one-shot and periodic callbacks.
Definition: BaTimer.h:67
Abstract interface class for reading the "dat" file generated by HttpLink.
Definition: CspRunTm.h:126
The HttpCommand class is a container class for the HttpRequest and HttpResponse command pair.
Definition: HttpServer.h:2151
An instance of the HttpDir class, which is a collection of zero or more resources,...
Definition: HttpServer.h:2777
The Web Server.
Definition: HttpServer.h:3393
The IoIntf class specifies an abstract file API, implementations include ZipIo, DiskIo,...
Definition: IoIntf.h:482
Resource information.
Definition: IoIntf.h:170
LHttpDir is the HttpDir instance used by Lua bindings and can be used by advanced Lua bindings creati...
Definition: balua.h:474
The LoginTracker class is an optional security enhancement that can be installed in an instance of on...
Definition: AuthenticatedUser.h:891
Contains information about the physical socket connection.
Definition: SoDispCon.h:120
A mutual exclusion class.
Definition: ThreadLib.h:196
Abstract interface class for reading a ZipFile.
Definition: ZipFileIterator.h:74