jdk/src/java.base/share/classes/sun/net/util/IPAddressUtil.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 32271 d6cb1602f271
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32271
d6cb1602f271 8087190: Regression in sun.net.util.IPAddressUtil.isIPv4LiteralAddress(String)
robm
parents: 25859
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
public class IPAddressUtil {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32271
diff changeset
    29
    private static final int INADDR4SZ = 4;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32271
diff changeset
    30
    private static final int INADDR16SZ = 16;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32271
diff changeset
    31
    private static final int INT16SZ = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
     * Converts IPv4 address in its textual presentation form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     * into its numeric binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * @param src a String representing an IPv4 address in standard format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * @return a byte array representing the IPv4 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     */
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    40
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static byte[] textToNumericFormatV4(String src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    {
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    43
        byte[] res = new byte[INADDR4SZ];
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    44
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    45
        long tmpValue = 0;
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    46
        int currByte = 0;
32271
d6cb1602f271 8087190: Regression in sun.net.util.IPAddressUtil.isIPv4LiteralAddress(String)
robm
parents: 25859
diff changeset
    47
        boolean newOctet = true;
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    48
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    49
        int len = src.length();
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    50
        if (len == 0 || len > 15) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    53
        /*
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    54
         * When only one part is given, the value is stored directly in
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    55
         * the network address without any byte rearrangement.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    56
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    57
         * When a two part address is supplied, the last part is
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    58
         * interpreted as a 24-bit quantity and placed in the right
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    59
         * most three bytes of the network address. This makes the
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    60
         * two part address format convenient for specifying Class A
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    61
         * network addresses as net.host.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    62
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    63
         * When a three part address is specified, the last part is
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    64
         * interpreted as a 16-bit quantity and placed in the right
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    65
         * most two bytes of the network address. This makes the
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    66
         * three part address format convenient for specifying
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    67
         * Class B net- work addresses as 128.net.host.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    68
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    69
         * When four parts are specified, each is interpreted as a
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    70
         * byte of data and assigned, from left to right, to the
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    71
         * four bytes of an IPv4 address.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    72
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    73
         * We determine and parse the leading parts, if any, as single
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    74
         * byte values in one pass directly into the resulting byte[],
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    75
         * then the remainder is treated as a 8-to-32-bit entity and
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    76
         * translated into the remaining bytes in the array.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    77
         */
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    78
        for (int i = 0; i < len; i++) {
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    79
            char c = src.charAt(i);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    80
            if (c == '.') {
32271
d6cb1602f271 8087190: Regression in sun.net.util.IPAddressUtil.isIPv4LiteralAddress(String)
robm
parents: 25859
diff changeset
    81
                if (newOctet || tmpValue < 0 || tmpValue > 0xff || currByte == 3) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    84
                res[currByte++] = (byte) (tmpValue & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    85
                tmpValue = 0;
32271
d6cb1602f271 8087190: Regression in sun.net.util.IPAddressUtil.isIPv4LiteralAddress(String)
robm
parents: 25859
diff changeset
    86
                newOctet = true;
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    87
            } else {
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    88
                int digit = Character.digit(c, 10);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    89
                if (digit < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    92
                tmpValue *= 10;
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    93
                tmpValue += digit;
32271
d6cb1602f271 8087190: Regression in sun.net.util.IPAddressUtil.isIPv4LiteralAddress(String)
robm
parents: 25859
diff changeset
    94
                newOctet = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    96
        }
32271
d6cb1602f271 8087190: Regression in sun.net.util.IPAddressUtil.isIPv4LiteralAddress(String)
robm
parents: 25859
diff changeset
    97
        if (newOctet || tmpValue < 0 || tmpValue >= (1L << ((4 - currByte) * 8))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   100
        switch (currByte) {
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   101
            case 0:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   102
                res[0] = (byte) ((tmpValue >> 24) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   103
            case 1:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   104
                res[1] = (byte) ((tmpValue >> 16) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   105
            case 2:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   106
                res[2] = (byte) ((tmpValue >>  8) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   107
            case 3:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   108
                res[3] = (byte) ((tmpValue >>  0) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   109
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Convert IPv6 presentation level address to network order binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * credit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *  Converted from C code from Solaris 8 (inet_pton)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Any component of the string following a per-cent % is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param src a String representing an IPv6 address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return a byte array representing the IPv6 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static byte[] textToNumericFormatV6(String src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Shortest valid string is "::", hence at least 2 chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (src.length() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int colonp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        char ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        boolean saw_xdigit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        char[] srcb = src.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        byte[] dst = new byte[INADDR16SZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int srcb_length = srcb.length;
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24042
diff changeset
   138
        int pc = src.indexOf ('%');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (pc == srcb_length -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (pc != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            srcb_length = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        colonp = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int i = 0, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /* Leading :: requires some special handling. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (srcb[i] == ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (srcb[++i] != ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int curtok = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        saw_xdigit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        while (i < srcb_length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            ch = srcb[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            int chval = Character.digit(ch, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (chval != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                val <<= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                val |= chval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (val > 0xffff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                saw_xdigit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (ch == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                curtok = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (!saw_xdigit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    if (colonp != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    colonp = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } else if (i == srcb_length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (j + INT16SZ > INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                dst[j++] = (byte) ((val >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                dst[j++] = (byte) (val & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                saw_xdigit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (ch == '.' && ((j + INADDR4SZ) <= INADDR16SZ)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                String ia4 = src.substring(curtok, srcb_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                /* check this IPv4 address has 3 dots, ie. A.B.C.D */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                int dot_count = 0, index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                while ((index = ia4.indexOf ('.', index)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    dot_count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (dot_count != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                byte[] v4addr = textToNumericFormatV4(ia4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (v4addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                for (int k = 0; k < INADDR4SZ; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    dst[j++] = v4addr[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                saw_xdigit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                break;  /* '\0' was seen by inet_pton4(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (saw_xdigit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (j + INT16SZ > INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            dst[j++] = (byte) ((val >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            dst[j++] = (byte) (val & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (colonp != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            int n = j - colonp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (j == INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            for (i = 1; i <= n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                dst[INADDR16SZ - i] = dst[colonp + n - i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                dst[colonp + n - i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            j = INADDR16SZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (j != INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        byte[] newdst = convertFromIPv4MappedAddress(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (newdst != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return newdst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param src a String representing an IPv4 address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return a boolean indicating whether src is an IPv4 literal address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public static boolean isIPv4LiteralAddress(String src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return textToNumericFormatV4(src) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param src a String representing an IPv6 address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return a boolean indicating whether src is an IPv6 literal address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public static boolean isIPv6LiteralAddress(String src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return textToNumericFormatV6(src) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Convert IPv4-Mapped address to IPv4 address. Both input and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * returned value are in network order binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param src a String representing an IPv4-Mapped address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return a byte array representing the IPv4 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static byte[] convertFromIPv4MappedAddress(byte[] addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (isIPv4MappedAddress(addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            byte[] newAddr = new byte[INADDR4SZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            System.arraycopy(addr, 12, newAddr, 0, INADDR4SZ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return newAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * IPv4 mapped IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private static boolean isIPv4MappedAddress(byte[] addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (addr.length < INADDR16SZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            (addr[2] == 0x00) && (addr[3] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            (addr[4] == 0x00) && (addr[5] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            (addr[6] == 0x00) && (addr[7] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            (addr[8] == 0x00) && (addr[9] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            (addr[10] == (byte)0xff) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            (addr[11] == (byte)0xff))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}