| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.util; |
| 12 |
|
| 13 |
import java.net.Inet4Address; |
| 14 |
import java.net.Inet6Address; |
| 15 |
import java.net.InetAddress; |
| 16 |
import java.net.UnknownHostException; |
| 17 |
import java.util.regex.Pattern; |
| 18 |
|
| 19 |
import org.minidns.dnsname.DnsName; |
| 20 |
|
| 21 |
public class InetAddressUtil { |
| 22 |
|
| 23 |
public static Inet4Address ipv4From(CharSequence cs) { |
| 24 |
InetAddress inetAddress; |
| 25 |
try { |
| 26 |
inetAddress = InetAddress.getByName(cs.toString()); |
| 27 |
} catch (UnknownHostException e) { |
| 28 |
throw new IllegalArgumentException(e); |
| 29 |
} |
| 30 |
if (inetAddress instanceof Inet4Address) { |
| 31 |
return (Inet4Address) inetAddress; |
| 32 |
} |
| 33 |
throw new IllegalArgumentException(); |
| 34 |
} |
| 35 |
|
| 36 |
public static Inet6Address ipv6From(CharSequence cs) { |
| 37 |
InetAddress inetAddress; |
| 38 |
try { |
| 39 |
inetAddress = InetAddress.getByName(cs.toString()); |
| 40 |
} catch (UnknownHostException e) { |
| 41 |
throw new IllegalArgumentException(e); |
| 42 |
} |
| 43 |
if (inetAddress instanceof Inet6Address) { |
| 44 |
return (Inet6Address) inetAddress; |
| 45 |
} |
| 46 |
throw new IllegalArgumentException(); |
| 47 |
} |
| 48 |
|
| 49 |
// IPV4_REGEX from http://stackoverflow.com/a/46168/194894 by Kevin Wong (http://stackoverflow.com/users/4792/kevin-wong) licensed under |
| 50 |
// CC BY-SA 3.0. |
| 51 |
private static final Pattern IPV4_PATTERN = Pattern.compile("\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"); |
| 52 |
|
| 53 |
public static boolean isIpV4Address(CharSequence address) { |
| 54 |
if (address == null) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
return IPV4_PATTERN.matcher(address).matches(); |
| 58 |
} |
| 59 |
|
| 60 |
// IPv6 Regular Expression from http://stackoverflow.com/a/17871737/194894 by David M. Syzdek |
| 61 |
// (http://stackoverflow.com/users/903194/david-m-syzdek) licensed under CC BY-SA 3.0. |
| 62 |
private static final Pattern IPV6_PATTERN = Pattern.compile( |
| 63 |
"(([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}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"); |
| 64 |
|
| 65 |
public static boolean isIpV6Address(CharSequence address) { |
| 66 |
if (address == null) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
return IPV6_PATTERN.matcher(address).matches(); |
| 70 |
} |
| 71 |
|
| 72 |
public static boolean isIpAddress(CharSequence address) { |
| 73 |
return isIpV6Address(address) || isIpV4Address(address); |
| 74 |
} |
| 75 |
|
| 76 |
public static InetAddress convertToInetAddressIfPossible(CharSequence address) { |
| 77 |
if (!isIpAddress(address)) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
String addressString = address.toString(); |
| 82 |
try { |
| 83 |
return InetAddress.getByName(addressString); |
| 84 |
} catch (UnknownHostException e) { |
| 85 |
// Should never happen. |
| 86 |
throw new AssertionError(e); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public static DnsName reverseIpAddressOf(Inet6Address inet6Address) { |
| 91 |
final String ipAddress = inet6Address.getHostAddress(); |
| 92 |
final String[] ipAddressParts = ipAddress.split(":"); |
| 93 |
|
| 94 |
String[] parts = new String[32]; |
| 95 |
int currentPartNum = 0; |
| 96 |
for (int i = ipAddressParts.length - 1; i >= 0; i--) { |
| 97 |
final String currentPart = ipAddressParts[i]; |
| 98 |
final int missingPlaces = 4 - currentPart.length(); |
| 99 |
for (int j = 0; j < missingPlaces; j++) { |
| 100 |
parts[currentPartNum++] = "0"; |
| 101 |
} |
| 102 |
for (int j = 0; j < currentPart.length(); j++) { |
| 103 |
parts[currentPartNum++] = Character.toString(currentPart.charAt(j)); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return DnsName.from(parts); |
| 108 |
} |
| 109 |
|
| 110 |
public static DnsName reverseIpAddressOf(Inet4Address inet4Address) { |
| 111 |
final String[] ipAddressParts = inet4Address.getHostAddress().split("\\."); |
| 112 |
assert ipAddressParts.length == 4; |
| 113 |
|
| 114 |
return DnsName.from(ipAddressParts); |
| 115 |
} |
| 116 |
} |