Merge bitcoin/bitcoin#25426: net: add new method Sock::GetSockName() that wraps getsockname() and use it in GetBindAddress()

a8d6abba5e net: change GetBindAddress() to take Sock argument (Vasil Dimov)
748dbcd9f2 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 a8d6abba5e

Tree-SHA512: 3a73463258c0057487fb3fd67215816b03a1c5160f45e45930eaeef86bb3611ec385794cdb08339aa074feba8ad67cd2bfd3836f6cbd40834e15d933214a05dc
This commit is contained in:
laanwj
2022-06-28 13:39:51 +02:00
6 changed files with 39 additions and 5 deletions

View File

@@ -102,6 +102,11 @@ int Sock::SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt
return setsockopt(m_socket, level, opt_name, static_cast<const char*>(opt_val), opt_len);
}
int Sock::GetSockName(sockaddr* name, socklen_t* name_len) const
{
return getsockname(m_socket, name, name_len);
}
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
{
// We need a `shared_ptr` owning `this` for `WaitMany()`, but don't want

View File

@@ -114,6 +114,13 @@ public:
const void* opt_val,
socklen_t opt_len) const;
/**
* getsockname(2) wrapper. Equivalent to
* `getsockname(this->Get(), name, name_len)`. Code that uses this
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
*/
[[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const;
using Event = uint8_t;
/**