xrootd
XrdClSocket.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_SOCKET_HH__
20 #define __XRD_CL_SOCKET_HH__
21 
22 #include <stdint.h>
23 #include <string>
24 #include <sys/socket.h>
25 #include <memory>
26 
28 #include "XrdNet/XrdNetAddr.hh"
29 
30 
31 namespace XrdCl
32 {
33  class AnyObject;
34  class Tls;
35  class AsyncSocketHandler;
36 
37  //----------------------------------------------------------------------------
39  //----------------------------------------------------------------------------
40  class Socket
41  {
42  public:
43  //------------------------------------------------------------------------
45  //------------------------------------------------------------------------
47  {
49  Connected = 2,
50  Connecting = 3
51  };
52 
53  //------------------------------------------------------------------------
58  //------------------------------------------------------------------------
59  Socket( int socket = -1, SocketStatus status = Disconnected );
60 
61  //------------------------------------------------------------------------
63  //------------------------------------------------------------------------
65 
66  //------------------------------------------------------------------------
68  //------------------------------------------------------------------------
69  XRootDStatus Initialize( int family = AF_INET );
70 
71  //------------------------------------------------------------------------
73  //------------------------------------------------------------------------
74  XRootDStatus SetFlags( int flags );
75 
76  //------------------------------------------------------------------------
78  //------------------------------------------------------------------------
79  XRootDStatus GetFlags( int &flags );
80 
81  //------------------------------------------------------------------------
83  //------------------------------------------------------------------------
84  XRootDStatus GetSockOpt( int level, int optname, void *optval,
85  socklen_t *optlen );
86 
87  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  XRootDStatus SetSockOpt( int level, int optname, const void *optval,
91  socklen_t optlen );
92 
93  //------------------------------------------------------------------------
100  //------------------------------------------------------------------------
101  XRootDStatus Connect( const std::string &host,
102  uint16_t port,
103  uint16_t timout = 10 );
104 
105  //------------------------------------------------------------------------
111  //------------------------------------------------------------------------
113  uint16_t timout = 10 );
114 
115  //------------------------------------------------------------------------
117  //------------------------------------------------------------------------
118  void Close();
119 
120  //------------------------------------------------------------------------
122  //------------------------------------------------------------------------
124  {
125  return pStatus;
126  }
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  void SetStatus( SocketStatus status )
132  {
133  pStatus = status;
134  }
135 
136  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  XRootDStatus ReadRaw( void *buffer, uint32_t size, int32_t timeout,
145  uint32_t &bytesRead );
146 
147  //------------------------------------------------------------------------
154  //------------------------------------------------------------------------
155  XRootDStatus WriteRaw( void *buffer, uint32_t size, int32_t timeout,
156  uint32_t &bytesWritten );
157 
158  //------------------------------------------------------------------------
164  //------------------------------------------------------------------------
165  XRootDStatus Send( const char *buffer, size_t size, int &bytesWritten );
166 
167  //----------------------------------------------------------------------------
177  //----------------------------------------------------------------------------
178  XRootDStatus Read( char *buffer, size_t size, int &bytesRead );
179 
180  //------------------------------------------------------------------------
182  //------------------------------------------------------------------------
183  int GetFD()
184  {
185  return pSocket;
186  }
187 
188  //------------------------------------------------------------------------
190  //------------------------------------------------------------------------
191  std::string GetSockName() const;
192 
193  //------------------------------------------------------------------------
195  //------------------------------------------------------------------------
196  std::string GetPeerName() const;
197 
198  //------------------------------------------------------------------------
200  //------------------------------------------------------------------------
201  std::string GetName() const;
202 
203  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
207  {
208  return pServerAddr;
209  }
210 
211  //------------------------------------------------------------------------
214  //------------------------------------------------------------------------
215  void SetChannelID( AnyObject *channelID )
216  {
217  pChannelID = channelID;
218  }
219 
220  //------------------------------------------------------------------------
223  //------------------------------------------------------------------------
224  const AnyObject* GetChannelID() const
225  {
226  return pChannelID;
227  }
228 
229  //------------------------------------------------------------------------
230  // Classify errno while reading/writing
231  //------------------------------------------------------------------------
232  static XRootDStatus ClassifyErrno( int error );
233 
234  //------------------------------------------------------------------------
235  // Cork the underlying socket
236  //
237  // As there is no way to do vector writes with SSL/TLS we need to cork
238  // the socket and then flash it when appropriate
239  //------------------------------------------------------------------------
241 
242  //------------------------------------------------------------------------
243  // Uncork the underlying socket
244  //------------------------------------------------------------------------
246 
247  //------------------------------------------------------------------------
248  // Flash the underlying socket
249  //------------------------------------------------------------------------
251 
252  //------------------------------------------------------------------------
253  // Check if the socket is corked
254  //------------------------------------------------------------------------
255  inline bool IsCorked() const
256  {
257  return pCorked;
258  }
259 
260  //------------------------------------------------------------------------
261  // Do special event mapping if applicable
262  //------------------------------------------------------------------------
263  uint8_t MapEvent( uint8_t event );
264 
265  //------------------------------------------------------------------------
266  // Enable encryption
267  //
268  // @param socketHandler : the socket handler that is handling the socket
269  // @param the host : host name for verification
270  //------------------------------------------------------------------------
272  const std::string &thehost = std::string() );
273 
274  //------------------------------------------------------------------------
275  // @return : true if socket is using TLS layer for encryption,
276  // false otherwise
277  //------------------------------------------------------------------------
278  bool IsEncrypted();
279 
280  protected:
281  //------------------------------------------------------------------------
292  //------------------------------------------------------------------------
293  XRootDStatus Poll( bool readyForReading, bool readyForWriting,
294  int32_t timeout );
295 
296  int pSocket;
299  mutable std::string pSockName; // mutable because it's for caching
300  mutable std::string pPeerName;
301  mutable std::string pName;
304  bool pCorked;
305 
306  std::unique_ptr<Tls> pTls;
307  };
308 }
309 
310 #endif // __XRD_CL_SOCKET_HH__
311 
XrdCl::Socket::ConnectToAddress
XRootDStatus ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
XrdCl::Socket::ReadRaw
XRootDStatus ReadRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
XrdClXRootDResponses.hh
XrdCl::Socket
A network socket.
Definition: XrdClSocket.hh:41
XrdCl::Socket::SetFlags
XRootDStatus SetFlags(int flags)
Set the socket flags (man fcntl)
XrdCl::Socket::SetStatus
void SetStatus(SocketStatus status)
Set socket status - do not use unless you know what you're doing.
Definition: XrdClSocket.hh:131
XrdCl::Socket::GetSockName
std::string GetSockName() const
Get the name of the socket.
XrdCl::Socket::Disconnected
@ Disconnected
The socket is disconnected.
Definition: XrdClSocket.hh:48
XrdCl::Socket::pChannelID
AnyObject * pChannelID
Definition: XrdClSocket.hh:303
XrdCl::Socket::GetFD
int GetFD()
Get the file descriptor.
Definition: XrdClSocket.hh:183
XrdCl::Socket::GetChannelID
const AnyObject * GetChannelID() const
Definition: XrdClSocket.hh:224
XrdCl::Socket::SetSockOpt
XRootDStatus SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
XrdCl::Socket::WriteRaw
XRootDStatus WriteRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesWritten)
XrdCl::Socket::GetPeerName
std::string GetPeerName() const
Get the name of the remote peer.
XrdCl::Socket::Connecting
@ Connecting
The connection process is in progress.
Definition: XrdClSocket.hh:50
XrdCl::Socket::Poll
XRootDStatus Poll(bool readyForReading, bool readyForWriting, int32_t timeout)
XrdCl::Socket::GetSockOpt
XRootDStatus GetSockOpt(int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
XrdNetAddr.hh
XrdCl::Socket::~Socket
~Socket()
Desctuctor.
XrdCl::Socket::pStatus
SocketStatus pStatus
Definition: XrdClSocket.hh:297
XrdCl::Socket::IsCorked
bool IsCorked() const
Definition: XrdClSocket.hh:255
XrdCl::Socket::pPeerName
std::string pPeerName
Definition: XrdClSocket.hh:300
XrdCl::Socket::MapEvent
uint8_t MapEvent(uint8_t event)
XrdCl::Socket::pSockName
std::string pSockName
Definition: XrdClSocket.hh:299
XrdCl::XRootDStatus
Request status.
Definition: XrdClXRootDResponses.hh:215
XrdCl::Socket::pSocket
int pSocket
Definition: XrdClSocket.hh:296
XrdCl::Socket::Uncork
XRootDStatus Uncork()
XrdCl::Socket::GetName
std::string GetName() const
Get the string representation of the socket.
XrdCl::Socket::pTls
std::unique_ptr< Tls > pTls
Definition: XrdClSocket.hh:306
XrdCl::Socket::Connected
@ Connected
The socket is connected.
Definition: XrdClSocket.hh:49
XrdCl::Socket::Send
XRootDStatus Send(const char *buffer, size_t size, int &bytesWritten)
XrdCl::Socket::Socket
Socket(int socket=-1, SocketStatus status=Disconnected)
XrdCl::Socket::pProtocolFamily
int pProtocolFamily
Definition: XrdClSocket.hh:302
XrdCl::Socket::ClassifyErrno
static XRootDStatus ClassifyErrno(int error)
XrdCl::Socket::Cork
XRootDStatus Cork()
XrdCl::AsyncSocketHandler
Definition: XrdClAsyncSocketHandler.hh:39
XrdCl::Socket::GetServerAddress
const XrdNetAddr & GetServerAddress() const
Get the server address.
Definition: XrdClSocket.hh:206
XrdCl::Socket::SetChannelID
void SetChannelID(AnyObject *channelID)
Definition: XrdClSocket.hh:215
XrdCl
Definition: XrdClAnyObject.hh:26
XrdCl::Socket::Close
void Close()
Disconnect.
XrdCl::Socket::Initialize
XRootDStatus Initialize(int family=AF_INET)
Initialize the socket.
XrdCl::Socket::Read
XRootDStatus Read(char *buffer, size_t size, int &bytesRead)
XrdCl::Socket::IsEncrypted
bool IsEncrypted()
XrdCl::Socket::SocketStatus
SocketStatus
Status of the socket.
Definition: XrdClSocket.hh:47
XrdCl::Socket::GetFlags
XRootDStatus GetFlags(int &flags)
Get the socket flags (man fcntl)
XrdCl::Socket::Flash
XRootDStatus Flash()
XrdCl::Socket::pName
std::string pName
Definition: XrdClSocket.hh:301
XrdNetAddr
Definition: XrdNetAddr.hh:42
XrdCl::AnyObject
Definition: XrdClAnyObject.hh:33
XrdCl::Socket::pCorked
bool pCorked
Definition: XrdClSocket.hh:304
XrdCl::Socket::Connect
XRootDStatus Connect(const std::string &host, uint16_t port, uint16_t timout=10)
XrdCl::Socket::GetStatus
SocketStatus GetStatus() const
Get the socket status.
Definition: XrdClSocket.hh:123
XrdCl::Socket::pServerAddr
XrdNetAddr pServerAddr
Definition: XrdClSocket.hh:298
XrdCl::Socket::TlsHandShake
XRootDStatus TlsHandShake(AsyncSocketHandler *socketHandler, const std::string &thehost=std::string())