rex-socket 0.1.68 → 0.1.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27acb62e9f5a756af4be153428826989e9287a3c61cf42ab472f531459ce4e6a
4
- data.tar.gz: 6aaf1a0a93ce1563f3c2c0d2fb02e2f8fca12767e0bdad54e2bac13a19381ee5
3
+ metadata.gz: 06a8d5b9b8dff6b4e9ef66dbccf00522bacff3f3e7e7b011f13a3548cd595f6d
4
+ data.tar.gz: 3fb1e4a3d8a356468b9ff998d7001eacd001ff825d97bf7ffae41b88489d2be3
5
5
  SHA512:
6
- metadata.gz: '0481a40864ca7aad2d346f510024ccff891f218b3ebbcea572e9342424d730241829218b4d0eadb69812c79fd4fa9e5a6b9e03749ee8eb22ac61ee71278ed5a3'
7
- data.tar.gz: 7de451e877c242251ab8a004dc806f4465849748696547b752168dbb58c24b48fb25e95771656da33a89a989f68b7030e58579f5b38a92f91bef35e50572c600
6
+ metadata.gz: d75a50f35c0853ad85026916638d1cea6724d4fbdf52f704bc396872ee85228c0f1db096b443ad52bc1631fb52e220e6ef44c024352e958df50b554043a68930
7
+ data.tar.gz: cd78a56eec5f4b4e780cf496eca88452057786065d300a0db2cb7eba00fa962d94c9c0e0227e73cb78a3931f018ae58259546e874b945cd154829f397d635320
@@ -135,37 +135,60 @@ module Rex::Socket::Udp
135
135
  end
136
136
 
137
137
  #
138
- # Receives a datagram and returns the data and host:port of the requestor
139
- # as [ data, host, port ].
138
+ # Receives a datagram and returns the data and sender address information as
139
+ # [ data, [address_family, port, host, host] ], matching stdlib
140
+ # UDPSocket#recvfrom. Like the stdlib method, this blocks until a datagram is
141
+ # available and has no timeout of its own (see #timed_recvfrom for a variant
142
+ # that does). The host appears in both the hostname and numeric address
143
+ # positions; no reverse-DNS lookup is performed.
144
+ #
145
+ # @param maxlen [Integer] maximum number of bytes to receive
146
+ # @param flags [Integer] flags passed to the underlying recvfrom(2) call (default: 0)
147
+ # @return [Array(String, Array)] the datagram and the sender address information
148
+ #
149
+ def recvfrom(maxlen, flags = 0)
150
+ # Block until the socket is readable to mirror the stdlib's blocking
151
+ # UDPSocket#recvfrom; a nil timeout waits indefinitely.
152
+ ::IO.select([ fd ], nil, nil, nil)
153
+ data, saddr = recvfrom_nonblock(maxlen, flags)
154
+ [ data, sender_addr_info(saddr) ]
155
+ end
156
+
157
+ #
158
+ # Receives a datagram like #recvfrom but waits at most +timeout+ seconds for
159
+ # one to arrive, returning nil if the timeout elapses first. The return value
160
+ # otherwise matches #recvfrom: [ data, [address_family, port, host, host] ].
161
+ #
162
+ # @param maxlen [Integer] maximum number of bytes to receive
163
+ # @param timeout [Numeric] seconds to wait for a datagram before giving up
164
+ # @return [Array(String, Array), nil] the datagram and sender address
165
+ # information, or nil if no datagram arrived within +timeout+ seconds
140
166
  #
141
- def recvfrom(length = 65535, timeout=def_read_timeout)
167
+ def timed_recvfrom(maxlen = 65535, timeout = def_read_timeout)
168
+ return nil unless ::IO.select([ fd ], nil, nil, timeout)
142
169
 
143
- begin
144
- if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
145
- (rv[0]) and (rv[0][0] == fd)
146
- )
147
- data, saddr = recvfrom_nonblock(length)
148
- af, host, port = Rex::Socket.from_sockaddr(saddr)
170
+ data, saddr = recvfrom_nonblock(maxlen)
171
+ [ data, sender_addr_info(saddr) ]
172
+ rescue ::Timeout::Error, ::Errno::ECONNREFUSED
173
+ nil
174
+ end
149
175
 
150
- return [ data, host, port ]
151
- else
152
- return [ '', nil, nil ]
153
- end
154
- rescue ::Timeout::Error
155
- return [ '', nil, nil ]
156
- rescue ::Interrupt
157
- raise $!
158
- rescue ::Exception
159
- return [ '', nil, nil ]
160
- end
176
+ #
177
+ # Converts a packed sockaddr into the stdlib UDPSocket#recvfrom-style sender
178
+ # address tuple [ address_family, port, host, host ].
179
+ #
180
+ def sender_addr_info(saddr)
181
+ af, host, port = Rex::Socket.from_sockaddr(saddr)
182
+ af_name = ::Socket.constants.grep(/^AF_/).find { |c| ::Socket.const_get(c) == af }.to_s
183
+ [ af_name, port, host, host ]
161
184
  end
185
+ private :sender_addr_info
162
186
 
163
187
  #
164
- # Calls recvfrom and only returns the data
188
+ # Calls #timed_read and returns the data
165
189
  #
166
190
  def get(timeout=nil)
167
- data, saddr, sport = recvfrom(65535, timeout)
168
- return data
191
+ timed_read(65535, timeout)
169
192
  end
170
193
 
171
194
  #
@@ -1,5 +1,5 @@
1
1
  module Rex
2
2
  module Socket
3
- VERSION = "0.1.68"
3
+ VERSION = "0.1.69"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rex-socket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.68
4
+ version: 0.1.69
5
5
  platform: ruby
6
6
  authors:
7
7
  - Metasploit Hackers