Class: Jabber::Connection
Overview
The connection class manages the TCP connection to the Jabber server
Constant Summary
Constants inherited from Stream
Stream::CONNECTED, Stream::DISCONNECTED
Instance Attribute Summary collapse
-
#allow_tls ⇒ Object
Allow TLS negotiation? Defaults to true.
-
#features_timeout ⇒ Object
How many seconds to wait for <stream:features/> before proceeding.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ssl_capath ⇒ Object
Optional CA-Path for TLS-handshake.
-
#ssl_verifycb ⇒ Object
Optional callback for verification of SSL peer.
Attributes inherited from Stream
Instance Method Summary collapse
- #accept_features ⇒ Object
-
#connect(host, port) ⇒ Object
Connects to the Jabber server through a TCP Socket and starts the Jabber parser.
-
#initialize(threaded = true) ⇒ Connection
constructor
Create a new connection to the given host and port, using threaded mode or not.
-
#is_tls? ⇒ Boolean
- Have we gone to TLS mode? result
- true
-
or [false].
-
#start ⇒ Object
Start the parser on the previously connected socket.
-
#starttls ⇒ Object
Do a <starttls/> (will be automatically done by connect if stream peer supports this).
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_tls ⇒ Object
Allow TLS negotiation? Defaults to true
17 18 19 |
# File 'lib/xmpp4r/connection.rb', line 17 def allow_tls @allow_tls end |
#features_timeout ⇒ Object
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 |
#host ⇒ Object (readonly)
Returns the value of attribute host.
14 15 16 |
# File 'lib/xmpp4r/connection.rb', line 14 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
14 15 16 |
# File 'lib/xmpp4r/connection.rb', line 14 def port @port end |
#ssl_capath ⇒ Object
Optional CA-Path for TLS-handshake
24 25 26 |
# File 'lib/xmpp4r/connection.rb', line 24 def ssl_capath @ssl_capath end |
#ssl_verifycb ⇒ Object
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_features ⇒ Object
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 |
#start ⇒ Object
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 |
#starttls ⇒ Object
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 |