diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f472b04d..1663053de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -222,12 +222,6 @@ set(LIB_SRC llarp/service.cpp llarp/transit_hop.cpp llarp/testnet.c - llarp/api/client.cpp - llarp/api/message.cpp - llarp/api/parser.cpp - llarp/api/messages/ack.cpp - llarp/api/messages/keepalive.cpp - llarp/api/messages/spawn.cpp llarp/dht/context.cpp llarp/dht/decode.cpp llarp/dht/dht_immediate.cpp @@ -267,7 +261,6 @@ set(DNS_SRC set(TEST_SRC test/main.cpp - test/api_unittest.cpp test/base32_unittest.cpp test/dht_unittest.cpp test/encrypted_frame_unittest.cpp diff --git a/client/main.cpp b/client/main.cpp index 25e429489..fc33c5182 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,21 +1,8 @@ #include -#include #include int main(int argc, char* argv[]) { - cSetLogLevel(eLogDebug); - std::string url = llarp::api::DefaultURL; - if(argc > 1) - { - url = argv[1]; - } - llarp::api::Client cl("hiddenservice"); - if(!cl.Start(url)) - { - llarp::LogError("failed to start session"); - return 1; - } - return cl.Mainloop(); + return 1; } \ No newline at end of file diff --git a/include/llarp/api.hpp b/include/llarp/api.hpp deleted file mode 100644 index 189e01755..000000000 --- a/include/llarp/api.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef LLARP_API_HPP -#define LLARP_API_HPP - -#include -#include -namespace llarp -{ - namespace api - { - const char DefaultURL[] = "127.0.0.1:34567"; - } -} // namespace llarp - -#endif \ No newline at end of file diff --git a/include/llarp/api/base.hpp b/include/llarp/api/base.hpp deleted file mode 100644 index ab4995812..000000000 --- a/include/llarp/api/base.hpp +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef LLARP_API_BASE_HPP -#define LLARP_API_BASE_HPP -#include -#include -#include -#include -#include - -namespace llarp -{ - namespace api - { - template < typename Handler > - struct Base - { - Base(llarp_ev_loop* evloop, Handler* handler) : m_Handler(handler) - { - loop = evloop; - worker = llarp_init_same_process_threadpool(); - logic = llarp_init_single_process_logic(worker); - } - - static void - HandleRecv(llarp_udp_io* u, const sockaddr* from, const void* buf, - ssize_t sz) - { - static_cast< Base< Handler >* >(u->user)->RecvFrom(from, buf, sz); - } - - void - RecvFrom(const sockaddr* from, const void* b, ssize_t sz) - { - if(from->sa_family != AF_INET - || ((sockaddr_in*)from)->sin_addr.s_addr != apiAddr.sin_addr.s_addr - || ((sockaddr_in*)from)->sin_port != apiAddr.sin_port) - { - // address missmatch - llarp::LogWarn("got packet from bad address"); - return; - } - llarp_buffer_t buf; - buf.base = (byte_t*)b; - buf.cur = buf.base; - buf.sz = sz; - IMessage* msg = m_MessageParser.ParseMessage(buf); - if(msg) - { - m_Handler->HandleMessage(msg); - delete msg; - } - else - llarp::LogWarn("Got Invalid Message"); - } - - bool - BindDefault() - { - return BindAddress(INADDR_LOOPBACK, 0); - } - - bool - BindAddress(in_addr_t addr, in_port_t port) - { - ouraddr.sin_family = AF_INET; - ouraddr.sin_addr.s_addr = htonl(addr); - ouraddr.sin_port = htons(port); - udp.user = this; - udp.recvfrom = &HandleRecv; - return llarp_ev_add_udp(loop, &udp, (const sockaddr*)&ouraddr) != -1; - } - - bool - SendMessage(const IMessage* msg) - { - llarp_buffer_t buf; - byte_t tmp[1500]; - buf.base = tmp; - buf.cur = buf.base; - buf.sz = sizeof(tmp); - if(msg->BEncode(&buf)) - { - buf.sz = buf.cur - buf.base; - return llarp_ev_udp_sendto(&udp, (const sockaddr*)&apiAddr, buf.base, - buf.sz) - != -1; - } - llarp::LogError("Failed to encode message"); - return false; - } - - int - Mainloop() - { - llarp_ev_loop_run_single_process(loop, worker, logic); - return 0; - } - - llarp_threadpool* worker; - llarp_logic* logic; - llarp_ev_loop* loop; - sockaddr_in ouraddr; - sockaddr_in apiAddr; - llarp_udp_io udp; - MessageParser m_MessageParser; - Handler* m_Handler; - }; - } // namespace api -} // namespace llarp - -#endif \ No newline at end of file diff --git a/include/llarp/api/client.hpp b/include/llarp/api/client.hpp deleted file mode 100644 index 6566167ac..000000000 --- a/include/llarp/api/client.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef LLARP_API_CLIENT_HPP -#define LLARP_API_CLIENT_HPP - -#include - -namespace llarp -{ - namespace api - { - struct ClientPImpl; - - struct Client - { - Client(const std::string& name); - ~Client(); - - bool - Start(const std::string& apiURL); - - int - Mainloop(); - - private: - ClientPImpl* m_Impl; - }; - - } // namespace api -} // namespace llarp -#endif \ No newline at end of file diff --git a/include/llarp/api/messages.hpp b/include/llarp/api/messages.hpp deleted file mode 100644 index d69c6be8a..000000000 --- a/include/llarp/api/messages.hpp +++ /dev/null @@ -1,146 +0,0 @@ -#ifndef LLARP_API_MESSAGES_HPP -#define LLARP_API_MESSAGES_HPP - -#include -#include -#include -#include -#include - -namespace llarp -{ - namespace api - { - // forward declare - struct Client; - struct Server; - - /// base message - struct IMessage : public IBEncodeMessage - { - uint64_t seqno = 0; - llarp::ShortHash hash; - - virtual ~IMessage(){}; - - // the function name this message belongs to - virtual std::string - FunctionName() const = 0; - - bool - BEncode(llarp_buffer_t* buf) const; - - virtual bool - DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf); - - /// encode the dictionary members after the A value and before the Y value - virtual bool - EncodeParams(llarp_buffer_t* buf) const = 0; - - bool - IsWellFormed(llarp_crypto* c, const std::string& password); - - void - CalculateHash(llarp_crypto* c, const std::string& password); - }; - - /// a "yes we got your command" type message - struct AckMessage : public IMessage - { - ~AckMessage(); - - bool - EncodeParams(llarp_buffer_t* buf) const; - - std::string - FunctionName() const - { - return "ack"; - } - }; - - // spawn hidden service message - struct SpawnMessage : public IMessage - { - ~SpawnMessage(); - - std::string SessionName; - llarp::service::ServiceInfo Info; - - bool - DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf); - - bool - EncodeParams(llarp_buffer_t* buf) const; - - std::string - FunctionName() const - { - return "spawn"; - } - }; - - /// a keepalive ping - struct KeepAliveMessage : public IMessage - { - ~KeepAliveMessage(); - - bool - EncodeParams(llarp_buffer_t* buf) const; - - std::string - FunctionName() const - { - return "keepalive"; - } - }; - - /// end a session with the router - struct DestroySessionMessage : public IMessage - { - }; - - /// base messgae type for hidden service control and transmission - struct HSMessage : public IMessage - { - llarp::PubKey pubkey; - llarp::Signature sig; - - /// validate signature on message (server side) - bool - SignatureIsValid(llarp_crypto* crypto) const; - - /// sign message using secret key (client side) - bool - SignMessge(llarp_crypto* crypto, byte_t* seckey); - }; - - /// create a new hidden service - struct CreateServiceMessgae : public HSMessage - { - }; - - /// end an already created hidden service we created - struct DestroyServiceMessage : public HSMessage - { - }; - - /// start lookup of another service's descriptor - struct LookupServiceMessage : public IMessage - { - }; - - /// publish our hidden service's descriptor - struct PublishServiceMessage : public IMessage - { - }; - - /// send pre encrypted data down a path we own - struct SendPathDataMessage : public IMessage - { - }; - - } // namespace api -} // namespace llarp - -#endif \ No newline at end of file diff --git a/include/llarp/api/parser.hpp b/include/llarp/api/parser.hpp deleted file mode 100644 index 4879fde20..000000000 --- a/include/llarp/api/parser.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef LLARP_API_PARSER_HPP -#define LLARP_API_PARSER_HPP -#include -#include - -namespace llarp -{ - namespace api - { - struct MessageParser - { - MessageParser(); - - IMessage * - ParseMessage(llarp_buffer_t buf); - - private: - static bool - OnKey(dict_reader *r, llarp_buffer_t *key); - IMessage *msg = nullptr; - dict_reader r; - }; - } // namespace api -} // namespace llarp - -#endif \ No newline at end of file diff --git a/include/llarp/api/server.hpp b/include/llarp/api/server.hpp deleted file mode 100644 index 3732a47b8..000000000 --- a/include/llarp/api/server.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef LLARP_API_SERVER_HPP -#define LLARP_API_SERVER_HPP - -#include -#include -#include - -namespace llarp -{ - namespace api - { - struct ServerPImpl; - - struct Server - { - Server(llarp_router* r); - ~Server(); - - bool - Bind(const std::string& url, llarp_ev_loop* loop); - - private: - ServerPImpl* m_Impl; - }; - - } // namespace api -} // namespace llarp - -#endif \ No newline at end of file diff --git a/llarp/api/client.cpp b/llarp/api/client.cpp deleted file mode 100644 index e232d4705..000000000 --- a/llarp/api/client.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace llarp -{ - namespace api - { - struct ClientPImpl - { - ClientPImpl(const std::string& sessionName, llarp_ev_loop* evloop) - : m_Base(evloop, this), m_SessionName(sessionName) - { - } - - ~ClientPImpl() - { - llarp_ev_loop_free(&m_Base.loop); - } - - bool - StartSession(const std::string& addr, uint16_t port) - { - inet_pton(AF_INET, addr.c_str(), &m_Base.apiAddr.sin_addr.s_addr); - m_Base.apiAddr.sin_family = AF_INET; - m_Base.apiAddr.sin_port = htons(port); - SpawnMessage msg; - msg.seqno = 0; - msg.SessionName = m_SessionName; - return m_Base.SendMessage(&msg); - } - - void - HandleMessage(const IMessage* msg) - { - } - - Base< ClientPImpl > m_Base; - std::string m_SessionName; - }; - - Client::Client(const std::string& name) - { - llarp_ev_loop* loop = nullptr; - llarp_ev_loop_alloc(&loop); - m_Impl = new ClientPImpl(name, loop); - } - - Client::~Client() - { - delete m_Impl; - } - - bool - Client::Start(const std::string& url) - { - if(url.find(":") == std::string::npos) - return false; - if(!m_Impl->m_Base.BindDefault()) - return false; - llarp::LogDebug("Bound Socket"); - std::string addr = url.substr(0, url.find(":")); - std::string strport = url.substr(url.find(":") + 1); - int port = std::stoi(strport); - if(port == -1) - { - llarp::LogError("bad port: ", strport); - return false; - } - llarp::LogDebug("starting session with ", addr, ":", port); - return m_Impl->StartSession(addr, port); - } - - int - Client::Mainloop() - { - return m_Impl->m_Base.Mainloop(); - } - - } // namespace api -} // namespace llarp \ No newline at end of file diff --git a/llarp/api/message.cpp b/llarp/api/message.cpp deleted file mode 100644 index 80cd03e61..000000000 --- a/llarp/api/message.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include - -namespace llarp -{ - namespace api - { - bool - IMessage::BEncode(llarp_buffer_t* buf) const - { - if(!bencode_start_dict(buf)) - return false; - if(!BEncodeWriteDictString("A", FunctionName(), buf)) - return false; - if(!EncodeParams(buf)) - return false; - if(!BEncodeWriteDictInt("Y", seqno, buf)) - return false; - if(!BEncodeWriteDictEntry("Z", hash, buf)) - return false; - return bencode_end(buf); - } - - bool - IMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) - { - bool read = false; - if(!BEncodeMaybeReadDictInt("Y", seqno, read, key, val)) - return false; - if(!BEncodeMaybeReadDictEntry("Z", hash, read, key, val)) - return false; - return read; - } - - bool - IMessage::IsWellFormed(llarp_crypto* crypto, const std::string& password) - { - // hash password - llarp::ShortHash secret; - llarp_buffer_t passbuf; - passbuf.base = (byte_t*)password.c_str(); - passbuf.cur = passbuf.base; - passbuf.sz = password.size(); - crypto->shorthash(secret, passbuf); - - llarp::ShortHash digest, tmpHash; - // save hash - tmpHash = hash; - // zero hash - hash.Zero(); - - // bencode - byte_t tmp[1500]; - llarp_buffer_t buf; - buf.base = tmp; - buf.cur = buf.base; - buf.sz = sizeof(tmp); - if(!BEncode(&buf)) - return false; - - // rewind buffer - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - // calculate message auth - crypto->hmac(digest, buf, secret); - // restore hash - hash = tmpHash; - return tmpHash == digest; - } - - void - IMessage::CalculateHash(llarp_crypto* crypto, const std::string& password) - { - // hash password - llarp::ShortHash secret; - llarp_buffer_t passbuf; - passbuf.base = (byte_t*)password.c_str(); - passbuf.cur = passbuf.base; - passbuf.sz = password.size(); - crypto->shorthash(secret, passbuf); - - // llarp::ShortHash digest; - // zero hash - hash.Zero(); - - // bencode - byte_t tmp[1500]; - llarp_buffer_t buf; - buf.base = tmp; - buf.cur = buf.base; - buf.sz = sizeof(tmp); - if(BEncode(&buf)) - { - // rewind buffer - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - // calculate message auth - crypto->hmac(hash, buf, secret); - } - } - } // namespace api -} // namespace llarp diff --git a/llarp/api/messages/ack.cpp b/llarp/api/messages/ack.cpp deleted file mode 100644 index 16576a426..000000000 --- a/llarp/api/messages/ack.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include - -namespace llarp -{ - namespace api - { - AckMessage::~AckMessage() - { - } - - bool - AckMessage::EncodeParams(llarp_buffer_t *buf) const - { - return true; - } - - } // namespace api -} // namespace llarp \ No newline at end of file diff --git a/llarp/api/messages/keepalive.cpp b/llarp/api/messages/keepalive.cpp deleted file mode 100644 index f55217e6e..000000000 --- a/llarp/api/messages/keepalive.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include - -namespace llarp -{ - namespace api - { - KeepAliveMessage::~KeepAliveMessage() - { - } - - bool - KeepAliveMessage::EncodeParams(llarp_buffer_t *buf) const - { - return true; - } - - } // namespace api -} // namespace llarp \ No newline at end of file diff --git a/llarp/api/messages/spawn.cpp b/llarp/api/messages/spawn.cpp deleted file mode 100644 index 5239d0284..000000000 --- a/llarp/api/messages/spawn.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include - -namespace llarp -{ - namespace api - { - SpawnMessage::~SpawnMessage() - { - } - - bool - SpawnMessage::EncodeParams(llarp_buffer_t *buf) const - { - return true; - } - - bool - SpawnMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val) - { - if(llarp_buffer_eq(key, "N")) - { - llarp_buffer_t strbuf; - if(!bencode_read_string(val, &strbuf)) - return false; - SessionName = std::string((char *)strbuf.cur, strbuf.sz); - return true; - } - if(llarp_buffer_eq(key, "S")) - { - return Info.BDecode(val); - } - return IMessage::DecodeKey(key, val); - } - - } // namespace api -} // namespace llarp \ No newline at end of file diff --git a/llarp/api/parser.cpp b/llarp/api/parser.cpp deleted file mode 100644 index 6cfddaf27..000000000 --- a/llarp/api/parser.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include -#include -#include - -namespace llarp -{ - namespace api - { - std::map< std::string, std::function< IMessage*() > > funcmap = { - {"ack", []() { return new AckMessage(); }}, - {"keepalive", []() { return new KeepAliveMessage(); }}, - {"spawn", []() { return new SpawnMessage(); }}}; - - MessageParser::MessageParser() - { - r.user = this; - r.on_key = &OnKey; - } - - bool - MessageParser::OnKey(dict_reader* r, llarp_buffer_t* key) - { - MessageParser* self = static_cast< MessageParser* >(r->user); - if(self->msg == nullptr && key == nullptr) // empty message - return false; - if(self->msg == nullptr && key) - { - // first message, function name - if(!llarp_buffer_eq(*key, "A")) - return false; - llarp_buffer_t strbuf; - if(!bencode_read_string(r->buffer, &strbuf)) - return false; - std::string funcname((char*)strbuf.cur, strbuf.sz); - auto itr = funcmap.find(funcname); - if(itr == funcmap.end()) - return false; - self->msg = itr->second(); - return true; - } - else if(self->msg && key) - { - return self->msg->DecodeKey(*key, r->buffer); - } - else if(self->msg && key == nullptr) - { - return true; - } - return false; - } - - IMessage* - MessageParser::ParseMessage(llarp_buffer_t buf) - { - if(bencode_read_dict(&buf, &r)) - return msg; - return nullptr; - } - - } // namespace api -} // namespace llarp \ No newline at end of file diff --git a/llarp/api/server.cpp b/llarp/api/server.cpp deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/api_unittest.cpp b/test/api_unittest.cpp deleted file mode 100644 index 1b57ad825..000000000 --- a/test/api_unittest.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -class APITest : public ::testing::Test -{ - public: - llarp_crypto crypto; - std::string apiPassword = "password"; - APITest() - { - llarp_crypto_libsodium_init(&crypto); - } - - ~APITest() - { - } -}; - -TEST_F(APITest, TestMessageWellFormed) -{ - llarp::api::AckMessage msg; - msg.seqno = 1; - msg.CalculateHash(&crypto, apiPassword); - ASSERT_TRUE(msg.IsWellFormed(&crypto, apiPassword)); -}; \ No newline at end of file