api.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <string>
  2. #include <map>
  3. #include "Config.h"
  4. #include "Log.h"
  5. #include "NetDb.hpp"
  6. #include "Transports.h"
  7. #include "Tunnel.h"
  8. #include "RouterContext.h"
  9. #include "Identity.h"
  10. #include "Destination.h"
  11. #include "Crypto.h"
  12. #include "FS.h"
  13. #include "api.h"
  14. namespace i2p
  15. {
  16. namespace api
  17. {
  18. void InitI2P (int argc, char* argv[], const char * appName)
  19. {
  20. i2p::config::Init ();
  21. i2p::config::ParseCmdline (argc, argv, true); // ignore unknown options and help
  22. i2p::config::Finalize ();
  23. std::string datadir; i2p::config::GetOption("datadir", datadir);
  24. i2p::fs::SetAppName (appName);
  25. i2p::fs::DetectDataDir(datadir, false);
  26. i2p::fs::Init();
  27. bool precomputation; i2p::config::GetOption("precomputation.elgamal", precomputation);
  28. i2p::crypto::InitCrypto (precomputation);
  29. int netID; i2p::config::GetOption("netid", netID);
  30. i2p::context.SetNetID (netID);
  31. i2p::context.Init ();
  32. }
  33. void TerminateI2P ()
  34. {
  35. i2p::crypto::TerminateCrypto ();
  36. }
  37. void StartI2P (std::shared_ptr<std::ostream> logStream)
  38. {
  39. if (logStream)
  40. i2p::log::Logger().SendTo (logStream);
  41. else
  42. i2p::log::Logger().SendTo (i2p::fs::DataDirPath (i2p::fs::GetAppName () + ".log"));
  43. i2p::log::Logger().Start ();
  44. LogPrint(eLogInfo, "API: starting NetDB");
  45. i2p::data::netdb.Start();
  46. LogPrint(eLogInfo, "API: starting Transports");
  47. i2p::transport::transports.Start();
  48. LogPrint(eLogInfo, "API: starting Tunnels");
  49. i2p::tunnel::tunnels.Start();
  50. }
  51. void StopI2P ()
  52. {
  53. LogPrint(eLogInfo, "API: shutting down");
  54. LogPrint(eLogInfo, "API: stopping Tunnels");
  55. i2p::tunnel::tunnels.Stop();
  56. LogPrint(eLogInfo, "API: stopping Transports");
  57. i2p::transport::transports.Stop();
  58. LogPrint(eLogInfo, "API: stopping NetDB");
  59. i2p::data::netdb.Stop();
  60. i2p::log::Logger().Stop ();
  61. }
  62. void RunPeerTest ()
  63. {
  64. i2p::transport::transports.PeerTest ();
  65. }
  66. std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
  67. const std::map<std::string, std::string> * params)
  68. {
  69. auto localDestination = std::make_shared<i2p::client::ClientDestination> (keys, isPublic, params);
  70. localDestination->Start ();
  71. return localDestination;
  72. }
  73. std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
  74. const std::map<std::string, std::string> * params)
  75. {
  76. i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
  77. auto localDestination = std::make_shared<i2p::client::ClientDestination> (keys, isPublic, params);
  78. localDestination->Start ();
  79. return localDestination;
  80. }
  81. void DestroyLocalDestination (std::shared_ptr<i2p::client::ClientDestination> dest)
  82. {
  83. if (dest)
  84. dest->Stop ();
  85. }
  86. void RequestLeaseSet (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
  87. {
  88. if (dest)
  89. dest->RequestDestination (remote);
  90. }
  91. std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
  92. {
  93. if (!dest) return nullptr;
  94. auto leaseSet = dest->FindLeaseSet (remote);
  95. if (leaseSet)
  96. {
  97. auto stream = dest->CreateStream (leaseSet);
  98. stream->Send (nullptr, 0); // connect
  99. return stream;
  100. }
  101. else
  102. {
  103. RequestLeaseSet (dest, remote);
  104. return nullptr;
  105. }
  106. }
  107. void AcceptStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
  108. {
  109. if (dest)
  110. dest->AcceptStreams (acceptor);
  111. }
  112. void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream)
  113. {
  114. if (stream)
  115. stream->Close ();
  116. }
  117. }
  118. }