Logo
~Sockets~
~Examples~
~Contact~


TcpSocket.h

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004-2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about 
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029 */
00030 #ifndef _SOCKETS_TcpSocket_H
00031 #define _SOCKETS_TcpSocket_H
00032 #include "sockets-config.h"
00033 #include "Socket.h"
00034 #include "CircularBuffer.h"
00035 #ifdef HAVE_OPENSSL
00036 #include <openssl/ssl.h>
00037 #include "SSLInitializer.h"
00038 #endif
00039 
00040 
00041 #define TCP_BUFSIZE_READ 16400
00042 
00043 
00044 #ifdef SOCKETS_NAMESPACE
00045 namespace SOCKETS_NAMESPACE {
00046 #endif
00047 
00048 class SocketAddress;
00049 
00050 
00053 class TcpSocket : public Socket
00054 {
00057         struct MES {
00058                 MES( const char *buf_in,size_t len_in)
00059                 :buf(new  char[len_in])
00060                 ,len(len_in)
00061                 ,ptr(0)
00062                 {
00063                         memcpy(buf,buf_in,len);
00064                 }
00065                 ~MES() { delete[] buf; }
00066                 size_t left() { return len - ptr; }
00067                  char *curbuf() { return buf + ptr; }
00068                  char *buf;
00069                 size_t len;
00070                 size_t ptr;
00071         };
00073         typedef std::list<MES *> ucharp_v;
00074 public:
00076         TcpSocket(ISocketHandler& );
00081         TcpSocket(ISocketHandler& h,size_t isize,size_t osize);
00082         ~TcpSocket();
00083 
00092         bool Open(ipaddr_t ip,port_t port,bool skip_socks = false);
00093 #ifdef ENABLE_IPV6
00094 #ifdef IPPROTO_IPV6
00095 
00099         bool Open(in6_addr ip,port_t port,bool skip_socks = false);
00100 #endif
00101 #endif
00102         bool Open(SocketAddress&,bool skip_socks = false);
00103         bool Open(SocketAddress&,SocketAddress& bind_address,bool skip_socks = false);
00107         bool Open(const std::string &host,port_t port);
00110         int Close();
00111 
00115         void Send(const std::string &s,int f = 0);
00117         void Sendf(const char *format, ...);
00122         void SendBuf(const char *buf,size_t len,int f = 0);
00126         virtual void OnRawData(const char *buf,size_t len);
00127 
00129         size_t GetInputLength();
00131         size_t GetOutputLength();
00132 
00135         void OnLine(const std::string& line);
00137         uint64_t GetBytesReceived(bool clear = false);
00139         uint64_t GetBytesSent(bool clear = false);
00140 
00142         void OnSocks4Connect();
00144         void OnSocks4ConnectFailed();
00147         bool OnSocks4Read();
00148 
00149 #ifdef ENABLE_RESOLVER
00150 
00151         void OnResolved(int id,ipaddr_t a,port_t port);
00152 #ifdef ENABLE_IPV6
00153         void OnResolved(int id,in6_addr& a,port_t port);
00154 #endif
00155 #endif
00156 #ifdef HAVE_OPENSSL
00157 
00158         void OnSSLConnect();
00160         void OnSSLAccept();
00163         virtual void InitSSLClient();
00166         virtual void InitSSLServer();
00167 #endif
00168 
00169 #ifdef ENABLE_RECONNECT
00170 
00171         void SetReconnect(bool = true);
00173         bool Reconnect();
00175         void SetIsReconnect(bool x = true);
00177         bool IsReconnect();
00178 #endif
00179 
00180         void DisableInputBuffer(bool = true);
00181 
00182         void OnOptions(int,int,int,SOCKET);
00183 
00184         void SetLineProtocol(bool = true);
00185 
00186 protected:
00187         TcpSocket(const TcpSocket& s);
00188         void OnRead();
00189         void OnWrite();
00190 #ifdef HAVE_OPENSSL
00191 
00193         void InitializeContext(const std::string& context, SSL_METHOD *meth_in = NULL);
00198         void InitializeContext(const std::string& context, const std::string& keyfile, const std::string& password, SSL_METHOD *meth_in = NULL);
00200 static  int SSL_password_cb(char *buf,int num,int rwflag,void *userdata);
00202         virtual SSL_CTX *GetSslContext();
00204         virtual SSL *GetSsl();
00206         bool SSLNegotiate();
00208         const std::string& GetPassword();
00209 #endif
00210 
00211         CircularBuffer ibuf; 
00212         CircularBuffer obuf; 
00213 
00214 private:
00215         TcpSocket& operator=(const TcpSocket& ) { return *this; }
00216         bool m_b_input_buffer_disabled;
00217         uint64_t m_bytes_sent;
00218         uint64_t m_bytes_received;
00219         bool m_skip_c; 
00220         char m_c; 
00221         std::string m_line; 
00222         ucharp_v m_mes; 
00223 #ifdef SOCKETS_DYNAMIC_TEMP
00224         char *m_buf; 
00225 #endif
00226 
00227 #ifdef HAVE_OPENSSL
00228 static  SSLInitializer m_ssl_init;
00229         SSL_CTX *m_ssl_ctx; 
00230         SSL *m_ssl; 
00231         BIO *m_sbio; 
00232         std::string m_password; 
00233 #endif
00234 
00235 #ifdef ENABLE_SOCKS4
00236         int m_socks4_state; 
00237         char m_socks4_vn; 
00238         char m_socks4_cd; 
00239         unsigned short m_socks4_dstport; 
00240         unsigned long m_socks4_dstip; 
00241 #endif
00242 
00243 #ifdef ENABLE_RESOLVER
00244         int m_resolver_id; 
00245 #endif
00246 
00247 #ifdef ENABLE_RECONNECT
00248         bool m_b_reconnect; 
00249         bool m_b_is_reconnect; 
00250 #endif
00251 
00252 };
00253 
00254 
00255 #ifdef SOCKETS_NAMESPACE
00256 }
00257 #endif
00258 
00259 #endif // _SOCKETS_TcpSocket_H
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4