Jump The Great Firewall [step14 embed lua], step14lua

Source: Internet
Author: User

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.

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.