dodo  0.0.1
A C++ library to create containerized Linux services
basesocket.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file basesocket.cpp
20  * Implements the dodo::network::BaseSocket class.
21  */
22 
23 #include <common/logger.hpp>
24 #include "network/basesocket.hpp"
25 #include <netinet/tcp.h>
26 #include <unistd.h>
27 #include <cmath>
28 
29 namespace dodo::network {
30 
32  socket_ = -1;
33  }
34 
35  BaseSocket::BaseSocket( int socket ) {
36  socket_ = socket;
37  }
38 
39  BaseSocket::BaseSocket( bool blocking, SocketParams params ) {
40  socket_ = socket( params.getAddressFamily(), params.getSocketType(), params.getProtocol() );
41  if ( socket_ == -1 ) throw_SystemExceptionObject( "socket allocation failed", errno, this );
42  setBlocking( blocking );
43  if ( params.getSocketType() == SocketParams::stSTREAM ) setTCPNoDelay( true );
44  }
45 
46  std::string BaseSocket::debugDetail() const {
47  std::stringstream ss;
48  ss << "socketfd=" << socket_;
49  return ss.str();
50  }
51 
53  auto rc = ::connect( socket_, (const sockaddr*)address.getAddress(), sizeof(sockaddr_storage) );
54  if ( rc < 0 ) {
55  switch ( errno ) {
65  return errno;
66  default: throw_SystemExceptionObject( "socket connect failed", errno, this );
67  };
68  } else return common::SystemError::ecOK;
69  }
70 
72  if ( socket_ != -1 ) {
73  auto rc = ::close( socket_ );
74  socket_ = -1;
75  if ( rc == -1 ) throw_SystemExceptionObject( "socket close failed", errno, this );
76  }
77  }
78 
80  socket_ = socket;
81  return *this;
82  }
83 
85  socket_ = socket.socket_;
86  return *this;
87  }
88 
89  common::SystemError BaseSocket::listen( const Address &address, int backlog ) {
90  common::SystemError error = bind( address );
91  if ( error == common::SystemError::ecOK ) {
92  int rc = ::listen( socket_, backlog );
93  if ( rc < 0 ) {
94  switch ( errno ) {
96  return errno;
97  }
98  throw_SystemExceptionObject( "Socket::listen failed on " << address.asString(), errno, this );
99  } else return common::SystemError::ecOK;
100  } else return error;
101  }
102 
104  int rc = ::bind( socket_, (const sockaddr*)&(address.addr_), sizeof(address.addr_) );
105  if ( rc < 0 ) {
106  switch ( errno ) {
109  return errno;
110  }
111  throw_SystemExceptionObject( "Socket::bind failed on address " << address.asString(), errno, this );
112  } else return common::SystemError::ecOK;
113  }
114 
115  void BaseSocket::setTCPNoDelay( bool set ) {
116  int value = set?1:0;
117  setsockopt( socket_, SOL_TCP, TCP_NODELAY, &value, sizeof(value) );
118  }
119 
120  void BaseSocket::setTCPKeepAlive( bool enable ) {
121  int value = enable?1:0;
122  setsockopt( socket_, SOL_SOCKET, SO_KEEPALIVE, &value, sizeof(value) );
123  }
124 
125  bool BaseSocket::getBlocking() const {
126  int flags = fcntl( socket_, F_GETFL, 0 );
127  if ( flags == -1 ) throw_SystemExceptionObject( "fcntl failed", errno, this );
128  return ! (flags & O_NONBLOCK);
129  }
130 
131  void BaseSocket::setBlocking( bool blocking ) {
132  if ( !isValid() ) throw_Exception( "setBlocking on invalid socket" );
133  int flags = fcntl( socket_, F_GETFL, 0 );
134  if ( flags == -1 ) throw_SystemExceptionObject( "fcntl failed", errno, this );
135  if ( blocking ) {
136  if ( flags & O_NONBLOCK ) {
137  int rc = fcntl( socket_, F_SETFL, flags ^ O_NONBLOCK );
138  if ( rc < 0 ) throw_SystemExceptionObject( "setsockopt failed", errno, this );
139  }
140  } else {
141  if ( !(flags & O_NONBLOCK) ) {
142  int rc = fcntl( socket_, F_SETFL, flags | O_NONBLOCK );
143  if ( rc < 0 ) throw_SystemExceptionObject( "setsockopt failed", errno, this );
144  }
145  }
146  }
147 
149  if ( !isValid() ) throw_Exception( "setReUseAddress on invalid socket" );
150  int opt_enable = 1;
151  auto rc = setsockopt( socket_,
152  SOL_SOCKET,
153  SO_REUSEADDR,
154  (char *)&opt_enable,
155  sizeof(opt_enable) );
156  if ( rc < 0 ) throw_SystemExceptionObject( "setsockopt SO_REUSEADDR failed", rc, this );
157  }
158 
160  int opt_enable = 1;
161  auto rc = setsockopt( socket_,
162  SOL_SOCKET,
163  SO_REUSEPORT,
164  (char *)&opt_enable,
165  sizeof(opt_enable) );
166  if ( rc < 0 ) throw_SystemExceptionObject( "setsockopt SO_REUSEPORT failed", rc, this );
167  }
168 
169  socklen_t BaseSocket::getSendBufSize() const {
170  socklen_t optvar = 0;
171  socklen_t optlen = sizeof(optvar);
172  auto rc = getsockopt( socket_, SOL_SOCKET, SO_SNDBUF, &optvar, &optlen );
173  if ( rc == -1 ) throw_SystemExceptionObject( "getSendBufSize failed", errno, this );
174  return optvar;
175  }
176 
177  socklen_t BaseSocket::getReceiveBufSize() const {
178  socklen_t optvar = 0;
179  socklen_t optlen = sizeof(optvar);
180  auto rc = getsockopt( socket_, SOL_SOCKET, SO_RCVBUF, &optvar, &optlen );
181  if ( rc == -1 ) throw_SystemExceptionObject( "getReceiveBufSize failed", errno, this );
182  return optvar;
183  }
184 
185  int BaseSocket::getTTL() const {
186  int optvar = 0;
187  socklen_t optlen = sizeof(optvar);
188  int rc = -1;
189  if ( getAddressFamily() == SocketParams::afINET ) {
190  rc = getsockopt( socket_, SOL_IP, IP_TTL, &optvar, &optlen );
191  }
192  else if ( getAddressFamily() == SocketParams::afINET6 ) {
193  rc = getsockopt( socket_, SOL_IP, IP_TTL, &optvar, &optlen );
194  }
195  if ( rc == -1 ) throw_SystemExceptionObject( "getTTL failed", errno, this );
196  return optvar;
197  }
198 
199  void BaseSocket::setSendBufSize( socklen_t size ) {
200  auto rc = setsockopt( socket_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size) );
201  if ( rc == -1 ) throw_SystemExceptionObject( "setSendBufSize failed", errno, this );
202  }
203 
204  void BaseSocket::setReceiveBufSize( socklen_t size ) {
205  int rc = setsockopt( socket_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size) );
206  if ( rc == -1 ) throw_SystemExceptionObject( "setReceiveBufSize failed", errno, this );
207  }
208 
209  void BaseSocket::setReceiveTimeout( double sec ) {
210  double frac, whole;
211  frac = modf ( sec , &whole );
212  struct timeval tv_out;
213  tv_out.tv_sec = (time_t)whole;
214  tv_out.tv_usec = (time_t)(frac * 1.0E6);
215  int rc = setsockopt( socket_, SOL_SOCKET, SO_RCVTIMEO, &tv_out, sizeof(tv_out) );
216  if ( rc == -1 ) throw_SystemExceptionObject( "setReceiveTimeout failed", errno, this );
217  }
218 
219  void BaseSocket::setSendTimeout( double sec ) {
220  double frac, whole;
221  frac = modf ( sec , &whole );
222  struct timeval tv_out;
223  tv_out.tv_sec = (time_t)whole;
224  tv_out.tv_usec = (time_t)(frac * 1.0E6);
225  int rc = setsockopt( socket_, SOL_SOCKET, SO_SNDTIMEO, &tv_out, sizeof(tv_out) );
226  if ( rc == -1 ) throw_SystemExceptionObject( "setSendTimeout failed", errno, this );
227  }
228 
229  void BaseSocket::setTTL( int ttl ) {
230  int rc = -1;
231  rc = setsockopt( socket_, SOL_IP, IP_TTL, &ttl, sizeof(ttl) );
232  if ( errno == 92 ) rc = setsockopt( socket_, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl) );
233  if ( rc == -1 ) throw_SystemExceptionObject( "setTTL failed", errno, this );
234  }
235 
237  sockaddr_storage addr;
238  socklen_t sz = sizeof(addr);
239  int rc = getsockname( socket_, (sockaddr*)&addr, &sz );
240  if ( rc < 0 ) addr.ss_family = AF_UNSPEC;
241  return Address(addr);
242  }
243 
245  socklen_t optvar = 0;
246  socklen_t optlen = sizeof(optvar);
247  int rc = getsockopt( socket_, SOL_SOCKET, SO_DOMAIN, &optvar, &optlen );
248  if ( rc == -1 ) throw_SystemExceptionObject( "getAddressFamily failed", errno, this );
249  return (SocketParams::AddressFamily)optvar;
250  }
251 
253  socklen_t optvar = 0;
254  socklen_t optlen = sizeof(optvar);
255  int rc = getsockopt( socket_, SOL_SOCKET, SO_TYPE, &optvar, &optlen );
256  if ( rc == -1 ) throw_SystemExceptionObject( "getSocketType failed", errno, this );
257  return (SocketParams::SocketType)optvar;
258  }
259 
261  socklen_t optvar = 0;
262  socklen_t optlen = sizeof(optvar);
263  int rc = getsockopt( socket_, SOL_SOCKET, SO_PROTOCOL, &optvar, &optlen );
264  if ( rc == -1 ) throw_SystemExceptionObject( "getProtocolNumber failed", errno, this );
265  return (SocketParams::ProtocolNumber)optvar;
266  }
267 
270  return p;
271  }
272 
274  sockaddr_storage addr;
275  socklen_t sz = sizeof(addr);
276  int rc = getpeername( socket_, (sockaddr*)&addr, &sz );
277  if ( rc < 0 ) addr.ss_family = AF_UNSPEC;
278  return Address(addr);
279  }
280 
281  common::SystemError BaseSocket::sendUInt8( uint8_t value, bool more ) {
282  uint8_t nwbo = value;
283  return send( &nwbo, sizeof(nwbo) );
284  }
285 
287  uint8_t t = 0;
288  ssize_t request = sizeof(t);
289  ssize_t received = 0;
290  ssize_t total = 0;
291  common::SystemError error;
292  do {
293  error = receive( &t, request, received );
294  total += received;
295  } while ( error == common::SystemError::ecOK && total < request );
296  if ( error == common::SystemError::ecOK ) {
297  value = t;
298  } else value = 0;
299  return error;
300  }
301 
302  common::SystemError BaseSocket::sendUInt16( uint16_t value, bool more ) {
303  uint16_t nwbo = htons( value );
304  return send( &nwbo, sizeof(nwbo) );
305  }
306 
308  uint16_t t = 0;
309  ssize_t request = sizeof(t);
310  ssize_t received = 0;
311  ssize_t total = 0;
312  common::SystemError error;
313  do {
314  error = receive( &t, request, received );
315  total += received;
316  } while ( error == common::SystemError::ecOK && total < request );
317  if ( error == common::SystemError::ecOK ) {
318  value = ntohs(t);
319  } else value = 0;
320  return error;
321  }
322 
323  common::SystemError BaseSocket::sendInt8( int8_t value, bool more ) {
324  int8_t nwbo = value;
325  return send( &nwbo, sizeof(nwbo), more );
326  }
327 
329  int8_t t = 0;
330  ssize_t request = sizeof(t);
331  ssize_t received = 0;
332  ssize_t total = 0;
333  common::SystemError error;
334  do {
335  error = receive( &t, request, received );
336  total += received;
337  } while ( error == common::SystemError::ecOK && total < request );
338  if ( error == common::SystemError::ecOK ) {
339  value = t;
340  } else value = 0;
341  return error;
342  }
343 
344  common::SystemError BaseSocket::sendInt16( int16_t value, bool more ) {
345  int16_t nwbo = htons( value );
346  return send( &nwbo, sizeof(nwbo), more );
347  }
348 
350  int16_t t = 0;
351  ssize_t request = sizeof(t);
352  ssize_t received = 0;
353  ssize_t total = 0;
354  common::SystemError error;
355  do {
356  error = receive( &t, request, received );
357  total += received;
358  } while ( error == common::SystemError::ecOK && total < request );
359  if ( error == common::SystemError::ecOK ) {
360  value = ntohs(t);
361  } else value = 0;
362  return error;
363  }
364 
365  common::SystemError BaseSocket::sendUInt32( uint32_t value, bool more ) {
366  uint32_t nwbo = htonl( value );
367  return send( &nwbo, sizeof(nwbo), more );
368  }
369 
371  uint32_t t = 0;
372  ssize_t request = sizeof(t);
373  ssize_t received = 0;
374  ssize_t total = 0;
375  common::SystemError error;
376  do {
377  error = receive( &t, request, received );
378  total += received;
379  } while ( error == common::SystemError::ecOK && total < request );
380  if ( error == common::SystemError::ecOK ) {
381  value = ntohl(t);
382  } else value = 0;
383  return error;
384  }
385 
386  common::SystemError BaseSocket::sendInt32( int32_t value, bool more ) {
387  int32_t nwbo = htonl( value );
388  return send( &nwbo, sizeof(nwbo), more );
389  }
390 
392  int32_t t = 0;
393  ssize_t request = sizeof(t);
394  ssize_t received = 0;
395  ssize_t total = 0;
396  common::SystemError error;
397  do {
398  error = receive( &t, request, received );
399  total += received;
400  } while ( error == common::SystemError::ecOK && total < request );
401  if ( error == common::SystemError::ecOK ) {
402  value = ntohl(t);
403  } else value = 0;
404  return error;
405  }
406 
407  common::SystemError BaseSocket::sendUInt64( uint64_t value, bool more ) {
408  uint64_t nwbo = htobe64( value );
409  return send( &nwbo, sizeof(nwbo), more );
410  }
411 
413  uint64_t t = 0;
414  ssize_t request = sizeof(t);
415  ssize_t received = 0;
416  ssize_t total = 0;
417  common::SystemError error;
418  do {
419  error = receive( &t, request, received );
420  total += received;
421  } while ( error == common::SystemError::ecOK && total < request );
422  if ( error == common::SystemError::ecOK ) {
423  value = be64toh( t );
424  } else value = 0;
425  return error;
426  }
427 
428  common::SystemError BaseSocket::sendInt64( int64_t value, bool more ) {
429  int64_t nwbo = htobe64( value );
430  return send( &nwbo, sizeof(nwbo), more );
431  }
432 
434  int64_t t = 0;
435  ssize_t request = sizeof(t);
436  ssize_t received = 0;
437  ssize_t total = 0;
438  common::SystemError error;
439  do {
440  error = receive( &t, request, received );
441  total += received;
442  } while ( error == common::SystemError::ecOK && total < request );
443  if ( error == common::SystemError::ecOK ) {
444  value = be64toh( t );
445  } else value = 0;
446  return error;
447  }
448 
449  common::SystemError BaseSocket::sendString( const std::string &s, bool more ) {
450  if ( getBlocking() ) {
451  common::SystemError error;
452  error = sendUInt64( s.size(), true );
453  if ( error != common::SystemError::ecOK ) return error;
454  error = send( s.c_str(), s.size(), more );
455  return error;
456  } else throw_Exception( "sendString used on a non-blocking socket" );
457  }
458 
460  s = "";
461  common::SystemError error;
462  if ( getBlocking() ) {
463  uint64_t stringsize = 0;
464  error = receiveUInt64( stringsize );
465  if ( error == common::SystemError::ecOK ) {
466  char buf[getReceiveBufSize()+1];
467  ssize_t received = 0;
468  uint64_t total = 0;
469  do {
470  error = receive( buf, stringsize, received );
471  total += received;
472  buf[received] = 0;
473  s += buf;
474  } while ( error == common::SystemError::ecOK && total < stringsize );
475  return error;
476  } else return error;
477  } else throw_ExceptionObject( "receiveString used on a non-blocking socket", this );
478  }
479 
480  common::SystemError BaseSocket::sendLine( const std::string &s, bool more ) {
481  common::SystemError error;
482  std::stringstream ss;
483  ss << s << '\n';
484  error = send( ss.str().c_str(), ss.str().length(), more );
485  return error;
486  }
487 
489  char c = 0;
490  ssize_t received = 0;
491  std::stringstream ss;
492  common::SystemError error = receive( &c, 1, received );
493  while ( error == common::SystemError::ecOK && received == 1 && c != '\n' ) {
494  if ( c != '\r' ) ss << c;
495  error = receive( &c, 1, received );
496  }
497  if ( error == common::SystemError::ecOK ) s = ss.str(); else s = "";
498  return error;
499  }
500 
501 }
dodo::network::BaseSocket::setReceiveTimeout
void setReceiveTimeout(double sec)
Set the receive timeout - a receive will fail if there is no data received before the timeout expires...
Definition: basesocket.cpp:209
dodo::common::SystemError::ecEADDRINUSE
@ ecEADDRINUSE
98 Address already in use
Definition: systemerror.hpp:158
dodo::network::BaseSocket::sendInt8
common::SystemError sendInt8(int8_t value, bool more=false)
Send an int8_t.
Definition: basesocket.cpp:323
dodo::network::SocketParams::getAddressFamily
AddressFamily getAddressFamily() const
Get the AddressFamily.
Definition: socketparams.hpp:148
dodo::network::BaseSocket::setTCPNoDelay
void setTCPNoDelay(bool set)
Set TCP_NODELAY.
Definition: basesocket.cpp:115
dodo::network::BaseSocket::getPeerAddress
Address getPeerAddress() const
Get the peer (remote) address for this socket.
Definition: basesocket.cpp:273
dodo::network::BaseSocket::listen
common::SystemError listen(const Address &address, int backlog)
Sets up a listening socket on Address.
Definition: basesocket.cpp:89
dodo::network::BaseSocket::connect
virtual common::SystemError connect(const Address &address)
Connect to the address.
Definition: basesocket.cpp:52
throw_ExceptionObject
#define throw_ExceptionObject(what, thing)
Throws an Exception with DebugContext, passes FILE and LINE to constructor.
Definition: exception.hpp:181
dodo::common::SystemError::ecEALREADY
@ ecEALREADY
114 Operation already in progress
Definition: systemerror.hpp:174
dodo::network::BaseSocket::sendLine
common::SystemError sendLine(const std::string &s, bool more)
Sends the std::string terminated by a ' '.
Definition: basesocket.cpp:480
dodo::network::BaseSocket::receiveInt64
common::SystemError receiveInt64(int64_t &value)
receive an int64_t
Definition: basesocket.cpp:433
dodo::network::BaseSocket::setBlocking
void setBlocking(bool blocking)
Set the Socket blocking mode.
Definition: basesocket.cpp:131
dodo::network::Address::asString
std::string asString(bool withport=false) const
Return a string representation of this Address.
Definition: address.cpp:107
dodo::network::BaseSocket::getReceiveBufSize
virtual socklen_t getReceiveBufSize() const
Get the maximum buffer length for receive.
Definition: basesocket.cpp:177
dodo::common::SystemError::ecETIMEDOUT
@ ecETIMEDOUT
110 Connection timed out
Definition: systemerror.hpp:170
dodo::network::BaseSocket::close
virtual void close()
Closes the socket, causing the connection, if it exists, to be terminated.
Definition: basesocket.cpp:71
dodo::network::BaseSocket::setReUseAddress
void setReUseAddress()
Enable the socket to re-use an address when listen/bind is called.
Definition: basesocket.cpp:148
dodo::network::BaseSocket::sendInt16
common::SystemError sendInt16(int16_t value, bool more=false)
Send an int16_t.
Definition: basesocket.cpp:344
dodo::common::SystemError::ecECONNREFUSED
@ ecECONNREFUSED
111 Connection refused
Definition: systemerror.hpp:171
dodo::network::SocketParams::AddressFamily
AddressFamily
Addres family type.
Definition: socketparams.hpp:41
dodo::network::Address
Generic network Address, supporting ipv4 and ipv6 transparently.
Definition: address.hpp:90
dodo::network::BaseSocket::setTTL
void setTTL(int ttl)
Set the Socket TTL (Time to Live).
Definition: basesocket.cpp:229
dodo::network::BaseSocket::getSocketType
SocketParams::SocketType getSocketType() const
Get the SocketParams::SocketType of the socket.
Definition: basesocket.cpp:252
dodo::network::BaseSocket::sendUInt8
common::SystemError sendUInt8(uint8_t value, bool more=false)
Send an uint8_t.
Definition: basesocket.cpp:281
dodo::network::BaseSocket::receiveInt8
common::SystemError receiveInt8(int8_t &value)
receive an int8_t
Definition: basesocket.cpp:328
dodo::network::BaseSocket::receiveLine
common::SystemError receiveLine(std::string &s)
receive a stream of characters until a ' ' is read (consumed from the socket read buffer but not adde...
Definition: basesocket.cpp:488
dodo::network::BaseSocket::sendUInt32
common::SystemError sendUInt32(uint32_t value, bool more=false)
Send an uint32_t.
Definition: basesocket.cpp:365
dodo::network::BaseSocket::sendInt64
common::SystemError sendInt64(int64_t value, bool more=false)
Send an int64_t.
Definition: basesocket.cpp:428
dodo::network::BaseSocket::sendInt32
common::SystemError sendInt32(int32_t value, bool more=false)
Send an int32_t.
Definition: basesocket.cpp:386
dodo::common::SystemError::ecEINPROGRESS
@ ecEINPROGRESS
115 Operation now in progress
Definition: systemerror.hpp:175
dodo::common::SystemError::ecEADDRNOTAVAIL
@ ecEADDRNOTAVAIL
99 Cannot assign requested address
Definition: systemerror.hpp:159
dodo::network::BaseSocket::getSendBufSize
socklen_t getSendBufSize() const
Get the maximum buffer length for send.
Definition: basesocket.cpp:169
dodo::network::BaseSocket::sendUInt16
common::SystemError sendUInt16(uint16_t value, bool more=false)
Send an uint16_t.
Definition: basesocket.cpp:302
dodo::network::BaseSocket::setTCPKeepAlive
void setTCPKeepAlive(bool enable)
Enable or disable TCP keep-alive on the socket.
Definition: basesocket.cpp:120
dodo::common::SystemError::ecEISCONN
@ ecEISCONN
106 Transport endpoint is already connected
Definition: systemerror.hpp:166
dodo::network::BaseSocket::BaseSocket
BaseSocket()
Default constructor creates an invalid socket.
Definition: basesocket.cpp:31
dodo::network::BaseSocket::receiveUInt16
common::SystemError receiveUInt16(uint16_t &value)
receive an uint16_t
Definition: basesocket.cpp:307
dodo::network::BaseSocket::getAddressFamily
SocketParams::AddressFamily getAddressFamily() const
Get the SocketParams::AddressFamily of the socket.
Definition: basesocket.cpp:244
dodo::network::SocketParams
Socket parameters - the family (domain), socket type and protocol triplet.
Definition: socketparams.hpp:35
dodo::network::BaseSocket::sendUInt64
common::SystemError sendUInt64(uint64_t value, bool more=false)
Send an sendUInt64.
Definition: basesocket.cpp:407
dodo::network::BaseSocket::socket_
int socket_
The socket file decsriptor.
Definition: basesocket.hpp:503
dodo::network::BaseSocket::getTTL
int getTTL() const
Get the Socket TTL (time to live) or the max number of packet hops.
Definition: basesocket.cpp:185
dodo::network::BaseSocket::getBlocking
virtual bool getBlocking() const
Rerurn true if the socket is operating in blocking mode.
Definition: basesocket.cpp:125
dodo::common::SystemError::ecENETUNREACH
@ ecENETUNREACH
101 Network is unreachable
Definition: systemerror.hpp:161
dodo::network::BaseSocket::receive
virtual common::SystemError receive(void *buf, ssize_t request, ssize_t &received)=0
Receive bytes from the socket.
dodo::network::BaseSocket::getAddress
Address getAddress() const
Get the local address for this socket.
Definition: basesocket.cpp:236
throw_Exception
#define throw_Exception(what)
Throws an Exception, passes FILE and LINE to constructor.
Definition: exception.hpp:174
throw_SystemExceptionObject
#define throw_SystemExceptionObject(what, errno, thing)
Throws an Exception with errno, passes FILE and LINE to constructor.
Definition: exception.hpp:196
dodo::network::BaseSocket::receiveString
common::SystemError receiveString(std::string &s)
Receive a std::string as sent by sendString( const std::string& ).
Definition: basesocket.cpp:459
dodo::common::SystemError::ecOK
@ ecOK
0 Not an error, success
Definition: systemerror.hpp:60
dodo::network
Interface for network communication.
Definition: address.hpp:37
dodo::network::BaseSocket::getSocketParams
SocketParams getSocketParams() const
Return the SocketParams.
Definition: basesocket.cpp:268
dodo::network::BaseSocket::setSendBufSize
void setSendBufSize(socklen_t size)
Set the size of the send buffer.
Definition: basesocket.cpp:199
dodo::network::BaseSocket::isValid
bool isValid() const
Return true when the socket descriptor is a valid, hence 'possible' descriptor.
Definition: basesocket.hpp:128
dodo::network::BaseSocket::receiveUInt8
common::SystemError receiveUInt8(uint8_t &value)
receive an uint8_t
Definition: basesocket.cpp:286
dodo::network::BaseSocket::debugDetail
virtual std::string debugDetail() const
Return debug object state as a string.
Definition: basesocket.cpp:46
dodo::network::BaseSocket::receiveUInt64
common::SystemError receiveUInt64(uint64_t &value)
receive an uint64_t
Definition: basesocket.cpp:412
dodo::network::SocketParams::getSocketType
SocketType getSocketType() const
Get the SocketType.
Definition: socketparams.hpp:154
dodo::network::Address::getAddress
const sockaddr_storage * getAddress() const
Get to the underlying sockaddr_storage.
Definition: address.hpp:165
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
logger.hpp
dodo::network::BaseSocket::getProtocolNumber
SocketParams::ProtocolNumber getProtocolNumber() const
Get the SocketParams::ProtocolNumber of the socket.
Definition: basesocket.cpp:260
dodo::network::Address::addr_
struct sockaddr_storage addr_
The address storage (for either ipv4 or ipv6).
Definition: address.hpp:289
dodo::network::BaseSocket::setReceiveBufSize
void setReceiveBufSize(socklen_t size)
Set the size of the receive buffer.
Definition: basesocket.cpp:204
dodo::network::BaseSocket::sendString
common::SystemError sendString(const std::string &s, bool more=false)
Sends the std::string as understood by receiveString().
Definition: basesocket.cpp:449
dodo::network::BaseSocket::operator=
BaseSocket & operator=(int socket)
Assign from existing socket descriptor (int).
Definition: basesocket.cpp:79
dodo::network::SocketParams::ProtocolNumber
ProtocolNumber
IANA Protocol number.
Definition: socketparams.hpp:73
dodo::network::SocketParams::SocketType
SocketType
Socket Type type.
Definition: socketparams.hpp:59
dodo::common::SystemError::ecEACCES
@ ecEACCES
13 Permission denied
Definition: systemerror.hpp:73
dodo::network::BaseSocket::setReUsePort
void setReUsePort()
Make the socket re-use a port when listen is called.
Definition: basesocket.cpp:159
dodo::network::BaseSocket::receiveInt32
common::SystemError receiveInt32(int32_t &value)
receive an int32_t
Definition: basesocket.cpp:391
dodo::network::BaseSocket::receiveUInt32
common::SystemError receiveUInt32(uint32_t &value)
receive an uint32_t
Definition: basesocket.cpp:370
dodo::network::BaseSocket::setSendTimeout
void setSendTimeout(double sec)
Set the send timeout - a send will fail if there is no data send before the timeout expires.
Definition: basesocket.cpp:219
dodo::network::BaseSocket::receiveInt16
common::SystemError receiveInt16(int16_t &value)
receive an int16_t
Definition: basesocket.cpp:349
dodo::network::SocketParams::getProtocol
ProtocolNumber getProtocol() const
Get the ProtocolNumber.
Definition: socketparams.hpp:160
dodo::network::BaseSocket::bind
common::SystemError bind(const Address &address)
Bind the socket to the Address.
Definition: basesocket.cpp:103
basesocket.hpp
dodo::network::BaseSocket
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
Definition: basesocket.hpp:36
dodo::network::BaseSocket::send
virtual common::SystemError send(const void *buf, ssize_t len, bool more=false)=0
Send bytes on the socket.