2014-11-17 22:42:45 +03:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2015-07-30 16:34:56 +03:00
|
|
|
#include "util/Log.h"
|
2015-08-26 23:17:10 +03:00
|
|
|
#include "NetworkDatabase.h"
|
2015-07-27 23:17:31 +03:00
|
|
|
#include "transport/Transports.h"
|
2015-07-30 17:25:43 +03:00
|
|
|
#include "tunnel/Tunnel.h"
|
2014-11-17 22:42:45 +03:00
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Identity.h"
|
|
|
|
#include "Destination.h"
|
2015-07-28 19:37:06 +03:00
|
|
|
#include "util/util.h"
|
2014-11-17 20:37:40 +03:00
|
|
|
#include "api.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace api
|
|
|
|
{
|
2015-07-17 00:29:52 +03:00
|
|
|
void InitI2P (int argc, char* argv[], const char * appName)
|
|
|
|
{
|
|
|
|
i2p::util::filesystem::SetAppName (appName);
|
|
|
|
i2p::util::config::OptionParser(argc, argv);
|
|
|
|
i2p::context.Init ();
|
|
|
|
}
|
2014-11-17 22:42:45 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void StartI2P (std::ostream * logStream)
|
|
|
|
{
|
|
|
|
if (logStream)
|
|
|
|
StartLog (logStream);
|
|
|
|
else
|
|
|
|
StartLog (i2p::util::filesystem::GetAppName () + ".log");
|
|
|
|
i2p::data::netdb.Start();
|
|
|
|
LogPrint("NetDB started");
|
|
|
|
i2p::transport::transports.Start();
|
|
|
|
LogPrint("Transports started");
|
|
|
|
i2p::tunnel::tunnels.Start();
|
|
|
|
LogPrint("Tunnels started");
|
|
|
|
}
|
2014-11-17 22:42:45 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void StopI2P ()
|
|
|
|
{
|
|
|
|
LogPrint("Shutdown started.");
|
|
|
|
i2p::tunnel::tunnels.Stop();
|
|
|
|
LogPrint("Tunnels stopped");
|
|
|
|
i2p::transport::transports.Stop();
|
|
|
|
LogPrint("Transports stopped");
|
|
|
|
i2p::data::netdb.Stop();
|
|
|
|
LogPrint("NetDB stopped");
|
|
|
|
StopLog ();
|
|
|
|
}
|
2014-11-18 01:00:30 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
i2p::client::ClientDestination * CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
|
|
|
|
const std::map<std::string, std::string> * params)
|
|
|
|
{
|
|
|
|
auto localDestination = new i2p::client::ClientDestination (keys, isPublic, params);
|
|
|
|
localDestination->Start ();
|
|
|
|
return localDestination;
|
|
|
|
}
|
2014-11-18 01:00:30 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
i2p::client::ClientDestination * CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
|
|
|
|
const std::map<std::string, std::string> * params)
|
|
|
|
{
|
|
|
|
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
|
|
|
|
auto localDestination = new i2p::client::ClientDestination (keys, isPublic, params);
|
|
|
|
localDestination->Start ();
|
|
|
|
return localDestination;
|
|
|
|
}
|
2014-11-18 01:00:30 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void DestroyLocalDestination (i2p::client::ClientDestination * dest)
|
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
{
|
|
|
|
dest->Stop ();
|
|
|
|
delete dest;
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:33:58 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void RequestLeaseSet (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
|
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
dest->RequestDestination (remote);
|
|
|
|
}
|
2014-11-18 17:33:58 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
std::shared_ptr<i2p::stream::Stream> CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
|
|
|
|
{
|
|
|
|
if (!dest) return nullptr;
|
|
|
|
auto leaseSet = dest->FindLeaseSet (remote);
|
|
|
|
if (leaseSet)
|
|
|
|
{
|
|
|
|
auto stream = dest->CreateStream (leaseSet);
|
|
|
|
stream->Send (nullptr, 0); // connect
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RequestLeaseSet (dest, remote);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:33:58 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void AcceptStream (i2p::client::ClientDestination * dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
|
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
dest->AcceptStreams (acceptor);
|
|
|
|
}
|
2014-11-18 17:33:58 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream)
|
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
stream->Close ();
|
|
|
|
}
|
2014-11-17 20:37:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|