XRootD
XrdClAsyncSocketHandler.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_ASYNC_SOCKET_HANDLER_HH__
20 #define __XRD_CL_ASYNC_SOCKET_HANDLER_HH__
21 
22 #include "XrdCl/XrdClSocket.hh"
23 #include "XrdCl/XrdClDefaultEnv.hh"
24 #include "XrdCl/XrdClPoller.hh"
28 #include "XrdCl/XrdClURL.hh"
33 #include "XrdOuc/XrdOucCompiler.hh"
34 
35 namespace XrdCl
36 {
37  class Stream;
38 
39  //----------------------------------------------------------------------------
42  //----------------------------------------------------------------------------
44  {
45  public:
46  //------------------------------------------------------------------------
48  //------------------------------------------------------------------------
49  AsyncSocketHandler( const URL &url,
50  Poller *poller,
51  TransportHandler *transport,
52  AnyObject *channelData,
53  uint16_t subStreamNum,
54  Stream *strm );
55 
56  //------------------------------------------------------------------------
58  //------------------------------------------------------------------------
60 
61  //------------------------------------------------------------------------
63  //------------------------------------------------------------------------
64  void SetAddress( const XrdNetAddr &address )
65  {
66  pSockAddr = address;
67  }
68 
69  //------------------------------------------------------------------------
71  //------------------------------------------------------------------------
72  const XrdNetAddr &GetAddress() const
73  {
74  return pSockAddr;
75  }
76 
77  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  XRootDStatus Connect( time_t timeout );
81 
82  //------------------------------------------------------------------------
84  //------------------------------------------------------------------------
86 
87  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  virtual void Event( uint8_t type, XrdCl::Socket */*socket*/ );
91 
92  //------------------------------------------------------------------------
94  //------------------------------------------------------------------------
96  {
99  return XRootDStatus();
100  }
101 
102  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
106  {
107  if( !pPoller->EnableWriteNotification( pSocket, false ) )
109  return XRootDStatus();
110  }
111 
112  //------------------------------------------------------------------------
114  //------------------------------------------------------------------------
115  const std::string &GetStreamName()
116  {
117  return pStreamName;
118  }
119 
120  //------------------------------------------------------------------------
122  //------------------------------------------------------------------------
124  {
125  return pLastActivity;
126  }
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  std::string GetIpStack() const;
132 
133  //------------------------------------------------------------------------
135  //------------------------------------------------------------------------
136  std::string GetIpAddr();
137 
138  //------------------------------------------------------------------------
140  //------------------------------------------------------------------------
141  std::string GetHostName();
142 
143  protected:
144 
145  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  static std::string ToStreamName( const URL &url, uint16_t strmnb );
149 
150  //------------------------------------------------------------------------
151  // Connect returned
152  //------------------------------------------------------------------------
154 
155  //------------------------------------------------------------------------
156  // Got a write readiness event
157  //------------------------------------------------------------------------
159 
160  //------------------------------------------------------------------------
161  // Got a write readiness event while handshaking
162  //------------------------------------------------------------------------
164 
165  //------------------------------------------------------------------------
166  // Got a read readiness event
167  //------------------------------------------------------------------------
169 
170  //------------------------------------------------------------------------
171  // Got a read readiness event while handshaking
172  //------------------------------------------------------------------------
174 
175  //------------------------------------------------------------------------
176  // Handle the handshake message
177  //------------------------------------------------------------------------
178  bool HandleHandShake( std::unique_ptr<Message> msg )
180 
181  //------------------------------------------------------------------------
182  // Prepare the next step of the hand-shake procedure
183  //------------------------------------------------------------------------
184  bool HandShakeNextStep( bool done ) XRD_WARN_UNUSED_RESULT;
185 
186  //------------------------------------------------------------------------
187  // Handle fault
188  //------------------------------------------------------------------------
189  void OnFault( XRootDStatus st );
190 
191  //------------------------------------------------------------------------
192  // Handle fault while handshaking
193  //------------------------------------------------------------------------
195 
196  //------------------------------------------------------------------------
197  // Handle write timeout event
198  //------------------------------------------------------------------------
200 
201  //------------------------------------------------------------------------
202  // Handle read timeout event
203  //------------------------------------------------------------------------
205 
206  //------------------------------------------------------------------------
207  // Handle timeout event while handshaking
208  //------------------------------------------------------------------------
210 
211  //------------------------------------------------------------------------
212  // Handle header corruption in case of kXR_status response
213  //------------------------------------------------------------------------
214  void OnHeaderCorruption();
215 
216  //------------------------------------------------------------------------
217  // Carry out the TLS hand-shake
218  //
219  // The TLS hand-shake is being initiated in HandleHandShake() by calling
220  // Socket::TlsHandShake(), however it returns suRetry the TLS hand-shake
221  // needs to be followed up by OnTlsHandShake().
222  //
223  // However, once the TLS connection has been established the server may
224  // decide to redo the TLS hand-shake at any time, this operation is handled
225  // under the hood by read and write requests and facilitated by
226  // Socket::MapEvent()
227  //------------------------------------------------------------------------
229 
230  //------------------------------------------------------------------------
231  // Handle read/write event if we are in the middle of a TLS hand-shake
232  //------------------------------------------------------------------------
233  // Handle read/write event if we are in the middle of a TLS hand-shake
235 
236  //------------------------------------------------------------------------
237  // Prepare a HS writer for sending and enable uplink
238  //------------------------------------------------------------------------
240 
241  //------------------------------------------------------------------------
242  // Extract the value of a wait response
243  //
244  // @param rsp : the server response
245  // @return : if rsp is a wait response then its value
246  // otherwise -1
247  //------------------------------------------------------------------------
248  inline kXR_int32 HandleWaitRsp( Message *rsp );
249 
250  //------------------------------------------------------------------------
251  // Check if HS wait time elapsed
252  //------------------------------------------------------------------------
254 
255  //------------------------------------------------------------------------
256  // Handler for read related socket events
257  //------------------------------------------------------------------------
258  inline bool EventRead( uint8_t type ) XRD_WARN_UNUSED_RESULT;
259 
260  //------------------------------------------------------------------------
261  // Handler for write related socket events
262  //------------------------------------------------------------------------
263  inline bool EventWrite( uint8_t type ) XRD_WARN_UNUSED_RESULT;
264 
265  //------------------------------------------------------------------------
266  // Data members
267  //------------------------------------------------------------------------
271  uint16_t pSubStreamNum;
273  std::string pStreamName;
276  std::unique_ptr<HandShakeData> pHandShakeData;
286 
287  std::unique_ptr<AsyncHSWriter> hswriter;
288  std::unique_ptr<AsyncMsgReader> rspreader;
289  std::unique_ptr<AsyncHSReader> hsreader;
290  std::unique_ptr<AsyncMsgWriter> reqwriter;
291  };
292 }
293 
294 #endif // __XRD_CL_ASYNC_SOCKET_HANDLER_HH__
int kXR_int32
Definition: XPtypes.hh:89
#define XRD_WARN_UNUSED_RESULT
Utility class encapsulating reading hand-shake response logic.
Utility class encapsulating writing hand-shake request logic.
Utility class encapsulating reading response message logic.
Utility class encapsulating writing request logic.
static std::string ToStreamName(const URL &url, uint16_t strmnb)
Convert Stream object and sub-stream number to stream name.
time_t GetLastActivity()
Get timestamp of last registered socket activity.
bool OnReadTimeout() XRD_WARN_UNUSED_RESULT
std::unique_ptr< AsyncHSWriter > hswriter
virtual bool OnConnectionReturn() XRD_WARN_UNUSED_RESULT
bool OnWriteTimeout() XRD_WARN_UNUSED_RESULT
bool OnWrite() XRD_WARN_UNUSED_RESULT
bool OnTimeoutWhileHandshaking() XRD_WARN_UNUSED_RESULT
bool CheckHSWait() XRD_WARN_UNUSED_RESULT
bool EventRead(uint8_t type) XRD_WARN_UNUSED_RESULT
std::unique_ptr< AsyncHSReader > hsreader
bool HandleHandShake(std::unique_ptr< Message > msg) XRD_WARN_UNUSED_RESULT
bool OnWriteWhileHandshaking() XRD_WARN_UNUSED_RESULT
XRootDStatus Close()
Close the connection.
void OnFaultWhileHandshaking(XRootDStatus st)
const std::string & GetStreamName()
Get stream name.
virtual void Event(uint8_t type, XrdCl::Socket *)
Handle a socket event.
kXR_int32 HandleWaitRsp(Message *rsp)
bool HandShakeNextStep(bool done) XRD_WARN_UNUSED_RESULT
bool OnReadWhileHandshaking() XRD_WARN_UNUSED_RESULT
std::unique_ptr< AsyncMsgWriter > reqwriter
XRootDStatus EnableUplink()
Enable uplink.
std::string GetIpStack() const
Get the IP stack.
bool EventWrite(uint8_t type) XRD_WARN_UNUSED_RESULT
std::string GetHostName()
Get hostname.
std::unique_ptr< AsyncMsgReader > rspreader
XRootDStatus DisableUplink()
Disable uplink.
std::unique_ptr< HandShakeData > pHandShakeData
bool OnRead() XRD_WARN_UNUSED_RESULT
XRootDStatus Connect(time_t timeout)
Connect to the currently set address.
const XrdNetAddr & GetAddress() const
Get the address that the socket is connected to.
AsyncSocketHandler(const URL &url, Poller *poller, TransportHandler *transport, AnyObject *channelData, uint16_t subStreamNum, Stream *strm)
Constructor.
bool OnTLSHandShake() XRD_WARN_UNUSED_RESULT
void SetAddress(const XrdNetAddr &address)
Set address.
bool SendHSMsg() XRD_WARN_UNUSED_RESULT
std::string GetIpAddr()
Get IP address.
The message representation used throughout the system.
Definition: XrdClMessage.hh:32
Interface for socket pollers.
Definition: XrdClPoller.hh:87
virtual bool EnableWriteNotification(Socket *socket, bool notify, uint16_t timeout=60)=0
A network socket.
Definition: XrdClSocket.hh:43
Perform the handshake and the authentication for each physical stream.
URL representation.
Definition: XrdClURL.hh:31
const uint16_t stFatal
Fatal error, it's still an error.
Definition: XrdClStatus.hh:33
const uint16_t errPollerError
Definition: XrdClStatus.hh:75
Data structure that carries the handshake information.