From 670508016b5472d89e8115f00820d6843733691c Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 4 Feb 2014 14:20:58 -0500 Subject: [PATCH 1/6] SessionConfirmed message --- SSU.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- SSU.h | 4 ++- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/SSU.cpp b/SSU.cpp index 5509b96e..12b9e49b 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -55,6 +55,10 @@ namespace ssu // session created ProcessSessionCreated (buf, len); break; + case eSessionStateCreatedSent: + // session confirmed + ProcessSessionConfirmed (buf, len); + break; default: LogPrint ("SSU state not implemented yet"); } @@ -88,12 +92,35 @@ namespace ssu { m_State = eSessionStateCreatedReceived; LogPrint ("Session created received"); - boost::asio::ip::address_v4 ourAddress (be32toh (*(uint32_t* )(buf + sizeof (SSUHeader) + 257))); - uint16_t ourPort = be16toh (*(uint16_t *)(buf + sizeof (SSUHeader) + 261)); - LogPrint ("Our external address is ", ourAddress.to_string (), ":", ourPort); + uint8_t * ourAddress = buf + sizeof (SSUHeader) + 257; + boost::asio::ip::address_v4 ourIP (be32toh (*(uint32_t* )(ourAddress))); + uint16_t ourPort = be16toh (*(uint16_t *)(ourAddress + 4)); + LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort); + uint32_t relayTag = be32toh (*(uint32_t *)(buf + sizeof (SSUHeader) + 263)); + SendSessionConfirmed (buf + sizeof (SSUHeader), ourAddress, relayTag); } } + void SSUSession::ProcessSessionConfirmed (uint8_t * buf, size_t len) + { + LogPrint ("Process session confirmed"); + if (Validate (buf, len, m_MacKey)) + { + Decrypt (buf, len, m_SessionKey); + SSUHeader * header = (SSUHeader *)buf; + if ((header->flag >> 4) == PAYLOAD_TYPE_SESSION_CONFIRMED) + { + m_State = eSessionStateConfirmedReceived; + LogPrint ("Session confirmed received"); + // TODO: + } + else + LogPrint ("Unexpected payload type ", (int)(header->flag >> 4)); + } + else + LogPrint ("MAC verifcation failed"); + } + void SSUSession::SendSessionRequest () { auto address = m_RemoteRouter ? m_RemoteRouter->GetSSUAddress () : nullptr; @@ -141,7 +168,7 @@ namespace ssu *(uint16_t *)(payload) = htobe16 (m_RemoteEndpoint.port ()); payload += 2; memcpy (signedData + 512, payload - 6, 6); // remote endpoint IP and port - *(uint32_t *)(signedData + 518) = m_Server->GetEndpoint ().address ().to_v4 ().to_ulong (); // our IP + *(uint32_t *)(signedData + 518) = htobe32 (m_Server->GetEndpoint ().address ().to_v4 ().to_ulong ()); // our IP *(uint16_t *)(signedData + 522) = htobe16 (m_Server->GetEndpoint ().port ()); // our port *(uint32_t *)(payload) = 0; // relay tag, always 0 for now payload += 4; @@ -164,6 +191,52 @@ namespace ssu m_Server->Send (buf, 368, m_RemoteEndpoint); } + void SSUSession::SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, uint32_t relayTag) + { + auto address = m_RemoteRouter ? m_RemoteRouter->GetSSUAddress () : nullptr; + if (!address) + { + LogPrint ("Missing remote SSU address"); + return; + } + + uint8_t buf[480 + 18]; + uint8_t * payload = buf + sizeof (SSUHeader); + *payload = 1; // 1 fragment + payload++; // info + size_t identLen = sizeof (i2p::context.GetRouterIdentity ()); // 387 bytes + *(uint16_t *)(payload) = htobe16 (identLen); + payload += 2; // cursize + memcpy (payload, (uint8_t *)&i2p::context.GetRouterIdentity (), identLen); + payload += identLen; + uint32_t signedOnTime = i2p::util::GetSecondsSinceEpoch (); + *(uint32_t *)(payload) = htobe32 (signedOnTime); // signed on time + payload += 4; + size_t paddingSize = ((payload - buf) + 40)%16; + if (paddingSize > 0) paddingSize = 16 - paddingSize; + // TODO: fill padding + payload += paddingSize; // padding size + + // signature + uint8_t signedData[532]; // x,y, our IP, our port, remote IP, remote port, relayTag, our signed on time + memcpy (signedData, i2p::context.GetRouterIdentity ().publicKey, 256); // x + memcpy (signedData + 256, y, 256); // y + memcpy (signedData + 512, ourAddress, 6); // our address/port as seem by party + *(uint32_t *)(signedData + 518) = htobe32 (m_RemoteEndpoint.address ().to_v4 ().to_ulong ()); // remote IP + *(uint16_t *)(signedData + 522) = htobe16 (m_RemoteEndpoint.port ()); // remote port + *(uint32_t *)(signedData + 524) = htobe32 (relayTag); // relay tag + *(uint32_t *)(signedData + 528) = htobe32 (signedOnTime); // signed on time + i2p::context.Sign (signedData, 532, payload); // DSA signature + + uint8_t iv[16]; + CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); + rnd.GenerateBlock (iv, 16); // random iv + // encrypt message with session key + FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_CONFIRMED, buf, 480, m_SessionKey, iv, m_MacKey); + m_State = eSessionStateConfirmedSent; + m_Server->Send (buf, 480, m_RemoteEndpoint); + } + bool SSUSession::ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, i2p::data::RouterInfo& r, uint8_t * buf, size_t len) { auto address = r.GetSSUAddress (); diff --git a/SSU.h b/SSU.h index 2c797804..c33b3f83 100644 --- a/SSU.h +++ b/SSU.h @@ -30,12 +30,12 @@ namespace ssu const uint8_t PAYLOAD_TYPE_SESSION_REQUEST = 0; const uint8_t PAYLOAD_TYPE_SESSION_CREATED = 1; const uint8_t PAYLOAD_TYPE_SESSION_CONFIRMED = 2; - const uint8_t PAYLOAD_TYPE_SESSION_DESTROY = 8; const uint8_t PAYLOAD_TYPE_RELAY_REQUEST = 3; const uint8_t PAYLOAD_TYPE_RELAY_RESPONSE = 4; const uint8_t PAYLOAD_TYPE_RELAY_INTRO = 5; const uint8_t PAYLOAD_TYPE_DATA = 6; const uint8_t PAYLOAD_TYPE_TEST = 7; + const uint8_t PAYLOAD_TYPE_SESSION_DESTROY = 8; enum SessionState { @@ -69,6 +69,8 @@ namespace ssu void SendSessionRequest (); void ProcessSessionCreated (uint8_t * buf, size_t len); void SendSessionCreated (const uint8_t * x); + void ProcessSessionConfirmed (uint8_t * buf, size_t len); + void SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, uint32_t relayTag); bool ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, i2p::data::RouterInfo& r, uint8_t * buf, size_t len); void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, uint8_t * aesKey, uint8_t * iv, uint8_t * macKey); From 82bd8cc69fb593522c23c5b8721049408af4f2aa Mon Sep 17 00:00:00 2001 From: Meeh Date: Wed, 5 Feb 2014 00:15:42 +0100 Subject: [PATCH 2/6] Should fix the segfault at exit --- Queue.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Queue.h b/Queue.h index ae60f099..449ff806 100644 --- a/Queue.h +++ b/Queue.h @@ -99,6 +99,10 @@ namespace util public: MsgQueue (): m_Thread (std::bind (&MsgQueue::Run, this)) {}; + void Stop() + { + m_Thread.detach(); + } private: void Run () From 8f1e300b13b447fa4df974d2d12d1d8f4118fe40 Mon Sep 17 00:00:00 2001 From: Meeh Date: Wed, 5 Feb 2014 00:43:50 +0100 Subject: [PATCH 3/6] Log to file, reload config and daemon mode --- README.md | 17 +++++++++---- i2p.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dc4e0a8c..20d84e5b 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,21 @@ $ ./i2p --host=YOUR_PUBLIC_IP The client should now reseed by itself. -Other options: -* --port= - The port to listen on -* --httpport= - The http port to listen on - - To visit an I2P page, you need to find the b32 address of your destination. After that, go to the webconsole and add it behind the url. (Remove http:// and b32.i2p from the address) This should resulting in for example: http://localhost:7070/4oes3rlgrpbkmzv4lqcfili23h3cvpwslqcfjlk6vvguxyggspwa + +Options +------- + +* --host= - The external IP +* --port= - The port to listen on +* --httpport= - The http port to listen on +* --log= - Enable or disable logging to file. 1 for yes, 0 for no. +* --daemon= - Eanble or disable daemon mode. 1 for yes, 0 for no. + + + diff --git a/i2p.cpp b/i2p.cpp index aed38cd0..991c8b8b 100644 --- a/i2p.cpp +++ b/i2p.cpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include "Log.h" #include "base64.h" #include "Transports.h" @@ -13,6 +16,21 @@ #include "HTTPServer.h" #include "util.h" +void handle_sighup(int n) +{ + if (i2p::util::config::GetArg("daemon", 0) == 1) + { + static bool first=true; + if (first) + { + first=false; + return; + } + } + LogPrint("Reloading config."); + i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); +} + int main( int argc, char* argv[] ) { i2p::util::config::OptionParser(argc,argv); @@ -23,10 +41,51 @@ int main( int argc, char* argv[] ) setlocale(LC_ALL, "Russian"); #endif + LogPrint("\n\n\n\ni2pd starting\n"); LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string()); i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); + struct sigaction sa; + sa.sa_handler = handle_sighup; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART; + if (sigaction(SIGHUP,&sa,0) == -1) + { + LogPrint("Failed to install SIGHUP handler."); + } + + if (i2p::util::config::GetArg("-daemon", 0) == 1) + { + pid_t pid; + pid = fork(); + if (pid > 0) + { + g_Log.Stop(); + return 0; + } + if (pid < 0) + { + return -1; + } + + umask(0); + int sid = setsid(); + if (sid < 0) + { + LogPrint("Error, could not create process group."); + return -1; + } + } + + if (i2p::util::config::GetArg("-log", 0) == 1) + { + std::string logfile = i2p::util::filesystem::GetDataDir().string(); + logfile.append("/debug.log"); + LogPrint("Logging to file enabled."); + freopen(logfile.c_str(),"a",stdout); + } + //TODO: This is an ugly workaround. fix it. //TODO: Autodetect public IP. i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"), @@ -38,12 +97,21 @@ int main( int argc, char* argv[] ) httpServer.Start (); i2p::data::netdb.Start (); i2p::transports.Start (); - i2p::tunnel::tunnels.Start (); - - std::this_thread::sleep_for (std::chrono::seconds(10000)); + i2p::tunnel::tunnels.Start (); + + int running = 1; + while (running) + { + std::this_thread::sleep_for (std::chrono::seconds(1000)); + } + i2p::tunnel::tunnels.Stop (); i2p::transports.Stop (); i2p::data::netdb.Stop (); - httpServer.Stop (); + httpServer.Stop (); + if (i2p::util::config::GetArg("-log", 0) == 1) + { + fclose (stdout); + } return 0; } From bcf10eb979594b823878847c592b70ab6af1928e Mon Sep 17 00:00:00 2001 From: Meeh Date: Wed, 5 Feb 2014 00:50:13 +0100 Subject: [PATCH 4/6] Try not to break windows builds. --- i2p.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/i2p.cpp b/i2p.cpp index 991c8b8b..468adeeb 100644 --- a/i2p.cpp +++ b/i2p.cpp @@ -2,9 +2,13 @@ #include #include #include + +#ifndef _WIN32 #include #include #include +#endif + #include "Log.h" #include "base64.h" #include "Transports.h" @@ -16,6 +20,7 @@ #include "HTTPServer.h" #include "util.h" +#ifndef _WIN32 void handle_sighup(int n) { if (i2p::util::config::GetArg("daemon", 0) == 1) @@ -30,6 +35,7 @@ void handle_sighup(int n) LogPrint("Reloading config."); i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); } +#endif int main( int argc, char* argv[] ) { @@ -46,6 +52,7 @@ int main( int argc, char* argv[] ) LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string()); i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); +#ifndef _WIN32 struct sigaction sa; sa.sa_handler = handle_sighup; sigemptyset(&sa.sa_mask); @@ -77,11 +84,16 @@ int main( int argc, char* argv[] ) return -1; } } +#endif if (i2p::util::config::GetArg("-log", 0) == 1) { std::string logfile = i2p::util::filesystem::GetDataDir().string(); +#ifndef _WIN32 logfile.append("/debug.log"); +#else + logfile.append("\\debug.log"); +#endif LogPrint("Logging to file enabled."); freopen(logfile.c_str(),"a",stdout); } @@ -102,6 +114,7 @@ int main( int argc, char* argv[] ) int running = 1; while (running) { + //TODO Meeh: Find something better to do here. std::this_thread::sleep_for (std::chrono::seconds(1000)); } @@ -109,6 +122,7 @@ int main( int argc, char* argv[] ) i2p::transports.Stop (); i2p::data::netdb.Stop (); httpServer.Stop (); + if (i2p::util::config::GetArg("-log", 0) == 1) { fclose (stdout); From 0dbb2ac55748ab5b28dd3048b599e329060e233d Mon Sep 17 00:00:00 2001 From: Meeh Date: Wed, 5 Feb 2014 01:28:18 +0100 Subject: [PATCH 5/6] Handle shutdown signals --- i2p.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/i2p.cpp b/i2p.cpp index 468adeeb..6636c2b6 100644 --- a/i2p.cpp +++ b/i2p.cpp @@ -20,6 +20,10 @@ #include "HTTPServer.h" #include "util.h" + +// Global +int running = 1; + #ifndef _WIN32 void handle_sighup(int n) { @@ -35,8 +39,13 @@ void handle_sighup(int n) LogPrint("Reloading config."); i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); } +void handle_shutdown(int sig) +{ + running = 0; // Exit loop +} #endif + int main( int argc, char* argv[] ) { i2p::util::config::OptionParser(argc,argv); @@ -84,6 +93,11 @@ int main( int argc, char* argv[] ) return -1; } } + + // Handle shutdown + signal(SIGABRT, &handle_shutdown); + signal(SIGTERM, &handle_shutdown); + signal(SIGINT, &handle_shutdown); #endif if (i2p::util::config::GetArg("-log", 0) == 1) @@ -102,21 +116,20 @@ int main( int argc, char* argv[] ) //TODO: Autodetect public IP. i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"), i2p::util::config::GetArg("-port", 17070)); - int httpport = i2p::util::config::GetArg("-httpport", 7070); - i2p::util::HTTPServer httpServer (httpport); + i2p::util::HTTPServer httpServer (i2p::util::config::GetArg("-httpport", 7070)); httpServer.Start (); i2p::data::netdb.Start (); i2p::transports.Start (); i2p::tunnel::tunnels.Start (); - int running = 1; while (running) { //TODO Meeh: Find something better to do here. - std::this_thread::sleep_for (std::chrono::seconds(1000)); + std::this_thread::sleep_for (std::chrono::seconds(1)); } + LogPrint("Shutdown started."); i2p::tunnel::tunnels.Stop (); i2p::transports.Stop (); From 135b3f1477ecb252c120570894338b17199beac2 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 4 Feb 2014 22:51:46 -0500 Subject: [PATCH 6/6] take UDP address from actual endpoint --- SSU.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SSU.cpp b/SSU.cpp index 12b9e49b..4a9892ab 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -134,7 +134,7 @@ namespace ssu uint8_t * payload = buf + sizeof (SSUHeader); memcpy (payload, i2p::context.GetRouterIdentity ().publicKey, 256); payload[256] = 4; // we assume ipv4 - *(uint32_t *)(payload + 257) = htobe32 (address->host.to_v4 ().to_ulong ()); + *(uint32_t *)(payload + 257) = htobe32 (m_RemoteEndpoint.address ().to_v4 ().to_ulong ()); uint8_t iv[16]; CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();