Bitcoin Core  24.99.0
P2P Digital Currency
sock.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_UTIL_SOCK_H
6 #define BITCOIN_UTIL_SOCK_H
7 
8 #include <compat/compat.h>
9 #include <util/threadinterrupt.h>
10 #include <util/time.h>
11 
12 #include <chrono>
13 #include <memory>
14 #include <string>
15 #include <unordered_map>
16 
21 static constexpr auto MAX_WAIT_FOR_IO = 1s;
22 
27 class Sock
28 {
29 public:
33  Sock();
34 
38  explicit Sock(SOCKET s);
39 
43  Sock(const Sock&) = delete;
44 
48  Sock(Sock&& other);
49 
53  virtual ~Sock();
54 
58  Sock& operator=(const Sock&) = delete;
59 
63  virtual Sock& operator=(Sock&& other);
64 
69  [[nodiscard]] virtual SOCKET Get() const;
70 
75  [[nodiscard]] virtual ssize_t Send(const void* data, size_t len, int flags) const;
76 
81  [[nodiscard]] virtual ssize_t Recv(void* buf, size_t len, int flags) const;
82 
87  [[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
88 
93  [[nodiscard]] virtual int Bind(const sockaddr* addr, socklen_t addr_len) const;
94 
99  [[nodiscard]] virtual int Listen(int backlog) const;
100 
107  [[nodiscard]] virtual std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const;
108 
114  [[nodiscard]] virtual int GetSockOpt(int level,
115  int opt_name,
116  void* opt_val,
117  socklen_t* opt_len) const;
118 
124  [[nodiscard]] virtual int SetSockOpt(int level,
125  int opt_name,
126  const void* opt_val,
127  socklen_t opt_len) const;
128 
134  [[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const;
135 
140  [[nodiscard]] virtual bool SetNonBlocking() const;
141 
146  [[nodiscard]] virtual bool IsSelectable() const;
147 
148  using Event = uint8_t;
149 
153  static constexpr Event RECV = 0b001;
154 
158  static constexpr Event SEND = 0b010;
159 
164  static constexpr Event ERR = 0b100;
165 
176  [[nodiscard]] virtual bool Wait(std::chrono::milliseconds timeout,
177  Event requested,
178  Event* occurred = nullptr) const;
179 
183  struct Events {
184  explicit Events(Event req) : requested{req} {}
187  };
188 
190  size_t operator()(const std::shared_ptr<const Sock>& s) const
191  {
192  return s ? s->m_socket : std::numeric_limits<SOCKET>::max();
193  }
194  };
195 
197  bool operator()(const std::shared_ptr<const Sock>& lhs,
198  const std::shared_ptr<const Sock>& rhs) const
199  {
200  if (lhs && rhs) {
201  return lhs->m_socket == rhs->m_socket;
202  }
203  if (!lhs && !rhs) {
204  return true;
205  }
206  return false;
207  }
208  };
209 
218  using EventsPerSock = std::unordered_map<std::shared_ptr<const Sock>, Events, HashSharedPtrSock, EqualSharedPtrSock>;
219 
228  [[nodiscard]] virtual bool WaitMany(std::chrono::milliseconds timeout,
229  EventsPerSock& events_per_sock) const;
230 
231  /* Higher level, convenience, methods. These may throw. */
232 
241  virtual void SendComplete(const std::string& data,
242  std::chrono::milliseconds timeout,
243  CThreadInterrupt& interrupt) const;
244 
257  [[nodiscard]] virtual std::string RecvUntilTerminator(uint8_t terminator,
258  std::chrono::milliseconds timeout,
259  CThreadInterrupt& interrupt,
260  size_t max_data) const;
261 
267  [[nodiscard]] virtual bool IsConnected(std::string& errmsg) const;
268 
269 protected:
274 
275 private:
279  void Close();
280 };
281 
283 std::string NetworkErrorString(int err);
284 
285 #endif // BITCOIN_UTIL_SOCK_H
int flags
Definition: bitcoin-tx.cpp:525
RAII helper class that manages a socket.
Definition: sock.h:28
virtual std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const
accept(2) wrapper.
Definition: sock.cpp:79
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:54
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
Definition: sock.h:158
SOCKET m_socket
Contained socket.
Definition: sock.h:273
virtual void SendComplete(const std::string &data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Send the given data, retrying on transient errors.
Definition: sock.cpp:254
virtual int Bind(const sockaddr *addr, socklen_t addr_len) const
bind(2) wrapper.
Definition: sock.cpp:69
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
Definition: sock.cpp:148
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Definition: sock.cpp:42
uint8_t Event
Definition: sock.h:148
Sock(const Sock &)=delete
Copy constructor, disabled because closing the same socket twice is undesirable.
virtual int GetSockName(sockaddr *name, socklen_t *name_len) const
getsockname(2) wrapper.
Definition: sock.cpp:115
void Close()
Close m_socket if it is not INVALID_SOCKET.
Definition: sock.cpp:403
Sock()
Default constructor, creates an empty object that does nothing when destroyed.
Definition: sock.cpp:32
virtual bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const
Same as Wait(), but wait on many sockets within the same timeout.
Definition: sock.cpp:168
static constexpr Event ERR
Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has ...
Definition: sock.h:164
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
Definition: sock.cpp:378
virtual int SetSockOpt(int level, int opt_name, const void *opt_val, socklen_t opt_len) const
setsockopt(2) wrapper.
Definition: sock.cpp:110
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:153
virtual SOCKET Get() const
Get the value of the contained socket.
Definition: sock.cpp:52
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
Definition: sock.cpp:105
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
Definition: sock.cpp:64
Sock & operator=(const Sock &)=delete
Copy assignment operator, disabled because closing the same socket twice is undesirable.
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:59
virtual std::string RecvUntilTerminator(uint8_t terminator, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt, size_t max_data) const
Read from socket until a terminator character is encountered.
Definition: sock.cpp:295
virtual int Listen(int backlog) const
listen(2) wrapper.
Definition: sock.cpp:74
virtual bool SetNonBlocking() const
Set the non-blocking option on the socket.
Definition: sock.cpp:120
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
Definition: sock.h:218
virtual bool IsSelectable() const
Check if the underlying socket can be used for select(2) (or the Wait() method).
Definition: sock.cpp:139
unsigned int SOCKET
Definition: compat.h:44
const char * name
Definition: rest.cpp:46
static constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.
Definition: sock.h:21
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:436
bool operator()(const std::shared_ptr< const Sock > &lhs, const std::shared_ptr< const Sock > &rhs) const
Definition: sock.h:197
Auxiliary requested/occurred events to wait for in WaitMany().
Definition: sock.h:183
Event requested
Definition: sock.h:185
Events(Event req)
Definition: sock.h:184
Event occurred
Definition: sock.h:186
size_t operator()(const std::shared_ptr< const Sock > &s) const
Definition: sock.h:190