lib/ronin/network/tcp/tcp.rb in ronin-support-0.5.1 vs lib/ronin/network/tcp/tcp.rb in ronin-support-0.5.2

- old
+ new

@@ -1,22 +1,22 @@ # -# Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com) +# Copyright (c) 2006-2021 Hal Brodigan (postmodern.mod3 at gmail.com) # -# This file is part of Ronin Support. +# This file is part of ronin-support. # -# Ronin Support is free software: you can redistribute it and/or modify +# 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 # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # -# Ronin Support is distributed in the hope that it will be useful, +# ronin-support is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License -# along with Ronin Support. If not, see <http://www.gnu.org/licenses/>. +# along with ronin-support. If not, see <https://www.gnu.org/licenses/>. # require 'socket' require 'timeout' @@ -83,11 +83,11 @@ # The host to connect to. # # @param [Integer] port # The port to connect to. # - # @param [String] local_host ('0.0.0.0') + # @param [String] local_host (nil) # The local host to bind to. # # @param [Integer] local_port (nil) # The local port to bind to. # @@ -109,22 +109,27 @@ # # puts socket.readlines # socket.close # end # - # @see http://rubydoc.info/stdlib/socket/TCPSocket + # @see https://rubydoc.info/stdlib/socket/TCPSocket # # @api public # def tcp_connect(host,port,local_host=nil,local_port=nil) - host = host.to_s - port = port.to_i - local_host = (local_host || '0.0.0.0').to_s - local_port = local_port.to_i + host = host.to_s + port = port.to_i - socket = TCPSocket.new(host,port,local_host,local_port) + 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 # @@ -276,14 +281,14 @@ end # # Creates a new TCPServer listening on a given host and port. # - # @param [Integer] port + # @param [Integer] port (nil) # The local port to listen on. # - # @param [String] host ('0.0.0.0') + # @param [String] host (nil) # The host to bind to. # # @param [Integer] backlog (5) # The maximum backlog of pending connections. # @@ -297,32 +302,36 @@ # The new TCP server. # # @example # tcp_server(1337) # - # @see http://rubydoc.info/stdlib/socket/TCPServer + # @see https://rubydoc.info/stdlib/socket/TCPServer # # @api public # def tcp_server(port=nil,host=nil,backlog=5) port = port.to_i - host = (host || '0.0.0.0').to_s - server = TCPServer.new(host,port) + 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 # # Creates a new temporary TCPServer listening on a host and port. # - # @param [Integer] port + # @param [Integer] port (nil) # The local port to bind to. # - # @param [String] host ('0.0.0.0') + # @param [String] host (nil) # The host to bind to. # # @param [Integer] backlog (5) # The maximum backlog of pending connections. # @@ -356,14 +365,14 @@ # # Creates a new TCPServer listening on a given host and port, # accepting clients in a loop. # - # @param [Integer] port + # @param [Integer] port (nil) # The local port to bind to. # - # @param [String] host ('0.0.0.0') + # @param [String] host (nil) # The host to bind to. # # @yield [client] # The given block will be passed the newly connected client. # After the block has finished, the client will be closed. @@ -395,13 +404,13 @@ # # Creates a new TCPServer listening on a given host and port, # accepts only one client and then stops listening. # - # @param [Integer] port + # @param [Integer] port (nil) # The local port to bind to. # - # @param [String] host ('0.0.0.0') + # @param [String] host (nil # The host to bind to. # # @yield [client] # The given block will be passed the newly connected client. # After the block has finished, both the client and the server will be