jdk/src/share/classes/sun/security/x509/IPAddressName.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2002 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.Integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.BitArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class implements the IPAddressName as required by the GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * ASN.1 object.  Both IPv4 and IPv6 addresses are supported using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * formats specified in IETF PKIX RFC2459.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * [RFC2459 4.2.1.7 Subject Alternative Name]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * When the subjectAltName extension contains a iPAddress, the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * MUST be stored in the octet string in "network byte order," as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * specified in RFC 791. The least significant bit (LSB) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * each octet is the LSB of the corresponding byte in the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * address. For IP Version 4, as specified in RFC 791, the octet string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * MUST contain exactly four octets.  For IP Version 6, as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * RFC 1883, the octet string MUST contain exactly sixteen octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * [RFC2459 4.2.1.11 Name Constraints]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The syntax of iPAddress MUST be as described in section 4.2.1.7 with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the following additions specifically for Name Constraints.  For IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * addresses, the ipAddress field of generalName MUST contain eight (8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * octets, encoded in the style of RFC 1519 (CIDR) to represent an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * address range.[RFC 1519]  For IPv6 addresses, the ipAddress field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * MUST contain 32 octets similarly encoded.  For example, a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * constraint for "class C" subnet 10.9.8.0 shall be represented as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * octets 0A 09 08 00 FF FF FF 00, representing the CIDR notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * 10.9.8.0/255.255.255.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class IPAddressName implements GeneralNameInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private byte[] address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean isIPv4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Create the IPAddressName object from the passed encoded Der value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @params derValue the encoded DER IPAddressName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public IPAddressName(DerValue derValue) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this(derValue.getOctetString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Create the IPAddressName object with the specified octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @params address the IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @throws IOException if address is not a valid IPv4 or IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public IPAddressName(byte[] address) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * A valid address must consist of 4 bytes of address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * optional 4 bytes of 4 bytes of mask, or 16 bytes of address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * and optional 16 bytes of mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (address.length == 4 || address.length == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            isIPv4 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else if (address.length == 16 || address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            isIPv4 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IOException("Invalid IPAddressName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Create an IPAddressName from a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * [IETF RFC1338 Supernetting & IETF RFC1519 Classless Inter-Domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Routing (CIDR)] For IPv4 addresses, the forms are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * "b1.b2.b3.b4" or "b1.b2.b3.b4/m1.m2.m3.m4", where b1 - b4 are decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * byte values 0-255 and m1 - m4 are decimal mask values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * 0 - 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * [IETF RFC2373 IP Version 6 Addressing Architecture]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * For IPv6 addresses, the forms are "a1:a2:...:a8" or "a1:a2:...:a8/n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * where a1-a8 are hexadecimal values representing the eight 16-bit pieces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * of the address. If /n is used, n is a decimal number indicating how many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * of the leftmost contiguous bits of the address comprise the prefix for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * this subnet. Internally, a mask value is created using the prefix length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param name String form of IPAddressName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException if name can not be converted to a valid IPv4 or IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *     address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public IPAddressName(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (name == null || name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IOException("IPAddress cannot be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (name.charAt(name.length() - 1) == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IOException("Invalid IPAddress: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (name.indexOf(':') >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // name is IPv6: uses colons as value separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // Parse name into byte-value address components and optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            parseIPv6(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            isIPv4 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else if (name.indexOf('.') >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            //name is IPv4: uses dots as value separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            parseIPv4(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            isIPv4 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IOException("Invalid IPAddress: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Parse an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param name IPv4 address with optional mask values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private void parseIPv4(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // Parse name into byte-value address components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int slashNdx = name.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (slashNdx == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            address = InetAddress.getByName(name).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            address = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // parse mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            byte[] mask = InetAddress.getByName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                (name.substring(slashNdx+1)).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // parse base address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            byte[] host = InetAddress.getByName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                (name.substring(0, slashNdx)).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            System.arraycopy(host, 0, address, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            System.arraycopy(mask, 0, address, 4, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Parse an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param name String IPv6 address with optional /<prefix length>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *             If /<prefix length> is present, address[] array will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *             be 32 bytes long, otherwise 16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private final static int MASKSIZE = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void parseIPv6(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int slashNdx = name.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (slashNdx == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            address = InetAddress.getByName(name).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            address = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            byte[] base = InetAddress.getByName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                (name.substring(0, slashNdx)).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.arraycopy(base, 0, address, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // append a mask corresponding to the num of prefix bits specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            int prefixLen = Integer.parseInt(name.substring(slashNdx+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (prefixLen > 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                throw new IOException("IPv6Address prefix is longer than 128");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // create new bit array initialized to zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            BitArray bitArray = new BitArray(MASKSIZE * 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // set all most significant bits up to prefix length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            for (int i = 0; i < prefixLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                bitArray.set(i, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            byte[] maskArray = bitArray.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // copy mask bytes into mask portion of address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            for (int i = 0; i < MASKSIZE; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                address[MASKSIZE+i] = maskArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Return the type of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return NAME_IP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Encode the IPAddress name into the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @params out the DER stream to encode the IPAddressName to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        out.putOctetString(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Return a printable string of IPaddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return "IPAddress: " + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // dump out hex rep for debugging purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return "IPAddress: " + enc.encodeBuffer(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Return a standard String representation of IPAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * See IPAddressName(String) for the formats used for IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * and IPv6 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @throws IOException if the IPAddress cannot be converted to a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public String getName() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (isIPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            //IPv4 address or subdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            byte[] host = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            System.arraycopy(address, 0, host, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            name = InetAddress.getByAddress(host).getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (address.length == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                byte[] mask = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                System.arraycopy(address, 4, mask, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                name = name + "/" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                       InetAddress.getByAddress(mask).getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            //IPv6 address or subdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            byte[] host = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            System.arraycopy(address, 0, host, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            name = InetAddress.getByAddress(host).getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // IPv6 subdomain: display prefix length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // copy subdomain into new array and convert to BitArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                byte[] maskBytes = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                for (int i=16; i < 32; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    maskBytes[i-16] = address[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                BitArray ba = new BitArray(16*8, maskBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                // Find first zero bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                for (; i < 16*8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    if (!ba.get(i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                name = name + "/" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                // Verify remaining bits 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                for (; i < 16*8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    if (ba.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        throw new IOException("Invalid IPv6 subdomain - set " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            "bit " + i + " not contiguous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Returns this IPAddress name as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return address.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Compares this name with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return true iff the names are identical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (!(obj instanceof IPAddressName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        byte[] other = ((IPAddressName)obj).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (other.length != address.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (address.length == 8 || address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            // Two subnet addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            // Mask each and compare masked values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            int maskLen = address.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            byte[] maskedThis = new byte[maskLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            byte[] maskedOther = new byte[maskLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            for (int i=0; i < maskLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                maskedThis[i] = (byte)(address[i] & address[i+maskLen]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                maskedOther[i] = (byte)(other[i] & other[i+maskLen]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (maskedThis[i] != maskedOther[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // Now compare masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            for (int i=maskLen; i < address.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (address[i] != other[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            // Two IPv4 host addresses or two IPv6 host addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // Compare bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return Arrays.equals(other, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        for (int i=0; i<address.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            retval += address[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Return type of constraint inputName places on this name:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *       (i.e. does not constrain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *   <li>NAME_MATCH = 0: input name matches name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *   <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *   <li>NAME_WIDENS = 2: input name widens name (is higher in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *       is same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * </ul>.  These results are used in checking NameConstraints during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * [RFC2459] The syntax of iPAddress MUST be as described in section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * 4.2.1.7 with the following additions specifically for Name Constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * For IPv4 addresses, the ipAddress field of generalName MUST contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * eight (8) octets, encoded in the style of RFC 1519 (CIDR) to represent an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * address range.[RFC 1519]  For IPv6 addresses, the ipAddress field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * MUST contain 32 octets similarly encoded.  For example, a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * constraint for "class C" subnet 10.9.8.0 shall be represented as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * octets 0A 09 08 00 FF FF FF 00, representing the CIDR notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * 10.9.8.0/255.255.255.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param inputName to be checked for being constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @returns constraint type above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @throws UnsupportedOperationException if name is not exact match, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * narrowing and widening are not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public int constrains(GeneralNameInterface inputName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (inputName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        else if (inputName.getType() != NAME_IP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        else if (((IPAddressName)inputName).equals(this))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            byte[] otherAddress = ((IPAddressName)inputName).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (otherAddress.length == 4 && address.length == 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                // Two host addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            else if ((otherAddress.length == 8 && address.length == 8) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                     (otherAddress.length == 32 && address.length == 32)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                // Two subnet addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                // See if one address fully encloses the other address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                boolean otherSubsetOfThis = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                boolean thisSubsetOfOther = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                boolean thisEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                boolean otherEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                int maskOffset = address.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                for (int i=0; i < maskOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    if ((byte)(address[i] & address[i+maskOffset]) != address[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        thisEmpty=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    if ((byte)(otherAddress[i] & otherAddress[i+maskOffset]) != otherAddress[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        otherEmpty=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    if (!(((byte)(address[i+maskOffset] & otherAddress[i+maskOffset]) == address[i+maskOffset]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                          ((byte)(address[i]   & address[i+maskOffset])      == (byte)(otherAddress[i] & address[i+maskOffset])))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        otherSubsetOfThis = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    if (!(((byte)(otherAddress[i+maskOffset] & address[i+maskOffset])      == otherAddress[i+maskOffset]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                          ((byte)(otherAddress[i]   & otherAddress[i+maskOffset]) == (byte)(address[i] & otherAddress[i+maskOffset])))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        thisSubsetOfOther = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (thisEmpty || otherEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    if (thisEmpty && otherEmpty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    else if (thisEmpty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                } else if (otherSubsetOfThis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                else if (thisSubsetOfOther)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            } else if (otherAddress.length == 8 || otherAddress.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                //Other is a subnet, this is a host address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                int maskOffset = otherAddress.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                for (; i < maskOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    // Mask this address by other address mask and compare to other address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    // If all match, then this address is in other address subnet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    if ((address[i] & otherAddress[i+maskOffset]) != otherAddress[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                if (i == maskOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            } else if (address.length == 8 || address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                //This is a subnet, other is a host address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                int maskOffset = address.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                for (; i < maskOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    // Mask other address by this address mask and compare to this address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    if ((otherAddress[i] & address[i+maskOffset]) != address[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (i == maskOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * NameConstraints minimum and maximum bounds and for calculating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * path lengths in name subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @returns distance of name from root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        throw new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            ("subtreeDepth() not defined for IPAddressName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
}