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 2007/10/23 16:01:07 UTC

svn commit: r587510 - in /directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value: HostAddress.java HostAddresses.java types/HostAddrType.java

Author: elecharny
Date: Tue Oct 23 07:01:06 2007
New Revision: 587510

URL: http://svn.apache.org/viewvc?rev=587510&view=rev
Log:
Modified the HostAddress and HostAddresses class to their new form (with the new encoder)

Modified:
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/HostAddrType.java

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java?rev=587510&r1=587509&r2=587510&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java Tue Oct 23 07:01:06 2007
@@ -52,10 +52,10 @@
 public class HostAddress extends AbstractAsn1Object
 {
     /** The logger */
-    private static final Logger log = LoggerFactory.getLogger( HostAddress.class );
+    private static final Logger LOG = LoggerFactory.getLogger( HostAddress.class );
 
     /** Speedup for logs */
-    private static final boolean IS_DEBUG = log.isDebugEnabled();
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
 
     /** The host address type. One of :
      *    Address Type                   Value
@@ -73,7 +73,7 @@
     private HostAddrType addrType;
 
     /** The address */
-    private byte[] addresse;
+    private byte[] address;
 
     // Storage for computed lengths
     private transient int addrTypeLength;
@@ -88,10 +88,10 @@
      * @param addrType
      * @param addr
      */
-    public HostAddress( HostAddrType addrType, byte[] addresse )
+    public HostAddress( HostAddrType addrType, byte[] address )
     {
         this.addrType = addrType;
-        this.addresse = addresse;
+        this.address = address;
     }
 
 
@@ -104,8 +104,8 @@
     {
         addrType = HostAddrType.ADDRTYPE_INET;
         byte[] newAddress = internetAddress.getAddress();
-        addresse = new byte[newAddress.length];
-        System.arraycopy( newAddress, 0, addresse, 0, newAddress.length );
+        address = new byte[newAddress.length];
+        System.arraycopy( newAddress, 0, address, 0, newAddress.length );
     }
 
 
@@ -115,24 +115,36 @@
      * @param that The {@link HostAddress} to compare with
      * @return true if the {@link HostAddress}'s are equal.
      */
-    public boolean equals( HostAddress that )
+    public boolean equals( Object that )
     {
-        if ( this.addrType != that.addrType || ( this.addresse != null && that.addresse == null )
-            || ( this.addresse == null && that.addresse != null ) )
+        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 ( this.addresse != null && that.addresse != null )
+        if ( address != null && hostAddress.address != null )
         {
-            if ( this.addresse.length != that.addresse.length )
+            if ( address.length != hostAddress.address.length )
             {
                 return false;
             }
 
-            for ( int ii = 0; ii < this.addresse.length; ii++ )
+            for ( int ii = 0; ii < address.length; ii++ )
             {
-                if ( this.addresse[ii] != that.addresse[ii] )
+                if ( address[ii] != hostAddress.address[ii] )
                 {
                     return false;
                 }
@@ -150,7 +162,18 @@
      */
     public byte[] getAddress()
     {
-        return addresse;
+        return address;
+    }
+
+
+    /**
+     * Set the address 
+     *
+     * @param addresse The address
+     */
+    public void setAddress( byte[] addresse )
+    {
+        this.address = addresse;
     }
 
 
@@ -182,13 +205,13 @@
         hostAddressLength = 1 + TLV.getNbBytes( addrTypeLength ) + addrTypeLength;
 
         // Compute the keyValue
-        if ( addresse == null )
+        if ( address == null )
         {
             addressLength = 1 + 1;
         }
         else
         {
-            addressLength = 1 + TLV.getNbBytes( addresse.length ) + addresse.length;
+            addressLength = 1 + TLV.getNbBytes( address.length ) + address.length;
         }
 
         hostAddressLength += 1 + TLV.getNbBytes( addressLength ) + addressLength;
@@ -236,11 +259,11 @@
             // The address, first the tag, then the value
             buffer.put( ( byte ) 0xA1 );
             buffer.put( TLV.getBytes( addressLength ) );
-            Value.encode( buffer, addresse );
+            Value.encode( buffer, address );
         }
         catch ( BufferOverflowException boe )
         {
-            log.error(
+            LOG.error(
                 "Cannot encode the HostAddress object, the PDU size is {} when only {} bytes has been allocated", 1
                     + TLV.getNbBytes( hostAddressLength ) + hostAddressLength, buffer.capacity() );
             throw new EncoderException( "The PDU buffer size is too small !" );
@@ -248,8 +271,8 @@
 
         if ( IS_DEBUG )
         {
-            log.debug( "Checksum encoding : {}", StringTools.dumpBytes( buffer.array() ) );
-            log.debug( "Checksum initial value : {}", toString() );
+            LOG.debug( "Checksum encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            LOG.debug( "Checksum initial value : {}", toString() );
         }
 
         return buffer;
@@ -268,6 +291,28 @@
 
 
     /**
+     * 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 );
+    }
+
+
+    /**
      * @see Object#toString()
      */
     public String toString()
@@ -276,7 +321,7 @@
 
         try
         {
-            result = InetAddress.getByAddress( addresse ).getHostAddress();
+            result = InetAddress.getByAddress( address ).getHostAddress();
         }
         catch ( UnknownHostException uhe )
         {

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java?rev=587510&r1=587509&r2=587510&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java Tue Oct 23 07:01:06 2007
@@ -20,13 +20,48 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+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>
  * @version $Rev$, $Date$
  */
-public class HostAddresses
+public class HostAddresses extends AbstractAsn1Object
 {
-    private HostAddress[] addresses;
+    /** 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;
 
 
     /**
@@ -34,9 +69,33 @@
      *
      * @param addresses
      */
+    public HostAddresses()
+    {
+        this.addresses = new ArrayList<HostAddress>();
+    }
+    
+
+    /**
+     * Creates a new instance of HostAddresses.
+     *
+     * @param addresses The associated addresses
+     */
     public HostAddresses( HostAddress[] addresses )
     {
-        this.addresses = addresses;
+        if ( addresses == null )
+        {
+            this.addresses = new ArrayList<HostAddress>();
+        }
+        else
+        {
+            this.addresses = Arrays.asList( addresses );
+        }
+    }
+
+
+    public void addHostAddress( HostAddress hostAddress )
+    {
+        addresses.add( hostAddress );
     }
 
 
@@ -50,13 +109,7 @@
     {
         if ( addresses != null )
         {
-            for ( int ii = 0; ii < addresses.length; ii++ )
-            {
-                if ( addresses[ii].equals( address ) )
-                {
-                    return true;
-                }
-            }
+            return addresses.contains( address );
         }
 
         return false;
@@ -71,22 +124,25 @@
      */
     public boolean equals( HostAddresses that )
     {
-        if ( ( this.addresses == null && that.addresses != null )
-            || ( this.addresses != null && that.addresses == null ) )
+        if ( ( addresses == null && that.addresses != null )
+            || ( addresses != null && that.addresses == null ) )
         {
             return false;
         }
 
-        if ( this.addresses != null && that.addresses != null )
+        if ( addresses != null && that.addresses != null )
         {
-            if ( this.addresses.length != that.addresses.length )
+            if ( addresses.size() != that.addresses.size() )
             {
                 return false;
             }
 
-            for ( int ii = 0; ii < this.addresses.length; ii++ )
+            HostAddress[] thisHostAddresses = ( HostAddress[] ) addresses.toArray();
+            HostAddress[] thatHostAddresses = ( HostAddress[] ) that.addresses.toArray();
+
+            for ( int i = 0; i < thisHostAddresses.length; i++ )
             {
-                if ( !this.addresses[ii].equals( that.addresses[ii] ) )
+                if ( !thisHostAddresses[i].equals( thatHostAddresses[i] ) )
                 {
                     return false;
                 }
@@ -104,22 +160,120 @@
      */
     public HostAddress[] getAddresses()
     {
-        return addresses;
+        return ( HostAddress[] ) addresses.toArray();
     }
 
 
-    public String toString()
+    /**
+     * 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()
     {
-        StringBuffer sb = new StringBuffer();
+        // Compute the addresses length.
+        addressesLength = 0;
 
-        for ( int ii = 0; ii < this.addresses.length; ii++ )
+        if ( ( addresses != null ) && ( addresses.size() != 0 ) )
         {
-            sb.append( addresses[ii].toString() );
+            for ( HostAddress hostAddress : addresses )
+            {
+                int length = hostAddress.computeLength();
+                addressesLength += length;
+            }
+        }
+
+        return 1 + TLV.getNbBytes( addressesLength ) + addressesLength;
+    }
 
-            if ( ii < addresses.length - 1 )
+
+    /**
+     * 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( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            // The HostAddresses SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            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(
+                "Cannot encode the HostAddresses object, the PDU size is {} when only {} bytes has been allocated", 1
+                    + TLV.getNbBytes( addressesLength ) + addressesLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        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/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/HostAddrType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/HostAddrType.java?rev=587510&r1=587509&r2=587510&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/HostAddrType.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/HostAddrType.java Tue Oct 23 07:01:06 2007
@@ -21,6 +21,29 @@
 
 
 /**
+ * Host Address type. Theyare 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>
  * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Tue, 22 May 2007) $
  */