0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A * (C) Copyright IBM Corp. 1999 All Rights Reserved.
0N/A * Copyright 1997 The Open Group Research Institute. All rights reserved.
0N/A */
0N/A
0N/Apackage sun.security.krb5.internal;
0N/A
0N/Aimport sun.security.krb5.Config;
0N/Aimport sun.security.krb5.Asn1Exception;
0N/Aimport sun.security.util.*;
0N/Aimport java.net.InetAddress;
0N/Aimport java.net.Inet4Address;
0N/Aimport java.net.Inet6Address;
0N/Aimport java.net.UnknownHostException;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * Implements the ASN.1 HostAddress type.
0N/A *
0N/A * <xmp>
0N/A * HostAddress ::= SEQUENCE {
0N/A * addr-type [0] Int32,
0N/A * address [1] OCTET STRING
0N/A * }
0N/A * </xmp>
0N/A *
0N/A * <p>
0N/A * This definition reflects the Network Working Group RFC 4120
0N/A * specification available at
0N/A * <a href="http://www.ietf.org/rfc/rfc4120.txt">
0N/A * http://www.ietf.org/rfc/rfc4120.txt</a>.
0N/A */
0N/A
0N/Apublic class HostAddress implements Cloneable {
0N/A int addrType;
0N/A byte[] address = null;
0N/A
0N/A private static InetAddress localInetAddress; //caches local inet address
0N/A private static final boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
0N/A private volatile int hashCode = 0;
0N/A
0N/A private HostAddress(int dummy) {}
0N/A
0N/A public Object clone() {
0N/A HostAddress new_hostAddress = new HostAddress(0);
0N/A new_hostAddress.addrType = addrType;
0N/A if (address != null) {
0N/A new_hostAddress.address = address.clone();
0N/A }
0N/A return new_hostAddress;
0N/A }
0N/A
0N/A
0N/A public int hashCode() {
0N/A if (hashCode == 0) {
0N/A int result = 17;
0N/A result = 37*result + addrType;
0N/A if (address != null) {
0N/A for (int i=0; i < address.length; i++) {
0N/A result = 37*result + address[i];
0N/A }
0N/A }
0N/A hashCode = result;
0N/A }
0N/A return hashCode;
0N/A
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A if (this == obj) {
0N/A return true;
0N/A }
0N/A
0N/A if (!(obj instanceof HostAddress)) {
0N/A return false;
0N/A }
0N/A
0N/A HostAddress h = (HostAddress)obj;
0N/A if (addrType != h.addrType ||
0N/A (address != null && h.address == null) ||
0N/A (address == null && h.address != null))
0N/A return false;
0N/A if (address != null && h.address != null) {
0N/A if (address.length != h.address.length)
0N/A return false;
0N/A for (int i = 0; i < address.length; i++)
0N/A if (address[i] != h.address[i])
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A private static synchronized InetAddress getLocalInetAddress()
0N/A throws UnknownHostException {
0N/A
0N/A if (localInetAddress == null) {
0N/A localInetAddress = InetAddress.getLocalHost();
0N/A }
0N/A if (localInetAddress == null) {
0N/A throw new UnknownHostException();
0N/A }
0N/A return (localInetAddress);
0N/A }
0N/A
0N/A /**
0N/A * Gets the InetAddress of this HostAddress.
0N/A * @return the IP address for this specified host.
0N/A * @exception if no IP address for the host could be found.
0N/A *
0N/A */
0N/A public InetAddress getInetAddress() throws UnknownHostException {
0N/A // the type of internet addresses is 2.
0N/A if (addrType == Krb5.ADDRTYPE_INET ||
0N/A addrType == Krb5.ADDRTYPE_INET6) {
0N/A return (InetAddress.getByAddress(address));
0N/A } else {
0N/A // if it is other type (ISO address, XNS address, etc)
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A private int getAddrType(InetAddress inetAddress) {
0N/A int addressType = 0;
0N/A if (inetAddress instanceof Inet4Address)
0N/A addressType = Krb5.ADDRTYPE_INET;
0N/A else if (inetAddress instanceof Inet6Address)
0N/A addressType = Krb5.ADDRTYPE_INET6;
0N/A return (addressType);
0N/A }
0N/A
0N/A // implicit default not in Config.java
0N/A public HostAddress() throws UnknownHostException {
0N/A InetAddress inetAddress = getLocalInetAddress();
0N/A addrType = getAddrType(inetAddress);
0N/A address = inetAddress.getAddress();
0N/A }
0N/A
0N/A /**
0N/A * Creates a HostAddress from the specified address and address type.
0N/A *
0N/A * @param new_addrType the value of the address type which matches the defined
0N/A * address family constants in the Berkeley Standard
0N/A * Distributions of Unix.
0N/A * @param new_address network address.
0N/A * @exception KrbApErrException if address type and address length do not match defined value.
0N/A *
0N/A */
0N/A public HostAddress(int new_addrType, byte[] new_address)
0N/A throws KrbApErrException, UnknownHostException {
0N/A switch(new_addrType) {
0N/A case Krb5.ADDRTYPE_INET: //Internet address
0N/A if (new_address.length != 4)
0N/A throw new KrbApErrException(0, "Invalid Internet address");
0N/A break;
0N/A case Krb5.ADDRTYPE_CHAOS:
0N/A if (new_address.length != 2) //CHAOSnet address
0N/A throw new KrbApErrException(0, "Invalid CHAOSnet address");
0N/A break;
0N/A case Krb5.ADDRTYPE_ISO: // ISO address
0N/A break;
0N/A case Krb5.ADDRTYPE_IPX: // XNS address
0N/A if (new_address.length != 6)
0N/A throw new KrbApErrException(0, "Invalid XNS address");
0N/A break;
0N/A case Krb5.ADDRTYPE_APPLETALK: //AppleTalk DDP address
0N/A if (new_address.length != 3)
0N/A throw new KrbApErrException(0, "Invalid DDP address");
0N/A break;
0N/A case Krb5.ADDRTYPE_DECNET: //DECnet Phase IV address
0N/A if (new_address.length != 2)
0N/A throw new KrbApErrException(0, "Invalid DECnet Phase IV address");
0N/A break;
0N/A case Krb5.ADDRTYPE_INET6: //Internet IPv6 address
0N/A if (new_address.length != 16)
0N/A throw new KrbApErrException(0, "Invalid Internet IPv6 address");
0N/A break;
0N/A }
0N/A
0N/A addrType = new_addrType;
0N/A if (new_address != null) {
0N/A address = new_address.clone();
0N/A }
0N/A if (DEBUG) {
0N/A if (addrType == Krb5.ADDRTYPE_INET ||
0N/A addrType == Krb5.ADDRTYPE_INET6) {
0N/A System.out.println("Host address is " +
0N/A InetAddress.getByAddress(address));
0N/A }
0N/A }
0N/A }
0N/A
0N/A public HostAddress(InetAddress inetAddress) {
0N/A addrType = getAddrType(inetAddress);
0N/A address = inetAddress.getAddress();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a host address from a single DER-encoded value.
0N/A * @param encoding a single DER-encoded value.
0N/A * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
0N/A * @exception IOException if an I/O error occurs while reading encoded data.
0N/A *
0N/A */
0N/A public HostAddress(DerValue encoding) throws Asn1Exception, IOException {
0N/A DerValue der = encoding.getData().getDerValue();
0N/A if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
0N/A addrType = der.getData().getBigInteger().intValue();
0N/A }
0N/A else
0N/A throw new Asn1Exception(Krb5.ASN1_BAD_ID);
0N/A der = encoding.getData().getDerValue();
0N/A if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
0N/A address = der.getData().getOctetString();
0N/A }
0N/A else
0N/A throw new Asn1Exception(Krb5.ASN1_BAD_ID);
0N/A if (encoding.getData().available() > 0)
0N/A throw new Asn1Exception(Krb5.ASN1_BAD_ID);
0N/A }
0N/A
0N/A /**
0N/A * Encodes a HostAddress object.
0N/A * @return a byte array of encoded HostAddress object.
0N/A * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
0N/A * @exception IOException if an I/O error occurs while reading encoded data.
0N/A *
0N/A */
0N/A
0N/A public byte[] asn1Encode() throws Asn1Exception, IOException {
0N/A DerOutputStream bytes = new DerOutputStream();
0N/A DerOutputStream temp = new DerOutputStream();
0N/A temp.putInteger(this.addrType);
0N/A bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
0N/A temp = new DerOutputStream();
0N/A temp.putOctetString(address);
0N/A bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
0N/A temp = new DerOutputStream();
0N/A temp.write(DerValue.tag_Sequence, bytes);
0N/A return temp.toByteArray();
0N/A }
0N/A
0N/A /**
0N/A * Parses (unmarshal) a host address from a DER input stream. This form
0N/A * parsing might be used when expanding a value which is part of
0N/A * a constructed sequence and uses explicitly tagged type.
0N/A *
0N/A * @exception Asn1Exception on error.
0N/A * @exception IOException if an I/O error occurs while reading encoded data.
0N/A * @param data the Der input stream value, which contains one or more marshaled value.
0N/A * @param explicitTag tag number.
0N/A * @param optional indicates if this data field is optional
0N/A * @return an instance of HostAddress.
0N/A *
0N/A */
0N/A public static HostAddress parse(DerInputStream data, byte explicitTag,
0N/A boolean optional)
0N/A throws Asn1Exception, IOException{
0N/A if ((optional) &&
0N/A (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
0N/A return null;
0N/A }
0N/A DerValue der = data.getDerValue();
0N/A if (explicitTag != (der.getTag() & (byte)0x1F)) {
0N/A throw new Asn1Exception(Krb5.ASN1_BAD_ID);
0N/A }
0N/A else {
0N/A DerValue subDer = der.getData().getDerValue();
0N/A return new HostAddress(subDer);
0N/A }
0N/A }
0N/A
0N/A}