From 98be832179f1750179782cec587609c32e285987 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Fri, 26 Jul 2024 15:23:04 +0800 Subject: [PATCH] Simplify expand IPv6 --- lib/openssl/ssl.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/openssl/ssl.rb b/lib/openssl/ssl.rb index 1cb2ad528..a72fd0e05 100644 --- a/lib/openssl/ssl.rb +++ b/lib/openssl/ssl.rb @@ -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