Skip to content

Commit

Permalink
SSU payload types
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 24, 2014
1 parent 3cf3e69 commit 9e91be6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
27 changes: 26 additions & 1 deletion SSU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ namespace i2p
{
namespace ssu
{

SSUSession::SSUSession (): m_State (eSessionStateUnknown)
{
}

void SSUSession::ProcessNextMessage (uint8_t * buf, std::size_t len)
{
}

SSUServer::SSUServer (boost::asio::io_service& service, int port):
m_Socket (service, boost::asio::ip::udp::v4 (), port)
{
}

SSUServer::~SSUServer ()
{
for (auto it: m_Sessions)
delete it.second;
}

void SSUServer::Start ()
{
Receive ();
Expand All @@ -33,7 +48,17 @@ namespace ssu
if (!ecode)
{
LogPrint ("SSU received ", bytes_transferred, " bytes");
// Handle
SSUSession * session = nullptr;
auto it = m_Sessions.find (m_SenderEndpoint);
if (it != m_Sessions.end ())
session = it->second;
if (session)
{
session = new SSUSession ();
m_Sessions[m_SenderEndpoint] = session;
LogPrint ("New SSU session from ", m_SenderEndpoint.address ().to_string (), ":", m_SenderEndpoint.port (), " created");
}
session->ProcessNextMessage (m_ReceiveBuffer, bytes_transferred);
Receive ();
}
else
Expand Down
37 changes: 37 additions & 0 deletions SSU.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SSU_H__

#include <inttypes.h>
#include <map>
#include <boost/asio.hpp>

namespace i2p
Expand All @@ -10,11 +11,46 @@ namespace ssu
{
const int SSU_MTU = 1484;

// payload types (4 bits)
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_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;

enum SessionState
{
eSessionStateUnknown,
eSessionStateRequestSent,
eSessionStateRequestReceived,
eSessionStateCreatedSent,
eSessionStateCreatedReceived,
eSessionStateConfirmedSent,
eSessionStateConfirmedReceived,
eSessionStateEstablised
};

class SSUSession
{
public:

SSUSession ();
void ProcessNextMessage (uint8_t * buf, std::size_t len);

private:

SessionState m_State;
};

class SSUServer
{
public:

SSUServer (boost::asio::io_service& service, int port);
~SSUServer ();
void Start ();
void Stop ();

Expand All @@ -28,6 +64,7 @@ namespace ssu
boost::asio::ip::udp::socket m_Socket;
boost::asio::ip::udp::endpoint m_SenderEndpoint;
uint8_t m_ReceiveBuffer[SSU_MTU];
std::map<boost::asio::ip::udp::endpoint, SSUSession *> m_Sessions;
};
}
}
Expand Down

0 comments on commit 9e91be6

Please sign in to comment.