From 1de569c16d6e22206bd343e9d5750526c226a725 Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Fri, 15 May 2026 16:39:22 -0400 Subject: [PATCH 1/2] Change the return value of #recvfrom to match stdlib --- lib/rex/socket/udp.rb | 50 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/rex/socket/udp.rb b/lib/rex/socket/udp.rb index 58ba2c0..4ef9398 100644 --- a/lib/rex/socket/udp.rb +++ b/lib/rex/socket/udp.rb @@ -117,37 +117,37 @@ def sendto(gram, peerhost, peerport, flags = 0) end # - # Receives a datagram and returns the data and host:port of the requestor - # as [ data, host, port ]. - # - def recvfrom(length = 65535, timeout=def_read_timeout) - - begin - if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and - (rv[0]) and (rv[0][0] == fd) - ) - data, saddr = recvfrom_nonblock(length) - af, host, port = Rex::Socket.from_sockaddr(saddr) - - return [ data, host, port ] - else - return [ '', nil, nil ] - end - rescue ::Timeout::Error - return [ '', nil, nil ] - rescue ::Interrupt - raise $! - rescue ::Exception - return [ '', nil, nil ] - end + # Receives a datagram and returns the data and sender address information + # as [ data, [address_family, port, host, host] ], matching the format of + # stdlib UDPSocket#recvfrom (host appears in both the hostname and numeric + # address positions; no reverse-DNS lookup is performed). + # + # @param maxlen [Integer] maximum number of bytes to receive + # @param timeout [Numeric] seconds to wait before raising Errno::EAGAIN + # (default: def_read_timeout = 10). NOTE: this parameter was previously + # named +flags+ and defaulted to 0; callers that passed flags=0 explicitly + # will now receive a 0-second (poll) receive instead of a 10-second wait. + # + def recvfrom(maxlen, timeout = def_read_timeout) + rv = ::IO.select([ fd ], nil, nil, timeout) + + raise Errno::EAGAIN, "Resource temporarily unavailable" if rv.nil? + + data, saddr = recvfrom_nonblock(maxlen) + af, host, port = Rex::Socket.from_sockaddr(saddr) + af_name = Socket.constants.grep(/^AF_/).find { |c| Socket.const_get(c) == af }.to_s + [data, [af_name, port, host, host]] + rescue ::Timeout::Error + raise Errno::EAGAIN, "Resource temporarily unavailable" + rescue ::Interrupt + raise end # # Calls recvfrom and only returns the data # def get(timeout=nil) - data, saddr, sport = recvfrom(65535, timeout) - return data + timed_read(65535, timeout) end # From 8c007dbb2e232cdb138dd09799b906849240c75b Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Fri, 15 May 2026 17:56:37 -0400 Subject: [PATCH 2/2] The second arg fto #recvfrom should be flags --- lib/rex/socket/udp.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/rex/socket/udp.rb b/lib/rex/socket/udp.rb index 4ef9398..f71c5f2 100644 --- a/lib/rex/socket/udp.rb +++ b/lib/rex/socket/udp.rb @@ -123,24 +123,19 @@ def sendto(gram, peerhost, peerport, flags = 0) # address positions; no reverse-DNS lookup is performed). # # @param maxlen [Integer] maximum number of bytes to receive - # @param timeout [Numeric] seconds to wait before raising Errno::EAGAIN - # (default: def_read_timeout = 10). NOTE: this parameter was previously - # named +flags+ and defaulted to 0; callers that passed flags=0 explicitly - # will now receive a 0-second (poll) receive instead of a 10-second wait. + # @param flags [Integer] flags passed to the underlying recvfrom(2) call (default: 0) # - def recvfrom(maxlen, timeout = def_read_timeout) - rv = ::IO.select([ fd ], nil, nil, timeout) + def recvfrom(maxlen, flags = 0) + rv = ::IO.select([ fd ], nil, nil, def_read_timeout) raise Errno::EAGAIN, "Resource temporarily unavailable" if rv.nil? - data, saddr = recvfrom_nonblock(maxlen) + data, saddr = recvfrom_nonblock(maxlen, flags) af, host, port = Rex::Socket.from_sockaddr(saddr) af_name = Socket.constants.grep(/^AF_/).find { |c| Socket.const_get(c) == af }.to_s [data, [af_name, port, host, host]] rescue ::Timeout::Error raise Errno::EAGAIN, "Resource temporarily unavailable" - rescue ::Interrupt - raise end #