@@ -50,11 +50,16 @@ def self.validate_destination(destination)
50
50
return true if parse_ip ( address_list . first )
51
51
52
52
elsif address_list . length == 2
53
- ipv4s = parse_ip ( address_list )
54
- return false if ipv4s . nil?
53
+ ips = parse_ip ( address_list )
54
+ return false if ips . nil?
55
55
56
- sorted_ipv4s = NetAddr . sort_IPv4 ( ipv4s )
57
- return true if ipv4s . first == sorted_ipv4s . first
56
+ sorted_ips = if ips . first . is_a? ( NetAddr ::IPv4 )
57
+ NetAddr . sort_IPv4 ( ips )
58
+ else
59
+ NetAddr . sort_IPv6 ( ips )
60
+ end
61
+
62
+ return true if ips . first == sorted_ips . first
58
63
end
59
64
60
65
false
@@ -65,19 +70,14 @@ def self.validate_boolean(bool)
65
70
end
66
71
67
72
def self . parse_ip ( val )
68
- if val . is_a? ( Array )
69
- val . map do |ip |
70
- NetAddr ::IPv4 . parse ( ip )
71
- end
72
- else
73
- NetAddr ::IPv4Net . parse ( val )
74
- end
75
- rescue NetAddr ::ValidationError
76
- nil
73
+ ipv4 = parse_ipv4 ( val )
74
+
75
+ ipv6 = parse_ipv6 ( val ) if !ipv4 && config . get ( :enable_ipv6 )
76
+
77
+ ipv4 || ipv6
77
78
end
78
79
79
80
def self . comma_delimited_destinations_enabled?
80
- config = VCAP ::CloudController ::Config . config
81
81
config . get ( :security_groups , :enable_comma_delimited_destinations )
82
82
end
83
83
@@ -92,7 +92,17 @@ def self.no_leading_zeros(destination)
92
92
no_zeros
93
93
end
94
94
95
+ private_class_method def self . config
96
+ VCAP ::CloudController ::Config . config
97
+ end
98
+
95
99
private_class_method def self . no_leading_zeros_in_address ( address )
100
+ return no_leading_zeros_in_ipv4_address ( address ) if address . include? ( '.' )
101
+
102
+ no_leading_zeros_in_ipv6_address ( address ) if address . include? ( ':' )
103
+ end
104
+
105
+ private_class_method def self . no_leading_zeros_in_ipv4_address ( address )
96
106
address . split ( '.' ) do |octet |
97
107
if octet . start_with? ( '0' ) && octet . length > 1
98
108
octet_parts = octet . split ( '/' )
@@ -104,5 +114,42 @@ def self.no_leading_zeros(destination)
104
114
105
115
true
106
116
end
117
+
118
+ private_class_method def self . no_leading_zeros_in_ipv6_address ( address )
119
+ address . split ( ':' ) . each do |segment |
120
+ next unless segment . start_with? ( '0' ) && segment . length > 1
121
+
122
+ segment_parts = segment . split ( '/' )
123
+ return false if segment_parts . length < 2
124
+
125
+ return false if segment_parts [ 0 ] . length > 1 && segment_parts [ 0 ] . start_with? ( '0' )
126
+ end
127
+
128
+ true
129
+ end
130
+
131
+ private_class_method def self . parse_ipv4 ( val )
132
+ if val . is_a? ( Array )
133
+ val . map do |ip |
134
+ NetAddr ::IPv4 . parse ( ip )
135
+ end
136
+ else
137
+ NetAddr ::IPv4Net . parse ( val )
138
+ end
139
+ rescue NetAddr ::ValidationError
140
+ nil
141
+ end
142
+
143
+ private_class_method def self . parse_ipv6 ( val )
144
+ if val . is_a? ( Array )
145
+ val . map do |ip |
146
+ NetAddr ::IPv6 . parse ( ip )
147
+ end
148
+ else
149
+ NetAddr ::IPv6Net . parse ( val )
150
+ end
151
+ rescue NetAddr ::ValidationError
152
+ nil
153
+ end
107
154
end
108
155
end
0 commit comments