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 <cstdint>
23 #include <string>
24 #include <sys/socket.h>
25 #include <memory>
26 
28 #include "XrdNet/XrdNetAddr.hh"
30 
31 
32 namespace XrdCl
33 {
34  class AnyObject;
35  class Tls;
36  class AsyncSocketHandler;
37  class Message;
38 
39  //----------------------------------------------------------------------------
41  //----------------------------------------------------------------------------
42  class Socket
43  {
44  public:
45  //------------------------------------------------------------------------
47  //------------------------------------------------------------------------
49  {
51  Connected = 2,
52  Connecting = 3
53  };
54 
55  //------------------------------------------------------------------------
60  //------------------------------------------------------------------------
61  Socket( int socket = -1, SocketStatus status = Disconnected );
62 
63  //------------------------------------------------------------------------
65  //------------------------------------------------------------------------
66  virtual ~Socket();
67 
68  //------------------------------------------------------------------------
70  //------------------------------------------------------------------------
71  XRootDStatus Initialize( int family = AF_INET );
72 
73  //------------------------------------------------------------------------
75  //------------------------------------------------------------------------
76  XRootDStatus SetFlags( int flags );
77 
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  XRootDStatus GetFlags( int &flags );
82 
83  //------------------------------------------------------------------------
85  //------------------------------------------------------------------------
86  XRootDStatus GetSockOpt( int level, int optname, void *optval,
87  socklen_t *optlen );
88 
89  //------------------------------------------------------------------------
91  //------------------------------------------------------------------------
92  XRootDStatus SetSockOpt( int level, int optname, const void *optval,
93  socklen_t optlen );
94 
95  //------------------------------------------------------------------------
102  //------------------------------------------------------------------------
103  XRootDStatus Connect( const std::string &host,
104  uint16_t port,
105  uint16_t timout = 10 );
106 
107  //------------------------------------------------------------------------
113  //------------------------------------------------------------------------
115  uint16_t timout = 10 );
116 
117  //------------------------------------------------------------------------
119  //------------------------------------------------------------------------
120  void Close();
121 
122  //------------------------------------------------------------------------
124  //------------------------------------------------------------------------
126  {
127  return pStatus;
128  }
129 
130  //------------------------------------------------------------------------
132  //------------------------------------------------------------------------
133  void SetStatus( SocketStatus status )
134  {
135  pStatus = status;
136  }
137 
138  //------------------------------------------------------------------------
145  //------------------------------------------------------------------------
146  XRootDStatus ReadRaw( void *buffer, uint32_t size, int32_t timeout,
147  uint32_t &bytesRead );
148 
149  //------------------------------------------------------------------------
156  //------------------------------------------------------------------------
157  XRootDStatus WriteRaw( void *buffer, uint32_t size, int32_t timeout,
158  uint32_t &bytesWritten );
159 
160  //------------------------------------------------------------------------
166  //------------------------------------------------------------------------
167  virtual XRootDStatus Send( const char *buffer, size_t size, int &bytesWritten );
168 
169  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  XRootDStatus Send( XrdSys::KernelBuffer &kbuff, int &bytesWritten );
176 
177  //------------------------------------------------------------------------
182  //------------------------------------------------------------------------
183  XRootDStatus Send( Message &msg, const std::string &strmname );
184 
185  //----------------------------------------------------------------------------
195  //----------------------------------------------------------------------------
196  virtual XRootDStatus Read( char *buffer, size_t size, int &bytesRead );
197 
198  //----------------------------------------------------------------------------
208  //----------------------------------------------------------------------------
209  XRootDStatus ReadV( iovec *iov, int iocnt, int &bytesRead );
210 
211  //------------------------------------------------------------------------
213  //------------------------------------------------------------------------
214  int GetFD()
215  {
216  return pSocket;
217  }
218 
219  //------------------------------------------------------------------------
221  //------------------------------------------------------------------------
222  std::string GetSockName() const;
223 
224  //------------------------------------------------------------------------
226  //------------------------------------------------------------------------
227  std::string GetPeerName() const;
228 
229  //------------------------------------------------------------------------
231  //------------------------------------------------------------------------
232  std::string GetName() const;
233 
234  //------------------------------------------------------------------------
236  //------------------------------------------------------------------------
238  {
239  return pServerAddr.get();
240  }
241 
242  //------------------------------------------------------------------------
245  //------------------------------------------------------------------------
246  void SetChannelID( AnyObject *channelID )
247  {
248  pChannelID = channelID;
249  }
250 
251  //------------------------------------------------------------------------
254  //------------------------------------------------------------------------
255  const AnyObject* GetChannelID() const
256  {
257  return pChannelID;
258  }
259 
260  //------------------------------------------------------------------------
261  // Classify errno while reading/writing
262  //------------------------------------------------------------------------
263  static XRootDStatus ClassifyErrno( int error );
264 
265  //------------------------------------------------------------------------
266  // Cork the underlying socket
267  //
268  // As there is no way to do vector writes with SSL/TLS we need to cork
269  // the socket and then flash it when appropriate
270  //------------------------------------------------------------------------
271  XRootDStatus Cork();
272 
273  //------------------------------------------------------------------------
274  // Uncork the underlying socket
275  //------------------------------------------------------------------------
277 
278  //------------------------------------------------------------------------
279  // Flash the underlying socket
280  //------------------------------------------------------------------------
282 
283  //------------------------------------------------------------------------
284  // Check if the socket is corked
285  //------------------------------------------------------------------------
286  inline bool IsCorked() const
287  {
288  return pCorked;
289  }
290 
291  //------------------------------------------------------------------------
292  // Do special event mapping if applicable
293  //------------------------------------------------------------------------
294  uint8_t MapEvent( uint8_t event );
295 
296  //------------------------------------------------------------------------
297  // Enable encryption
298  //
299  // @param socketHandler : the socket handler that is handling the socket
300  // @param the host : host name for verification
301  //------------------------------------------------------------------------
303  const std::string &thehost = std::string() );
304 
305  //------------------------------------------------------------------------
306  // @return : true if socket is using TLS layer for encryption,
307  // false otherwise
308  //------------------------------------------------------------------------
309  bool IsEncrypted();
310 
311  protected:
312  //------------------------------------------------------------------------
323  //------------------------------------------------------------------------
324  XRootDStatus Poll( bool readyForReading, bool readyForWriting,
325  int32_t timeout );
326 
327  int pSocket;
329  std::unique_ptr<XrdNetAddr> pServerAddr;
330  mutable std::string pSockName; // mutable because it's for caching
331  mutable std::string pPeerName;
332  mutable std::string pName;
335  bool pCorked;
336 
337  std::unique_ptr<Tls> pTls;
338  };
339 }
340 
341 #endif // __XRD_CL_SOCKET_HH__
342 
The message representation used throughout the system.
Definition: XrdClMessage.hh:32
A network socket.
Definition: XrdClSocket.hh:43
std::string GetSockName() const
Get the name of the socket.
Definition: XrdClSocket.cc:632
std::string pSockName
Definition: XrdClSocket.hh:330
std::string GetName() const
Get the string representation of the socket.
Definition: XrdClSocket.cc:672
virtual XRootDStatus Send(const char *buffer, size_t size, int &bytesWritten)
Definition: XrdClSocket.cc:461
XRootDStatus Initialize(int family=AF_INET)
Initialize the socket.
Definition: XrdClSocket.cc:63
const AnyObject * GetChannelID() const
Definition: XrdClSocket.hh:255
static XRootDStatus ClassifyErrno(int error)
Definition: XrdClSocket.cc:692
Socket(int socket=-1, SocketStatus status=Disconnected)
Definition: XrdClSocket.cc:44
SocketStatus
Status of the socket.
Definition: XrdClSocket.hh:49
@ 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
bool IsEncrypted()
Definition: XrdClSocket.cc:867
XRootDStatus Flash()
Definition: XrdClSocket.cc:818
void SetChannelID(AnyObject *channelID)
Definition: XrdClSocket.hh:246
SocketStatus pStatus
Definition: XrdClSocket.hh:328
AnyObject * pChannelID
Definition: XrdClSocket.hh:334
XRootDStatus Uncork()
Definition: XrdClSocket.cc:800
virtual XRootDStatus Read(char *buffer, size_t size, int &bytesRead)
Definition: XrdClSocket.cc:740
std::unique_ptr< Tls > pTls
Definition: XrdClSocket.hh:337
XRootDStatus ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
Definition: XrdClSocket.cc:212
int GetFD()
Get the file descriptor.
Definition: XrdClSocket.hh:214
void Close()
Disconnect.
Definition: XrdClSocket.cc:262
XRootDStatus SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
Definition: XrdClSocket.cc:167
bool IsCorked() const
Definition: XrdClSocket.hh:286
XRootDStatus ReadV(iovec *iov, int iocnt, int &bytesRead)
Definition: XrdClSocket.cc:761
XRootDStatus TlsHandShake(AsyncSocketHandler *socketHandler, const std::string &thehost=std::string())
Definition: XrdClSocket.cc:844
XRootDStatus GetFlags(int &flags)
Get the socket flags (man fcntl)
Definition: XrdClSocket.cc:137
uint8_t MapEvent(uint8_t event)
Definition: XrdClSocket.cc:835
std::string pName
Definition: XrdClSocket.hh:332
const XrdNetAddr * GetServerAddress() const
Get the server address.
Definition: XrdClSocket.hh:237
XRootDStatus Connect(const std::string &host, uint16_t port, uint16_t timout=10)
Definition: XrdClSocket.cc:183
XRootDStatus Cork()
Definition: XrdClSocket.cc:782
XRootDStatus GetSockOpt(int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
Definition: XrdClSocket.cc:152
XRootDStatus WriteRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesWritten)
Definition: XrdClSocket.cc:375
std::string pPeerName
Definition: XrdClSocket.hh:331
XRootDStatus ReadRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
Read raw bytes from the socket.
Definition: XrdClSocket.cc:280
SocketStatus GetStatus() const
Get the socket status.
Definition: XrdClSocket.hh:125
XRootDStatus Poll(bool readyForReading, bool readyForWriting, int32_t timeout)
Definition: XrdClSocket.cc:541
virtual ~Socket()
Desctuctor.
Definition: XrdClSocket.cc:55
std::unique_ptr< XrdNetAddr > pServerAddr
Definition: XrdClSocket.hh:329
void SetStatus(SocketStatus status)
Set socket status - do not use unless you know what you're doing.
Definition: XrdClSocket.hh:133
XRootDStatus SetFlags(int flags)
Set the socket flags (man fcntl)
Definition: XrdClSocket.cc:123
std::string GetPeerName() const
Get the name of the remote peer.
Definition: XrdClSocket.cc:652