0N/A/*
2362N/A * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.net.util;
0N/A
0N/Apublic class IPAddressUtil {
0N/A private final static int INADDR4SZ = 4;
0N/A private final static int INADDR16SZ = 16;
0N/A private final static int INT16SZ = 2;
0N/A
0N/A /*
0N/A * Converts IPv4 address in its textual presentation form
0N/A * into its numeric binary form.
0N/A *
0N/A * @param src a String representing an IPv4 address in standard format
0N/A * @return a byte array representing the IPv4 numeric address
0N/A */
0N/A public static byte[] textToNumericFormatV4(String src)
0N/A {
0N/A if (src.length() == 0) {
0N/A return null;
0N/A }
0N/A
0N/A byte[] res = new byte[INADDR4SZ];
0N/A String[] s = src.split("\\.", -1);
0N/A long val;
0N/A try {
0N/A switch(s.length) {
0N/A case 1:
0N/A /*
0N/A * When only one part is given, the value is stored directly in
0N/A * the network address without any byte rearrangement.
0N/A */
0N/A
0N/A val = Long.parseLong(s[0]);
0N/A if (val < 0 || val > 0xffffffffL)
0N/A return null;
0N/A res[0] = (byte) ((val >> 24) & 0xff);
0N/A res[1] = (byte) (((val & 0xffffff) >> 16) & 0xff);
0N/A res[2] = (byte) (((val & 0xffff) >> 8) & 0xff);
0N/A res[3] = (byte) (val & 0xff);
0N/A break;
0N/A case 2:
0N/A /*
0N/A * When a two part address is supplied, the last part is
0N/A * interpreted as a 24-bit quantity and placed in the right
0N/A * most three bytes of the network address. This makes the
0N/A * two part address format convenient for specifying Class A
0N/A * network addresses as net.host.
0N/A */
0N/A
0N/A val = Integer.parseInt(s[0]);
0N/A if (val < 0 || val > 0xff)
0N/A return null;
0N/A res[0] = (byte) (val & 0xff);
0N/A val = Integer.parseInt(s[1]);
0N/A if (val < 0 || val > 0xffffff)
0N/A return null;
0N/A res[1] = (byte) ((val >> 16) & 0xff);
0N/A res[2] = (byte) (((val & 0xffff) >> 8) &0xff);
0N/A res[3] = (byte) (val & 0xff);
0N/A break;
0N/A case 3:
0N/A /*
0N/A * When a three part address is specified, the last part is
0N/A * interpreted as a 16-bit quantity and placed in the right
0N/A * most two bytes of the network address. This makes the
0N/A * three part address format convenient for specifying
0N/A * Class B net- work addresses as 128.net.host.
0N/A */
0N/A for (int i = 0; i < 2; i++) {
0N/A val = Integer.parseInt(s[i]);
0N/A if (val < 0 || val > 0xff)
0N/A return null;
0N/A res[i] = (byte) (val & 0xff);
0N/A }
0N/A val = Integer.parseInt(s[2]);
0N/A if (val < 0 || val > 0xffff)
0N/A return null;
0N/A res[2] = (byte) ((val >> 8) & 0xff);
0N/A res[3] = (byte) (val & 0xff);
0N/A break;
0N/A case 4:
0N/A /*
0N/A * When four parts are specified, each is interpreted as a
0N/A * byte of data and assigned, from left to right, to the
0N/A * four bytes of an IPv4 address.
0N/A */
0N/A for (int i = 0; i < 4; i++) {
0N/A val = Integer.parseInt(s[i]);
0N/A if (val < 0 || val > 0xff)
0N/A return null;
0N/A res[i] = (byte) (val & 0xff);
0N/A }
0N/A break;
0N/A default:
0N/A return null;
0N/A }
0N/A } catch(NumberFormatException e) {
0N/A return null;
0N/A }
0N/A return res;
0N/A }
0N/A
0N/A /*
0N/A * Convert IPv6 presentation level address to network order binary form.
0N/A * credit:
0N/A * Converted from C code from Solaris 8 (inet_pton)
0N/A *
0N/A * Any component of the string following a per-cent % is ignored.
0N/A *
0N/A * @param src a String representing an IPv6 address in textual format
0N/A * @return a byte array representing the IPv6 numeric address
0N/A */
0N/A public static byte[] textToNumericFormatV6(String src)
0N/A {
0N/A // Shortest valid string is "::", hence at least 2 chars
0N/A if (src.length() < 2) {
0N/A return null;
0N/A }
0N/A
0N/A int colonp;
0N/A char ch;
0N/A boolean saw_xdigit;
0N/A int val;
0N/A char[] srcb = src.toCharArray();
0N/A byte[] dst = new byte[INADDR16SZ];
0N/A
0N/A int srcb_length = srcb.length;
0N/A int pc = src.indexOf ("%");
0N/A if (pc == srcb_length -1) {
0N/A return null;
0N/A }
0N/A
0N/A if (pc != -1) {
0N/A srcb_length = pc;
0N/A }
0N/A
0N/A colonp = -1;
0N/A int i = 0, j = 0;
0N/A /* Leading :: requires some special handling. */
0N/A if (srcb[i] == ':')
0N/A if (srcb[++i] != ':')
0N/A return null;
0N/A int curtok = i;
0N/A saw_xdigit = false;
0N/A val = 0;
0N/A while (i < srcb_length) {
0N/A ch = srcb[i++];
0N/A int chval = Character.digit(ch, 16);
0N/A if (chval != -1) {
0N/A val <<= 4;
0N/A val |= chval;
0N/A if (val > 0xffff)
0N/A return null;
0N/A saw_xdigit = true;
0N/A continue;
0N/A }
0N/A if (ch == ':') {
0N/A curtok = i;
0N/A if (!saw_xdigit) {
0N/A if (colonp != -1)
0N/A return null;
0N/A colonp = j;
0N/A continue;
0N/A } else if (i == srcb_length) {
0N/A return null;
0N/A }
0N/A if (j + INT16SZ > INADDR16SZ)
0N/A return null;
0N/A dst[j++] = (byte) ((val >> 8) & 0xff);
0N/A dst[j++] = (byte) (val & 0xff);
0N/A saw_xdigit = false;
0N/A val = 0;
0N/A continue;
0N/A }
0N/A if (ch == '.' && ((j + INADDR4SZ) <= INADDR16SZ)) {
0N/A String ia4 = src.substring(curtok, srcb_length);
0N/A /* check this IPv4 address has 3 dots, ie. A.B.C.D */
0N/A int dot_count = 0, index=0;
0N/A while ((index = ia4.indexOf ('.', index)) != -1) {
0N/A dot_count ++;
0N/A index ++;
0N/A }
0N/A if (dot_count != 3) {
0N/A return null;
0N/A }
0N/A byte[] v4addr = textToNumericFormatV4(ia4);
0N/A if (v4addr == null) {
0N/A return null;
0N/A }
0N/A for (int k = 0; k < INADDR4SZ; k++) {
0N/A dst[j++] = v4addr[k];
0N/A }
0N/A saw_xdigit = false;
0N/A break; /* '\0' was seen by inet_pton4(). */
0N/A }
0N/A return null;
0N/A }
0N/A if (saw_xdigit) {
0N/A if (j + INT16SZ > INADDR16SZ)
0N/A return null;
0N/A dst[j++] = (byte) ((val >> 8) & 0xff);
0N/A dst[j++] = (byte) (val & 0xff);
0N/A }
0N/A
0N/A if (colonp != -1) {
0N/A int n = j - colonp;
0N/A
0N/A if (j == INADDR16SZ)
0N/A return null;
0N/A for (i = 1; i <= n; i++) {
0N/A dst[INADDR16SZ - i] = dst[colonp + n - i];
0N/A dst[colonp + n - i] = 0;
0N/A }
0N/A j = INADDR16SZ;
0N/A }
0N/A if (j != INADDR16SZ)
0N/A return null;
0N/A byte[] newdst = convertFromIPv4MappedAddress(dst);
0N/A if (newdst != null) {
0N/A return newdst;
0N/A } else {
0N/A return dst;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * @param src a String representing an IPv4 address in textual format
0N/A * @return a boolean indicating whether src is an IPv4 literal address
0N/A */
0N/A public static boolean isIPv4LiteralAddress(String src) {
0N/A return textToNumericFormatV4(src) != null;
0N/A }
0N/A
0N/A /**
0N/A * @param src a String representing an IPv6 address in textual format
0N/A * @return a boolean indicating whether src is an IPv6 literal address
0N/A */
0N/A public static boolean isIPv6LiteralAddress(String src) {
0N/A return textToNumericFormatV6(src) != null;
0N/A }
0N/A
0N/A /*
0N/A * Convert IPv4-Mapped address to IPv4 address. Both input and
0N/A * returned value are in network order binary form.
0N/A *
0N/A * @param src a String representing an IPv4-Mapped address in textual format
0N/A * @return a byte array representing the IPv4 numeric address
0N/A */
0N/A public static byte[] convertFromIPv4MappedAddress(byte[] addr) {
0N/A if (isIPv4MappedAddress(addr)) {
0N/A byte[] newAddr = new byte[INADDR4SZ];
0N/A System.arraycopy(addr, 12, newAddr, 0, INADDR4SZ);
0N/A return newAddr;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the InetAddress is an
0N/A * IPv4 mapped IPv6 address.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the InetAddress is
0N/A * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
0N/A */
0N/A private static boolean isIPv4MappedAddress(byte[] addr) {
0N/A if (addr.length < INADDR16SZ) {
0N/A return false;
0N/A }
0N/A if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
0N/A (addr[2] == 0x00) && (addr[3] == 0x00) &&
0N/A (addr[4] == 0x00) && (addr[5] == 0x00) &&
0N/A (addr[6] == 0x00) && (addr[7] == 0x00) &&
0N/A (addr[8] == 0x00) && (addr[9] == 0x00) &&
0N/A (addr[10] == (byte)0xff) &&
0N/A (addr[11] == (byte)0xff)) {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A}