Skip to content

Commit 8feb4e4

Browse files
naumenkogssipa
andcommitted
Add asmap utility which queries a mapping
The scripts for creating a compact IP->ASN mapping are here: https://github.com/sipa/asmap Co-authored-by: Pieter Wuille <[email protected]>
1 parent 89a1f7a commit 8feb4e4

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ BITCOIN_CORE_H = \
210210
txmempool.h \
211211
ui_interface.h \
212212
undo.h \
213+
util/asmap.h \
213214
util/bip32.h \
214215
util/bytevectorhash.h \
215216
util/check.h \
@@ -510,6 +511,7 @@ libbitcoin_util_a_SOURCES = \
510511
support/cleanse.cpp \
511512
sync.cpp \
512513
threadinterrupt.cpp \
514+
util/asmap.cpp \
513515
util/bip32.cpp \
514516
util/bytevectorhash.cpp \
515517
util/error.cpp \

src/util/asmap.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <vector>
6+
#include <assert.h>
7+
#include <crypto/common.h>
8+
9+
namespace {
10+
11+
uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, uint8_t minval, const std::vector<uint8_t> &bit_sizes)
12+
{
13+
uint32_t val = minval;
14+
bool bit;
15+
for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
16+
bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
17+
if (bit_sizes_it + 1 != bit_sizes.end()) {
18+
bit = *bitpos;
19+
bitpos++;
20+
} else {
21+
bit = 0;
22+
}
23+
if (bit) {
24+
val += (1 << *bit_sizes_it);
25+
} else {
26+
for (int b = 0; b < *bit_sizes_it; b++) {
27+
bit = *bitpos;
28+
bitpos++;
29+
val += bit << (*bit_sizes_it - 1 - b);
30+
}
31+
return val;
32+
}
33+
}
34+
return -1;
35+
}
36+
37+
const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
38+
uint32_t DecodeType(std::vector<bool>::const_iterator& bitpos)
39+
{
40+
return DecodeBits(bitpos, 0, TYPE_BIT_SIZES);
41+
}
42+
43+
const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
44+
uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos)
45+
{
46+
return DecodeBits(bitpos, 1, ASN_BIT_SIZES);
47+
}
48+
49+
50+
const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
51+
uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos)
52+
{
53+
return DecodeBits(bitpos, 2, MATCH_BIT_SIZES);
54+
}
55+
56+
57+
const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
58+
uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos)
59+
{
60+
return DecodeBits(bitpos, 17, JUMP_BIT_SIZES);
61+
}
62+
63+
}
64+
65+
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
66+
{
67+
std::vector<bool>::const_iterator pos = asmap.begin();
68+
uint8_t bits = ip.size();
69+
uint8_t default_asn = 0;
70+
uint32_t opcode, jump, match, matchlen;
71+
while (1) {
72+
assert(pos != asmap.end());
73+
opcode = DecodeType(pos);
74+
if (opcode == 0) {
75+
return DecodeASN(pos);
76+
} else if (opcode == 1) {
77+
jump = DecodeJump(pos);
78+
if (ip[ip.size() - bits]) {
79+
pos += jump;
80+
}
81+
bits--;
82+
} else if (opcode == 2) {
83+
match = DecodeMatch(pos);
84+
matchlen = CountBits(match) - 1;
85+
for (uint32_t bit = 0; bit < matchlen; bit++) {
86+
if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
87+
return default_asn;
88+
}
89+
bits--;
90+
}
91+
} else if (opcode == 3) {
92+
default_asn = DecodeASN(pos);
93+
} else {
94+
assert(0);
95+
}
96+
}
97+
}

src/util/asmap.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_ASMAP_H
6+
#define BITCOIN_UTIL_ASMAP_H
7+
8+
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip);
9+
10+
#endif // BITCOIN_UTIL_ASMAP_H

0 commit comments

Comments
 (0)