Class: Arachni::Reactor::Connection
- Inherits:
-
Object
- Object
- Arachni::Reactor::Connection
- Includes:
- Callbacks
- Defined in:
- lib/arachni/reactor/connection.rb,
lib/arachni/reactor/connection/tls.rb,
lib/arachni/reactor/connection/error.rb,
lib/arachni/reactor/connection/callbacks.rb,
lib/arachni/reactor/connection/peer_info.rb
Overview
Defined Under Namespace
Modules: Callbacks, PeerInfo, TLS Classes: Error
Constant Summary collapse
- BLOCK_SIZE =
Maximum amount of data to be written or read at a time.
We set this to the same max block size as the OpenSSL buffers because more than this tends to cause SSL errors and broken #select behavior – 1024 * 16 at the time of writing.
OpenSSL::Buffering::BLOCK_SIZE
Instance Attribute Summary collapse
-
#reactor ⇒ Reactor
Reactor associated with this connection.
-
#role ⇒ Symbol
readonly
‘:client` or `:server`.
-
#socket ⇒ Socket
readonly
Ruby ‘Socket` associated with this connection.
Instance Method Summary collapse
- #_connect ⇒ Object
-
#_read ⇒ Object
Processes a ‘read` event for this connection.
-
#_write ⇒ Integer
Processes a ‘write` event for this connection.
-
#accept ⇒ Connection?
Accepts a new client connection.
-
#attach(reactor) ⇒ Bool
‘true` if the connection was attached, `nil` if the connection was already attached.
-
#attached? ⇒ Bool
‘true` if the connection is #attached? to a #reactor, `false` otherwise.
-
#close(reason = nil) ⇒ Object
Closes the connection and detaches it from the Arachni::Reactor.
-
#close_without_callback ⇒ Object
Closes the connection and detaches it from the Arachni::Reactor.
-
#closed? ⇒ Bool
‘true` if the connection has been closed, `false` otherwise.
- #configure(options = {}) ⇒ Object
- #connected? ⇒ Boolean
- #detach ⇒ Bool
-
#detached? ⇒ Bool
‘true` if the connection is not #attached? to a #reactor, `false` otherwise.
-
#has_outgoing_data? ⇒ Bool
‘true` if the connection has outgoing data that have not yet been written, `false` otherwise.
-
#inet? ⇒ Bool
‘true` when using an Internet socket, `nil` if no #socket is available, `false` otherwise.
-
#listener? ⇒ Bool
‘true` if the connection is a server listener.
-
#to_io ⇒ IO?
IO stream or ‘nil` if no #socket is available.
-
#unix? ⇒ Bool?
‘true` when using a UNIX-domain socket, `nil` if no #socket is available, `false` otherwise.
- #write(data) ⇒ Object
Methods included from Callbacks
#on_attach, #on_close, #on_connect, #on_detach, #on_flush, #on_read, #on_write
Instance Attribute Details
#reactor ⇒ Reactor
34 35 36 |
# File 'lib/arachni/reactor/connection.rb', line 34 def reactor @reactor end |
#role ⇒ Symbol (readonly)
38 39 40 |
# File 'lib/arachni/reactor/connection.rb', line 38 def role @role end |
#socket ⇒ Socket (readonly)
30 31 32 |
# File 'lib/arachni/reactor/connection.rb', line 30 def socket @socket end |
Instance Method Details
#_connect ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/arachni/reactor/connection.rb', line 223 def _connect return true if unix? || connected? begin Error.translate do socket.connect_nonblock( Socket.sockaddr_in( @port, @host ) ) end # Already connected. :) rescue Errno::EISCONN end @connected = true on_connect true rescue IO::WaitReadable, IO::WaitWritable, Errno::EINPROGRESS rescue Error => e close e end |
#_read ⇒ Object
If this is a server #listener? it will delegate to #accept.
If this is a normal socket it will read BLOCK_SIZE amount of data. and pass it to Arachni::Reactor::Connection::Callbacks#on_read.
Processes a ‘read` event for this connection.
250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/arachni/reactor/connection.rb', line 250 def _read return _connect if !listener? && !connected? return accept if listener? Error.translate do on_read @socket.read_nonblock( BLOCK_SIZE ) end # Not ready to read or write yet, we'll catch it on future Reactor ticks. rescue IO::WaitReadable, IO::WaitWritable rescue Error => e close e end |
#_write ⇒ Integer
Will call Arachni::Reactor::Connection::Callbacks#on_write every time any of the buffer is consumed, can be multiple times when performing partial writes.
Will call Arachni::Reactor::Connection::Callbacks#on_flush once all of the buffer has been consumed.
Processes a ‘write` event for this connection.
Consumes and writes BLOCK_SIZE amount of data from the the beginning of the #write buffer to the socket.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/arachni/reactor/connection.rb', line 277 def _write return _connect if !connected? chunk = write_buffer.slice( 0, BLOCK_SIZE ) total_written = 0 begin Error.translate do # Send out the chunk, **all** of it, or at least try to. loop do total_written += written = @socket.write_nonblock( chunk ) write_buffer.slice!( 0, written ) # Call #on_write every time any of the buffer is consumed. on_write break if written == chunk.size chunk.slice!( 0, written ) end end # Not ready to read or write yet, we'll catch it on future Reactor ticks. rescue IO::WaitReadable, IO::WaitWritable end if write_buffer.empty? @socket.flush on_flush end total_written rescue Error => e close e end |
#accept ⇒ Connection?
Accepts a new client connection.
185 186 187 188 189 190 191 192 |
# File 'lib/arachni/reactor/connection.rb', line 185 def accept return if !(accepted = socket_accept) connection = @server_handler.call connection.configure socket: accepted, role: :server @reactor.attach connection connection end |
#attach(reactor) ⇒ Bool
Will first detach if already #attached?.
Sets #reactor.
Returns ‘true` if the connection was attached, `nil` if the connection was already attached.
105 106 107 108 109 110 111 112 |
# File 'lib/arachni/reactor/connection.rb', line 105 def attach( reactor ) return if reactor.attached?( self ) detach if attached? reactor.attach self true end |
#attached? ⇒ Bool
84 85 86 |
# File 'lib/arachni/reactor/connection.rb', line 84 def attached? @reactor && @reactor.attached?( self ) end |
#close(reason = nil) ⇒ Object
Will call Arachni::Reactor::Connection::Callbacks#on_close right before closing the socket and detaching from the Reactor.
Closes the connection and detaches it from the Arachni::Reactor.
170 171 172 173 174 175 176 |
# File 'lib/arachni/reactor/connection.rb', line 170 def close( reason = nil ) return if closed? on_close reason close_without_callback nil end |
#close_without_callback ⇒ Object
Will not call Arachni::Reactor::Connection::Callbacks#on_close.
Closes the connection and detaches it from the Arachni::Reactor.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/arachni/reactor/connection.rb', line 133 def close_without_callback return if closed? @closed = true if listener? && unix? && (path = to_io.path) && File.exist?( path ) File.delete( path ) end if @socket @socket.close rescue nil end detach nil end |
#closed? ⇒ Bool
152 153 154 |
# File 'lib/arachni/reactor/connection.rb', line 152 def closed? !!@closed end |
#configure(options = {}) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/arachni/reactor/connection.rb', line 202 def configure( = {} ) @socket = [:socket] @role = [:role] @host = [:host] @port = [:port] @server_handler = [:server_handler] # If we're a server without a handler then we're an accepted connection. if unix? || role == :server @connected = true on_connect end nil end |
#connected? ⇒ Boolean
218 219 220 |
# File 'lib/arachni/reactor/connection.rb', line 218 def connected? !!@connected end |
#detach ⇒ Bool
Removes #reactor.
122 123 124 125 126 127 128 |
# File 'lib/arachni/reactor/connection.rb', line 122 def detach return if detached? @reactor.detach self true end |
#detached? ⇒ Bool
91 92 93 |
# File 'lib/arachni/reactor/connection.rb', line 91 def detached? !attached? end |
#has_outgoing_data? ⇒ Bool
159 160 161 |
# File 'lib/arachni/reactor/connection.rb', line 159 def has_outgoing_data? !write_buffer.empty? end |
#inet? ⇒ Bool
52 53 54 55 |
# File 'lib/arachni/reactor/connection.rb', line 52 def inet? return if !to_io to_io.is_a?( TCPServer ) || to_io.is_a?( TCPSocket ) || to_io.is_a?( Socket ) end |
#listener? ⇒ Bool
66 67 68 69 |
# File 'lib/arachni/reactor/connection.rb', line 66 def listener? return if !to_io to_io.is_a?( TCPServer ) || (unix? && to_io.is_a?( UNIXServer )) end |
#to_io ⇒ IO?
59 60 61 62 |
# File 'lib/arachni/reactor/connection.rb', line 59 def to_io return if !@socket @socket.to_io end |
#unix? ⇒ Bool?
43 44 45 46 47 |
# File 'lib/arachni/reactor/connection.rb', line 43 def unix? return if !to_io return false if !Arachni::Reactor.supports_unix_sockets? to_io.is_a?( UNIXServer ) || to_io.is_a?( UNIXSocket ) end |
#write(data) ⇒ Object
The data will be buffered and sent in future Arachni::Reactor ticks.
75 76 77 78 79 |
# File 'lib/arachni/reactor/connection.rb', line 75 def write( data ) @reactor.schedule do write_buffer << data end end |