RouterInfo.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "I2PEndian.h"
  4. #include <fstream>
  5. #include <boost/lexical_cast.hpp>
  6. #include <boost/make_shared.hpp>
  7. #if (BOOST_VERSION >= 105300)
  8. #include <boost/atomic.hpp>
  9. #endif
  10. #include "version.h"
  11. #include "Crypto.h"
  12. #include "Base.h"
  13. #include "Timestamp.h"
  14. #include "Log.h"
  15. #include "NetDb.hpp"
  16. #include "RouterContext.h"
  17. #include "RouterInfo.h"
  18. namespace i2p
  19. {
  20. namespace data
  21. {
  22. RouterInfo::RouterInfo (): m_Buffer (nullptr)
  23. {
  24. m_Addresses = boost::make_shared<Addresses>(); // create empty list
  25. }
  26. RouterInfo::RouterInfo (const std::string& fullPath):
  27. m_FullPath (fullPath), m_IsUpdated (false), m_IsUnreachable (false),
  28. m_SupportedTransports (0), m_Caps (0)
  29. {
  30. m_Addresses = boost::make_shared<Addresses>(); // create empty list
  31. m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
  32. ReadFromFile ();
  33. }
  34. RouterInfo::RouterInfo (const uint8_t * buf, int len):
  35. m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0)
  36. {
  37. m_Addresses = boost::make_shared<Addresses>(); // create empty list
  38. if (len <= MAX_RI_BUFFER_SIZE)
  39. {
  40. m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
  41. memcpy (m_Buffer, buf, len);
  42. m_BufferLen = len;
  43. ReadFromBuffer (true);
  44. }
  45. else
  46. {
  47. LogPrint (eLogError, "RouterInfo: Buffer is too long ", len, ". Ignored");
  48. m_Buffer = nullptr;
  49. m_IsUnreachable = true;
  50. }
  51. }
  52. RouterInfo::~RouterInfo ()
  53. {
  54. delete[] m_Buffer;
  55. }
  56. void RouterInfo::Update (const uint8_t * buf, size_t len)
  57. {
  58. if (len > MAX_RI_BUFFER_SIZE)
  59. {
  60. LogPrint (eLogError, "RouterInfo: Buffer is too long ", len);
  61. m_IsUnreachable = true;
  62. return;
  63. }
  64. // verify signature since we have identity already
  65. int l = len - m_RouterIdentity->GetSignatureLen ();
  66. if (m_RouterIdentity->Verify (buf, l, buf + l))
  67. {
  68. // clean up
  69. m_IsUpdated = true;
  70. m_IsUnreachable = false;
  71. m_SupportedTransports = 0;
  72. m_Caps = 0;
  73. // don't clean up m_Addresses, it will be replaced in ReadFromStream
  74. m_Properties.clear ();
  75. // copy buffer
  76. if (!m_Buffer)
  77. m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
  78. memcpy (m_Buffer, buf, len);
  79. m_BufferLen = len;
  80. // skip identity
  81. size_t identityLen = m_RouterIdentity->GetFullLen ();
  82. // read new RI
  83. std::stringstream str (std::string ((char *)m_Buffer + identityLen, m_BufferLen - identityLen));
  84. ReadFromStream (str);
  85. // don't delete buffer until saved to the file
  86. }
  87. else
  88. {
  89. LogPrint (eLogError, "RouterInfo: signature verification failed");
  90. m_IsUnreachable = true;
  91. }
  92. }
  93. void RouterInfo::SetRouterIdentity (std::shared_ptr<const IdentityEx> identity)
  94. {
  95. m_RouterIdentity = identity;
  96. m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
  97. }
  98. bool RouterInfo::LoadFile ()
  99. {
  100. std::ifstream s(m_FullPath, std::ifstream::binary);
  101. if (s.is_open ())
  102. {
  103. s.seekg (0,std::ios::end);
  104. m_BufferLen = s.tellg ();
  105. if (m_BufferLen < 40 || m_BufferLen > MAX_RI_BUFFER_SIZE)
  106. {
  107. LogPrint(eLogError, "RouterInfo: File", m_FullPath, " is malformed");
  108. return false;
  109. }
  110. s.seekg(0, std::ios::beg);
  111. if (!m_Buffer) m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
  112. s.read((char *)m_Buffer, m_BufferLen);
  113. }
  114. else
  115. {
  116. LogPrint (eLogError, "RouterInfo: Can't open file ", m_FullPath);
  117. return false;
  118. }
  119. return true;
  120. }
  121. void RouterInfo::ReadFromFile ()
  122. {
  123. if (LoadFile ())
  124. ReadFromBuffer (false);
  125. else
  126. m_IsUnreachable = true;
  127. }
  128. void RouterInfo::ReadFromBuffer (bool verifySignature)
  129. {
  130. m_RouterIdentity = std::make_shared<IdentityEx>(m_Buffer, m_BufferLen);
  131. size_t identityLen = m_RouterIdentity->GetFullLen ();
  132. if (identityLen >= m_BufferLen)
  133. {
  134. LogPrint (eLogError, "RouterInfo: identity length ", identityLen, " exceeds buffer size ", m_BufferLen);
  135. m_IsUnreachable = true;
  136. return;
  137. }
  138. if (verifySignature)
  139. {
  140. // reject RSA signatures
  141. if (m_RouterIdentity->IsRSA ())
  142. {
  143. LogPrint (eLogError, "RouterInfo: RSA signature type is not allowed");
  144. m_IsUnreachable = true;
  145. return;
  146. }
  147. // verify signature
  148. int l = m_BufferLen - m_RouterIdentity->GetSignatureLen ();
  149. if (l < 0 || !m_RouterIdentity->Verify ((uint8_t *)m_Buffer, l, (uint8_t *)m_Buffer + l))
  150. {
  151. LogPrint (eLogError, "RouterInfo: signature verification failed");
  152. m_IsUnreachable = true;
  153. return;
  154. }
  155. m_RouterIdentity->DropVerifier ();
  156. }
  157. // parse RI
  158. std::stringstream str;
  159. str.write ((const char *)m_Buffer + identityLen, m_BufferLen - identityLen);
  160. ReadFromStream (str);
  161. if (!str)
  162. {
  163. LogPrint (eLogError, "RouterInfo: malformed message");
  164. m_IsUnreachable = true;
  165. }
  166. }
  167. void RouterInfo::ReadFromStream (std::istream& s)
  168. {
  169. s.read ((char *)&m_Timestamp, sizeof (m_Timestamp));
  170. m_Timestamp = be64toh (m_Timestamp);
  171. // read addresses
  172. auto addresses = boost::make_shared<Addresses>();
  173. uint8_t numAddresses;
  174. s.read ((char *)&numAddresses, sizeof (numAddresses)); if (!s) return;
  175. bool introducers = false;
  176. for (int i = 0; i < numAddresses; i++)
  177. {
  178. uint8_t supportedTransports = 0;
  179. auto address = std::make_shared<Address>();
  180. s.read ((char *)&address->cost, sizeof (address->cost));
  181. s.read ((char *)&address->date, sizeof (address->date));
  182. bool isNTCP2Only = false;
  183. char transportStyle[6];
  184. auto transportStyleLen = ReadString (transportStyle, 6, s) - 1;
  185. if (!strncmp (transportStyle, "NTCP", 4)) // NTCP or NTCP2
  186. {
  187. address->transportStyle = eTransportNTCP;
  188. if (transportStyleLen > 4 && transportStyle[4] == '2') isNTCP2Only= true;
  189. }
  190. else if (!strcmp (transportStyle, "SSU"))
  191. {
  192. address->transportStyle = eTransportSSU;
  193. address->ssu.reset (new SSUExt ());
  194. address->ssu->mtu = 0;
  195. }
  196. else
  197. address->transportStyle = eTransportUnknown;
  198. address->port = 0;
  199. uint16_t size, r = 0;
  200. s.read ((char *)&size, sizeof (size)); if (!s) return;
  201. size = be16toh (size);
  202. while (r < size)
  203. {
  204. char key[255], value[255];
  205. r += ReadString (key, 255, s);
  206. s.seekg (1, std::ios_base::cur); r++; // =
  207. r += ReadString (value, 255, s);
  208. s.seekg (1, std::ios_base::cur); r++; // ;
  209. if (!s) return;
  210. if (!strcmp (key, "host"))
  211. {
  212. boost::system::error_code ecode;
  213. address->host = boost::asio::ip::address::from_string (value, ecode);
  214. if (!ecode)
  215. {
  216. #if BOOST_VERSION >= 104900
  217. if (!address->host.is_unspecified ()) // check if address is valid
  218. #else
  219. address->host.to_string (ecode);
  220. if (!ecode)
  221. #endif
  222. {
  223. // add supported protocol
  224. if (address->host.is_v4 ())
  225. supportedTransports |= (address->transportStyle == eTransportNTCP) ? eNTCPV4 : eSSUV4;
  226. else
  227. supportedTransports |= (address->transportStyle == eTransportNTCP) ? eNTCPV6 : eSSUV6;
  228. }
  229. }
  230. }
  231. else if (!strcmp (key, "port"))
  232. address->port = boost::lexical_cast<int>(value);
  233. else if (!strcmp (key, "mtu"))
  234. {
  235. if (address->ssu)
  236. address->ssu->mtu = boost::lexical_cast<int>(value);
  237. else
  238. LogPrint (eLogWarning, "RouterInfo: Unexpected field 'mtu' for NTCP");
  239. }
  240. else if (!strcmp (key, "key"))
  241. {
  242. if (address->ssu)
  243. Base64ToByteStream (value, strlen (value), address->ssu->key, 32);
  244. else
  245. LogPrint (eLogWarning, "RouterInfo: Unexpected field 'key' for NTCP");
  246. }
  247. else if (!strcmp (key, "caps"))
  248. ExtractCaps (value);
  249. else if (!strcmp (key, "s")) // ntcp2 static key
  250. {
  251. if (!address->ntcp2) address->ntcp2.reset (new NTCP2Ext ());
  252. supportedTransports |= (address->host.is_v4 ()) ? eNTCP2V4 : eNTCP2V6;
  253. Base64ToByteStream (value, strlen (value), address->ntcp2->staticKey, 32);
  254. }
  255. else if (!strcmp (key, "i")) // ntcp2 iv
  256. {
  257. if (!address->ntcp2) address->ntcp2.reset (new NTCP2Ext ());
  258. supportedTransports |= (address->host.is_v4 ()) ? eNTCP2V4 : eNTCP2V6;
  259. Base64ToByteStream (value, strlen (value), address->ntcp2->iv, 16);
  260. address->ntcp2->isPublished = true; // presence if "i" means "published"
  261. }
  262. else if (key[0] == 'i')
  263. {
  264. // introducers
  265. if (!address->ssu)
  266. {
  267. LogPrint (eLogError, "RouterInfo: Introducer is presented for non-SSU address. Skipped");
  268. continue;
  269. }
  270. introducers = true;
  271. size_t l = strlen(key);
  272. unsigned char index = key[l-1] - '0'; // TODO:
  273. key[l-1] = 0;
  274. if (index > 9)
  275. {
  276. LogPrint (eLogError, "RouterInfo: Unexpected introducer's index ", index, " skipped");
  277. if (s) continue; else return;
  278. }
  279. if (index >= address->ssu->introducers.size ())
  280. address->ssu->introducers.resize (index + 1);
  281. Introducer& introducer = address->ssu->introducers.at (index);
  282. if (!strcmp (key, "ihost"))
  283. {
  284. boost::system::error_code ecode;
  285. introducer.iHost = boost::asio::ip::address::from_string (value, ecode);
  286. }
  287. else if (!strcmp (key, "iport"))
  288. introducer.iPort = boost::lexical_cast<int>(value);
  289. else if (!strcmp (key, "itag"))
  290. introducer.iTag = boost::lexical_cast<uint32_t>(value);
  291. else if (!strcmp (key, "ikey"))
  292. Base64ToByteStream (value, strlen (value), introducer.iKey, 32);
  293. else if (!strcmp (key, "iexp"))
  294. introducer.iExp = boost::lexical_cast<uint32_t>(value);
  295. }
  296. if (!s) return;
  297. }
  298. if (introducers) supportedTransports |= eSSUV4; // in case if host is not presented
  299. if (isNTCP2Only && address->ntcp2) address->ntcp2->isNTCP2Only = true;
  300. if (supportedTransports)
  301. {
  302. addresses->push_back(address);
  303. m_SupportedTransports |= supportedTransports;
  304. }
  305. }
  306. #if (BOOST_VERSION >= 105300)
  307. boost::atomic_store (&m_Addresses, addresses);
  308. #else
  309. m_Addresses = addresses; // race condition
  310. #endif
  311. // read peers
  312. uint8_t numPeers;
  313. s.read ((char *)&numPeers, sizeof (numPeers)); if (!s) return;
  314. s.seekg (numPeers*32, std::ios_base::cur); // TODO: read peers
  315. // read properties
  316. uint16_t size, r = 0;
  317. s.read ((char *)&size, sizeof (size)); if (!s) return;
  318. size = be16toh (size);
  319. while (r < size)
  320. {
  321. char key[255], value[255];
  322. r += ReadString (key, 255, s);
  323. s.seekg (1, std::ios_base::cur); r++; // =
  324. r += ReadString (value, 255, s);
  325. s.seekg (1, std::ios_base::cur); r++; // ;
  326. if (!s) return;
  327. m_Properties[key] = value;
  328. // extract caps
  329. if (!strcmp (key, "caps"))
  330. ExtractCaps (value);
  331. // check netId
  332. else if (!strcmp (key, ROUTER_INFO_PROPERTY_NETID) && atoi (value) != i2p::context.GetNetID ())
  333. {
  334. LogPrint (eLogError, "RouterInfo: Unexpected ", ROUTER_INFO_PROPERTY_NETID, "=", value);
  335. m_IsUnreachable = true;
  336. }
  337. // family
  338. else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY))
  339. {
  340. m_Family = value;
  341. boost::to_lower (m_Family);
  342. }
  343. else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY_SIG))
  344. {
  345. if (!netdb.GetFamilies ().VerifyFamily (m_Family, GetIdentHash (), value))
  346. {
  347. LogPrint (eLogWarning, "RouterInfo: family signature verification failed");
  348. m_Family.clear ();
  349. }
  350. }
  351. if (!s) return;
  352. }
  353. if (!m_SupportedTransports || !m_Addresses->size() || (UsesIntroducer () && !introducers))
  354. SetUnreachable (true);
  355. }
  356. bool RouterInfo::IsFamily(const std::string & fam) const {
  357. return m_Family == fam;
  358. }
  359. void RouterInfo::ExtractCaps (const char * value)
  360. {
  361. const char * cap = value;
  362. while (*cap)
  363. {
  364. switch (*cap)
  365. {
  366. case CAPS_FLAG_FLOODFILL:
  367. m_Caps |= Caps::eFloodfill;
  368. break;
  369. case CAPS_FLAG_HIGH_BANDWIDTH1:
  370. case CAPS_FLAG_HIGH_BANDWIDTH2:
  371. case CAPS_FLAG_HIGH_BANDWIDTH3:
  372. m_Caps |= Caps::eHighBandwidth;
  373. break;
  374. case CAPS_FLAG_EXTRA_BANDWIDTH1:
  375. case CAPS_FLAG_EXTRA_BANDWIDTH2:
  376. m_Caps |= Caps::eExtraBandwidth | Caps::eHighBandwidth;
  377. break;
  378. case CAPS_FLAG_HIDDEN:
  379. m_Caps |= Caps::eHidden;
  380. break;
  381. case CAPS_FLAG_REACHABLE:
  382. m_Caps |= Caps::eReachable;
  383. break;
  384. case CAPS_FLAG_UNREACHABLE:
  385. m_Caps |= Caps::eUnreachable;
  386. break;
  387. case CAPS_FLAG_SSU_TESTING:
  388. m_Caps |= Caps::eSSUTesting;
  389. break;
  390. case CAPS_FLAG_SSU_INTRODUCER:
  391. m_Caps |= Caps::eSSUIntroducer;
  392. break;
  393. default: ;
  394. }
  395. cap++;
  396. }
  397. }
  398. void RouterInfo::UpdateCapsProperty ()
  399. {
  400. std::string caps;
  401. if (m_Caps & eFloodfill)
  402. {
  403. if (m_Caps & eExtraBandwidth) caps += (m_Caps & eHighBandwidth) ?
  404. CAPS_FLAG_EXTRA_BANDWIDTH2 : // 'X'
  405. CAPS_FLAG_EXTRA_BANDWIDTH1; // 'P'
  406. else
  407. caps += CAPS_FLAG_HIGH_BANDWIDTH3; // 'O'
  408. caps += CAPS_FLAG_FLOODFILL; // floodfill
  409. }
  410. else
  411. {
  412. if (m_Caps & eExtraBandwidth)
  413. caps += (m_Caps & eHighBandwidth) ? CAPS_FLAG_EXTRA_BANDWIDTH2 /* 'X' */ : CAPS_FLAG_EXTRA_BANDWIDTH1; /*'P' */
  414. else
  415. caps += (m_Caps & eHighBandwidth) ? CAPS_FLAG_HIGH_BANDWIDTH3 /* 'O' */: CAPS_FLAG_LOW_BANDWIDTH2 /* 'L' */; // bandwidth
  416. }
  417. if (m_Caps & eHidden) caps += CAPS_FLAG_HIDDEN; // hidden
  418. if (m_Caps & eReachable) caps += CAPS_FLAG_REACHABLE; // reachable
  419. if (m_Caps & eUnreachable) caps += CAPS_FLAG_UNREACHABLE; // unreachable
  420. SetProperty ("caps", caps);
  421. }
  422. void RouterInfo::WriteToStream (std::ostream& s) const
  423. {
  424. uint64_t ts = htobe64 (m_Timestamp);
  425. s.write ((const char *)&ts, sizeof (ts));
  426. // addresses
  427. uint8_t numAddresses = m_Addresses->size ();
  428. s.write ((char *)&numAddresses, sizeof (numAddresses));
  429. for (const auto& addr_ptr : *m_Addresses)
  430. {
  431. const Address& address = *addr_ptr;
  432. s.write ((const char *)&address.cost, sizeof (address.cost));
  433. s.write ((const char *)&address.date, sizeof (address.date));
  434. std::stringstream properties;
  435. if (address.transportStyle == eTransportNTCP)
  436. WriteString (address.IsNTCP2 () ? "NTCP2" : "NTCP", s);
  437. else if (address.transportStyle == eTransportSSU)
  438. {
  439. WriteString ("SSU", s);
  440. // caps
  441. WriteString ("caps", properties);
  442. properties << '=';
  443. std::string caps;
  444. if (IsPeerTesting ()) caps += CAPS_FLAG_SSU_TESTING;
  445. if (IsIntroducer ()) caps += CAPS_FLAG_SSU_INTRODUCER;
  446. WriteString (caps, properties);
  447. properties << ';';
  448. }
  449. else
  450. WriteString ("", s);
  451. if (!address.IsNTCP2 () || address.IsPublishedNTCP2 ())
  452. {
  453. WriteString ("host", properties);
  454. properties << '=';
  455. WriteString (address.host.to_string (), properties);
  456. properties << ';';
  457. }
  458. if (address.transportStyle == eTransportSSU)
  459. {
  460. // write introducers if any
  461. if (address.ssu->introducers.size () > 0)
  462. {
  463. int i = 0;
  464. for (const auto& introducer: address.ssu->introducers)
  465. {
  466. WriteString ("ihost" + boost::lexical_cast<std::string>(i), properties);
  467. properties << '=';
  468. WriteString (introducer.iHost.to_string (), properties);
  469. properties << ';';
  470. i++;
  471. }
  472. i = 0;
  473. for (const auto& introducer: address.ssu->introducers)
  474. {
  475. WriteString ("ikey" + boost::lexical_cast<std::string>(i), properties);
  476. properties << '=';
  477. char value[64];
  478. size_t l = ByteStreamToBase64 (introducer.iKey, 32, value, 64);
  479. value[l] = 0;
  480. WriteString (value, properties);
  481. properties << ';';
  482. i++;
  483. }
  484. i = 0;
  485. for (const auto& introducer: address.ssu->introducers)
  486. {
  487. WriteString ("iport" + boost::lexical_cast<std::string>(i), properties);
  488. properties << '=';
  489. WriteString (boost::lexical_cast<std::string>(introducer.iPort), properties);
  490. properties << ';';
  491. i++;
  492. }
  493. i = 0;
  494. for (const auto& introducer: address.ssu->introducers)
  495. {
  496. WriteString ("itag" + boost::lexical_cast<std::string>(i), properties);
  497. properties << '=';
  498. WriteString (boost::lexical_cast<std::string>(introducer.iTag), properties);
  499. properties << ';';
  500. i++;
  501. }
  502. i = 0;
  503. for (const auto& introducer: address.ssu->introducers)
  504. {
  505. if (introducer.iExp) // expiration is specified
  506. {
  507. WriteString ("iexp" + boost::lexical_cast<std::string>(i), properties);
  508. properties << '=';
  509. WriteString (boost::lexical_cast<std::string>(introducer.iExp), properties);
  510. properties << ';';
  511. }
  512. i++;
  513. }
  514. }
  515. // write intro key
  516. WriteString ("key", properties);
  517. properties << '=';
  518. char value[64];
  519. size_t l = ByteStreamToBase64 (address.ssu->key, 32, value, 64);
  520. value[l] = 0;
  521. WriteString (value, properties);
  522. properties << ';';
  523. // write mtu
  524. if (address.ssu->mtu)
  525. {
  526. WriteString ("mtu", properties);
  527. properties << '=';
  528. WriteString (boost::lexical_cast<std::string>(address.ssu->mtu), properties);
  529. properties << ';';
  530. }
  531. }
  532. if (address.IsPublishedNTCP2 ())
  533. {
  534. // publish i for NTCP2
  535. WriteString ("i", properties); properties << '=';
  536. WriteString (address.ntcp2->iv.ToBase64 (), properties); properties << ';';
  537. }
  538. if (!address.IsNTCP2 () || address.IsPublishedNTCP2 ())
  539. {
  540. WriteString ("port", properties);
  541. properties << '=';
  542. WriteString (boost::lexical_cast<std::string>(address.port), properties);
  543. properties << ';';
  544. }
  545. if (address.IsNTCP2 ())
  546. {
  547. // publish s and v for NTCP2
  548. WriteString ("s", properties); properties << '=';
  549. WriteString (address.ntcp2->staticKey.ToBase64 (), properties); properties << ';';
  550. WriteString ("v", properties); properties << '=';
  551. WriteString ("2", properties); properties << ';';
  552. }
  553. uint16_t size = htobe16 (properties.str ().size ());
  554. s.write ((char *)&size, sizeof (size));
  555. s.write (properties.str ().c_str (), properties.str ().size ());
  556. }
  557. // peers
  558. uint8_t numPeers = 0;
  559. s.write ((char *)&numPeers, sizeof (numPeers));
  560. // properties
  561. std::stringstream properties;
  562. for (const auto& p : m_Properties)
  563. {
  564. WriteString (p.first, properties);
  565. properties << '=';
  566. WriteString (p.second, properties);
  567. properties << ';';
  568. }
  569. uint16_t size = htobe16 (properties.str ().size ());
  570. s.write ((char *)&size, sizeof (size));
  571. s.write (properties.str ().c_str (), properties.str ().size ());
  572. }
  573. bool RouterInfo::IsNewer (const uint8_t * buf, size_t len) const
  574. {
  575. if (!m_RouterIdentity) return false;
  576. size_t size = m_RouterIdentity->GetFullLen ();
  577. if (size + 8 > len) return false;
  578. return bufbe64toh (buf + size) > m_Timestamp;
  579. }
  580. const uint8_t * RouterInfo::LoadBuffer ()
  581. {
  582. if (!m_Buffer)
  583. {
  584. if (LoadFile ())
  585. LogPrint (eLogDebug, "RouterInfo: Buffer for ", GetIdentHashAbbreviation (GetIdentHash ()), " loaded from file");
  586. }
  587. return m_Buffer;
  588. }
  589. void RouterInfo::CreateBuffer (const PrivateKeys& privateKeys)
  590. {
  591. m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp
  592. std::stringstream s;
  593. uint8_t ident[1024];
  594. auto identLen = privateKeys.GetPublic ()->ToBuffer (ident, 1024);
  595. auto signatureLen = privateKeys.GetPublic ()->GetSignatureLen ();
  596. s.write ((char *)ident, identLen);
  597. WriteToStream (s);
  598. m_BufferLen = s.str ().size ();
  599. if (!m_Buffer)
  600. m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
  601. if (m_BufferLen + signatureLen < MAX_RI_BUFFER_SIZE)
  602. {
  603. memcpy (m_Buffer, s.str ().c_str (), m_BufferLen);
  604. // signature
  605. privateKeys.Sign ((uint8_t *)m_Buffer, m_BufferLen, (uint8_t *)m_Buffer + m_BufferLen);
  606. m_BufferLen += signatureLen;
  607. }
  608. else
  609. LogPrint (eLogError, "RouterInfo: Our RouterInfo is too long ", m_BufferLen + signatureLen);
  610. }
  611. bool RouterInfo::SaveToFile (const std::string& fullPath)
  612. {
  613. m_FullPath = fullPath;
  614. if (!m_Buffer) {
  615. LogPrint (eLogError, "RouterInfo: Can't save, m_Buffer == NULL");
  616. return false;
  617. }
  618. std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
  619. if (!f.is_open ()) {
  620. LogPrint(eLogError, "RouterInfo: Can't save to ", fullPath);
  621. return false;
  622. }
  623. f.write ((char *)m_Buffer, m_BufferLen);
  624. return true;
  625. }
  626. size_t RouterInfo::ReadString (char * str, size_t len, std::istream& s) const
  627. {
  628. uint8_t l;
  629. s.read ((char *)&l, 1);
  630. if (l < len)
  631. {
  632. s.read (str, l);
  633. if (!s) l = 0; // failed, return empty string
  634. str[l] = 0;
  635. }
  636. else
  637. {
  638. LogPrint (eLogWarning, "RouterInfo: string length ", (int)l, " exceeds buffer size ", len);
  639. s.seekg (l, std::ios::cur); // skip
  640. str[0] = 0;
  641. }
  642. return l+1;
  643. }
  644. void RouterInfo::WriteString (const std::string& str, std::ostream& s) const
  645. {
  646. uint8_t len = str.size ();
  647. s.write ((char *)&len, 1);
  648. s.write (str.c_str (), len);
  649. }
  650. void RouterInfo::AddNTCPAddress (const char * host, int port)
  651. {
  652. auto addr = std::make_shared<Address>();
  653. addr->host = boost::asio::ip::address::from_string (host);
  654. addr->port = port;
  655. addr->transportStyle = eTransportNTCP;
  656. addr->cost = 6;
  657. addr->date = 0;
  658. for (const auto& it: *m_Addresses) // don't insert same address twice
  659. if (*it == *addr) return;
  660. m_SupportedTransports |= addr->host.is_v6 () ? eNTCPV6 : eNTCPV4;
  661. m_Addresses->push_front(std::move(addr)); // always make NTCP first
  662. }
  663. void RouterInfo::AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu)
  664. {
  665. auto addr = std::make_shared<Address>();
  666. addr->host = boost::asio::ip::address::from_string (host);
  667. addr->port = port;
  668. addr->transportStyle = eTransportSSU;
  669. addr->cost = 10; // NTCP should have priority over SSU
  670. addr->date = 0;
  671. addr->ssu.reset (new SSUExt ());
  672. addr->ssu->mtu = mtu;
  673. memcpy (addr->ssu->key, key, 32);
  674. for (const auto& it: *m_Addresses) // don't insert same address twice
  675. if (*it == *addr) return;
  676. m_SupportedTransports |= addr->host.is_v6 () ? eSSUV6 : eSSUV4;
  677. m_Addresses->push_back(std::move(addr));
  678. m_Caps |= eSSUTesting;
  679. m_Caps |= eSSUIntroducer;
  680. }
  681. void RouterInfo::AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv, const boost::asio::ip::address& host, int port)
  682. {
  683. auto addr = std::make_shared<Address>();
  684. addr->host = host;
  685. addr->port = port;
  686. addr->transportStyle = eTransportNTCP;
  687. addr->cost = port ? 3 : 14; // override from RouterContext::PublishNTCP2Address
  688. addr->date = 0;
  689. addr->ntcp2.reset (new NTCP2Ext ());
  690. addr->ntcp2->isNTCP2Only = true; // NTCP2 only address
  691. if (port) addr->ntcp2->isPublished = true;
  692. memcpy (addr->ntcp2->staticKey, staticKey, 32);
  693. memcpy (addr->ntcp2->iv, iv, 16);
  694. m_Addresses->push_back(std::move(addr));
  695. }
  696. bool RouterInfo::AddIntroducer (const Introducer& introducer)
  697. {
  698. for (auto& addr : *m_Addresses)
  699. {
  700. if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ())
  701. {
  702. for (auto& intro: addr->ssu->introducers)
  703. if (intro.iTag == introducer.iTag) return false; // already presented
  704. addr->ssu->introducers.push_back (introducer);
  705. return true;
  706. }
  707. }
  708. return false;
  709. }
  710. bool RouterInfo::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
  711. {
  712. for (auto& addr: *m_Addresses)
  713. {
  714. if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ())
  715. {
  716. for (auto it = addr->ssu->introducers.begin (); it != addr->ssu->introducers.end (); ++it)
  717. if ( boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e)
  718. {
  719. addr->ssu->introducers.erase (it);
  720. return true;
  721. }
  722. }
  723. }
  724. return false;
  725. }
  726. void RouterInfo::SetCaps (uint8_t caps)
  727. {
  728. m_Caps = caps;
  729. UpdateCapsProperty ();
  730. }
  731. void RouterInfo::SetCaps (const char * caps)
  732. {
  733. SetProperty ("caps", caps);
  734. m_Caps = 0;
  735. ExtractCaps (caps);
  736. }
  737. void RouterInfo::SetProperty (const std::string& key, const std::string& value)
  738. {
  739. m_Properties[key] = value;
  740. }
  741. void RouterInfo::DeleteProperty (const std::string& key)
  742. {
  743. m_Properties.erase (key);
  744. }
  745. std::string RouterInfo::GetProperty (const std::string& key) const
  746. {
  747. auto it = m_Properties.find (key);
  748. if (it != m_Properties.end ())
  749. return it->second;
  750. return "";
  751. }
  752. bool RouterInfo::IsNTCP (bool v4only) const
  753. {
  754. if (v4only)
  755. return m_SupportedTransports & eNTCPV4;
  756. else
  757. return m_SupportedTransports & (eNTCPV4 | eNTCPV6);
  758. }
  759. bool RouterInfo::IsSSU (bool v4only) const
  760. {
  761. if (v4only)
  762. return m_SupportedTransports & eSSUV4;
  763. else
  764. return m_SupportedTransports & (eSSUV4 | eSSUV6);
  765. }
  766. bool RouterInfo::IsSSUV6 () const
  767. {
  768. return m_SupportedTransports & eSSUV6;
  769. }
  770. bool RouterInfo::IsNTCP2 (bool v4only) const
  771. {
  772. if (v4only)
  773. return m_SupportedTransports & eNTCP2V4;
  774. else
  775. return m_SupportedTransports & (eNTCP2V4 | eNTCP2V6);
  776. }
  777. bool RouterInfo::IsV6 () const
  778. {
  779. return m_SupportedTransports & (eNTCPV6 | eSSUV6 | eNTCP2V6);
  780. }
  781. bool RouterInfo::IsV4 () const
  782. {
  783. return m_SupportedTransports & (eNTCPV4 | eSSUV4 | eNTCP2V4);
  784. }
  785. void RouterInfo::EnableV6 ()
  786. {
  787. if (!IsV6 ())
  788. m_SupportedTransports |= eNTCPV6 | eSSUV6 | eNTCP2V6;
  789. }
  790. void RouterInfo::EnableV4 ()
  791. {
  792. if (!IsV4 ())
  793. m_SupportedTransports |= eNTCPV4 | eSSUV4 | eNTCP2V4;
  794. }
  795. void RouterInfo::DisableV6 ()
  796. {
  797. if (IsV6 ())
  798. {
  799. m_SupportedTransports &= ~(eNTCPV6 | eSSUV6 | eNTCP2V6);
  800. for (auto it = m_Addresses->begin (); it != m_Addresses->end ();)
  801. {
  802. auto addr = *it;
  803. if (addr->host.is_v6 ())
  804. it = m_Addresses->erase (it);
  805. else
  806. ++it;
  807. }
  808. }
  809. }
  810. void RouterInfo::DisableV4 ()
  811. {
  812. if (IsV4 ())
  813. {
  814. m_SupportedTransports &= ~(eNTCPV4 | eSSUV4 | eNTCP2V4);
  815. for (auto it = m_Addresses->begin (); it != m_Addresses->end ();)
  816. {
  817. auto addr = *it;
  818. if (addr->host.is_v4 ())
  819. it = m_Addresses->erase (it);
  820. else
  821. ++it;
  822. }
  823. }
  824. }
  825. bool RouterInfo::UsesIntroducer () const
  826. {
  827. return m_Caps & Caps::eUnreachable; // non-reachable
  828. }
  829. std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCPAddress (bool v4only) const
  830. {
  831. return GetAddress (
  832. [v4only](std::shared_ptr<const RouterInfo::Address> address)->bool
  833. {
  834. return (address->transportStyle == eTransportNTCP) && !address->IsNTCP2Only () && (!v4only || address->host.is_v4 ());
  835. });
  836. }
  837. std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSUAddress (bool v4only) const
  838. {
  839. return GetAddress (
  840. [v4only](std::shared_ptr<const RouterInfo::Address> address)->bool
  841. {
  842. return (address->transportStyle == eTransportSSU) && (!v4only || address->host.is_v4 ());
  843. });
  844. }
  845. std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSUV6Address () const
  846. {
  847. return GetAddress (
  848. [](std::shared_ptr<const RouterInfo::Address> address)->bool
  849. {
  850. return (address->transportStyle == eTransportSSU) && address->host.is_v6 ();
  851. });
  852. }
  853. template<typename Filter>
  854. std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (Filter filter) const
  855. {
  856. // TODO: make it more generic using comparator
  857. #if (BOOST_VERSION >= 105300)
  858. auto addresses = boost::atomic_load (&m_Addresses);
  859. #else
  860. auto addresses = m_Addresses;
  861. #endif
  862. for (const auto& address : *addresses)
  863. if (filter (address)) return address;
  864. return nullptr;
  865. }
  866. std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCP2Address (bool publishedOnly, bool v4only) const
  867. {
  868. return GetAddress (
  869. [publishedOnly, v4only](std::shared_ptr<const RouterInfo::Address> address)->bool
  870. {
  871. return address->IsNTCP2 () && (!publishedOnly || address->IsPublishedNTCP2 ()) && (!v4only || address->host.is_v4 ());
  872. });
  873. }
  874. std::shared_ptr<RouterProfile> RouterInfo::GetProfile () const
  875. {
  876. if (!m_Profile)
  877. m_Profile = GetRouterProfile (GetIdentHash ());
  878. return m_Profile;
  879. }
  880. void RouterInfo::Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const
  881. {
  882. auto encryptor = m_RouterIdentity->CreateEncryptor (nullptr);
  883. if (encryptor)
  884. encryptor->Encrypt (data, encrypted, ctx, true);
  885. }
  886. }
  887. }