Logo Search packages:      
Sourcecode: znc version File versions

bool Csock::Connect ( const CS_STRING &  sBindHost = "",
bool  bSkipSetup = false 
) [virtual]

Create the connection

Parameters:
sBindHost the ip you want to bind to locally
bSkipSetup if true, setting up the vhost etc is skipped
Returns:
true on success

Definition at line 483 of file Csocket.cpp.

References DNSLookup(), OUTBOUND, and SetupVHost().

Referenced by ConnectSSL().

{
      // bind to a hostname if requested
      m_sBindHost = sBindHost;
      if ( !bSkipSetup )
      {
            int iDNSRet = ETIMEDOUT;
            while( true )
            {
                  iDNSRet = DNSLookup( DNS_VHOST );
                  if ( iDNSRet == EAGAIN )
                        continue;

                  break;
            }
            if ( iDNSRet != 0 )
                  return( false );

            if ( !sBindHost.empty() )
            {
                  // try to bind 3 times, otherwise exit failure
                  bool bBound = false;
                  for( int a = 0; a < 3 && !bBound; a++ )
                  {
                        if ( SetupVHost() )
                              bBound = true;
#ifdef _WIN32
                        Sleep( 5000 );
#else
                        usleep( 5000 );   // quick pause, common lets BIND!)(!*!
#endif /* _WIN32 */
                  }

                  if ( !bBound )
                  {
                        CS_DEBUG( "Failure to bind to " << sBindHost );
                        return( false );
                  }
            }
      }

      // set it none blocking
#ifdef _WIN32
      u_long iOpts = 1;
      ioctlsocket( m_iReadSock, FIONBIO, &iOpts );
#else
      int fdflags = fcntl (m_iReadSock, F_GETFL, 0);
      fcntl( m_iReadSock, F_SETFL, fdflags|O_NONBLOCK );
#endif /* _WIN32 */

      m_iConnType = OUTBOUND;

      int ret = -1;
      if( !GetIPv6() )
            ret = connect( m_iReadSock, (struct sockaddr *)m_address.GetSockAddr(), m_address.GetSockAddrLen() );
#ifdef HAVE_IPV6
      else
            ret = connect( m_iReadSock, (struct sockaddr *)m_address.GetSockAddr6(), m_address.GetSockAddrLen6() );
#endif /* HAVE_IPV6 */
#ifndef _WIN32
      if ( ( ret == -1 ) && ( GetSockError() != EINPROGRESS ) )
#else
      if ( ( ret == -1 ) && ( GetSockError() != EINPROGRESS ) && ( GetSockError() != WSAEWOULDBLOCK ) )
#endif /* _WIN32 */

      {
            CS_DEBUG( "Connect Failed. ERRNO [" << GetSockError() << "] FD [" << m_iReadSock << "]" );
            return( false );
      }

      if ( m_bBLOCK )
      {
#ifdef _WIN32
            u_long iOpts = 0;
            ioctlsocket( m_iReadSock, FIONBIO, &iOpts );
#else
            // unset the flags afterwords, rather than have connect block
            int fdflags = fcntl (m_iReadSock, F_GETFL, 0);
            fdflags &= ~O_NONBLOCK;
            fcntl( m_iReadSock, F_SETFL, fdflags );
#endif /* _WIN32 */
      }

      if ( m_eConState != CST_OK )
            m_eConState = CST_OK;

      return( true );
}


Generated by  Doxygen 1.6.0   Back to index