Commit graph

11 commits

Author SHA1 Message Date
Vasil Dimov
e286cd0d7b
net: flag relevant Sock methods with [[nodiscard]] 2021-04-13 17:26:20 +02:00
fanquake
3b0078f958
doc: fixup -Wdocumentation issues 2021-04-06 14:50:17 +08:00
Vasil Dimov
b5861100f8
net: add connect() and getsockopt() wrappers to Sock
Extend the `Sock` class with wrappers to `connect()` and `getsockopt()`.

This will make it possible to mock code which uses those.
2021-03-16 13:53:26 +01:00
Vasil Dimov
9b05c49ade
fuzz: implement unimplemented FuzzedSock methods
We want `Get()` to always return the same value, otherwise it will look
like the `FuzzedSock` implementation itself is broken. So assign
`m_socket` a random number in the `FuzzedSock` constructor.

There is nothing to fuzz about the `Get()` and `Release()` methods, so
use the ones from the base class `Sock`.

`Reset()` is just setting our socket to `INVALID_SOCKET`. We don't want
to use the base `Reset()` because it will close `m_socket` and given
that our `m_socket` is just a random number it may end up closing a real
opened file descriptor if it coincides with our random `m_socket`.
2021-03-16 13:53:25 +01:00
Vasil Dimov
80a5a8ea2b
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.
2021-03-16 11:00:57 +01:00
Vasil Dimov
5bac7e45e1
net: extend Sock with a method to check whether connected
This will be convenient in the I2P SAM implementation.
2021-03-01 17:36:17 +01:00
Vasil Dimov
42c779f503
net: extend Sock with methods for robust send & read until terminator
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.
2021-03-01 17:36:16 +01:00
Vasil Dimov
ea1845315a
net: extend Sock::Wait() to report a timeout
Previously `Sock::Wait()` would not have signaled to the caller whether
a timeout or one of the requested events occurred since that was not
needed by any of the callers.

Such functionality will be needed in the I2P implementation, thus extend
the `Sock::Wait()` method.
2021-03-01 13:22:18 +01:00
Vasil Dimov
34bcfab562
net: move the constant maxWait out of InterruptibleRecv()
Move `maxWait` out of `InterruptibleRecv()` and rename it to
`MAX_WAIT_FOR_IO` so that it can be reused by other code.
2021-03-01 13:22:17 +01:00
Vasil Dimov
ba9d73268f
net: add RAII socket and use it instead of bare SOCKET
Introduce a class to manage the lifetime of a socket - when the object
that contains the socket goes out of scope, the underlying socket will
be closed.

In addition, the new `Sock` class has a `Send()`, `Recv()` and `Wait()`
methods that can be overridden by unit tests to mock the socket
operations.

The `Wait()` method also hides the
`#ifdef USE_POLL poll() #else select() #endif` technique from higher
level code.
2021-02-10 13:30:08 +01:00
Vasil Dimov
dec9b5e850
net: move CloseSocket() from netbase to util/sock
Move `CloseSocket()` (and `NetworkErrorString()` which it uses) from
`netbase.{h,cpp}` to newly added `src/util/sock.{h,cpp}`.

This is necessary in order to use `CloseSocket()` from a newly
introduced Sock class (which will live in `src/util/sock.{h,cpp}`).
`sock.{h,cpp}` cannot depend on netbase because netbase will depend
on it.
2021-02-10 13:30:08 +01:00