Module: Ronin::Network::Mixins::TCP

Includes:
Mixin, TCP
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

Methods included from Mixin

included

Methods included from TCP

#tcp_server_loop

Instance Method Details

#tcp_accept {|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.

Examples:

tcp_accept do |client|
  client.puts 'lol'
end

Yields:

  • (client)

    The given block will be passed the newly connected client. When the block has finished, the newly connected client and the server will be closed.

Yield Parameters:

  • client (TCPSocket)

    The newly connected client.

See Also:

Since:

  • 0.5.0


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/ronin/network/mixins/tcp.rb', line 323

def tcp_accept(&block)
  print_info "Listening on #{server_host_port} ..."

  super(self.server_port,self.server_host) 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 "Disconnected client #{client_host}:#{client_port}"
  end

  print_info "Closed #{server_host_port}"
  return nil
end

#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.

Examples:

tcp_banner
# => "220 mx.google.com ESMTP c20sm3096959rvf.1"

Yields:

  • (banner)

    If a block is given, it will be passed the grabbed banner.

Yield Parameters:

  • banner (String)

    The grabbed banner.

See Also:


200
201
202
203
204
# File 'lib/ronin/network/mixins/tcp.rb', line 200

def tcp_banner(&block)
  print_debug "Grabbing banner from #{host_port}"

  return super(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.

Examples:

tcp_connect # => TCPSocket
tcp_connect do |socket|
  socket.write("GET / HTTP/1.1\n\r\n\r")

  puts socket.readlines
  socket.close
end

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.

See Also:


120
121
122
123
124
# File 'lib/ronin/network/mixins/tcp.rb', line 120

def tcp_connect(&block)
  print_info "Connecting to #{host_port} ..."

  return super(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.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.

See Also:


146
147
148
149
150
151
# File 'lib/ronin/network/mixins/tcp.rb', line 146

def tcp_connect_and_send(data,&block)
  print_info "Connecting to #{host_port} ..."
  print_debug "Sending data: #{data.inspect}"

  return super(data,self.host,self.port,self.local_host,self.local_port,&block)
end

#tcp_open?(timeout = nil) ⇒ Boolean? (protected)

Tests whether the TCP port, specified by the host and port parameters, is open.

See Also:

Since:

  • 0.5.0


84
85
86
87
88
# File 'lib/ronin/network/mixins/tcp.rb', line 84

def tcp_open?(timeout=nil)
  print_info "Testing if #{host_port} is open ..."

  super(self.host,self.port,self.local_host,self.local_port,timeout)
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.

Examples:

buffer = "GET /" + ('A' * 4096) + "\n\r"
tcp_send(buffer)
# => true

See Also:


222
223
224
225
226
227
228
229
230
# File 'lib/ronin/network/mixins/tcp.rb', line 222

def tcp_send(data)
  print_info "Connecting to #{host_port} ..."
  print_debug "Sending data: #{data.inspect}"

  super(data,self.host,self.port,self.local_host,self.local_port)

  print_info "Disconnected from #{host_port}"
  return true
end

#tcp_server {|server| ... } ⇒ TCPServer (protected)

Creates a new TCPServer object listening on the server_host and server_port parameters.

Examples:

tcp_server

Yields:

  • (server)

    The given block will be passed the newly created server.

Yield Parameters:

  • server (TCPServer)

    The newly created server.

See Also:


252
253
254
255
256
# File 'lib/ronin/network/mixins/tcp.rb', line 252

def tcp_server(&block)
  print_info "Listening on #{self.server_host_port} ..."

  return super(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.

Examples:

tcp_server_session do |server|
  client1 = server.accept
  client2 = server.accept

  client2.write(server.read_line)

  client1.close
  client2.close
end

Yields:

  • (server)

    The given block will be passed the newly created server. When the block has finished, the server will be closed.

Yield Parameters:

  • server (TCPServer)

    The newly created server.

See Also:


286
287
288
289
290
291
292
293
# File 'lib/ronin/network/mixins/tcp.rb', line 286

def tcp_server_session(&block)
  print_info "Listening on #{server_host_port} ..."

  super(self.server_port,self.server_host,&block)

  print_info "Closed #{server_host_port}"
  return nil
end

#tcp_session {|socket| ... } ⇒ nil (protected)

Creates a TCP session to the host and port specified by the host and port parameters.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. After the block has returned, the socket will be closed.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.

See Also:


170
171
172
173
174
175
176
177
# File 'lib/ronin/network/mixins/tcp.rb', line 170

def tcp_session(&block)
  print_info "Connecting to #{host_port} ..."

  super(self.host,self.port,self.local_host,self.local_port,&block)

  print_info "Disconnected from #{host_port}"
  return nil
end

#tcp_single_server(&block) ⇒ Object (protected)

Deprecated.

Deprecated as of 0.5.0. Use #tcp_accept instead.


346
347
348
# File 'lib/ronin/network/mixins/tcp.rb', line 346

def tcp_single_server(&block)
  tcp_accept(&block)
end