xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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,
53  };
54 
55  //------------------------------------------------------------------------
60  //------------------------------------------------------------------------
61  Socket( int socket = -1, SocketStatus status = Disconnected );
62 
63  //------------------------------------------------------------------------
65  //------------------------------------------------------------------------
66  ~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  XRootDStatus Send( const char *buffer, size_t size, int &bytesWritten );
168 
169  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  XRootDStatus Send( XrdSys::KernelBuffer &kbuff, int &bytesWritten );
176 
177  //------------------------------------------------------------------------
183  //------------------------------------------------------------------------
184  XRootDStatus Send( Message &msg, const std::string &strmname );
185 
186  //----------------------------------------------------------------------------
196  //----------------------------------------------------------------------------
197  XRootDStatus Read( char *buffer, size_t size, int &bytesRead );
198 
199  //----------------------------------------------------------------------------
209  //----------------------------------------------------------------------------
210  XRootDStatus ReadV( iovec *iov, int iocnt, int &bytesRead );
211 
212  //------------------------------------------------------------------------
214  //------------------------------------------------------------------------
215  int GetFD()
216  {
217  return pSocket;
218  }
219 
220  //------------------------------------------------------------------------
222  //------------------------------------------------------------------------
223  std::string GetSockName() const;
224 
225  //------------------------------------------------------------------------
227  //------------------------------------------------------------------------
228  std::string GetPeerName() const;
229 
230  //------------------------------------------------------------------------
232  //------------------------------------------------------------------------
233  std::string GetName() const;
234 
235  //------------------------------------------------------------------------
237  //------------------------------------------------------------------------
239  {
240  return pServerAddr.get();
241  }
242 
243  //------------------------------------------------------------------------
246  //------------------------------------------------------------------------
247  void SetChannelID( AnyObject *channelID )
248  {
249  pChannelID = channelID;
250  }
251 
252  //------------------------------------------------------------------------
255  //------------------------------------------------------------------------
256  const AnyObject* GetChannelID() const
257  {
258  return pChannelID;
259  }
260 
261  //------------------------------------------------------------------------
262  // Classify errno while reading/writing
263  //------------------------------------------------------------------------
264  static XRootDStatus ClassifyErrno( int error );
265 
266  //------------------------------------------------------------------------
267  // Cork the underlying socket
268  //
269  // As there is no way to do vector writes with SSL/TLS we need to cork
270  // the socket and then flash it when appropriate
271  //------------------------------------------------------------------------
272  XRootDStatus Cork();
273 
274  //------------------------------------------------------------------------
275  // Uncork the underlying socket
276  //------------------------------------------------------------------------
278 
279  //------------------------------------------------------------------------
280  // Flash the underlying socket
281  //------------------------------------------------------------------------
283 
284  //------------------------------------------------------------------------
285  // Check if the socket is corked
286  //------------------------------------------------------------------------
287  inline bool IsCorked() const
288  {
289  return pCorked;
290  }
291 
292  //------------------------------------------------------------------------
293  // Do special event mapping if applicable
294  //------------------------------------------------------------------------
295  uint8_t MapEvent( uint8_t event );
296 
297  //------------------------------------------------------------------------
298  // Enable encryption
299  //
300  // @param socketHandler : the socket handler that is handling the socket
301  // @param the host : host name for verification
302  //------------------------------------------------------------------------
304  const std::string &thehost = std::string() );
305 
306  //------------------------------------------------------------------------
307  // @return : true if socket is using TLS layer for encryption,
308  // false otherwise
309  //------------------------------------------------------------------------
310  bool IsEncrypted();
311 
312  protected:
313  //------------------------------------------------------------------------
324  //------------------------------------------------------------------------
325  XRootDStatus Poll( bool readyForReading, bool readyForWriting,
326  int32_t timeout );
327 
328  int pSocket;
330  std::unique_ptr<XrdNetAddr> pServerAddr;
331  mutable std::string pSockName; // mutable because it's for caching
332  mutable std::string pPeerName;
333  mutable std::string pName;
336  bool pCorked;
337 
338  std::unique_ptr<Tls> pTls;
339  };
340 }
341 
342 #endif // __XRD_CL_SOCKET_HH__
343 
XRootDStatus ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
std::string GetSockName() const
Get the name of the socket.
XRootDStatus SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
Definition: XrdClAnyObject.hh:32
Socket(int socket=-1, SocketStatus status=Disconnected)
XRootDStatus GetSockOpt(int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
std::string pPeerName
Definition: XrdClSocket.hh:332
The message representation used throughout the system.
Definition: XrdClMessage.hh:29
std::unique_ptr< Tls > pTls
Definition: XrdClSocket.hh:338
std::string GetPeerName() const
Get the name of the remote peer.
SocketStatus pStatus
Definition: XrdClSocket.hh:329
XRootDStatus SetFlags(int flags)
Set the socket flags (man fcntl)
std::string GetName() const
Get the string representation of the socket.
XRootDStatus ReadRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
XRootDStatus Initialize(int family=AF_INET)
Initialize the socket.
SocketStatus GetStatus() const
Get the socket status.
Definition: XrdClSocket.hh:125
std::string pSockName
Definition: XrdClSocket.hh:331
const AnyObject * GetChannelID() const
Definition: XrdClSocket.hh:256
The socket is disconnected.
Definition: XrdClSocket.hh:50
std::string pName
Definition: XrdClSocket.hh:333
Definition: XrdNetAddr.hh:41
void SetChannelID(AnyObject *channelID)
Definition: XrdClSocket.hh:247
bool IsCorked() const
Definition: XrdClSocket.hh:287
uint8_t MapEvent(uint8_t event)
XRootDStatus Flash()
Definition: XrdOucIOVec.hh:65
bool IsEncrypted()
XRootDStatus Connect(const std::string &host, uint16_t port, uint16_t timout=10)
void SetStatus(SocketStatus status)
Set socket status - do not use unless you know what you&#39;re doing.
Definition: XrdClSocket.hh:133
Request status.
Definition: XrdClXRootDResponses.hh:218
XRootDStatus GetFlags(int &flags)
Get the socket flags (man fcntl)
XRootDStatus TlsHandShake(AsyncSocketHandler *socketHandler, const std::string &thehost=std::string())
Definition: XrdSysKernelBuffer.hh:45
XRootDStatus WriteRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesWritten)
XRootDStatus Poll(bool readyForReading, bool readyForWriting, int32_t timeout)
The socket is connected.
Definition: XrdClSocket.hh:51
AnyObject * pChannelID
Definition: XrdClSocket.hh:335
const XrdNetAddr * GetServerAddress() const
Get the server address.
Definition: XrdClSocket.hh:238
static XRootDStatus ClassifyErrno(int error)
~Socket()
Desctuctor.
int pProtocolFamily
Definition: XrdClSocket.hh:334
bool pCorked
Definition: XrdClSocket.hh:336
XRootDStatus Read(char *buffer, size_t size, int &bytesRead)
int pSocket
Definition: XrdClSocket.hh:328
XRootDStatus Uncork()
void Close()
Disconnect.
SocketStatus
Status of the socket.
Definition: XrdClSocket.hh:48
XRootDStatus ReadV(iovec *iov, int iocnt, int &bytesRead)
XRootDStatus Cork()
std::unique_ptr< XrdNetAddr > pServerAddr
Definition: XrdClSocket.hh:330
A network socket.
Definition: XrdClSocket.hh:42
int GetFD()
Get the file descriptor.
Definition: XrdClSocket.hh:215
Definition: XrdClAsyncSocketHandler.hh:42
XRootDStatus Send(const char *buffer, size_t size, int &bytesWritten)
The connection process is in progress.
Definition: XrdClSocket.hh:52