### Home Classes Methods

### In Files

- lib/xmpp4r/connection.rb

### Parent

Jabber::Stream

### Namespace

- CLASS Jabber::Connection::SSLSocketUTF8

### Methods

- ::new
- #accept_features
- #close!
- #connect
- #is_tls?
- #start
- #starttls

### Files

- CHANGELOG
- COPYING
- LICENSE
- README.rdoc
- README_ruby19.txt

### Class/Module Index

Quicksearch

- Jabber
- Jabber::ArgumentError
- Jabber::Bytestreams
- Jabber::Bytestreams::IBB
- Jabber::Bytestreams::IBBInitiator
- Jabber::Bytestreams::IBBQueueItem
- Jabber::Bytestreams::IBBTarget
- Jabber::Bytestreams::IqQueryBytestreams
- Jabber::Bytestreams::IqSi
- Jabber::Bytestreams::IqSiFile
- Jabber::Bytestreams::IqSiFileRange
- Jabber::Bytestreams::SOCKS5Bytestreams
- Jabber::Bytestreams::SOCKS5BytestreamsInitiator
- Jabber::Bytestreams::SOCKS5BytestreamsPeer
- Jabber::Bytestreams::SOCKS5BytestreamsServer
- Jabber::Bytestreams::SOCKS5BytestreamsServerStreamHost
- Jabber::Bytestreams::SOCKS5BytestreamsTarget
- Jabber::Bytestreams::SOCKS5Socket
- Jabber::Bytestreams::StreamHost
- Jabber::Bytestreams::StreamHostUsed
- Jabber::Callback
- Jabber::CallbackList
- Jabber::Caps
- Jabber::Caps::C
- Jabber::Caps::Helper
- Jabber::Client
- Jabber::ClientAuthenticationFailure
- Jabber::Command
- Jabber::Command::IqCommand
- Jabber::Command::Responder
- Jabber::Component
- Jabber::ComponentAuthenticationFailure
- Jabber::Connection
- Jabber::Connection::SSLSocketUTF8
- Jabber::Dataforms
- Jabber::Dataforms::XData
- Jabber::Dataforms::XDataField
- Jabber::Dataforms::XDataInstructions
- Jabber::Dataforms::XDataReported
- Jabber::Dataforms::XDataTitle
- Jabber::Delay
- Jabber::Delay::XDelay
- Jabber::Discovery
- Jabber::Discovery::Feature
- Jabber::Discovery::Helper
- Jabber::Discovery::Identity
- Jabber::Discovery::IqQueryDiscoInfo
- Jabber::Discovery::IqQueryDiscoItems
- Jabber::Discovery::Item
- Jabber::Discovery::Responder
- Jabber::EntityTime
- Jabber::EntityTime::IqTime
- Jabber::EntityTime::Responder
- Jabber::ErrorResponse
- Jabber::FeatureNegotiation
- Jabber::FeatureNegotiation::IqFeature
- Jabber::FileTransfer
- Jabber::FileTransfer::FileSource
- Jabber::FileTransfer::Helper
- Jabber::FileTransfer::TransferSource
- Jabber::Framework
- Jabber::Framework::Base
- Jabber::Framework::Bot
- Jabber::HTTPBinding
- Jabber::HTTPBinding::Client
- Jabber::IdGenerator
- Jabber::InvalidChatState
- Jabber::Iq
- Jabber::IqQuery
- Jabber::JID
- Jabber::JabberError
- Jabber::LastActivity
- Jabber::LastActivity::Helper
- Jabber::LastActivity::IqQueryLastActivity
- Jabber::MUC
- Jabber::MUC::IqQueryMUCAdmin
- Jabber::MUC::IqQueryMUCAdminItem
- Jabber::MUC::IqQueryMUCOwner
- Jabber::MUC::MUCBrowser
- Jabber::MUC::MUCClient
- Jabber::MUC::SimpleMUCClient
- Jabber::MUC::UserItem
- Jabber::MUC::XMUC
- Jabber::MUC::XMUCUser
- Jabber::MUC::XMUCUserInvite
- Jabber::MUC::XMUCUserItem
- Jabber::Message
- Jabber::NoNameXmlnsRegistered
- Jabber::NotConnected
- Jabber::Observable
- Jabber::Observable::Contact
- Jabber::Observable::PubSub
- Jabber::Observable::PubSub::AlreadySet
- Jabber::Observable::PubSub::NoService
- Jabber::Observable::Subscriptions
- Jabber::Presence
- Jabber::PubSub
- Jabber::PubSub::Configuration
- Jabber::PubSub::Event
- Jabber::PubSub::EventItem
- Jabber::PubSub::EventItems
- Jabber::PubSub::IqPubSub
- Jabber::PubSub::IqPubSubOwner
- Jabber::PubSub::Item
- Jabber::PubSub::Items
- Jabber::PubSub::NodeBrowser
- Jabber::PubSub::NodeConfig
- Jabber::PubSub::NodeHelper
- Jabber::PubSub::OAuthPubSubStreamHelper
- Jabber::PubSub::OAuthServiceHelper
- Jabber::PubSub::OwnerNodeConfig
- Jabber::PubSub::Publish
- Jabber::PubSub::Retract
- Jabber::PubSub::ServiceHelper
- Jabber::PubSub::Subscription
- Jabber::PubSub::SubscriptionConfig
- Jabber::PubSub::Unsubscribe
- Jabber::RPC
- Jabber::RPC::Client
- Jabber::RPC::IqQueryRPC
- Jabber::RPC::Server
- Jabber::Reliable
- Jabber::Reliable::Connection
- Jabber::Reliable::Listener
- Jabber::Roster
- Jabber::Roster::Helper
- Jabber::Roster::Helper::RosterItem
- Jabber::Roster::IqQueryRoster
- Jabber::Roster::RosterItem
- Jabber::Roster::RosterX
- Jabber::Roster::RosterXItem
- Jabber::Roster::XRoster
- Jabber::Roster::XRosterItem
- Jabber::SASL
- Jabber::SASL::Anonymous
- Jabber::SASL::Base
- Jabber::SASL::DigestMD5
- Jabber::SASL::Plain
- Jabber::SOCKS5Error
- Jabber::Semaphore
- Jabber::ServerDisconnected
- Jabber::Stream
- Jabber::Stream::ThreadBlock
- Jabber::StreamParser
- Jabber::Test
- Jabber::Test::ListenerMocker
- Jabber::UserLocation
- Jabber::UserLocation::Helper
- Jabber::UserLocation::Location
- Jabber::UserTune
- Jabber::UserTune::Helper
- Jabber::UserTune::Tune
- Jabber::Vcard
- Jabber::Vcard::Helper
- Jabber::Vcard::IqVcard
- Jabber::Version
- Jabber::Version::IqQueryVersion
- Jabber::Version::Responder
- Jabber::Version::SimpleResponder
- Jabber::X
- Jabber::XHTML
- Jabber::XHTML::Body
- Jabber::XHTML::HTML
- Jabber::XMPPElement
- Jabber::XMPPStanza
- Jabber::XParent
- REXML
- REXML::Element
- REXML::IOSource
- XMLRPC
- XMLRPC::Create
- Base64
- ObservableThing
- QObserver
- ThreadStore

No matching classes.

# Jabber::Connection

The connection class manages the TCP connection to the Jabber server

### Attributes

allow_tls[RW]

Allow TLS negotiation? Defaults to true

features_timeout[RW]

How many seconds to wait for <stream:features/> before proceeding

host[R]
keepalive_interval[RW]

Keep-alive interval in seconds, or nil to disable keepalive, defaults to 60 (see private method keepalive_loop for implementation details)

port[R]
ssl_capath[RW]

Optional CA-Path for TLS-handshake

ssl_verifycb[RW]

Optional callback for verification of SSL peer

use_ssl[RW]

whether to use the old and deprecated SSL protocol Defaults to false

### Public Class Methods

new() click to toggle source

Create a new connection to the given host and port

    # File lib/xmpp4r/connection.rb, line 51
    def initialize
      super()
      @host = nil
      @port = nil
      @allow_tls = defined? OpenSSL
      @tls = false
      @ssl_capath = nil
      @ssl_verifycb = nil
      @features_timeout = 10
      @keepalive_interval = 60
      @use_ssl = false
    end

### Public Instance Methods

accept_features() click to toggle source

    # File lib/xmpp4r/connection.rb, line 108
    def accept_features
      begin
        Timeout::timeout(@features_timeout) {
          Jabber::debuglog("FEATURES: waiting...")
          @features_sem.wait
          Jabber::debuglog("FEATURES: waiting finished")
        }
      rescue Timeout::Error
        Jabber::debuglog("FEATURES: timed out when waiting, stream peer seems not XMPP compliant")
      end

      if @allow_tls and not is_tls? and @stream_features['starttls'] == 'urn:ietf:params:xml:ns:xmpp-tls'
        begin
          starttls
        rescue
          Jabber::debuglog("STARTTLS:\nFailure: #{$!}")
        end
      end
    end

close!() click to toggle source

Closing connection: first kill keepaliveThread (but only if it's not me), then call Stream#close!

    # File lib/xmpp4r/connection.rb, line 102
    def close!
      @keepaliveThread.kill if @keepaliveThread and @keepaliveThread.alive? and @keepaliveThread != Thread.current
      super
      @keepaliveThread.kill if @keepaliveThread and @keepaliveThread.alive?
    end

connect(host, port) click to toggle source

Connect to the Jabber server through a TCP Socket, start the Jabber parser, invoke to accept_features to wait for TLS, start the keep-alive thread

    # File lib/xmpp4r/connection.rb, line 69
    def connect(host, port)
      @host = host
      @port = port
      # Reset is_tls?, so that it works when reconnecting
      @tls = false

      Jabber::debuglog("CONNECTING:\n#{@host}:#{@port}")
      @socket = TCPSocket.new(@host, @port)

      # We want to use the old and deprecated SSL protocol (usually on port 5223)
      if @use_ssl
        ssl = SSLSocketUTF8.new(@socket)
        ssl.connect # start SSL session
        ssl.sync_close = true
        Jabber::debuglog("SSL connection established.")
        @socket = ssl
      end

      start

      accept_features

      unless @keepalive_interval.nil?
        @keepaliveThread = Thread.new do
          Thread.current.abort_on_exception = true
          keepalive_loop
        end
      end
    end

is_tls?() click to toggle source

Have we gone to TLS mode?

result

true

  or [false]

    # File lib/xmpp4r/connection.rb, line 194
    def is_tls?
      @tls
    end

start() click to toggle source

Start the parser on the previously connected socket

    # File lib/xmpp4r/connection.rb, line 130
    def start
      super(@socket)
    end

starttls() click to toggle source

Do a <starttls/> (will be automatically done by connect if stream peer supports this)

    # File lib/xmpp4r/connection.rb, line 137
    def starttls
      stls = REXML::Element.new('starttls')
      stls.add_namespace('urn:ietf:params:xml:ns:xmpp-tls')

      reply = nil
      send(stls) { |r|
        reply = r
        true
      }
      if reply.name != 'proceed'
        raise ServerError.new(reply.first_element('error'))
      end
      # Don't be interrupted
      stop

      begin
        error = nil

        # Context/user set-able stuff
        ctx = OpenSSL::SSL::SSLContext.new
        if @ssl_capath
          ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
          ctx.ca_path = @ssl_capath
        else
          ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
        end
        ctx.verify_callback = @ssl_verifycb

        # SSL connection establishing
        sslsocket = SSLSocketUTF8.new(@socket, ctx)
        sslsocket.sync_close = true
        Jabber::debuglog("TLSv1: OpenSSL handshake in progress")
        sslsocket.connect

        # Make REXML believe it's a real socket
        class << sslsocket
          def kind_of?(o)
            o == IO ? true : super
          end
        end

        # We're done and will use it
        @tls = true
        @socket = sslsocket
      rescue
        error = $!
      ensure
        Jabber::debuglog("TLSv1: restarting parser")
        start
        accept_features
        raise error if error
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.