rex-socket 0.1.67 → 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: d08e421781956b8e7f7cae6c731fb1589401f375774dbe51fde3dba15170ae3c
4
- data.tar.gz: 646375e1d851896604d9b4b342bc1158fb0628b42a659fe58622551a66bb097d
3
+ metadata.gz: 06a8d5b9b8dff6b4e9ef66dbccf00522bacff3f3e7e7b011f13a3548cd595f6d
4
+ data.tar.gz: 3fb1e4a3d8a356468b9ff998d7001eacd001ff825d97bf7ffae41b88489d2be3
5
5
  SHA512:
6
- metadata.gz: 850daee218c584254d1eaf047ad3843b4fcaa648a83add08ae29c71c9142be59613239e6bf46c7c1996b3710bfb02b271320f62ff8d55b3f93c1a2073bd286a2
7
- data.tar.gz: 3fc4e74930e01ff0a54a22b7f135e83e49693a053b397619b00b2e7ede551cfd4875946c00d46fa537508266c2c79ca68af5d87e0ce970a9e23b1d0e880e7a6d
6
+ metadata.gz: d75a50f35c0853ad85026916638d1cea6724d4fbdf52f704bc396872ee85228c0f1db096b443ad52bc1631fb52e220e6ef44c024352e958df50b554043a68930
7
+ data.tar.gz: cd78a56eec5f4b4e780cf496eca88452057786065d300a0db2cb7eba00fa962d94c9c0e0227e73cb78a3931f018ae58259546e874b945cd154829f397d635320
@@ -97,57 +97,98 @@ module Rex::Socket::Udp
97
97
  #
98
98
  # Sends a datagram to the supplied host:port with optional flags.
99
99
  #
100
+ # @deprecated Use {#send} with the stdlib 4-arg form send(mesg, flags, host, port) instead.
101
+ # Note the argument order differs: sendto(gram, host, port, flags) vs send(mesg, flags, host, port).
102
+ #
100
103
  def sendto(gram, peerhost, peerport, flags = 0)
104
+ warn "#{self.class}#sendto is deprecated; use send(mesg, flags, host, port) instead", uplevel: 1
105
+ send(gram, flags, peerhost, peerport)
106
+ end
101
107
 
102
- # Catch unconnected IPv6 sockets talking to IPv4 addresses
103
- peer = Rex::Socket.resolv_nbo(peerhost)
104
- if (peer.length == 4 and self.ipv == 6)
105
- peerhost = Rex::Socket.getaddress(peerhost, true)
106
- if peerhost[0,7].downcase != '::ffff:'
107
- peerhost = '::ffff:' + peerhost
108
+ #
109
+ # Sends a datagram using the stdlib 4-arg form send(mesg, flags, host, port).
110
+ #
111
+ # The 4-arg form handles IPv6/IPv4 address mapping and dispatches via
112
+ # BasicSocket#send with a packed sockaddr, so channel/pivoted sockets that
113
+ # override sendto are not involved. Also accepts the 3-arg sockaddr form used
114
+ # by lower-level callers, and the 2-arg connected-socket form.
115
+ #
116
+ def send(mesg, flags, host = nil, port = nil)
117
+ if host && port
118
+ # Catch unconnected IPv6 sockets talking to IPv4 addresses
119
+ peer = Rex::Socket.resolv_nbo(host)
120
+ if peer.length == 4 && self.ipv == 6
121
+ host_address = Rex::Socket.getaddress(host, true)
122
+ host = '::ffff:' + host_address unless host_address.downcase.start_with?('::ffff:')
108
123
  end
124
+ begin
125
+ super(mesg, flags, Rex::Socket.to_sockaddr(host, port))
126
+ rescue ::Errno::EHOSTUNREACH, ::Errno::ENETDOWN, ::Errno::ENETUNREACH, ::Errno::ENETRESET,
127
+ ::Errno::EHOSTDOWN, ::Errno::EACCES, ::Errno::EINVAL, ::Errno::EADDRNOTAVAIL
128
+ nil
129
+ end
130
+ elsif host
131
+ super(mesg, flags, host)
132
+ else
133
+ super(mesg, flags)
109
134
  end
135
+ end
110
136
 
111
- begin
112
- send(gram, flags, Rex::Socket.to_sockaddr(peerhost, peerport))
113
- rescue ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
114
- return nil
115
- end
116
-
137
+ #
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) ]
117
155
  end
118
156
 
119
157
  #
120
- # Receives a datagram and returns the data and host:port of the requestor
121
- # as [ data, host, port ].
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] ].
122
161
  #
123
- def recvfrom(length = 65535, timeout=def_read_timeout)
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
166
+ #
167
+ def timed_recvfrom(maxlen = 65535, timeout = def_read_timeout)
168
+ return nil unless ::IO.select([ fd ], nil, nil, timeout)
124
169
 
125
- begin
126
- if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
127
- (rv[0]) and (rv[0][0] == fd)
128
- )
129
- data, saddr = recvfrom_nonblock(length)
130
- 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
131
175
 
132
- return [ data, host, port ]
133
- else
134
- return [ '', nil, nil ]
135
- end
136
- rescue ::Timeout::Error
137
- return [ '', nil, nil ]
138
- rescue ::Interrupt
139
- raise $!
140
- rescue ::Exception
141
- return [ '', nil, nil ]
142
- 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 ]
143
184
  end
185
+ private :sender_addr_info
144
186
 
145
187
  #
146
- # Calls recvfrom and only returns the data
188
+ # Calls #timed_read and returns the data
147
189
  #
148
190
  def get(timeout=nil)
149
- data, saddr, sport = recvfrom(65535, timeout)
150
- return data
191
+ timed_read(65535, timeout)
151
192
  end
152
193
 
153
194
  #
@@ -1,5 +1,5 @@
1
1
  module Rex
2
2
  module Socket
3
- VERSION = "0.1.67"
3
+ VERSION = "0.1.69"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rex-socket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.67
4
+ version: 0.1.69
5
5
  platform: ruby
6
6
  authors:
7
7
  - Metasploit Hackers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-05-27 00:00:00.000000000 Z
11
+ date: 2026-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake