Browse Source

SSU added

pull/14/head
orignal 11 years ago
parent
commit
3cf3e69aef
  1. 2
      Makefile
  2. 44
      SSU.cpp
  3. 36
      SSU.h
  4. 21
      Transports.cpp
  5. 2
      Transports.h

2
Makefile

@ -3,7 +3,7 @@ CC = g++
CFLAGS = -g -Wall -std=c++0x CFLAGS = -g -Wall -std=c++0x
OBJECTS = i2p.o base64.o NTCPSession.o RouterInfo.o Transports.o RouterContext.o \ OBJECTS = i2p.o base64.o NTCPSession.o RouterInfo.o Transports.o RouterContext.o \
NetDb.o LeaseSet.o Tunnel.o TunnelEndpoint.o TunnelGateway.o TransitTunnel.o \ NetDb.o LeaseSet.o Tunnel.o TunnelEndpoint.o TunnelGateway.o TransitTunnel.o \
I2NPProtocol.o Log.o Garlic.o HTTPServer.o Streaming.o Identity.o I2NPProtocol.o Log.o Garlic.o HTTPServer.o Streaming.o Identity.o SSU.o
INCFLAGS = INCFLAGS =
LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem
LIBS = LIBS =

44
SSU.cpp

@ -0,0 +1,44 @@
#include <boost/bind.hpp>
#include "Log.h"
#include "hmac.h"
#include "SSU.h"
namespace i2p
{
namespace ssu
{
SSUServer::SSUServer (boost::asio::io_service& service, int port):
m_Socket (service, boost::asio::ip::udp::v4 (), port)
{
}
void SSUServer::Start ()
{
Receive ();
}
void SSUServer::Stop ()
{
m_Socket.close ();
}
void SSUServer::Receive ()
{
m_Socket.async_receive_from (boost::asio::buffer (m_ReceiveBuffer, SSU_MTU), m_SenderEndpoint,
boost::bind (&SSUServer::HandleReceivedFrom, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void SSUServer::HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (!ecode)
{
LogPrint ("SSU received ", bytes_transferred, " bytes");
// Handle
Receive ();
}
else
LogPrint ("SSU receive error: ", ecode.message ());
}
}
}

36
SSU.h

@ -0,0 +1,36 @@
#ifndef SSU_H__
#define SSU_H__
#include <inttypes.h>
#include <boost/asio.hpp>
namespace i2p
{
namespace ssu
{
const int SSU_MTU = 1484;
class SSUServer
{
public:
SSUServer (boost::asio::io_service& service, int port);
void Start ();
void Stop ();
private:
void Receive ();
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred);
private:
boost::asio::ip::udp::socket m_Socket;
boost::asio::ip::udp::endpoint m_SenderEndpoint;
uint8_t m_ReceiveBuffer[SSU_MTU];
};
}
}
#endif

21
Transports.cpp

@ -12,7 +12,7 @@ namespace i2p
Transports transports; Transports transports;
Transports::Transports (): Transports::Transports ():
m_Thread (0), m_Work (m_Service),m_NTCPAcceptor (0) m_Thread (nullptr), m_Work (m_Service),m_NTCPAcceptor (nullptr), m_SSUServer (nullptr)
{ {
} }
@ -34,11 +34,22 @@ namespace i2p
m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service, m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port)); boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port));
LogPrint ("Start listening port ", address.port); LogPrint ("Start listening TCP port ", address.port);
auto conn = new i2p::ntcp::NTCPServerConnection (m_Service); auto conn = new i2p::ntcp::NTCPServerConnection (m_Service);
m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this,
conn, boost::asio::placeholders::error)); conn, boost::asio::placeholders::error));
} }
else if (address.transportStyle == RouterInfo::eTransportSSU)
{
if (!m_SSUServer)
{
m_SSUServer = new i2p::ssu::SSUServer (m_Service, address.port);
LogPrint ("Start listening UDP port ", address.port);
m_SSUServer->Start ();
}
else
LogPrint ("SSU server already exists");
}
} }
} }
@ -49,6 +60,12 @@ namespace i2p
m_NTCPSessions.clear (); m_NTCPSessions.clear ();
delete m_NTCPAcceptor; delete m_NTCPAcceptor;
if (m_SSUServer)
{
m_SSUServer->Stop ();
delete m_SSUServer;
}
m_IsRunning = false; m_IsRunning = false;
m_Service.stop (); m_Service.stop ();
if (m_Thread) if (m_Thread)

2
Transports.h

@ -7,6 +7,7 @@
#include <string> #include <string>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "NTCPSession.h" #include "NTCPSession.h"
#include "SSU.h"
#include "RouterInfo.h" #include "RouterInfo.h"
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
@ -47,6 +48,7 @@ namespace i2p
boost::asio::ip::tcp::acceptor * m_NTCPAcceptor; boost::asio::ip::tcp::acceptor * m_NTCPAcceptor;
std::map<i2p::data::IdentHash, i2p::ntcp::NTCPSession *> m_NTCPSessions; std::map<i2p::data::IdentHash, i2p::ntcp::NTCPSession *> m_NTCPSessions;
i2p::ssu::SSUServer * m_SSUServer;
public: public:

Loading…
Cancel
Save