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 6674 2026-10-29 17:01:49Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2008 - 2026
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#ifndef BALUA_ENABLE_LEGACY_PEM_KEY_API
46#define BALUA_ENABLE_LEGACY_PEM_KEY_API (!SHARKSSL_PSA_CONSUMER)
47#endif
48#if SHARKSSL_PSA_CONSUMER && BALUA_ENABLE_LEGACY_PEM_KEY_API
49#error Legacy PEM private-key bindings require the native SharkSSL backend
50#endif
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56#include "lua.h"
57#include "lauxlib.h"
58#if BA_ENABLE_SHARKSSL
59#include "SharkSSL.h"
60#endif
61#ifdef __cplusplus
62}
63#endif
64
65#include "AuthenticatedUser.h"
66#include "ThreadLib.h"
67#include "DoubleList.h"
69
70
94#ifdef __cplusplus
95extern "C" {
96#endif
97
98#ifdef BASLIB_VER_NO
99#define BALUA_VERSION BASLIB_VER_NO
100#else
101#define BALUA_VERSION 1
102#endif
103#define BA_ENV_IX lua_upvalueindex(1)
104#define BA_TABLE "_BA"
115#define balua_create(p) _balua_create(p, BALUA_VERSION)
116
117#define baluaENV_getmetatable(L,mtId) lua_rawgeti(L, BA_ENV_IX, mtId)
118#define baluaENV_checkudata(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,TRUE)
119#define baluaENV_isudtype(L,ud,mtid) _baluaENV_isudtype(L,ud,mtid,FALSE)
120#define balua_pushbatab(L) lua_getfield(L,LUA_REGISTRYINDEX,BA_TABLE)
121/* Similar to luaL_newlibtable, but sets batab as upvalue */
122#define balua_newlib(L,l) \
123 (luaL_newlibtable(L,l), balua_pushbatab(L), luaL_setfuncs(L,l,1))
124#define baluaENV_getmutex(L) baluaENV_getparam(L)->mutex
129#define balua_getmutex(L) balua_getparam(L)->mutex
136#define BA_LUA_DECLARE_MUTEX(name, L) ThreadMutex* name = baluaENV_getmutex(L)
141#define balua_releasemutex(m) if(m) ThreadMutex_release(m)
145#define balua_setmutex(m) if(m) ThreadMutex_set(m)
146
147#ifdef NDEBUG
148#define balua_check(x,y) x
149#else
150#define balua_check(x,y) baAssert(y == x)
151#endif
152
153#ifndef BALUA_XH
154#define BALUA_XH 0xA5
155#endif
156
157#define dmpstk1(L,x) \
158 HttpTrace_printf(0,"%d: %s %p\n", __LINE__,lua_typename(L,lua_type(L,(x))), \
159 lua_topointer(L,x))
160#define dmpstk2(L,x) { \
161 int ix=lua_absindex(L,x); \
162 HttpTrace_printf(0,"%4d: %2d %s\n",__LINE__,ix,luaL_tolstring(L,x,0)); \
163 lua_pop(L,1); \
164 }while(0)
165#define dmptop(L) \
166 HttpTrace_printf(0,"%d: top %d\n",__LINE__,lua_gettop(L))
167#define dmpTab(L, ix) do { \
168 HttpTrace_printf(0,"%d: TAB %d %p\n", \
169 __LINE__, lua_absindex(L,ix), lua_topointer(L,ix)); \
170 lua_pushnil(L); \
171 while(lua_next(L, ix < 0 ? ix-1: ix) != 0) \
172 { \
173 HttpTrace_printf(0,"\t%s = %s\n", \
174 luaL_tolstring(L,-3,0), \
175 luaL_tolstring(L,-1,0)); \
176 lua_pop(L,3); \
177 } \
178 } while(0)
179
180
181struct BaTimer;
182struct LoginTracker;
183
190typedef struct
191{
192 lua_State* L;
194 struct BaTimer* timer;
199 const U8* zipPubKey;
200 const U8* zipBinPwd;
204
205typedef struct
206{
207 IoIntf* i;
208 int destroy;
209} baio_t;
210
211
212#ifdef BA_LEAK_CHECK
213BA_API void* baLMallocL(lua_State* L, size_t size,const char* file, int line);
214#define baLMalloc(L,size) baLMallocL(L,size,__FILE__,__LINE__)
215BA_API void* baLReallocL(lua_State* L, void* p, size_t size,
216 const char* file, int line);
217#define baLRealloc(L,p,size) baLReallocL(L,p,size,__FILE__,__LINE__)
218BA_API char* baLStrdupL(lua_State* L, const char* str,
219 const char* file, int line);
220#define baLStrdup(L,str) baLStrdupL(L,str,__FILE__,__LINE__)
221#else
228BA_API void* baLMalloc(lua_State* L, size_t size);
240BA_API void* baLRealloc(lua_State* L, void* p, size_t size);
250BA_API char* baLStrdup(lua_State* L, const char* str);
251#endif
252
256BA_API lua_State* _balua_create(const BaLua_param* p, int version);
257
264BA_API void balua_close(lua_State* L);
265
281 lua_State* L, U32 noOfLoginTrackerNodes, U32 maxNumberOfLogins,
282 BaTime banTime);
283
288BA_API lua_State* balua_getmainthread(lua_State* L);
289BA_API int balua_typeerror(lua_State *L, int narg, const char *tname);
290#define balua_optboolean(L,narg,def) luaL_opt(L, balua_checkboolean, narg, def)
291BA_API int balua_checkboolean(lua_State *L, int narg);
292BA_API int baluaENV_newmetatable(lua_State *L, int mtid, int inheritmtid);
293BA_API void baluaENV_register(
294 lua_State *L, int mtid, int inheritmtid, const luaL_Reg* lib);
295BA_API void* baluaENV_newuserdata(lua_State *L, int mtid, size_t size);
296BA_API void* _baluaENV_isudtype(lua_State* L, int udIx, int mtid, int check);
297BA_API HttpCommand* baluaENV_checkcmd(lua_State* L, int ix);
302BA_API BaLua_param* balua_getparam(lua_State* L);
303BA_API BaLua_param* baluaENV_getparam(lua_State* L);
304BA_API int balua_errorhandler(lua_State* L);
305BA_API void balua_manageerr(
306 lua_State* L,const char* ewhere,const char* emsg,HttpCommand* cmd);
307BA_API void balua_resumeerr(lua_State* L,const char* ewhere);
319BA_API int balua_loadfile(
320 lua_State *L, const char *filename, struct IoIntf* io, int envix);
321
331BA_API void
332balua_getuservalue(lua_State* L, int index);
333
334
351BA_API int balua_wkRef(lua_State* L);
352
359BA_API int balua_wkRefIx(lua_State* L, int index);
360
366BA_API void balua_wkPush(lua_State* L, int reference);
367
372BA_API void balua_wkUnref(lua_State* L, int reference); /* Weak */
374
375
376
377#ifdef _DOXYGEN
389 lua_State* L, struct IoIntf* io, const char* filename);
390#else
391#define balua_loadconfig(L, io, filename) \
392 balua_loadconfigExt(L, io, filename, 0)
393#endif
394
409 lua_State* L, struct IoIntf* io, const char* filename, int nresults);
410
411
412BA_API IoIntf* baluaENV_checkIoIntf(lua_State *L, int udIx);
413
430 lua_State* L, const char* name, struct IoIntf* newio);
438BA_API struct IoIntf** balua_createiointf(lua_State* L);
439BA_API HttpDir* baluaENV_toDir(lua_State* L, int ix);
440BA_API HttpDir* baluaENV_createDir(lua_State* L,int mtId,size_t dirSize);
441BA_API const char* balua_getStringField(
442 lua_State *L, int ix, const char *k, const char *def);
443BA_API lua_Integer balua_getIntField(
444 lua_State *L, int ix, const char *k, lua_Integer defVal);
445BA_API BaBool balua_getBoolField(
446 lua_State *L, int ix, const char *k, BaBool defVal);
447BA_API int balua_getTabField(lua_State *L, int ix, const char *k);
448BA_API const char* balua_checkStringField(lua_State *L, int ix, const char *k);
449BA_API lua_Integer balua_checkIntField(lua_State *L, int ix, const char *k);
450
451
452/* Internally used by the xrc code */
453BAI_FUNC void balua_setEncodedField(
454 lua_State *L, int idx, const U8 k[], size_t kz);
455BAI_FUNC void balua_setEncodedFunc(
456 lua_State *L, lua_CFunction f, const U8 k[], size_t kz, int nup);
457
458#if BA_ENABLE_SHARKSSL
459typedef struct
460{
461 int (*pushCertificate)(lua_State *L, SoDispCon* con);
462 int (*pushCipher)(lua_State *L, SoDispCon* con);
463 void (*unlockSharkSSL)(lua_State *L,struct SharkSsl* super);
464 struct SharkSsl* (*lockSharkSSL)(
465 lua_State *L,int tabIx,SharkSsl_Role role,SharkSsl* lockedShark);
466} LSharkSSLFuncs;
467 /* Auto set if using xrc/lua/lsharkssl.c */
468extern const LSharkSSLFuncs* lSharkSSLFuncs;
469#else
470extern const void* lSharkSSLFuncs;
471#endif
472
473enum BaUserDataTypes {
474 BA_VMPTR=1,
475 BA_IOINTFTAB,
476 BA_DIRTAB,
477 BA_CMDTAB,
478 BA_JSONPARSER,
479 BA_TDIR,
480 BA_TDIR_RSRDR,
481 BA_TDIR_DAV,
482 BA_TRESINTF,
483 BA_TIOINTF,
484 BA_TDIRITER,
485 BA_THTTPCMD,
486 BA_TCOOKIE,
487 BA_TSESSION,
488 BA_TSETRESPONSE,
489 BA_TAUTHORIZERINTF,
490 BA_TLUA_AUTHORIZER,
491 BA_TUSERINTF,
492 BA_TLUA_USER,
493 BA_TJAUTHORIZER,
494 BA_TJUSER,
495 BA_TAUTHENTICATORINTF,
496 BA_TAUTHENTICATOR,
497 BA_TTIMER,
498 BA_TUPLOAD,
499 BA_TASYNCRESP,
500 BA_TDIR_REDIRECTOR,
501 BA_TDIR_TRACELOGGER,
502 BA_DBGMON_NEWIO_CB, /* ldbgmon.c */
503 BA_IOINTFPTRTAB /* balua_installZIO */
504};
505
506
519typedef struct LHttpDir
520{
521 lua_State *LM;
522 HttpDir_Service orgServiceFunc;
523 struct LHttpDir* parent;
524 int parentRef;
525 int funcRef; /* Reference to Lua service function, if any: In registry */
526 int authenticatorRef; /* authenticator if any: In ENV */
527 int authorizerRef; /* authorizer if any: In ENV */
528 int utabRef; /* Table associated with udata saved in BA_WKTABLE */
530
531
532BA_API int LHttpResRdr_loadLsp(
533 lua_State* L, IoIntf* io, const char* pathname, IoStat* st);
537BA_API void balua_ubjson(lua_State* L);
538BA_API void balua_luaio(lua_State* L);
539BA_API void luaopen_ba_redirector(lua_State *L);
540BA_API void ba_ldbgmon(
541 lua_State* L, void (*exitCb)(void*,BaBool), void* exitCbData);
542BA_API void balua_revcon(lua_State* L);
543BA_API void balua_mallinfo(lua_State* L);
544struct CspReader;
545
561 const U8* pubKey, U32 fileSize, struct CspReader* reader);
562struct ZipReader;
563
579BA_API void balua_installZIO(lua_State* L, const char* name, struct ZipReader* reader);
580
581
582#ifdef __cplusplus
583}
584#endif
585
586 /* LSP Plugin */
588
589#endif
BA_API 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 * baLRealloc(lua_State *L, void *p, size_t size)
Resize application memory, retrying after a full Lua GC on allocation failure.
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.
BA_API char * baLStrdup(lua_State *L, const char *str)
Duplicate a string using baLMalloc, including its full-GC retry on failure.
int(* HttpDir_Service)(struct HttpDir *o, const char *relPath, HttpCommand *cmd)
The HttpDir service callback function.
Definition: HttpServer.h:2794
uint16_t U16
Unsigned 16-bit integer.
Definition: GenPrimT.h:94
S64 BaTime
An arithmetic type representing calendar time with epoch of 1970-01-01 00:00:00 UTC,...
Definition: GenPrimT.h:106
uint32_t U32
Unsigned 32-bit integer.
Definition: GenPrimT.h:96
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:92
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:121
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:191
struct BaTimer * timer
Timer bindings activated if not NULL.
Definition: balua.h:194
struct LoginTracker * tracker
Optional login tracker.
Definition: balua.h:197
const U8 * zipBinPwd
Borrowed password bytes, or NULL for no default ZIP password.
Definition: balua.h:200
ThreadMutex * mutex
Derived from server with BA_ENABLE_SERVER=1; supplied directly with BA_ENABLE_SERVER=0.
Definition: balua.h:196
const U8 * zipPubKey
Borrowed SharkSSL ECC public key, or NULL to disable ZIP signature checks.
Definition: balua.h:199
BaBool pwdRequired
Nonzero requires encryption on all files in ZIP I/O using this policy.
Definition: balua.h:202
lua_State * L
Input NULL with BA_ENABLE_SERVER=1; existing state with BA_ENABLE_SERVER=0.
Definition: balua.h:192
IoIntf * vmio
Required VM I/O interface used for Lua resources.
Definition: balua.h:195
HttpServer * server
Borrowed server, or NULL when server bindings are not needed.
Definition: balua.h:193
U16 zipBinPwdLen
Password byte count, matching zipBinPwd.
Definition: balua.h:201
int errHndRef
Initialize to zero; internal ba.seterrh reference.
Definition: balua.h:198
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:2179
An instance of the HttpDir class, which is a collection of zero or more resources,...
Definition: HttpServer.h:2824
Route and process HTTP requests using a borrowed dispatcher.
Definition: HttpServer.h:3463
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:520
The LoginTracker class is an optional security enhancement that can be installed in an instance of on...
Definition: AuthenticatedUser.h:892
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