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 #ifndef _SOCKETS_TcpSocket_H
00031 #define _SOCKETS_TcpSocket_H
00032 #include "sockets-config.h"
00033 #include "StreamSocket.h"
00034 #ifdef HAVE_OPENSSL
00035 #include <openssl/ssl.h>
00036 #include "SSLInitializer.h"
00037 #endif
00038 #include "Mutex.h"
00039 #include <map>
00040
00041
00042 #define TCP_BUFSIZE_READ 16400
00043 #define TCP_OUTPUT_CAPACITY 1024000
00044
00045
00046 #define TCP_DISCONNECT_WRITE 1
00047 #define TCP_DISCONNECT_ERROR 2
00048 #define TCP_DISCONNECT_SSL 4
00049
00050
00051 #ifdef SOCKETS_NAMESPACE
00052 namespace SOCKETS_NAMESPACE {
00053 #endif
00054
00055 class SocketAddress;
00056
00057
00060 class TcpSocket : public StreamSocket
00061 {
00063 protected:
00066 class CircularBuffer
00067 {
00068 public:
00069 CircularBuffer(size_t size);
00070 ~CircularBuffer();
00071
00073 bool Write(const char *p,size_t l);
00075 bool Read(char *dest,size_t l);
00077 bool Remove(size_t l);
00079 std::string ReadString(size_t l);
00080
00082 size_t GetLength();
00084 const char *GetStart();
00086 size_t GetL();
00088 size_t Space();
00089
00091 unsigned long ByteCounter(bool clear = false);
00092
00093 private:
00094 CircularBuffer(const CircularBuffer& s) {}
00095 CircularBuffer& operator=(const CircularBuffer& ) { return *this; }
00096 char *buf;
00097 size_t m_max;
00098 size_t m_q;
00099 size_t m_b;
00100 size_t m_t;
00101 unsigned long m_count;
00102 };
00105 struct OUTPUT {
00106 OUTPUT();
00107 OUTPUT(const char *buf, size_t len);
00108 size_t Space();
00109 void Add(const char *buf, size_t len);
00110 size_t Remove(size_t len);
00111 const char *Buf();
00112 size_t Len();
00113
00114 size_t _b;
00115 size_t _t;
00116 size_t _q;
00117 char _buf[TCP_OUTPUT_CAPACITY];
00118 };
00119 typedef std::list<OUTPUT *> output_l;
00120
00121 public:
00123 TcpSocket(ISocketHandler& );
00128 TcpSocket(ISocketHandler& h,size_t isize,size_t osize);
00129 ~TcpSocket();
00130
00139 bool Open(ipaddr_t ip,port_t port,bool skip_socks = false);
00140 #ifdef ENABLE_IPV6
00141 #ifdef IPPROTO_IPV6
00142
00146 bool Open(in6_addr ip,port_t port,bool skip_socks = false);
00147 #endif
00148 #endif
00149 bool Open(SocketAddress&,bool skip_socks = false);
00150 bool Open(SocketAddress&,SocketAddress& bind_address,bool skip_socks = false);
00154 bool Open(const std::string &host,port_t port);
00155
00157 void OnConnectTimeout();
00158 #ifdef _WIN32
00159
00160 void OnException();
00161 #endif
00162
00165 int Close();
00166
00170 void Send(const std::string &s,int f = 0);
00172 void Sendf(const char *format, ...);
00177 void SendBuf(const char *buf,size_t len,int f = 0);
00181 virtual void OnRawData(const char *buf,size_t len);
00182
00187 virtual void OnWriteComplete();
00189 size_t GetInputLength();
00191 size_t ReadInput(char *buf, size_t sz);
00193 size_t GetOutputLength();
00194
00197 void OnLine(const std::string& line);
00199 uint64_t GetBytesReceived(bool clear = false);
00201 uint64_t GetBytesSent(bool clear = false);
00202
00204 void OnSocks4Connect();
00206 void OnSocks4ConnectFailed();
00209 bool OnSocks4Read();
00210
00211 #ifdef ENABLE_RESOLVER
00212
00213 void OnResolved(int id,ipaddr_t a,port_t port);
00214 #ifdef ENABLE_IPV6
00215 void OnResolved(int id,in6_addr& a,port_t port);
00216 #endif
00217 #endif
00218 #ifdef HAVE_OPENSSL
00219
00220 void OnSSLConnect();
00222 void OnSSLAccept();
00225 virtual void InitSSLClient();
00228 virtual void InitSSLServer();
00229 #endif
00230
00231 #ifdef ENABLE_RECONNECT
00232
00233 void SetReconnect(bool = true);
00235 bool Reconnect();
00237 void SetIsReconnect(bool x = true);
00239 bool IsReconnect();
00240 #endif
00241
00243 void DisableInputBuffer(bool = true);
00244
00245 void OnOptions(int,int,int,SOCKET);
00246
00247 void SetLineProtocol(bool = true);
00248
00252 const std::string& GetLine() const;
00253
00254
00255 bool SetTcpNodelay(bool = true);
00256
00257 virtual int Protocol();
00258
00260 void SetTransferLimit(size_t sz);
00263 virtual void OnTransferLimit();
00264
00265 protected:
00266 TcpSocket(const TcpSocket& );
00267
00268 void OnRead();
00269 void OnRead( char *buf, size_t n );
00270 void OnWrite();
00271 #ifdef HAVE_OPENSSL
00272
00274 void InitializeContext(const std::string& context, SSL_METHOD *meth_in = NULL);
00279 void InitializeContext(const std::string& context, const std::string& keyfile, const std::string& password, SSL_METHOD *meth_in = NULL);
00285 void InitializeContext(const std::string& context, const std::string& certfile, const std::string& keyfile, const std::string& password, SSL_METHOD *meth_in = NULL);
00287 static int SSL_password_cb(char *buf,int num,int rwflag,void *userdata);
00289 virtual SSL_CTX *GetSslContext();
00291 virtual SSL *GetSsl();
00293 bool SSLNegotiate();
00295 const std::string& GetPassword();
00296 #endif
00297
00298 CircularBuffer ibuf;
00299
00300 private:
00301 TcpSocket& operator=(const TcpSocket& ) { return *this; }
00302
00304 int TryWrite(const char *buf, size_t len);
00306 void Buffer(const char *buf, size_t len);
00307
00308
00309 bool m_b_input_buffer_disabled;
00310 uint64_t m_bytes_sent;
00311 uint64_t m_bytes_received;
00312 bool m_skip_c;
00313 char m_c;
00314 std::string m_line;
00315 #ifdef SOCKETS_DYNAMIC_TEMP
00316 char *m_buf;
00317 #endif
00318 output_l m_obuf;
00319 OUTPUT *m_obuf_top;
00320 size_t m_transfer_limit;
00321 size_t m_output_length;
00322
00323 #ifdef HAVE_OPENSSL
00324 static SSLInitializer m_ssl_init;
00325 SSL_CTX *m_ssl_ctx;
00326 SSL *m_ssl;
00327 BIO *m_sbio;
00328 std::string m_password;
00329 static Mutex m_server_ssl_mutex;
00330 static std::map<std::string, SSL_CTX *> m_client_contexts;
00331 static std::map<std::string, SSL_CTX *> m_server_contexts;
00332 #endif
00333
00334 #ifdef ENABLE_SOCKS4
00335 int m_socks4_state;
00336 char m_socks4_vn;
00337 char m_socks4_cd;
00338 unsigned short m_socks4_dstport;
00339 unsigned long m_socks4_dstip;
00340 #endif
00341
00342 #ifdef ENABLE_RESOLVER
00343 int m_resolver_id;
00344 #endif
00345
00346 #ifdef ENABLE_RECONNECT
00347 bool m_b_reconnect;
00348 bool m_b_is_reconnect;
00349 #endif
00350
00351 };
00352
00353
00354 #ifdef SOCKETS_NAMESPACE
00355 }
00356 #endif
00357
00358 #endif // _SOCKETS_TcpSocket_H
00359