diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2021-04-13 14:37:16 +0200 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2022-06-20 14:51:18 +0200 |
commit | 748dbcd9f29dbe4110da8a06f08e3eefa95f5321 (patch) | |
tree | 34a5c7076f0f18cbf5681cd6c59f624ff8cd31e5 /src/util | |
parent | 8e7eeb5971444c0c93e9a89bbdcc3a51a19e09e9 (diff) | |
download | bitcoin-748dbcd9f29dbe4110da8a06f08e3eefa95f5321.tar.xz |
net: add new method Sock::GetSockName() that wraps getsockname()
This will help to increase `Sock` usage and make more code mockable.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/sock.cpp | 5 | ||||
-rw-r--r-- | src/util/sock.h | 7 |
2 files changed, 12 insertions, 0 deletions
diff --git a/src/util/sock.cpp b/src/util/sock.cpp index 7d5069423a..b4c0aa4205 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -111,6 +111,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 diff --git a/src/util/sock.h b/src/util/sock.h index 3245820995..96d0b3b56b 100644 --- a/src/util/sock.h +++ b/src/util/sock.h @@ -126,6 +126,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; /** |