Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
add initial boilerplate for lua interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
majestrate committed Jan 19, 2016
1 parent 51d9a21 commit 243c898
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions build/cmake/FindLuaJIT.cmake
Original file line number Diff line number Diff line change
@@ -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()

12 changes: 12 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions src/core/lua/Funcs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef SRC_CORE_LUA_FUNCS_H_
#define SRC_CORE_LUA_FUNCS_H_


#include <lua.hpp>
// 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
84 changes: 84 additions & 0 deletions src/core/lua/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "Funcs.h"
#include <boost/asio.hpp>
#include <string>

#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;
}
10 changes: 10 additions & 0 deletions src/core/lua/NetDB.cpp
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 11 additions & 0 deletions src/core/lua/NetDB.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef SRC_CORE_LUA_NETDB_HPP_
#define SRC_CORE_LUA_NETDB_HPP_
#include <string>
#include "RouterInfo.h"

namespace i2lua
{
i2p::data::RouterInfo* FindRouterByHash(const std::string & hash);
}

#endif
Empty file added src/core/lua/Profile.cpp
Empty file.
Empty file added src/core/lua/TunnelBuild.cpp
Empty file.
7 changes: 4 additions & 3 deletions src/core/util/OldLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 243c898

Please sign in to comment.