2014-09-24 18:01:26 +02:00
|
|
|
#ifndef SAM_H__
|
|
|
|
#define SAM_H__
|
|
|
|
|
2014-09-24 20:59:03 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string>
|
2014-09-24 22:39:31 +02:00
|
|
|
#include <map>
|
|
|
|
#include <list>
|
2014-09-24 18:01:26 +02:00
|
|
|
#include <thread>
|
2014-10-06 03:59:05 +02:00
|
|
|
#include <mutex>
|
2014-11-22 22:35:58 +01:00
|
|
|
#include <memory>
|
2014-09-24 18:01:26 +02:00
|
|
|
#include <boost/asio.hpp>
|
2014-10-02 18:42:28 +02:00
|
|
|
#include "Identity.h"
|
|
|
|
#include "LeaseSet.h"
|
2014-09-24 20:59:03 +02:00
|
|
|
#include "Streaming.h"
|
2014-10-22 17:46:54 +02:00
|
|
|
#include "Destination.h"
|
2014-09-24 18:01:26 +02:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-16 16:28:44 +02:00
|
|
|
namespace client
|
2014-09-24 18:01:26 +02:00
|
|
|
{
|
2015-03-27 14:44:27 +01:00
|
|
|
const size_t SAM_SOCKET_BUFFER_SIZE = 8192;
|
2014-12-27 03:06:24 +01:00
|
|
|
const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds
|
2016-12-23 01:38:17 +01:00
|
|
|
const int SAM_SESSION_READINESS_CHECK_INTERVAL = 20; // in seconds
|
2014-09-24 20:59:03 +02:00
|
|
|
const char SAM_HANDSHAKE[] = "HELLO VERSION";
|
2014-12-16 21:54:02 +01:00
|
|
|
const char SAM_HANDSHAKE_REPLY[] = "HELLO REPLY RESULT=OK VERSION=%s\n";
|
|
|
|
const char SAM_HANDSHAKE_I2P_ERROR[] = "HELLO REPLY RESULT=I2P_ERROR\n";
|
2014-09-25 19:22:25 +02:00
|
|
|
const char SAM_SESSION_CREATE[] = "SESSION CREATE";
|
2014-10-03 03:40:15 +02:00
|
|
|
const char SAM_SESSION_CREATE_REPLY_OK[] = "SESSION STATUS RESULT=OK DESTINATION=%s\n";
|
2014-09-27 14:11:00 +02:00
|
|
|
const char SAM_SESSION_CREATE_DUPLICATED_ID[] = "SESSION STATUS RESULT=DUPLICATED_ID\n";
|
2014-10-01 20:52:32 +02:00
|
|
|
const char SAM_SESSION_CREATE_DUPLICATED_DEST[] = "SESSION STATUS RESULT=DUPLICATED_DEST\n";
|
2015-06-24 21:19:10 +02:00
|
|
|
const char SAM_SESSION_STATUS_INVALID_KEY[] = "SESSION STATUS RESULT=INVALID_KEY\n";
|
2014-09-25 19:58:09 +02:00
|
|
|
const char SAM_STREAM_CONNECT[] = "STREAM CONNECT";
|
2014-09-27 14:11:00 +02:00
|
|
|
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 21:40:57 +02:00
|
|
|
const char SAM_STREAM_ACCEPT[] = "STREAM ACCEPT";
|
2015-03-27 02:23:59 +01:00
|
|
|
const char SAM_DATAGRAM_SEND[] = "DATAGRAM SEND";
|
2014-09-30 17:08:38 +02: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-02 22:55:01 +02:00
|
|
|
const char SAM_NAMING_LOOKUP[] = "NAMING LOOKUP";
|
|
|
|
const char SAM_NAMING_REPLY[] = "NAMING REPLY RESULT=OK NAME=ME VALUE=%s\n";
|
2016-02-01 04:37:38 +01:00
|
|
|
const char SAM_DATAGRAM_RECEIVED[] = "DATAGRAM RECEIVED DESTINATION=%s SIZE=%lu\n";
|
2014-10-03 21:08:41 +02:00
|
|
|
const char SAM_NAMING_REPLY_INVALID_KEY[] = "NAMING REPLY RESULT=INVALID_KEY NAME=%s\n";
|
2014-12-16 21:54:02 +01:00
|
|
|
const char SAM_NAMING_REPLY_KEY_NOT_FOUND[] = "NAMING REPLY RESULT=INVALID_KEY_NOT_FOUND NAME=%s\n";
|
|
|
|
const char SAM_PARAM_MIN[] = "MIN";
|
|
|
|
const char SAM_PARAM_MAX[] = "MAX";
|
2014-09-25 19:22:25 +02:00
|
|
|
const char SAM_PARAM_STYLE[] = "STYLE";
|
|
|
|
const char SAM_PARAM_ID[] = "ID";
|
2014-09-29 20:18:06 +02:00
|
|
|
const char SAM_PARAM_SILENT[] = "SILENT";
|
2014-09-25 19:22:25 +02:00
|
|
|
const char SAM_PARAM_DESTINATION[] = "DESTINATION";
|
2014-12-16 22:23:42 +01:00
|
|
|
const char SAM_PARAM_NAME[] = "NAME";
|
2015-03-27 02:23:59 +01:00
|
|
|
const char SAM_PARAM_SIGNATURE_TYPE[] = "SIGNATURE_TYPE";
|
|
|
|
const char SAM_PARAM_SIZE[] = "SIZE";
|
2014-09-25 19:22:25 +02:00
|
|
|
const char SAM_VALUE_TRANSIENT[] = "TRANSIENT";
|
2014-10-22 22:42:26 +02:00
|
|
|
const char SAM_VALUE_STREAM[] = "STREAM";
|
|
|
|
const char SAM_VALUE_DATAGRAM[] = "DATAGRAM";
|
|
|
|
const char SAM_VALUE_RAW[] = "RAW";
|
2014-09-29 20:18:06 +02:00
|
|
|
const char SAM_VALUE_TRUE[] = "true";
|
|
|
|
const char SAM_VALUE_FALSE[] = "false";
|
2014-09-25 19:22:25 +02:00
|
|
|
|
|
|
|
enum SAMSocketType
|
|
|
|
{
|
|
|
|
eSAMSocketTypeUnknown,
|
|
|
|
eSAMSocketTypeSession,
|
2014-09-26 21:40:57 +02:00
|
|
|
eSAMSocketTypeStream,
|
2014-11-23 03:56:59 +01:00
|
|
|
eSAMSocketTypeAcceptor,
|
|
|
|
eSAMSocketTypeTerminated
|
2014-09-25 19:22:25 +02:00
|
|
|
};
|
2014-09-24 20:59:03 +02:00
|
|
|
|
|
|
|
class SAMBridge;
|
2014-12-29 17:40:03 +01:00
|
|
|
struct SAMSession;
|
2014-11-22 22:35:58 +01:00
|
|
|
class SAMSocket: public std::enable_shared_from_this<SAMSocket>
|
2014-09-24 20:59:03 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SAMSocket (SAMBridge& owner);
|
|
|
|
~SAMSocket ();
|
2014-12-04 17:24:00 +01:00
|
|
|
void CloseStream (); // TODO: implement it better
|
|
|
|
|
2014-09-24 20:59:03 +02:00
|
|
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
|
|
|
void ReceiveHandshake ();
|
2014-12-04 17:24:00 +01:00
|
|
|
void SetSocketType (SAMSocketType socketType) { m_SocketType = socketType; };
|
2015-02-21 04:47:36 +01:00
|
|
|
SAMSocketType GetSocketType () const { return m_SocketType; };
|
2014-09-24 20:59:03 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-12-04 17:24:00 +01:00
|
|
|
void Terminate ();
|
2014-09-24 20:59:03 +02:00
|
|
|
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 19:22:25 +02: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 20:59:03 +02:00
|
|
|
void Receive ();
|
|
|
|
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
|
2014-09-26 21:40:57 +02:00
|
|
|
void I2PReceive ();
|
|
|
|
void HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2014-11-23 17:33:58 +01:00
|
|
|
void HandleI2PAccept (std::shared_ptr<i2p::stream::Stream> stream);
|
2014-09-26 21:40:57 +02:00
|
|
|
void HandleWriteI2PData (const boost::system::error_code& ecode);
|
2015-03-03 21:31:49 +01:00
|
|
|
void HandleI2PDatagramReceive (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
2014-09-24 20:59:03 +02:00
|
|
|
|
2014-09-25 19:22:25 +02:00
|
|
|
void ProcessSessionCreate (char * buf, size_t len);
|
|
|
|
void ProcessStreamConnect (char * buf, size_t len);
|
2014-09-26 21:40:57 +02:00
|
|
|
void ProcessStreamAccept (char * buf, size_t len);
|
2014-09-30 17:08:38 +02:00
|
|
|
void ProcessDestGenerate ();
|
2014-10-02 22:55:01 +02:00
|
|
|
void ProcessNamingLookup (char * buf, size_t len);
|
2015-03-27 19:02:27 +01:00
|
|
|
size_t ProcessDatagramSend (char * buf, size_t len, const char * data); // from SAM 1.0
|
2015-03-27 02:23:59 +01:00
|
|
|
void ExtractParams (char * buf, std::map<std::string, std::string>& params);
|
2014-09-25 19:22:25 +02:00
|
|
|
|
2015-01-27 17:27:58 +01:00
|
|
|
void Connect (std::shared_ptr<const i2p::data::LeaseSet> remote);
|
2015-04-07 18:02:25 +02:00
|
|
|
void HandleConnectLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet);
|
2015-11-03 15:15:49 +01:00
|
|
|
void SendNamingLookupReply (std::shared_ptr<const i2p::data::IdentityEx> identity);
|
2015-04-07 18:02:25 +02:00
|
|
|
void HandleNamingLookupLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, i2p::data::IdentHash ident);
|
2014-10-16 22:36:12 +02:00
|
|
|
void HandleSessionReadinessCheckTimer (const boost::system::error_code& ecode);
|
|
|
|
void SendSessionCreateReplyOk ();
|
2014-10-02 18:42:28 +02:00
|
|
|
|
2014-09-24 20:59:03 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
SAMBridge& m_Owner;
|
|
|
|
boost::asio::ip::tcp::socket m_Socket;
|
2014-10-02 18:42:28 +02:00
|
|
|
boost::asio::deadline_timer m_Timer;
|
2014-09-24 20:59:03 +02:00
|
|
|
char m_Buffer[SAM_SOCKET_BUFFER_SIZE + 1];
|
2015-03-27 19:02:27 +01:00
|
|
|
size_t m_BufferOffset;
|
2014-09-24 20:59:03 +02:00
|
|
|
uint8_t m_StreamBuffer[SAM_SOCKET_BUFFER_SIZE];
|
2014-09-25 19:22:25 +02:00
|
|
|
SAMSocketType m_SocketType;
|
|
|
|
std::string m_ID; // nickname
|
2014-09-29 20:18:06 +02:00
|
|
|
bool m_IsSilent;
|
2014-11-23 17:33:58 +01:00
|
|
|
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
2016-04-03 04:16:49 +02:00
|
|
|
std::shared_ptr<SAMSession> m_Session;
|
2014-09-24 20:59:03 +02:00
|
|
|
};
|
|
|
|
|
2014-09-24 22:39:31 +02:00
|
|
|
struct SAMSession
|
|
|
|
{
|
2015-02-24 21:40:50 +01:00
|
|
|
std::shared_ptr<ClientDestination> localDestination;
|
2016-04-01 17:36:56 +02:00
|
|
|
std::list<std::shared_ptr<SAMSocket> > m_Sockets;
|
|
|
|
std::mutex m_SocketsMutex;
|
|
|
|
|
|
|
|
/** safely add a socket to this session */
|
|
|
|
void AddSocket(std::shared_ptr<SAMSocket> sock) {
|
|
|
|
std::lock_guard<std::mutex> lock(m_SocketsMutex);
|
|
|
|
m_Sockets.push_back(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** safely remove a socket from this session */
|
|
|
|
void DelSocket(std::shared_ptr<SAMSocket> sock) {
|
|
|
|
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);
|
2016-08-09 00:53:37 +02:00
|
|
|
for(const auto& sock : m_Sockets ) l.push_back(sock);
|
2016-04-01 17:36:56 +02:00
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
2016-12-23 01:38:17 +01:00
|
|
|
|
|
|
|
SAMSession (std::shared_ptr<ClientDestination> dest);
|
|
|
|
~SAMSession ();
|
2016-12-18 17:49:50 +01:00
|
|
|
|
2014-12-17 21:21:50 +01:00
|
|
|
void CloseStreams ();
|
2014-09-24 22:39:31 +02:00
|
|
|
};
|
|
|
|
|
2014-09-24 18:01:26 +02:00
|
|
|
class SAMBridge
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-11-30 15:44:32 +01:00
|
|
|
SAMBridge (const std::string& address, int port);
|
2014-09-24 18:01:26 +02:00
|
|
|
~SAMBridge ();
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2014-09-24 20:59:03 +02:00
|
|
|
|
|
|
|
boost::asio::io_service& GetService () { return m_Service; };
|
2016-04-03 04:16:49 +02:00
|
|
|
std::shared_ptr<SAMSession> CreateSession (const std::string& id, const std::string& destination, // empty string means transient
|
2014-12-16 22:23:42 +01:00
|
|
|
const std::map<std::string, std::string> * params);
|
2014-09-24 22:39:31 +02:00
|
|
|
void CloseSession (const std::string& id);
|
2016-04-03 04:16:49 +02:00
|
|
|
std::shared_ptr<SAMSession> FindSession (const std::string& id) const;
|
2014-09-24 18:01:26 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Run ();
|
|
|
|
|
|
|
|
void Accept ();
|
2014-11-22 22:35:58 +01:00
|
|
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<SAMSocket> socket);
|
2014-09-24 18:01:26 +02:00
|
|
|
|
2014-10-22 22:42:26 +02:00
|
|
|
void ReceiveDatagram ();
|
|
|
|
void HandleReceivedDatagram (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
|
2014-09-24 18:01:26 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
|
|
|
boost::asio::io_service m_Service;
|
|
|
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
2014-10-22 22:42:26 +02:00
|
|
|
boost::asio::ip::udp::endpoint m_DatagramEndpoint, m_SenderEndpoint;
|
|
|
|
boost::asio::ip::udp::socket m_DatagramSocket;
|
2015-02-21 04:47:36 +01:00
|
|
|
mutable std::mutex m_SessionsMutex;
|
2016-04-03 04:16:49 +02:00
|
|
|
std::map<std::string, std::shared_ptr<SAMSession> > m_Sessions;
|
2014-10-24 03:14:17 +02:00
|
|
|
uint8_t m_DatagramReceiveBuffer[i2p::datagram::MAX_DATAGRAM_SIZE+1];
|
2015-02-21 04:47:36 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP
|
|
|
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
2014-09-24 18:01:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|