Browse Source

create SAM session

pull/102/head
orignal 10 years ago
parent
commit
393d4bc231
  1. 47
      SAM.cpp
  2. 12
      SAM.h
  3. 52
      Streaming.cpp
  4. 7
      Streaming.h

47
SAM.cpp

@ -1,5 +1,6 @@
#include <string.h> #include <string.h>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include "base64.h"
#include "Log.h" #include "Log.h"
#include "SAM.h" #include "SAM.h"
@ -14,8 +15,12 @@ namespace stream
SAMSocket::~SAMSocket () SAMSocket::~SAMSocket ()
{ {
if (m_Stream)
{
m_Stream->Close ();
delete m_Stream; delete m_Stream;
} }
}
void SAMSocket::Terminate () void SAMSocket::Terminate ()
{ {
@ -201,5 +206,47 @@ namespace stream
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Accept (); Accept ();
} }
bool SAMBridge::CreateSession (const std::string& id, const char * destination, size_t len)
{
if (m_Sessions.find (id) != m_Sessions.end ()) // session exists
return false;
StreamingDestination * localDestination = nullptr;
if (destination)
{
uint8_t * buf = new uint8_t[len];
size_t l = i2p::data::Base64ToByteStream (destination, len, buf, len);
i2p::data::PrivateKeys keys;
keys.FromBuffer (buf, l);
delete[] buf;
localDestination = GetLocalDestination (keys);
}
else // transient
localDestination = CreateNewLocalDestination ();
if (localDestination)
{
SAMSession session;
session.localDestination = localDestination;
session.isTransient = !destination;
m_Sessions[id] = session;
return true;
}
return false;
}
void SAMBridge::CloseSession (const std::string& id)
{
auto it = m_Sessions.find (id);
if (it != m_Sessions.end ())
{
for (auto it1 : it->second.sockets)
delete it1;
it->second.sockets.clear ();
if (it->second.isTransient)
DeleteLocalDestination (it->second.localDestination);
m_Sessions.erase (it);
}
}
} }
} }

12
SAM.h

@ -3,6 +3,8 @@
#include <inttypes.h> #include <inttypes.h>
#include <string> #include <string>
#include <map>
#include <list>
#include <thread> #include <thread>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "Streaming.h" #include "Streaming.h"
@ -48,6 +50,13 @@ namespace stream
Stream * m_Stream; Stream * m_Stream;
}; };
struct SAMSession
{
StreamingDestination * localDestination;
std::list<SAMSocket *> sockets;
bool isTransient;
};
class SAMBridge class SAMBridge
{ {
public: public:
@ -59,6 +68,8 @@ namespace stream
void Stop (); void Stop ();
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
bool CreateSession (const std::string& id, const char * destination = nullptr, size_t len = 0); // null means transient
void CloseSession (const std::string& id);
private: private:
@ -74,6 +85,7 @@ namespace stream
boost::asio::io_service m_Service; boost::asio::io_service m_Service;
boost::asio::ip::tcp::acceptor m_Acceptor; boost::asio::ip::tcp::acceptor m_Acceptor;
SAMSocket * m_NewSocket; SAMSocket * m_NewSocket;
std::map<std::string, SAMSession> m_Sessions;
}; };
} }
} }

52
Streaming.cpp

@ -551,8 +551,17 @@ namespace stream
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
} }
StreamingDestination::StreamingDestination (boost::asio::io_service& service, const i2p::data::PrivateKeys& keys):
m_Service (service), m_Keys (keys), m_LeaseSet (nullptr), m_IsPublic (false)
{
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
}
StreamingDestination::~StreamingDestination () StreamingDestination::~StreamingDestination ()
{ {
// TODO: delete streams
if (m_Pool) if (m_Pool)
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
delete m_LeaseSet; delete m_LeaseSet;
@ -701,6 +710,34 @@ namespace stream
return localDestination; return localDestination;
} }
StreamingDestination * StreamingDestinations::CreateNewLocalDestination ()
{
auto localDestination = new StreamingDestination (m_Service);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
return localDestination;
}
void StreamingDestinations::DeleteLocalDestination (StreamingDestination * destination)
{
if (!destination) return;
auto it = m_Destinations.find (destination->GetIdentHash ());
if (it != m_Destinations.end ())
{
delete it->second;
m_Destinations.erase (it);
}
}
StreamingDestination * StreamingDestinations::GetLocalDestination (const i2p::data::PrivateKeys& keys)
{
auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ());
if (it != m_Destinations.end ())
return it->second;
auto localDestination = new StreamingDestination (m_Service, keys);
m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination;
return localDestination;
}
Stream * StreamingDestinations::CreateClientStream (const i2p::data::LeaseSet& remote) Stream * StreamingDestinations::CreateClientStream (const i2p::data::LeaseSet& remote)
{ {
if (!m_SharedLocalDestination) return nullptr; if (!m_SharedLocalDestination) return nullptr;
@ -770,6 +807,21 @@ namespace stream
return destinations.GetSharedLocalDestination (); return destinations.GetSharedLocalDestination ();
} }
StreamingDestination * CreateNewLocalDestination ()
{
return destinations.CreateNewLocalDestination ();
}
void DeleteLocalDestination (StreamingDestination * destination)
{
destinations.DeleteLocalDestination (destination);
}
StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys)
{
return destinations.GetLocalDestination (keys);
}
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination)
{ {
return destinations.FindLocalDestination (destination); return destinations.FindLocalDestination (destination);

7
Streaming.h

@ -143,6 +143,7 @@ namespace stream
StreamingDestination (boost::asio::io_service& service); StreamingDestination (boost::asio::io_service& service);
StreamingDestination (boost::asio::io_service& service, const std::string& fullPath); StreamingDestination (boost::asio::io_service& service, const std::string& fullPath);
StreamingDestination (boost::asio::io_service& service, const i2p::data::PrivateKeys& keys);
~StreamingDestination (); ~StreamingDestination ();
const i2p::data::LeaseSet * GetLeaseSet (); const i2p::data::LeaseSet * GetLeaseSet ();
@ -194,6 +195,9 @@ namespace stream
Stream * CreateClientStream (const i2p::data::LeaseSet& remote); Stream * CreateClientStream (const i2p::data::LeaseSet& remote);
void DeleteStream (Stream * stream); void DeleteStream (Stream * stream);
StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; };
StreamingDestination * CreateNewLocalDestination ();
void DeleteLocalDestination (StreamingDestination * destination);
StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys);
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const;
StreamingDestination * LoadLocalDestination (const std::string& filename); StreamingDestination * LoadLocalDestination (const std::string& filename);
@ -219,6 +223,9 @@ namespace stream
void StartStreaming (); void StartStreaming ();
void StopStreaming (); void StopStreaming ();
StreamingDestination * GetSharedLocalDestination (); StreamingDestination * GetSharedLocalDestination ();
StreamingDestination * CreateNewLocalDestination ();
void DeleteLocalDestination (StreamingDestination * destination);
StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys);
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination); StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination);
StreamingDestination * LoadLocalDestination (const std::string& filename); StreamingDestination * LoadLocalDestination (const std::string& filename);

Loading…
Cancel
Save