i2pd/SAM.h

171 lines
6.1 KiB
C
Raw Normal View History

2014-09-24 20:01:26 +04:00
#ifndef SAM_H__
#define SAM_H__
2014-09-24 22:59:03 +04:00
#include <inttypes.h>
#include <string>
2014-09-25 00:39:31 +04:00
#include <map>
#include <list>
2014-09-24 20:01:26 +04:00
#include <thread>
2014-10-06 05:59:05 +04:00
#include <mutex>
2014-09-24 20:01:26 +04:00
#include <boost/asio.hpp>
#include "Identity.h"
#include "LeaseSet.h"
2014-09-24 22:59:03 +04:00
#include "Streaming.h"
#include "Destination.h"
2014-09-24 20:01:26 +04:00
namespace i2p
{
namespace client
2014-09-24 20:01:26 +04:00
{
2014-09-24 22:59:03 +04:00
const size_t SAM_SOCKET_BUFFER_SIZE = 4096;
const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds
const int SAM_CONNECT_TIMEOUT = 5; // in seconds
2014-10-03 23:08:41 +04:00
const int SAM_NAMING_LOOKUP_TIMEOUT = 5; // in seconds
const int SAM_SESSION_READINESS_CHECK_INTERVAL = 20; // in seconds
2014-09-24 22:59:03 +04:00
const char SAM_HANDSHAKE[] = "HELLO VERSION";
2014-10-10 05:40:45 +04:00
const char SAM_HANDSHAKE_REPLY[] = "HELLO REPLY RESULT=OK VERSION=3.0\n";
2014-09-25 21:22:25 +04:00
const char SAM_SESSION_CREATE[] = "SESSION CREATE";
2014-10-03 05:40:15 +04:00
const char SAM_SESSION_CREATE_REPLY_OK[] = "SESSION STATUS RESULT=OK DESTINATION=%s\n";
const char SAM_SESSION_CREATE_DUPLICATED_ID[] = "SESSION STATUS RESULT=DUPLICATED_ID\n";
const char SAM_SESSION_CREATE_DUPLICATED_DEST[] = "SESSION STATUS RESULT=DUPLICATED_DEST\n";
2014-09-25 21:58:09 +04:00
const char SAM_STREAM_CONNECT[] = "STREAM CONNECT";
const char SAM_STREAM_STATUS_OK[] = "STREAM STATUS RESULT=OK\n";
const char SAM_STREAM_STATUS_INVALID_ID[] = "STREAM STATUS RESULT=INVALID_ID\n";
const char SAM_STREAM_STATUS_CANT_REACH_PEER[] = "STREAM STATUS RESULT=CANT_REACH_PEER\n";
const char SAM_STREAM_STATUS_I2P_ERROR[] = "STREAM STATUS RESULT=I2P_ERROR\n";
2014-09-26 23:40:57 +04:00
const char SAM_STREAM_ACCEPT[] = "STREAM ACCEPT";
2014-09-30 19:08:38 +04:00
const char SAM_DEST_GENERATE[] = "DEST GENERATE";
const char SAM_DEST_REPLY[] = "DEST REPLY PUB=%s PRIV=%s\n";
const char SAM_DEST_REPLY_I2P_ERROR[] = "DEST REPLY RESULT=I2P_ERROR\n";
2014-10-03 00:55:01 +04:00
const char SAM_NAMING_LOOKUP[] = "NAMING LOOKUP";
const char SAM_NAMING_REPLY[] = "NAMING REPLY RESULT=OK NAME=ME VALUE=%s\n";
2014-10-31 23:44:44 +03:00
const char SAM_DATAGRAM_RECEIVED[] = "DATAGRAM_RECEIVED DESTINATION=%s SIZE=%i\n";
2014-10-03 23:08:41 +04:00
const char SAM_NAMING_REPLY_INVALID_KEY[] = "NAMING REPLY RESULT=INVALID_KEY NAME=%s\n";
const char SAM_NAMING_REPLY_KEY_NOT_FOUND[] = "NAMING REPLY RESULT=INVALID_KEY_NOT_FOUND NAME=%s\n";
2014-09-25 21:22:25 +04:00
const char SAM_PARAM_STYLE[] = "STYLE";
const char SAM_PARAM_ID[] = "ID";
2014-09-29 22:18:06 +04:00
const char SAM_PARAM_SILENT[] = "SILENT";
2014-09-25 21:22:25 +04:00
const char SAM_PARAM_DESTINATION[] = "DESTINATION";
2014-10-03 00:55:01 +04:00
const char SAM_PARAM_NAME[] = "NAME";
2014-09-25 21:22:25 +04:00
const char SAM_VALUE_TRANSIENT[] = "TRANSIENT";
const char SAM_VALUE_STREAM[] = "STREAM";
const char SAM_VALUE_DATAGRAM[] = "DATAGRAM";
const char SAM_VALUE_RAW[] = "RAW";
2014-09-29 22:18:06 +04:00
const char SAM_VALUE_TRUE[] = "true";
const char SAM_VALUE_FALSE[] = "false";
2014-09-25 21:22:25 +04:00
enum SAMSocketType
{
eSAMSocketTypeUnknown,
eSAMSocketTypeSession,
2014-09-26 23:40:57 +04:00
eSAMSocketTypeStream,
eSAMSocketTypeAcceptor
2014-09-25 21:22:25 +04:00
};
2014-09-24 22:59:03 +04:00
class SAMBridge;
class SAMSession;
2014-09-24 22:59:03 +04:00
class SAMSocket
{
public:
SAMSocket (SAMBridge& owner);
~SAMSocket ();
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
void ReceiveHandshake ();
private:
void Terminate ();
void HandleHandshakeReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleHandshakeReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2014-09-25 21:22:25 +04:00
void HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void SendMessageReply (const char * msg, size_t len, bool close);
void HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close);
2014-09-24 22:59:03 +04:00
void Receive ();
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2014-09-26 23:40:57 +04:00
void I2PReceive ();
void HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleI2PAccept (i2p::stream::Stream * stream);
void HandleWriteI2PData (const boost::system::error_code& ecode);
2014-10-31 23:44:44 +03:00
void HandleI2PDatagramReceive (const i2p::data::IdentityEx& ident, const uint8_t * buf, size_t len);
2014-09-24 22:59:03 +04:00
2014-09-25 21:22:25 +04:00
void ProcessSessionCreate (char * buf, size_t len);
void ProcessStreamConnect (char * buf, size_t len);
2014-09-26 23:40:57 +04:00
void ProcessStreamAccept (char * buf, size_t len);
2014-09-30 19:08:38 +04:00
void ProcessDestGenerate ();
2014-10-03 00:55:01 +04:00
void ProcessNamingLookup (char * buf, size_t len);
2014-09-25 21:22:25 +04:00
void ExtractParams (char * buf, size_t len, std::map<std::string, std::string>& params);
2014-10-07 20:07:10 +04:00
void Connect (const i2p::data::LeaseSet& remote);
void HandleStreamDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident);
2014-10-03 23:08:41 +04:00
void HandleNamingLookupDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident);
2014-10-13 00:22:14 +04:00
void SendNamingLookupReply (const i2p::data::LeaseSet * leaseSet);
void HandleSessionReadinessCheckTimer (const boost::system::error_code& ecode);
void SendSessionCreateReplyOk ();
2014-09-24 22:59:03 +04:00
private:
SAMBridge& m_Owner;
boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_Timer;
2014-09-24 22:59:03 +04:00
char m_Buffer[SAM_SOCKET_BUFFER_SIZE + 1];
uint8_t m_StreamBuffer[SAM_SOCKET_BUFFER_SIZE];
2014-09-25 21:22:25 +04:00
SAMSocketType m_SocketType;
std::string m_ID; // nickname
2014-09-29 22:18:06 +04:00
bool m_IsSilent;
i2p::stream::Stream * m_Stream;
2014-10-03 05:40:15 +04:00
SAMSession * m_Session;
2014-09-24 22:59:03 +04:00
};
2014-09-25 00:39:31 +04:00
struct SAMSession
{
ClientDestination * localDestination;
2014-09-25 00:39:31 +04:00
std::list<SAMSocket *> sockets;
};
2014-09-24 20:01:26 +04:00
class SAMBridge
{
public:
SAMBridge (int port);
~SAMBridge ();
void Start ();
void Stop ();
2014-09-24 22:59:03 +04:00
boost::asio::io_service& GetService () { return m_Service; };
2014-09-25 21:22:25 +04:00
SAMSession * CreateSession (const std::string& id, const std::string& destination = ""); // empty string means transient
2014-09-25 00:39:31 +04:00
void CloseSession (const std::string& id);
2014-09-25 21:22:25 +04:00
SAMSession * FindSession (const std::string& id);
2014-09-24 20:01:26 +04:00
private:
void Run ();
void Accept ();
void HandleAccept(const boost::system::error_code& ecode);
void ReceiveDatagram ();
void HandleReceivedDatagram (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2014-09-24 20:01:26 +04:00
private:
bool m_IsRunning;
std::thread * m_Thread;
boost::asio::io_service m_Service;
boost::asio::ip::tcp::acceptor m_Acceptor;
boost::asio::ip::udp::endpoint m_DatagramEndpoint, m_SenderEndpoint;
boost::asio::ip::udp::socket m_DatagramSocket;
2014-09-24 22:59:03 +04:00
SAMSocket * m_NewSocket;
2014-10-06 05:59:05 +04:00
std::mutex m_SessionsMutex;
2014-09-25 00:39:31 +04:00
std::map<std::string, SAMSession> m_Sessions;
2014-10-24 05:14:17 +04:00
uint8_t m_DatagramReceiveBuffer[i2p::datagram::MAX_DATAGRAM_SIZE+1];
2014-09-24 20:01:26 +04:00
};
}
}
#endif