2014-11-17 22:42:45 +03:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include "Log.h"
|
|
|
|
#include "NetDb.h"
|
|
|
|
#include "Transports.h"
|
|
|
|
#include "Tunnel.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Identity.h"
|
|
|
|
#include "Destination.h"
|
|
|
|
#include "util.h"
|
2014-11-17 20:37:40 +03:00
|
|
|
#include "api.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace api
|
|
|
|
{
|
2014-11-18 17:33:58 +03:00
|
|
|
void InitI2P (int argc, char* argv[], const char * appName)
|
2014-11-17 22:42:45 +03:00
|
|
|
{
|
2014-11-18 17:33:58 +03:00
|
|
|
i2p::util::filesystem::SetAppName (appName);
|
2014-11-17 22:42:45 +03:00
|
|
|
i2p::util::config::OptionParser(argc, argv);
|
|
|
|
i2p::context.Init ();
|
|
|
|
}
|
|
|
|
|
2014-12-09 00:27:10 +03:00
|
|
|
void StartI2P (std::ostream * logStream)
|
2014-11-17 22:42:45 +03:00
|
|
|
{
|
2014-12-09 00:27:10 +03:00
|
|
|
if (logStream)
|
|
|
|
StartLog (logStream);
|
|
|
|
else
|
2015-11-03 17:15:49 +03:00
|
|
|
StartLog (i2p::util::filesystem::GetFullPath (i2p::util::filesystem::GetAppName () + ".log"));
|
2015-12-18 16:37:31 +03:00
|
|
|
LogPrint(eLogInfo, "API: starting NetDB");
|
2014-11-17 22:42:45 +03:00
|
|
|
i2p::data::netdb.Start();
|
2015-12-18 16:37:31 +03:00
|
|
|
LogPrint(eLogInfo, "API: starting Transports");
|
2014-11-17 22:42:45 +03:00
|
|
|
i2p::transport::transports.Start();
|
2015-12-18 16:37:31 +03:00
|
|
|
LogPrint(eLogInfo, "API: starting Tunnels");
|
2014-11-17 22:42:45 +03:00
|
|
|
i2p::tunnel::tunnels.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopI2P ()
|
|
|
|
{
|
2015-12-18 16:37:31 +03:00
|
|
|
LogPrint(eLogInfo, "API: shutting down");
|
|
|
|
LogPrint(eLogInfo, "API: stopping Tunnels");
|
2014-11-17 22:42:45 +03:00
|
|
|
i2p::tunnel::tunnels.Stop();
|
2015-12-18 16:37:31 +03:00
|
|
|
LogPrint(eLogInfo, "API: stopping Transports");
|
2014-11-17 22:42:45 +03:00
|
|
|
i2p::transport::transports.Stop();
|
2015-12-18 16:37:31 +03:00
|
|
|
LogPrint(eLogInfo, "API: stopping NetDB");
|
2014-11-17 22:42:45 +03:00
|
|
|
i2p::data::netdb.Stop();
|
|
|
|
StopLog ();
|
|
|
|
}
|
2014-11-18 01:00:30 +03:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void RunPeerTest ()
|
|
|
|
{
|
|
|
|
i2p::transport::transports.PeerTest ();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
|
2014-12-11 21:28:37 +03:00
|
|
|
const std::map<std::string, std::string> * params)
|
2014-11-18 01:00:30 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
auto localDestination = std::make_shared<i2p::client::ClientDestination> (keys, isPublic, params);
|
2014-11-18 01:00:30 +03:00
|
|
|
localDestination->Start ();
|
|
|
|
return localDestination;
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
|
2014-12-11 21:28:37 +03:00
|
|
|
const std::map<std::string, std::string> * params)
|
2014-11-18 01:00:30 +03:00
|
|
|
{
|
2014-11-29 00:46:26 +03:00
|
|
|
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
|
2015-11-03 17:15:49 +03:00
|
|
|
auto localDestination = std::make_shared<i2p::client::ClientDestination> (keys, isPublic, params);
|
2014-11-18 01:00:30 +03:00
|
|
|
localDestination->Start ();
|
|
|
|
return localDestination;
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void DestroyLocalDestination (std::shared_ptr<i2p::client::ClientDestination> dest)
|
2014-11-18 01:00:30 +03:00
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
dest->Stop ();
|
|
|
|
}
|
2014-11-18 17:33:58 +03:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void RequestLeaseSet (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
|
2014-11-18 17:33:58 +03:00
|
|
|
{
|
|
|
|
if (dest)
|
2014-12-26 05:13:51 +03:00
|
|
|
dest->RequestDestination (remote);
|
2014-11-18 17:33:58 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
|
2014-11-18 17:33:58 +03:00
|
|
|
{
|
2014-12-09 22:59:19 +03:00
|
|
|
if (!dest) return nullptr;
|
|
|
|
auto leaseSet = dest->FindLeaseSet (remote);
|
2014-11-18 17:33:58 +03:00
|
|
|
if (leaseSet)
|
|
|
|
{
|
2015-01-27 19:27:58 +03:00
|
|
|
auto stream = dest->CreateStream (leaseSet);
|
2014-11-18 17:33:58 +03:00
|
|
|
stream->Send (nullptr, 0); // connect
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RequestLeaseSet (dest, remote);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void AcceptStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
|
2014-11-18 17:33:58 +03:00
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
dest->AcceptStreams (acceptor);
|
|
|
|
}
|
|
|
|
|
2014-11-23 19:33:58 +03:00
|
|
|
void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream)
|
2014-11-18 17:33:58 +03:00
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
stream->Close ();
|
|
|
|
}
|
2014-11-17 20:37:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|