mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-09 12:31:03 +00:00
Merge pull request #11 from orignal/master
Merge pull request #11 from orignal/master
This commit is contained in:
commit
9f60b50a63
60
SSU.cpp
60
SSU.cpp
@ -47,6 +47,11 @@ namespace ssu
|
|||||||
{
|
{
|
||||||
switch (m_State)
|
switch (m_State)
|
||||||
{
|
{
|
||||||
|
case eSessionStateEstablised:
|
||||||
|
// most common case
|
||||||
|
ProcessMessage (buf, len);
|
||||||
|
break;
|
||||||
|
// establishing
|
||||||
case eSessionStateUnknown:
|
case eSessionStateUnknown:
|
||||||
// session request
|
// session request
|
||||||
ProcessSessionRequest (buf, len, senderEndpoint);
|
ProcessSessionRequest (buf, len, senderEndpoint);
|
||||||
@ -64,6 +69,34 @@ namespace ssu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SSUSession::ProcessMessage (uint8_t * buf, size_t len)
|
||||||
|
{
|
||||||
|
if (Validate (buf, len, m_MacKey))
|
||||||
|
{
|
||||||
|
Decrypt (buf, len, m_SessionKey);
|
||||||
|
SSUHeader * header = (SSUHeader *)buf;
|
||||||
|
uint8_t payloadType = header->flag >> 4;
|
||||||
|
switch (payloadType)
|
||||||
|
{
|
||||||
|
case PAYLOAD_TYPE_DATA:
|
||||||
|
LogPrint ("SSU data received");
|
||||||
|
ProcessData (buf + sizeof (SSUHeader), len - sizeof (SSUHeader));
|
||||||
|
break;
|
||||||
|
case PAYLOAD_TYPE_TEST:
|
||||||
|
LogPrint ("SSU test received");
|
||||||
|
break;
|
||||||
|
case PAYLOAD_TYPE_SESSION_DESTROY:
|
||||||
|
LogPrint ("SSU session destroy received");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
LogPrint ("Unexpected SSU payload type ", (int)payloadType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: try intro key as well
|
||||||
|
else
|
||||||
|
LogPrint ("MAC verifcation failed");
|
||||||
|
}
|
||||||
|
|
||||||
void SSUSession::ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
void SSUSession::ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
||||||
{
|
{
|
||||||
LogPrint ("Process session request");
|
LogPrint ("Process session request");
|
||||||
@ -98,6 +131,7 @@ namespace ssu
|
|||||||
LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort);
|
LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort);
|
||||||
uint32_t relayTag = be32toh (*(uint32_t *)(buf + sizeof (SSUHeader) + 263));
|
uint32_t relayTag = be32toh (*(uint32_t *)(buf + sizeof (SSUHeader) + 263));
|
||||||
SendSessionConfirmed (buf + sizeof (SSUHeader), ourAddress, relayTag);
|
SendSessionConfirmed (buf + sizeof (SSUHeader), ourAddress, relayTag);
|
||||||
|
m_State = eSessionStateEstablised;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,6 +147,7 @@ namespace ssu
|
|||||||
m_State = eSessionStateConfirmedReceived;
|
m_State = eSessionStateConfirmedReceived;
|
||||||
LogPrint ("Session confirmed received");
|
LogPrint ("Session confirmed received");
|
||||||
// TODO:
|
// TODO:
|
||||||
|
m_State = eSessionStateEstablised;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4));
|
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4));
|
||||||
@ -327,6 +362,31 @@ namespace ssu
|
|||||||
// TODO:
|
// TODO:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SSUSession::ProcessData (uint8_t * buf, size_t len)
|
||||||
|
{
|
||||||
|
//uint8_t * start = buf;
|
||||||
|
uint8_t flag = *buf;
|
||||||
|
buf++;
|
||||||
|
LogPrint ("Process SSU data flags=", (int)flag);
|
||||||
|
uint8_t numFragments = *buf; // number of fragments
|
||||||
|
buf++;
|
||||||
|
for (int i = 0; i < numFragments; i++)
|
||||||
|
{
|
||||||
|
uint32_t msgID = be32toh (*(uint32_t *)buf); // message ID
|
||||||
|
buf += 4;
|
||||||
|
uint8_t frag[4];
|
||||||
|
frag[0] = 0;
|
||||||
|
memcpy (frag + 1, buf, 3);
|
||||||
|
buf += 3;
|
||||||
|
uint32_t fragmentInfo = be32toh (*(uint32_t *)frag); // fragment info
|
||||||
|
uint16_t fragmentSize = fragmentInfo & 0x1FFF; // bits 0 - 13
|
||||||
|
bool isLast = fragmentInfo & 0x010000; // bit 16
|
||||||
|
uint8_t fragmentNum = fragmentInfo >> 17; // bits 23 - 17
|
||||||
|
LogPrint ("SSU data fragment ", (int)fragmentNum, " of message ", msgID, " size=", (int)fragmentSize, isLast ? " last" : " non-last");
|
||||||
|
buf += fragmentSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SSUServer::SSUServer (boost::asio::io_service& service, int port):
|
SSUServer::SSUServer (boost::asio::io_service& service, int port):
|
||||||
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_Socket (service, m_Endpoint)
|
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_Socket (service, m_Endpoint)
|
||||||
{
|
{
|
||||||
|
2
SSU.h
2
SSU.h
@ -65,12 +65,14 @@ namespace ssu
|
|||||||
|
|
||||||
void CreateAESandMacKey (uint8_t * pubKey, uint8_t * aesKey, uint8_t * macKey);
|
void CreateAESandMacKey (uint8_t * pubKey, uint8_t * aesKey, uint8_t * macKey);
|
||||||
|
|
||||||
|
void ProcessMessage (uint8_t * buf, size_t len); // call for established session
|
||||||
void ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
void ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
||||||
void SendSessionRequest ();
|
void SendSessionRequest ();
|
||||||
void ProcessSessionCreated (uint8_t * buf, size_t len);
|
void ProcessSessionCreated (uint8_t * buf, size_t len);
|
||||||
void SendSessionCreated (const uint8_t * x);
|
void SendSessionCreated (const uint8_t * x);
|
||||||
void ProcessSessionConfirmed (uint8_t * buf, size_t len);
|
void ProcessSessionConfirmed (uint8_t * buf, size_t len);
|
||||||
void SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, uint32_t relayTag);
|
void SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, uint32_t relayTag);
|
||||||
|
void ProcessData (uint8_t * buf, size_t len);
|
||||||
|
|
||||||
bool ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, i2p::data::RouterInfo& r, uint8_t * buf, size_t len);
|
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);
|
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, uint8_t * aesKey, uint8_t * iv, uint8_t * macKey);
|
||||||
|
89
i2p.cpp
89
i2p.cpp
@ -29,23 +29,29 @@
|
|||||||
int running = 1;
|
int running = 1;
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
void handle_sighup(int n)
|
void handle_signal(int sig)
|
||||||
{
|
{
|
||||||
if (i2p::util::config::GetArg("daemon", 0) == 1)
|
switch (sig)
|
||||||
{
|
{
|
||||||
static bool first=true;
|
case SIGHUP:
|
||||||
if (first)
|
if (i2p::util::config::GetArg("daemon", 0) == 1)
|
||||||
{
|
{
|
||||||
first=false;
|
static bool first=true;
|
||||||
return;
|
if (first)
|
||||||
}
|
{
|
||||||
|
first=false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LogPrint("Reloading config.");
|
||||||
|
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
|
||||||
|
break;
|
||||||
|
case SIGABRT:
|
||||||
|
case SIGTERM:
|
||||||
|
case SIGINT:
|
||||||
|
running = 0; // Exit loop
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
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
|
#endif
|
||||||
|
|
||||||
@ -66,15 +72,6 @@ int main( int argc, char* argv[] )
|
|||||||
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
|
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
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)
|
if (i2p::util::config::GetArg("-daemon", 0) == 1)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
@ -96,12 +93,36 @@ int main( int argc, char* argv[] )
|
|||||||
LogPrint("Error, could not create process group.");
|
LogPrint("Error, could not create process group.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
chdir(i2p::util::filesystem::GetDataDir().string().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle shutdown
|
// Pidfile
|
||||||
signal(SIGABRT, &handle_shutdown);
|
std::string pidfile = i2p::util::filesystem::GetDataDir().string();
|
||||||
signal(SIGTERM, &handle_shutdown);
|
pidfile.append("/i2pd.pid");
|
||||||
signal(SIGINT, &handle_shutdown);
|
int pidFilehandle = open(pidfile.c_str(), O_RDWR|O_CREAT, 0600);
|
||||||
|
if (pidFilehandle == -1 )
|
||||||
|
{
|
||||||
|
LogPrint("Error, could not create pid file (", pidfile, ")\nIs an instance already running?");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (lockf(pidFilehandle,F_TLOCK,0) == -1)
|
||||||
|
{
|
||||||
|
LogPrint("Error, could not lock pid file (", pidfile, ")\nIs an instance already running?");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
char pid[10];
|
||||||
|
sprintf(pid,"%d\n",getpid());
|
||||||
|
write(pidFilehandle, pid, strlen(pid));
|
||||||
|
|
||||||
|
// Signal handler
|
||||||
|
struct sigaction sa;
|
||||||
|
sa.sa_handler = handle_signal;
|
||||||
|
sigemptyset(&sa.sa_mask);
|
||||||
|
sa.sa_flags = SA_RESTART;
|
||||||
|
sigaction(SIGHUP,&sa,0);
|
||||||
|
sigaction(SIGABRT,&sa,0);
|
||||||
|
sigaction(SIGTERM,&sa,0);
|
||||||
|
sigaction(SIGINT,&sa,0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (i2p::util::config::GetArg("-log", 0) == 1)
|
if (i2p::util::config::GetArg("-log", 0) == 1)
|
||||||
@ -123,9 +144,9 @@ int main( int argc, char* argv[] )
|
|||||||
|
|
||||||
i2p::util::HTTPServer httpServer (i2p::util::config::GetArg("-httpport", 7070));
|
i2p::util::HTTPServer httpServer (i2p::util::config::GetArg("-httpport", 7070));
|
||||||
|
|
||||||
httpServer.Start ();
|
httpServer.Start ();
|
||||||
i2p::data::netdb.Start ();
|
i2p::data::netdb.Start ();
|
||||||
i2p::transports.Start ();
|
i2p::transports.Start ();
|
||||||
i2p::tunnel::tunnels.Start ();
|
i2p::tunnel::tunnels.Start ();
|
||||||
|
|
||||||
while (running)
|
while (running)
|
||||||
@ -135,14 +156,18 @@ int main( int argc, char* argv[] )
|
|||||||
}
|
}
|
||||||
LogPrint("Shutdown started.");
|
LogPrint("Shutdown started.");
|
||||||
|
|
||||||
i2p::tunnel::tunnels.Stop ();
|
i2p::tunnel::tunnels.Stop ();
|
||||||
i2p::transports.Stop ();
|
i2p::transports.Stop ();
|
||||||
i2p::data::netdb.Stop ();
|
i2p::data::netdb.Stop ();
|
||||||
httpServer.Stop ();
|
httpServer.Stop ();
|
||||||
|
|
||||||
if (i2p::util::config::GetArg("-log", 0) == 1)
|
if (i2p::util::config::GetArg("-log", 0) == 1)
|
||||||
{
|
{
|
||||||
fclose (stdout);
|
fclose (stdout);
|
||||||
}
|
}
|
||||||
|
#ifndef _WIN32
|
||||||
|
close(pidFilehandle);
|
||||||
|
unlink(pidfile.c_str());
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user