Skip to content

Commit 5b44784

Browse files
committed
Merge bitcoin/bitcoin#28577: net: raise V1_PREFIX_LEN from 12 to 16
ba2e5bf net: raise V1_PREFIX_LEN from 12 to 16 (Pieter Wuille) Pull request description: A "version" message in the V1 protocol starts with a fixed 16 bytes: * The 4-byte network magic * The 12-byte command string: "version" plus 5 0x00 bytes The current code detects incoming V1 connections by just looking at the first 12 bytes (matching an [earlier version](bitcoin/bips#1496) of BIP324), but 16 bytes is more precise. This isn't an observable difference right now, as a 12 byte prefix ought to be negligible already, but it may become observable with future extensions to the protocol, so make the code match the specification. ACKs for top commit: achow101: ACK ba2e5bf theStack: re-ACK ba2e5bf mzumsande: Code review ACK ba2e5bf Tree-SHA512: 64876b03613bd1c5dda82f4ca1b367014365f9ae4cfa30f45c5758a563c68cbea81a98d02ba616c264674c204517aac8b7de94da10f32e77b56267a43710c651
2 parents 8369467 + ba2e5bf commit 5b44784

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/net.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,10 @@ void V2Transport::ProcessReceivedMaybeV1Bytes() noexcept
10981098
AssertLockNotHeld(m_send_mutex);
10991099
Assume(m_recv_state == RecvState::KEY_MAYBE_V1);
11001100
// We still have to determine if this is a v1 or v2 connection. The bytes being received could
1101-
// be the beginning of either a v1 packet (network magic + "version\x00"), or of a v2 public
1102-
// key. BIP324 specifies that a mismatch with this 12-byte string should trigger sending of the
1103-
// key.
1104-
std::array<uint8_t, V1_PREFIX_LEN> v1_prefix = {0, 0, 0, 0, 'v', 'e', 'r', 's', 'i', 'o', 'n', 0};
1101+
// be the beginning of either a v1 packet (network magic + "version\x00\x00\x00\x00\x00"), or
1102+
// of a v2 public key. BIP324 specifies that a mismatch with this 16-byte string should trigger
1103+
// sending of the key.
1104+
std::array<uint8_t, V1_PREFIX_LEN> v1_prefix = {0, 0, 0, 0, 'v', 'e', 'r', 's', 'i', 'o', 'n', 0, 0, 0, 0, 0};
11051105
std::copy(std::begin(Params().MessageStart()), std::end(Params().MessageStart()), v1_prefix.begin());
11061106
Assume(m_recv_buffer.size() <= v1_prefix.size());
11071107
if (!std::equal(m_recv_buffer.begin(), m_recv_buffer.end(), v1_prefix.begin())) {
@@ -1295,7 +1295,7 @@ size_t V2Transport::GetMaxBytesToProcess() noexcept
12951295
// receive buffer.
12961296
Assume(m_recv_buffer.size() <= V1_PREFIX_LEN);
12971297
// As long as we're not sure if this is a v1 or v2 connection, don't receive more than what
1298-
// is strictly necessary to distinguish the two (12 bytes). If we permitted more than
1298+
// is strictly necessary to distinguish the two (16 bytes). If we permitted more than
12991299
// the v1 header size (24 bytes), we may not be able to feed the already-received bytes
13001300
// back into the m_v1_fallback V1 transport.
13011301
return V1_PREFIX_LEN - m_recv_buffer.size();

src/net.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ class V2Transport final : public Transport
473473

474474
/** The length of the V1 prefix to match bytes initially received by responders with to
475475
* determine if their peer is speaking V1 or V2. */
476-
static constexpr size_t V1_PREFIX_LEN = 12;
476+
static constexpr size_t V1_PREFIX_LEN = 16;
477477

478478
// The sender side and receiver side of V2Transport are state machines that are transitioned
479479
// through, based on what has been received. The receive state corresponds to the contents of,

0 commit comments

Comments
 (0)