aboutsummaryrefslogtreecommitdiff
path: root/src/util/sock.cpp
AgeCommit message (Collapse)Author
2023-10-31sock: change Sock::SendComplete() to take SpanVasil Dimov
This would make it easier to pass other than `std::string` types, to be used in the `Socks5()` function.
2023-08-25net: remove Sock default constructor, it's not necessaryVasil Dimov
2023-08-25net: remove now unnecessary Sock::Get()Vasil Dimov
`Sock::Get()` was used only in `sock.{cpp,h}`. Remove it and access `Sock::m_socket` directly. Unit tests that used `Get()` to test for equality still verify that the behavior is correct by using the added `operator==()`.
2023-07-20Merge bitcoin/bitcoin#26654: util: Show descriptive error messages when ↵fanquake
FileCommit fails 5408a55fc87350baeae3a32f1003f956d5533a79 Consolidate Win32-specific error formatting (John Moffett) c95a4432d7c9d0e5829cd802908700ba2e2c25dc Show descriptive error messages when FileCommit fails (John Moffett) Pull request description: Only raw [`errno`](https://en.cppreference.com/w/cpp/error/errno) int values are logged if `FileCommit` fails. These values are implementation-specific, so it makes it harder to debug based on user reports. For instance, https://github.com/bitcoin/bitcoin/issues/26455#issue-1436654238 and [another](https://bitcointalk.org/index.php?topic=5182526.0#:~:text=FileCommit%3A%20FlushFileBuffers%20failed%3A%205). Instead, use `SysErrorString` (or the refactored Windows equivalent `Win32ErrorString`) to display both the raw int value and the descriptive message. All other instances in the code I could find where `errno` or (Windows-only) `GetLastError()`/`WSAGetLastError()` are logged use the full descriptive string. For example: https://github.com/bitcoin/bitcoin/blob/1b680948d43b1d39645b9d839a6fa7c6c1786b51/src/util/sock.cpp#L390 https://github.com/bitcoin/bitcoin/blob/1b680948d43b1d39645b9d839a6fa7c6c1786b51/src/util/sock.cpp#L272 https://github.com/bitcoin/bitcoin/blob/7e1007a3c6c9a921c2b60919b84a60eaabfe1c5d/src/netbase.cpp#L515-L516 https://github.com/bitcoin/bitcoin/blob/8ccab65f289e3cce392cbe01d5fc0e7437f51f1e/src/init.cpp#L164 I refactored the Windows formatting code to put it in `syserror.cpp`, as it's applicable to all Win32 API system errors, not just networking errors. To be clear, the Windows API functions `WSAGetLastError()` and `GetLastError()` are currently [equivalent](https://stackoverflow.com/questions/15586224/is-wsagetlasterror-just-an-alias-for-getlasterror). ACKs for top commit: MarcoFalke: lgtm ACK 5408a55fc87350baeae3a32f1003f956d5533a79 💡 Tree-SHA512: 3921cbac98bd9edaf84d3dd7a43896c7921f144c8ca2cde9bc96d5fb05281f7c55e7cc99db8debf6203b5f916f053025e4fa741f51458fe2c53bb57b0a781027
2023-06-30Consolidate Win32-specific error formattingJohn Moffett
GetErrorReason()'s Win32 implementation does the same thing as Win32ErrorString(int err) from syserror.cpp, so call the latter. Also remove now-unnecessary headers from sock.cpp and less verbose handling of #ifdefs.
2023-06-30Show descriptive error messages when FileCommit failsJohn Moffett
Only raw errno codes are logged if FileCommit fails. These are implementation-specific, so it makes it harder to debug based on user reports. Instead, use SysErrorString to display both the raw int value and the descriptive message.
2023-05-20refactor: Move system from util to common libraryTheCharlatan
Since the kernel library no longer depends on the system file, move it to the common library instead in accordance to the diagram in doc/design/libraries.md.
2022-12-24scripted-diff: Bump copyright headersHennadii Stepanov
-BEGIN VERIFY SCRIPT- ./contrib/devtools/copyright_header.py update ./ -END VERIFY SCRIPT- Commits of previous years: - 2021: f47dda2c58b5d8d623e0e7ff4e74bc352dfa83d7 - 2020: fa0074e2d82928016a43ca408717154a1c70a4db - 2019: aaaaad6ac95b402fe18d019d67897ced6b316ee0
2022-11-01util: move threadinterrupt into utilfanquake
2022-07-20net: convert standalone SetSocketNonBlocking() to Sock::SetNonBlocking()Vasil Dimov
This further encapsulates syscalls inside the `Sock` class. Co-authored-by: practicalswift <practicalswift@users.noreply.github.com>
2022-07-20moveonly: move SetSocketNonBlocking() from netbase to util/sockVasil Dimov
To be converted to a method of the `Sock` class.
2022-07-20net: convert standalone IsSelectableSocket() to Sock::IsSelectable()Vasil Dimov
This makes the callers mockable.
2022-07-20moveonly: move IsSelectableSocket() from compat.h to sock.{h,cpp}Vasil Dimov
To be converted to a method of the `Sock` class.
2022-07-20refactor: move compat.h into compat/fanquake
2022-06-28Merge bitcoin/bitcoin#24378: refactor: make bind() and listen() ↵laanwj
mockable/testable b2733ab6a85b234a88b83bdc77a0d043e18385b3 net: add new method Sock::Listen() that wraps listen() (Vasil Dimov) 3ad7de225efce3e76530f56bee8a8f7a75ea0f3c net: add new method Sock::Bind() that wraps bind() (Vasil Dimov) Pull request description: _This is a piece of #21878, chopped off to ease review._ Add new methods `Sock::Bind()` and `Sock::Listen()` that wrap `bind()` and `listen()`. This will help to increase `Sock` usage and make more code mockable. ACKs for top commit: pk-b2: ACK b2733ab6a85b234a88b83bdc77a0d043e18385b3 laanwj: Code review ACK b2733ab6a85b234a88b83bdc77a0d043e18385b3 Tree-SHA512: c6e737606703e2106fe60cc000cfbbae3a7f43deadb25f70531e2cac0457e0b0581440279d14c76c492eb85c12af4adde52c30baf74542c41597e419817488e8
2022-06-28Merge bitcoin/bitcoin#25426: net: add new method Sock::GetSockName() that ↵laanwj
wraps getsockname() and use it in GetBindAddress() a8d6abba5ec4ae2a3375e9be0b739f298899eca2 net: change GetBindAddress() to take Sock argument (Vasil Dimov) 748dbcd9f29dbe4110da8a06f08e3eefa95f5321 net: add new method Sock::GetSockName() that wraps getsockname() (Vasil Dimov) Pull request description: _This is a piece of #21878, chopped off to ease review._ Wrap the syscall `getsockname()` in `Sock::GetSockName()` and change `GetBindAddress()` to take a `Sock` argument so that it can use the wrapper. This further encapsulates syscalls inside the `Sock` class and makes the callers mockable. ACKs for top commit: laanwj: Code review ACK a8d6abba5ec4ae2a3375e9be0b739f298899eca2 Tree-SHA512: 3a73463258c0057487fb3fd67215816b03a1c5160f45e45930eaeef86bb3611ec385794cdb08339aa074feba8ad67cd2bfd3836f6cbd40834e15d933214a05dc
2022-06-22net: rename Sock::Reset() to Sock::Close() and make it privateVasil Dimov
Outside of `Sock`, `Sock::Reset()` was used in just one place (in `i2p.cpp`) which can use the assignment operator instead. This simplifies the public `Sock` API by having one method less.
2022-06-20net: remove CloseSocket()Vasil Dimov
Do the closing in `Sock::Reset()` and remove the standalone `CloseSocket()`. This reduces the exposure of low-level sockets (i.e. integer file descriptors) outside of the `Sock` class.
2022-06-20net: remove now unused Sock::Release()Vasil Dimov
2022-06-20net: add new method Sock::GetSockName() that wraps getsockname()Vasil Dimov
This will help to increase `Sock` usage and make more code mockable.
2022-06-09net: introduce Sock::WaitMany()Vasil Dimov
It allows waiting concurrently on more than one socket. Being a `virtual` `Sock` method it can be overriden by tests. Will be used to replace `CConnman::SocketEvents()`.
2022-06-09net: also wait for exceptional events in Sock::Wait()Vasil Dimov
This mimics closely `CConnman::SocketEvents()` and the underlying `poll(2)`.
2022-05-18net: add new method Sock::Listen() that wraps listen()Vasil Dimov
This will help to increase `Sock` usage and make more code mockable.
2022-05-18net: add new method Sock::Bind() that wraps bind()Vasil Dimov
This will help to increase `Sock` usage and make more code mockable.
2022-04-28util: Replace non-threadsafe strerrorlaanwj
Some uses of non-threadsafe `strerror` have snuck into the code since they were removed in #4152. Add a wrapper `SysErrorString` for thread-safe strerror alternatives and replace all uses of `strerror` with this.
2022-04-15net: add new method Sock::SetSockOpt() that wraps setsockopt()Vasil Dimov
This will help to increase `Sock` usage and make more code mockable.
2021-12-01net: add new method Sock::Accept() that wraps accept()Vasil Dimov
This will help to increase `Sock` usage and make more code mockable.
2021-09-11Cleanup headers after #20788Hennadii Stepanov
2021-04-13net: flag relevant Sock methods with [[nodiscard]]Vasil Dimov
2021-03-16net: add connect() and getsockopt() wrappers to SockVasil Dimov
Extend the `Sock` class with wrappers to `connect()` and `getsockopt()`. This will make it possible to mock code which uses those.
2021-03-16i2p: limit the size of incoming messagesVasil Dimov
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-01net: extend Sock with a method to check whether connectedVasil Dimov
This will be convenient in the I2P SAM implementation.
2021-03-01net: extend Sock with methods for robust send & read until terminatorVasil Dimov
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-01net: extend Sock::Wait() to report a timeoutVasil Dimov
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-02-10net: add RAII socket and use it instead of bare SOCKETVasil Dimov
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-10net: move CloseSocket() from netbase to util/sockVasil Dimov
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.