Logo
~Sockets~
~Examples~
~Contact~


TcpSocket Class Reference
[Basic sockets]

Socket implementation for TCP. More...

#include <TcpSocket.h>

Inheritance diagram for TcpSocket:
Collaboration diagram for TcpSocket:

List of all members.


Public Member Functions

 TcpSocket (ISocketHandler &)
 Constructor with standard values on input/output buffers.
 TcpSocket (ISocketHandler &h, size_t isize, size_t osize)
 Constructor with custom values for i/o buffer.
 ~TcpSocket ()
bool Open (ipaddr_t ip, port_t port, bool skip_socks=false)
 Open a connection to a remote server.
bool Open (SocketAddress &, bool skip_socks=false)
bool Open (SocketAddress &, SocketAddress &bind_address, bool skip_socks=false)
bool Open (const std::string &host, port_t port)
 Open connection.
void OnConnectTimeout ()
 Connect timeout callback.
int Close ()
 Close file descriptor - internal use only.
void Send (const std::string &s, int f=0)
 Send a string.
void Sendf (const char *format,...)
 Send string using printf formatting.
void SendBuf (const char *buf, size_t len, int f=0)
 Send buffer of bytes.
virtual void OnRawData (const char *buf, size_t len)
 This callback is executed after a successful read from the socket.
virtual void OnWriteComplete ()
 Called when output buffer has been sent.
size_t GetInputLength ()
 Number of bytes in input buffer.
size_t ReadInput (char *buf, size_t sz)
 Read from input buffer.
size_t GetOutputLength ()
 Number of bytes in output buffer.
void OnLine (const std::string &line)
 Callback fires when a socket in line protocol has read one full line.
uint64_t GetBytesReceived (bool clear=false)
 Get counter of number of bytes received.
uint64_t GetBytesSent (bool clear=false)
 Get counter of number of bytes sent.
void OnSocks4Connect ()
 Socks4 specific callback.
void OnSocks4ConnectFailed ()
 Socks4 specific callback.
bool OnSocks4Read ()
 Socks4 specific callback.
void OnResolved (int id, ipaddr_t a, port_t port)
 Callback executed when resolver thread has finished a resolve request.
void OnSSLConnect ()
 Callback for 'New' ssl support - replaces SSLSocket.
void OnSSLAccept ()
 Callback for 'New' ssl support - replaces SSLSocket.
virtual void InitSSLClient ()
 This method must be implemented to initialize the ssl context for an outgoing connection.
virtual void InitSSLServer ()
 This method must be implemented to initialize the ssl context for an incoming connection.
void SetReconnect (bool=true)
 Flag that says a broken connection will try to reconnect.
bool Reconnect ()
 Check reconnect on lost connection flag status.
void SetIsReconnect (bool x=true)
 Flag to determine if a reconnect is in progress.
bool IsReconnect ()
 Socket is reconnecting.
void DisableInputBuffer (bool=true)
 Use this if you only use OnRawData to process received data.
void OnOptions (int, int, int, SOCKET)
 Called when a client socket is created, to set socket options.
void SetLineProtocol (bool=true)
 Called after OnRead if socket is in line protocol mode.
const std::string & GetLine () const
 Get the unfinished line when using SetLineProtocol = true.
bool SetTcpNodelay (bool=true)
virtual int Protocol ()
 Returns IPPROTO_TCP or IPPROTO_SCTP.
void SetTransferLimit (size_t sz)
 Trigger limit for callback OnTransferLimit.
virtual void OnTransferLimit ()
 This callback fires when the output buffer drops below the value set by SetTransferLimit.

Protected Types

typedef std::list< OUTPUT * > output_l

Protected Member Functions

 TcpSocket (const TcpSocket &)
void OnRead ()
 Called when there is something to be read from the file descriptor.
void OnRead (char *buf, size_t n)
void OnWrite ()
 Called when there is room for another write on the file descriptor.
void InitializeContext (const std::string &context, const SSL_METHOD *meth_in=NULL)
 SSL; Initialize ssl context for a client socket.
void InitializeContext (const std::string &context, const std::string &keyfile, const std::string &password, const SSL_METHOD *meth_in=NULL)
 SSL; Initialize ssl context for a server socket.
void InitializeContext (const std::string &context, const std::string &certfile, const std::string &keyfile, const std::string &password, const SSL_METHOD *meth_in=NULL)
 SSL; Initialize ssl context for a server socket.
virtual SSL_CTX * GetSslContext ()
 SSL; Get pointer to ssl context structure.
virtual SSL * GetSsl ()
 SSL; Get pointer to ssl structure.
bool SSLNegotiate ()
 ssl; still negotiating connection.
const std::string & GetPassword ()
 SSL; Get ssl password.

Static Protected Member Functions

static int SSL_password_cb (char *buf, int num, int rwflag, void *userdata)
 SSL; Password callback method.

Protected Attributes

CircularBuffer ibuf
 Circular input buffer.

Private Member Functions

TcpSocketoperator= (const TcpSocket &)
int TryWrite (const char *buf, size_t len)
 the actual send()
void Buffer (const char *buf, size_t len)
 add data to output buffer top

Private Attributes

bool m_b_input_buffer_disabled
uint64_t m_bytes_sent
uint64_t m_bytes_received
bool m_skip_c
 Skip second char of CRLF or LFCR sequence in OnRead.
char m_c
 First char in CRLF or LFCR sequence.
std::string m_line
 Current line in line protocol mode.
char * m_buf
 temporary read buffer
output_l m_obuf
 output buffer
OUTPUTm_obuf_top
 output buffer on top
size_t m_transfer_limit
size_t m_output_length
SSL_CTX * m_ssl_ctx
 ssl context
SSL * m_ssl
 ssl 'socket'
BIO * m_sbio
 ssl bio
std::string m_password
 ssl password
int m_socks4_state
 socks4 support
char m_socks4_vn
 socks4 support, temporary variable
char m_socks4_cd
 socks4 support, temporary variable
unsigned short m_socks4_dstport
 socks4 support
unsigned long m_socks4_dstip
 socks4 support
int m_resolver_id
 Resolver id (if any) for current Open call.
bool m_b_reconnect
 Reconnect on lost connection flag.
bool m_b_is_reconnect
 Trying to reconnect.

Static Private Attributes

static SSLInitializer m_ssl_init
static Mutex m_server_ssl_mutex
static std::map< std::string,
SSL_CTX * > 
m_client_contexts
static std::map< std::string,
SSL_CTX * > 
m_server_contexts

Classes

class  CircularBuffer
 Buffer class containing one read/write circular buffer. More...
struct  OUTPUT
 Output buffer struct. More...

Detailed Description

Socket implementation for TCP.

Definition at line 60 of file TcpSocket.h.


Member Typedef Documentation

typedef std::list<OUTPUT *> TcpSocket::output_l [protected]

Definition at line 119 of file TcpSocket.h.


Constructor & Destructor Documentation

TcpSocket::TcpSocket ( ISocketHandler h  ) 

Constructor with standard values on input/output buffers.

Definition at line 80 of file TcpSocket.cpp.

00080                                       : StreamSocket(h)
00081 ,ibuf(TCP_BUFSIZE_READ)
00082 ,m_b_input_buffer_disabled(false)
00083 ,m_bytes_sent(0)
00084 ,m_bytes_received(0)
00085 ,m_skip_c(false)
00086 #ifdef SOCKETS_DYNAMIC_TEMP
00087 ,m_buf(new char[TCP_BUFSIZE_READ + 1])
00088 #endif
00089 ,m_obuf_top(NULL)
00090 ,m_transfer_limit(0)
00091 ,m_output_length(0)
00092 #ifdef HAVE_OPENSSL
00093 ,m_ssl_ctx(NULL)
00094 ,m_ssl(NULL)
00095 ,m_sbio(NULL)
00096 #endif
00097 #ifdef ENABLE_SOCKS4
00098 ,m_socks4_state(0)
00099 #endif
00100 #ifdef ENABLE_RESOLVER
00101 ,m_resolver_id(0)
00102 #endif
00103 #ifdef ENABLE_RECONNECT
00104 ,m_b_reconnect(false)
00105 ,m_b_is_reconnect(false)
00106 #endif
00107 {
00108 }

TcpSocket::TcpSocket ( ISocketHandler h,
size_t  isize,
size_t  osize 
)

Constructor with custom values for i/o buffer.

Parameters:
h ISocketHandler reference
isize Input buffer size
osize Output buffer size

Definition at line 117 of file TcpSocket.cpp.

00117                                                                 : StreamSocket(h)
00118 ,ibuf(isize)
00119 ,m_b_input_buffer_disabled(false)
00120 ,m_bytes_sent(0)
00121 ,m_bytes_received(0)
00122 ,m_skip_c(false)
00123 #ifdef SOCKETS_DYNAMIC_TEMP
00124 ,m_buf(new char[TCP_BUFSIZE_READ + 1])
00125 #endif
00126 ,m_obuf_top(NULL)
00127 ,m_transfer_limit(0)
00128 ,m_output_length(0)
00129 #ifdef HAVE_OPENSSL
00130 ,m_ssl_ctx(NULL)
00131 ,m_ssl(NULL)
00132 ,m_sbio(NULL)
00133 #endif
00134 #ifdef ENABLE_SOCKS4
00135 ,m_socks4_state(0)
00136 #endif
00137 #ifdef ENABLE_RESOLVER
00138 ,m_resolver_id(0)
00139 #endif
00140 #ifdef ENABLE_RECONNECT
00141 ,m_b_reconnect(false)
00142 ,m_b_is_reconnect(false)
00143 #endif
00144 {
00145 }

TcpSocket::~TcpSocket (  ) 

Definition at line 151 of file TcpSocket.cpp.

References m_buf, m_obuf, and m_ssl.

00152 {
00153 #ifdef SOCKETS_DYNAMIC_TEMP
00154         delete[] m_buf;
00155 #endif
00156         // %! empty m_obuf
00157         while (m_obuf.size())
00158         {
00159                 output_l::iterator it = m_obuf.begin();
00160                 OUTPUT *p = *it;
00161                 delete p;
00162                 m_obuf.erase(it);
00163         }
00164 #ifdef HAVE_OPENSSL
00165         if (m_ssl)
00166         {
00167                 SSL_free(m_ssl);
00168         }
00169 #endif
00170 }

TcpSocket::TcpSocket ( const TcpSocket s  )  [protected]

Definition at line 908 of file TcpSocket.cpp.

00909 :StreamSocket(s)
00910 ,ibuf(0)
00911 {
00912 }


Member Function Documentation

bool TcpSocket::Open ( ipaddr_t  ip,
port_t  port,
bool  skip_socks = false 
)

Open a connection to a remote server.

If you want your socket to connect to a server, always call Open before Add'ing a socket to the sockethandler. If not, the connection attempt will not be monitored by the socket handler...

Parameters:
ip IP address
port Port number
skip_socks Do not use socks4 even if configured

Definition at line 173 of file TcpSocket.cpp.

Referenced by HttpGetSocket::HttpGetSocket(), OnResolved(), Open(), HttpPutSocket::Open(), and HttpPostSocket::Open().

00174 {
00175         Ipv4Address ad(ip, port);
00176         Ipv4Address local;
00177         return Open(ad, local, skip_socks);
00178 }

bool TcpSocket::Open ( SocketAddress ad,
bool  skip_socks = false 
)

Definition at line 192 of file TcpSocket.cpp.

References Open().

00193 {
00194         Ipv4Address bind_ad("0.0.0.0", 0);
00195         return Open(ad, bind_ad, skip_socks);
00196 }

bool TcpSocket::Open ( SocketAddress ad,
SocketAddress bind_address,
bool  skip_socks = false 
)

Connecting();

Definition at line 199 of file TcpSocket.cpp.

References Socket::Attach(), closesocket, Socket::CopyConnection(), Socket::CreateSocket(), Errno, ISocketHandler::FindConnection(), SocketAddress::GetFamily(), SocketAddress::GetPort(), Socket::GetSocks4Host(), Socket::GetSocks4Port(), Socket::Handler(), INVALID_SOCKET, SocketAddress::IsValid(), Utility::l2ip(), Utility::l2string(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), Open(), Reconnect(), StreamSocket::SetCallOnConnect(), Socket::SetClientRemoteAddress(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), Socket::SetIsClient(), Socket::SetNonblocking(), Socket::SetRemoteAddress(), Socket::SetSocks4(), Socket::Socks4(), and StrError.

00200 {
00201         if (!ad.IsValid())
00202         {
00203                 Handler().LogError(this, "Open", 0, "Invalid SocketAddress", LOG_LEVEL_FATAL);
00204                 SetCloseAndDelete();
00205                 return false;
00206         }
00207         if (Handler().GetCount() >= FD_SETSIZE)
00208         {
00209                 Handler().LogError(this, "Open", 0, "no space left in fd_set", LOG_LEVEL_FATAL);
00210                 SetCloseAndDelete();
00211                 return false;
00212         }
00213         SetConnecting(false);
00214 #ifdef ENABLE_SOCKS4
00215         SetSocks4(false);
00216 #endif
00217         // check for pooling
00218 #ifdef ENABLE_POOL
00219         if (Handler().PoolEnabled())
00220         {
00221                 ISocketHandler::PoolSocket *pools = Handler().FindConnection(SOCK_STREAM, "tcp", ad);
00222                 if (pools)
00223                 {
00224                         CopyConnection( pools );
00225                         delete pools;
00226 
00227                         SetIsClient();
00228                         SetCallOnConnect(); // ISocketHandler must call OnConnect
00229                         Handler().LogError(this, "SetCallOnConnect", 0, "Found pooled connection", LOG_LEVEL_INFO);
00230                         return true;
00231                 }
00232         }
00233 #endif
00234         // if not, create new connection
00235         SOCKET s = CreateSocket(ad.GetFamily(), SOCK_STREAM, "tcp");
00236         if (s == INVALID_SOCKET)
00237         {
00238                 return false;
00239         }
00240         // socket must be nonblocking for async connect
00241         if (!SetNonblocking(true, s))
00242         {
00243                 SetCloseAndDelete();
00244                 closesocket(s);
00245                 return false;
00246         }
00247 #ifdef ENABLE_POOL
00248         SetIsClient(); // client because we connect
00249 #endif
00250         SetClientRemoteAddress(ad);
00251         int n = 0;
00252         if (bind_ad.GetPort() != 0)
00253         {
00254                 bind(s, bind_ad, bind_ad);
00255         }
00256 #ifdef ENABLE_SOCKS4
00257         if (!skip_socks && GetSocks4Host() && GetSocks4Port())
00258         {
00259                 Ipv4Address sa(GetSocks4Host(), GetSocks4Port());
00260                 {
00261                         std::string sockshost;
00262                         Utility::l2ip(GetSocks4Host(), sockshost);
00263                         Handler().LogError(this, "Open", 0, "Connecting to socks4 server @ " + sockshost + ":" +
00264                                 Utility::l2string(GetSocks4Port()), LOG_LEVEL_INFO);
00265                 }
00266                 SetSocks4();
00267                 n = connect(s, sa, sa);
00268                 SetRemoteAddress(sa);
00269         }
00270         else
00271 #endif
00272         {
00273                 n = connect(s, ad, ad);
00274                 SetRemoteAddress(ad);
00275         }
00276         if (n == -1)
00277         {
00278                 // check error code that means a connect is in progress
00279 #ifdef _WIN32
00280                 if (Errno == WSAEWOULDBLOCK)
00281 #else
00282                 if (Errno == EINPROGRESS)
00283 #endif
00284                 {
00285                         Attach(s);
00286                         SetConnecting( true ); // this flag will control fd_set's
00287                 }
00288                 else
00289 #ifdef ENABLE_SOCKS4
00290                 if (Socks4() && Handler().Socks4TryDirect() ) // retry
00291                 {
00292                         closesocket(s);
00293                         return Open(ad, true);
00294                 }
00295                 else
00296 #endif
00297 #ifdef ENABLE_RECONNECT
00298                 if (Reconnect())
00299                 {
00300                         Handler().LogError(this, "connect: failed, reconnect pending", Errno, StrError(Errno), LOG_LEVEL_INFO);
00301                         Attach(s);
00302                         SetConnecting( true ); // this flag will control fd_set's
00303                 }
00304                 else
00305 #endif
00306                 {
00307                         Handler().LogError(this, "connect: failed", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00308                         SetCloseAndDelete();
00309                         closesocket(s);
00310                         return false;
00311                 }
00312         }
00313         else
00314         {
00315                 Attach(s);
00316                 SetCallOnConnect(); // ISocketHandler must call OnConnect
00317         }
00318 
00319         // 'true' means connected or connecting(not yet connected)
00320         // 'false' means something failed
00321         return true; 
00322 }

bool TcpSocket::Open ( const std::string &  host,
port_t  port 
)

Open connection.

Parameters:
host Hostname
port Port number

Definition at line 325 of file TcpSocket.cpp.

References Socket::Handler(), Utility::isipv4(), Utility::isipv6(), m_resolver_id, Open(), Socket::Resolve(), Socket::SetCloseAndDelete(), and Utility::u2ip().

00326 {
00327 #ifdef ENABLE_IPV6
00328 #ifdef IPPROTO_IPV6
00329         if (IsIpv6())
00330         {
00331 #ifdef ENABLE_RESOLVER
00332                 if (!Handler().ResolverEnabled() || Utility::isipv6(host) )
00333                 {
00334 #endif
00335                         in6_addr a;
00336                         if (!Utility::u2ip(host, a))
00337                         {
00338                                 SetCloseAndDelete();
00339                                 return false;
00340                         }
00341                         Ipv6Address ad(a, port);
00342                         Ipv6Address local;
00343                         return Open(ad, local);
00344 #ifdef ENABLE_RESOLVER
00345                 }
00346                 m_resolver_id = Resolve6(host, port);
00347                 return true;
00348 #endif
00349         }
00350 #endif
00351 #endif
00352 #ifdef ENABLE_RESOLVER
00353         if (!Handler().ResolverEnabled() || Utility::isipv4(host) )
00354         {
00355 #endif
00356                 ipaddr_t l;
00357                 if (!Utility::u2ip(host,l))
00358                 {
00359                         SetCloseAndDelete();
00360                         return false;
00361                 }
00362                 Ipv4Address ad(l, port);
00363                 Ipv4Address local;
00364                 return Open(ad, local);
00365 #ifdef ENABLE_RESOLVER
00366         }
00367         // resolve using async resolver thread
00368         m_resolver_id = Resolve(host, port);
00369         return true;
00370 #endif
00371 }

void TcpSocket::OnConnectTimeout (  )  [virtual]

Connect timeout callback.

Todo:
state reason why connect failed

Todo:
state reason why connect failed

Reimplemented from Socket.

Definition at line 1664 of file TcpSocket.cpp.

References StreamSocket::GetConnectionRetries(), StreamSocket::GetConnectionRetry(), Socket::Handler(), StreamSocket::IncreaseConnectionRetries(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), Socket::OnConnectFailed(), Socket::OnConnectRetry(), OnSocks4ConnectFailed(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), StreamSocket::SetRetryClientConnect(), and Socket::Socks4().

01665 {
01666         Handler().LogError(this, "connect", -1, "connect timeout", LOG_LEVEL_FATAL);
01667 #ifdef ENABLE_SOCKS4
01668         if (Socks4())
01669         {
01670                 OnSocks4ConnectFailed();
01671                 // retry direct connection
01672         }
01673         else
01674 #endif
01675         if (GetConnectionRetry() == -1 ||
01676                 (GetConnectionRetry() && GetConnectionRetries() < GetConnectionRetry()) )
01677         {
01678                 IncreaseConnectionRetries();
01679                 // ask socket via OnConnectRetry callback if we should continue trying
01680                 if (OnConnectRetry())
01681                 {
01682                         SetRetryClientConnect();
01683                 }
01684                 else
01685                 {
01686                         SetCloseAndDelete( true );
01688                         OnConnectFailed();
01689                 }
01690         }
01691         else
01692         {
01693                 SetCloseAndDelete(true);
01695                 OnConnectFailed();
01696         }
01697         //
01698         SetConnecting(false);
01699 }

int TcpSocket::Close (  )  [virtual]

Close file descriptor - internal use only.

See also:
SetCloseAndDelete

Reimplemented from Socket.

Definition at line 1342 of file TcpSocket.cpp.

References Socket::Close(), Errno, StreamSocket::GetShutdown(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, Socket::IsConnected(), Socket::IsSSL(), LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, ISocketHandler::LogError(), Socket::Lost(), m_ssl, Socket::SetNonblocking(), and StrError.

01343 {
01344         if (GetSocket() == INVALID_SOCKET) // this could happen
01345         {
01346                 Handler().LogError(this, "Socket::Close", 0, "file descriptor invalid", LOG_LEVEL_WARNING);
01347                 return 0;
01348         }
01349         int n;
01350         SetNonblocking(true);
01351         if (!Lost() && IsConnected() && !(GetShutdown() & SHUT_WR))
01352         {
01353                 if (shutdown(GetSocket(), SHUT_WR) == -1)
01354                 {
01355                         // failed...
01356                         Handler().LogError(this, "shutdown", Errno, StrError(Errno), LOG_LEVEL_ERROR);
01357                 }
01358         }
01359         //
01360         char tmp[1000];
01361         if (!Lost() && (n = recv(GetSocket(),tmp,1000,0)) >= 0)
01362         {
01363                 if (n)
01364                 {
01365                         Handler().LogError(this, "read() after shutdown", n, "bytes read", LOG_LEVEL_WARNING);
01366                 }
01367         }
01368 #ifdef HAVE_OPENSSL
01369         if (IsSSL() && m_ssl)
01370                 SSL_shutdown(m_ssl);
01371         if (m_ssl)
01372         {
01373                 SSL_free(m_ssl);
01374                 m_ssl = NULL;
01375         }
01376 #endif
01377         return Socket::Close();
01378 }

void TcpSocket::Send ( const std::string &  s,
int  f = 0 
) [virtual]

void TcpSocket::Sendf ( const char *  format,
  ... 
)

Send string using printf formatting.

Definition at line 1029 of file TcpSocket.cpp.

References Send().

Referenced by HttpDebugSocket::OnFirst().

01030 {
01031         va_list ap;
01032         va_start(ap, format);
01033         char slask[5000]; // vsprintf / vsnprintf temporary
01034 #ifdef _WIN32
01035         vsprintf(slask, format, ap);
01036 #else
01037         vsnprintf(slask, 5000, format, ap);
01038 #endif
01039         va_end(ap);
01040         Send( slask );
01041 }

void TcpSocket::SendBuf ( const char *  buf,
size_t  len,
int  f = 0 
) [virtual]

Send buffer of bytes.

Parameters:
buf Buffer pointer
len Length of data
f Dummy flags -- not used

Reimplemented from Socket.

Definition at line 849 of file TcpSocket.cpp.

References Buffer(), Socket::CloseAndDelete(), StreamSocket::Connecting(), ISocketHandler::Get(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, Socket::IsConnected(), LOG_LEVEL_INFO, ISocketHandler::LogError(), m_obuf, m_obuf_top, StreamSocket::Ready(), Socket::Set(), and TryWrite().

Referenced by HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), HttpDebugSocket::OnData(), OnSocks4Connect(), HttpBaseSocket::OnTransferLimit(), Ajp13Socket::OnTransferLimit(), Ajp13Socket::ReceiveBody(), Ajp13Socket::Respond(), Send(), and HttpdSocket::Send64().

00850 {
00851         if (!Ready() && !Connecting())
00852         {
00853                 Handler().LogError(this, "SendBuf", -1, "Attempt to write to a non-ready socket" ); // warning
00854                 if (GetSocket() == INVALID_SOCKET)
00855                         Handler().LogError(this, "SendBuf", 0, " * GetSocket() == INVALID_SOCKET", LOG_LEVEL_INFO);
00856                 if (Connecting())
00857                         Handler().LogError(this, "SendBuf", 0, " * Connecting()", LOG_LEVEL_INFO);
00858                 if (CloseAndDelete())
00859                         Handler().LogError(this, "SendBuf", 0, " * CloseAndDelete()", LOG_LEVEL_INFO);
00860                 return;
00861         }
00862         if (!IsConnected())
00863         {
00864                 Handler().LogError(this, "SendBuf", -1, "Attempt to write to a non-connected socket, will be sent on connect" ); // warning
00865                 Buffer(buf, len);
00866                 return;
00867         }
00868         if (m_obuf_top)
00869         {
00870                 Buffer(buf, len);
00871                 return;
00872         }
00873         int n = TryWrite(buf, len);
00874         if (n >= 0 && n < (int)len)
00875         {
00876                 Buffer(buf + n, len - n);
00877         }
00878         // if ( data in buffer || !IsConnected )
00879         // {
00880         //   add to buffer
00881         // }
00882         // else
00883         // try_send
00884         // if any data is unsent, buffer it and set m_wfds
00885 
00886         // check output buffer set, set/reset m_wfds accordingly
00887         {
00888                 bool br;
00889                 bool bw;
00890                 bool bx;
00891                 Handler().Get(GetSocket(), br, bw, bx);
00892                 if (m_obuf.size())
00893                         Set(br, true);
00894                 else
00895                         Set(br, false);
00896         }
00897 }

void TcpSocket::OnRawData ( const char *  buf,
size_t  len 
) [virtual]

This callback is executed after a successful read from the socket.

Parameters:
buf Pointer to the data
len Length of the data

Reimplemented in AjpBaseSocket, and HTTPSocket.

Definition at line 1406 of file TcpSocket.cpp.

Referenced by OnRead().

01407 {
01408 }

void TcpSocket::OnWriteComplete (  )  [virtual]

Called when output buffer has been sent.

Note: Will only be called IF the output buffer has been used. Send's that was successful without needing the output buffer will not generate a call to this method.

Definition at line 630 of file TcpSocket.cpp.

Referenced by OnWrite().

00631 {
00632 }

size_t TcpSocket::GetInputLength (  ) 

Number of bytes in input buffer.

Definition at line 1411 of file TcpSocket.cpp.

References TcpSocket::CircularBuffer::GetLength(), and ibuf.

Referenced by OnRead(), OnSocks4Read(), and ReadInput().

01412 {
01413         return ibuf.GetLength();
01414 }

size_t TcpSocket::ReadInput ( char *  buf,
size_t  sz 
)

Read from input buffer.

Definition at line 1417 of file TcpSocket.cpp.

References GetInputLength(), ibuf, and TcpSocket::CircularBuffer::Read().

01418 {
01419         size_t sz = max_sz < GetInputLength() ? max_sz : GetInputLength();
01420         ibuf.Read(buf, sz);
01421         return sz;
01422 }

size_t TcpSocket::GetOutputLength (  ) 

Number of bytes in output buffer.

Definition at line 1425 of file TcpSocket.cpp.

References m_output_length.

Referenced by HttpBaseSocket::OnTransferLimit(), Ajp13Socket::OnTransferLimit(), OnWrite(), and SSLNegotiate().

01426 {
01427         return m_output_length;
01428 }

void TcpSocket::OnLine ( const std::string &  line  )  [virtual]

Callback fires when a socket in line protocol has read one full line.

Parameters:
line Line read

Reimplemented from Socket.

Reimplemented in HTTPSocket, ResolvSocket, and SmtpdSocket.

Definition at line 900 of file TcpSocket.cpp.

Referenced by OnRead().

00901 {
00902 }

uint64_t TcpSocket::GetBytesReceived ( bool  clear = false  )  [virtual]

Get counter of number of bytes received.

Reimplemented from Socket.

Definition at line 1431 of file TcpSocket.cpp.

References m_bytes_received.

01432 {
01433         uint64_t z = m_bytes_received;
01434         if (clear)
01435                 m_bytes_received = 0;
01436         return z;
01437 }

uint64_t TcpSocket::GetBytesSent ( bool  clear = false  )  [virtual]

Get counter of number of bytes sent.

Reimplemented from Socket.

Definition at line 1440 of file TcpSocket.cpp.

References m_bytes_sent.

01441 {
01442         uint64_t z = m_bytes_sent;
01443         if (clear)
01444                 m_bytes_sent = 0;
01445         return z;
01446 }

void TcpSocket::OnSocks4Connect (  )  [virtual]

Socks4 specific callback.

Todo:
warn

Todo:
warn

Reimplemented from Socket.

Definition at line 919 of file TcpSocket.cpp.

References Socket::GetClientRemoteAddress(), Socket::GetSocks4Userid(), m_socks4_state, and SendBuf().

00920 {
00921         char request[1000];
00922         memset(request, 0, sizeof(request));
00923         request[0] = 4; // socks v4
00924         request[1] = 1; // command code: CONNECT
00925         {
00926                 std::auto_ptr<SocketAddress> ad = GetClientRemoteAddress();
00927                 if (ad.get())
00928                 {
00929                         struct sockaddr *p0 = (struct sockaddr *)*ad;
00930                         struct sockaddr_in *p = (struct sockaddr_in *)p0;
00931                         if (p -> sin_family == AF_INET)
00932                         {
00933                                 memcpy(request + 2, &p -> sin_port, 2); // nwbo is ok here
00934                                 memcpy(request + 4, &p -> sin_addr, sizeof(struct in_addr));
00935                         }
00936                         else
00937                         {
00939                         }
00940                 }
00941                 else
00942                 {
00944                 }
00945         }
00946         strcpy(request + 8, GetSocks4Userid().c_str());
00947         size_t length = GetSocks4Userid().size() + 8 + 1;
00948         SendBuf(request, length);
00949         m_socks4_state = 0;
00950 }

void TcpSocket::OnSocks4ConnectFailed (  )  [virtual]

Socks4 specific callback.

Reimplemented from Socket.

Definition at line 953 of file TcpSocket.cpp.

References Socket::Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), Socket::OnConnectFailed(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), and StreamSocket::SetRetryClientConnect().

Referenced by OnConnectTimeout(), and OnWrite().

00954 {
00955         Handler().LogError(this,"OnSocks4ConnectFailed",0,"connection to socks4 server failed, trying direct connection",LOG_LEVEL_WARNING);
00956         if (!Handler().Socks4TryDirect())
00957         {
00958                 SetConnecting(false);
00959                 SetCloseAndDelete();
00960                 OnConnectFailed(); // just in case
00961         }
00962         else
00963         {
00964                 SetRetryClientConnect();
00965         }
00966 }

bool TcpSocket::OnSocks4Read (  )  [virtual]

Socks4 specific callback.

Returns:
'need_more'

Reimplemented from Socket.

Definition at line 969 of file TcpSocket.cpp.

References GetInputLength(), Socket::Handler(), ibuf, LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), m_socks4_cd, m_socks4_dstip, m_socks4_dstport, m_socks4_state, m_socks4_vn, Socket::OnConnect(), Socket::OnConnectFailed(), TcpSocket::CircularBuffer::Read(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), and Socket::SetSocks4().

Referenced by OnRead().

00970 {
00971         switch (m_socks4_state)
00972         {
00973         case 0:
00974                 ibuf.Read(&m_socks4_vn, 1);
00975                 m_socks4_state = 1;
00976                 break;
00977         case 1:
00978                 ibuf.Read(&m_socks4_cd, 1);
00979                 m_socks4_state = 2;
00980                 break;
00981         case 2:
00982                 if (GetInputLength() > 1)
00983                 {
00984                         ibuf.Read( (char *)&m_socks4_dstport, 2);
00985                         m_socks4_state = 3;
00986                 }
00987                 else
00988                 {
00989                         return true;
00990                 }
00991                 break;
00992         case 3:
00993                 if (GetInputLength() > 3)
00994                 {
00995                         ibuf.Read( (char *)&m_socks4_dstip, 4);
00996                         SetSocks4(false);
00997                         
00998                         switch (m_socks4_cd)
00999                         {
01000                         case 90:
01001                                 OnConnect();
01002                                 Handler().LogError(this, "OnSocks4Read", 0, "Connection established", LOG_LEVEL_INFO);
01003                                 break;
01004                         case 91:
01005                         case 92:
01006                         case 93:
01007                                 Handler().LogError(this,"OnSocks4Read",m_socks4_cd,"socks4 server reports connect failed",LOG_LEVEL_FATAL);
01008                                 SetConnecting(false);
01009                                 SetCloseAndDelete();
01010                                 OnConnectFailed();
01011                                 break;
01012                         default:
01013                                 Handler().LogError(this,"OnSocks4Read",m_socks4_cd,"socks4 server unrecognized response",LOG_LEVEL_FATAL);
01014                                 SetCloseAndDelete();
01015                                 break;
01016                         }
01017                 }
01018                 else
01019                 {
01020                         return true;
01021                 }
01022                 break;
01023         }
01024         return false;
01025 }

void TcpSocket::OnResolved ( int  id,
ipaddr_t  a,
port_t  port 
) [virtual]

Callback executed when resolver thread has finished a resolve request.

Reimplemented from Socket.

Definition at line 375 of file TcpSocket.cpp.

References ISocketHandler::Add(), DEB, Socket::Handler(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_resolver_id, Open(), and Socket::SetCloseAndDelete().

Referenced by ResolvSocket::OnLine().

00376 {
00377 DEB(    fprintf(stderr, "TcpSocket::OnResolved id %d addr %x port %d\n", id, a, port);)
00378         if (id == m_resolver_id)
00379         {
00380                 if (a && port)
00381                 {
00382                         Ipv4Address ad(a, port);
00383                         Ipv4Address local;
00384                         if (Open(ad, local))
00385                         {
00386                                 if (!Handler().Valid(this))
00387                                 {
00388                                         Handler().Add(this);
00389                                 }
00390                         }
00391                 }
00392                 else
00393                 {
00394                         Handler().LogError(this, "OnResolved", 0, "Resolver failed", LOG_LEVEL_FATAL);
00395                         SetCloseAndDelete();
00396                 }
00397         }
00398         else
00399         {
00400                 Handler().LogError(this, "OnResolved", id, "Resolver returned wrong job id", LOG_LEVEL_FATAL);
00401                 SetCloseAndDelete();
00402         }
00403 }

void TcpSocket::OnSSLConnect (  )  [virtual]

Callback for 'New' ssl support - replaces SSLSocket.

Internal use.

Reimplemented from Socket.

Definition at line 1045 of file TcpSocket.cpp.

References DEB, Socket::GetSocket(), InitSSLClient(), m_sbio, m_ssl, m_ssl_ctx, Socket::SetCloseAndDelete(), Socket::SetNonblocking(), Socket::SetSSLNegotiate(), and SSLNegotiate().

01046 {
01047         SetNonblocking(true);
01048         {
01049                 if (m_ssl_ctx)
01050                 {
01051 DEB(                    fprintf(stderr, "SSL Context already initialized - closing socket\n");)
01052                         SetCloseAndDelete(true);
01053                         return;
01054                 }
01055                 InitSSLClient();
01056         }
01057         if (m_ssl_ctx)
01058         {
01059                 /* Connect the SSL socket */
01060                 m_ssl = SSL_new(m_ssl_ctx);
01061                 if (!m_ssl)
01062                 {
01063 DEB(                    fprintf(stderr, " m_ssl is NULL\n");)
01064                         SetCloseAndDelete(true);
01065                         return;
01066                 }
01067                 SSL_set_mode(m_ssl, SSL_MODE_AUTO_RETRY);
01068                 m_sbio = BIO_new_socket((int)GetSocket(), BIO_NOCLOSE);
01069                 if (!m_sbio)
01070                 {
01071 DEB(                    fprintf(stderr, " m_sbio is NULL\n");)
01072                         SetCloseAndDelete(true);
01073                         return;
01074                 }
01075                 SSL_set_bio(m_ssl, m_sbio, m_sbio);
01076                 if (!SSLNegotiate())
01077                 {
01078                         SetSSLNegotiate();
01079                 }
01080         }
01081         else
01082         {
01083                 SetCloseAndDelete();
01084         }
01085 }

void TcpSocket::OnSSLAccept (  )  [virtual]

Callback for 'New' ssl support - replaces SSLSocket.

Internal use.

Reimplemented from Socket.

Definition at line 1088 of file TcpSocket.cpp.

References DEB, Socket::GetSocket(), InitSSLServer(), m_sbio, m_ssl, m_ssl_ctx, Socket::SetCloseAndDelete(), Socket::SetNonblocking(), Socket::SetSSLNegotiate(), and Socket::SetSSLServer().

01089 {
01090         SetNonblocking(true);
01091         {
01092                 if (m_ssl_ctx)
01093                 {
01094 DEB(                    fprintf(stderr, "SSL Context already initialized - closing socket\n");)
01095                         SetCloseAndDelete(true);
01096                         return;
01097                 }
01098                 InitSSLServer();
01099                 SetSSLServer();
01100         }
01101         if (m_ssl_ctx)
01102         {
01103                 m_ssl = SSL_new(m_ssl_ctx);
01104                 if (!m_ssl)
01105                 {
01106 DEB(                    fprintf(stderr, " m_ssl is NULL\n");)
01107                         SetCloseAndDelete(true);
01108                         return;
01109                 }
01110                 SSL_set_mode(m_ssl, SSL_MODE_AUTO_RETRY);
01111                 m_sbio = BIO_new_socket((int)GetSocket(), BIO_NOCLOSE);
01112                 if (!m_sbio)
01113                 {
01114 DEB(                    fprintf(stderr, " m_sbio is NULL\n");)
01115                         SetCloseAndDelete(true);
01116                         return;
01117                 }
01118                 SSL_set_bio(m_ssl, m_sbio, m_sbio);
01119 //              if (!SSLNegotiate())
01120                 {
01121                         SetSSLNegotiate();
01122                 }
01123         }
01124 }

void TcpSocket::InitSSLClient (  )  [virtual]

This method must be implemented to initialize the ssl context for an outgoing connection.

Definition at line 1224 of file TcpSocket.cpp.

References InitializeContext().

Referenced by OnSSLConnect().

01225 {
01226         InitializeContext("", SSLv23_method());
01227 }

void TcpSocket::InitSSLServer (  )  [virtual]

This method must be implemented to initialize the ssl context for an incoming connection.

Definition at line 1230 of file TcpSocket.cpp.

References Socket::Handler(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), and Socket::SetCloseAndDelete().

Referenced by OnSSLAccept().

01231 {
01232         Handler().LogError(this, "InitSSLServer", 0, "You MUST implement your own InitSSLServer method", LOG_LEVEL_FATAL);
01233         SetCloseAndDelete();
01234 }

void TcpSocket::SetReconnect ( bool  x = true  ) 

Flag that says a broken connection will try to reconnect.

Definition at line 1399 of file TcpSocket.cpp.

References m_b_reconnect.

01400 {
01401         m_b_reconnect = x;
01402 }

bool TcpSocket::Reconnect (  ) 

Check reconnect on lost connection flag status.

Definition at line 1450 of file TcpSocket.cpp.

References m_b_reconnect.

Referenced by Open().

01451 {
01452         return m_b_reconnect;
01453 }

void TcpSocket::SetIsReconnect ( bool  x = true  ) 

Flag to determine if a reconnect is in progress.

Definition at line 1456 of file TcpSocket.cpp.

References m_b_is_reconnect.

01457 {
01458         m_b_is_reconnect = x;
01459 }

bool TcpSocket::IsReconnect (  ) 

Socket is reconnecting.

Definition at line 1462 of file TcpSocket.cpp.

References m_b_is_reconnect.

Referenced by SSLNegotiate().

01463 {
01464         return m_b_is_reconnect;
01465 }

void TcpSocket::DisableInputBuffer ( bool  x = true  ) 

Use this if you only use OnRawData to process received data.

Definition at line 1477 of file TcpSocket.cpp.

References m_b_input_buffer_disabled.

Referenced by HTTPSocket::HTTPSocket(), and SetLineProtocol().

01478 {
01479         m_b_input_buffer_disabled = x;
01480 }

void TcpSocket::OnOptions ( int  family,
int  type,
int  protocol,
SOCKET  s 
) [virtual]

Called when a client socket is created, to set socket options.

Parameters:
family AF_INET, AF_INET6, etc
type SOCK_STREAM, SOCK_DGRAM, etc
protocol Protocol number (tcp, udp, sctp, etc)
s Socket file descriptor

Implements Socket.

Definition at line 1483 of file TcpSocket.cpp.

References DEB, Socket::SetSoKeepalive(), and Socket::SetSoReuseaddr().

01484 {
01485 DEB(    fprintf(stderr, "Socket::OnOptions()\n");)
01486 #ifdef SO_NOSIGPIPE
01487         SetSoNosigpipe(true);
01488 #endif
01489         SetSoReuseaddr(true);
01490         SetSoKeepalive(true);
01491 }

void TcpSocket::SetLineProtocol ( bool  x = true  )  [virtual]

Called after OnRead if socket is in line protocol mode.

See also:
SetLineProtocol Enable the OnLine callback. Do not create your own OnRead callback when using this.

Reimplemented from StreamSocket.

Definition at line 1494 of file TcpSocket.cpp.

References DisableInputBuffer(), and StreamSocket::SetLineProtocol().

Referenced by HTTPSocket::HTTPSocket(), HTTPSocket::OnLine(), HTTPSocket::OnRawData(), HTTPSocket::Reset(), ResolvSocket::ResolvSocket(), and SmtpdSocket::SmtpdSocket().

01495 {
01496         StreamSocket::SetLineProtocol(x);
01497         DisableInputBuffer(x);
01498 }

const std::string & TcpSocket::GetLine (  )  const

Get the unfinished line when using SetLineProtocol = true.

The finished line will always be reported with a call to OnLine.

Definition at line 1501 of file TcpSocket.cpp.

References m_line.

01502 {
01503         return m_line;
01504 }

bool TcpSocket::SetTcpNodelay ( bool  x = true  ) 

Definition at line 1507 of file TcpSocket.cpp.

References Errno, Socket::GetSocket(), Socket::Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01508 {
01509 #ifdef TCP_NODELAY
01510         int optval = x ? 1 : 0;
01511         if (setsockopt(GetSocket(), IPPROTO_TCP, TCP_NODELAY, (char *)&optval, sizeof(optval)) == -1)
01512         {
01513                 Handler().LogError(this, "setsockopt(IPPROTO_TCP, TCP_NODELAY)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01514                 return false;
01515         }
01516         return true;
01517 #else
01518         Handler().LogError(this, "socket option not available", 0, "TCP_NODELAY", LOG_LEVEL_INFO);
01519         return false;
01520 #endif
01521 }

int TcpSocket::Protocol (  )  [virtual]

Returns IPPROTO_TCP or IPPROTO_SCTP.

Implements StreamSocket.

Definition at line 1738 of file TcpSocket.cpp.

01739 {
01740         return IPPROTO_TCP;
01741 }

void TcpSocket::SetTransferLimit ( size_t  sz  ) 

Trigger limit for callback OnTransferLimit.

Definition at line 1744 of file TcpSocket.cpp.

References m_transfer_limit.

Referenced by HttpBaseSocket::OnTransferLimit(), and Ajp13Socket::OnTransferLimit().

01745 {
01746         m_transfer_limit = sz;
01747 }

void TcpSocket::OnTransferLimit (  )  [virtual]

This callback fires when the output buffer drops below the value set by SetTransferLimit.

Default: 0 (disabled).

Reimplemented in Ajp13Socket, and HttpBaseSocket.

Definition at line 1750 of file TcpSocket.cpp.

Referenced by OnWrite().

01751 {
01752 }

void TcpSocket::OnRead (  )  [protected, virtual]

Called when there is something to be read from the file descriptor.

Reimplemented from Socket.

Definition at line 434 of file TcpSocket.cpp.

References DEB, Errno, Socket::GetSocket(), Socket::GetTrafficMonitor(), Socket::Handler(), ibuf, Socket::IsSSL(), LOG_LEVEL_ERROR, LOG_LEVEL_FATAL, LOG_LEVEL_WARNING, ISocketHandler::LogError(), m_b_input_buffer_disabled, m_buf, m_bytes_received, m_ssl, Socket::OnDisconnect(), StreamSocket::Ready(), Socket::SetCloseAndDelete(), StreamSocket::SetFlushBeforeClose(), Socket::SetLost(), StreamSocket::SetShutdown(), StrError, TCP_BUFSIZE_READ, TCP_DISCONNECT_ERROR, TCP_DISCONNECT_SSL, and TcpSocket::CircularBuffer::Write().

Referenced by HTTPSocket::OnRawData().

00435 {
00436         int n = 0;
00437 #ifdef SOCKETS_DYNAMIC_TEMP
00438         char *buf = m_buf;
00439 #else
00440         char buf[TCP_BUFSIZE_READ];
00441 #endif
00442 #ifdef HAVE_OPENSSL
00443         if (IsSSL())
00444         {
00445                 if (!Ready())
00446                         return;
00447                 n = SSL_read(m_ssl, buf, TCP_BUFSIZE_READ);
00448                 if (n == -1)
00449                 {
00450                         n = SSL_get_error(m_ssl, n);
00451                         switch (n)
00452                         {
00453                         case SSL_ERROR_NONE:
00454                         case SSL_ERROR_WANT_READ:
00455                         case SSL_ERROR_WANT_WRITE:
00456                                 break;
00457                         case SSL_ERROR_ZERO_RETURN:
00458 DEB(                            fprintf(stderr, "SSL_read() returns zero - closing socket\n");)
00459                                 OnDisconnect();
00460                                 OnDisconnect(TCP_DISCONNECT_SSL|TCP_DISCONNECT_ERROR, n);
00461                                 SetCloseAndDelete(true);
00462                                 SetFlushBeforeClose(false);
00463                                 SetLost();
00464                                 break;
00465                         default:
00466 DEB(                            fprintf(stderr, "SSL read problem, errcode = %d\n",n);)
00467                                 OnDisconnect();
00468                                 OnDisconnect(TCP_DISCONNECT_SSL|TCP_DISCONNECT_ERROR, n);
00469                                 SetCloseAndDelete(true);
00470                                 SetFlushBeforeClose(false);
00471                                 SetLost();
00472                         }
00473                         return;
00474                 }
00475                 else
00476                 if (!n)
00477                 {
00478                         OnDisconnect();
00479                         OnDisconnect(TCP_DISCONNECT_SSL, 0);
00480                         SetCloseAndDelete(true);
00481                         SetFlushBeforeClose(false);
00482                         SetLost();
00483                         SetShutdown(SHUT_WR);
00484                         return;
00485                 }
00486                 else
00487                 if (n > 0 && n <= TCP_BUFSIZE_READ)
00488                 {
00489                         m_bytes_received += n;
00490                         if (GetTrafficMonitor())
00491                         {
00492                                 GetTrafficMonitor() -> fwrite(buf, 1, n);
00493                         }
00494                         if (!m_b_input_buffer_disabled && !ibuf.Write(buf,n))
00495                         {
00496                                 Handler().LogError(this, "OnRead(ssl)", 0, "ibuf overflow", LOG_LEVEL_WARNING);
00497                         }
00498                 }
00499                 else
00500                 {
00501                         Handler().LogError(this, "OnRead(ssl)", n, "abnormal value from SSL_read", LOG_LEVEL_ERROR);
00502                 }
00503         }
00504         else
00505 #endif // HAVE_OPENSSL
00506         {
00507                 n = recv(GetSocket(), buf, TCP_BUFSIZE_READ, MSG_NOSIGNAL);
00508                 if (n == -1)
00509                 {
00510                         Handler().LogError(this, "read", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00511                         OnDisconnect();
00512                         OnDisconnect(TCP_DISCONNECT_ERROR, Errno);
00513                         SetCloseAndDelete(true);
00514                         SetFlushBeforeClose(false);
00515                         SetLost();
00516                         return;
00517                 }
00518                 else
00519                 if (!n)
00520                 {
00521                         OnDisconnect();
00522                         OnDisconnect(0, 0);
00523                         SetCloseAndDelete(true);
00524                         SetFlushBeforeClose(false);
00525                         SetLost();
00526                         SetShutdown(SHUT_WR);
00527                         return;
00528                 }
00529                 else
00530                 if (n > 0 && n <= TCP_BUFSIZE_READ)
00531                 {
00532                         m_bytes_received += n;
00533                         if (GetTrafficMonitor())
00534                         {
00535                                 GetTrafficMonitor() -> fwrite(buf, 1, n);
00536                         }
00537                         if (!m_b_input_buffer_disabled && !ibuf.Write(buf,n))
00538                         {
00539                                 Handler().LogError(this, "OnRead", 0, "ibuf overflow", LOG_LEVEL_WARNING);
00540                         }
00541                 }
00542                 else
00543                 {
00544                         Handler().LogError(this, "OnRead", n, "abnormal value from recv", LOG_LEVEL_ERROR);
00545                 }
00546         }
00547         //
00548         OnRead( buf, n );
00549 }

void TcpSocket::OnRead ( char *  buf,
size_t  n 
) [protected]

Definition at line 552 of file TcpSocket.cpp.

References Socket::CloseAndDelete(), GetInputLength(), StreamSocket::LineProtocol(), m_b_input_buffer_disabled, m_c, m_line, m_skip_c, OnLine(), OnRawData(), OnSocks4Read(), Socket::Socks4(), and TCP_BUFSIZE_READ.

00553 {
00554         // unbuffered
00555         if (n > 0 && n <= TCP_BUFSIZE_READ)
00556         {
00557                 if (LineProtocol())
00558                 {
00559                         buf[n] = 0;
00560                         size_t i = 0;
00561                         if (m_skip_c && (buf[i] == 13 || buf[i] == 10) && buf[i] != m_c)
00562                         {
00563                                 m_skip_c = false;
00564                                 i++;
00565                         }
00566                         size_t x = i;
00567                         for (; i < n && LineProtocol(); i++)
00568                         {
00569                                 while ((buf[i] == 13 || buf[i] == 10) && LineProtocol())
00570                                 {
00571                                         char c = buf[i];
00572                                         buf[i] = 0;
00573                                         if (buf[x])
00574                                         {
00575                                                 m_line += (buf + x);
00576                                         }
00577                                         OnLine( m_line );
00578                                         i++;
00579                                         m_skip_c = true;
00580                                         m_c = c;
00581                                         if (i < n && (buf[i] == 13 || buf[i] == 10) && buf[i] != c)
00582                                         {
00583                                                 m_skip_c = false;
00584                                                 i++;
00585                                         }
00586                                         x = i;
00587                                         m_line = "";
00588                                 }
00589                                 if (!LineProtocol())
00590                                 {
00591                                         break;
00592                                 }
00593                         }
00594                         if (!LineProtocol())
00595                         {
00596                                 if (i < n)
00597                                 {
00598                                         OnRawData(buf + i, n - i);
00599                                 }
00600                         }
00601                         else
00602                         if (buf[x])
00603                         {
00604                                 m_line += (buf + x);
00605                         }
00606                 }
00607                 else
00608                 {
00609                         OnRawData(buf, n);
00610                 }
00611         }
00612         if (m_b_input_buffer_disabled)
00613         {
00614                 return;
00615         }
00616         // further processing: socks4
00617 #ifdef ENABLE_SOCKS4
00618         if (Socks4())
00619         {
00620                 bool need_more = false;
00621                 while (GetInputLength() && !need_more && !CloseAndDelete())
00622                 {
00623                         need_more = OnSocks4Read();
00624                 }
00625         }
00626 #endif
00627 }

void TcpSocket::OnWrite (  )  [protected, virtual]

Called when there is room for another write on the file descriptor.

Reimplemented from Socket.

Definition at line 635 of file TcpSocket.cpp.

References StreamSocket::Connecting(), ISocketHandler::Get(), StreamSocket::GetConnectionRetries(), StreamSocket::GetConnectionRetry(), GetOutputLength(), Socket::GetSocket(), Socket::Handler(), Socket::IsDisableRead(), LOG_LEVEL_ERROR, LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_obuf, m_obuf_top, m_output_length, m_transfer_limit, Socket::OnConnectFailed(), OnSocks4ConnectFailed(), OnTransferLimit(), OnWriteComplete(), Socket::Set(), StreamSocket::SetCallOnConnect(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), Socket::Socks4(), Socket::SoError(), StrError, and TryWrite().

Referenced by SSLNegotiate().

00636 {
00637         if (Connecting())
00638         {
00639                 int err = SoError();
00640 
00641                 // don't reset connecting flag on error here, we want the OnConnectFailed timeout later on
00642                 if (!err) // ok
00643                 {
00644                         Set(!IsDisableRead(), false);
00645                         SetConnecting(false);
00646                         SetCallOnConnect();
00647                         return;
00648                 }
00649                 Handler().LogError(this, "tcp: connect failed", err, StrError(err), LOG_LEVEL_FATAL);
00650                 Set(false, false); // no more monitoring because connection failed
00651 
00652                 // failed
00653 #ifdef ENABLE_SOCKS4
00654                 if (Socks4())
00655                 {
00656                         // %! leave 'Connecting' flag set?
00657                         OnSocks4ConnectFailed();
00658                         return;
00659                 }
00660 #endif
00661                 if (GetConnectionRetry() == -1 ||
00662                         (GetConnectionRetry() && GetConnectionRetries() < GetConnectionRetry()) )
00663                 {
00664                         // even though the connection failed at once, only retry after
00665                         // the connection timeout.
00666                         // should we even try to connect again, when CheckConnect returns
00667                         // false it's because of a connection error - not a timeout...
00668                         return;
00669                 }
00670                 SetConnecting(false);
00671                 SetCloseAndDelete( true );
00673                 OnConnectFailed();
00674                 return;
00675         }
00676         // try send next block in buffer
00677         // if full block is sent, repeat
00678         // if all blocks are sent, reset m_wfds
00679 
00680         bool repeat = false;
00681         size_t sz = m_transfer_limit ? GetOutputLength() : 0;
00682         do
00683         {
00684                 if (m_obuf.empty())
00685                 {
00686                         Handler().LogError(this, "OnWrite", m_output_length, "Empty output buffer in OnWrite", LOG_LEVEL_ERROR);
00687                         break;
00688                 }
00689                 output_l::iterator it = m_obuf.begin();
00690                 OUTPUT *p = *it;
00691                 repeat = false;
00692                 int n = TryWrite(p -> Buf(), p -> Len());
00693                 if (n > 0)
00694                 {
00695                         size_t left = p -> Remove(n);
00696                         m_output_length -= n;
00697                         if (!left)
00698                         {
00699                                 delete p;
00700                                 m_obuf.erase(it);
00701                                 if (!m_obuf.size())
00702                                 {
00703                                         m_obuf_top = NULL;
00704                                         OnWriteComplete();
00705                                 }
00706                                 else
00707                                 {
00708                                         repeat = true;
00709                                 }
00710                         }
00711                 }
00712         } while (repeat);
00713 
00714         if (m_transfer_limit && sz > m_transfer_limit && GetOutputLength() < m_transfer_limit)
00715         {
00716                 OnTransferLimit();
00717         }
00718 
00719         // check output buffer set, set/reset m_wfds accordingly
00720         {
00721                 bool br;
00722                 bool bw;
00723                 bool bx;
00724                 Handler().Get(GetSocket(), br, bw, bx);
00725                 if (m_obuf.size())
00726                         Set(br, true);
00727                 else
00728                         Set(br, false);
00729         }
00730 }

void TcpSocket::InitializeContext ( const std::string &  context,
const SSL_METHOD *  meth_in = NULL 
) [protected]

SSL; Initialize ssl context for a client socket.

Parameters:
meth_in SSL method

Definition at line 1237 of file TcpSocket.cpp.

References m_client_contexts, and m_ssl_ctx.

Referenced by InitSSLClient().

01238 {
01239         static Mutex mutex;
01240         Lock lock(mutex);
01241         /* Create our context*/
01242         if (m_client_contexts.find(context) == m_client_contexts.end())
01243         {
01244                 SSL_METHOD *meth = meth_in ? const_cast<SSL_METHOD *>(meth_in) : SSLv3_method();
01245                 m_ssl_ctx = m_client_contexts[context] = SSL_CTX_new(meth);
01246                 SSL_CTX_set_mode(m_ssl_ctx, SSL_MODE_AUTO_RETRY);
01247         }
01248         else
01249         {
01250                 m_ssl_ctx = m_client_contexts[context];
01251         }
01252 }

void TcpSocket::InitializeContext ( const std::string &  context,
const std::string &  keyfile,
const std::string &  password,
const SSL_METHOD *  meth_in = NULL 
) [protected]

SSL; Initialize ssl context for a server socket.

Parameters:
keyfile Combined private key/certificate file
password Password for private key
meth_in SSL method

Definition at line 1255 of file TcpSocket.cpp.

References Socket::Handler(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_password, m_server_contexts, m_server_ssl_mutex, m_ssl_ctx, and SSL_password_cb().

01256 {
01257         Lock lock(m_server_ssl_mutex);
01258         /* Create our context*/
01259         if (m_server_contexts.find(context) == m_server_contexts.end())
01260         {
01261                 SSL_METHOD *meth = meth_in ? const_cast<SSL_METHOD *>(meth_in) : SSLv3_method();
01262                 m_ssl_ctx = m_server_contexts[context] = SSL_CTX_new(meth);
01263                 SSL_CTX_set_mode(m_ssl_ctx, SSL_MODE_AUTO_RETRY);
01264                 // session id
01265                 if (context.size())
01266                         SSL_CTX_set_session_id_context(m_ssl_ctx, (const unsigned char *)context.c_str(), (unsigned int)context.size());
01267                 else
01268                         SSL_CTX_set_session_id_context(m_ssl_ctx, (const unsigned char *)"--empty--", 9);
01269         }
01270         else
01271         {
01272                 m_ssl_ctx = m_server_contexts[context];
01273         }
01274 
01275         /* Load our keys and certificates*/
01276         if (!(SSL_CTX_use_certificate_file(m_ssl_ctx, keyfile.c_str(), SSL_FILETYPE_PEM)))
01277         {
01278                 Handler().LogError(this, "TcpSocket InitializeContext", 0, "Couldn't read certificate file " + keyfile, LOG_LEVEL_FATAL);
01279         }
01280 
01281         m_password = password;
01282         SSL_CTX_set_default_passwd_cb(m_ssl_ctx, SSL_password_cb);
01283         SSL_CTX_set_default_passwd_cb_userdata(m_ssl_ctx, this);
01284         if (!(SSL_CTX_use_PrivateKey_file(m_ssl_ctx, keyfile.c_str(), SSL_FILETYPE_PEM)))
01285         {
01286                 Handler().LogError(this, "TcpSocket InitializeContext", 0, "Couldn't read private key file " + keyfile, LOG_LEVEL_FATAL);
01287         }
01288 }

void TcpSocket::InitializeContext ( const std::string &  context,
const std::string &  certfile,
const std::string &  keyfile,
const std::string &  password,
const SSL_METHOD *  meth_in = NULL 
) [protected]

SSL; Initialize ssl context for a server socket.

Parameters:
certfile Separate certificate file
keyfile Combined private key/certificate file
password Password for private key
meth_in SSL method

Definition at line 1291 of file TcpSocket.cpp.

References Socket::Handler(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_password, m_server_contexts, m_server_ssl_mutex, m_ssl_ctx, and SSL_password_cb().

01292 {
01293         Lock lock(m_server_ssl_mutex);
01294         /* Create our context*/
01295         if (m_server_contexts.find(context) == m_server_contexts.end())
01296         {
01297                 SSL_METHOD *meth = meth_in ? const_cast<SSL_METHOD *>(meth_in) : SSLv3_method();
01298                 m_ssl_ctx = m_server_contexts[context] = SSL_CTX_new(meth);
01299                 SSL_CTX_set_mode(m_ssl_ctx, SSL_MODE_AUTO_RETRY);
01300                 // session id
01301                 if (context.size())
01302                         SSL_CTX_set_session_id_context(m_ssl_ctx, (const unsigned char *)context.c_str(), (unsigned int)context.size());
01303                 else
01304                         SSL_CTX_set_session_id_context(m_ssl_ctx, (const unsigned char *)"--empty--", 9);
01305         }
01306         else
01307         {
01308                 m_ssl_ctx = m_server_contexts[context];
01309         }
01310 
01311         /* Load our keys and certificates*/
01312         if (!(SSL_CTX_use_certificate_file(m_ssl_ctx, certfile.c_str(), SSL_FILETYPE_PEM)))
01313         {
01314                 Handler().LogError(this, "TcpSocket InitializeContext", 0, "Couldn't read certificate file " + keyfile, LOG_LEVEL_FATAL);
01315         }
01316 
01317         m_password = password;
01318         SSL_CTX_set_default_passwd_cb(m_ssl_ctx, SSL_password_cb);
01319         SSL_CTX_set_default_passwd_cb_userdata(m_ssl_ctx, this);
01320         if (!(SSL_CTX_use_PrivateKey_file(m_ssl_ctx, keyfile.c_str(), SSL_FILETYPE_PEM)))
01321         {
01322                 Handler().LogError(this, "TcpSocket InitializeContext", 0, "Couldn't read private key file " + keyfile, LOG_LEVEL_FATAL);
01323         }
01324 }

int TcpSocket::SSL_password_cb ( char *  buf,
int  num,
int  rwflag,
void *  userdata 
) [static, protected]

SSL; Password callback method.

Definition at line 1327 of file TcpSocket.cpp.

References GetPassword().

Referenced by InitializeContext().

01328 {
01329         Socket *p0 = static_cast<Socket *>(userdata);
01330         TcpSocket *p = dynamic_cast<TcpSocket *>(p0);
01331         std::string pw = p ? p -> GetPassword() : "";
01332         if ( (size_t)num < pw.size() + 1)
01333         {
01334                 return 0;
01335         }
01336         strcpy(buf,pw.c_str());
01337         return (int)pw.size();
01338 }

SSL_CTX * TcpSocket::GetSslContext (  )  [protected, virtual]

SSL; Get pointer to ssl context structure.

Reimplemented from Socket.

Definition at line 1382 of file TcpSocket.cpp.

References Socket::Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), and m_ssl_ctx.

01383 {
01384         if (!m_ssl_ctx)
01385                 Handler().LogError(this, "GetSslContext", 0, "SSL Context is NULL; check InitSSLServer/InitSSLClient", LOG_LEVEL_WARNING);
01386         return m_ssl_ctx;
01387 }

SSL * TcpSocket::GetSsl (  )  [protected, virtual]

SSL; Get pointer to ssl structure.

Reimplemented from Socket.

Definition at line 1389 of file TcpSocket.cpp.

References Socket::Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), and m_ssl.

01390 {
01391         if (!m_ssl)
01392                 Handler().LogError(this, "GetSsl", 0, "SSL is NULL; check InitSSLServer/InitSSLClient", LOG_LEVEL_WARNING);
01393         return m_ssl;
01394 }

bool TcpSocket::SSLNegotiate (  )  [protected, virtual]

ssl; still negotiating connection.

Todo:
: resurrect certificate check... client

Todo:
: resurrect certificate check... server

Reimplemented from Socket.

Definition at line 1127 of file TcpSocket.cpp.

References DEB, GetOutputLength(), Socket::Handler(), IsReconnect(), Socket::IsSSLServer(), LOG_LEVEL_INFO, ISocketHandler::LogError(), m_ssl, Socket::OnAccept(), Socket::OnConnect(), Socket::OnReconnect(), Socket::OnSSLAcceptFailed(), Socket::OnSSLConnectFailed(), OnWrite(), Socket::SetCloseAndDelete(), Socket::SetConnected(), Socket::SetNonblocking(), and Socket::SetSSLNegotiate().

Referenced by OnSSLConnect().

01128 {
01129         if (!IsSSLServer()) // client
01130         {
01131                 int r = SSL_connect(m_ssl);
01132                 if (r > 0)
01133                 {
01134                         SetSSLNegotiate(false);
01136 //                      CheckCertificateChain( "");//ServerHOST);
01137                         SetNonblocking(false);
01138                         //
01139                         {
01140                                 SetConnected();
01141                                 if (GetOutputLength())
01142                                 {
01143                                         OnWrite();
01144                                 }
01145                         }
01146 #ifdef ENABLE_RECONNECT
01147                         if (IsReconnect())
01148                                 OnReconnect();
01149                         else
01150 #endif
01151                         {
01152                                 OnConnect();
01153                         }
01154                         Handler().LogError(this, "SSLNegotiate/SSL_connect", 0, "Connection established", LOG_LEVEL_INFO);
01155                         return true;
01156                 }
01157                 else
01158                 if (!r)
01159                 {
01160                         Handler().LogError(this, "SSLNegotiate/SSL_connect", 0, "Connection failed", LOG_LEVEL_INFO);
01161                         SetSSLNegotiate(false);
01162                         SetCloseAndDelete();
01163                         OnSSLConnectFailed();
01164                 }
01165                 else
01166                 {
01167                         r = SSL_get_error(m_ssl, r);
01168                         if (r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
01169                         {
01170                                 Handler().LogError(this, "SSLNegotiate/SSL_connect", -1, "Connection failed", LOG_LEVEL_INFO);
01171 DEB(                            fprintf(stderr, "SSL_connect() failed - closing socket, return code: %d\n",r);)
01172                                 SetSSLNegotiate(false);
01173                                 SetCloseAndDelete(true);
01174                                 OnSSLConnectFailed();
01175                         }
01176                 }
01177         }
01178         else // server
01179         {
01180                 int r = SSL_accept(m_ssl);
01181                 if (r > 0)
01182                 {
01183                         SetSSLNegotiate(false);
01185 //                      CheckCertificateChain( "");//ClientHOST);
01186                         SetNonblocking(false);
01187                         //
01188                         {
01189                                 SetConnected();
01190                                 if (GetOutputLength())
01191                                 {
01192                                         OnWrite();
01193                                 }
01194                         }
01195                         OnAccept();
01196                         Handler().LogError(this, "SSLNegotiate/SSL_accept", 0, "Connection established", LOG_LEVEL_INFO);
01197                         return true;
01198                 }
01199                 else
01200                 if (!r)
01201                 {
01202                         Handler().LogError(this, "SSLNegotiate/SSL_accept", 0, "Connection failed", LOG_LEVEL_INFO);
01203                         SetSSLNegotiate(false);
01204                         SetCloseAndDelete();
01205                         OnSSLAcceptFailed();
01206                 }
01207                 else
01208                 {
01209                         r = SSL_get_error(m_ssl, r);
01210                         if (r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
01211                         {
01212                                 Handler().LogError(this, "SSLNegotiate/SSL_accept", -1, "Connection failed", LOG_LEVEL_INFO);
01213 DEB(                            fprintf(stderr, "SSL_accept() failed - closing socket, return code: %d\n",r);)
01214                                 SetSSLNegotiate(false);
01215                                 SetCloseAndDelete(true);
01216                                 OnSSLAcceptFailed();
01217                         }
01218                 }
01219         }
01220         return false;
01221 }

const std::string & TcpSocket::GetPassword (  )  [protected]

SSL; Get ssl password.

Definition at line 1470 of file TcpSocket.cpp.

References m_password.

Referenced by SSL_password_cb().

01471 {
01472         return m_password;
01473 }

TcpSocket& TcpSocket::operator= ( const TcpSocket  )  [inline, private]

Definition at line 301 of file TcpSocket.h.

00301 { return *this; }

int TcpSocket::TryWrite ( const char *  buf,
size_t  len 
) [private]

the actual send()

Definition at line 733 of file TcpSocket.cpp.

References DEB, Errno, Socket::GetSocket(), Socket::GetTrafficMonitor(), Socket::Handler(), Socket::IsSSL(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_bytes_sent, m_ssl, Socket::OnDisconnect(), Socket::SetCloseAndDelete(), StreamSocket::SetFlushBeforeClose(), Socket::SetLost(), StrError, TCP_DISCONNECT_ERROR, TCP_DISCONNECT_SSL, and TCP_DISCONNECT_WRITE.

Referenced by OnWrite(), and SendBuf().

00734 {
00735         int n = 0;
00736 #ifdef HAVE_OPENSSL
00737         if (IsSSL())
00738         {
00739                 n = SSL_write(m_ssl, buf, (int)len);
00740                 if (n == -1)
00741                 {
00742                         int errnr = SSL_get_error(m_ssl, n);
00743                         if ( errnr != SSL_ERROR_WANT_READ && errnr != SSL_ERROR_WANT_WRITE )
00744                         {
00745                                 OnDisconnect();
00746                                 OnDisconnect(TCP_DISCONNECT_WRITE|TCP_DISCONNECT_ERROR|TCP_DISCONNECT_SSL, errnr);
00747                                 SetCloseAndDelete(true);
00748                                 SetFlushBeforeClose(false);
00749                                 SetLost();
00750                                 {
00751                                         char errbuf[256];
00752                                         ERR_error_string_n(errnr, errbuf, 256);
00753                                         Handler().LogError(this, "OnWrite/SSL_write", errnr, errbuf, LOG_LEVEL_FATAL);
00754                                 }
00755                         }
00756                         return 0;
00757                 }
00758                 else
00759                 if (!n)
00760                 {
00761                         OnDisconnect();
00762                         OnDisconnect(TCP_DISCONNECT_WRITE|TCP_DISCONNECT_SSL, 0);
00763                         SetCloseAndDelete(true);
00764                         SetFlushBeforeClose(false);
00765                         SetLost();
00766 DEB(                    {
00767                                 int errnr = SSL_get_error(m_ssl, n);
00768                                 char errbuf[256];
00769                                 ERR_error_string_n(errnr, errbuf, 256);
00770                                 fprintf(stderr, "SSL_write() returns 0: %d : %s\n",errnr, errbuf);
00771                         })
00772                 }
00773         }
00774         else
00775 #endif // HAVE_OPENSSL
00776         {
00777                 n = send(GetSocket(), buf, (int)len, MSG_NOSIGNAL);
00778                 if (n == -1)
00779                 {
00780                 // normal error codes:
00781                 // WSAEWOULDBLOCK
00782                 //       EAGAIN or EWOULDBLOCK
00783 #ifdef _WIN32
00784                         if (Errno != WSAEWOULDBLOCK)
00785 #else
00786                         if (Errno != EWOULDBLOCK)
00787 #endif
00788                         {       
00789                                 Handler().LogError(this, "send", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00790                                 OnDisconnect();
00791                                 OnDisconnect(TCP_DISCONNECT_WRITE|TCP_DISCONNECT_ERROR, Errno);
00792                                 SetCloseAndDelete(true);
00793                                 SetFlushBeforeClose(false);
00794                                 SetLost();
00795                         }
00796                         return 0;
00797                 }
00798         }
00799         if (n > 0)
00800         {
00801                 m_bytes_sent += n;
00802                 if (GetTrafficMonitor())
00803                 {
00804                         GetTrafficMonitor() -> fwrite(buf, 1, n);
00805                 }
00806         }
00807         return n;
00808 }

void TcpSocket::Buffer ( const char *  buf,
size_t  len 
) [private]

add data to output buffer top

Definition at line 811 of file TcpSocket.cpp.

References m_obuf, m_obuf_top, and m_output_length.

Referenced by SendBuf().

00812 {
00813         size_t ptr = 0;
00814         m_output_length += len;
00815         while (ptr < len)
00816         {
00817                 // buf/len => pbuf/sz
00818                 size_t space = 0;
00819                 if (m_obuf_top && (space = m_obuf_top -> Space()) > 0)
00820                 {
00821                         const char *pbuf = buf + ptr;
00822                         size_t sz = len - ptr;
00823                         if (space >= sz)
00824                         {
00825                                 m_obuf_top -> Add(pbuf, sz);
00826                                 ptr += sz;
00827                         }
00828                         else
00829                         {
00830                                 m_obuf_top -> Add(pbuf, space);
00831                                 ptr += space;
00832                         }
00833                 }
00834                 else
00835                 {
00836                         m_obuf_top = new OUTPUT;
00837                         m_obuf.push_back( m_obuf_top );
00838                 }
00839         }
00840 }


Member Data Documentation

Circular input buffer.

Definition at line 298 of file TcpSocket.h.

Referenced by GetInputLength(), OnRead(), OnSocks4Read(), and ReadInput().

Definition at line 309 of file TcpSocket.h.

Referenced by DisableInputBuffer(), and OnRead().

uint64_t TcpSocket::m_bytes_sent [private]

Definition at line 310 of file TcpSocket.h.

Referenced by GetBytesSent(), and TryWrite().

uint64_t TcpSocket::m_bytes_received [private]

Definition at line 311 of file TcpSocket.h.

Referenced by GetBytesReceived(), and OnRead().

bool TcpSocket::m_skip_c [private]

Skip second char of CRLF or LFCR sequence in OnRead.

Definition at line 312 of file TcpSocket.h.

Referenced by OnRead().

char TcpSocket::m_c [private]

First char in CRLF or LFCR sequence.

Definition at line 313 of file TcpSocket.h.

Referenced by OnRead().

std::string TcpSocket::m_line [private]

Current line in line protocol mode.

Reimplemented in HTTPSocket.

Definition at line 314 of file TcpSocket.h.

Referenced by GetLine(), and OnRead().

char* TcpSocket::m_buf [private]

temporary read buffer

Definition at line 316 of file TcpSocket.h.

Referenced by OnRead(), and ~TcpSocket().

output buffer

Definition at line 318 of file TcpSocket.h.

Referenced by Buffer(), OnWrite(), SendBuf(), and ~TcpSocket().

output buffer on top

Definition at line 319 of file TcpSocket.h.

Referenced by Buffer(), OnWrite(), and SendBuf().

size_t TcpSocket::m_transfer_limit [private]

Definition at line 320 of file TcpSocket.h.

Referenced by OnWrite(), and SetTransferLimit().

size_t TcpSocket::m_output_length [private]

Definition at line 321 of file TcpSocket.h.

Referenced by Buffer(), GetOutputLength(), and OnWrite().

Definition at line 324 of file TcpSocket.h.

SSL_CTX* TcpSocket::m_ssl_ctx [private]

ssl context

Definition at line 325 of file TcpSocket.h.

Referenced by GetSslContext(), InitializeContext(), OnSSLAccept(), and OnSSLConnect().

SSL* TcpSocket::m_ssl [private]

ssl 'socket'

Definition at line 326 of file TcpSocket.h.

Referenced by Close(), GetSsl(), OnRead(), OnSSLAccept(), OnSSLConnect(), SSLNegotiate(), TryWrite(), and ~TcpSocket().

BIO* TcpSocket::m_sbio [private]

ssl bio

Definition at line 327 of file TcpSocket.h.

Referenced by OnSSLAccept(), and OnSSLConnect().

std::string TcpSocket::m_password [private]

ssl password

Definition at line 328 of file TcpSocket.h.

Referenced by GetPassword(), and InitializeContext().

Definition at line 329 of file TcpSocket.h.

Referenced by InitializeContext().

std::map< std::string, SSL_CTX * > TcpSocket::m_client_contexts [static, private]

Definition at line 330 of file TcpSocket.h.

Referenced by InitializeContext().

std::map< std::string, SSL_CTX * > TcpSocket::m_server_contexts [static, private]

Definition at line 331 of file TcpSocket.h.

Referenced by InitializeContext().

socks4 support

Definition at line 335 of file TcpSocket.h.

Referenced by OnSocks4Connect(), and OnSocks4Read().

char TcpSocket::m_socks4_vn [private]

socks4 support, temporary variable

Definition at line 336 of file TcpSocket.h.

Referenced by OnSocks4Read().

char TcpSocket::m_socks4_cd [private]

socks4 support, temporary variable

Definition at line 337 of file TcpSocket.h.

Referenced by OnSocks4Read().

unsigned short TcpSocket::m_socks4_dstport [private]

socks4 support

Definition at line 338 of file TcpSocket.h.

Referenced by OnSocks4Read().

unsigned long TcpSocket::m_socks4_dstip [private]

socks4 support

Definition at line 339 of file TcpSocket.h.

Referenced by OnSocks4Read().

int TcpSocket::m_resolver_id [private]

Resolver id (if any) for current Open call.

Definition at line 343 of file TcpSocket.h.

Referenced by OnResolved(), and Open().

bool TcpSocket::m_b_reconnect [private]

Reconnect on lost connection flag.

Definition at line 347 of file TcpSocket.h.

Referenced by Reconnect(), and SetReconnect().

Trying to reconnect.

Definition at line 348 of file TcpSocket.h.

Referenced by IsReconnect(), and SetIsReconnect().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4