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 
57  AsyncSocketHandler( other.pUrl, other.pPoller, other.pTransport,
58  other.pChannelData, other.pSubStreamNum,
59  other.pStream )
60  {
62  }
63 
64  //------------------------------------------------------------------------
66  //------------------------------------------------------------------------
68 
69  //------------------------------------------------------------------------
71  //------------------------------------------------------------------------
72  void SetAddress( const XrdNetAddr &address )
73  {
74  pSockAddr = address;
75  }
76 
77  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  const XrdNetAddr &GetAddress() const
81  {
82  return pSockAddr;
83  }
84 
85  //------------------------------------------------------------------------
87  //------------------------------------------------------------------------
88  XRootDStatus Connect( time_t timeout );
89 
90  //------------------------------------------------------------------------
94  //------------------------------------------------------------------------
96 
97  //------------------------------------------------------------------------
101  //------------------------------------------------------------------------
103 
104  //------------------------------------------------------------------------
106  //------------------------------------------------------------------------
107  virtual void Event( uint8_t type, XrdCl::Socket */*socket*/ );
108 
109  //------------------------------------------------------------------------
111  //------------------------------------------------------------------------
113  {
116  return XRootDStatus();
117  }
118 
119  //------------------------------------------------------------------------
121  //------------------------------------------------------------------------
123  {
124  if( !pPoller->EnableWriteNotification( pSocket, false ) )
126  return XRootDStatus();
127  }
128 
129  //------------------------------------------------------------------------
131  //------------------------------------------------------------------------
132  const std::string &GetStreamName()
133  {
134  return pStreamName;
135  }
136 
137  //------------------------------------------------------------------------
139  //------------------------------------------------------------------------
141  {
142  return pLastActivity;
143  }
144 
145  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  std::string GetIpStack() const;
149 
150  //------------------------------------------------------------------------
152  //------------------------------------------------------------------------
153  std::string GetIpAddr();
154 
155  //------------------------------------------------------------------------
157  //------------------------------------------------------------------------
158  std::string GetHostName();
159 
160  protected:
161 
162  //------------------------------------------------------------------------
164  //------------------------------------------------------------------------
165  static std::string ToStreamName( const URL &url, uint16_t strmnb );
166 
167  //------------------------------------------------------------------------
168  // Connect returned
169  //------------------------------------------------------------------------
171 
172  //------------------------------------------------------------------------
173  // Got a write readiness event
174  //------------------------------------------------------------------------
176 
177  //------------------------------------------------------------------------
178  // Got a write readiness event while handshaking
179  //------------------------------------------------------------------------
181 
182  //------------------------------------------------------------------------
183  // Got a read readiness event
184  //------------------------------------------------------------------------
186 
187  //------------------------------------------------------------------------
188  // Got a read readiness event while handshaking
189  //------------------------------------------------------------------------
191 
192  //------------------------------------------------------------------------
193  // Handle the handshake message
194  //------------------------------------------------------------------------
195  bool HandleHandShake( std::unique_ptr<Message> msg )
197 
198  //------------------------------------------------------------------------
199  // Prepare the next step of the hand-shake procedure
200  //------------------------------------------------------------------------
201  bool HandShakeNextStep( bool done ) XRD_WARN_UNUSED_RESULT;
202 
203  //------------------------------------------------------------------------
204  // Handle fault
205  //------------------------------------------------------------------------
206  void OnFault( XRootDStatus st );
207 
208  //------------------------------------------------------------------------
209  // Handle fault while handshaking
210  //------------------------------------------------------------------------
212 
213  //------------------------------------------------------------------------
214  // Handle write timeout event
215  //------------------------------------------------------------------------
217 
218  //------------------------------------------------------------------------
219  // Handle read timeout event
220  //------------------------------------------------------------------------
222 
223  //------------------------------------------------------------------------
224  // Handle timeout event while handshaking
225  //------------------------------------------------------------------------
227 
228  //------------------------------------------------------------------------
229  // Handle header corruption in case of kXR_status response
230  //------------------------------------------------------------------------
231  void OnHeaderCorruption();
232 
233  //------------------------------------------------------------------------
234  // Carry out the TLS hand-shake
235  //
236  // The TLS hand-shake is being initiated in HandleHandShake() by calling
237  // Socket::TlsHandShake(), however it returns suRetry the TLS hand-shake
238  // needs to be followed up by OnTlsHandShake().
239  //
240  // However, once the TLS connection has been established the server may
241  // decide to redo the TLS hand-shake at any time, this operation is handled
242  // under the hood by read and write requests and facilitated by
243  // Socket::MapEvent()
244  //------------------------------------------------------------------------
246 
247  //------------------------------------------------------------------------
248  // Handle read/write event if we are in the middle of a TLS hand-shake
249  //------------------------------------------------------------------------
250  // Handle read/write event if we are in the middle of a TLS hand-shake
252 
253  //------------------------------------------------------------------------
254  // Prepare a HS writer for sending and enable uplink
255  //------------------------------------------------------------------------
257 
258  //------------------------------------------------------------------------
259  // Extract the value of a wait response
260  //
261  // @param rsp : the server response
262  // @return : if rsp is a wait response then its value
263  // otherwise -1
264  //------------------------------------------------------------------------
265  inline kXR_int32 HandleWaitRsp( Message *rsp );
266 
267  //------------------------------------------------------------------------
268  // Check if HS wait time elapsed
269  //------------------------------------------------------------------------
271 
272  //------------------------------------------------------------------------
273  // Handler for read related socket events
274  //------------------------------------------------------------------------
275  inline bool EventRead( uint8_t type ) XRD_WARN_UNUSED_RESULT;
276 
277  //------------------------------------------------------------------------
278  // Handler for write related socket events
279  //------------------------------------------------------------------------
280  inline bool EventWrite( uint8_t type ) XRD_WARN_UNUSED_RESULT;
281 
282  //------------------------------------------------------------------------
283  // Data members
284  //------------------------------------------------------------------------
288  uint16_t pSubStreamNum;
290  std::string pStreamName;
293  std::unique_ptr<HandShakeData> pHandShakeData;
305 
306  std::unique_ptr<AsyncHSWriter> hswriter;
307  std::unique_ptr<AsyncMsgReader> rspreader;
308  std::unique_ptr<AsyncHSReader> hsreader;
309  std::unique_ptr<AsyncMsgWriter> reqwriter;
310 
311  std::shared_ptr<Channel> pOpenChannel;
312  };
313 }
314 
315 #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.
std::shared_ptr< Channel > pOpenChannel
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
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 AsyncSocketHandler &other)
AsyncSocketHandler(const URL &url, Poller *poller, TransportHandler *transport, AnyObject *channelData, uint16_t subStreamNum, Stream *strm)
Constructors.
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:88
virtual bool EnableWriteNotification(Socket *socket, bool notify, time_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.