|
7 | 7 | import java.net.Inet6Address; |
8 | 8 | import java.net.InetAddress; |
9 | 9 | import java.net.UnknownHostException; |
| 10 | +import lombok.experimental.UtilityClass; |
10 | 11 |
|
11 | 12 | /** |
12 | 13 | * Routines dealing with IP addresses. Includes functions similar to those in the |
13 | 14 | * java.net.InetAddress class. |
14 | 15 | * |
15 | 16 | * @author Brian Wellington |
16 | 17 | */ |
| 18 | +@UtilityClass |
17 | 19 | public final class Address { |
18 | 20 |
|
19 | 21 | public static final int IPv4 = 1; |
20 | 22 | public static final int IPv6 = 2; |
21 | 23 |
|
22 | | - private Address() {} |
23 | | - |
24 | | - private static byte[] parseV4(String s) { |
25 | | - int numDigits; |
26 | | - int currentOctet; |
27 | | - byte[] values = new byte[4]; |
28 | | - int currentValue; |
29 | | - int length = s.length(); |
30 | | - |
31 | | - currentOctet = 0; |
32 | | - currentValue = 0; |
33 | | - numDigits = 0; |
34 | | - for (int i = 0; i < length; i++) { |
35 | | - char c = s.charAt(i); |
36 | | - if (c >= '0' && c <= '9') { |
37 | | - /* Can't have more than 3 digits per octet. */ |
38 | | - if (numDigits == 3) { |
39 | | - return null; |
40 | | - } |
41 | | - /* Octets shouldn't start with 0, unless they are 0. */ |
42 | | - if (numDigits > 0 && currentValue == 0) { |
43 | | - return null; |
44 | | - } |
45 | | - numDigits++; |
46 | | - currentValue *= 10; |
47 | | - currentValue += c - '0'; |
48 | | - /* 255 is the maximum value for an octet. */ |
49 | | - if (currentValue > 255) { |
50 | | - return null; |
51 | | - } |
52 | | - } else if (c == '.') { |
53 | | - /* Can't have more than 3 dots. */ |
54 | | - if (currentOctet == 3) { |
55 | | - return null; |
56 | | - } |
57 | | - /* Two consecutive dots are bad. */ |
58 | | - if (numDigits == 0) { |
59 | | - return null; |
60 | | - } |
61 | | - values[currentOctet++] = (byte) currentValue; |
62 | | - currentValue = 0; |
63 | | - numDigits = 0; |
64 | | - } else { |
65 | | - return null; |
66 | | - } |
67 | | - } |
68 | | - /* Must have 4 octets. */ |
69 | | - if (currentOctet != 3) { |
70 | | - return null; |
71 | | - } |
72 | | - /* The fourth octet can't be empty. */ |
73 | | - if (numDigits == 0) { |
74 | | - return null; |
75 | | - } |
76 | | - values[currentOctet] = (byte) currentValue; |
77 | | - return values; |
78 | | - } |
79 | | - |
80 | | - private static byte[] parseV6(String s) { |
81 | | - int range = -1; |
82 | | - byte[] data = new byte[16]; |
83 | | - |
84 | | - String[] tokens = s.split(":", -1); |
85 | | - |
86 | | - int first = 0; |
87 | | - int last = tokens.length - 1; |
88 | | - |
89 | | - if (tokens[0].isEmpty()) { |
90 | | - // If the first two tokens are empty, it means the string |
91 | | - // started with ::, which is fine. If only the first is |
92 | | - // empty, the string started with :, which is bad. |
93 | | - if (last - first > 0 && tokens[1].isEmpty()) { |
94 | | - first++; |
95 | | - } else { |
96 | | - return null; |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - if (tokens[last].isEmpty()) { |
101 | | - // If the last two tokens are empty, it means the string |
102 | | - // ended with ::, which is fine. If only the last is |
103 | | - // empty, the string ended with :, which is bad. |
104 | | - if (last - first > 0 && tokens[last - 1].isEmpty()) { |
105 | | - last--; |
106 | | - } else { |
107 | | - return null; |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - if (last - first + 1 > 8) { |
112 | | - return null; |
113 | | - } |
114 | | - |
115 | | - int i, j; |
116 | | - for (i = first, j = 0; i <= last; i++) { |
117 | | - if (tokens[i].isEmpty()) { |
118 | | - if (range >= 0) { |
119 | | - return null; |
120 | | - } |
121 | | - range = j; |
122 | | - continue; |
123 | | - } |
124 | | - |
125 | | - if (tokens[i].indexOf('.') >= 0) { |
126 | | - // An IPv4 address must be the last component |
127 | | - if (i < last) { |
128 | | - return null; |
129 | | - } |
130 | | - // There can't have been more than 6 components. |
131 | | - if (i > 6) { |
132 | | - return null; |
133 | | - } |
134 | | - byte[] v4addr = Address.toByteArray(tokens[i], IPv4); |
135 | | - if (v4addr == null) { |
136 | | - return null; |
137 | | - } |
138 | | - for (int k = 0; k < 4; k++) { |
139 | | - data[j++] = v4addr[k]; |
140 | | - } |
141 | | - break; |
142 | | - } |
143 | | - |
144 | | - try { |
145 | | - for (int k = 0; k < tokens[i].length(); k++) { |
146 | | - char c = tokens[i].charAt(k); |
147 | | - if (Character.digit(c, 16) < 0) { |
148 | | - return null; |
149 | | - } |
150 | | - } |
151 | | - int x = Integer.parseInt(tokens[i], 16); |
152 | | - if (x > 0xFFFF || x < 0) { |
153 | | - return null; |
154 | | - } |
155 | | - data[j++] = (byte) (x >>> 8); |
156 | | - data[j++] = (byte) (x & 0xFF); |
157 | | - } catch (NumberFormatException e) { |
158 | | - return null; |
159 | | - } |
160 | | - } |
161 | | - |
162 | | - if (j < 16 && range < 0) { |
163 | | - return null; |
164 | | - } |
165 | | - |
166 | | - if (range >= 0) { |
167 | | - int empty = 16 - j; |
168 | | - System.arraycopy(data, range, data, range + empty, j - range); |
169 | | - for (i = range; i < range + empty; i++) { |
170 | | - data[i] = 0; |
171 | | - } |
172 | | - } |
173 | | - |
174 | | - return data; |
175 | | - } |
176 | | - |
177 | 24 | /** |
178 | 25 | * Convert a string containing an IP address to an array of 4 or 16 integers. |
179 | 26 | * |
@@ -212,9 +59,9 @@ public static int[] toArray(String s) { |
212 | 59 | */ |
213 | 60 | public static byte[] toByteArray(String s, int family) { |
214 | 61 | if (family == IPv4) { |
215 | | - return parseV4(s); |
| 62 | + return IPAddressUtils.parseV4(s); |
216 | 63 | } else if (family == IPv6) { |
217 | | - return parseV6(s); |
| 64 | + return IPAddressUtils.parseV6(s); |
218 | 65 | } else { |
219 | 66 | throw new IllegalArgumentException("unknown address family"); |
220 | 67 | } |
@@ -450,7 +297,7 @@ public static InetAddress truncate(InetAddress address, int maskLength) { |
450 | 297 | for (int i = 0; i < maskBits; i++) { |
451 | 298 | bitmask |= 1 << (7 - i); |
452 | 299 | } |
453 | | - bytes[maskLength / 8] &= bitmask; |
| 300 | + bytes[maskLength / 8] &= (byte) bitmask; |
454 | 301 | try { |
455 | 302 | return InetAddress.getByAddress(bytes); |
456 | 303 | } catch (UnknownHostException e) { |
|
0 commit comments