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"
30 
31 
32 namespace XrdCl
33 {
34  class AnyObject;
35  class Tls;
36  class AsyncSocketHandler;
37 
38  //----------------------------------------------------------------------------
40  //----------------------------------------------------------------------------
41  class Socket
42  {
43  public:
44  //------------------------------------------------------------------------
46  //------------------------------------------------------------------------
48  {
50  Connected = 2,
52  };
53 
54  //------------------------------------------------------------------------
59  //------------------------------------------------------------------------
60  Socket( int socket = -1, SocketStatus status = Disconnected );
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  ~Socket();
66 
67  //------------------------------------------------------------------------
69  //------------------------------------------------------------------------
70  XRootDStatus Initialize( int family = AF_INET );
71 
72  //------------------------------------------------------------------------
74  //------------------------------------------------------------------------
75  XRootDStatus SetFlags( int flags );
76 
77  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  XRootDStatus GetFlags( int &flags );
81 
82  //------------------------------------------------------------------------
84  //------------------------------------------------------------------------
85  XRootDStatus GetSockOpt( int level, int optname, void *optval,
86  socklen_t *optlen );
87 
88  //------------------------------------------------------------------------
90  //------------------------------------------------------------------------
91  XRootDStatus SetSockOpt( int level, int optname, const void *optval,
92  socklen_t optlen );
93 
94  //------------------------------------------------------------------------
101  //------------------------------------------------------------------------
102  XRootDStatus Connect( const std::string &host,
103  uint16_t port,
104  uint16_t timout = 10 );
105 
106  //------------------------------------------------------------------------
112  //------------------------------------------------------------------------
114  uint16_t timout = 10 );
115 
116  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  void Close();
120 
121  //------------------------------------------------------------------------
123  //------------------------------------------------------------------------
125  {
126  return pStatus;
127  }
128 
129  //------------------------------------------------------------------------
131  //------------------------------------------------------------------------
132  void SetStatus( SocketStatus status )
133  {
134  pStatus = status;
135  }
136 
137  //------------------------------------------------------------------------
144  //------------------------------------------------------------------------
145  XRootDStatus ReadRaw( void *buffer, uint32_t size, int32_t timeout,
146  uint32_t &bytesRead );
147 
148  //------------------------------------------------------------------------
155  //------------------------------------------------------------------------
156  XRootDStatus WriteRaw( void *buffer, uint32_t size, int32_t timeout,
157  uint32_t &bytesWritten );
158 
159  //------------------------------------------------------------------------
165  //------------------------------------------------------------------------
166  XRootDStatus Send( const char *buffer, size_t size, int &bytesWritten );
167 
168  //------------------------------------------------------------------------
173  //------------------------------------------------------------------------
174  XRootDStatus Send( XrdSys::KernelBuffer &kbuff, int &bytesWritten );
175 
176  //----------------------------------------------------------------------------
186  //----------------------------------------------------------------------------
187  XRootDStatus Read( char *buffer, size_t size, int &bytesRead );
188 
189  //------------------------------------------------------------------------
191  //------------------------------------------------------------------------
192  int GetFD()
193  {
194  return pSocket;
195  }
196 
197  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  std::string GetSockName() const;
201 
202  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  std::string GetPeerName() const;
206 
207  //------------------------------------------------------------------------
209  //------------------------------------------------------------------------
210  std::string GetName() const;
211 
212  //------------------------------------------------------------------------
214  //------------------------------------------------------------------------
216  {
217  return pServerAddr.get();
218  }
219 
220  //------------------------------------------------------------------------
223  //------------------------------------------------------------------------
224  void SetChannelID( AnyObject *channelID )
225  {
226  pChannelID = channelID;
227  }
228 
229  //------------------------------------------------------------------------
232  //------------------------------------------------------------------------
233  const AnyObject* GetChannelID() const
234  {
235  return pChannelID;
236  }
237 
238  //------------------------------------------------------------------------
239  // Classify errno while reading/writing
240  //------------------------------------------------------------------------
241  static XRootDStatus ClassifyErrno( int error );
242 
243  //------------------------------------------------------------------------
244  // Cork the underlying socket
245  //
246  // As there is no way to do vector writes with SSL/TLS we need to cork
247  // the socket and then flash it when appropriate
248  //------------------------------------------------------------------------
249  XRootDStatus Cork();
250 
251  //------------------------------------------------------------------------
252  // Uncork the underlying socket
253  //------------------------------------------------------------------------
255 
256  //------------------------------------------------------------------------
257  // Flash the underlying socket
258  //------------------------------------------------------------------------
260 
261  //------------------------------------------------------------------------
262  // Check if the socket is corked
263  //------------------------------------------------------------------------
264  inline bool IsCorked() const
265  {
266  return pCorked;
267  }
268 
269  //------------------------------------------------------------------------
270  // Do special event mapping if applicable
271  //------------------------------------------------------------------------
272  uint8_t MapEvent( uint8_t event );
273 
274  //------------------------------------------------------------------------
275  // Enable encryption
276  //
277  // @param socketHandler : the socket handler that is handling the socket
278  // @param the host : host name for verification
279  //------------------------------------------------------------------------
281  const std::string &thehost = std::string() );
282 
283  //------------------------------------------------------------------------
284  // @return : true if socket is using TLS layer for encryption,
285  // false otherwise
286  //------------------------------------------------------------------------
287  bool IsEncrypted();
288 
289  protected:
290  //------------------------------------------------------------------------
301  //------------------------------------------------------------------------
302  XRootDStatus Poll( bool readyForReading, bool readyForWriting,
303  int32_t timeout );
304 
305  int pSocket;
307  std::unique_ptr<XrdNetAddr> pServerAddr;
308  mutable std::string pSockName; // mutable because it's for caching
309  mutable std::string pPeerName;
310  mutable std::string pName;
313  bool pCorked;
314 
315  std::unique_ptr<Tls> pTls;
316  };
317 }
318 
319 #endif // __XRD_CL_SOCKET_HH__
320 
XRootDStatus ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
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:309
std::unique_ptr< Tls > pTls
Definition: XrdClSocket.hh:315
SocketStatus pStatus
Definition: XrdClSocket.hh:306
XRootDStatus SetFlags(int flags)
Set the socket flags (man fcntl)
XRootDStatus ReadRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
XRootDStatus Initialize(int family=AF_INET)
Initialize the socket.
std::string pSockName
Definition: XrdClSocket.hh:308
The socket is disconnected.
Definition: XrdClSocket.hh:49
std::string pName
Definition: XrdClSocket.hh:310
Definition: XrdNetAddr.hh:41
void SetChannelID(AnyObject *channelID)
Definition: XrdClSocket.hh:224
uint8_t MapEvent(uint8_t event)
XRootDStatus Flash()
bool IsCorked() const
Definition: XrdClSocket.hh:264
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:132
Request status.
Definition: XrdClXRootDResponses.hh:218
Definition: XrdClAnyObject.hh:25
std::string GetPeerName() const
Get the name of the remote peer.
XRootDStatus GetFlags(int &flags)
Get the socket flags (man fcntl)
XRootDStatus TlsHandShake(AsyncSocketHandler *socketHandler, const std::string &thehost=std::string())
Definition: XrdSysKernelBuffer.hh:45
const AnyObject * GetChannelID() const
Definition: XrdClSocket.hh:233
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:50
AnyObject * pChannelID
Definition: XrdClSocket.hh:312
static XRootDStatus ClassifyErrno(int error)
~Socket()
Desctuctor.
int pProtocolFamily
Definition: XrdClSocket.hh:311
bool pCorked
Definition: XrdClSocket.hh:313
std::string GetName() const
Get the string representation of the socket.
XRootDStatus Read(char *buffer, size_t size, int &bytesRead)
int pSocket
Definition: XrdClSocket.hh:305
XRootDStatus Uncork()
void Close()
Disconnect.
SocketStatus
Status of the socket.
Definition: XrdClSocket.hh:47
XRootDStatus Cork()
std::unique_ptr< XrdNetAddr > pServerAddr
Definition: XrdClSocket.hh:307
std::string GetSockName() const
Get the name of the socket.
A network socket.
Definition: XrdClSocket.hh:41
int GetFD()
Get the file descriptor.
Definition: XrdClSocket.hh:192
const XrdNetAddr * GetServerAddress() const
Get the server address.
Definition: XrdClSocket.hh:215
SocketStatus GetStatus() const
Get the socket status.
Definition: XrdClSocket.hh:124
Definition: XrdClAsyncSocketHandler.hh:39
XRootDStatus Send(const char *buffer, size_t size, int &bytesWritten)
The connection process is in progress.
Definition: XrdClSocket.hh:51