Module: Ronin::Network::TCP
- Included in:
- Net, Mixins::TCP, SSL, Support
- Defined in:
- lib/ronin/network/tcp/tcp.rb,
lib/ronin/network/tcp/proxy.rb
Overview
Provides helper methods for using the TCP protocol.
Defined Under Namespace
Classes: Proxy
Instance Method Summary collapse
-
#tcp_accept(port = nil, host = nil) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepts only one client and then stops listening.
-
#tcp_banner(host, port, local_host = nil, local_port = nil) {|banner| ... } ⇒ String
Reads the banner from the service running on the given host and port.
-
#tcp_connect(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ TCPSocket
Creates a new TCPSocket object connected to a given host and port.
-
#tcp_connect_and_send(data, host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ Object
Creates a new TCPSocket object, connected to a given host and port.
-
#tcp_open?(host, port, local_host = nil, local_port = nil, timeout = nil) ⇒ Boolean?
Tests whether a remote TCP port is open.
-
#tcp_send(data, host, port, local_host = nil, local_port = nil) ⇒ true
Connects to a specified host and port, sends the given data and then closes the connection.
-
#tcp_server(port = nil, host = nil, backlog = 5) {|server| ... } ⇒ TCPServer
Creates a new TCPServer listening on a given host and port.
-
#tcp_server_loop(port = nil, host = nil) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepting clients in a loop.
-
#tcp_server_session(port = nil, host = nil, backlog = 5) {|server| ... } ⇒ nil
Creates a new temporary TCPServer listening on a host and port.
-
#tcp_session(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ nil
Creates a new temporary TCPSocket object, connected to the given host and port.
-
#tcp_single_server(port = nil, host = nil) ⇒ Object
deprecated
Deprecated.
Deprecated as of 0.5.0. Use #tcp_accept instead.
Instance Method Details
#tcp_accept(port = nil, host = nil) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepts only one client and then stops listening.
434 435 436 437 438 439 440 441 |
# File 'lib/ronin/network/tcp/tcp.rb', line 434 def tcp_accept(port=nil,host=nil) tcp_server_session(port,host,1) do |server| client = server.accept yield client if block_given? client.close end end |
#tcp_banner(host, port, local_host = nil, local_port = nil) {|banner| ... } ⇒ String
Reads the banner from the service running on the given host and port.
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/ronin/network/tcp/tcp.rb', line 235 def (host,port,local_host=nil,local_port=nil) = nil tcp_session(host,port,local_host,local_port) do |socket| = socket.readline.strip end yield if block_given? return end |
#tcp_connect(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ TCPSocket
Creates a new TCPSocket object connected to a given host and port.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ronin/network/tcp/tcp.rb', line 118 def tcp_connect(host,port,local_host=nil,local_port=nil) host = host.to_s port = port.to_i socket = if local_host || local_port local_host = local_host.to_s local_port = local_port.to_i TCPSocket.new(host,port,local_host,local_port) else TCPSocket.new(host,port) end yield socket if block_given? return socket end |
#tcp_connect_and_send(data, host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ Object
Creates a new TCPSocket object, connected to a given host and port. The given data will then be written to the newly created TCPSocket.
162 163 164 165 166 167 168 |
# File 'lib/ronin/network/tcp/tcp.rb', line 162 def tcp_connect_and_send(data,host,port,local_host=nil,local_port=nil) socket = tcp_connect(host,port,local_host,local_port) socket.write(data) yield socket if block_given? return socket end |
#tcp_open?(host, port, local_host = nil, local_port = nil, timeout = nil) ⇒ Boolean?
Tests whether a remote TCP port is open.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ronin/network/tcp/tcp.rb', line 63 def tcp_open?(host,port,local_host=nil,local_port=nil,timeout=nil) timeout ||= 5 begin Timeout.timeout(timeout) do tcp_session(host,port,local_host,local_port) end return true rescue Timeout::Error return nil rescue SocketError, SystemCallError return false end end |
#tcp_send(data, host, port, local_host = nil, local_port = nil) ⇒ true
Connects to a specified host and port, sends the given data and then closes the connection.
275 276 277 278 279 280 281 |
# File 'lib/ronin/network/tcp/tcp.rb', line 275 def tcp_send(data,host,port,local_host=nil,local_port=nil) tcp_session(host,port,local_host,local_port) do |socket| socket.write(data) end return true end |
#tcp_server(port = nil, host = nil, backlog = 5) {|server| ... } ⇒ TCPServer
Creates a new TCPServer listening on a given host and port.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/ronin/network/tcp/tcp.rb', line 311 def tcp_server(port=nil,host=nil,backlog=5) port = port.to_i server = if host host = host.to_s TCPServer.new(host,port) else TCPServer.new(port) end server.listen(backlog) yield server if block_given? return server end |
#tcp_server_loop(port = nil, host = nil) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepting clients in a loop.
394 395 396 397 398 399 400 401 402 403 |
# File 'lib/ronin/network/tcp/tcp.rb', line 394 def tcp_server_loop(port=nil,host=nil) tcp_server_session(port,host) do |server| loop do client = server.accept yield client if block_given? client.close end end end |
#tcp_server_session(port = nil, host = nil, backlog = 5) {|server| ... } ⇒ nil
Creates a new temporary TCPServer listening on a host and port.
360 361 362 363 364 |
# File 'lib/ronin/network/tcp/tcp.rb', line 360 def tcp_server_session(port=nil,host=nil,backlog=5,&block) server = tcp_server(port,host,backlog,&block) server.close() return nil end |
#tcp_session(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ nil
Creates a new temporary TCPSocket object, connected to the given host and port.
197 198 199 200 201 202 203 |
# File 'lib/ronin/network/tcp/tcp.rb', line 197 def tcp_session(host,port,local_host=nil,local_port=nil) socket = tcp_connect(host,port,local_host,local_port) yield socket if block_given? socket.close return nil end |
#tcp_single_server(port = nil, host = nil) ⇒ Object
Deprecated as of 0.5.0. Use #tcp_accept instead.
447 448 449 |
# File 'lib/ronin/network/tcp/tcp.rb', line 447 def tcp_single_server(port=nil,host=nil) tcp_accept(port,host) end |