mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
8.5 KiB
224 lines
8.5 KiB
![]()
10 years ago
|
#ifndef SAM_H__
|
||
|
#define SAM_H__
|
||
|
|
||
![]()
10 years ago
|
#include <inttypes.h>
|
||
|
#include <string>
|
||
![]()
10 years ago
|
#include <map>
|
||
|
#include <list>
|
||
![]()
10 years ago
|
#include <thread>
|
||
![]()
10 years ago
|
#include <mutex>
|
||
![]()
10 years ago
|
#include <memory>
|
||
![]()
10 years ago
|
#include <boost/asio.hpp>
|
||
![]()
10 years ago
|
#include "Identity.h"
|
||
|
#include "LeaseSet.h"
|
||
![]()
10 years ago
|
#include "Streaming.h"
|
||
![]()
10 years ago
|
#include "Destination.h"
|
||
![]()
10 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
![]()
10 years ago
|
namespace client
|
||
![]()
10 years ago
|
{
|
||
![]()
10 years ago
|
const size_t SAM_SOCKET_BUFFER_SIZE = 8192;
|
||
![]()
10 years ago
|
const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds
|
||
![]()
8 years ago
|
const int SAM_SESSION_READINESS_CHECK_INTERVAL = 20; // in seconds
|
||
![]()
10 years ago
|
const char SAM_HANDSHAKE[] = "HELLO VERSION";
|
||
![]()
10 years ago
|
const char SAM_HANDSHAKE_REPLY[] = "HELLO REPLY RESULT=OK VERSION=%s\n";
|
||
![]()
8 years ago
|
const char SAM_HANDSHAKE_I2P_ERROR[] = "HELLO REPLY RESULT=I2P_ERROR\n";
|
||
![]()
10 years ago
|
const char SAM_SESSION_CREATE[] = "SESSION CREATE";
|
||
![]()
10 years ago
|
const char SAM_SESSION_CREATE_REPLY_OK[] = "SESSION STATUS RESULT=OK DESTINATION=%s\n";
|
||
![]()
10 years ago
|
const char SAM_SESSION_CREATE_DUPLICATED_ID[] = "SESSION STATUS RESULT=DUPLICATED_ID\n";
|
||
![]()
8 years ago
|
const char SAM_SESSION_CREATE_DUPLICATED_DEST[] = "SESSION STATUS RESULT=DUPLICATED_DEST\n";
|
||
![]()
9 years ago
|
const char SAM_SESSION_STATUS_INVALID_KEY[] = "SESSION STATUS RESULT=INVALID_KEY\n";
|
||
![]()
8 years ago
|
const char SAM_SESSION_STATUS_I2P_ERROR[] = "SESSION STATUS RESULT=I2P_ERROR MESSAGE=%s\n";
|
||
![]()
10 years ago
|
const char SAM_STREAM_CONNECT[] = "STREAM CONNECT";
|
||
![]()
10 years ago
|
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";
|
||
![]()
8 years ago
|
const char SAM_STREAM_ACCEPT[] = "STREAM ACCEPT";
|
||
![]()
10 years ago
|
const char SAM_DATAGRAM_SEND[] = "DATAGRAM SEND";
|
||
![]()
10 years ago
|
const char SAM_DEST_GENERATE[] = "DEST GENERATE";
|
||
![]()
8 years ago
|
const char SAM_DEST_REPLY[] = "DEST REPLY PUB=%s PRIV=%s\n";
|
||
![]()
10 years ago
|
const char SAM_DEST_REPLY_I2P_ERROR[] = "DEST REPLY RESULT=I2P_ERROR\n";
|
||
![]()
10 years ago
|
const char SAM_NAMING_LOOKUP[] = "NAMING LOOKUP";
|
||
|
const char SAM_NAMING_REPLY[] = "NAMING REPLY RESULT=OK NAME=ME VALUE=%s\n";
|
||
![]()
9 years ago
|
const char SAM_DATAGRAM_RECEIVED[] = "DATAGRAM RECEIVED DESTINATION=%s SIZE=%lu\n";
|
||
![]()
10 years ago
|
const char SAM_NAMING_REPLY_INVALID_KEY[] = "NAMING REPLY RESULT=INVALID_KEY NAME=%s\n";
|
||
![]()
10 years ago
|
const char SAM_NAMING_REPLY_KEY_NOT_FOUND[] = "NAMING REPLY RESULT=INVALID_KEY_NOT_FOUND NAME=%s\n";
|
||
![]()
8 years ago
|
const char SAM_PARAM_MIN[] = "MIN";
|
||
|
const char SAM_PARAM_MAX[] = "MAX";
|
||
|
const char SAM_PARAM_STYLE[] = "STYLE";
|
||
|
const char SAM_PARAM_ID[] = "ID";
|
||
![]()
10 years ago
|
const char SAM_PARAM_SILENT[] = "SILENT";
|
||
![]()
8 years ago
|
const char SAM_PARAM_DESTINATION[] = "DESTINATION";
|
||
![]()
10 years ago
|
const char SAM_PARAM_NAME[] = "NAME";
|
||
![]()
8 years ago
|
const char SAM_PARAM_SIGNATURE_TYPE[] = "SIGNATURE_TYPE";
|
||
![]()
10 years ago
|
const char SAM_PARAM_SIZE[] = "SIZE";
|
||
![]()
8 years ago
|
const char SAM_VALUE_TRANSIENT[] = "TRANSIENT";
|
||
![]()
10 years ago
|
const char SAM_VALUE_STREAM[] = "STREAM";
|
||
|
const char SAM_VALUE_DATAGRAM[] = "DATAGRAM";
|
||
![]()
8 years ago
|
const char SAM_VALUE_RAW[] = "RAW";
|
||
|
const char SAM_VALUE_TRUE[] = "true";
|
||
![]()
8 years ago
|
const char SAM_VALUE_FALSE[] = "false";
|
||
![]()
8 years ago
|
const char SAM_VALUE_HOST[] = "HOST";
|
||
|
const char SAM_VALUE_PORT[] = "PORT";
|
||
![]()
10 years ago
|
|
||
|
enum SAMSocketType
|
||
|
{
|
||
|
eSAMSocketTypeUnknown,
|
||
|
eSAMSocketTypeSession,
|
||
![]()
10 years ago
|
eSAMSocketTypeStream,
|
||
![]()
10 years ago
|
eSAMSocketTypeAcceptor,
|
||
|
eSAMSocketTypeTerminated
|
||
![]()
10 years ago
|
};
|
||
![]()
10 years ago
|
|
||
|
class SAMBridge;
|
||
![]()
10 years ago
|
struct SAMSession;
|
||
![]()
10 years ago
|
class SAMSocket: public std::enable_shared_from_this<SAMSocket>
|
||
![]()
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
|
SAMSocket (SAMBridge& owner);
|
||
![]()
8 years ago
|
~SAMSocket ();
|
||
|
void CloseStream (); // TODO: implement it better
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
||
|
void ReceiveHandshake ();
|
||
![]()
10 years ago
|
void SetSocketType (SAMSocketType socketType) { m_SocketType = socketType; };
|
||
![]()
10 years ago
|
SAMSocketType GetSocketType () const { return m_SocketType; };
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
void Terminate ();
|
||
![]()
10 years ago
|
private:
|
||
|
|
||
|
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);
|
||
![]()
10 years ago
|
void HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||
![]()
8 years ago
|
void SendMessageReply (const char * msg, size_t len, bool close);
|
||
![]()
10 years ago
|
void HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close);
|
||
![]()
10 years ago
|
void Receive ();
|
||
|
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||
|
|
||
![]()
8 years ago
|
void I2PReceive ();
|
||
![]()
10 years ago
|
void HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||
![]()
10 years ago
|
void HandleI2PAccept (std::shared_ptr<i2p::stream::Stream> stream);
|
||
![]()
10 years ago
|
void HandleWriteI2PData (const boost::system::error_code& ecode);
|
||
![]()
10 years ago
|
void HandleI2PDatagramReceive (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
void ProcessSessionCreate (char * buf, size_t len);
|
||
![]()
8 years ago
|
void ProcessStreamConnect (char * buf, size_t len, size_t rem);
|
||
![]()
10 years ago
|
void ProcessStreamAccept (char * buf, size_t len);
|
||
![]()
8 years ago
|
void ProcessDestGenerate (char * buf, size_t len);
|
||
![]()
10 years ago
|
void ProcessNamingLookup (char * buf, size_t len);
|
||
![]()
8 years ago
|
void SendI2PError(const std::string & msg);
|
||
|
size_t ProcessDatagramSend (char * buf, size_t len, const char * data); // from SAM 1.0
|
||
![]()
10 years ago
|
void ExtractParams (char * buf, std::map<std::string, std::string>& params);
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
void Connect (std::shared_ptr<const i2p::data::LeaseSet> remote);
|
||
![]()
10 years ago
|
void HandleConnectLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet);
|
||
![]()
9 years ago
|
void SendNamingLookupReply (std::shared_ptr<const i2p::data::IdentityEx> identity);
|
||
![]()
10 years ago
|
void HandleNamingLookupLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, i2p::data::IdentHash ident);
|
||
![]()
10 years ago
|
void HandleSessionReadinessCheckTimer (const boost::system::error_code& ecode);
|
||
|
void SendSessionCreateReplyOk ();
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
private:
|
||
|
|
||
|
SAMBridge& m_Owner;
|
||
|
boost::asio::ip::tcp::socket m_Socket;
|
||
![]()
10 years ago
|
boost::asio::deadline_timer m_Timer;
|
||
![]()
10 years ago
|
char m_Buffer[SAM_SOCKET_BUFFER_SIZE + 1];
|
||
![]()
10 years ago
|
size_t m_BufferOffset;
|
||
![]()
10 years ago
|
uint8_t m_StreamBuffer[SAM_SOCKET_BUFFER_SIZE];
|
||
![]()
10 years ago
|
SAMSocketType m_SocketType;
|
||
|
std::string m_ID; // nickname
|
||
![]()
10 years ago
|
bool m_IsSilent;
|
||
![]()
8 years ago
|
bool m_IsAccepting; // for eSAMSocketTypeAcceptor only
|
||
![]()
10 years ago
|
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
||
![]()
9 years ago
|
std::shared_ptr<SAMSession> m_Session;
|
||
![]()
8 years ago
|
};
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
struct SAMSession
|
||
|
{
|
||
![]()
10 years ago
|
std::shared_ptr<ClientDestination> localDestination;
|
||
![]()
9 years ago
|
std::list<std::shared_ptr<SAMSocket> > m_Sockets;
|
||
![]()
8 years ago
|
std::shared_ptr<boost::asio::ip::udp::endpoint> UDPEndpoint;
|
||
![]()
9 years ago
|
std::mutex m_SocketsMutex;
|
||
|
|
||
|
/** safely add a socket to this session */
|
||
![]()
8 years ago
|
void AddSocket(const std::shared_ptr<SAMSocket> & sock) {
|
||
![]()
9 years ago
|
std::lock_guard<std::mutex> lock(m_SocketsMutex);
|
||
|
m_Sockets.push_back(sock);
|
||
|
}
|
||
|
|
||
|
/** safely remove a socket from this session */
|
||
![]()
8 years ago
|
void DelSocket(const std::shared_ptr<SAMSocket> & sock) {
|
||
![]()
9 years ago
|
std::lock_guard<std::mutex> lock(m_SocketsMutex);
|
||
|
m_Sockets.remove(sock);
|
||
|
}
|
||
|
|
||
|
/** get a list holding a copy of all sam sockets from this session */
|
||
|
std::list<std::shared_ptr<SAMSocket> > ListSockets() {
|
||
|
std::list<std::shared_ptr<SAMSocket> > l;
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(m_SocketsMutex);
|
||
![]()
8 years ago
|
for(const auto& sock : m_Sockets ) l.push_back(sock);
|
||
![]()
9 years ago
|
}
|
||
|
return l;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
SAMSession (std::shared_ptr<ClientDestination> dest);
|
||
![]()
8 years ago
|
~SAMSession ();
|
||
![]()
8 years ago
|
|
||
![]()
10 years ago
|
void CloseStreams ();
|
||
![]()
10 years ago
|
};
|
||
|
|
||
![]()
10 years ago
|
class SAMBridge
|
||
|
{
|
||
|
public:
|
||
|
|
||
![]()
9 years ago
|
SAMBridge (const std::string& address, int port);
|
||
![]()
10 years ago
|
~SAMBridge ();
|
||
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
![]()
8 years ago
|
|
||
![]()
10 years ago
|
boost::asio::io_service& GetService () { return m_Service; };
|
||
![]()
9 years ago
|
std::shared_ptr<SAMSession> CreateSession (const std::string& id, const std::string& destination, // empty string means transient
|
||
![]()
10 years ago
|
const std::map<std::string, std::string> * params);
|
||
![]()
10 years ago
|
void CloseSession (const std::string& id);
|
||
![]()
9 years ago
|
std::shared_ptr<SAMSession> FindSession (const std::string& id) const;
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
/** send raw data to remote endpoint from our UDP Socket */
|
||
|
void SendTo(const uint8_t * buf, size_t len, std::shared_ptr<boost::asio::ip::udp::endpoint> remote);
|
||
![]()
8 years ago
|
|
||
![]()
10 years ago
|
private:
|
||
|
|
||
|
void Run ();
|
||
|
|
||
|
void Accept ();
|
||
![]()
10 years ago
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<SAMSocket> socket);
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
void ReceiveDatagram ();
|
||
|
void HandleReceivedDatagram (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||
|
|
||
![]()
10 years ago
|
private:
|
||
|
|
||
|
bool m_IsRunning;
|
||
![]()
8 years ago
|
std::thread * m_Thread;
|
||
![]()
10 years ago
|
boost::asio::io_service m_Service;
|
||
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||
![]()
10 years ago
|
boost::asio::ip::udp::endpoint m_DatagramEndpoint, m_SenderEndpoint;
|
||
|
boost::asio::ip::udp::socket m_DatagramSocket;
|
||
![]()
10 years ago
|
mutable std::mutex m_SessionsMutex;
|
||
![]()
9 years ago
|
std::map<std::string, std::shared_ptr<SAMSession> > m_Sessions;
|
||
![]()
10 years ago
|
uint8_t m_DatagramReceiveBuffer[i2p::datagram::MAX_DATAGRAM_SIZE+1];
|
||
![]()
10 years ago
|
|
||
|
public:
|
||
|
|
||
|
// for HTTP
|
||
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
||
![]()
8 years ago
|
};
|
||
![]()
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|