MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ProcessReceivedPacketBytes

Method ProcessReceivedPacketBytes

src/net.cpp:1212–1279  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1210}
1211
1212bool V2Transport::ProcessReceivedPacketBytes() noexcept
1213{
1214 AssertLockHeld(m_recv_mutex);
1215 Assume(m_recv_state == RecvState::VERSION || m_recv_state == RecvState::APP);
1216
1217 // The maximum permitted contents length for a packet, consisting of:
1218 // - 0x00 byte: indicating long message type encoding
1219 // - 12 bytes of message type
1220 // - payload
1221 static constexpr size_t MAX_CONTENTS_LEN =
1222 1 + CMessageHeader::MESSAGE_TYPE_SIZE +
1223 std::min<size_t>(MAX_SIZE, MAX_PROTOCOL_MESSAGE_LENGTH);
1224
1225 if (m_recv_buffer.size() == BIP324Cipher::LENGTH_LEN) {
1226 // Length descriptor received.
1227 m_recv_len = m_cipher.DecryptLength(MakeByteSpan(m_recv_buffer));
1228 if (m_recv_len > MAX_CONTENTS_LEN) {
1229 LogDebug(BCLog::NET, "V2 transport error: packet too large (%u bytes), peer=%d\n", m_recv_len, m_nodeid);
1230 return false;
1231 }
1232 } else if (m_recv_buffer.size() > BIP324Cipher::LENGTH_LEN && m_recv_buffer.size() == m_recv_len + BIP324Cipher::EXPANSION) {
1233 // Ciphertext received, decrypt it into m_recv_decode_buffer.
1234 // Note that it is impossible to reach this branch without hitting the branch above first,
1235 // as GetMaxBytesToProcess only allows up to LENGTH_LEN into the buffer before that point.
1236 m_recv_decode_buffer.resize(m_recv_len);
1237 bool ignore{false};
1238 bool ret = m_cipher.Decrypt(
1239 /*input=*/MakeByteSpan(m_recv_buffer).subspan(BIP324Cipher::LENGTH_LEN),
1240 /*aad=*/MakeByteSpan(m_recv_aad),
1241 /*ignore=*/ignore,
1242 /*contents=*/MakeWritableByteSpan(m_recv_decode_buffer));
1243 if (!ret) {
1244 LogDebug(BCLog::NET, "V2 transport error: packet decryption failure (%u bytes), peer=%d\n", m_recv_len, m_nodeid);
1245 return false;
1246 }
1247 // We have decrypted a valid packet with the AAD we expected, so clear the expected AAD.
1248 ClearShrink(m_recv_aad);
1249 // Feed the last 4 bytes of the Poly1305 authentication tag (and its timing) into our RNG.
1250 RandAddEvent(ReadLE32(m_recv_buffer.data() + m_recv_buffer.size() - 4));
1251
1252 // At this point we have a valid packet decrypted into m_recv_decode_buffer. If it's not a
1253 // decoy, which we simply ignore, use the current state to decide what to do with it.
1254 if (!ignore) {
1255 switch (m_recv_state) {
1256 case RecvState::VERSION:
1257 // Version message received; transition to application phase. The contents is
1258 // ignored, but can be used for future extensions.
1259 SetReceiveState(RecvState::APP);
1260 break;
1261 case RecvState::APP:
1262 // Application message decrypted correctly. It can be extracted using GetMessage().
1263 SetReceiveState(RecvState::APP_READY);
1264 break;
1265 default:
1266 // Any other state is invalid (this function should not have been called).
1267 Assume(false);
1268 }
1269 }

Callers

nothing calls this directly

Calls 10

MakeByteSpanFunction · 0.85
MakeWritableByteSpanFunction · 0.85
ClearShrinkFunction · 0.85
RandAddEventFunction · 0.85
ReadLE32Function · 0.85
DecryptLengthMethod · 0.80
sizeMethod · 0.45
resizeMethod · 0.45
DecryptMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected