This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial boilerplate for lua interpreter
- Loading branch information
1 parent
51d9a21
commit 243c898
Showing
10 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters