1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.commons.validator.routines; |
19 | |
|
20 | |
import java.io.Serializable; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Arrays; |
23 | |
import java.util.List; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 8 | public class InetAddressValidator implements Serializable { |
38 | |
|
39 | |
private static final int IPV4_MAX_OCTET_VALUE = 255; |
40 | |
|
41 | |
private static final int MAX_UNSIGNED_SHORT = 0xffff; |
42 | |
|
43 | |
private static final int BASE_16 = 16; |
44 | |
|
45 | |
private static final long serialVersionUID = -919201640201914789L; |
46 | |
|
47 | |
private static final String IPV4_REGEX = |
48 | |
"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; |
49 | |
|
50 | |
|
51 | |
private static final int IPV6_MAX_HEX_GROUPS = 8; |
52 | |
|
53 | |
|
54 | |
private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP = 4; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 1 | private static final InetAddressValidator VALIDATOR = new InetAddressValidator(); |
60 | |
|
61 | |
|
62 | 8 | private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX); |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public static InetAddressValidator getInstance() { |
69 | 56866 | return VALIDATOR; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public boolean isValid(String inetAddress) { |
78 | 28453 | return isValidInet4Address(inetAddress) || isValidInet6Address(inetAddress); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public boolean isValidInet4Address(String inet4Address) { |
87 | |
|
88 | 79104 | String[] groups = ipv4Validator.match(inet4Address); |
89 | |
|
90 | 79104 | if (groups == null) { |
91 | 66599 | return false; |
92 | |
} |
93 | |
|
94 | |
|
95 | 41664 | for (String ipSegment : groups) { |
96 | 34394 | if (ipSegment == null || ipSegment.length() == 0) { |
97 | 0 | return false; |
98 | |
} |
99 | |
|
100 | 34394 | int iIpSegment = 0; |
101 | |
|
102 | |
try { |
103 | 34394 | iIpSegment = Integer.parseInt(ipSegment); |
104 | 0 | } catch(NumberFormatException e) { |
105 | 0 | return false; |
106 | 34394 | } |
107 | |
|
108 | 34394 | if (iIpSegment > IPV4_MAX_OCTET_VALUE) { |
109 | 5232 | return false; |
110 | |
} |
111 | |
|
112 | 29162 | if (ipSegment.length() > 1 && ipSegment.startsWith("0")) { |
113 | 3 | return false; |
114 | |
} |
115 | |
|
116 | |
} |
117 | |
|
118 | 7270 | return true; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public boolean isValidInet6Address(String inet6Address) { |
129 | 25763 | boolean containsCompressedZeroes = inet6Address.contains("::"); |
130 | 25763 | if (containsCompressedZeroes && (inet6Address.indexOf("::") != inet6Address.lastIndexOf("::"))) { |
131 | 73 | return false; |
132 | |
} |
133 | 25690 | if ((inet6Address.startsWith(":") && !inet6Address.startsWith("::")) |
134 | |
|| (inet6Address.endsWith(":") && !inet6Address.endsWith("::"))) { |
135 | 105 | return false; |
136 | |
} |
137 | 25585 | String[] octets = inet6Address.split(":"); |
138 | 25585 | if (containsCompressedZeroes) { |
139 | 222 | List<String> octetList = new ArrayList<String>(Arrays.asList(octets)); |
140 | 222 | if (inet6Address.endsWith("::")) { |
141 | |
|
142 | 29 | octetList.add(""); |
143 | 193 | } else if (inet6Address.startsWith("::") && !octetList.isEmpty()) { |
144 | 75 | octetList.remove(0); |
145 | |
} |
146 | 222 | octets = octetList.toArray(new String[octetList.size()]); |
147 | |
} |
148 | 25585 | if (octets.length > IPV6_MAX_HEX_GROUPS) { |
149 | 8 | return false; |
150 | |
} |
151 | 25577 | int validOctets = 0; |
152 | 25577 | int emptyOctets = 0; |
153 | 30006 | for (int index = 0; index < octets.length; index++) { |
154 | 26651 | String octet = octets[index]; |
155 | 26651 | if (octet.length() == 0) { |
156 | 1802 | emptyOctets++; |
157 | 1802 | if (emptyOctets > 1) { |
158 | 0 | return false; |
159 | |
} |
160 | |
} else { |
161 | 24849 | emptyOctets = 0; |
162 | |
|
163 | 24849 | if (index == octets.length - 1 && octet.contains(".")) { |
164 | 22214 | if (!isValidInet4Address(octet)) { |
165 | 22157 | return false; |
166 | |
} |
167 | 57 | validOctets += 2; |
168 | 57 | continue; |
169 | |
} |
170 | 2635 | if (octet.length() > IPV6_MAX_HEX_DIGITS_PER_GROUP) { |
171 | 52 | return false; |
172 | |
} |
173 | 2583 | int octetInt = 0; |
174 | |
try { |
175 | 2583 | octetInt = Integer.parseInt(octet, BASE_16); |
176 | 13 | } catch (NumberFormatException e) { |
177 | 13 | return false; |
178 | 2570 | } |
179 | 2570 | if (octetInt < 0 || octetInt > MAX_UNSIGNED_SHORT) { |
180 | 0 | return false; |
181 | |
} |
182 | |
} |
183 | 4372 | validOctets++; |
184 | |
} |
185 | 3355 | if (validOctets > IPV6_MAX_HEX_GROUPS || (validOctets < IPV6_MAX_HEX_GROUPS && !containsCompressedZeroes)) { |
186 | 3184 | return false; |
187 | |
} |
188 | 171 | return true; |
189 | |
} |
190 | |
} |