| 737 | } |
| 738 | |
| 739 | int V1Transport::readHeader(std::span<const uint8_t> msg_bytes) |
| 740 | { |
| 741 | AssertLockHeld(m_recv_mutex); |
| 742 | // copy data to temporary parsing buffer |
| 743 | unsigned int nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos; |
| 744 | unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size()); |
| 745 | |
| 746 | memcpy(&hdrbuf[nHdrPos], msg_bytes.data(), nCopy); |
| 747 | nHdrPos += nCopy; |
| 748 | |
| 749 | // if header incomplete, exit |
| 750 | if (nHdrPos < CMessageHeader::HEADER_SIZE) |
| 751 | return nCopy; |
| 752 | |
| 753 | // deserialize to CMessageHeader |
| 754 | try { |
| 755 | hdrbuf >> hdr; |
| 756 | } |
| 757 | catch (const std::exception&) { |
| 758 | LogDebug(BCLog::NET, "Header error: Unable to deserialize, peer=%d\n", m_node_id); |
| 759 | return -1; |
| 760 | } |
| 761 | |
| 762 | // Check start string, network magic |
| 763 | if (hdr.pchMessageStart != m_magic_bytes) { |
| 764 | LogDebug(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id); |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | // reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH |
| 769 | // NOTE: failing to perform this check previously allowed a malicious peer to make us allocate 32MiB of memory per |
| 770 | // connection. See https://bitcoincore.org/en/2024/07/03/disclose_receive_buffer_oom. |
| 771 | if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) { |
| 772 | LogDebug(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetMessageType()), hdr.nMessageSize, m_node_id); |
| 773 | return -1; |
| 774 | } |
| 775 | |
| 776 | // switch state to reading message data |
| 777 | in_data = true; |
| 778 | |
| 779 | return nCopy; |
| 780 | } |
| 781 | |
| 782 | int V1Transport::readData(std::span<const uint8_t> msg_bytes) |
| 783 | { |
nothing calls this directly
no test coverage detected