jdk/src/share/classes/sun/net/util/IPAddressUtil.java
author rriggs
Fri, 30 May 2014 15:46:12 -0400
changeset 24685 215fa91e1b4c
parent 24042 82290278c4af
permissions -rw-r--r--
8044461: Cleanup new Boolean and single character strings Reviewed-by: rriggs, alanb, lancea Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 2005, 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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
    private final static int INADDR4SZ = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
    private final static int INADDR16SZ = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    private final static int INT16SZ = 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;
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    47
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    48
        int len = src.length();
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    49
        if (len == 0 || len > 15) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    52
        /*
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    53
         * 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
    54
         * the network address without any byte rearrangement.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    55
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    56
         * When a two part address is supplied, the last part is
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    57
         * interpreted as a 24-bit quantity and placed in the right
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    58
         * most three bytes of the network address. This makes the
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    59
         * two part address format convenient for specifying Class A
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    60
         * network addresses as net.host.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    61
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    62
         * When a three part address is specified, the last part is
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    63
         * interpreted as a 16-bit quantity and placed in the right
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    64
         * most two bytes of the network address. This makes the
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    65
         * three part address format convenient for specifying
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    66
         * Class B net- work addresses as 128.net.host.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    67
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    68
         * When four parts are specified, each is interpreted as a
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    69
         * byte of data and assigned, from left to right, to the
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    70
         * four bytes of an IPv4 address.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    71
         *
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    72
         * We determine and parse the leading parts, if any, as single
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    73
         * byte values in one pass directly into the resulting byte[],
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    74
         * 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
    75
         * translated into the remaining bytes in the array.
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    76
         */
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    77
        for (int i = 0; i < len; i++) {
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    78
            char c = src.charAt(i);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    79
            if (c == '.') {
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    80
                if (tmpValue < 0 || tmpValue > 0xff || currByte == 3) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    83
                res[currByte++] = (byte) (tmpValue & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    84
                tmpValue = 0;
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    85
            } else {
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    86
                int digit = Character.digit(c, 10);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    87
                if (digit < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    90
                tmpValue *= 10;
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    91
                tmpValue += digit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    93
        }
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    94
        if (tmpValue < 0 || tmpValue >= (1L << ((4 - currByte) * 8))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
24042
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    97
        switch (currByte) {
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    98
            case 0:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
    99
                res[0] = (byte) ((tmpValue >> 24) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   100
            case 1:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   101
                res[1] = (byte) ((tmpValue >> 16) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   102
            case 2:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   103
                res[2] = (byte) ((tmpValue >>  8) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   104
            case 3:
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   105
                res[3] = (byte) ((tmpValue >>  0) & 0xff);
82290278c4af 8040747: Improve performance of IP address parsing
mduigou
parents: 5506
diff changeset
   106
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Convert IPv6 presentation level address to network order binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * credit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *  Converted from C code from Solaris 8 (inet_pton)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Any component of the string following a per-cent % is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param src a String representing an IPv6 address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return a byte array representing the IPv6 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static byte[] textToNumericFormatV6(String src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // Shortest valid string is "::", hence at least 2 chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (src.length() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int colonp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        char ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        boolean saw_xdigit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        char[] srcb = src.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        byte[] dst = new byte[INADDR16SZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int srcb_length = srcb.length;
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24042
diff changeset
   135
        int pc = src.indexOf ('%');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (pc == srcb_length -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (pc != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            srcb_length = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        colonp = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int i = 0, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        /* Leading :: requires some special handling. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (srcb[i] == ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (srcb[++i] != ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int curtok = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        saw_xdigit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        while (i < srcb_length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            ch = srcb[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            int chval = Character.digit(ch, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (chval != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                val <<= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                val |= chval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (val > 0xffff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                saw_xdigit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (ch == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                curtok = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if (!saw_xdigit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    if (colonp != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    colonp = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                } else if (i == srcb_length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (j + INT16SZ > INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                dst[j++] = (byte) ((val >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                dst[j++] = (byte) (val & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                saw_xdigit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (ch == '.' && ((j + INADDR4SZ) <= INADDR16SZ)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                String ia4 = src.substring(curtok, srcb_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                /* check this IPv4 address has 3 dots, ie. A.B.C.D */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                int dot_count = 0, index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                while ((index = ia4.indexOf ('.', index)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    dot_count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (dot_count != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                byte[] v4addr = textToNumericFormatV4(ia4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (v4addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                for (int k = 0; k < INADDR4SZ; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    dst[j++] = v4addr[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                saw_xdigit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                break;  /* '\0' was seen by inet_pton4(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (saw_xdigit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (j + INT16SZ > INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            dst[j++] = (byte) ((val >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            dst[j++] = (byte) (val & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (colonp != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            int n = j - colonp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (j == INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            for (i = 1; i <= n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                dst[INADDR16SZ - i] = dst[colonp + n - i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                dst[colonp + n - i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            j = INADDR16SZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (j != INADDR16SZ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        byte[] newdst = convertFromIPv4MappedAddress(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (newdst != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return newdst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param src a String representing an IPv4 address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return a boolean indicating whether src is an IPv4 literal address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public static boolean isIPv4LiteralAddress(String src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return textToNumericFormatV4(src) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param src a String representing an IPv6 address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return a boolean indicating whether src is an IPv6 literal address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public static boolean isIPv6LiteralAddress(String src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return textToNumericFormatV6(src) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Convert IPv4-Mapped address to IPv4 address. Both input and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * returned value are in network order binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param src a String representing an IPv4-Mapped address in textual format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return a byte array representing the IPv4 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public static byte[] convertFromIPv4MappedAddress(byte[] addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (isIPv4MappedAddress(addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            byte[] newAddr = new byte[INADDR4SZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            System.arraycopy(addr, 12, newAddr, 0, INADDR4SZ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return newAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * IPv4 mapped IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static boolean isIPv4MappedAddress(byte[] addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (addr.length < INADDR16SZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            (addr[2] == 0x00) && (addr[3] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            (addr[4] == 0x00) && (addr[5] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            (addr[6] == 0x00) && (addr[7] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            (addr[8] == 0x00) && (addr[9] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            (addr[10] == (byte)0xff) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            (addr[11] == (byte)0xff))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
}