00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // XRootD is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // XRootD is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00017 //------------------------------------------------------------------------------ 00018 00019 #ifndef __XRD_CL_SOCKET_HH__ 00020 #define __XRD_CL_SOCKET_HH__ 00021 00022 #include <stdint.h> 00023 #include <string> 00024 #include <sys/socket.h> 00025 #include <memory> 00026 00027 #include "XrdCl/XrdClXRootDResponses.hh" 00028 #include "XrdNet/XrdNetAddr.hh" 00029 00030 00031 namespace XrdCl 00032 { 00033 class AnyObject; 00034 class Tls; 00035 class AsyncSocketHandler; 00036 00037 //---------------------------------------------------------------------------- 00039 //---------------------------------------------------------------------------- 00040 class Socket 00041 { 00042 public: 00043 //------------------------------------------------------------------------ 00045 //------------------------------------------------------------------------ 00046 enum SocketStatus 00047 { 00048 Disconnected = 1, 00049 Connected = 2, 00050 Connecting = 3 00051 }; 00052 00053 //------------------------------------------------------------------------ 00058 //------------------------------------------------------------------------ 00059 Socket( int socket = -1, SocketStatus status = Disconnected ); 00060 00061 //------------------------------------------------------------------------ 00063 //------------------------------------------------------------------------ 00064 ~Socket(); 00065 00066 //------------------------------------------------------------------------ 00068 //------------------------------------------------------------------------ 00069 XRootDStatus Initialize( int family = AF_INET ); 00070 00071 //------------------------------------------------------------------------ 00073 //------------------------------------------------------------------------ 00074 XRootDStatus SetFlags( int flags ); 00075 00076 //------------------------------------------------------------------------ 00078 //------------------------------------------------------------------------ 00079 XRootDStatus GetFlags( int &flags ); 00080 00081 //------------------------------------------------------------------------ 00083 //------------------------------------------------------------------------ 00084 XRootDStatus GetSockOpt( int level, int optname, void *optval, 00085 socklen_t *optlen ); 00086 00087 //------------------------------------------------------------------------ 00089 //------------------------------------------------------------------------ 00090 XRootDStatus SetSockOpt( int level, int optname, const void *optval, 00091 socklen_t optlen ); 00092 00093 //------------------------------------------------------------------------ 00100 //------------------------------------------------------------------------ 00101 XRootDStatus Connect( const std::string &host, 00102 uint16_t port, 00103 uint16_t timout = 10 ); 00104 00105 //------------------------------------------------------------------------ 00111 //------------------------------------------------------------------------ 00112 XRootDStatus ConnectToAddress( const XrdNetAddr &addr, 00113 uint16_t timout = 10 ); 00114 00115 //------------------------------------------------------------------------ 00117 //------------------------------------------------------------------------ 00118 void Close(); 00119 00120 //------------------------------------------------------------------------ 00122 //------------------------------------------------------------------------ 00123 SocketStatus GetStatus() const 00124 { 00125 return pStatus; 00126 } 00127 00128 //------------------------------------------------------------------------ 00130 //------------------------------------------------------------------------ 00131 void SetStatus( SocketStatus status ) 00132 { 00133 pStatus = status; 00134 } 00135 00136 //------------------------------------------------------------------------ 00143 //------------------------------------------------------------------------ 00144 XRootDStatus ReadRaw( void *buffer, uint32_t size, int32_t timeout, 00145 uint32_t &bytesRead ); 00146 00147 //------------------------------------------------------------------------ 00154 //------------------------------------------------------------------------ 00155 XRootDStatus WriteRaw( void *buffer, uint32_t size, int32_t timeout, 00156 uint32_t &bytesWritten ); 00157 00158 //------------------------------------------------------------------------ 00164 //------------------------------------------------------------------------ 00165 XRootDStatus Send( const char *buffer, size_t size, int &bytesWritten ); 00166 00167 //---------------------------------------------------------------------------- 00177 //---------------------------------------------------------------------------- 00178 XRootDStatus Read( char *buffer, size_t size, int &bytesRead ); 00179 00180 //------------------------------------------------------------------------ 00182 //------------------------------------------------------------------------ 00183 int GetFD() 00184 { 00185 return pSocket; 00186 } 00187 00188 //------------------------------------------------------------------------ 00190 //------------------------------------------------------------------------ 00191 std::string GetSockName() const; 00192 00193 //------------------------------------------------------------------------ 00195 //------------------------------------------------------------------------ 00196 std::string GetPeerName() const; 00197 00198 //------------------------------------------------------------------------ 00200 //------------------------------------------------------------------------ 00201 std::string GetName() const; 00202 00203 //------------------------------------------------------------------------ 00205 //------------------------------------------------------------------------ 00206 const XrdNetAddr &GetServerAddress() const 00207 { 00208 return pServerAddr; 00209 } 00210 00211 //------------------------------------------------------------------------ 00214 //------------------------------------------------------------------------ 00215 void SetChannelID( AnyObject *channelID ) 00216 { 00217 pChannelID = channelID; 00218 } 00219 00220 //------------------------------------------------------------------------ 00223 //------------------------------------------------------------------------ 00224 const AnyObject* GetChannelID() const 00225 { 00226 return pChannelID; 00227 } 00228 00229 //------------------------------------------------------------------------ 00230 // Classify errno while reading/writing 00231 //------------------------------------------------------------------------ 00232 static XRootDStatus ClassifyErrno( int error ); 00233 00234 //------------------------------------------------------------------------ 00235 // Cork the underlying socket 00236 // 00237 // As there is no way to do vector writes with SSL/TLS we need to cork 00238 // the socket and then flash it when appropriate 00239 //------------------------------------------------------------------------ 00240 XRootDStatus Cork(); 00241 00242 //------------------------------------------------------------------------ 00243 // Uncork the underlying socket 00244 //------------------------------------------------------------------------ 00245 XRootDStatus Uncork(); 00246 00247 //------------------------------------------------------------------------ 00248 // Flash the underlying socket 00249 //------------------------------------------------------------------------ 00250 XRootDStatus Flash(); 00251 00252 //------------------------------------------------------------------------ 00253 // Check if the socket is corked 00254 //------------------------------------------------------------------------ 00255 inline bool IsCorked() const 00256 { 00257 return pCorked; 00258 } 00259 00260 //------------------------------------------------------------------------ 00261 // Do special event mapping if applicable 00262 //------------------------------------------------------------------------ 00263 uint8_t MapEvent( uint8_t event ); 00264 00265 //------------------------------------------------------------------------ 00266 // Enable encryption 00267 // 00268 // @param socketHandler : the socket handler that is handling the socket 00269 // @param the host : host name for verification 00270 //------------------------------------------------------------------------ 00271 XRootDStatus TlsHandShake( AsyncSocketHandler *socketHandler, 00272 const std::string &thehost = std::string() ); 00273 00274 //------------------------------------------------------------------------ 00275 // @return : true if socket is using TLS layer for encryption, 00276 // false otherwise 00277 //------------------------------------------------------------------------ 00278 bool IsEncrypted(); 00279 00280 protected: 00281 //------------------------------------------------------------------------ 00292 //------------------------------------------------------------------------ 00293 XRootDStatus Poll( bool readyForReading, bool readyForWriting, 00294 int32_t timeout ); 00295 00296 int pSocket; 00297 SocketStatus pStatus; 00298 XrdNetAddr pServerAddr; 00299 mutable std::string pSockName; // mutable because it's for caching 00300 mutable std::string pPeerName; 00301 mutable std::string pName; 00302 int pProtocolFamily; 00303 AnyObject *pChannelID; 00304 bool pCorked; 00305 00306 std::unique_ptr<Tls> pTls; 00307 }; 00308 } 00309 00310 #endif // __XRD_CL_SOCKET_HH__ 00311