Module: Ronin::Network::Mixins::TCP
- Includes:
- Mixin
- Defined in:
- lib/ronin/network/mixins/tcp.rb
Overview
Adds TCP convenience methods and connection parameters to a class.
Defines the following parameters:
host
(String
) - TCP host.port
(Integer
) - TCP port.local_host
(String
) - TCP local host.local_port
(Integer
) - TCP local port.server_host
(String
) - TCP server host.server_port
(Integer
) - TCP server port.
Instance Method Summary collapse
-
#tcp_banner {|banner| ... } ⇒ String
protected
Connects to the host and port specified by the
host
andport
parameters, reads the banner then closes the connection. -
#tcp_connect {|socket| ... } ⇒ TCPSocket
protected
Opens a TCP connection to the host and port specified by the
host
andport
parameters. -
#tcp_connect_and_send(data) {|socket| ... } ⇒ TCPSocket
protected
Connects to the host and port specified by the
host
andport
parameters, then sends the given data. -
#tcp_send(data) ⇒ true
protected
Connects to the host and port specified by the
host
andport
parameters, sends the given data and then disconnects. -
#tcp_server {|server| ... } ⇒ TCPServer
protected
Creates a new TCPServer object listening on the
server_host
andserver_port
parameters. -
#tcp_server_session {|server| ... } ⇒ nil
protected
Creates a new temporary TCPServer object listening on the
server_host
andserver_port
parameters. -
#tcp_session {|socket| ... } ⇒ nil
protected
Creates a TCP session to the host and port specified by the
host
andport
parameters. -
#tcp_single_server {|client| ... } ⇒ nil
protected
Creates a new temporary TCPServer object listening on
server_host
andserver_port
parameters.
Methods included from Mixin
Instance Method Details
#tcp_banner {|banner| ... } ⇒ String (protected)
Connects to the host and port specified by the host
and port
parameters, reads the banner then closes the connection.
175 176 177 178 179 |
# File 'lib/ronin/network/mixins/tcp.rb', line 175 def (&block) print_debug "Grabbing banner from #{self.host}:#{self.port}" return ::Net.(self.host,self.port,self.local_host,self.local_port,&block) end |
#tcp_connect {|socket| ... } ⇒ TCPSocket (protected)
Opens a TCP connection to the host and port specified by the
host
and port
parameters. If the local_host
and
local_port
parameters are set, they will be used for
the local host and port of the TCP connection.
101 102 103 104 105 |
# File 'lib/ronin/network/mixins/tcp.rb', line 101 def tcp_connect(&block) print_info "Connecting to #{self.host}:#{self.port} ..." return ::Net.tcp_connect(self.host,self.port,self.local_host,self.local_port,&block) end |
#tcp_connect_and_send(data) {|socket| ... } ⇒ TCPSocket (protected)
Connects to the host and port specified by the host
and port
parameters, then sends the given data.
125 126 127 128 129 130 |
# File 'lib/ronin/network/mixins/tcp.rb', line 125 def tcp_connect_and_send(data,&block) print_info "Connecting to #{self.host}:#{self.port} ..." print_debug "Sending data: #{data.inspect}" return ::Net.tcp_connect_and_send(data,self.host,self.port,self.local_host,self.local_port,&block) end |
#tcp_send(data) ⇒ true (protected)
Connects to the host and port specified by the host
and port
parameters, sends the given data and then disconnects.
195 196 197 198 199 200 201 202 203 |
# File 'lib/ronin/network/mixins/tcp.rb', line 195 def tcp_send(data) print_info "Connecting to #{self.host}:#{self.port} ..." print_debug "Sending data: #{data.inspect}" ::Net.tcp_send(data,self.host,self.port,self.local_host,self.local_port) print_info "Disconnected from #{self.host}:#{self.port}" return true end |
#tcp_server {|server| ... } ⇒ TCPServer (protected)
Creates a new TCPServer object listening on the server_host
and server_port
parameters.
223 224 225 226 227 228 229 230 231 |
# File 'lib/ronin/network/mixins/tcp.rb', line 223 def tcp_server(&block) if self.server_host print_info "Listening on #{self.server_host}:#{self.server_port} ..." else print_info "Listening on #{self.server_port} ..." end return ::Net.tcp_server(self.server_port,self.server_host,&block) end |
#tcp_server_session {|server| ... } ⇒ nil (protected)
Creates a new temporary TCPServer object listening on the
server_host
and server_port
parameters.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/ronin/network/mixins/tcp.rb', line 259 def tcp_server_session(&block) if self.server_host print_info "Listening on #{self.server_host}:#{self.server_port} ..." else print_info "Listening on #{self.server_port} ..." end ::Net.tcp_server_session(&block) if self.server_host print_info "Closed #{self.server_host}:#{self.server_port}" else print_info "Closed #{self.server_port}" end return nil end |
#tcp_session {|socket| ... } ⇒ nil (protected)
Creates a TCP session to the host and port specified by the
host
and port
parameters.
147 148 149 150 151 152 153 154 |
# File 'lib/ronin/network/mixins/tcp.rb', line 147 def tcp_session(&block) print_info "Connecting to #{self.host}:#{self.port} ..." Net.tcp_session(self.host,self.port,self.local_host,self.local_port,&block) print_info "Disconnected from #{self.host}:#{self.port}" return nil end |
#tcp_single_server {|client| ... } ⇒ nil (protected)
Creates a new temporary TCPServer object listening on
server_host
and server_port
parameters.
The TCPServer will accepting one client, pass the newly connected
client to a given block, disconnects the client and stops
listening.
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/ronin/network/mixins/tcp.rb', line 301 def tcp_single_server(&block) if self.server_host print_info "Listening on #{self.server_host}:#{self.server_port} ..." else print_info "Listening on #{self.server_port} ..." end ::Net.tcp_single_server do |client| client_addr = client.peeraddr client_host = (client_addr[2] || client_addr[3]) client_port = client_addr[1] print_info "Client connected #{client_host}:#{client_port}" yield client if block_given? print_info "Disconnecting client #{client_host}:#{client_port}" end if self.server_host print_info "Closed #{self.server_host}:#{self.server_port}" else print_info "Closed #{self.server_port}" end return nil end |