Skip to content

Commit

Permalink
Simplify expand IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Jul 26, 2024
1 parent 4e9c478 commit 98be832
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/openssl/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,16 @@ def timeout=(value)
if ip.count('.') == 3 # IPv4
ip.split('.').map(&:to_i).pack('C*')
elsif ip.include?(':') # IPv6
hextets = ip.split(':')
if hextets.count('') > 1
raise ArgumentError, "Invalid IP address format"
if ip.include?('::')
parts = ip.split('::')
left = parts[0].split(':')
right = parts[1] ? parts[1].split(':') : []
middle = ['0'] * (8 - left.size - right.size)
ip = (left + middle + right)
else
ip = ip.split(':')
end
if hextets.include?('')
empty_index = hextets.index('')
sub_hextets = hextets[empty_index + 1..-1]
hextets.delete_at(empty_index)
hextets.fill('0', empty_index, 8 - hextets.size)
hextets += sub_hextets
end
hextets.map { |h| h.hex }.pack('n*')
ip.map { |h| h.hex }.pack('n*')
else
raise ArgumentError, "Invalid IP address format"
end
Expand Down

0 comments on commit 98be832

Please sign in to comment.