HomePhabricator

net: extend Sock with methods for robust send & read until terminator

Description

net: extend Sock with methods for robust send & read until terminator

Summary:

Introduce two high level, convenience methods in the Sock class:

  • SendComplete(): keep trying to send the specified data until either successfully sent all of it, timeout or interrupted.
  • RecvUntilTerminator(): read until a terminator is encountered (never after it), timeout or interrupted.

These will be convenient in the I2P SAM implementation.

SendComplete() can also be used in the SOCKS5 implementation instead of calling send() directly.

i2p: limit the size of incoming messages

Put a limit on the amount of data Sock::RecvUntilTerminator() can read
if no terminator is received.

In the case of I2P this avoids a runaway (or malicious) I2P proxy
sending us tons of data without a terminator before a timeout is
triggered.

test: add a test to ensure RecvUntilTerminator() limit works

This is a backport of core#20685 [12/20] and core#21407 (partial)
https://github.com/bitcoin/bitcoin/pull/20685/commits/42c779f503eb8437b6232773a4a2472306cc9f3d
https://github.com/bitcoin/bitcoin/pull/21407/commits/80a5a8ea2b7ad512c74c29df5b504e9be6cf23a0
https://github.com/bitcoin/bitcoin/pull/21407/commits/7059e6d82275b44efc41675ee10760145b6c1073

Note: the backport of core#21047 is missing the changes to i2p.{cpp|h} that will be included in a following commit.

Depends on D11022

Test Plan:
With clang-tidy
ninja && ninja-check

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11023

Details

Provenance
Vasil Dimov <vd@FreeBSD.org>Authored on Dec 4 2020, 14:11
PiRKCommitted on Feb 11 2022, 08:06
jasonbcoxPushed on Feb 11 2022, 08:08
Parents
rSTAGINGc1e9bd74019c: net: extend Sock::Wait() to report a timeout
Branches
Unknown
Tags
Unknown

Event Timeline