-
Notifications
You must be signed in to change notification settings - Fork 974
/
NetworkUtils.java
172 lines (156 loc) · 6.94 KB
/
NetworkUtils.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.util;
import io.undertow.UndertowMessages;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Stuart Douglas
* @author baranowb
*/
public class NetworkUtils {
public static final String IP4_EXACT = "(?:\\d{1,3}\\.){3}\\d{1,3}";
/**
* IPV6 match. ?: - unnamed groups are used for performance reasons.
* Requirements:
* - match full or partial IPV6 ( sliding '::')
* - match end to start - ^$ to ensure it does not match part of some random (\d:){n,m}
* - IPv4-Embedded IPv6 Address
*
* NO:
* - IPv4 mapped/translated into IPv6
*
* ^(?:([0-9a-fA-F]{1,4}:){7,7}(?:[0-9a-fA-F]){1,4} - full address
* |(?:([0-9a-fA-F]{1,4}:)){1,7}(?:(:)) - last compressed
* |(?:([0-9a-fA-F]{1,4}:)){1,6}(?:(:[0-9a-fA-F]){1,4}) - second to last
* |(?:([0-9a-fA-F]{1,4}:)){1,5}(?:(:[0-9a-fA-F]{1,4})){1,2} - etc
* |(?:([0-9a-fA-F]{1,4}:)){1,4}(?:(:[0-9a-fA-F]{1,4})){1,3}
* |(?:([0-9a-fA-F]{1,4}:)){1,3}(?:(:[0-9a-fA-F]{1,4})){1,4}
* |(?:([0-9a-fA-F]{1,4}:)){1,2}(?:(:[0-9a-fA-F]{1,4})){1,5}
* |(?:([0-9a-fA-F]{1,4}:))(?:(:[0-9a-fA-F]{1,4})){1,6}
* |(?:(:))(?:((:[0-9a-fA-F]{1,4}){1,7}|(?:(:)))))$ - all the way compressed
*/
public static final String IP6_EXACT = "^(?:([0-9a-fA-F]{1,4}:){7,7}(?:[0-9a-fA-F]){1,4}|(?:([0-9a-fA-F]{1,4}:)){1,7}(?:(:))|(?:([0-9a-fA-F]{1,4}:)){1,6}(?:(:[0-9a-fA-F]){1,4})|(?:([0-9a-fA-F]{1,4}:)){1,5}(?:(:[0-9a-fA-F]{1,4})){1,2}|(?:([0-9a-fA-F]{1,4}:)){1,4}(?:(:[0-9a-fA-F]{1,4})){1,3}|(?:([0-9a-fA-F]{1,4}:)){1,3}(?:(:[0-9a-fA-F]{1,4})){1,4}|(?:([0-9a-fA-F]{1,4}:)){1,2}(?:(:[0-9a-fA-F]{1,4})){1,5}|(?:([0-9a-fA-F]{1,4}:))(?:(:[0-9a-fA-F]{1,4})){1,6}|(?:(:))(?:((:[0-9a-fA-F]{1,4}){1,7}|(?:(:)))))$";
public static String formatPossibleIpv6Address(String address) {
if (address == null) {
return null;
}
if (!address.contains(":")) {
return address;
}
if (address.startsWith("[") && address.endsWith("]")) {
return address;
}
return "[" + address + "]";
}
public static InetAddress parseIpv4Address(String addressString) throws IOException {
String[] parts = addressString.split("\\.");
if (parts.length != 4) {
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
}
byte[] data = new byte[4];
for (int i = 0; i < 4; ++i) {
String part = parts[i];
if (part.length() == 0 || (part.charAt(0) == '0' && part.length() > 1)) {
//leading zeros are not allowed
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
}
data[i] = (byte) Integer.parseInt(part);
}
return InetAddress.getByAddress(data);
}
public static InetAddress parseIpv6Address(final String addressString) throws IllegalArgumentException, UnknownHostException {
return InetAddress.getByAddress(parseIpv6AddressToBytes(addressString));
}
public static byte[] parseIpv6AddressToBytes(final String addressString) throws IllegalArgumentException, UnknownHostException {
boolean startsWithColon = addressString.startsWith(":");
if (startsWithColon && !addressString.startsWith("::")) {
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
}
String[] parts = splitIPv6(addressString);
byte[] data = new byte[16];
int partOffset = 0;
boolean seenEmpty = false;
if (parts.length > 8) {
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
}
for (int i = 0; i < parts.length; ++i) {
String part = parts[i];
if (part.length() > 4) {
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
} else if (part.isEmpty()) {
if (seenEmpty) {
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
}
seenEmpty = true;
int off = 8 - parts.length;//this works because of the empty part that represents the double colon, so the parts list is one larger than the number of digits
if (off < 0) {
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
}
partOffset = off * 2;
} else {
int num = Integer.parseInt(part, 16);
data[i * 2 + partOffset] = (byte) (num >> 8);
data[i * 2 + partOffset + 1] = (byte) (num);
}
}
if ((parts.length < 8 && !addressString.endsWith("::")) && !seenEmpty) {
//address was too small
throw UndertowMessages.MESSAGES.invalidIpAddress(addressString);
}
return data;
}
private static String[] splitIPv6(final String src) {
final List<String> list = new ArrayList<>();
final int size = src.length();
char previous = 0;
final StringBuilder bits = new StringBuilder(8);
for(int i = 0; i<size;i++) {
final char current = src.charAt(i);
if(current != ':'){
bits.append(String.valueOf(current));
}
if(previous == current && current == ':') {
list.add("");
} else if (((current == ':') || (i == size - 1)) && bits.length() > 0) {
list.add(bits.toString());
bits.setLength(0);
}
previous = current;
}
return list.toArray(new String[list.size()]);
}
public static String toObfuscatedString(InetAddress address) {
if (address == null) {
return null;
}
String s = address.getHostAddress();
if (address instanceof Inet4Address) {
// IPv4 addresses: cut off last byte
return s.substring(0, s.lastIndexOf(".")+1);
}
// IPv6 addresses: cut off at second colon
return s.substring(0, s.indexOf(":", s.indexOf(":")+1)+1);
}
private NetworkUtils() {
}
}