1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.net;
29
30 import java.net.InetAddress;
31 import java.net.InetSocketAddress;
32 import java.net.SocketAddress;
33 import java.net.UnknownHostException;
34 import java.util.regex.Pattern;
35
36 import org.apache.hc.core5.util.Args;
37
38
39
40
41
42
43 public class InetAddressUtils {
44
45
46
47
48
49
50 public static final byte IPV4 = 1;
51
52
53
54
55
56 public static final byte IPV6 = 4;
57
58 private InetAddressUtils() {
59 }
60
61 private static final String IPV4_BASIC_PATTERN_STRING =
62 "(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){1}" +
63 "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){2}" +
64 "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
65
66 private static final Pattern IPV4_PATTERN =
67 Pattern.compile("^" + IPV4_BASIC_PATTERN_STRING + "$");
68
69 private static final Pattern IPV4_MAPPED_IPV6_PATTERN =
70 Pattern.compile("^::[fF]{4}:" + IPV4_BASIC_PATTERN_STRING + "$");
71
72 private static final Pattern IPV6_STD_PATTERN =
73 Pattern.compile(
74 "^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$");
75
76 private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =
77 Pattern.compile(
78 "^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" +
79 "::" +
80 "(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$");
81
82
83
84
85
86 private static final Pattern SCOPE_ID_PATTERN = Pattern.compile("^[a-zA-Z0-9\\-]+$");
87
88
89
90
91 private static final String SCOPE_ID_DELIMITER = "%";
92
93
94
95
96
97
98 private static final char COLON_CHAR = ':';
99
100
101 private static final int MAX_COLON_COUNT = 7;
102
103
104
105
106
107
108
109 public static boolean isIPv4Address(final String input) {
110 return IPV4_PATTERN.matcher(input).matches();
111 }
112
113 public static boolean isIPv4MappedIPv64Address(final String input) {
114 return IPV4_MAPPED_IPV6_PATTERN.matcher(input).matches();
115 }
116
117 static boolean hasValidIPv6ColonCount(final String input) {
118 int colonCount = 0;
119 for (int i = 0; i < input.length(); i++) {
120 if (input.charAt(i) == COLON_CHAR) {
121 colonCount++;
122 }
123 }
124
125 return colonCount >= 2 && colonCount <= MAX_COLON_COUNT;
126 }
127
128
129
130
131
132
133
134 public static boolean isIPv6StdAddress(final String input) {
135 return hasValidIPv6ColonCount(input) && IPV6_STD_PATTERN.matcher(input).matches();
136 }
137
138
139
140
141
142
143
144 public static boolean isIPv6HexCompressedAddress(final String input) {
145 return hasValidIPv6ColonCount(input) && IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();
146 }
147
148
149
150
151
152
153
154 public static boolean isIPv6Address(final String input) {
155 final int index = input.indexOf(SCOPE_ID_DELIMITER);
156 if (index == -1) {
157 return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input);
158 } else {
159 final String address = input.substring(0, index);
160 if (isIPv6StdAddress(address) || isIPv6HexCompressedAddress(address)) {
161
162 final String scopeId = input.substring(index + 1);
163
164 return !scopeId.isEmpty() && SCOPE_ID_PATTERN.matcher(scopeId).matches();
165 } else {
166 return false;
167 }
168 }
169 }
170
171
172
173
174
175
176
177
178 public static boolean isIPv6URLBracketedAddress(final String input) {
179 return input.startsWith("[") && input.endsWith("]") && isIPv6Address(input.substring(1, input.length() - 1));
180 }
181
182
183
184
185
186
187 public static void formatAddress(
188 final StringBuilder buffer,
189 final SocketAddress socketAddress) {
190 Args.notNull(buffer, "buffer");
191 if (socketAddress instanceof InetSocketAddress) {
192 final InetSocketAddress socketaddr = (InetSocketAddress) socketAddress;
193 final InetAddress inetaddr = socketaddr.getAddress();
194 if (inetaddr != null) {
195 buffer.append(inetaddr.getHostAddress()).append(':').append(socketaddr.getPort());
196 } else {
197 buffer.append(socketAddress);
198 }
199 } else {
200 buffer.append(socketAddress);
201 }
202 }
203
204
205
206
207
208
209 public static String getCanonicalLocalHostName() {
210 try {
211 final InetAddress localHost = InetAddress.getLocalHost();
212 return localHost.getCanonicalHostName();
213 } catch (final UnknownHostException ex) {
214 return "localhost";
215 }
216 }
217
218 }