lib/ronin/network/tcp.rb in ronin-support-0.4.0.rc1 vs lib/ronin/network/tcp.rb in ronin-support-0.4.0.rc2

- old
+ new

@@ -1,7 +1,7 @@ # -# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com) +# Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com) # # This file is part of Ronin Support. # # Ronin Support is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published @@ -32,11 +32,11 @@ # The host to connect to. # # @param [Integer] port # The port to connect to. # - # @param [String] local_host (nil) + # @param [String] local_host ('0.0.0.0') # The local host to bind to. # # @param [Integer] local_port (nil) # The local port to bind to. # @@ -51,28 +51,27 @@ # # @example # tcp_connect('www.hackety.org',80) # => TCPSocket # # @example - # tcp_connect('www.wired.com',80) do |sock| - # sock.write("GET /\n\n") - # puts sock.readlines - # sock.close + # tcp_connect('www.wired.com',80) do |socket| + # socket.write("GET /\n\n") + # + # puts socket.readlines + # socket.close # end # # @api public # def tcp_connect(host,port,local_host=nil,local_port=nil) - host = host.to_s - local_host = if local_host - local_host.to_s - end + host = host.to_s + local_host = (local_host || '0.0.0.0').to_s - sock = TCPSocket.new(host,port,local_host,local_port) + socket = TCPSocket.new(host,port,local_host,local_port) - yield sock if block_given? - return sock + yield socket if block_given? + return socket end # # Creates a new TCPSocket object, connected to a given host and port. # The given data will then be written to the newly created TCPSocket. @@ -99,15 +98,15 @@ # The newly created TCPSocket object. # # @api public # def tcp_connect_and_send(data,host,port,local_host=nil,local_port=nil) - sock = tcp_connect(host,port,local_host,local_port) - sock.write(data) + socket = tcp_connect(host,port,local_host,local_port) + socket.write(data) - yield sock if block_given? - return sock + yield socket if block_given? + return socket end # # Creates a new temporary TCPSocket object, connected to the given host # and port. @@ -134,15 +133,15 @@ # @return [nil] # # @api public # def tcp_session(host,port,local_host=nil,local_port=nil) - sock = tcp_connect(host,port,local_host,local_port) + socket = tcp_connect(host,port,local_host,local_port) - yield sock if block_given? + yield socket if block_given? - sock.close + socket.close return nil end # # Reads the banner from the service running on the given host and port. @@ -175,12 +174,12 @@ # @api public # def tcp_banner(host,port,local_host=nil,local_port=nil) banner = nil - tcp_session(host,port,local_host,local_port) do |sock| - banner = sock.readline.strip + tcp_session(host,port,local_host,local_port) do |socket| + banner = socket.readline.strip end yield banner if block_given? return banner end @@ -213,12 +212,12 @@ # # => true # # @api public # def tcp_send(data,host,port,local_host=nil,local_port=nil) - tcp_session(host,port,local_host,local_port) do |sock| - sock.write(data) + tcp_session(host,port,local_host,local_port) do |socket| + socket.write(data) end return true end @@ -237,12 +236,12 @@ # @example # tcp_server(1337) # # @api public # - def tcp_server(port,host='0.0.0.0') - host = host.to_s + def tcp_server(port=nil,host=nil) + host = (host || '0.0.0.0').to_s server = TCPServer.new(host,port) server.listen(3) yield server if block_given? @@ -278,11 +277,11 @@ # client2.close # end # # @api public # - def tcp_server_session(port,host='0.0.0.0',&block) + def tcp_server_session(port=nil,host=nil,&block) server = tcp_server(port,host,&block) server.close() return nil end @@ -304,10 +303,10 @@ # client.puts 'lol' # end # # @api public # - def tcp_single_server(port,host='0.0.0.0') + def tcp_single_server(port=nil,host=nil) host = host.to_s server = TCPServer.new(host,port) server.listen(1)