Class: Jabber::Connection

Inherits:
Stream
  • Object
show all
Defined in:
lib/xmpp4r/connection.rb

Overview

The connection class manages the TCP connection to the Jabber server

Direct Known Subclasses

Client, Component

Constant Summary

Constants inherited from Stream

Stream::CONNECTED, Stream::DISCONNECTED

Instance Attribute Summary collapse

Attributes inherited from Stream

#fd, #status

Instance Method Summary collapse

Methods inherited from Stream

#add_iq_callback, #add_message_callback, #add_presence_callback, #add_stanza_callback, #add_xml_callback, #close, #close!, #delete_iq_callback, #delete_message_callback, #delete_presence_callback, #delete_stanza_callback, #delete_xml_callback, #is_connected?, #is_disconnected?, #on_exception, #parse_failure, #parser_end, #poll, #process, #receive, #send, #send_with_id, #stop, #wait_and_process

Constructor Details

#initialize(threaded = true) ⇒ Connection

Create a new connection to the given host and port, using threaded mode or not.



32
33
34
35
36
37
38
39
40
41
# File 'lib/xmpp4r/connection.rb', line 32

def initialize(threaded = true)
  super(threaded)
  @host = nil
  @port = nil
  @allow_tls = true
  @tls = false
  @ssl_capath = nil
  @ssl_verifycb = nil
  @features_timeout = 10
end

Instance Attribute Details

#allow_tlsObject

Allow TLS negotiation? Defaults to true



17
18
19
# File 'lib/xmpp4r/connection.rb', line 17

def allow_tls
  @allow_tls
end

#features_timeoutObject

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



21
22
23
# File 'lib/xmpp4r/connection.rb', line 21

def features_timeout
  @features_timeout
end

#hostObject (readonly)

Returns the value of attribute host



14
15
16
# File 'lib/xmpp4r/connection.rb', line 14

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port



14
15
16
# File 'lib/xmpp4r/connection.rb', line 14

def port
  @port
end

#ssl_capathObject

Optional CA-Path for TLS-handshake



24
25
26
# File 'lib/xmpp4r/connection.rb', line 24

def ssl_capath
  @ssl_capath
end

#ssl_verifycbObject

Optional callback for verification of SSL peer



27
28
29
# File 'lib/xmpp4r/connection.rb', line 27

def ssl_verifycb
  @ssl_verifycb
end

Instance Method Details

#accept_featuresObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/xmpp4r/connection.rb', line 59

def accept_features
  begin
    Timeout::timeout(@features_timeout) {
      Jabber::debuglog("FEATURES: waiting...")
      @features_lock.lock
      @features_lock.unlock
      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

#connect(host, port) ⇒ Object

Connects to the Jabber server through a TCP Socket and starts the Jabber parser.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xmpp4r/connection.rb', line 46

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)
  start

  accept_features
end

#is_tls?Boolean

Have we gone to TLS mode?

result
true

or [false]



148
149
150
# File 'lib/xmpp4r/connection.rb', line 148

def is_tls?
  @tls
end

#startObject

Start the parser on the previously connected socket



82
83
84
85
86
# File 'lib/xmpp4r/connection.rb', line 82

def start
  @features_lock.lock

  super(@socket)
end

#starttlsObject

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/xmpp4r/connection.rb', line 91

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 ErrorException(reply.first_element('error'))
  end
  # Don't be interrupted
  stop

  begin
    error = nil

    # Context/user set-able stuff
    ctx = OpenSSL::SSL::SSLContext.new('TLSv1')
    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 = OpenSSL::SSL::SSLSocket.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