jdk/src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddresses.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 9035 jdk/src/share/classes/sun/security/krb5/internal/HostAddresses.java@1255eb81cc2f
child 27946 9f99b93cbbb2
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7977
diff changeset
     2
 * Copyright (c) 2000, 2011, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.Inet4Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.Inet6Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.krb5.internal.ccache.CCacheOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Implements the ASN.1 HostAddresses type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * HostAddresses   -- NOTE: subtly different from rfc1510,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *                 -- but has a value mapping and encodes the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         ::= SEQUENCE OF HostAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * HostAddress     ::= SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *         addr-type       [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *         address         [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class HostAddresses implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private HostAddress[] addresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private volatile int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public HostAddresses(HostAddress[] new_addresses) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (new_addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
           addresses = new HostAddress[new_addresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
           for (int i = 0; i < new_addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                if (new_addresses[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                   throw new IOException("Cannot create a HostAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                   addresses[i] = (HostAddress)new_addresses[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
           }
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
    public HostAddresses() throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        addresses = new HostAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        addresses[0] = new HostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private HostAddresses(int dummy) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public HostAddresses(PrincipalName serverPrincipal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        throws UnknownHostException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        String[] components = serverPrincipal.getNameStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (serverPrincipal.getNameType() != PrincipalName.KRB_NT_SRV_HST ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            components.length < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String host = components[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        InetAddress addr[] = InetAddress.getAllByName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        HostAddress hAddrs[] = new HostAddress[addr.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (int i = 0; i < addr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            hAddrs[i] = new HostAddress(addr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        addresses = hAddrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        HostAddresses new_hostAddresses = new HostAddresses(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            new_hostAddresses.addresses = new HostAddress[addresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                new_hostAddresses.addresses[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        (HostAddress)addresses[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return new_hostAddresses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public boolean inList(HostAddress addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (addresses[i].equals(addr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (hashCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                for (int i=0; i < addresses.length; i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    result = 37*result + addresses[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            hashCode = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (!(obj instanceof HostAddresses)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        HostAddresses addrs = (HostAddresses)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if ((addresses == null && addrs.addresses != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            (addresses != null && addrs.addresses == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (addresses != null && addrs.addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (addresses.length != addrs.addresses.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if (!addresses[i].equals(addrs.addresses[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    * Constructs a new <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public HostAddresses(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws  Asn1Exception, IOException {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   182
        Vector<HostAddress> tempAddresses = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DerValue der = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        while (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            tempAddresses.addElement(new HostAddress(der));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (tempAddresses.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            addresses = new HostAddress[tempAddresses.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            tempAddresses.copyInto(addresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    * Encodes a <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    * @return byte array of encoded <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (addresses != null && addresses.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                bytes.write(addresses[i].asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Parse (unmarshal) a <code>HostAddresses</code> from a DER input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @exception Asn1Exception if an Asn1Exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param optional indicates if this data field is optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return an instance of <code>HostAddresses</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static HostAddresses parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                      byte explicitTag, boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return new HostAddresses(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * Writes data field values in <code>HostAddresses</code> in FCC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * format to a <code>CCacheOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * @param cos a <code>CCacheOutputStream</code> to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public void writeAddrs(CCacheOutputStream cos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        cos.write32(addresses.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            cos.write16(addresses[i].addrType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            cos.write32(addresses[i].address.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            cos.write(addresses[i].address, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                      addresses[i].address.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public InetAddress[] getInetAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (addresses == null || addresses.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   268
        ArrayList<InetAddress> ipAddrs = new ArrayList<>(addresses.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if ((addresses[i].addrType == Krb5.ADDRTYPE_INET) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    (addresses[i].addrType == Krb5.ADDRTYPE_INET6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    ipAddrs.add(addresses[i].getInetAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            } catch (java.net.UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // Should not happen since IP address given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        InetAddress[] retVal = new InetAddress[ipAddrs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return ipAddrs.toArray(retVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Returns all the IP addresses of the local host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public static HostAddresses getLocalAddresses() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        String hostname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        InetAddress[] inetAddresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            InetAddress localHost = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            hostname = localHost.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            inetAddresses = InetAddress.getAllByName(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            HostAddress[] hAddresses = new HostAddress[inetAddresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            for (int i = 0; i < inetAddresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    hAddresses[i] = new HostAddress(inetAddresses[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                System.out.println(">>> KrbKdcReq local addresses for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                   + hostname + " are: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                for (int i = 0; i < inetAddresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    System.out.println("\n\t" + inetAddresses[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    if (inetAddresses[i] instanceof Inet4Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        System.out.println("IPv4 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    if (inetAddresses[i] instanceof Inet6Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        System.out.println("IPv6 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return (new HostAddresses(hAddresses));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throw new IOException(exc.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Creates a new HostAddresses instance from the supplied list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * of InetAddresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public HostAddresses(InetAddress[] inetAddresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (inetAddresses == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                addresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        addresses = new HostAddress[inetAddresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        for (int i = 0; i < inetAddresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            addresses[i] = new HostAddress(inetAddresses[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
}