Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/krb5/internal/HostAddresses.java
38923 views
/*1* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*27* (C) Copyright IBM Corp. 1999 All Rights Reserved.28* Copyright 1997 The Open Group Research Institute. All rights reserved.29*/3031package sun.security.krb5.internal;3233import sun.security.krb5.Config;34import sun.security.krb5.PrincipalName;35import sun.security.krb5.KrbException;36import sun.security.krb5.Asn1Exception;37import sun.security.util.*;3839import java.net.*;40import java.util.*;41import java.io.IOException;42import sun.security.krb5.internal.ccache.CCacheOutputStream;4344/**45* Implements the ASN.1 HostAddresses type.46*47* <pre>{@code48* HostAddresses -- NOTE: subtly different from rfc1510,49* -- but has a value mapping and encodes the same50* ::= SEQUENCE OF HostAddress51*52* HostAddress ::= SEQUENCE {53* addr-type [0] Int32,54* address [1] OCTET STRING55* }56* }</pre>57*58* <p>59* This definition reflects the Network Working Group RFC 412060* specification available at61* <a href="http://www.ietf.org/rfc/rfc4120.txt">62* http://www.ietf.org/rfc/rfc4120.txt</a>.63*/6465public class HostAddresses implements Cloneable {66private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;67private HostAddress[] addresses = null;68private volatile int hashCode = 0;6970public HostAddresses(HostAddress[] new_addresses) throws IOException {71if (new_addresses != null) {72addresses = new HostAddress[new_addresses.length];73for (int i = 0; i < new_addresses.length; i++) {74if (new_addresses[i] == null) {75throw new IOException("Cannot create a HostAddress");76} else {77addresses[i] = (HostAddress)new_addresses[i].clone();78}79}80}81}8283public HostAddresses() throws UnknownHostException {84addresses = new HostAddress[1];85addresses[0] = new HostAddress();86}8788private HostAddresses(int dummy) {}8990public HostAddresses(PrincipalName serverPrincipal)91throws UnknownHostException, KrbException {9293String[] components = serverPrincipal.getNameStrings();9495if (serverPrincipal.getNameType() != PrincipalName.KRB_NT_SRV_HST ||96components.length < 2)97throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name");9899String host = components[1];100InetAddress addr[] = InetAddress.getAllByName(host);101HostAddress hAddrs[] = new HostAddress[addr.length];102103for (int i = 0; i < addr.length; i++) {104hAddrs[i] = new HostAddress(addr[i]);105}106107addresses = hAddrs;108}109110public Object clone() {111HostAddresses new_hostAddresses = new HostAddresses(0);112if (addresses != null) {113new_hostAddresses.addresses = new HostAddress[addresses.length];114for (int i = 0; i < addresses.length; i++) {115new_hostAddresses.addresses[i] =116(HostAddress)addresses[i].clone();117}118}119return new_hostAddresses;120}121122public boolean inList(HostAddress addr) {123if (addresses != null) {124for (int i = 0; i < addresses.length; i++)125if (addresses[i].equals(addr))126return true;127}128return false;129}130131public int hashCode() {132if (hashCode == 0) {133int result = 17;134if (addresses != null) {135for (int i=0; i < addresses.length; i++) {136result = 37*result + addresses[i].hashCode();137}138}139hashCode = result;140}141return hashCode;142143}144145146public boolean equals(Object obj) {147if (this == obj) {148return true;149}150151if (!(obj instanceof HostAddresses)) {152return false;153}154155HostAddresses addrs = (HostAddresses)obj;156if ((addresses == null && addrs.addresses != null) ||157(addresses != null && addrs.addresses == null))158return false;159if (addresses != null && addrs.addresses != null) {160if (addresses.length != addrs.addresses.length)161return false;162for (int i = 0; i < addresses.length; i++)163if (!addresses[i].equals(addrs.addresses[i]))164return false;165}166return true;167}168169/**170* Constructs a new <code>HostAddresses</code> object.171* @param encoding a single DER-encoded value.172* @exception Asn1Exception if an error occurs while decoding an173* ASN1 encoded data.174* @exception IOException if an I/O error occurs while reading175* encoded data.176*/177public HostAddresses(DerValue encoding)178throws Asn1Exception, IOException {179Vector<HostAddress> tempAddresses = new Vector<>();180DerValue der = null;181while (encoding.getData().available() > 0) {182der = encoding.getData().getDerValue();183tempAddresses.addElement(new HostAddress(der));184}185if (tempAddresses.size() > 0) {186addresses = new HostAddress[tempAddresses.size()];187tempAddresses.copyInto(addresses);188}189}190191192/**193* Encodes a <code>HostAddresses</code> object.194* @return byte array of encoded <code>HostAddresses</code> object.195* @exception Asn1Exception if an error occurs while decoding an196* ASN1 encoded data.197* @exception IOException if an I/O error occurs while reading198* encoded data.199*/200public byte[] asn1Encode() throws Asn1Exception, IOException {201DerOutputStream bytes = new DerOutputStream();202DerOutputStream temp = new DerOutputStream();203204if (addresses != null && addresses.length > 0) {205for (int i = 0; i < addresses.length; i++)206bytes.write(addresses[i].asn1Encode());207}208temp.write(DerValue.tag_Sequence, bytes);209return temp.toByteArray();210}211212/**213* Parse (unmarshal) a <code>HostAddresses</code> from a DER input stream.214* This form215* parsing might be used when expanding a value which is part of216* a constructed sequence and uses explicitly tagged type.217*218* @exception Asn1Exception if an Asn1Exception occurs.219* @param data the Der input stream value, which contains one or more220* marshaled value.221* @param explicitTag tag number.222* @param optional indicates if this data field is optional.223* @return an instance of <code>HostAddresses</code>.224*/225public static HostAddresses parse(DerInputStream data,226byte explicitTag, boolean optional)227throws Asn1Exception, IOException {228if ((optional) &&229(((byte)data.peekByte() & (byte)0x1F) != explicitTag))230return null;231DerValue der = data.getDerValue();232if (explicitTag != (der.getTag() & (byte)0x1F)) {233throw new Asn1Exception(Krb5.ASN1_BAD_ID);234} else {235DerValue subDer = der.getData().getDerValue();236return new HostAddresses(subDer);237}238}239240/**241* Writes data field values in <code>HostAddresses</code> in FCC242* format to a <code>CCacheOutputStream</code>.243*244* @param cos a <code>CCacheOutputStream</code> to be written to.245* @exception IOException if an I/O exception occurs.246* @see sun.security.krb5.internal.ccache.CCacheOutputStream247*/248249public void writeAddrs(CCacheOutputStream cos) throws IOException {250if (addresses == null || addresses.length == 0) {251cos.write32(0);252return;253}254cos.write32(addresses.length);255for (int i = 0; i < addresses.length; i++) {256cos.write16(addresses[i].addrType);257cos.write32(addresses[i].address.length);258cos.write(addresses[i].address, 0,259addresses[i].address.length);260}261}262263264public InetAddress[] getInetAddresses() {265266if (addresses == null || addresses.length == 0)267return null;268269ArrayList<InetAddress> ipAddrs = new ArrayList<>(addresses.length);270271for (int i = 0; i < addresses.length; i++) {272try {273if ((addresses[i].addrType == Krb5.ADDRTYPE_INET) ||274(addresses[i].addrType == Krb5.ADDRTYPE_INET6)) {275ipAddrs.add(addresses[i].getInetAddress());276}277} catch (java.net.UnknownHostException e) {278// Should not happen since IP address given279return null;280}281}282283InetAddress[] retVal = new InetAddress[ipAddrs.size()];284return ipAddrs.toArray(retVal);285286}287288/**289* Returns all the IP addresses of the local host.290*/291public static HostAddresses getLocalAddresses() throws IOException292{293Set<InetAddress> all = new LinkedHashSet<>();294try {295if (DEBUG) {296System.out.println(">>> KrbKdcReq local addresses are:");297}298String extra = Config.getInstance().getAll(299"libdefaults", "extra_addresses");300if (extra != null) {301for (String s: extra.split("\\s+")) {302all.add(InetAddress.getByName(s));303if (DEBUG) {304System.out.println(" extra_addresses: "305+ InetAddress.getByName(s));306}307}308}309for (NetworkInterface ni:310Collections.list(NetworkInterface.getNetworkInterfaces())) {311if (DEBUG) {312System.out.println(" NetworkInterface " + ni + ":");313System.out.println(" "314+ Collections.list(ni.getInetAddresses()));315}316all.addAll(Collections.list(ni.getInetAddresses()));317}318return new HostAddresses(all.toArray(new InetAddress[all.size()]));319} catch (Exception exc) {320throw new IOException(exc.toString());321}322}323324/**325* Creates a new HostAddresses instance from the supplied list326* of InetAddresses.327*/328public HostAddresses(InetAddress[] inetAddresses)329{330if (inetAddresses == null)331{332addresses = null;333return;334}335336addresses = new HostAddress[inetAddresses.length];337for (int i = 0; i < inetAddresses.length; i++)338addresses[i] = new HostAddress(inetAddresses[i]);339}340341@Override342public String toString() {343return Arrays.toString(addresses);344}345}346347348