00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _SOCKETS_TcpSocket_H
00033 #define _SOCKETS_TcpSocket_H
00034 #include "sockets-config.h"
00035 #include "StreamSocket.h"
00036 #ifdef HAVE_OPENSSL
00037 #include <openssl/ssl.h>
00038 #include "SSLInitializer.h"
00039 #endif
00040 #include "Mutex.h"
00041 #include <map>
00042
00043
00044 #define TCP_BUFSIZE_READ 16400
00045 #define TCP_OUTPUT_CAPACITY 1024000
00046
00047
00048 #define TCP_DISCONNECT_WRITE 1
00049 #define TCP_DISCONNECT_ERROR 2
00050 #define TCP_DISCONNECT_SSL 4
00051
00052
00053 #ifdef SOCKETS_NAMESPACE
00054 namespace SOCKETS_NAMESPACE {
00055 #endif
00056
00057 class SocketAddress;
00058
00059
00062 class TcpSocket : public StreamSocket
00063 {
00065 protected:
00068 class CircularBuffer
00069 {
00070 public:
00071 CircularBuffer(size_t size);
00072 ~CircularBuffer();
00073
00075 bool Write(const char *p,size_t l);
00077 bool Read(char *dest,size_t l);
00079 bool Remove(size_t l);
00081 std::string ReadString(size_t l);
00082
00084 size_t GetLength();
00086 const char *GetStart();
00088 size_t GetL();
00090 size_t Space();
00091
00093 unsigned long ByteCounter(bool clear = false);
00094
00095 private:
00096 CircularBuffer(const CircularBuffer& ) {}
00097 CircularBuffer& operator=(const CircularBuffer& ) { return *this; }
00098 char *buf;
00099 size_t m_max;
00100 size_t m_q;
00101 size_t m_b;
00102 size_t m_t;
00103 unsigned long m_count;
00104 };
00107 struct OUTPUT {
00108 OUTPUT();
00109 OUTPUT(const char *buf, size_t len);
00110 size_t Space();
00111 void Add(const char *buf, size_t len);
00112 size_t Remove(size_t len);
00113 const char *Buf();
00114 size_t Len();
00115
00116 size_t _b;
00117 size_t _t;
00118 size_t _q;
00119 char _buf[TCP_OUTPUT_CAPACITY];
00120 };
00121 typedef std::list<OUTPUT *> output_l;
00122
00123 public:
00125 TcpSocket(ISocketHandler& );
00130 TcpSocket(ISocketHandler& h,size_t isize,size_t osize);
00131 ~TcpSocket();
00132
00141 bool Open(ipaddr_t ip,port_t port,bool skip_socks = false);
00142 #ifdef ENABLE_IPV6
00143 #ifdef IPPROTO_IPV6
00144
00148 bool Open(in6_addr ip,port_t port,bool skip_socks = false);
00149 #endif
00150 #endif
00151 bool Open(SocketAddress&,bool skip_socks = false);
00152 bool Open(SocketAddress&,SocketAddress& bind_address,bool skip_socks = false);
00156 bool Open(const std::string &host,port_t port);
00157
00159 void OnConnectTimeout();
00160 #ifdef _WIN32
00161
00162 void OnException();
00163 #endif
00164
00167 int Close();
00168
00172 void Send(const std::string &s,int f = 0);
00174 void Sendf(const char *format, ...);
00179 void SendBuf(const char *buf,size_t len,int f = 0);
00183 virtual void OnRawData(const char *buf,size_t len);
00184
00189 virtual void OnWriteComplete();
00191 size_t GetInputLength();
00193 size_t ReadInput(char *buf, size_t sz);
00195 size_t GetOutputLength();
00196
00199 void OnLine(const std::string& line);
00201 uint64_t GetBytesReceived(bool clear = false);
00203 uint64_t GetBytesSent(bool clear = false);
00204
00206 void OnSocks4Connect();
00208 void OnSocks4ConnectFailed();
00211 bool OnSocks4Read();
00212
00213 #ifdef ENABLE_RESOLVER
00214
00215 void OnResolved(int id,ipaddr_t a,port_t port);
00216 #ifdef ENABLE_IPV6
00217 void OnResolved(int id,in6_addr& a,port_t port);
00218 #endif
00219 #endif
00220 #ifdef HAVE_OPENSSL
00221
00222 void OnSSLConnect();
00224 void OnSSLAccept();
00227 virtual void InitSSLClient();
00230 virtual void InitSSLServer();
00231 #endif
00232
00233 #ifdef ENABLE_RECONNECT
00234
00235 void SetReconnect(bool = true);
00237 bool Reconnect();
00239 void SetIsReconnect(bool x = true);
00241 bool IsReconnect();
00242 #endif
00243
00245 void DisableInputBuffer(bool = true);
00246
00247 void OnOptions(int,int,int,SOCKET);
00248
00249 void SetLineProtocol(bool = true);
00250
00254 const std::string GetLine() const;
00255
00256
00257 bool SetTcpNodelay(bool = true);
00258
00259 virtual int Protocol();
00260
00262 void SetTransferLimit(size_t sz);
00265 virtual void OnTransferLimit();
00266
00267 protected:
00268 TcpSocket(const TcpSocket& );
00269
00270 void OnRead();
00271 void OnRead( char *buf, size_t n );
00272 void OnWrite();
00273 #ifdef HAVE_OPENSSL
00274
00276 void InitializeContext(const std::string& context, const SSL_METHOD *meth_in = NULL);
00281 void InitializeContext(const std::string& context, const std::string& keyfile, const std::string& password, const SSL_METHOD *meth_in = NULL);
00287 void InitializeContext(const std::string& context, const std::string& certfile, const std::string& keyfile, const std::string& password, const SSL_METHOD *meth_in = NULL);
00289 void UseCertificateChainFile(const std::string& filename);
00291 static int SSL_password_cb(char *buf,int num,int rwflag,void *userdata);
00293 virtual SSL_CTX *GetSslContext();
00295 virtual SSL *GetSsl();
00297 bool SSLNegotiate();
00299 const std::string& GetPassword();
00300 #endif
00301
00302 CircularBuffer ibuf;
00303
00304 private:
00305 TcpSocket& operator=(const TcpSocket& ) { return *this; }
00306
00308 void SendFromOutputBuffer();
00310 int TryWrite(const char *buf, size_t len);
00312 void Buffer(const char *buf, size_t len);
00313
00314
00315 bool m_b_input_buffer_disabled;
00316 uint64_t m_bytes_sent;
00317 uint64_t m_bytes_received;
00318 bool m_skip_c;
00319 char m_c;
00320 std::vector<char> m_line;
00321 size_t m_line_ptr;
00322 #ifdef SOCKETS_DYNAMIC_TEMP
00323 char *m_buf;
00324 #endif
00325 output_l m_obuf;
00326 OUTPUT *m_obuf_top;
00327 size_t m_transfer_limit;
00328 size_t m_output_length;
00329 size_t m_repeat_length;
00330
00331 #ifdef HAVE_OPENSSL
00332 static SSLInitializer m_ssl_init;
00333 SSL_CTX *m_ssl_ctx;
00334 SSL *m_ssl;
00335 BIO *m_sbio;
00336 std::string m_password;
00337 static Mutex m_server_ssl_mutex;
00338 static std::map<std::string, SSL_CTX *> m_client_contexts;
00339 static std::map<std::string, SSL_CTX *> m_server_contexts;
00340 #endif
00341
00342 #ifdef ENABLE_SOCKS4
00343 int m_socks4_state;
00344 char m_socks4_vn;
00345 char m_socks4_cd;
00346 unsigned short m_socks4_dstport;
00347 unsigned long m_socks4_dstip;
00348 #endif
00349
00350 #ifdef ENABLE_RESOLVER
00351 int m_resolver_id;
00352 #endif
00353
00354 #ifdef ENABLE_RECONNECT
00355 bool m_b_reconnect;
00356 bool m_b_is_reconnect;
00357 #endif
00358
00359 };
00360
00361
00362 #ifdef SOCKETS_NAMESPACE
00363 }
00364 #endif
00365
00366 #endif // _SOCKETS_TcpSocket_H
00367