23
23
#include " serialize.h"
24
24
#include " streams.h"
25
25
26
- int CAddrInfo::GetTriedBucket (const uint256& nKey) const
26
+ int CAddrInfo::GetTriedBucket (const uint256& nKey, const std::vector< bool > &asmap ) const
27
27
{
28
28
uint64_t hash1 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetKey ()).GetHash ().GetCheapHash ();
29
- uint64_t hash2 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetGroup () << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetHash ().GetCheapHash ();
30
- return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
29
+ uint64_t hash2 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetGroup (asmap) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetHash ().GetCheapHash ();
30
+ int tried_bucket = hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
31
+ uint32_t mapped_as = GetMappedAS (asmap);
32
+ LogPrint (" net" , " IP %s mapped to AS%i belongs to tried bucket %i\n " , ToStringIP (), mapped_as, tried_bucket);
33
+ return tried_bucket;
31
34
}
32
35
33
- int CAddrInfo::GetNewBucket (const uint256& nKey, const CNetAddr& src) const
36
+ int CAddrInfo::GetNewBucket (const uint256& nKey, const CNetAddr& src, const std::vector< bool > &asmap ) const
34
37
{
35
- std::vector<unsigned char > vchSourceGroupKey = src.GetGroup ();
36
- uint64_t hash1 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetGroup () << vchSourceGroupKey).GetHash ().GetCheapHash ();
38
+ std::vector<unsigned char > vchSourceGroupKey = src.GetGroup (asmap );
39
+ uint64_t hash1 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetGroup (asmap ) << vchSourceGroupKey).GetHash ().GetCheapHash ();
37
40
uint64_t hash2 = (CHashWriter (SER_GETHASH, 0 ) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetHash ().GetCheapHash ();
38
- return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
41
+ int new_bucket = hash2 % ADDRMAN_NEW_BUCKET_COUNT;
42
+ uint32_t mapped_as = GetMappedAS (asmap);
43
+ LogPrint (" net" , " IP %s mapped to AS%i belongs to new bucket %i\n " , ToStringIP (), mapped_as, new_bucket);
44
+ return new_bucket;
39
45
}
40
46
41
47
int CAddrInfo::GetBucketPosition (const uint256 &nKey, bool fNew , int nBucket) const
@@ -175,7 +181,7 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
175
181
assert (info.nRefCount == 0 );
176
182
177
183
// which tried bucket to move the entry to
178
- int nKBucket = info.GetTriedBucket (nKey);
184
+ int nKBucket = info.GetTriedBucket (nKey, m_asmap );
179
185
int nKBucketPos = info.GetBucketPosition (nKey, false , nKBucket);
180
186
181
187
// first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
@@ -191,7 +197,7 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
191
197
nTried--;
192
198
193
199
// find which new bucket it belongs to
194
- int nUBucket = infoOld.GetNewBucket (nKey);
200
+ int nUBucket = infoOld.GetNewBucket (nKey, m_asmap );
195
201
int nUBucketPos = infoOld.GetBucketPosition (nKey, true , nUBucket);
196
202
ClearNew (nUBucket, nUBucketPos);
197
203
assert (vvNew[nUBucket][nUBucketPos] == -1 );
@@ -301,7 +307,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
301
307
fNew = true ;
302
308
}
303
309
304
- int nUBucket = pinfo->GetNewBucket (nKey, source);
310
+ int nUBucket = pinfo->GetNewBucket (nKey, source, m_asmap );
305
311
int nUBucketPos = pinfo->GetBucketPosition (nKey, true , nUBucket);
306
312
if (vvNew[nUBucket][nUBucketPos] != nId) {
307
313
bool fInsert = vvNew[nUBucket][nUBucketPos] == -1 ;
@@ -453,7 +459,7 @@ int CAddrMan::Check_()
453
459
if (vvTried[n][i] != -1 ) {
454
460
if (!setTried.count (vvTried[n][i]))
455
461
return -11 ;
456
- if (mapInfo[vvTried[n][i]].GetTriedBucket (nKey) != n)
462
+ if (mapInfo[vvTried[n][i]].GetTriedBucket (nKey, m_asmap ) != n)
457
463
return -17 ;
458
464
if (mapInfo[vvTried[n][i]].GetBucketPosition (nKey, false , n) != i)
459
465
return -18 ;
@@ -530,3 +536,30 @@ void CAddrMan::Connected_(const CService& addr, int64_t nTime)
530
536
int CAddrMan::RandomInt (int nMax){
531
537
return GetRandInt (nMax);
532
538
}
539
+
540
+ std::vector<bool > CAddrMan::DecodeAsmap (fs::path path)
541
+ {
542
+ std::vector<bool > bits;
543
+ FILE *filestr = fsbridge::fopen (path, " rb" );
544
+ CAutoFile file (filestr, SER_DISK, CLIENT_VERSION);
545
+ if (file.IsNull ()) {
546
+ LogPrintf (" Failed to open asmap file from disk\n " );
547
+ return bits;
548
+ }
549
+ fseek (filestr, 0 , SEEK_END);
550
+ int length = ftell (filestr);
551
+ LogPrintf (" Opened asmap file %s (%d bytes) from disk\n " , path, length);
552
+ fseek (filestr, 0 , SEEK_SET);
553
+ char cur_byte;
554
+ for (int i = 0 ; i < length; ++i) {
555
+ file >> cur_byte;
556
+ for (int bit = 0 ; bit < 8 ; ++bit) {
557
+ bits.push_back ((cur_byte >> bit) & 1 );
558
+ }
559
+ }
560
+ if (!SanityCheckASMap (bits)) {
561
+ LogPrintf (" Sanity check of asmap file %s failed\n " , path);
562
+ return {};
563
+ }
564
+ return bits;
565
+ }
0 commit comments