Skip to content

Commit d97d272

Browse files
committed
parse compact tracker response
1 parent af1106e commit d97d272

2 files changed

Lines changed: 14 additions & 33 deletions

File tree

libi2pd_client/Torrents.cpp

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ namespace torrents
295295
return len;
296296
}
297297

298-
std::string Torrent::GetHexStraingInfoHash () const
298+
std::string Torrent::GetHexStringInfoHash () const
299299
{
300300
std::string infoHash;
301301
BIGNUM * bn = BN_bin2bn (m_InfoHash.data (), SHA_DIGEST_LENGTH, nullptr);
@@ -313,7 +313,7 @@ namespace torrents
313313
{
314314
i2p::http::HTTPReq req;
315315
req.parse (m_Announce);
316-
req.AddHeader ("info_hash", GetHexStraingInfoHash ());
316+
req.AddHeader ("info_hash", GetHexStringInfoHash ());
317317
return req;
318318
}
319319

@@ -336,32 +336,14 @@ namespace torrents
336336
size_t Torrent::ParsePeers (std::string_view buf)
337337
{
338338
m_Peers.clear ();
339-
return ParseList (buf, std::bind (&Torrent::ParsePeer, this, std::placeholders::_1));
340-
}
341-
342-
size_t Torrent::ParsePeer (std::string_view buf)
343-
{
344-
std::string peerID;
345-
std::shared_ptr<const i2p::client::Address> addr;
346-
size_t ret = ParseDictionary (buf, [&peerID, &addr](std::string_view key, std::string_view buf)->size_t
347-
{
348-
if (key == "id") // peer_id
349-
{
350-
auto [id, l] = ExtractByteString (buf);
351-
if (l) peerID = id;
352-
return l;
353-
}
354-
else if (key == "ip") // address
355-
{
356-
auto [address, l] = ExtractByteString (buf);
357-
if (l) addr = i2p::client::context.GetAddressBook ().GetAddress (address);
358-
return l;
359-
}
360-
return 0;
361-
});
362-
if (ret && addr)
363-
m_Peers.emplace_back (std::pair{ peerID, addr });
364-
return ret;
339+
auto [hashes, len] = ExtractByteString (buf);
340+
while (!hashes.empty ())
341+
{
342+
m_Peers.emplace_back (std::make_shared<const i2p::client::Address>(
343+
i2p::data::IdentHash((const uint8_t *)hashes.substr (0, SHA256_DIGEST_LENGTH).data ())));
344+
hashes = hashes.substr (SHA256_DIGEST_LENGTH);
345+
}
346+
return len;
365347
}
366348

367349
PeerConnection::PeerConnection (i2p::client::I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream):
@@ -745,12 +727,12 @@ namespace torrents
745727
return;
746728
}
747729
req.AddHeader ("peer_id", m_PeerID);
748-
req.AddHeader ("ip", GetLocalDestination ()->GetIdentHash ().ToBase32 () + ".b32.i2p");
730+
req.AddHeader ("ip", GetLocalDestination ()->GetIdentity ()->ToBase64 ());
749731
req.AddHeader ("port", std::to_string (6881));
732+
req.AddHeader ("compact", "1");
750733
req.AddHeader ("uploaded", "0"); // TODO
751734
req.AddHeader ("downloaded", "0"); // TODO
752735
req.AddHeader ("left", "1"); // TODO
753-
req.AddHeader ("compact", "1"); // TODO
754736
req.AddHeader ("numwant", "0"); // TODO
755737
CreateStream ([this, req, torrent](std::shared_ptr<i2p::stream::Stream> stream)
756738
{

libi2pd_client/Torrents.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ namespace torrents
9191

9292
private:
9393

94-
std::string GetHexStraingInfoHash () const;
94+
std::string GetHexStringInfoHash () const;
9595

9696
size_t ParsePieces (std::string_view buf);
9797
size_t ParseInfo (std::string_view buf);
9898
size_t ParsePeers (std::string_view buf);
99-
size_t ParsePeer (std::string_view buf);
10099

101100
private:
102101

@@ -105,7 +104,7 @@ namespace torrents
105104
int m_Interval;
106105
InfoHash m_InfoHash; // SHA1
107106
std::vector<Piece> m_Pieces;
108-
std::list<std::pair<std::string, std::shared_ptr<const i2p::client::Address> > > m_Peers;
107+
std::list<std::shared_ptr<const i2p::client::Address> > m_Peers;
109108
};
110109

111110
class TorrentsTunnel;

0 commit comments

Comments
 (0)