Jump The Great Firewall [step14 embed lua], step14lua
First, we need to publicize that the website of qtun has been opened and the function is being added gradually.
I. Reason for embedding lua
As the number of configurable parameters of qtun is increasing, it is necessary to write the parameters into the configuration file. Because the C language is not good at string processing, it adds a lightweight lua scripting language and embedding lua is more helpful for third-party plug-ins.
Ii. Code Modification
The script_global_init function script_global_init is called during the init_lua process to initialize the lua environment.int script_global_init(lua_State* lua){ load_lib(lua, "_G", luaopen_base); load_lib(lua, LUA_TABLIBNAME, luaopen_table); load_lib(lua, LUA_STRLIBNAME, luaopen_string); load_lib(lua, LUA_IOLIBNAME, luaopen_io); lua_pushcfunction(lua, _syslog); lua_setglobal(lua, "_syslog"); init_qtun_state(lua); init_qtun_conf(lua); init_qtun_log(lua); return 1;}
It can be seen from the code that: qtun. state and qtun. conf map the C object to lua through retriable. Only the constant of log level is defined in qtun. log.
The script_load_config function script_load_config loads the configuration file by calling the scripts/load_config.lua script.int script_load_config(lua_State* lua, library_conf_t* conf, const char* file_path){ char path[MAX_PATH]; lua_pushlightuserdata(lua, conf); lua_setglobal(lua, "__conf__"); strcpy(path, qtun->this_path); strcat(path, "scripts/load_config.lua"); if (luaL_dofile(lua, path) != 0) { fprintf(stderr, "%s\n", lua_tostring(qtun->lua, -1)); lua_close(qtun->lua); exit(1); } return 1;}
Next let's take a look at the entire process of the main function.
3. lua script
Finally, let's take a look at what these three lua scripts look like.
Iv. complete code
The complete code can be viewed in Step 14.