You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2010/11/07 15:54:59 UTC
svn commit: r1032292 - in /directory/apacheds/trunk/kerberos-codec/src:
main/java/org/apache/directory/shared/kerberos/
main/java/org/apache/directory/shared/kerberos/codec/encryptedData/actions/
main/java/org/apache/directory/shared/kerberos/codec/hos...
Author: elecharny
Date: Sun Nov 7 14:54:58 2010
New Revision: 1032292
URL: http://svn.apache.org/viewvc?rev=1032292&view=rev
Log:
o Added the codec for HostAddress
o Fixed a few typoes in other codecs
Added:
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressContainer.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressGrammar.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressStatesEnum.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddrType.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddress.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressInit.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddrType.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddress.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java
directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressDecoderTest.java
Modified:
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/KerberosConstants.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/encryptedData/actions/EncryptedDataInit.java
directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EncryptedDecoderTest.java
Modified: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/KerberosConstants.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/KerberosConstants.java?rev=1032292&r1=1032291&r2=1032292&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/KerberosConstants.java (original)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/KerberosConstants.java Sun Nov 7 14:54:58 2010
@@ -44,4 +44,8 @@ public class KerberosConstants
public static final int ENCRYPTED_DATA_ETYPE_TAG = 0xA0;
public static final int ENCRYPTED_DATA_KVNO_TAG = 0xA1;
public static final int ENCRYPTED_DATA_CIPHER_TAG = 0xA2;
+
+ /** HostAddress' tags */
+ public static final int HOST_ADDRESS_ADDR_TYPE_TAG = 0xA0;
+ public static final int HOST_ADDRESS_ADDRESS_TAG = 0xA1;
}
Modified: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/encryptedData/actions/EncryptedDataInit.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/encryptedData/actions/EncryptedDataInit.java?rev=1032292&r1=1032291&r2=1032292&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/encryptedData/actions/EncryptedDataInit.java (original)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/encryptedData/actions/EncryptedDataInit.java Sun Nov 7 14:54:58 2010
@@ -33,7 +33,7 @@ import org.slf4j.LoggerFactory;
/**
- * The action used to initialize the EncryptedPart object
+ * The action used to initialize the EncryptedData object
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
@@ -47,11 +47,11 @@ public class EncryptedDataInit extends G
/**
- * Instantiates a new EncryptedPartInit action.
+ * Instantiates a new EncryptedDataInit action.
*/
public EncryptedDataInit()
{
- super( "Creates a EncryptedPart instance" );
+ super( "Creates a EncryptedData instance" );
}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressContainer.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressContainer.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressContainer.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.codec.hostAddress;
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+
+
+/**
+ * The HostAddress container stores the HostAddress decoded by the Asn1Decoder.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HostAddressContainer extends AbstractContainer
+{
+ /** An HostAddress container */
+ private HostAddress hostAddress;
+
+ /**
+ * Creates a new HostAddressContainer object.
+ */
+ public HostAddressContainer()
+ {
+ super();
+ this.stateStack = new int[1];
+ this.grammar = HostAddressGrammar.getInstance();
+ setTransition( HostAddressStatesEnum.START_STATE );
+ }
+
+
+ /**
+ * @return Returns the HostAddress.
+ */
+ public HostAddress getHostAddress()
+ {
+ return hostAddress;
+ }
+
+
+ /**
+ * Set a HostAddress Object into the container. It will be completed by the
+ * KerberosDecoder.
+ *
+ * @param hostAddress The HostAddress to set.
+ */
+ public void setHostAddress( HostAddress hostAddress )
+ {
+ this.hostAddress = hostAddress;
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressGrammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressGrammar.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressGrammar.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressGrammar.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.codec.hostAddress;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.Grammar;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.kerberos.KerberosConstants;
+import org.apache.directory.shared.kerberos.codec.actions.CheckNotNullLength;
+import org.apache.directory.shared.kerberos.codec.hostAddress.actions.HostAddressAddrType;
+import org.apache.directory.shared.kerberos.codec.hostAddress.actions.HostAddressAddress;
+import org.apache.directory.shared.kerberos.codec.hostAddress.actions.HostAddressInit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the HostAddress structure. All the actions are declared
+ * in this class. As it is a singleton, these declaration are only done once. If
+ * an action is to be added or modified, this is where the work is to be done !
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public final class HostAddressGrammar extends AbstractGrammar
+{
+ /** The logger */
+ static final Logger LOG = LoggerFactory.getLogger( HostAddressGrammar.class );
+
+ /** A speedup for logger */
+ static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+ /** The instance of grammar. HostAddressGrammar is a singleton */
+ private static Grammar instance = new HostAddressGrammar();
+
+
+ /**
+ * Creates a new HostAddressGrammar object.
+ */
+ private HostAddressGrammar()
+ {
+ setName( HostAddressGrammar.class.getName() );
+
+ // Create the transitions table
+ super.transitions = new GrammarTransition[HostAddressStatesEnum.LAST_HOST_ADDRESS_STATE.ordinal()][256];
+
+ // ============================================================================================
+ // HostAddress
+ // ============================================================================================
+ // --------------------------------------------------------------------------------------------
+ // Transition from HostAddress init to HostAddress SEQ
+ // --------------------------------------------------------------------------------------------
+ // HostAddress ::= SEQUENCE
+ super.transitions[HostAddressStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = new GrammarTransition(
+ HostAddressStatesEnum.START_STATE, HostAddressStatesEnum.HOST_ADDRESS_SEQ_STATE, UniversalTag.SEQUENCE.getValue(),
+ new HostAddressInit() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from HostAddress SEQ to addr-type tag
+ // --------------------------------------------------------------------------------------------
+ // HostAddress ::= SEQUENCE
+ // addr-type [0]
+ super.transitions[HostAddressStatesEnum.HOST_ADDRESS_SEQ_STATE.ordinal()][KerberosConstants.HOST_ADDRESS_ADDR_TYPE_TAG] = new GrammarTransition(
+ HostAddressStatesEnum.HOST_ADDRESS_SEQ_STATE, HostAddressStatesEnum.HOST_ADDRESS_ADDR_TYPE_TAG_STATE, KerberosConstants.HOST_ADDRESS_ADDR_TYPE_TAG,
+ new CheckNotNullLength() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from addr-type tag to addr-type value
+ // --------------------------------------------------------------------------------------------
+ // HostAddress ::= SEQUENCE
+ // addr-type [0] Int32,
+ super.transitions[HostAddressStatesEnum.HOST_ADDRESS_ADDR_TYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] = new GrammarTransition(
+ HostAddressStatesEnum.HOST_ADDRESS_ADDR_TYPE_TAG_STATE, HostAddressStatesEnum.HOST_ADDRESS_ADDR_TYPE_STATE, UniversalTag.INTEGER.getValue(),
+ new HostAddressAddrType() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from addr-type value to address tag
+ // --------------------------------------------------------------------------------------------
+ // HostAddress ::= SEQUENCE
+ // ...
+ // address [1]
+ super.transitions[HostAddressStatesEnum.HOST_ADDRESS_ADDR_TYPE_STATE.ordinal()][KerberosConstants.HOST_ADDRESS_ADDRESS_TAG] = new GrammarTransition(
+ HostAddressStatesEnum.HOST_ADDRESS_ADDR_TYPE_STATE, HostAddressStatesEnum.HOST_ADDRESS_ADDRESS_TAG_STATE, KerberosConstants.HOST_ADDRESS_ADDRESS_TAG,
+ new CheckNotNullLength() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from address tag to address value
+ // --------------------------------------------------------------------------------------------
+ // HostAddress ::= SEQUENCE
+ // ...
+ // address [1] OCTET STRING
+ super.transitions[HostAddressStatesEnum.HOST_ADDRESS_ADDRESS_TAG_STATE.ordinal()][UniversalTag.OCTET_STRING.ordinal()] = new GrammarTransition(
+ HostAddressStatesEnum.HOST_ADDRESS_ADDRESS_TAG_STATE, HostAddressStatesEnum.HOST_ADDRESS_ADDRESS_STATE, UniversalTag.OCTET_STRING.ordinal(),
+ new HostAddressAddress() );
+ }
+
+
+ // ~ Methods
+ // ------------------------------------------------------------------------------------
+
+ /**
+ * Get the instance of this grammar
+ *
+ * @return An instance on the HostAddress Grammar
+ */
+ public static Grammar getInstance()
+ {
+ return instance;
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressStatesEnum.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressStatesEnum.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/HostAddressStatesEnum.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.codec.hostAddress;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.Grammar;
+import org.apache.directory.shared.asn1.ber.grammar.States;
+import org.apache.directory.shared.kerberos.codec.KerberosMessageGrammar;
+
+
+/**
+ * This class store the HostAddress grammar's constants. It is also used for debugging
+ * purpose
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum HostAddressStatesEnum implements States
+{
+ // Start
+ START_STATE, // 0
+
+ // ----- PrincipalName message --------------------------------------
+ HOST_ADDRESS_SEQ_STATE, // 1
+
+ HOST_ADDRESS_ADDR_TYPE_TAG_STATE, // 2
+ HOST_ADDRESS_ADDR_TYPE_STATE, // 3
+
+ HOST_ADDRESS_ADDRESS_TAG_STATE, // 4
+ HOST_ADDRESS_ADDRESS_STATE, // 5
+
+ // End
+ LAST_HOST_ADDRESS_STATE; // 6
+
+
+ /**
+ * Get the grammar name
+ *
+ * @param grammar The grammar code
+ * @return The grammar name
+ */
+ public String getGrammarName( int grammar )
+ {
+ return "HOST_ADDRESS_GRAMMAR";
+ }
+
+
+ /**
+ * Get the grammar name
+ *
+ * @param grammar The grammar class
+ * @return The grammar name
+ */
+ public String getGrammarName( Grammar grammar )
+ {
+ if ( grammar instanceof KerberosMessageGrammar )
+ {
+ return "HOST_ADDRESS_GRAMMAR";
+ }
+ else
+ {
+ return "UNKNOWN GRAMMAR";
+ }
+ }
+
+
+ /**
+ * Get the string representing the state
+ *
+ * @param state The state number
+ * @return The String representing the state
+ */
+ public String getState( int state )
+ {
+ return ( ( state == LAST_HOST_ADDRESS_STATE.ordinal() ) ? "HOST_ADDRESS_END_STATE" : name() );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isEndState()
+ {
+ return this == LAST_HOST_ADDRESS_STATE;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public HostAddressStatesEnum getStartState()
+ {
+ return START_STATE;
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddrType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddrType.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddrType.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddrType.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.codec.hostAddress.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.util.IntegerDecoder;
+import org.apache.directory.shared.asn1.util.IntegerDecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.KerberosMessageGrammar;
+import org.apache.directory.shared.kerberos.codec.hostAddress.HostAddressContainer;
+import org.apache.directory.shared.kerberos.components.HostAddrType;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to initialize the HostAddress object
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HostAddressAddrType extends GrammarAction
+{
+ /** The logger */
+ private static final Logger LOG = LoggerFactory.getLogger( KerberosMessageGrammar.class );
+
+ /** Speedup for logs */
+ private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+ /**
+ * Instantiates a new HostAddressInit action.
+ */
+ public HostAddressAddrType()
+ {
+ super( "Creates a HostAddress instance" );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public void action( Asn1Container container ) throws DecoderException
+ {
+ HostAddressContainer hostAddressContainer = ( HostAddressContainer ) container;
+
+ TLV tlv = hostAddressContainer.getCurrentTLV();
+
+ // The Length should not be null
+ if ( tlv.getLength() == 0 )
+ {
+ LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+ // This will generate a PROTOCOL_ERROR
+ throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+ }
+
+ // Get the address type now
+ Value value = tlv.getValue();
+
+ try
+ {
+ int addrType = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE );
+ HostAddrType hostAddrType = HostAddrType.getTypeByOrdinal( addrType );
+
+ HostAddress hostAddressData = hostAddressContainer.getHostAddress();
+
+ hostAddressData.setAddrType( hostAddrType );
+
+ if ( IS_DEBUG )
+ {
+ LOG.debug( "addr-type : {}", hostAddrType );
+ }
+ }
+ catch ( IntegerDecoderException ide )
+ {
+ LOG.error( I18n.err( I18n.ERR_04070, StringTools.dumpBytes( value.getData() ), ide
+ .getLocalizedMessage() ) );
+
+ // This will generate a PROTOCOL_ERROR
+ throw new DecoderException( ide.getMessage() );
+ }
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddress.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddress.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddress.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressAddress.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.codec.hostAddress.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.KerberosMessageGrammar;
+import org.apache.directory.shared.kerberos.codec.hostAddress.HostAddressContainer;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to add the HostAddress address value
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HostAddressAddress extends GrammarAction
+{
+ /** The logger */
+ private static final Logger LOG = LoggerFactory.getLogger( KerberosMessageGrammar.class );
+
+ /** Speedup for logs */
+ private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+ /**
+ * Instantiates a new HostAddressAddress action.
+ */
+ public HostAddressAddress()
+ {
+ super( "Store the HostAddress' address" );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public void action( Asn1Container container ) throws DecoderException
+ {
+ HostAddressContainer hostAddressContainer = ( HostAddressContainer ) container;
+
+ TLV tlv = hostAddressContainer.getCurrentTLV();
+
+ // The Length should not be null
+ if ( tlv.getLength() == 0 )
+ {
+ LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+ // This will generate a PROTOCOL_ERROR
+ throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+ }
+
+ // Get the address value now
+ Value value = tlv.getValue();
+
+ HostAddress hostAddressData = hostAddressContainer.getHostAddress();
+
+ hostAddressData.setAddress( value.getData() );
+ hostAddressContainer.setGrammarEndAllowed( true );
+
+ if ( IS_DEBUG )
+ {
+ LOG.debug( "address : {}" + StringTools.dumpBytes( value.getData() ) );
+ }
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressInit.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressInit.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressInit.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddress/actions/HostAddressInit.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.codec.hostAddress.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.KerberosMessageGrammar;
+import org.apache.directory.shared.kerberos.codec.hostAddress.HostAddressContainer;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to initialize the HostAddress object
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HostAddressInit extends GrammarAction
+{
+ /** The logger */
+ private static final Logger LOG = LoggerFactory.getLogger( KerberosMessageGrammar.class );
+
+ /** Speedup for logs */
+ private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+ /**
+ * Instantiates a new HostAddressInit action.
+ */
+ public HostAddressInit()
+ {
+ super( "Creates a HostAddress instance" );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public void action( Asn1Container container ) throws DecoderException
+ {
+ HostAddressContainer hostAddressContainer = ( HostAddressContainer ) container;
+
+ TLV tlv = hostAddressContainer.getCurrentTLV();
+
+ // The Length should not be null
+ if ( tlv.getLength() == 0 )
+ {
+ LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+ // This will generate a PROTOCOL_ERROR
+ throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+ }
+
+ HostAddress hostAddress = new HostAddress();
+ hostAddressContainer.setHostAddress( hostAddress );
+
+ if ( IS_DEBUG )
+ {
+ LOG.debug( "HostAddress created" );
+ }
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddrType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddrType.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddrType.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddrType.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,296 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.components;
+
+
+/**
+ * Host Address type. They are described in RFC 4120, chap. 7.5.3.
+ *
+ * Only a few of them are declared :
+ *
+ * Address Type Value
+ *
+ * IPv4 2
+ * Directional 3
+ * ChaosNet 5
+ * XNS 6
+ * ISO 7
+ * DECNET Phase IV 12
+ * AppleTalk DDP 16
+ * NetBios 20
+ * IPv6 24
+ *
+ * The other address types are simply ignored. They are part of a Unix
+ * include file.
+ *
+ * @todo find the original include where those ignored values come from
+ *
+ * To be realistic, we may encounter IPv4, IPv6 and NetBios addresses in the real world...
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum HostAddrType
+{
+ /**
+ * Constant for the "null" host address type.
+ */
+ NULL( 0 ),
+
+ /**
+ * Constant for the "Unix" host address type.
+ *
+ * Not in RFC
+ */
+ // ADDRTYPE_UNIX( 1 ),
+
+ /**
+ * Constant for the "Internet" host address type.
+ */
+ ADDRTYPE_INET( 2 ),
+
+ /**
+ * Constant for the "Arpanet" host address type.
+ */
+ ADDRTYPE_IMPLINK( 3 ),
+
+ /**
+ * Constant for the "PUP" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_PUP( 4 ),
+
+ /**
+ * Constant for the "CHAOS" host address type.
+ */
+ ADDRTYPE_CHAOS( 5 ),
+
+ /**
+ * Constant for the "XEROX Network Services" host address type.
+ */
+ ADDRTYPE_XNS( 6 ),
+
+ /**
+ * Constant for the "IPX" host address type.
+ *
+ * Not in RFC
+ */
+ // ADDRTYPE_IPX( 6 ),
+
+ /**
+ * Constant for the "OSI" host address type.
+ */
+ ADDRTYPE_OSI( 7 ),
+
+ /**
+ * Constant for the "European Computer Manufacturers" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_ECMA( 8 ),
+
+ /**
+ * Constant for the "Datakit" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_DATAKIT( 9 ),
+
+ /**
+ * Constant for the "CCITT" host address type.
+ *
+ * Not in RFC
+ */
+ //pADDRTYPE_CCITT( 10 ),
+
+ /**
+ * Constant for the "SNA" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_SNA( 11 ),
+
+ /**
+ * Constant for the "DECnet" host address type.
+ */
+ ADDRTYPE_DECNET( 12 ),
+
+ /**
+ * Constant for the "Direct Data Link Interface" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_DLI( 13 ),
+
+ /**
+ * Constant for the "LAT" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_LAT( 14 ),
+
+ /**
+ * Constant for the "NSC Hyperchannel" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_HYLINK( 15 ),
+
+ /**
+ * Constant for the "AppleTalk" host address type.
+ */
+ ADDRTYPE_APPLETALK( 16 ),
+
+ /**
+ * Constant for the "VoiceView" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_VOICEVIEW( 18 ),
+
+ /**
+ * Constant for the "Firefox" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_FIREFOX( 19 ),
+
+ /**
+ * Constant for the "NetBios" host address type.
+ *
+ * Not in RFC
+ */
+ ADDRTYPE_NETBIOS( 20 ),
+
+ /**
+ * Constant for the "Banyan" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_BAN( 21 ),
+
+ /**
+ * Constant for the "ATM" host address type.
+ *
+ * Not in RFC
+ */
+ //ADDRTYPE_ATM( 22 ),
+
+ /**
+ * Constant for the "Internet Protocol V6" host address type.
+ */
+ ADDRTYPE_INET6( 24 );
+
+ /**
+ * The value/code for the host address type.
+ */
+ private final int ordinal;
+
+
+ /**
+ * Private constructor prevents construction outside of this class.
+ */
+ private HostAddrType( int ordinal )
+ {
+ this.ordinal = ordinal;
+ }
+
+
+ /**
+ * Returns the host address type when specified by its ordinal.
+ *
+ * @param type
+ * @return The host address type.
+ */
+ public static HostAddrType getTypeByOrdinal( int type )
+ {
+ switch ( type )
+ {
+ case 0 : return NULL;
+ //case 1 : return ADDRTYPE_UNIX;
+ case 2 : return ADDRTYPE_INET;
+ case 3 : return ADDRTYPE_IMPLINK;
+ //case 4 : return ADDRTYPE_PUP;
+ case 5 : return ADDRTYPE_CHAOS;
+ case 6 : return ADDRTYPE_XNS;
+ case 7 : return ADDRTYPE_OSI;
+ //case 8 : return ADDRTYPE_ECMA;
+ //case 9 : return ADDRTYPE_DATAKIT;
+ //case 10 : return pADDRTYPE_CCITT;
+ //case 11 : return ADDRTYPE_SNA;
+ case 12 : return ADDRTYPE_DECNET;
+ //case 13 : return ADDRTYPE_DLI;
+ //case 14 : return ADDRTYPE_LAT;
+ //case 15 : return ADDRTYPE_HYLINK;
+ case 16 : return ADDRTYPE_APPLETALK;
+ //case 18 : return ADDRTYPE_VOICEVIEW;
+ //case 19 : return ADDRTYPE_FIREFOX;
+ case 20 : return ADDRTYPE_NETBIOS;
+ //case 21 : return ADDRTYPE_BAN;
+ //case 22 : return ADDRTYPE_ATM;
+ case 24 : return ADDRTYPE_INET6;
+ default : return NULL;
+ }
+ }
+
+
+ /**
+ * Returns the number associated with this host address type.
+ *
+ * @return The host address type ordinal.
+ */
+ public int getOrdinal()
+ {
+ return ordinal;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String toString()
+ {
+ switch ( ordinal )
+ {
+ //case 1 : return "Unix" + "(" + ordinal + ")" ;
+ case 2 : return "Internet" + "(" + ordinal + ")" ;
+ case 3 : return "Arpanet" + "(" + ordinal + ")" ;
+ //case 4 : return "PUP" + "(" + ordinal + ")" ;
+ case 5 : return "CHAOS" + "(" + ordinal + ")" ;
+ case 6 : return "XEROX Network Services" + "(" + ordinal + ")" ;
+ case 7 : return "OSI" + "(" + ordinal + ")" ;
+ //case 8 : return "European Computer Manufacturers" + "(" + ordinal + ")" ;
+ //case 9 : return "Datakit" + "(" + ordinal + ")" ;
+ //case 10 : return "CCITT" + "(" + ordinal + ")" ;
+ //case 11 : return "SNA" + "(" + ordinal + ")" ;
+ case 12 : return "DECnet" + "(" + ordinal + ")" ;
+ //case 13 : return "Direct Data Link Interface" + "(" + ordinal + ")" ;
+ //case 14 : return "LAT" + "(" + ordinal + ")" ;
+ //case 15 : return "NSC Hyperchannel" + "(" + ordinal + ")" ;
+ //case 16 : return "AppleTalk" + "(" + ordinal + ")" ;
+ //case 18 : return "VoiceView" + "(" + ordinal + ")" ;
+ //case 19 : return "Firefox" + "(" + ordinal + ")" ;
+ case 20 : return "NetBios" + "(" + ordinal + ")" ;
+ //case 21 : return "Banyan" + "(" + ordinal + ")" ;
+ //case 22 : return "ATM" + "(" + ordinal + ")" ;
+ case 24 : return "Internet Protocol V6" + "(" + ordinal + ")" ;
+ default : return "null" + "(" + ordinal + ")" ;
+ }
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddress.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddress.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddress.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddress.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,358 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.components;
+
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.directory.server.i18n.I18n;
+import org.apache.directory.shared.asn1.AbstractAsn1Object;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Provides host address information.
+ *
+ * The ASN.1 grammaor for this structure is :
+ *
+ * HostAddress ::= SEQUENCE {
+ * addr-type [0] Int32,
+ * address [1] OCTET STRING
+ * }
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HostAddress extends AbstractAsn1Object
+{
+ /** The logger */
+ private static final Logger LOG = LoggerFactory.getLogger( HostAddress.class );
+
+ /** Speedup for logs */
+ private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+ /** The host address type. One of :
+ * Address Type Value
+ *
+ * IPv4 2
+ * Directional 3
+ * ChaosNet 5
+ * XNS 6
+ * ISO 7
+ * DECNET Phase IV 12
+ * AppleTalk DDP 16
+ * NetBios 20
+ * IPv6 24
+ */
+ private HostAddrType addrType;
+
+ /** The address */
+ private byte[] address;
+
+ // Storage for computed lengths
+ private transient int addrTypeLength;
+ private transient int addressLength;
+ private transient int hostAddressLength;
+ private transient int hostAddressSeqLength;
+
+
+ /**
+ * Creates an empty HostAdress instance
+ */
+ public HostAddress()
+ {
+ }
+
+ /**
+ * Creates a new instance of HostAddress.
+ *
+ * @param addrType The type of address
+ * @param address The address
+ */
+ public HostAddress( HostAddrType addrType, byte[] address )
+ {
+ this.addrType = addrType;
+ this.address = address;
+ }
+
+
+ /**
+ * Creates a new instance of HostAddress.
+ *
+ * @param internetAddress The Inet form address
+ */
+ public HostAddress( InetAddress internetAddress )
+ {
+ addrType = HostAddrType.ADDRTYPE_INET;
+ byte[] newAddress = internetAddress.getAddress();
+ address = new byte[newAddress.length];
+ System.arraycopy( newAddress, 0, address, 0, newAddress.length );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode()
+ {
+ int hash = 37;
+ hash = hash * 17 + addrType.hashCode();
+
+ if ( address != null )
+ {
+ hash = hash * 17 + Arrays.hashCode( address );
+ }
+
+ return hash;
+ }
+
+
+ /**
+ * Returns whether one {@link HostAddress} is equal to another.
+ *
+ * @param that The {@link HostAddress} to compare with
+ * @return true if the {@link HostAddress}'s are equal.
+ */
+ @Override
+ public boolean equals( Object that )
+ {
+ if ( this == that )
+ {
+ return true;
+ }
+
+ if ( !(that instanceof HostAddress ) )
+ {
+ return false;
+ }
+
+ HostAddress hostAddress = (HostAddress)that;
+
+ if ( addrType != hostAddress.addrType || ( address != null && hostAddress.address == null )
+ || ( address == null && hostAddress.address != null ) )
+ {
+ return false;
+ }
+
+ if ( address != null && hostAddress.address != null )
+ {
+ if ( address.length != hostAddress.address.length )
+ {
+ return false;
+ }
+
+ for ( int ii = 0; ii < address.length; ii++ )
+ {
+ if ( address[ii] != hostAddress.address[ii] )
+ {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+
+
+ /**
+ * Get the bytes for this address.
+ *
+ * @return The bytes of this address.
+ */
+ public byte[] getAddress()
+ {
+ return address;
+ }
+
+
+ /**
+ * Set the address
+ *
+ * @param addresse The address
+ */
+ public void setAddress( byte[] addresse )
+ {
+ this.address = addresse;
+ }
+
+
+ /**
+ * Compute the host address length
+ *
+ * HostAddress :
+ *
+ * 0x30 L1 hostAddress sequence
+ * |
+ * +--> 0xA0 L2 addrType tag
+ * | |
+ * | +--> 0x02 L2-1 addrType (int)
+ * |
+ * +--> 0xA1 L3 address tag
+ * |
+ * +--> 0x04 L3-1 address (OCTET STRING)
+ *
+ * where L1 = L2 + length(0xA0) + length(L2) +
+ * L3 + length(0xA1) + length(L3)
+ * and
+ * L2 = L2-1 + length(0x02) + length( L2-1)
+ * L3 = L3-1 + length(0x04) + length( L3-1)
+ */
+ public int computeLength()
+ {
+ // Compute the keyType. The Length will always be contained in 1 byte
+ addrTypeLength = 1 + 1 + Value.getNbBytes( addrType.getOrdinal() );
+ hostAddressLength = 1 + TLV.getNbBytes( addrTypeLength ) + addrTypeLength;
+
+ // Compute the keyValue
+ if ( address == null )
+ {
+ addressLength = 1 + 1;
+ }
+ else
+ {
+ addressLength = 1 + TLV.getNbBytes( address.length ) + address.length;
+ }
+
+ hostAddressLength += 1 + TLV.getNbBytes( addressLength ) + addressLength;
+
+ // Compute the whole sequence length
+ hostAddressSeqLength = 1 + Value.getNbBytes( hostAddressLength ) + hostAddressLength;
+
+ return hostAddressSeqLength;
+ }
+
+
+ /**
+ * Encode the HostAddress message to a PDU.
+ *
+ * HostAddress :
+ *
+ * 0x30 LL
+ * 0xA0 LL
+ * 0x02 0x01 addr-type
+ * 0xA1 LL
+ * 0x04 LL address
+ *
+ * @param buffer The buffer where to put the PDU. It should have been allocated
+ * before, with the right size.
+ * @return The constructed PDU.
+ */
+ public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+ {
+ if ( buffer == null )
+ {
+ throw new EncoderException( I18n.err( I18n.ERR_148 ) );
+ }
+
+ try
+ {
+ // The HostAddress SEQ Tag
+ buffer.put( UniversalTag.SEQUENCE.getValue() );
+ buffer.put( TLV.getBytes( hostAddressLength ) );
+
+ // The addr-type, first the tag, then the value
+ buffer.put( ( byte ) 0xA0 );
+ buffer.put( TLV.getBytes( addrTypeLength ) );
+ Value.encode( buffer, addrType.getOrdinal() );
+
+ // The address, first the tag, then the value
+ buffer.put( ( byte ) 0xA1 );
+ buffer.put( TLV.getBytes( addressLength ) );
+ Value.encode( buffer, address );
+ }
+ catch ( BufferOverflowException boe )
+ {
+ LOG.error( I18n.err( I18n.ERR_143, 1 + TLV.getNbBytes( hostAddressLength )
+ + hostAddressLength, buffer.capacity() ) );
+ throw new EncoderException( I18n.err( I18n.ERR_138 ) );
+ }
+
+ if ( IS_DEBUG )
+ {
+ LOG.debug( "Checksum encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+ LOG.debug( "Checksum initial value : {}", toString() );
+ }
+
+ return buffer;
+ }
+
+
+ /**
+ * Returns the {@link HostAddrType} of this {@link HostAddress}.
+ *
+ * @return The {@link HostAddrType}.
+ */
+ public HostAddrType getAddrType()
+ {
+ return addrType;
+ }
+
+
+ /**
+ * Set the addr-type field
+ *
+ * @param addrType The address type
+ */
+ public void setAddrType( HostAddrType addrType )
+ {
+ this.addrType = addrType;
+ }
+
+
+ /**
+ * Set the addr-type field
+ *
+ * @param addrType The address type
+ */
+ public void setAddrType( int addrType )
+ {
+ this.addrType = HostAddrType.getTypeByOrdinal( addrType );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public String toString()
+ {
+ String result = "";
+
+ try
+ {
+ result = InetAddress.getByAddress( address ).getHostAddress();
+ }
+ catch ( UnknownHostException uhe )
+ {
+ result = "Unknow host : " + StringTools.utf8ToString( address );
+ }
+
+ return result;
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.components;
+
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.directory.server.i18n.I18n;
+import org.apache.directory.shared.asn1.AbstractAsn1Object;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Store a list of addresses.
+ *
+ * The ASN.1 grammar is :
+ *
+ * -- NOTE: HostAddresses is always used as an OPTIONAL field and
+ * -- should not be empty.
+ * HostAddresses -- NOTE: subtly different from rfc1510,
+ * -- but has a value mapping and encodes the same
+ * ::= SEQUENCE OF HostAddress
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HostAddresses extends AbstractAsn1Object
+{
+ /** The logger */
+ private static final Logger LOG = LoggerFactory.getLogger( HostAddresses.class );
+
+ /** Speedup for logs */
+ private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+ /** List of all HostAddress stored */
+ private List<HostAddress> addresses;
+
+ // Storage for computed lengths
+ private transient int addressesLength;
+
+
+ /**
+ * Creates a new instance of HostAddresses.
+ */
+ public HostAddresses()
+ {
+ this.addresses = new ArrayList<HostAddress>();
+ }
+
+
+ /**
+ * Creates a new instance of HostAddresses.
+ *
+ * @param addresses The associated addresses
+ */
+ public HostAddresses( HostAddress[] addresses )
+ {
+ if ( addresses == null )
+ {
+ this.addresses = new ArrayList<HostAddress>();
+ }
+ else
+ {
+ this.addresses = Arrays.asList( addresses );
+ }
+ }
+
+
+ /**
+ * Adds an {@link HostAddresses} to the list
+ * @param hostAddress The address to add
+ */
+ public void addHostAddress( HostAddress hostAddress )
+ {
+ addresses.add( hostAddress );
+ }
+
+
+ /**
+ * Returns true if this {@link HostAddresses} contains a specified {@link HostAddress}.
+ *
+ * @param address The address we are looking for in the existing list
+ * @return true if this {@link HostAddresses} contains a specified {@link HostAddress}.
+ */
+ public boolean contains( HostAddress address )
+ {
+ if ( addresses != null )
+ {
+ return addresses.contains( address );
+ }
+
+ return false;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode()
+ {
+ int hash = 37;
+
+ if ( addresses != null )
+ {
+ hash = hash * 17 + addresses.size();
+ hash = 17 + addresses.hashCode();
+ }
+
+ return hash;
+ }
+
+
+ /**
+ * Returns true if two {@link HostAddresses} are equal.
+ *
+ * @param that Th {@link HostAddresses} we want to compare with the current one
+ * @return true if two {@link HostAddresses} are equal.
+ */
+ public boolean equals( HostAddresses that )
+ {
+ if ( that == null )
+ {
+ return false;
+ }
+
+ // Addresses can't be null after creation
+ if ( addresses.size() != that.addresses.size() )
+ {
+ return false;
+ }
+
+ for ( int i = 0; i < addresses.size(); i++ )
+ {
+ if ( !addresses.get( i ).equals( that.addresses.get( i ) ) )
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+
+ /**
+ * Returns the contained {@link HostAddress}s as an array.
+ *
+ * @return An array of {@link HostAddress}s.
+ */
+ public HostAddress[] getAddresses()
+ {
+ return addresses.toArray( new HostAddress[0] );
+ }
+
+
+ /**
+ * Compute the hostAddresses length
+ *
+ * HostAddresses :
+ *
+ * 0x30 L1 hostAddresses sequence of HostAddresses
+ * |
+ * +--> 0x30 L2[1] Hostaddress[1]
+ * |
+ * +--> 0x30 L2[2] Hostaddress[2]
+ * |
+ * ...
+ * |
+ * +--> 0x30 L2[n] Hostaddress[n]
+ *
+ * where L1 = sum( L2[1], l2[2], ..., L2[n] )
+ */
+ public int computeLength()
+ {
+ // Compute the addresses length.
+ addressesLength = 0;
+
+ if ( ( addresses != null ) && ( addresses.size() != 0 ) )
+ {
+ for ( HostAddress hostAddress : addresses )
+ {
+ int length = hostAddress.computeLength();
+ addressesLength += length;
+ }
+ }
+
+ return 1 + TLV.getNbBytes( addressesLength ) + addressesLength;
+ }
+
+
+ /**
+ * Encode the HostAddress message to a PDU.
+ *
+ * HostAddress :
+ *
+ * 0x30 LL
+ * 0x30 LL hostaddress[1]
+ * 0x30 LL hostaddress[1]
+ * ...
+ * 0x30 LL hostaddress[1]
+ *
+ * @param buffer The buffer where to put the PDU. It should have been allocated
+ * before, with the right size.
+ * @return The constructed PDU.
+ */
+ public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+ {
+ if ( buffer == null )
+ {
+ throw new EncoderException( I18n.err( I18n.ERR_148 ) );
+ }
+
+ try
+ {
+ // The HostAddresses SEQ Tag
+ buffer.put( UniversalTag.SEQUENCE.getValue() );
+ buffer.put( TLV.getBytes( addressesLength ) );
+
+ // The hostAddress list, if it's not empty
+ if ( ( addresses != null ) && ( addresses.size() != 0 ) )
+ {
+ for ( HostAddress hostAddress : addresses )
+ {
+ hostAddress.encode( buffer );
+ }
+ }
+ }
+ catch ( BufferOverflowException boe )
+ {
+ LOG.error( I18n.err( I18n.ERR_144, 1 + TLV.getNbBytes( addressesLength )
+ + addressesLength, buffer.capacity() ) );
+ throw new EncoderException( I18n.err( I18n.ERR_138 ) );
+ }
+
+ if ( IS_DEBUG )
+ {
+ LOG.debug( "HostAddresses encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+ LOG.debug( "HostAddresses initial value : {}", toString() );
+ }
+
+ return buffer;
+ }
+
+
+ /**
+ * @see Object#toString()
+ */
+ public String toString()
+ {
+ StringBuilder sb = new StringBuilder();
+ boolean isFirst = true;
+
+ for ( HostAddress hostAddress : addresses )
+ {
+ if ( isFirst )
+ {
+ isFirst = false;
+ }
+ else
+ {
+ sb.append( ", " );
+ }
+
+ sb.append( hostAddress.toString() );
+ }
+
+ return sb.toString();
+ }
+}
Modified: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EncryptedDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EncryptedDecoderTest.java?rev=1032292&r1=1032291&r2=1032292&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EncryptedDecoderTest.java (original)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EncryptedDecoderTest.java Sun Nov 7 14:54:58 2010
@@ -84,7 +84,6 @@ public class EncryptedDecoderTest
}
catch ( DecoderException de )
{
- de.printStackTrace();
fail( de.getMessage() );
}
@@ -147,7 +146,6 @@ public class EncryptedDecoderTest
}
catch ( DecoderException de )
{
- de.printStackTrace();
fail( de.getMessage() );
}
Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressDecoderTest.java?rev=1032292&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressDecoderTest.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressDecoderTest.java Sun Nov 7 14:54:58 2010
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.shared.kerberos.codec;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.directory.junit.tools.Concurrent;
+import org.apache.directory.junit.tools.ConcurrentJunitRunner;
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.kerberos.codec.hostAddress.HostAddressContainer;
+import org.apache.directory.shared.kerberos.components.HostAddrType;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * Test the HostAddress decoder.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrent()
+public class HostAddressDecoderTest
+{
+ /**
+ * Test the decoding of a HostAddress
+ */
+ @Test
+ public void testHostAddress()
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x16 );
+
+ stream.put( new byte[]
+ { 0x30, 0x14,
+ (byte)0xA0, 0x03, // addr-type
+ 0x02, 0x01, 0x02, // IPV4
+ (byte)0xA1, 0x0D, // address : 192.168.0.1
+ 0x04, 0x0B, '1', '9', '2', '.', '1', '6', '8', '.', '0', '.', '1'
+ } );
+
+ String decodedPdu = StringTools.dumpBytes( stream.array() );
+ stream.flip();
+
+ // Allocate a EncryptedHostAddressData Container
+ Asn1Container hostAddressContainer = new HostAddressContainer();
+
+ // Decode the HostAddress PDU
+ try
+ {
+ kerberosDecoder.decode( stream, hostAddressContainer );
+ }
+ catch ( DecoderException de )
+ {
+ fail( de.getMessage() );
+ }
+
+ // Check the decoded HostAddress
+ HostAddress hostAddress = ( ( HostAddressContainer ) hostAddressContainer ).getHostAddress();
+
+ assertEquals( HostAddrType.ADDRTYPE_INET, hostAddress.getAddrType() );
+ assertTrue( Arrays.equals( StringTools.getBytesUtf8( "192.168.0.1" ), hostAddress.getAddress() ) );
+
+ // Check the encoding
+ ByteBuffer bb = ByteBuffer.allocate( hostAddress.computeLength() );
+
+ try
+ {
+ bb = hostAddress.encode( bb );
+
+ // Check the length
+ assertEquals( 0x16, bb.limit() );
+
+ String encodedPdu = StringTools.dumpBytes( bb.array() );
+
+ assertEquals( encodedPdu, decodedPdu );
+ }
+ catch ( EncoderException ee )
+ {
+ fail();
+ }
+ }
+
+
+ /**
+ * Test the decoding of a HostAddress with nothing in it
+ */
+ @Test( expected = DecoderException.class)
+ public void testHostAddressEmpty() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x02 );
+
+ stream.put( new byte[]
+ { 0x30, 0x00 } );
+
+ stream.flip();
+
+ // Allocate a HostAddress Container
+ Asn1Container hostAddressContainer = new HostAddressContainer();
+
+ // Decode the HostAddress PDU
+ kerberosDecoder.decode( stream, hostAddressContainer );
+ fail();
+ }
+
+
+ /**
+ * Test the decoding of a HostAddress with no addr-type
+ */
+ @Test( expected = DecoderException.class)
+ public void testHostAddressNoAddrType() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x04 );
+
+ stream.put( new byte[]
+ { 0x30, 0x02,
+ (byte)0xA0, 0x00 // addr-type
+ } );
+
+ stream.flip();
+
+ // Allocate a HostAddress Container
+ Asn1Container hostAddressContainer = new HostAddressContainer();
+
+ // Decode the HostAddress PDU
+ kerberosDecoder.decode( stream, hostAddressContainer );
+ fail();
+ }
+
+
+ /**
+ * Test the decoding of a HostAddress with an empty addr-type
+ */
+ @Test( expected = DecoderException.class)
+ public void testHostAddressEmptyAddrType() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x0B );
+
+ stream.put( new byte[]
+ { 0x30, 0x04,
+ (byte)0xA0, 0x03, // addr-type
+ 0x02, 0x00 //
+ } );
+
+ stream.flip();
+
+ // Allocate a HostAddress Container
+ Asn1Container hostAddressContainer = new HostAddressContainer();
+
+ // Decode the HostAddress PDU
+ kerberosDecoder.decode( stream, hostAddressContainer );
+ fail();
+ }
+
+
+ /**
+ * Test the decoding of a HostAddress with no address
+ */
+ @Test( expected = DecoderException.class)
+ public void testHostAddressNoAddress() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x09 );
+
+ stream.put( new byte[]
+ { 0x30, 0x07,
+ (byte)0xA0, 0x03, // addr-type
+ 0x02, 0x01, 0x02, //
+ (byte)0xA1, 0x00 // address
+ } );
+
+ stream.flip();
+
+ // Allocate a HostAddress Container
+ Asn1Container hostAddressContainer = new HostAddressContainer();
+
+ // Decode the HostAddress PDU
+ kerberosDecoder.decode( stream, hostAddressContainer );
+ fail();
+ }
+
+
+ /**
+ * Test the decoding of a HostAddress empty address
+ */
+ @Test( expected = DecoderException.class )
+ public void testHostAddressEmptyAddress() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x0E );
+
+ stream.put( new byte[]
+ { 0x30, 0x14,
+ (byte)0xA0, 0x03, // addr-type
+ 0x02, 0x01, 0x02, // IPV4
+ (byte)0xA1, 0x02, // address
+ 0x04, 0x00
+ } );
+
+ stream.flip();
+
+ // Allocate a HostAddress Container
+ Asn1Container hostAddressContainer = new HostAddressContainer();
+
+ // Decode the HostAddress PDU
+ kerberosDecoder.decode( stream, hostAddressContainer );
+ fail();
+ }
+}