XRootD
XrdCl::Socket Class Reference

A network socket. More...

#include <XrdClSocket.hh>

+ Collaboration diagram for XrdCl::Socket:

Public Types

enum  SocketStatus {
  Disconnected = 1 ,
  Connected = 2 ,
  Connecting = 3
}
 Status of the socket. More...
 

Public Member Functions

 Socket (int socket=-1, SocketStatus status=Disconnected)
 
virtual ~Socket ()
 Desctuctor. More...
 
void Close ()
 Disconnect. More...
 
XRootDStatus Connect (const std::string &host, uint16_t port, uint16_t timout=10)
 
XRootDStatus ConnectToAddress (const XrdNetAddr &addr, uint16_t timout=10)
 
XRootDStatus Cork ()
 
XRootDStatus Flash ()
 
const AnyObjectGetChannelID () const
 
int GetFD ()
 Get the file descriptor. More...
 
XRootDStatus GetFlags (int &flags)
 Get the socket flags (man fcntl) More...
 
std::string GetName () const
 Get the string representation of the socket. More...
 
std::string GetPeerName () const
 Get the name of the remote peer. More...
 
const XrdNetAddrGetServerAddress () const
 Get the server address. More...
 
std::string GetSockName () const
 Get the name of the socket. More...
 
XRootDStatus GetSockOpt (int level, int optname, void *optval, socklen_t *optlen)
 Get socket options. More...
 
SocketStatus GetStatus () const
 Get the socket status. More...
 
XRootDStatus Initialize (int family=AF_INET)
 Initialize the socket. More...
 
bool IsCorked () const
 
bool IsEncrypted ()
 
uint8_t MapEvent (uint8_t event)
 
virtual XRootDStatus Read (char *buffer, size_t size, int &bytesRead)
 
XRootDStatus ReadRaw (void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
 Read raw bytes from the socket. More...
 
XRootDStatus ReadV (iovec *iov, int iocnt, int &bytesRead)
 
virtual XRootDStatus Send (const char *buffer, size_t size, int &bytesWritten)
 
XRootDStatus Send (Message &msg, const std::string &strmname)
 
XRootDStatus Send (XrdSys::KernelBuffer &kbuff, int &bytesWritten)
 
void SetChannelID (AnyObject *channelID)
 
XRootDStatus SetFlags (int flags)
 Set the socket flags (man fcntl) More...
 
XRootDStatus SetSockOpt (int level, int optname, const void *optval, socklen_t optlen)
 Set socket options. More...
 
void SetStatus (SocketStatus status)
 Set socket status - do not use unless you know what you're doing. More...
 
XRootDStatus TlsHandShake (AsyncSocketHandler *socketHandler, const std::string &thehost=std::string())
 
XRootDStatus Uncork ()
 
XRootDStatus WriteRaw (void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesWritten)
 

Static Public Member Functions

static XRootDStatus ClassifyErrno (int error)
 

Protected Member Functions

XRootDStatus Poll (bool readyForReading, bool readyForWriting, int32_t timeout)
 

Protected Attributes

AnyObjectpChannelID
 
bool pCorked
 
std::string pName
 
std::string pPeerName
 
int pProtocolFamily
 
std::unique_ptr< XrdNetAddrpServerAddr
 
int pSocket
 
std::string pSockName
 
SocketStatus pStatus
 
std::unique_ptr< TlspTls
 

Detailed Description

A network socket.

Definition at line 42 of file XrdClSocket.hh.

Member Enumeration Documentation

◆ SocketStatus

Status of the socket.

Enumerator
Disconnected 

The socket is disconnected.

Connected 

The socket is connected.

Connecting 

The connection process is in progress.

Definition at line 48 of file XrdClSocket.hh.

49  {
50  Disconnected = 1,
51  Connected = 2,
52  Connecting = 3
53  };
@ Disconnected
The socket is disconnected.
Definition: XrdClSocket.hh:50
@ Connected
The socket is connected.
Definition: XrdClSocket.hh:51
@ Connecting
The connection process is in progress.
Definition: XrdClSocket.hh:52

Constructor & Destructor Documentation

◆ Socket()

XrdCl::Socket::Socket ( int  socket = -1,
SocketStatus  status = Disconnected 
)

Constructor

Parameters
socketalready connected socket if available, -1 otherwise
statusstatus of a socket if available

Definition at line 44 of file XrdClSocket.cc.

44  :
45  pSocket(socket), pStatus( status ),
46  pProtocolFamily( AF_INET ),
47  pChannelID( 0 ),
48  pCorked( false )
49  {
50  };
SocketStatus pStatus
Definition: XrdClSocket.hh:328
AnyObject * pChannelID
Definition: XrdClSocket.hh:334

◆ ~Socket()

XrdCl::Socket::~Socket ( )
virtual

Desctuctor.

Definition at line 55 of file XrdClSocket.cc.

56  {
57  Close();
58  };
void Close()
Disconnect.
Definition: XrdClSocket.cc:262

References Close().

+ Here is the call graph for this function:

Member Function Documentation

◆ ClassifyErrno()

XRootDStatus XrdCl::Socket::ClassifyErrno ( int  error)
static

Definition at line 692 of file XrdClSocket.cc.

693  {
694  switch( errno )
695  {
696 
697  case EAGAIN:
698 #if EAGAIN != EWOULDBLOCK
699  case EWOULDBLOCK:
700 #endif
701  {
702  //------------------------------------------------------------------
703  // Reading/writing operation would block! So we are done for now,
704  // but we will be back ;-)
705  //------------------------------------------------------------------
706  return XRootDStatus( stOK, suRetry );
707  }
708  case ECONNRESET:
709  case EDESTADDRREQ:
710  case EMSGSIZE:
711  case ENOTCONN:
712  case ENOTSOCK:
713  {
714  //------------------------------------------------------------------
715  // Actual socket error error!
716  //------------------------------------------------------------------
717  return XRootDStatus( stError, errSocketError, errno );
718  }
719  case EFAULT:
720  {
721  //------------------------------------------------------------------
722  // The buffer provided by the user for reading/writing is invalid
723  //------------------------------------------------------------------
724  return XRootDStatus( stError, errInvalidArgs );
725  }
726  default:
727  {
728  //------------------------------------------------------------------
729  // Not a socket error
730  //------------------------------------------------------------------
731  return XRootDStatus( stError, errInternal, errno );
732  }
733  }
734  }
const uint16_t suRetry
Definition: XrdClStatus.hh:40
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
const uint16_t errInternal
Internal error.
Definition: XrdClStatus.hh:56
const uint16_t stOK
Everything went OK.
Definition: XrdClStatus.hh:31
const uint16_t errInvalidArgs
Definition: XrdClStatus.hh:58
const uint16_t errSocketError
Definition: XrdClStatus.hh:72

References XrdCl::errInternal, XrdCl::errInvalidArgs, XrdCl::errSocketError, XrdCl::stError, XrdCl::stOK, and XrdCl::suRetry.

Referenced by Read(), ReadV(), and Send().

+ Here is the caller graph for this function:

◆ Close()

void XrdCl::Socket::Close ( )

Disconnect.

Definition at line 262 of file XrdClSocket.cc.

263  {
264  if( pTls ) pTls->Shutdown();
265 
266  if( pSocket != -1 )
267  {
268  close( pSocket );
270  pSocket = -1;
271  pSockName = "";
272  pPeerName = "";
273  pName = "";
274  }
275  }
#define close(a)
Definition: XrdPosix.hh:43
std::string pSockName
Definition: XrdClSocket.hh:330
std::unique_ptr< Tls > pTls
Definition: XrdClSocket.hh:337
std::string pName
Definition: XrdClSocket.hh:332
std::string pPeerName
Definition: XrdClSocket.hh:331

References close, Disconnected, pName, pPeerName, pSocket, pSockName, pStatus, and pTls.

Referenced by ~Socket(), XrdCl::AsyncSocketHandler::Close(), XrdCl::AsyncSocketHandler::Connect(), ConnectToAddress(), Initialize(), ReadRaw(), and WriteRaw().

+ Here is the caller graph for this function:

◆ Connect()

XRootDStatus XrdCl::Socket::Connect ( const std::string &  host,
uint16_t  port,
uint16_t  timout = 10 
)

Connect to the given host name

Parameters
hostname of the host to connect to
portport to connect to
timouttimeout in seconds, 0 for no timeout handling (may be used for non blocking IO)

Definition at line 183 of file XrdClSocket.cc.

186  {
187  if( pSocket == -1 || pStatus == Connected || pStatus == Connecting )
188  return XRootDStatus( stError, errInvalidOp );
189 
190  std::vector<XrdNetAddr> addrs;
191  std::ostringstream o; o << host << ":" << port;
192  XRootDStatus st;
193 
194  if( pProtocolFamily == AF_INET6 )
195  st = Utils::GetHostAddresses( addrs, URL( o.str() ), Utils::IPAll );
196  else
197  st = Utils::GetHostAddresses( addrs, URL( o.str() ), Utils::IPv4 );
198 
199  if( !st.IsOK() )
200  return st;
201 
203  addrs );
204 
205 
206  return ConnectToAddress( addrs[0], timeout );
207  }
static Log * GetLog()
Get default log.
XRootDStatus ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
Definition: XrdClSocket.cc:212
static void LogHostAddresses(Log *log, uint64_t type, const std::string &hostId, std::vector< XrdNetAddr > &addresses)
Log all the addresses on the list.
Definition: XrdClUtils.cc:234
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
Definition: XrdClUtils.cc:140
const uint64_t PostMasterMsg
const uint16_t errInvalidOp
Definition: XrdClStatus.hh:51

References Connected, Connecting, ConnectToAddress(), XrdCl::errInvalidOp, XrdCl::Utils::GetHostAddresses(), XrdCl::DefaultEnv::GetLog(), XrdCl::Utils::IPAll, XrdCl::Utils::IPv4, XrdCl::Status::IsOK(), XrdCl::Utils::LogHostAddresses(), XrdCl::PostMasterMsg, pProtocolFamily, pSocket, pStatus, and XrdCl::stError.

+ Here is the call graph for this function:

◆ ConnectToAddress()

XRootDStatus XrdCl::Socket::ConnectToAddress ( const XrdNetAddr addr,
uint16_t  timout = 10 
)

Connect to the given host address

Parameters
addraddress of the host to connect to
timouttimeout in seconds, 0 for no timeout handling (may be used for non blocking IO)

Definition at line 212 of file XrdClSocket.cc.

214  {
215  if( pSocket == -1 || pStatus == Connected || pStatus == Connecting )
216  return XRootDStatus( stError, errInvalidOp );
217 
218  pServerAddr.reset( new XrdNetAddr( addr ) );;
219 
220  //--------------------------------------------------------------------------
221  // Make sure TLS is off when the physical connection is newly established
222  //--------------------------------------------------------------------------
223  pTls.reset();
224 
225  //--------------------------------------------------------------------------
226  // Connect
227  //--------------------------------------------------------------------------
228  int status = XrdNetConnect::Connect( pSocket, pServerAddr->SockAddr(),
229  pServerAddr->SockSize(), timeout );
230  if( status != 0 )
231  {
232  XRootDStatus st( stError );
233 
234  //------------------------------------------------------------------------
235  // If we connect asynchronously this is not really an error
236  //------------------------------------------------------------------------
237  if( !timeout && status == EINPROGRESS )
238  {
240  return XRootDStatus();
241  }
242 
243  //------------------------------------------------------------------------
244  // Errors
245  //------------------------------------------------------------------------
246  else if( status == ETIMEDOUT )
247  st.code = errSocketTimeout;
248  else
249  st.code = errSocketError;
250  st.errNo = status;
251 
252  Close();
253  return st;
254  }
255  pStatus = Connected;
256  return XRootDStatus();
257  }
std::unique_ptr< XrdNetAddr > pServerAddr
Definition: XrdClSocket.hh:329
static int Connect(int fd, const struct sockaddr *name, int namelen, int tsec=-1)
const uint16_t errSocketTimeout
Definition: XrdClStatus.hh:73

References Close(), XrdCl::Status::code, XrdNetConnect::Connect(), Connected, Connecting, XrdCl::errInvalidOp, XrdCl::Status::errNo, XrdCl::errSocketError, XrdCl::errSocketTimeout, pServerAddr, pSocket, pStatus, pTls, and XrdCl::stError.

Referenced by Connect(), and XrdCl::AsyncSocketHandler::Connect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Cork()

XRootDStatus XrdCl::Socket::Cork ( )

Definition at line 782 of file XrdClSocket.cc.

783  {
784 #if defined(TCP_CORK) && !defined(__GNU__)
785  // it's not defined on mac, we might want explore the possibility of using TCP_NOPUSH
786  if( pCorked ) return XRootDStatus();
787 
788  int state = 1;
789  int rc = setsockopt( pSocket, IPPROTO_TCP, TCP_CORK, &state, sizeof( state ) );
790  if( rc != 0 )
791  return XRootDStatus( stFatal, errSocketOptError, errno );
792 #endif
793  pCorked = true;
794  return XRootDStatus();
795  }
#define IPPROTO_TCP
Definition: XrdNetUtils.cc:800
const uint16_t errSocketOptError
Definition: XrdClStatus.hh:76
const uint16_t stFatal
Fatal error, it's still an error.
Definition: XrdClStatus.hh:33

References XrdCl::errSocketOptError, IPPROTO_TCP, pCorked, pSocket, and XrdCl::stFatal.

Referenced by Flash(), and XrdCl::AsyncSocketHandler::OnConnectionReturn().

+ Here is the caller graph for this function:

◆ Flash()

XRootDStatus XrdCl::Socket::Flash ( )

Definition at line 818 of file XrdClSocket.cc.

819  {
820  //----------------------------------------------------------------------
821  // Uncork the socket in order to flash the socket
822  //----------------------------------------------------------------------
823  XRootDStatus st = Uncork();
824  if( !st.IsOK() ) return st;
825 
826  //----------------------------------------------------------------------
827  // Once the data has been flashed we can cork the socket back
828  //----------------------------------------------------------------------
829  return Cork();
830  }
XRootDStatus Uncork()
Definition: XrdClSocket.cc:800
XRootDStatus Cork()
Definition: XrdClSocket.cc:782

References Cork(), XrdCl::Status::IsOK(), and Uncork().

Referenced by XrdCl::AsyncHSWriter::Write(), and XrdCl::AsyncMsgWriter::Write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetChannelID()

const AnyObject* XrdCl::Socket::GetChannelID ( ) const
inline

Get Channel ID (an object that allows to identify all sockets corresponding to the same channel)

Definition at line 255 of file XrdClSocket.hh.

256  {
257  return pChannelID;
258  }

References pChannelID.

◆ GetFD()

int XrdCl::Socket::GetFD ( )
inline

Get the file descriptor.

Definition at line 214 of file XrdClSocket.hh.

215  {
216  return pSocket;
217  }

References pSocket.

Referenced by XrdCl::Tls::Tls(), and XrdCl::PollerBuiltIn::AddSocket().

+ Here is the caller graph for this function:

◆ GetFlags()

XRootDStatus XrdCl::Socket::GetFlags ( int &  flags)

Get the socket flags (man fcntl)

Definition at line 137 of file XrdClSocket.cc.

138  {
139  if( pSocket == -1 )
140  return XRootDStatus( stError, errInvalidOp );
141 
142  int st = ::fcntl( pSocket, F_GETFL, 0 );
143  if( st == -1 )
144  return XRootDStatus( stError, errSocketError, errno );
145  flags = st;
146  return XRootDStatus();
147  }
int fcntl(int fd, int cmd,...)

References XrdCl::errInvalidOp, XrdCl::errSocketError, fcntl(), pSocket, and XrdCl::stError.

+ Here is the call graph for this function:

◆ GetName()

std::string XrdCl::Socket::GetName ( ) const

Get the string representation of the socket.

Definition at line 672 of file XrdClSocket.cc.

673  {
674  if( pStatus != Connected )
675  return "<x><--><x>";
676 
677  if( pName.length() )
678  return pName;
679 
680  pName = "<";
681  pName += GetSockName();
682  pName += "><--><";
683  pName += GetPeerName();
684  pName += ">";
685  return pName;
686  }
std::string GetSockName() const
Get the name of the socket.
Definition: XrdClSocket.cc:632
std::string GetPeerName() const
Get the name of the remote peer.
Definition: XrdClSocket.cc:652

References Connected, GetPeerName(), GetSockName(), pName, and pStatus.

Referenced by XrdCl::PollerBuiltIn::AddSocket(), XrdCl::PollerBuiltIn::EnableReadNotification(), XrdCl::PollerBuiltIn::EnableWriteNotification(), XrdCl::PollerBuiltIn::RemoveSocket(), and XrdCl::PollerBuiltIn::Stop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetPeerName()

std::string XrdCl::Socket::GetPeerName ( ) const

Get the name of the remote peer.

Definition at line 652 of file XrdClSocket.cc.

653  {
654  if( pStatus != Connected )
655  return "";
656 
657  if( pPeerName.length() )
658  return pPeerName;
659 
660  char nameBuff[256];
661  int len = XrdNetUtils::IPFormat( pSocket, nameBuff, sizeof(nameBuff) );
662  if( len == 0 )
663  return "";
664 
665  pPeerName = nameBuff;
666  return pPeerName;
667  }
static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int opts=0)
Definition: XrdNetUtils.cc:584

References Connected, XrdNetUtils::IPFormat(), pPeerName, pSocket, and pStatus.

Referenced by GetName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetServerAddress()

const XrdNetAddr* XrdCl::Socket::GetServerAddress ( ) const
inline

Get the server address.

Definition at line 237 of file XrdClSocket.hh.

238  {
239  return pServerAddr.get();
240  }

References pServerAddr.

Referenced by XrdCl::AsyncSocketHandler::OnConnectionReturn().

+ Here is the caller graph for this function:

◆ GetSockName()

std::string XrdCl::Socket::GetSockName ( ) const

Get the name of the socket.

Definition at line 632 of file XrdClSocket.cc.

633  {
634  if( pStatus != Connected )
635  return "";
636 
637  if( pSockName.length() )
638  return pSockName;
639 
640  char nameBuff[256];
641  int len = XrdNetUtils::IPFormat( -pSocket, nameBuff, sizeof(nameBuff) );
642  if( len == 0 )
643  return "";
644 
645  pSockName = nameBuff;
646  return pSockName;
647  }

References Connected, XrdNetUtils::IPFormat(), pSocket, pSockName, and pStatus.

Referenced by GetName(), and XrdCl::AsyncSocketHandler::OnConnectionReturn().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetSockOpt()

XRootDStatus XrdCl::Socket::GetSockOpt ( int  level,
int  optname,
void *  optval,
socklen_t *  optlen 
)

Get socket options.

Definition at line 152 of file XrdClSocket.cc.

154  {
155  if( pSocket == -1 )
156  return XRootDStatus( stError, errInvalidOp );
157 
158  if( ::getsockopt( pSocket, level, optname, optval, optlen ) != 0 )
159  return XRootDStatus( stError, errSocketOptError, errno );
160 
161  return XRootDStatus();
162  }

References XrdCl::errInvalidOp, XrdCl::errSocketOptError, pSocket, and XrdCl::stError.

Referenced by XrdCl::AsyncSocketHandler::OnConnectionReturn().

+ Here is the caller graph for this function:

◆ GetStatus()

SocketStatus XrdCl::Socket::GetStatus ( ) const
inline

Get the socket status.

Definition at line 125 of file XrdClSocket.hh.

126  {
127  return pStatus;
128  }

References pStatus.

Referenced by XrdCl::PollerBuiltIn::AddSocket(), and XrdCl::AsyncSocketHandler::EventWrite().

+ Here is the caller graph for this function:

◆ Initialize()

XRootDStatus XrdCl::Socket::Initialize ( int  family = AF_INET)

Initialize the socket.

Definition at line 63 of file XrdClSocket.cc.

64  {
65  if( pSocket != -1 )
66  return XRootDStatus( stError, errInvalidOp );
67 
68  pSocket = XrdSysFD_Socket( family, SOCK_STREAM, 0 );
69  if( pSocket < 0 )
70  {
71  pSocket = -1;
72  return XRootDStatus( stError, errSocketError );
73  }
74 
75  pProtocolFamily = family;
76 
77  //--------------------------------------------------------------------------
78  // Make the socket non blocking and disable the Nagle algorithm since
79  // we will be using this for transmitting messages not handling streams
80  //--------------------------------------------------------------------------
81  int flags;
82  if( (flags = ::fcntl( pSocket, F_GETFL, 0 )) == -1 )
83  flags = 0;
84  if( ::fcntl( pSocket, F_SETFL, flags | O_NONBLOCK | O_NDELAY ) == -1 )
85  {
86  Close();
87  return XRootDStatus( stError, errFcntl, errno );
88  }
89 
91  flags = DefaultNoDelay;
92  env->GetInt( "NoDelay", flags );
93  if( setsockopt( pSocket, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof( int ) ) < 0 )
94  {
95  Close();
96  return XRootDStatus( stError, errFcntl, errno );
97  }
98 
99  //--------------------------------------------------------------------------
100  // We use send with MSG_NOSIGNAL to avoid SIGPIPEs on Linux, on MacOSX
101  // we set SO_NOSIGPIPE option, on Solaris we ignore the SIGPIPE
102  //--------------------------------------------------------------------------
103 #ifdef __APPLE__
104  int set = 1;
105  XRootDStatus st = SetSockOpt( SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(int) );
106  if( !st.IsOK() )
107  {
108  Close();
109  return st;
110  }
111 #elif __solaris__
112  struct sigaction act;
113  act.sa_handler = SIG_IGN;
114  sigaction( SIGPIPE, &act, NULL );
115 #endif
116 
117  return XRootDStatus();
118  }
static Env * GetEnv()
Get default client environment.
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:89
XRootDStatus SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
Definition: XrdClSocket.cc:167
const uint16_t errFcntl
failed manipulate file descriptor
Definition: XrdClStatus.hh:53
const int DefaultNoDelay

References Close(), XrdCl::DefaultNoDelay, XrdCl::errFcntl, XrdCl::errInvalidOp, XrdCl::errSocketError, fcntl(), XrdCl::DefaultEnv::GetEnv(), XrdCl::Env::GetInt(), IPPROTO_TCP, XrdCl::Status::IsOK(), pProtocolFamily, pSocket, SetSockOpt(), and XrdCl::stError.

Referenced by XrdCl::AsyncSocketHandler::Connect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsCorked()

bool XrdCl::Socket::IsCorked ( ) const
inline

Definition at line 286 of file XrdClSocket.hh.

287  {
288  return pCorked;
289  }

References pCorked.

Referenced by XrdCl::Tls::Connect(), XrdCl::Tls::Read(), and XrdCl::Tls::Send().

+ Here is the caller graph for this function:

◆ IsEncrypted()

bool XrdCl::Socket::IsEncrypted ( )

Definition at line 867 of file XrdClSocket.cc.

868  {
869  return bool( pTls.get() );
870  }

References pTls.

Referenced by XrdCl::AsyncSocketHandler::HandleHandShake(), and XrdCl::XRootDMsgHandler::WriteMessageBody().

+ Here is the caller graph for this function:

◆ MapEvent()

uint8_t XrdCl::Socket::MapEvent ( uint8_t  event)

Definition at line 835 of file XrdClSocket.cc.

836  {
837  if( pTls ) return pTls->MapEvent( event );
838  return event;
839  }

References pTls.

Referenced by XrdCl::AsyncSocketHandler::Event().

+ Here is the caller graph for this function:

◆ Poll()

XRootDStatus XrdCl::Socket::Poll ( bool  readyForReading,
bool  readyForWriting,
int32_t  timeout 
)
protected

Poll the socket to see whether it is ready for IO

Parameters
readyForReadingpoll for readiness to read
readyForWritingpoll for readiness to write
timeouttimeout in seconds, -1 to wait indefinitely
Returns
stOK - ready for IO errSocketDisconnected - on disconnection errSocketError - on socket error errSocketTimeout - on socket timeout errInvalidOp - when called on a non connected socket

Definition at line 541 of file XrdClSocket.cc.

543  {
544  //--------------------------------------------------------------------------
545  // Check if we're connected
546  //--------------------------------------------------------------------------
547  if( pStatus != Connected )
548  return XRootDStatus( stError, errInvalidOp );
549 
550  //--------------------------------------------------------------------------
551  // Prepare the stuff
552  //--------------------------------------------------------------------------
553  pollfd pollDesc;
554  int pollRet;
555  bool useTimeout = (timeout!=-1);
556  time_t now = 0;
557  time_t newNow = 0;
558  short hupEvents = POLLHUP;
559 
560 #ifdef __linux__
561  hupEvents |= POLLRDHUP;
562 #endif
563 
564  if( useTimeout )
565  now = ::time(0);
566 
567  pollDesc.fd = pSocket;
568  pollDesc.events = POLLERR | POLLNVAL | hupEvents;
569 
570  if( readyForReading )
571  pollDesc.events |= (POLLIN | POLLPRI);
572 
573  if( readyForWriting )
574  pollDesc.events |= POLLOUT;
575 
576  //--------------------------------------------------------------------------
577  // We loop on poll because it may return -1 even thought no fatal error
578  // has occurred, these may be:
579  // * a signal interrupting the execution (errno == EINTR)
580  // * a failure to initialize some internal structures (Solaris only)
581  // (errno == EAGAIN)
582  //--------------------------------------------------------------------------
583  do
584  {
585  pollRet = poll( &pollDesc, 1, (useTimeout ? timeout*1000 : -1) );
586  if( (pollRet < 0) && (errno != EINTR) && (errno != EAGAIN) )
587  return XRootDStatus( stError, errPoll, errno );
588 
589  //------------------------------------------------------------------------
590  // Check if we did not time out in the case where we are not supposed
591  // to wait indefinitely
592  //------------------------------------------------------------------------
593  if( useTimeout )
594  {
595  newNow = time(0);
596  timeout -= (newNow-now);
597  now = newNow;
598  if( timeout < 0 )
599  return XRootDStatus( stError, errSocketTimeout );
600  }
601  }
602  while( pollRet == -1 );
603 
604  //--------------------------------------------------------------------------
605  // Check if we have timed out
606  //--------------------------------------------------------------------------
607  if( pollRet == 0 )
608  return XRootDStatus( stError, errSocketTimeout );
609 
610  //--------------------------------------------------------------------------
611  // We have some events
612  //--------------------------------------------------------------------------
613  if( pollDesc.revents & (POLLIN | POLLPRI | POLLOUT) )
614  return XRootDStatus( stOK );
615 
616  //--------------------------------------------------------------------------
617  // We've been hang up on
618  //--------------------------------------------------------------------------
619  if( pollDesc.revents & hupEvents )
620  return XRootDStatus( stError, errSocketDisconnected );
621 
622  //--------------------------------------------------------------------------
623  // We're messed up, either because we messed up ourselves (POLLNVAL) or
624  // got messed up by the network (POLLERR)
625  //--------------------------------------------------------------------------
626  return XRootDStatus( stError, errSocketError );
627  }
const uint16_t errPoll
error while polling descriptors
Definition: XrdClStatus.hh:54
const uint16_t errSocketDisconnected
Definition: XrdClStatus.hh:74

References Connected, XrdCl::errInvalidOp, XrdCl::errPoll, XrdCl::errSocketDisconnected, XrdCl::errSocketError, XrdCl::errSocketTimeout, pSocket, pStatus, XrdCl::stError, and XrdCl::stOK.

Referenced by ReadRaw(), and WriteRaw().

+ Here is the caller graph for this function:

◆ Read()

XRootDStatus XrdCl::Socket::Read ( char *  buffer,
size_t  size,
int &  bytesRead 
)
virtual

Read helper for raw socket

Parameters
buffer: the sink for the data
size: size of the sink
bytesRead: number of bytes actually written into the sink
Returns
: success : ( stOK ) EAGAIN : ( stOK, suRetry ) EWOULDBLOCK : ( stOK, suRetry ) other error : ( stError, errSocketError )

Definition at line 740 of file XrdClSocket.cc.

741  {
742  if( pTls ) return pTls->Read( buffer, size, bytesRead );
743 
744  int status = ::read( pSocket, buffer, size );
745 
746  // if the server shut down the socket declare a socket error (it
747  // will trigger a re-connect)
748  if( status == 0 )
749  return XRootDStatus( stError, errSocketError, errno );
750 
751  if( status < 0 )
752  return ClassifyErrno( errno );
753 
754  bytesRead = status;
755  return XRootDStatus();
756  }
ssize_t read(int fildes, void *buf, size_t nbyte)
static XRootDStatus ClassifyErrno(int error)
Definition: XrdClSocket.cc:692

References ClassifyErrno(), XrdCl::errSocketError, pSocket, pTls, read(), and XrdCl::stError.

Referenced by XrdCl::XRootDTransport::GetBody(), XrdCl::XRootDTransport::GetHeader(), XrdCl::XRootDTransport::GetMore(), and XrdCl::AsyncRawReaderIntfc::ReadBytesAsync().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ReadRaw()

XRootDStatus XrdCl::Socket::ReadRaw ( void *  buffer,
uint32_t  size,
int32_t  timeout,
uint32_t &  bytesRead 
)

Read raw bytes from the socket.

Read raw bytes from the socket

Parameters
bufferdata to be sent
sizesize of the data buffer
timeouttimout value in seconds, -1 to wait indefinitely
bytesReadthe amount of data actually read

Definition at line 280 of file XrdClSocket.cc.

282  {
283  //--------------------------------------------------------------------------
284  // Check if we're connected
285  //--------------------------------------------------------------------------
286  if( pStatus != Connected )
287  return XRootDStatus( stError, errInvalidOp );
288 
289  //--------------------------------------------------------------------------
290  // Some useful variables
291  //--------------------------------------------------------------------------
292  bytesRead = 0;
293 
294  char *current = (char *)buffer;
295  bool useTimeout = (timeout!=-1);
296  time_t now = 0;
297  time_t newNow = 0;
298  XRootDStatus sc;
299 
300  if( useTimeout )
301  now = ::time(0);
302 
303  //--------------------------------------------------------------------------
304  // Repeat the following until we have read all the requested data
305  //--------------------------------------------------------------------------
306  while ( bytesRead < size )
307  {
308  //------------------------------------------------------------------------
309  // Check if we can read something
310  //------------------------------------------------------------------------
311  sc = Poll( true, false, useTimeout ? timeout : -1 );
312 
313  //------------------------------------------------------------------------
314  // It looks like we've got an event. Let's check if we can read something.
315  //------------------------------------------------------------------------
316  if( sc.status == stOK )
317  {
318  ssize_t n = ::read( pSocket, current, (size-bytesRead) );
319 
320  if( n > 0 )
321  {
322  bytesRead += n;
323  current += n;
324  }
325 
326  //----------------------------------------------------------------------
327  // We got a close here - this means that there is no more data in
328  // the buffer so we disconnect
329  //----------------------------------------------------------------------
330  if( n == 0 )
331  {
332  Close();
333  return XRootDStatus( stError, errSocketDisconnected );
334  }
335 
336  //----------------------------------------------------------------------
337  // Error
338  //----------------------------------------------------------------------
339  if( (n < 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) )
340  {
341  Close();
342  return XRootDStatus( stError, errSocketError, errno );
343  }
344  }
345  else
346  {
347  Close();
348  return sc;
349  }
350 
351  //------------------------------------------------------------------------
352  // Do we still have time to wait for data?
353  //------------------------------------------------------------------------
354  if( useTimeout )
355  {
356  newNow = ::time(0);
357  timeout -= (newNow-now);
358  now = newNow;
359  if( timeout < 0 )
360  break;
361  }
362  }
363 
364  //--------------------------------------------------------------------------
365  // Have we managed to read everything?
366  //--------------------------------------------------------------------------
367  if( bytesRead < size )
368  return XRootDStatus( stError, errSocketTimeout );
369  return XRootDStatus( stOK );
370  }
XRootDStatus Poll(bool readyForReading, bool readyForWriting, int32_t timeout)
Definition: XrdClSocket.cc:541

References Close(), Connected, XrdCl::errInvalidOp, XrdCl::errSocketDisconnected, XrdCl::errSocketError, XrdCl::errSocketTimeout, Poll(), pSocket, pStatus, read(), XrdCl::Status::status, XrdCl::stError, and XrdCl::stOK.

+ Here is the call graph for this function:

◆ ReadV()

XRootDStatus XrdCl::Socket::ReadV ( iovec *  iov,
int  iocnt,
int &  bytesRead 
)

ReadV helper for raw socket

Parameters
iov: the buffers for the data
iocnt: number of buffers
bytesRead: number of bytes actually written into the sink
Returns
: success : ( stOK ) EAGAIN : ( stOK, suRetry ) EWOULDBLOCK : ( stOK, suRetry ) other error : ( stError, errSocketError )

Definition at line 761 of file XrdClSocket.cc.

762  {
763  if( pTls ) return pTls->ReadV( iov, iovcnt, bytesRead );
764 
765  int status = ::readv( pSocket, iov, iovcnt );
766 
767  // if the server shut down the socket declare a socket error (it
768  // will trigger a re-connect)
769  if( status == 0 )
770  return XRootDStatus( stError, errSocketError, errno );
771 
772  if( status < 0 )
773  return ClassifyErrno( errno );
774 
775  bytesRead = status;
776  return XRootDStatus();
777  }
ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)

References ClassifyErrno(), XrdCl::errSocketError, pSocket, pTls, readv(), and XrdCl::stError.

Referenced by XrdCl::AsyncPageReader::Read().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Send() [1/3]

XRootDStatus XrdCl::Socket::Send ( const char *  buffer,
size_t  size,
int &  bytesWritten 
)
virtual

Portable wrapper around SIGPIPE free send

Parameters
buffer: data to be written
size: size of the data buffer
bytesWritten: the amount of data actually written

Definition at line 461 of file XrdClSocket.cc.

462  {
463  if( pTls ) return pTls->Send( buffer, size, bytesWritten );
464 
465  //--------------------------------------------------------------------------
466  // We use send with MSG_NOSIGNAL to avoid SIGPIPEs on Linux
467  //--------------------------------------------------------------------------
468 #if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
469  int status = ::send( pSocket, buffer, size, MSG_NOSIGNAL );
470 #else
471  int status = ::write( pSocket, buffer, size );
472 #endif
473 
474  if( status <= 0 )
475  return ClassifyErrno( errno );
476 
477  bytesWritten = status;
478  return XRootDStatus();
479  }
ssize_t write(int fildes, const void *buf, size_t nbyte)

References ClassifyErrno(), pSocket, pTls, and write().

Referenced by Send(), XrdCl::AsyncHSWriter::Write(), XrdCl::AsyncMsgWriter::Write(), and XrdCl::XRootDMsgHandler::WriteMessageBody().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Send() [2/3]

XRootDStatus XrdCl::Socket::Send ( Message msg,
const std::string &  strmname 
)

Write message to the socket

Parameters
msg: message (request) to be sent
strmname: stream name (for logging purposes)

Definition at line 503 of file XrdClSocket.cc.

504  {
505  //----------------------------------------------------------------------
506  // Try to write down the current message
507  //----------------------------------------------------------------------
508  size_t btsleft = msg.GetSize() - msg.GetCursor();
509  if( !btsleft ) return XRootDStatus();
510 
511  while( btsleft )
512  {
513  int wrtcnt = 0;
514  XRootDStatus st = Send( msg.GetBufferAtCursor(), btsleft, wrtcnt );
515 
516  if( !st.IsOK() )
517  {
518  msg.SetCursor( 0 );
519  return st;
520  }
521 
522  if( st.code == suRetry ) return st;
523 
524  msg.AdvanceCursor( wrtcnt );
525  btsleft -= wrtcnt;
526  }
527 
528  //----------------------------------------------------------------------
529  // We have written the message successfully
530  //----------------------------------------------------------------------
531  Log *log = DefaultEnv::GetLog();
532  log->Dump( AsyncSockMsg, "[%s] Wrote a message: %s (%p), %d bytes",
533  strmname.c_str(), msg.GetObfuscatedDescription().c_str(),
534  &msg, msg.GetSize() );
535  return XRootDStatus();
536  }
virtual XRootDStatus Send(const char *buffer, size_t size, int &bytesWritten)
Definition: XrdClSocket.cc:461
const uint64_t AsyncSockMsg
XrdSysError Log
Definition: XrdConfig.cc:112

References XrdCl::Buffer::AdvanceCursor(), XrdCl::AsyncSockMsg, XrdCl::Status::code, XrdCl::Log::Dump(), XrdCl::Buffer::GetBufferAtCursor(), XrdCl::Buffer::GetCursor(), XrdCl::DefaultEnv::GetLog(), XrdCl::Message::GetObfuscatedDescription(), XrdCl::Buffer::GetSize(), XrdCl::Status::IsOK(), Send(), XrdCl::Buffer::SetCursor(), and XrdCl::suRetry.

+ Here is the call graph for this function:

◆ Send() [3/3]

XRootDStatus XrdCl::Socket::Send ( XrdSys::KernelBuffer kbuff,
int &  bytesWritten 
)

Write data from a kernel buffer to the socket

Parameters
kbuff: data to be written
bytesWritten: the amount of data actually written

Write data from a kernel buffer to the socket

Parameters
kbuff: data to be written
Returns
: the amount of data actually written

Definition at line 487 of file XrdClSocket.cc.

488  {
489  if( pTls ) return XRootDStatus( stError, errNotSupported, 0,
490  "Cannot send a kernel-buffer over TLS." );
491 
492  ssize_t status = XrdSys::Send( pSocket, kbuff );
493 
494  if( status <= 0 )
495  return ClassifyErrno( errno );
496  bytesWritten += status;
497  return XRootDStatus();
498  }
const uint16_t errNotSupported
Definition: XrdClStatus.hh:62
ssize_t Send(int fd, KernelBuffer &buffer)

References ClassifyErrno(), XrdCl::errNotSupported, pSocket, pTls, XrdSys::Send(), and XrdCl::stError.

+ Here is the call graph for this function:

◆ SetChannelID()

void XrdCl::Socket::SetChannelID ( AnyObject channelID)
inline

Set Channel ID (an object that allows to identify all sockets corresponding to the same channel)

Definition at line 246 of file XrdClSocket.hh.

247  {
248  pChannelID = channelID;
249  }

References pChannelID.

Referenced by XrdCl::AsyncSocketHandler::AsyncSocketHandler().

+ Here is the caller graph for this function:

◆ SetFlags()

XRootDStatus XrdCl::Socket::SetFlags ( int  flags)

Set the socket flags (man fcntl)

Definition at line 123 of file XrdClSocket.cc.

124  {
125  if( pSocket == -1 )
126  return XRootDStatus( stError, errInvalidOp );
127 
128  int st = ::fcntl( pSocket, F_SETFL, flags );
129  if( st == -1 )
130  return XRootDStatus( stError, errSocketError, errno );
131  return XRootDStatus();
132  }

References XrdCl::errInvalidOp, XrdCl::errSocketError, fcntl(), pSocket, and XrdCl::stError.

+ Here is the call graph for this function:

◆ SetSockOpt()

XRootDStatus XrdCl::Socket::SetSockOpt ( int  level,
int  optname,
const void *  optval,
socklen_t  optlen 
)

Set socket options.

Definition at line 167 of file XrdClSocket.cc.

169  {
170  if( pSocket == -1 )
171  return XRootDStatus( stError, errInvalidOp );
172 
173  if( ::setsockopt( pSocket, level, optname, optval, optlen ) != 0 )
174  return XRootDStatus( stError, errSocketOptError, errno );
175 
176  return XRootDStatus();
177  }

References XrdCl::errInvalidOp, XrdCl::errSocketOptError, pSocket, and XrdCl::stError.

Referenced by XrdCl::AsyncSocketHandler::Connect(), and Initialize().

+ Here is the caller graph for this function:

◆ SetStatus()

void XrdCl::Socket::SetStatus ( SocketStatus  status)
inline

Set socket status - do not use unless you know what you're doing.

Definition at line 133 of file XrdClSocket.hh.

134  {
135  pStatus = status;
136  }

References pStatus.

Referenced by XrdCl::AsyncSocketHandler::Connect(), and XrdCl::AsyncSocketHandler::OnConnectionReturn().

+ Here is the caller graph for this function:

◆ TlsHandShake()

XRootDStatus XrdCl::Socket::TlsHandShake ( AsyncSocketHandler socketHandler,
const std::string &  thehost = std::string() 
)

Definition at line 844 of file XrdClSocket.cc.

846  {
847  try
848  {
849  if( !pServerAddr ) return XRootDStatus( stError, errInvalidOp );
850  if( !pTls ) pTls.reset( new Tls( this, socketHandler ) );
851  return pTls->Connect( thehost, pServerAddr.get() );
852  }
853  catch( std::exception& ex )
854  {
855  // the exception has been thrown when we tried to create
856  // the TLS context
857  return XRootDStatus( stFatal, errTlsError, 0, ex.what() );
858  }
859 
860  return XRootDStatus();
861  }
const uint16_t errTlsError
Definition: XrdClStatus.hh:80

References XrdCl::errInvalidOp, XrdCl::errTlsError, pServerAddr, pTls, XrdCl::stError, and XrdCl::stFatal.

Referenced by XrdCl::AsyncSocketHandler::DoTlsHandShake().

+ Here is the caller graph for this function:

◆ Uncork()

XRootDStatus XrdCl::Socket::Uncork ( )

Definition at line 800 of file XrdClSocket.cc.

801  {
802 #if defined(TCP_CORK) && !defined(__GNU__)
803  // it's not defined on mac, we might want explore the possibility of using TCP_NOPUSH
804  if( !pCorked ) return XRootDStatus();
805 
806  int state = 0;
807  int rc = setsockopt( pSocket, IPPROTO_TCP, TCP_CORK, &state, sizeof( state ) );
808  if( rc != 0 )
809  return XRootDStatus( stFatal, errSocketOptError, errno );
810 #endif
811  pCorked = false;
812  return XRootDStatus();
813  }

References XrdCl::errSocketOptError, IPPROTO_TCP, pCorked, pSocket, and XrdCl::stFatal.

Referenced by XrdCl::Tls::Connect(), Flash(), XrdCl::Tls::Read(), and XrdCl::Tls::Send().

+ Here is the caller graph for this function:

◆ WriteRaw()

XRootDStatus XrdCl::Socket::WriteRaw ( void *  buffer,
uint32_t  size,
int32_t  timeout,
uint32_t &  bytesWritten 
)

Write raw bytes to the socket

Parameters
bufferdata to be written
sizesize of the data buffer
timeouttimeout value in seconds, -1 to wait indefinitely
bytesWrittenthe amount of data actually written

Definition at line 375 of file XrdClSocket.cc.

377  {
378  //--------------------------------------------------------------------------
379  // Check if we're connected
380  //--------------------------------------------------------------------------
381  if( pStatus != Connected )
382  return XRootDStatus( stError, errInvalidOp );
383 
384  //--------------------------------------------------------------------------
385  // Some useful variables
386  //--------------------------------------------------------------------------
387  bytesWritten = 0;
388 
389  char *current = (char *)buffer;
390  bool useTimeout = (timeout!=-1);
391  time_t now = 0;
392  time_t newNow = 0;
393  XRootDStatus sc;
394 
395  if( useTimeout )
396  now = ::time(0);
397 
398  //--------------------------------------------------------------------------
399  // Repeat the following until we have written everything
400  //--------------------------------------------------------------------------
401  while ( bytesWritten < size )
402  {
403  //------------------------------------------------------------------------
404  // Check if we can read something
405  //------------------------------------------------------------------------
406  sc = Poll( false, true, useTimeout ? timeout : -1 );
407 
408  //------------------------------------------------------------------------
409  // Let's write
410  //------------------------------------------------------------------------
411  if( sc.status == stOK )
412  {
413  ssize_t n = ::write( pSocket, current, (size-bytesWritten) );
414 
415  if( n > 0 )
416  {
417  bytesWritten += n;
418  current += n;
419  }
420 
421  //----------------------------------------------------------------------
422  // Error
423  //----------------------------------------------------------------------
424  if( (n <= 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) )
425  {
426  Close();
427  return XRootDStatus( stError, errSocketError, errno );
428  }
429  }
430  else
431  {
432  Close();
433  return sc;
434  }
435 
436  //------------------------------------------------------------------------
437  // Do we still have time to wait for data?
438  //------------------------------------------------------------------------
439  if( useTimeout )
440  {
441  newNow = ::time(0);
442  timeout -= (newNow-now);
443  now = newNow;
444  if( timeout < 0 )
445  break;
446  }
447  }
448 
449  //--------------------------------------------------------------------------
450  // Have we managed to read everything?
451  //--------------------------------------------------------------------------
452  if( bytesWritten < size )
453  return XRootDStatus( stError, errSocketTimeout );
454 
455  return XRootDStatus( stOK );
456  }

References Close(), Connected, XrdCl::errInvalidOp, XrdCl::errSocketError, XrdCl::errSocketTimeout, Poll(), pSocket, pStatus, XrdCl::Status::status, XrdCl::stError, XrdCl::stOK, and write().

+ Here is the call graph for this function:

Member Data Documentation

◆ pChannelID

AnyObject* XrdCl::Socket::pChannelID
protected

Definition at line 334 of file XrdClSocket.hh.

Referenced by GetChannelID(), and SetChannelID().

◆ pCorked

bool XrdCl::Socket::pCorked
protected

Definition at line 335 of file XrdClSocket.hh.

Referenced by Cork(), IsCorked(), and Uncork().

◆ pName

std::string XrdCl::Socket::pName
mutableprotected

Definition at line 332 of file XrdClSocket.hh.

Referenced by Close(), and GetName().

◆ pPeerName

std::string XrdCl::Socket::pPeerName
mutableprotected

Definition at line 331 of file XrdClSocket.hh.

Referenced by Close(), and GetPeerName().

◆ pProtocolFamily

int XrdCl::Socket::pProtocolFamily
protected

Definition at line 333 of file XrdClSocket.hh.

Referenced by Connect(), and Initialize().

◆ pServerAddr

std::unique_ptr<XrdNetAddr> XrdCl::Socket::pServerAddr
protected

Definition at line 329 of file XrdClSocket.hh.

Referenced by ConnectToAddress(), GetServerAddress(), and TlsHandShake().

◆ pSocket

◆ pSockName

std::string XrdCl::Socket::pSockName
mutableprotected

Definition at line 330 of file XrdClSocket.hh.

Referenced by Close(), and GetSockName().

◆ pStatus

SocketStatus XrdCl::Socket::pStatus
protected

◆ pTls

std::unique_ptr<Tls> XrdCl::Socket::pTls
protected

The documentation for this class was generated from the following files: