lib/ronin/network/udp/udp.rb in ronin-support-0.5.1 vs lib/ronin/network/udp/udp.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' @@ -67,15 +67,12 @@ Timeout.timeout(timeout) do udp_session(host,port,local_host,local_port) do |socket| # send an empty UDP packet, just like nmap socket.syswrite('') - # send junk data, to elicit an error message - socket.syswrite("\0" * 64) - - # test if we've received any data - socket.sysread(1) + # send an empty UDP packet again, to elicit an Errno::ECONNREFUSED + socket.syswrite('') end end return true rescue Timeout::Error @@ -116,22 +113,27 @@ # @example # udp_connect('www.wired.com',80) do |socket| # puts socket.readlines # end # - # @see http://rubydoc.info/stdlib/socket/UDPSocket + # @see https://rubydoc.info/stdlib/socket/UDPSocket # # @api public # def udp_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 = UDPSocket.new - socket.bind(local_host,local_port) if (local_host && local_port) + + if local_host || local_port + local_host = local_host.to_s + local_port = local_port.to_i + + socket.bind(local_host,local_port) + end + socket.connect(host,port) yield socket if block_given? return socket end @@ -286,29 +288,29 @@ end # # Creates a new UDPServer 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. # # @return [UDPServer] # The new UDP server. # # @example # udp_server(1337) # - # @see http://rubydoc.info/stdlib/socket/UDPSocket + # @see https://rubydoc.info/stdlib/socket/UDPSocket # # @api public # def udp_server(port=nil,host=nil) port = port.to_i - host = (host || '0.0.0.0').to_s + host = host.to_s server = UDPSocket.new server.bind(host,port) yield server if block_given? @@ -316,14 +318,14 @@ end # # Creates a new temporary UDPServer listening on a given 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. # # @yield [server] # The block which will be called after the server has been created. # After the block has finished, the server will be closed. @@ -348,14 +350,14 @@ # # Creates a new UDPServer listening on a given host and port, # accepting messages from clients in a loop. # - # @param [Integer] port + # @param [Integer] port (nil) # The port the UDPServer will listen on. # - # @param [String] host + # @param [String] host (nil) # The optional host the UDPServer will bind to. # # @yield [server, (client_host, client_port), mesg] # The given block will be passed the client host/port and the received # message. @@ -395,13 +397,13 @@ # # Creates a new UDPServer listening on a given host and port, # accepts only one message from a client. # - # @param [Integer] port + # @param [Integer] port (nil) # The port the UDPServer will listen on. # - # @param [String] host + # @param [String] host (nil) # The optional host the UDPServer will bind to. # # @yield [server, (client_host, client_port), mesg] # The given block will be passed the client host/port and the received # message.