From 243c898e996f5220e9c474d996e3f3e059fd3483 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 19 Jan 2016 08:57:04 -0500 Subject: [PATCH] add initial boilerplate for lua interpreter --- CMakeLists.txt | 2 + build/cmake/FindLuaJIT.cmake | 67 ++++++++++++++++++++++++++++ src/core/CMakeLists.txt | 12 ++++++ src/core/lua/Funcs.h | 25 +++++++++++ src/core/lua/Main.cpp | 84 ++++++++++++++++++++++++++++++++++++ src/core/lua/NetDB.cpp | 10 +++++ src/core/lua/NetDB.hpp | 11 +++++ src/core/lua/Profile.cpp | 0 src/core/lua/TunnelBuild.cpp | 0 src/core/util/OldLog.h | 7 +-- 10 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 build/cmake/FindLuaJIT.cmake create mode 100644 src/core/lua/Funcs.h create mode 100644 src/core/lua/Main.cpp create mode 100644 src/core/lua/NetDB.cpp create mode 100644 src/core/lua/NetDB.hpp create mode 100644 src/core/lua/Profile.cpp create mode 100644 src/core/lua/TunnelBuild.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f82e8c3c..ac068a87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ option(WITH_OPTIMIZE "Optimization flags" OFF) option(WITH_STATIC "Static build" OFF) option(WITH_TESTS "Build unit tests" OFF) option(WITH_UPNP "Include support for UPnP client" OFF) +option(WITH_LUA "Build lua interpreter" OFF) set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build/cmake/") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) @@ -203,6 +204,7 @@ message(STATUS " OPTIMIZATION : ${WITH_OPTIMIZE}") message(STATUS " STATIC BUILD : ${WITH_STATIC}") message(STATUS " TESTS : ${WITH_TESTS}") message(STATUS " UPnP : ${WITH_UPNP}") +message(STATUS " Lua Interpreter : ${WITH_LUA}") message(STATUS "---------------------------------------") # Handle paths nicely diff --git a/build/cmake/FindLuaJIT.cmake b/build/cmake/FindLuaJIT.cmake new file mode 100644 index 00000000..ac4d2222 --- /dev/null +++ b/build/cmake/FindLuaJIT.cmake @@ -0,0 +1,67 @@ +## +# Copyright (c) 2015-2016, The Kovri I2P Router Project +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are +# permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other +# materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be +# used to endorse or promote products derived from this software without specific +# prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +# - Find LUAJIT + +if(LUAJIT_INCLUDE_DIR) + message(STATUS "Provided LuaJIT headers ${LUAJIT_INCLUDE_DIR}") +else() + find_path(LUAJIT_INCLUDE_DIR luajit.h + /usr/include/luajit-2.0 + /usr/local/include/luajit-2.0 + /opt/local/include/luajit-2.0 + /usr/include/luajit + /usr/local/include/luajit + /opt/local/include/luajit + $ENV{SystemDrive}/luajit + ${PROJECT_SOURCE_DIR}/../../luajit) +endif() + +if(LUAJIT_LIBRARIES) + message(STATUS "Provided LuaJIT libraries ${LUAJIT_LIBRARIES}") +else() + find_library(LUAJIT_LIBRARIES NAMES luajit-5.1 + PATHS + /usr/lib + /usr/local/lib + /opt/local/lib + $ENV{SystemDrive}/luajit/lib + $ENV{LUAJIT}/lib) + +endif(LUAJIT_LIBRARIES) + +if(LUAJIT_INCLUDE_DIR AND LUAJIT_LIBRARIES) + set(LUAJIT_FOUND TRUE) + message(STATUS "Found LuaJIT headers: ${LUAJIT_INCLUDE_DIR} Library: ${LUAJIT_LIBRARIES}") +else() + set(LUAJIT_FOUND FALSE) + message(SEND_ERROR "LuaJIT not found.") +endif() + diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d5c3528e..e3e46658 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -108,6 +108,18 @@ if(WITH_LIBRARY) RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + + if(WITH_LUA) + find_package("LuaJIT") + set(LUA_NAME "i2lua") + set(LUA_SRC + "lua/Main.cpp" + ) + include_directories(${LUAJIT_INCLUDE_DIR}) + add_executable(${LUA_NAME} ${LUA_SRC}) + target_link_libraries(${LUA_NAME} ${LUAJIT_LIBRARIES} ${CORE_NAME}) + endif() + endif() # vim: noai:ts=2:sw=2 diff --git a/src/core/lua/Funcs.h b/src/core/lua/Funcs.h new file mode 100644 index 00000000..4008e228 --- /dev/null +++ b/src/core/lua/Funcs.h @@ -0,0 +1,25 @@ +#ifndef SRC_CORE_LUA_FUNCS_H_ +#define SRC_CORE_LUA_FUNCS_H_ + + +#include +// initialize the i2p router parameters +int kovri_init(lua_State* L); +// run the i2p router +int kovri_start(lua_State* L); +// set a hook function that does tunnel building strategy +int kovri_set_tunnel_build_strategy(lua_State* L); +// get a RI given its hash as a string +int kovri_get_ri_by_hash(lua_State* L); +// get a random RI +int kovri_get_ri_random(lua_State* L); + +luaL_Reg funcs[] = { + {"startRouter", kovri_start}, + {"setBuildStrategy", kovri_set_tunnel_build_strategy}, + {"getRouterByHash", kovri_get_ri_by_hash}, + {"getRandomHash", kovri_get_ri_random}, + {0, 0} +}; + +#endif diff --git a/src/core/lua/Main.cpp b/src/core/lua/Main.cpp new file mode 100644 index 00000000..fd7ff938 --- /dev/null +++ b/src/core/lua/Main.cpp @@ -0,0 +1,84 @@ +#include "Funcs.h" +#include +#include + +#include "NetworkDatabase.h" +#include "RouterContext.h" +#include "transport/Transports.h" +#include "tunnel/Tunnel.h" +// +// entry point for luajit i2p router +// +int main(int argc, char * argv[]) { + (void) argc; + (void) argv; + lua_State* l = luaL_newstate(); + luaL_openlibs(l); + luaL_register(l, "i2lua", funcs); +} + +int kovri_init(lua_State* L) { + int n = lua_gettop(L); // num of args + if (n != 1) { + lua_pushliteral(L, "incorrect number of arguments"); + lua_error(L); + } else { + + std::string host = "0.0.0.0"; + int port = 0; + bool v6 = false; + bool floodfill = false; + if (!lua_isnumber(L, 1)) { + lua_pushliteral(L, "invalid argument, not an int"); + lua_error(L); + } else { + lua_Number l_port = lua_tonumber(L, 1); + port = (int)l_port; + if ( port > 0 ) { + i2p::context.UpdatePort(port); + i2p::context.UpdateAddress(boost::asio::ip::address::from_string(host)); + i2p::context.SetSupportsV6(v6); + i2p::context.SetFloodfill(floodfill); + i2p::context.SetHighBandwidth(); + } else { + lua_pushliteral(L, "invalid argument, not an int"); + lua_error(L); + } + } + } + return 0; +} + +int kovri_start(lua_State* L) { + try { + + i2p::data::netdb.Start(); + i2p::transport::transports.Start(); + i2p::tunnel::tunnels.Start(); + + } catch( std::runtime_error & ex ) { + lua_pushstring(L, ex.what()); + lua_error(L); + } + return 0; +} + +//TODO: implement +int kovri_set_tunnel_build_strategy(lua_State* L) { + (void) L; + return 0; +} + + +int kovri_get_ri_random(lua_State* L) { + auto ptr = i2p::data::netdb.GetRandomRouter(); + const void * vptr = ptr.get(); + lua_pushlightuserdata(L, (void*)vptr); + return 1; +} + +// TODO: implement +int kovri_get_ri_by_hash(lua_State* L) { + (void) L; + return 0; +} diff --git a/src/core/lua/NetDB.cpp b/src/core/lua/NetDB.cpp new file mode 100644 index 00000000..a33eaf8e --- /dev/null +++ b/src/core/lua/NetDB.cpp @@ -0,0 +1,10 @@ +#include "lua/NetDB.hpp" +#include "NetworkDatabase.h" + +namespace i2lua +{ + i2p::data::RouterInfo* FindRouterByHash(const std::string & hash) { + i2p::data::RouterInfo* ri = new i2p::data::RouterInfo; + ri->Load + } +} diff --git a/src/core/lua/NetDB.hpp b/src/core/lua/NetDB.hpp new file mode 100644 index 00000000..1fd4b256 --- /dev/null +++ b/src/core/lua/NetDB.hpp @@ -0,0 +1,11 @@ +#ifndef SRC_CORE_LUA_NETDB_HPP_ +#define SRC_CORE_LUA_NETDB_HPP_ +#include +#include "RouterInfo.h" + +namespace i2lua +{ + i2p::data::RouterInfo* FindRouterByHash(const std::string & hash); +} + +#endif diff --git a/src/core/lua/Profile.cpp b/src/core/lua/Profile.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/core/lua/TunnelBuild.cpp b/src/core/lua/TunnelBuild.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/core/util/OldLog.h b/src/core/util/OldLog.h index 9b4fbcb7..bf189653 100644 --- a/src/core/util/OldLog.h +++ b/src/core/util/OldLog.h @@ -75,9 +75,10 @@ void DeprecatedLogPrint( auto l = kovri::log::Log::Get(); if (l == nullptr) { // fallback logging to std::clog - std::clog << "!!! "; - DeprecatedLog(std::clog, args...); - std::clog << std::endl; + // let's not anymore actually ~ psi + // std::clog << "!!! "; + // DeprecatedLog(std::clog, args...); + // std::clog << std::endl; } else { auto log = l->Default(); if (level == eLogDebug) {