Author: erodriguez
Date: Tue Oct 19 11:10:02 2004
New Revision: 55080
Added:
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddress.java
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddressType.java
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddresses.java
Modified:
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/decoder/KerberosMessageDecoder.java
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/encoder/KerberosMessageEncoder.java
Log:
HostAddress implementation, with updates to ASN.1 codecs.
Modified:
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/decoder/KerberosMessageDecoder.java
==============================================================================
---
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/decoder/KerberosMessageDecoder.java
(original)
+++
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/decoder/KerberosMessageDecoder.java
Tue Oct 19 11:10:02 2004
@@ -183,7 +183,7 @@
}*/
protected HostAddress decodeHostAddress(DERSequence sequence) {
- int type = 0;
+ HostAddressType type = HostAddressType.NULL;
byte[] value = new byte[0];
for (Enumeration e = sequence.getObjects();
e.hasMoreElements();) {
@@ -194,7 +194,7 @@
switch (tag) {
case 0:
DERInteger addressType =
(DERInteger)derObject;
- type =
addressType.getValue().intValue();
+ type =
HostAddressType.getTypeByOrdinal(addressType.getValue().intValue());
break;
case 1:
DEROctetString address =
(DEROctetString)derObject;
Modified:
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/encoder/KerberosMessageEncoder.java
==============================================================================
---
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/encoder/KerberosMessageEncoder.java
(original)
+++
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/io/encoder/KerberosMessageEncoder.java
Tue Oct 19 11:10:02 2004
@@ -100,20 +100,6 @@
}
/*
- HostAddress ::= SEQUENCE {
- addr-type[0] INTEGER,
- address[1] OCTET STRING
- }*/
- protected DERSequence encodeHostAddress(HostAddress host) {
- ASN1EncodableVector vector = new ASN1EncodableVector();
-
- vector.add(new DERTaggedObject(0, new
DERInteger(host.getAddrType())));
- vector.add(new DERTaggedObject(1, new
DEROctetString(host.getAddress())));
-
- return new DERSequence(vector);
- }
-
- /*
HostAddresses ::= SEQUENCE OF SEQUENCE {
addr-type[0] INTEGER,
address[1] OCTET STRING
@@ -121,15 +107,27 @@
protected DERSequence encodeHostAddresses(HostAddresses hosts) {
HostAddress[] addresses = hosts.getAddresses();
- ASN1EncodableVector outerVector = new ASN1EncodableVector();
+ ASN1EncodableVector vector = new ASN1EncodableVector();
for (int i = 0; i < addresses.length; i++) {
- ASN1EncodableVector vector = new ASN1EncodableVector();
- vector.add(new DERTaggedObject(0, new
DERInteger(addresses[i].getAddrType())));
- vector.add(new DERTaggedObject(1, new
DEROctetString(addresses[i].getAddress())));
- outerVector.add(new DERSequence(vector));
+ vector.add(encodeHostAddress(addresses[i]));
}
- return new DERSequence(outerVector);
+
+ return new DERSequence(vector);
+ }
+
+ /*
+ HostAddress ::= SEQUENCE {
+ addr-type[0] INTEGER,
+ address[1] OCTET STRING
+ }*/
+ protected DERSequence encodeHostAddress(HostAddress host) {
+ ASN1EncodableVector vector = new ASN1EncodableVector();
+
+ vector.add(new DERTaggedObject(0, new
DERInteger(host.getAddressType().getOrdinal())));
+ vector.add(new DERTaggedObject(1, new
DEROctetString(host.getAddress())));
+
+ return new DERSequence(vector);
}
/*
Added:
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddress.java
==============================================================================
--- (empty file)
+++
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddress.java
Tue Oct 19 11:10:02 2004
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.kerberos.messages.value;
+
+import java.net.*;
+
+/**
+ * Provides host address information.
+ */
+public class HostAddress {
+
+ // Host address length - inet
+ public static final int ADDR_LEN_INET = 4;
+ // Host address length - chaos
+ public static final int ADDR_LEN_CHAOS = 2;
+ // Host address length - osi
+ public static final int ADDR_LEN_OSI = 0; // variable
+ // Host address length - xns
+ public static final int ADDR_LEN_XNS = 6;
+ // Host address length - appletalk
+ public static final int ADDR_LEN_APPLETALK = 3;
+ // Host address length - decnet
+ public static final int ADDR_LEN_DECNET = 2;
+
+ private static byte[] localInetAddress;
+
+ private HostAddressType _addressType;
+ private byte[] _address;
+
+ /**
+ * Class constructors
+ */
+ public HostAddress() {
+ try {
+ _addressType = HostAddressType.ADDRTYPE_INET;
+ _address = getLocalInetAddress();
+ } catch (UnknownHostException uhe) {
+ uhe.printStackTrace();
+ }
+ }
+
+ public HostAddress(HostAddressType addressType, byte[] address) {
+ _addressType = addressType;
+ _address = address;
+ }
+
+ public HostAddress(InetAddress internetAddress) {
+ _addressType = HostAddressType.ADDRTYPE_INET;
+ byte[] newAddress = internetAddress.getAddress();
+ _address = new byte[newAddress.length];
+ System.arraycopy(newAddress, 0, _address, 0, newAddress.length);
+ }
+
+ public boolean equals(HostAddress h) {
+ if (_addressType != h._addressType ||
+ (_address != null && h._address == null) ||
+ (_address == null && h._address != null))
+ return false;
+ if (_address != null && h._address != null) {
+ if (_address.length != h._address.length)
+ return false;
+ for (int i = 0; i < _address.length; i++)
+ if (_address[i] != h._address[i])
+ return false;
+ }
+ return true;
+ }
+
+ public static byte[] getLocalInetAddress() throws UnknownHostException {
+ if (localInetAddress == null)
+ localInetAddress =
InetAddress.getLocalHost().getAddress();
+
+ byte[] result = new byte[localInetAddress.length];
+ System.arraycopy(localInetAddress, 0, result, 0,
localInetAddress.length);
+
+ return result;
+ }
+
+ public byte[] getAddress() {
+ return _address;
+ }
+ public HostAddressType getAddressType() {
+ return _addressType;
+ }
+}
+
Added:
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddressType.java
==============================================================================
--- (empty file)
+++
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddressType.java
Tue Oct 19 11:10:02 2004
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.kerberos.messages.value;
+
+import java.util.*;
+
+public final class HostAddressType implements Comparable {
+
+ /**
+ * Enumeration elements are constructed once upon class loading.
+ * Order of appearance here determines the order of compareTo.
+ */
+ public static final HostAddressType NULL = new
HostAddressType(0, "null");
+ public static final HostAddressType ADDRTYPE_UNIX = new
HostAddressType(1, "Unix");
+ public static final HostAddressType ADDRTYPE_INET = new
HostAddressType(2, "Internet");
+ public static final HostAddressType ADDRTYPE_IMPLINK = new
HostAddressType(3, "Arpanet");
+ public static final HostAddressType ADDRTYPE_PUP = new
HostAddressType(4, "PUP");
+ public static final HostAddressType ADDRTYPE_CHAOS = new
HostAddressType(5, "CHAOS");
+ public static final HostAddressType ADDRTYPE_XNS = new
HostAddressType(6, "XEROX Network Services");
+ public static final HostAddressType ADDRTYPE_IPX = new
HostAddressType(6, "IPX");
+ public static final HostAddressType ADDRTYPE_OSI = new
HostAddressType(7, "OSI");
+ public static final HostAddressType ADDRTYPE_ECMA = new
HostAddressType(8, "European Computer Manufacturers");
+ public static final HostAddressType ADDRTYPE_DATAKIT = new
HostAddressType(9, "Datakit");
+ public static final HostAddressType ADDRTYPE_CCITT = new
HostAddressType(10, "CCITT");
+ public static final HostAddressType ADDRTYPE_SNA = new
HostAddressType(11, "SNA");
+ public static final HostAddressType ADDRTYPE_DECNET = new
HostAddressType(12, "DECnet");
+ public static final HostAddressType ADDRTYPE_DLI = new
HostAddressType(13, "Direct Data Link Interface");
+ public static final HostAddressType ADDRTYPE_LAT = new
HostAddressType(14, "LAT");
+ public static final HostAddressType ADDRTYPE_HYLINK = new
HostAddressType(15, "NSC Hyperchannel");
+ public static final HostAddressType ADDRTYPE_APPLETALK = new
HostAddressType(16, "AppleTalk");
+ public static final HostAddressType ADDRTYPE_NETBIOS = new
HostAddressType(17, "NetBios");
+ public static final HostAddressType ADDRTYPE_VOICEVIEW = new
HostAddressType(18, "VoiceView");
+ public static final HostAddressType ADDRTYPE_FIREFOX = new
HostAddressType(19, "Firefox");
+ public static final HostAddressType ADDRTYPE_BAN = new
HostAddressType(21, "Banyan");
+ public static final HostAddressType ADDRTYPE_ATM = new
HostAddressType(22, "ATM");
+ public static final HostAddressType ADDRTYPE_INET6 = new
HostAddressType(23, "Internet Protocol V6");
+
+ public String toString() {
+ return _fName + " (" + _fOrdinal + ")";
+ }
+
+ public int compareTo(Object that) {
+ return _fOrdinal - ((HostAddressType) that)._fOrdinal;
+ }
+
+ public static HostAddressType getTypeByOrdinal(int type) {
+ for (int i = 0; i < fValues.length; i++)
+ if (fValues[i]._fOrdinal == type)
+ return fValues[i];
+ return NULL;
+ }
+
+ public int getOrdinal() {
+ return _fOrdinal;
+ }
+
+ /// PRIVATE /////
+ private final String _fName;
+ private final int _fOrdinal;
+
+ /**
+ * Private constructor prevents construction outside of this class.
+ */
+ private HostAddressType(int ordinal, String name) {
+ _fOrdinal = ordinal;
+ _fName = name;
+ }
+
+ /**
+ * These two lines are all that's necessary to export a List of VALUES.
+ */
+ private static final HostAddressType[] fValues = {NULL, ADDRTYPE_UNIX,
ADDRTYPE_INET,
+ ADDRTYPE_IMPLINK, ADDRTYPE_PUP, ADDRTYPE_CHAOS,
ADDRTYPE_XNS, ADDRTYPE_IPX,
+ ADDRTYPE_OSI, ADDRTYPE_ECMA, ADDRTYPE_DATAKIT,
ADDRTYPE_CCITT, ADDRTYPE_SNA,
+ ADDRTYPE_DECNET, ADDRTYPE_DLI, ADDRTYPE_LAT,
ADDRTYPE_HYLINK, ADDRTYPE_APPLETALK,
+ ADDRTYPE_NETBIOS, ADDRTYPE_VOICEVIEW, ADDRTYPE_FIREFOX,
ADDRTYPE_BAN, ADDRTYPE_ATM,
+ ADDRTYPE_INET6};
+ // VALUES needs to be located here, otherwise illegal forward reference
+ public static final List VALUES =
Collections.unmodifiableList(Arrays.asList(fValues));
+}
+
Added:
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddresses.java
==============================================================================
--- (empty file)
+++
incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/messages/value/HostAddresses.java
Tue Oct 19 11:10:02 2004
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.kerberos.messages.value;
+
+public class HostAddresses {
+
+ private HostAddress[] _addresses;
+
+ /**
+ * Class constructors
+ */
+ public HostAddresses(HostAddress[] newAddresses) {
+ _addresses = newAddresses;
+ }
+
+ public HostAddresses() {
+ _addresses = new HostAddress[1];
+ _addresses[0] = new HostAddress();
+ }
+
+ public boolean contains(HostAddress address) {
+ if (_addresses != null) {
+ for (int i = 0; i < _addresses.length; i++)
+ if (_addresses[i].equals(address))
+ return true;
+ }
+ return false;
+ }
+
+ public boolean equals(HostAddresses addresses) {
+ if ((_addresses == null && addresses._addresses != null) ||
+ (_addresses != null && addresses._addresses == null))
+ return false;
+ if (_addresses != null && addresses._addresses != null) {
+ if (_addresses.length != addresses._addresses.length)
+ return false;
+ for (int i = 0; i < _addresses.length; i++)
+ if
(!_addresses[i].equals(addresses._addresses[i]))
+ return false;
+ }
+ return true;
+ }
+
+ public HostAddress[] getAddresses() {
+ return _addresses;
+ }
+}
+