You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by he...@apache.org on 2008/02/18 23:14:21 UTC
svn commit: r628890 [1/4] - in
/directory/apacheds/branches/bigbang/protocol-dhcp/src:
main/java/org/apache/directory/server/dhcp/
main/java/org/apache/directory/server/dhcp/io/
main/java/org/apache/directory/server/dhcp/messages/ main/java/org/apache/...
Author: hennejg
Date: Mon Feb 18 14:14:09 2008
New Revision: 628890
URL: http://svn.apache.org/viewvc?rev=628890&view=rev
Log:
Merge of next-generation DHCP code.
Added:
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/HardwareAddress.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/ByteOption.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/IntOption.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/ShortOption.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/StringOption.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/UnrecognizedOption.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/AbstractDhcpService.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/DhcpService.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/Lease.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/StoreBasedDhcpService.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/store/
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/store/AbstractDhcpStore.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/store/DhcpConfigElement.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/store/DhcpStore.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/store/Host.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/store/SimpleDhcpStore.java (with props)
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/store/Subnet.java (with props)
Removed:
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/DhcpService.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpOptionsDecoder.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpOptionsEncoder.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/DhcpMessageModifier.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/EndOption.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/PadOption.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/DhcpServiceImpl.java
Modified:
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/DhcpException.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageDecoder.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageEncoder.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/DhcpMessage.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressListOption.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressOption.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/DhcpOption.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/OptionsField.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/BootfileName.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/ClientIdentifier.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/DhcpMessageType.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/IpAddressLeaseTime.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/MaximumDhcpMessageSize.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/Message.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/OptionOverload.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/ParameterRequestList.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/RebindingTimeValue.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/RenewalTimeValue.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/RequestedIpAddress.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/ServerIdentifier.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/TftpServerName.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/dhcp/VendorClassIdentifier.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/linklayer/ArpCacheTimeout.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/linklayer/EthernetEncapsulation.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/linklayer/TrailerEncapsulation.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/DefaultFingerServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/DefaultIrcServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/DefaultWwwServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/MobileIpHomeAgents.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NbddServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NetbiosNameServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NetbiosNodeType.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NetbiosScope.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NisDomain.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NisPlusDomain.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NisPlusServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NisServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NntpServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/NtpServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/Pop3Servers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/SmtpServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/StdaServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/StreetTalkServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/VendorSpecificInformation.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/XWindowDisplayManagers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/misc/XWindowFontServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perhost/DefaultIpTimeToLive.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perhost/IpForwarding.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perhost/MaximumDatagramSize.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perhost/NonLocalSourceRouting.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perhost/PathMtuAgingTimeout.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perhost/PathMtuPlateauTable.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perhost/PolicyFilter.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/AllSubnetsAreLocal.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/BroadcastAddress.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/InterfaceMtu.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/MaskSupplier.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/PerformMaskDiscovery.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/PerformRouterDiscovery.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/RouterSolicitationAddress.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/perinterface/StaticRoute.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/tcp/TcpDefaultTimeToLive.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/tcp/TcpKeepaliveGarbage.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/tcp/TcpKeepaliveInterval.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/BootFileSize.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/CookieServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/DomainName.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/DomainNameServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/ExtensionsPath.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/HostName.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/ImpressServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/LogServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/LprServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/MeritDumpFile.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/NameServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/ResourceLocationServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/RootPath.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/Routers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/SubnetMask.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/SwapServer.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/TimeOffset.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/vendor/TimeServers.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/protocol/DhcpDecoder.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/protocol/DhcpEncoder.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/protocol/DhcpProtocolHandler.java
directory/apacheds/branches/bigbang/protocol-dhcp/src/test/java/org/apache/directory/server/dhcp/AbstractDhcpTestCase.java
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/DhcpException.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/DhcpException.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/DhcpException.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/DhcpException.java Mon Feb 18 14:14:09 2008
@@ -50,4 +50,18 @@
{
super( description );
}
+
+
+ /**
+ * This constructor is used if a description of the event that caused the
+ * exception is required.
+ *
+ * @param description
+ * this is a description of the exception
+ * @param e
+ */
+ public DhcpException(String description, Exception e)
+ {
+ super( description, e );
+ }
}
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageDecoder.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageDecoder.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageDecoder.java Mon Feb 18 14:14:09 2008
@@ -6,27 +6,34 @@
* 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
- *
+ *
+ * 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.server.dhcp.io;
+import java.io.UnsupportedEncodingException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.nio.ByteBuffer;
+import java.util.Arrays;
import org.apache.directory.server.dhcp.DhcpException;
import org.apache.directory.server.dhcp.messages.DhcpMessage;
-import org.apache.directory.server.dhcp.messages.DhcpMessageModifier;
-import org.apache.directory.server.dhcp.messages.MessageType;
+import org.apache.directory.server.dhcp.messages.HardwareAddress;
+import org.apache.directory.server.dhcp.options.DhcpOption;
+import org.apache.directory.server.dhcp.options.OptionsField;
+import org.apache.directory.server.dhcp.options.dhcp.DhcpMessageType;
+import org.apache.directory.server.dhcp.options.dhcp.UnrecognizedOption;
/**
@@ -35,6 +42,7 @@
*/
public class DhcpMessageDecoder
{
+
/**
* Convert a byte buffer into a DhcpMessage.
*
@@ -44,57 +52,149 @@
*/
public DhcpMessage decode( ByteBuffer buffer ) throws DhcpException
{
- /**
- * TODO - need to figure out why the buffer needs to be rewound.
- */
- buffer.rewind();
+ byte op = buffer.get();
- DhcpMessageModifier modifier = new DhcpMessageModifier();
+ short htype = ( short ) ( buffer.get() & 0xff );
+ short hlen = ( short ) ( buffer.get() & 0xff );
+ short hops = ( short ) ( buffer.get() & 0xff );
+ int xid = buffer.getInt();
+ int secs = buffer.getShort() & 0xffff;
+ short flags = buffer.getShort();
- modifier.setMessageType( MessageType.DHCPDISCOVER );
+ InetAddress ciaddr = decodeAddress( buffer );
+ InetAddress yiaddr = decodeAddress( buffer );
+ InetAddress siaddr = decodeAddress( buffer );
+ InetAddress giaddr = decodeAddress( buffer );
- modifier.setOpCode( buffer.get() );
- modifier.setHardwareAddressType( buffer.get() );
+ byte[] chaddr = decodeBytes( buffer, 16 );
- short hardwareAddressLength = ( short ) ( buffer.get() & 0xff );
+ String sname = decodeString( buffer, 64 );
+ String file = decodeString( buffer, 128 );
- modifier.setHardwareAddressLength( ( byte ) hardwareAddressLength );
- modifier.setHardwareOptions( buffer.get() );
+ OptionsField options = decodeOptions( buffer );
- modifier.setTransactionId( buffer.getInt() );
- modifier.setSeconds( buffer.getShort() );
- modifier.setFlags( buffer.getShort() );
+ // message type option: may be null if option isn't set (BOOTP)
+ DhcpMessageType mto = ( DhcpMessageType ) options.get( DhcpMessageType.class );
- byte[] nextFourBytes = new byte[4];
+ return new DhcpMessage( null != mto ? mto.getType() : null, op, new HardwareAddress( htype, hlen, chaddr ),
+ hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, sname, file, options );
+ }
- buffer.get( nextFourBytes );
- modifier.setActualClientAddress( nextFourBytes );
- buffer.get( nextFourBytes );
- modifier.setAssignedClientAddress( nextFourBytes );
+ /**
+ * @param buffer
+ * @param len
+ * @return
+ */
+ private static byte[] decodeBytes( ByteBuffer buffer, int len )
+ {
+ byte[] bytes = new byte[len];
+ buffer.get( bytes );
+ return bytes;
+ }
- buffer.get( nextFourBytes );
- modifier.setNextServerAddress( nextFourBytes );
- buffer.get( nextFourBytes );
- modifier.setRelayAgentAddress( nextFourBytes );
+ /**
+ * @param buffer
+ * @return
+ */
+ private static String decodeString( ByteBuffer buffer, int len )
+ {
+ byte[] bytes = new byte[len];
+ buffer.get( bytes );
- byte[] clientHardwareAddress = new byte[16];
+ // find zero-terminator
+ int slen = 0;
+ while ( bytes[slen] != 0 )
+ slen++;
+
+ try
+ {
+ return new String( bytes, 0, slen, "ASCII" );
+ }
+ catch ( UnsupportedEncodingException e )
+ {
+ throw new RuntimeException( "No ASCII encoding", e );
+ }
+ }
- buffer.get( clientHardwareAddress );
- modifier.setClientHardwareAddress( clientHardwareAddress );
- byte[] serverHostname = new byte[64];
- buffer.get( serverHostname );
- modifier.setServerHostname( serverHostname );
+ /**
+ * Read a 4-byte inet address from the buffer.
+ *
+ * @param buffer
+ * @return
+ * @throws UnknownHostException
+ */
+ private static InetAddress decodeAddress( ByteBuffer buffer )
+ {
+ byte[] addr = new byte[4];
+ buffer.get( addr );
+ try
+ {
+ return InetAddress.getByAddress( addr );
+ }
+ catch ( UnknownHostException e )
+ {
+ // should not happen
+ return null;
+ }
+ }
- byte[] bootFileName = new byte[128];
- buffer.get( bootFileName );
- modifier.setBootFileName( bootFileName );
+ private static final byte[] VENDOR_MAGIC_COOKIE =
+ { ( byte ) 99, ( byte ) 130, ( byte ) 83, ( byte ) 99 };
- DhcpOptionsDecoder decoder = new DhcpOptionsDecoder();
- modifier.setOptions( decoder.decode( buffer ) );
- return modifier.getDhcpMessage();
+ public OptionsField decodeOptions( ByteBuffer message ) throws DhcpException
+ {
+ byte[] magicCookie = new byte[4];
+ message.get( magicCookie );
+
+ if ( !Arrays.equals( VENDOR_MAGIC_COOKIE, magicCookie ) )
+ {
+ throw new DhcpException( "Parse exception." );
+ }
+
+ byte code;
+ byte length;
+ byte value[];
+
+ OptionsField options = new OptionsField();
+
+ while ( true )
+ {
+ code = message.get();
+ if ( code == 0 ) // pad option
+ continue;
+
+ if ( code == -1 ) // end option
+ break;
+
+ length = message.get();
+ value = new byte[length];
+ message.get( value );
+
+ options.add( getOptionInstance( code, value ) );
+ }
+
+ return options;
+ }
+
+
+ private DhcpOption getOptionInstance( int tag, byte[] value ) throws DhcpException
+ {
+ try
+ {
+ Class c = DhcpOption.getClassByTag( tag );
+
+ DhcpOption o = null != c ? ( DhcpOption ) c.newInstance() : new UnrecognizedOption( ( byte ) tag );
+ o.setData( value );
+
+ return o;
+ }
+ catch ( Exception e )
+ {
+ throw new DhcpException( "Can't set option value: " + e.toString() );
+ }
}
}
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageEncoder.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageEncoder.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/io/DhcpMessageEncoder.java Mon Feb 18 14:14:09 2008
@@ -6,25 +6,31 @@
* 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
- *
+ *
+ * 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.server.dhcp.io;
+import java.io.UnsupportedEncodingException;
+import java.net.InetAddress;
import java.nio.ByteBuffer;
+import java.util.Iterator;
import org.apache.directory.server.dhcp.messages.DhcpMessage;
+import org.apache.directory.server.dhcp.messages.HardwareAddress;
+import org.apache.directory.server.dhcp.options.DhcpOption;
import org.apache.directory.server.dhcp.options.OptionsField;
+import org.apache.directory.server.dhcp.options.dhcp.DhcpMessageType;
/**
@@ -41,25 +47,125 @@
*/
public void encode( ByteBuffer byteBuffer, DhcpMessage message )
{
- byteBuffer.put( message.getOpCode() );
- byteBuffer.put( message.getHardwareAddressType() );
- byteBuffer.put( message.getHardwareAddressLength() );
- byteBuffer.put( message.getHardwareOptions() );
+ byteBuffer.put( message.getOp() );
+
+ HardwareAddress hardwareAddress = message.getHardwareAddress();
+
+ byteBuffer.put( ( byte ) ( null != hardwareAddress ? hardwareAddress.getType() : 0 ) );
+ byteBuffer.put( ( byte ) ( null != hardwareAddress ? hardwareAddress.getLength() : 0 ) );
+ byteBuffer.put( ( byte ) message.getHopCount() );
byteBuffer.putInt( message.getTransactionId() );
- byteBuffer.putShort( message.getSeconds() );
+ byteBuffer.putShort( ( short ) message.getSeconds() );
byteBuffer.putShort( message.getFlags() );
- byteBuffer.put( message.getActualClientAddress() );
- byteBuffer.put( message.getAssignedClientAddress() );
- byteBuffer.put( message.getNextServerAddress() );
- byteBuffer.put( message.getRelayAgentAddress() );
- byteBuffer.put( message.getClientHardwareAddress() );
- byteBuffer.put( message.getServerHostname() );
- byteBuffer.put( message.getBootFileName() );
+
+ writeAddress( byteBuffer, message.getCurrentClientAddress() );
+ writeAddress( byteBuffer, message.getAssignedClientAddress() );
+ writeAddress( byteBuffer, message.getNextServerAddress() );
+ writeAddress( byteBuffer, message.getRelayAgentAddress() );
+
+ writeBytes( byteBuffer, ( null != hardwareAddress ? hardwareAddress.getAddress() : new byte[]
+ {} ), 16 );
+
+ writeString( byteBuffer, message.getServerHostname(), 64 );
+ writeString( byteBuffer, message.getBootFileName(), 128 );
OptionsField options = message.getOptions();
- DhcpOptionsEncoder optionsEncoder = new DhcpOptionsEncoder();
+ // update message type option (if set)
+ if ( null != message.getMessageType() )
+ options.add( new DhcpMessageType( message.getMessageType() ) );
+
+ encodeOptions( options, byteBuffer );
+ }
+
+
+ /**
+ * Write a zero-terminated string to a field of len bytes.
+ *
+ * @param byteBuffer
+ * @param serverHostname
+ * @param i
+ */
+ private void writeString( ByteBuffer byteBuffer, String string, int len )
+ {
+ if ( null == string )
+ string = "";
+
+ try
+ {
+ byte sbytes[] = string.getBytes( "ASCII" );
+
+ // writeBytes will automatically zero-pad and thus terminate the
+ // string.
+ writeBytes( byteBuffer, sbytes, len );
+ }
+ catch ( UnsupportedEncodingException e )
+ {
+ // should not happen
+ throw new RuntimeException( "No ASCII encoding", e );
+ }
+ }
+
+
+ /**
+ * Write an InetAddress to the byte buffer.
+ *
+ * @param byteBuffer
+ * @param currentClientAddress
+ */
+ private void writeAddress( ByteBuffer byteBuffer, InetAddress currentClientAddress )
+ {
+ if ( null == currentClientAddress )
+ {
+ byte emptyAddress[] =
+ { 0, 0, 0, 0 };
+ byteBuffer.put( emptyAddress );
+ }
+ else
+ {
+ byte[] addressBytes = currentClientAddress.getAddress();
+ byteBuffer.put( addressBytes );
+ }
+ }
+
+
+ /**
+ * Write an array of bytes to the buffer. Write exactly len bytes,
+ * truncating if more than len, padding if less than len bytes are
+ * available.
+ *
+ * @param byteBuffer
+ * @param currentClientAddress
+ */
+ private void writeBytes( ByteBuffer byteBuffer, byte bytes[], int len )
+ {
+ if ( null == bytes )
+ bytes = new byte[]
+ {};
+
+ byteBuffer.put( bytes, 0, Math.min(len, bytes.length) );
+
+ // pad as necessary
+ int remain = len - bytes.length;
+ while ( remain-- > 0 )
+ byteBuffer.put( ( byte ) 0 );
+ }
+
+ private static final byte[] VENDOR_MAGIC_COOKIE =
+ { ( byte ) 99, ( byte ) 130, ( byte ) 83, ( byte ) 99 };
+
+
+ public void encodeOptions( OptionsField options, ByteBuffer message )
+ {
+ message.put( VENDOR_MAGIC_COOKIE );
+
+ for ( Iterator i = options.iterator(); i.hasNext(); )
+ {
+ DhcpOption option = ( DhcpOption ) i.next();
+ option.writeTo( message );
+ }
- optionsEncoder.encode( options, byteBuffer );
+ // add end option
+ message.put( ( byte ) 0xff );
}
}
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/DhcpMessage.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/DhcpMessage.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/DhcpMessage.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/DhcpMessage.java Mon Feb 18 14:14:09 2008
@@ -6,276 +6,295 @@
* 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
- *
+ *
+ * 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.server.dhcp.messages;
+import java.net.InetAddress;
import org.apache.directory.server.dhcp.options.OptionsField;
-
/**
+ * A DHCP (RFC 2131) message. Field descriptions contain the oroginal RFC field
+ * names in brackets.
+ *
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
-public class DhcpMessage
-{
- private MessageType messageType;
-
- private byte opCode;
- private byte hardwareAddressType;
- private byte hardwareAddressLength;
- private byte hardwareOptions;
- private int transactionId;
- private short seconds;
- private short flags;
- private byte actualClientAddress[] = new byte[4];
- private byte assignedClientAddress[] = new byte[4];
- private byte nextServerAddress[] = new byte[4];
- private byte relayAgentAddress[] = new byte[4];
- private byte clientHardwareAddress[] = new byte[16];
- private byte serverHostname[] = new byte[64];
- private byte bootFileName[] = new byte[128];
-
- private OptionsField options = new OptionsField();
-
-
- /**
- * Creates a new instance of DhcpMessage.
- *
- * @param messageType
- * @param opCode
- * @param hardwareAddressType
- * @param hardwareAddressLength
- * @param hardwareOptions
- * @param transactionId
- * @param seconds
- * @param flags
- * @param actualClientAddress
- * @param assignedClientAddress
- * @param nextServerAddress
- * @param relayAgentAddress
- * @param clientHardwareAddress
- * @param serverHostname
- * @param bootFileName
- * @param options
- */
- public DhcpMessage( MessageType messageType, byte opCode, byte hardwareAddressType, byte hardwareAddressLength,
- byte hardwareOptions, int transactionId, short seconds, short flags, byte[] actualClientAddress,
- byte[] assignedClientAddress, byte[] nextServerAddress, byte[] relayAgentAddress, byte[] clientHardwareAddress,
- byte[] serverHostname, byte[] bootFileName, OptionsField options )
- {
- this.messageType = messageType;
- this.opCode = opCode;
- this.hardwareAddressType = hardwareAddressType;
- this.hardwareAddressLength = hardwareAddressLength;
- this.hardwareOptions = hardwareOptions;
- this.transactionId = transactionId;
- this.seconds = seconds;
- this.flags = flags;
- this.actualClientAddress = actualClientAddress;
- this.assignedClientAddress = assignedClientAddress;
- this.nextServerAddress = nextServerAddress;
- this.relayAgentAddress = relayAgentAddress;
- this.clientHardwareAddress = clientHardwareAddress;
- this.serverHostname = serverHostname;
- this.bootFileName = bootFileName;
- this.options = options;
- }
-
-
- /**
- * Message type.
- *
- * @return The {@link MessageType}.
- */
- public MessageType getMessageType()
- {
- return messageType;
- }
-
-
- /**
- * Message op code / message type.
- * 1 = BOOTREQUEST, 2 = BOOTREPLY
- *
- * @return The message op code (type).
- */
- public byte getOpCode()
- {
- return opCode;
- }
-
-
- /**
- * Hardware address type, see ARP section in
- * "Assigned Numbers" RFC; e.g., '1' = 10mb ethernet.
- *
- * @return The hardware address type.
- */
- public byte getHardwareAddressType()
- {
- return hardwareAddressType;
- }
-
-
- /**
- * Hardware address length (e.g. '6' for 10mb ethernet).
- *
- * @return The hardware address length.
- */
- public byte getHardwareAddressLength()
- {
- return hardwareAddressLength;
- }
-
-
- /**
- * Client sets to zero, optionally used by relay agents
- * when booting via a relay agent.
- *
- * @return The hardware options.
- */
- public byte getHardwareOptions()
- {
- return hardwareOptions;
- }
-
-
- /**
- * Transaction ID, a random number chosen by the client,
- * used by the client and server to associate messages
- * and responses between a client and a server.
- *
- * @return The transaction ID.
- */
- public int getTransactionId()
- {
- return transactionId;
- }
-
-
- /**
- * Filled in by client, seconds elapsed since client
- * began address acquisition or renewal process.
- *
- * @return The seconds.
- */
- public short getSeconds()
- {
- return seconds;
- }
-
-
- /**
- * Flags.
- *
- * @return The flags.
- */
- public short getFlags()
- {
- return flags;
- }
-
-
- /**
- * Client IP address; only filled in if client is in BOUND,
- * RENEW or REBINDING state and can respond to ARP requests.
- *
- * @return The actual client addresses.
- */
- public byte[] getActualClientAddress()
- {
- return actualClientAddress;
- }
-
-
- /**
- * Get 'your' (client) IP address.
- *
- * @return The assigned client addresses.
- */
- public byte[] getAssignedClientAddress()
- {
- return assignedClientAddress;
- }
-
-
- /**
- * IP address of next server to use in bootstrap;
- * returned in DHCPOFFER, DHCPACK by server.
- *
- * @return The next server address.
- */
- public byte[] getNextServerAddress()
- {
- return nextServerAddress;
- }
-
-
- /**
- * Relay agent IP address, used in booting via a relay agent.
- *
- * @return The relay agent address.
- */
- public byte[] getRelayAgentAddress()
- {
- return relayAgentAddress;
- }
-
-
- /**
- * Client hardware address.
- *
- * @return The client hardware address.
- */
- public byte[] getClientHardwareAddress()
- {
- return clientHardwareAddress;
- }
-
-
- /**
- * Optional server host name, null terminated string.
- *
- * @return The server hostname.
- */
- public byte[] getServerHostname()
- {
- return serverHostname;
- }
-
-
- /**
- * Boot file name, null terminated string; "generic" name or null
- * in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
- *
- * @return The boot file name.
- */
- public byte[] getBootFileName()
- {
- return bootFileName;
- }
-
-
- /**
- * Optional parameters field. See the options
- * documents for a list of defined options.
- *
- * @return The options.
- */
- public OptionsField getOptions()
- {
- return options;
- }
+public class DhcpMessage {
+ /**
+ * Flag value: request broadcast answer.
+ *
+ * @see #flags;
+ */
+ public static final int FLAG_BROADCAST = 0x01;
+
+ /**
+ * [yiaddr] 'your' (client) IP address.
+ */
+ private InetAddress assignedClientAddress;
+
+ /**
+ * [file] Boot file name, null terminated string; "generic" name or null in
+ * DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
+ */
+ private String bootFileName;
+
+ /**
+ * [ciaddr] Current client IP address; only filled in if client is in BOUND,
+ * RENEW or REBINDING state and can respond to ARP requests.
+ */
+ private InetAddress currentClientAddress;
+
+ /**
+ * [flags] Flags. (LSB is broadcast flag)
+ */
+ private short flags;
+
+ /**
+ * [hops] Client sets to zero, optionally used by relay agents when booting
+ * via a relay agent.
+ */
+ private short hopCount;
+
+ /**
+ * [op] Message op code. 1 = BOOTREQUEST, 2 = BOOTREPLY, ...
+ */
+ private byte op;
+
+ /**
+ * Operation constant: boot request (client to server).
+ *
+ * @see #op
+ */
+ public static final byte OP_BOOTREQUEST = 1;
+
+ /**
+ * Operation constant: boot reply (server to client).
+ *
+ * @see #op
+ */
+ public static final byte OP_BOOTREPLY = 2;
+
+ /**
+ * [siaddr] IP address of next server to use in bootstrap; returned in
+ * DHCPOFFER, DHCPACK by server.
+ */
+ private InetAddress nextServerAddress;
+
+ /**
+ * [options] Optional parameters field. See the options documents for a list
+ * of defined options.
+ */
+ private OptionsField options = new OptionsField();
+
+ /**
+ * [giaddr] Relay agent IP address, used in booting via a relay agent.
+ */
+ private InetAddress relayAgentAddress;
+
+ /**
+ * [secs] Filled in by client, seconds elapsed since client began address
+ * acquisition or renewal process.
+ */
+ private int seconds;
+
+ /**
+ * [sname] Optional server host name, null terminated string.
+ */
+ private String serverHostname;
+
+ /**
+ * [xid] Transaction ID, a random number chosen by the client, used by the
+ * client and server to associate messages and responses between a client and
+ * a server.
+ */
+ private int transactionId;
+
+ /**
+ * The DHCP message type option.
+ */
+ private MessageType messageType;
+
+ private HardwareAddress hardwareAddress;
+
+ /**
+ * Create a default dhcp message.
+ */
+ public DhcpMessage() {
+
+ }
+
+ /**
+ * Create a DHCP message based on the supplied values.
+ *
+ * @param messageType
+ * @param op
+ * @param hardwareAddressType
+ * @param hardwareAddressLength
+ * @param hops
+ * @param transactionId
+ * @param seconds
+ * @param flags
+ * @param currentClientAddress
+ * @param assignedClientAddress
+ * @param nextServerAddress
+ * @param relayAgentAddress
+ * @param clientHardwareAddress
+ * @param serverHostname
+ * @param bootFileName
+ * @param options
+ */
+ public DhcpMessage(MessageType messageType, byte op,
+ HardwareAddress hardwareAddress, short hops, int transactionId,
+ int seconds, short flags, InetAddress currentClientAddress,
+ InetAddress assignedClientAddress, InetAddress nextServerAddress,
+ InetAddress relayAgentAddress, String serverHostname,
+ String bootFileName, OptionsField options) {
+ this.messageType = messageType;
+ this.op = op;
+ this.hardwareAddress = hardwareAddress;
+ this.hopCount = hops;
+ this.transactionId = transactionId;
+ this.seconds = seconds;
+ this.flags = flags;
+ this.currentClientAddress = currentClientAddress;
+ this.assignedClientAddress = assignedClientAddress;
+ this.nextServerAddress = nextServerAddress;
+ this.relayAgentAddress = relayAgentAddress;
+ this.serverHostname = serverHostname;
+ this.bootFileName = bootFileName;
+ this.options = options;
+ }
+
+ public InetAddress getAssignedClientAddress() {
+ return assignedClientAddress;
+ }
+
+ public String getBootFileName() {
+ return bootFileName;
+ }
+
+ public InetAddress getCurrentClientAddress() {
+ return currentClientAddress;
+ }
+
+ public short getFlags() {
+ return flags;
+ }
+
+ public short getHopCount() {
+ return hopCount;
+ }
+
+ public MessageType getMessageType() {
+ return messageType;
+ }
+
+ public InetAddress getNextServerAddress() {
+ return nextServerAddress;
+ }
+
+ public OptionsField getOptions() {
+ return options;
+ }
+
+ public InetAddress getRelayAgentAddress() {
+ return relayAgentAddress;
+ }
+
+ public int getSeconds() {
+ return seconds;
+ }
+
+ public String getServerHostname() {
+ return serverHostname;
+ }
+
+ public int getTransactionId() {
+ return transactionId;
+ }
+
+ public void setAssignedClientAddress(InetAddress assignedClientAddress) {
+ this.assignedClientAddress = assignedClientAddress;
+ }
+
+ public void setBootFileName(String bootFileName) {
+ this.bootFileName = bootFileName;
+ }
+
+ public void setCurrentClientAddress(InetAddress currentClientAddress) {
+ this.currentClientAddress = currentClientAddress;
+ }
+
+ public void setFlags(short flags) {
+ this.flags = flags;
+ }
+
+ public void setHopCount(short hopCount) {
+ this.hopCount = hopCount;
+ }
+
+ public void setMessageType(MessageType messageType) {
+ this.messageType = messageType;
+ }
+
+ public void setNextServerAddress(InetAddress nextServerAddress) {
+ this.nextServerAddress = nextServerAddress;
+ }
+
+ public void setOptions(OptionsField options) {
+ this.options = options;
+ }
+
+ public void setRelayAgentAddress(InetAddress relayAgentAddress) {
+ this.relayAgentAddress = relayAgentAddress;
+ }
+
+ public void setSeconds(int seconds) {
+ this.seconds = seconds;
+ }
+
+ public void setServerHostname(String serverHostname) {
+ this.serverHostname = serverHostname;
+ }
+
+ public void setTransactionId(int transactionId) {
+ this.transactionId = transactionId;
+ }
+
+ public byte getOp() {
+ return op;
+ }
+
+ public void setOp(byte op) {
+ this.op = op;
+ }
+
+ public HardwareAddress getHardwareAddress() {
+ return hardwareAddress;
+ }
+
+ public void setHardwareAddress(HardwareAddress hardwareAddress) {
+ this.hardwareAddress = hardwareAddress;
+ }
+
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(messageType).append(": hwAddress=").append(hardwareAddress)
+ .append(", tx=").append(transactionId).append(", options=").append(
+ options);
+
+ return sb.toString();
+ }
}
Added: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/HardwareAddress.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/HardwareAddress.java?rev=628890&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/HardwareAddress.java (added)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/HardwareAddress.java Mon Feb 18 14:14:09 2008
@@ -0,0 +1,202 @@
+/*
+ * 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.server.dhcp.messages;
+
+
+import java.text.ParseException;
+import java.util.Arrays;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+
+/**
+ * A representation of a DHCP hardware address.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 551805 $, $Date: 2007-06-29 00:57:04 -0500 (Fr, 29 Jun 2007) $
+ */
+public final class HardwareAddress
+{
+ /**
+ * [htype] Hardware address type, see ARP section in "Assigned Numbers" RFC;
+ * e.g., '1' = 10mb ethernet.
+ */
+ private final short type;
+
+ /**
+ * [hlen] Hardware address length (e.g. '6' for 10mb ethernet).
+ */
+ private final short length;
+
+ /**
+ * [chaddr] Client hardware address.
+ */
+ private final byte[] address;
+
+
+ /**
+ * @param htype
+ * @param hlen
+ * @param chaddr
+ */
+ public HardwareAddress(short type, short length, byte[] address)
+ {
+ this.type = type;
+ this.length = length;
+ this.address = address;
+ }
+
+
+ public byte[] getAddress()
+ {
+ return address;
+ }
+
+
+ public short getLength()
+ {
+ return length;
+ }
+
+
+ public short getType()
+ {
+ return type;
+ }
+
+
+ /*
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode()
+ {
+ int hashCode = 98643532 ^ type ^ length;
+ for ( int i = 0; i < length; i++ )
+ hashCode ^= address[i];
+
+ return hashCode;
+ }
+
+
+ /*
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals( Object obj )
+ {
+ if ( null == obj || !( obj.getClass().equals( HardwareAddress.class ) ) )
+ return false;
+
+ HardwareAddress hw = ( HardwareAddress ) obj;
+
+ return length == hw.length && type == hw.type && Arrays.equals( address, hw.address );
+ }
+
+
+ /**
+ * Create the string representation of the hardware address native to the
+ * corresponding address type. This method currently supports only type
+ * 1==ethernet with the representation <code>a1:a2:a3:a4:a5:a6</code>.<br>
+ * For all other types, this method falls back to the representation created
+ * by toString().
+ *
+ * @see java.lang.Object#toString()
+ */
+ public String getNativeRepresentation()
+ {
+ StringBuffer sb = new StringBuffer();
+ switch ( type )
+ {
+ case 1:
+ for ( int i = 0; i < length; i++ )
+ {
+ if ( i > 0 )
+ sb.append( ":" );
+ String hex = Integer.toHexString( address[i] & 0xff );
+ if ( hex.length() < 2 )
+ sb.append( '0' );
+ sb.append( hex );
+ }
+ break;
+ default:
+ sb.append( toString() );
+ }
+
+ return sb.toString();
+ }
+
+
+ /**
+ * Create a string representation of the hardware address. The string
+ * representation is in the format<br>
+ * <code>t/a1:a2:a3...</code><br>
+ * Where <code>t</code> represents the address type (decimal) and
+ * <code>a<sub>n</sub></code> represent the address bytes (hexadecimal).
+ *
+ * @see java.lang.Object#toString()
+ */
+ public String toString()
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append( type );
+ sb.append( "/" );
+ for ( int i = 0; i < length; i++ )
+ {
+ if ( i > 0 )
+ sb.append( ":" );
+ String hex = Integer.toHexString( address[i] & 0xff );
+ if ( hex.length() < 2 )
+ sb.append( '0' );
+ sb.append( hex );
+ }
+
+ return sb.toString();
+ }
+
+ private static final Pattern PARSE_PATTERN = Pattern
+ .compile( "(\\d+)\\s+(?:(\\p{XDigit}{1,2}):)*(\\p{XDigit}{1,2})?" );
+
+
+ /**
+ * Parses a string representation of a hardware address according to the
+ * specification given in {@link #toString()}.
+ *
+ * @param s
+ * @return
+ * @throws ParseException
+ */
+ public static HardwareAddress valueOf( String s )
+ {
+ if ( null == s || s.length() == 0 )
+ return null;
+
+ Matcher m = PARSE_PATTERN.matcher( s );
+ if ( !m.matches() )
+ throw new IllegalArgumentException( "Can't parse string representation: " + s );
+
+ int type = Integer.parseInt( m.group( 1 ) );
+ int len = m.groupCount() - 1;
+
+ byte addr[] = new byte[len];
+ for ( int i = 0; i < addr.length; i++ )
+ addr[i] = ( byte ) Integer.parseInt( m.group( i + 2 ), 16 );
+
+ return new HardwareAddress( ( short ) type, ( short ) len, addr );
+ }
+}
Propchange: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/HardwareAddress.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java Mon Feb 18 14:14:09 2008
@@ -6,16 +6,16 @@
* 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
- *
+ *
+ * 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.server.dhcp.messages;
@@ -32,117 +32,95 @@
*/
public final class MessageType implements Comparable
{
- /**
- * Constant for the "Null" message type.
- */
- public static final MessageType NULL = new MessageType( 0, "Null" );
+ // FIXME: does this class make a lot of sense in absence of real (1.5)
+ // enums?
+ // The DOCPDISCOVER et. al. constants can't be used conveniently in
+ // switches,
+ // therefore the byte constants fpr the opcodes are duplicated here.
+ public static final byte CODE_DHCPINFORM = 8;
- /**
- * Constant for the "DHCP Discover" message type.
- */
- public static final MessageType DHCPDISCOVER = new MessageType( 1, "DHCP Discover" );
+ public static final byte CODE_DHCPRELEASE = 7;
- /**
- * Constant for the "DHCP Offer" message type.
- */
- public static final MessageType DHCPOFFER = new MessageType( 2, "DHCP Offer" );
+ public static final byte CODE_DHCPNAK = 6;
- /**
- * Constant for the "DHCP Request" message type.
- */
- public static final MessageType DHCPREQUEST = new MessageType( 3, "DHCP Request" );
+ public static final byte CODE_DHCPACK = 5;
- /**
- * Constant for the "DHCP Decline" message type.
- */
- public static final MessageType DHCPDECLINE = new MessageType( 4, "DHCP Decline" );
+ public static final byte CODE_DHCPDECLINE = 4;
- /**
- * Constant for the "DHCP Acknowledge" message type.
- */
- public static final MessageType DHCPACK = new MessageType( 5, "DHCP Acknowledge" );
+ public static final byte CODE_DHCPREQUEST = 3;
- /**
- * Constant for the "DHCP Not Acknowledge" message type.
- */
- public static final MessageType DHCPNAK = new MessageType( 6, "DHCP Not Acknowledge" );
+ public static final byte CODE_DHCPOFFER = 2;
- /**
- * Constant for the "DHCP Release" message type.
- */
- public static final MessageType DHCPRELEASE = new MessageType( 7, "DHCP Release" );
+ public static final byte CODE_DHCPDISCOVER = 1;
/**
- * Constant for the "DHCP Inform" message type.
+ * Enumeration elements are constructed once upon class loading. Order of
+ * appearance here determines the order of compareTo.
*/
- public static final MessageType DHCPINFORM = new MessageType( 8, "DHCP Inform" );
+ public static final MessageType DHCPDISCOVER = new MessageType( CODE_DHCPDISCOVER, "DHCP Discover" );
- /**
- * Array for building a List of VALUES.
- */
- private static final MessageType[] values =
- { NULL, DHCPDISCOVER, DHCPOFFER, DHCPREQUEST, DHCPDECLINE, DHCPACK, DHCPNAK, DHCPRELEASE, DHCPINFORM };
+ public static final MessageType DHCPOFFER = new MessageType( CODE_DHCPOFFER, "DHCP Offer" );
- /**
- * A list of all the message type constants.
- */
- public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
+ public static final MessageType DHCPREQUEST = new MessageType( CODE_DHCPREQUEST, "DHCP Request" );
- /**
- * The name of the message type.
- */
- private final String name;
+ public static final MessageType DHCPDECLINE = new MessageType( CODE_DHCPDECLINE, "DHCP Decline" );
- /**
- * The value/code for the message type.
- */
- private final int ordinal;
+ public static final MessageType DHCPACK = new MessageType( CODE_DHCPACK, "DHCP Acknowledge" );
+ public static final MessageType DHCPNAK = new MessageType( CODE_DHCPNAK, "DHCP Not Acknowledge" );
- /**
- * Private constructor prevents construction outside of this class.
- */
- private MessageType( int ordinal, String name )
+ public static final MessageType DHCPRELEASE = new MessageType( CODE_DHCPRELEASE, "DHCP Release" );
+
+ public static final MessageType DHCPINFORM = new MessageType( CODE_DHCPINFORM, "DHCP Inform" );
+
+
+ public String toString()
{
- this.ordinal = ordinal;
- this.name = name;
+ return name;
}
- /**
- * Returns the message type when specified by its ordinal.
- *
- * @param type
- * @return The message type.
- */
- public static MessageType getTypeByOrdinal( int type )
+ public int compareTo( Object that )
+ {
+ return ordinal - ( ( MessageType ) that ).ordinal;
+ }
+
+
+ public static MessageType getTypeByCode( byte type )
{
for ( int ii = 0; ii < values.length; ii++ )
if ( values[ii].ordinal == type )
return values[ii];
- return NULL;
+ return new MessageType( type, "Unrecognized" );
}
- /**
- * Returns the number associated with this message type.
- *
- * @return The message type ordinal.
- */
- public int getOrdinal()
+ public byte getCode()
{
return ordinal;
}
+ // / PRIVATE /////
+ private final String name;
- public int compareTo( Object that )
- {
- return ordinal - ( ( MessageType ) that ).ordinal;
- }
+ private final byte ordinal;
- public String toString()
+ /**
+ * Private constructor prevents construction outside of this class.
+ */
+ private MessageType(byte ordinal, String name)
{
- return name;
+ this.ordinal = ordinal;
+ this.name = name;
}
+
+ /**
+ * These two lines are all that's necessary to export a List of VALUES.
+ */
+ private static final MessageType[] values =
+ { DHCPDISCOVER, DHCPOFFER, DHCPREQUEST, DHCPDECLINE, DHCPACK, DHCPNAK, DHCPRELEASE, DHCPINFORM };
+
+ // VALUES needs to be located here, otherwise illegal forward reference
+ public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
}
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressListOption.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressListOption.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressListOption.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressListOption.java Mon Feb 18 14:14:09 2008
@@ -6,56 +6,33 @@
* 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
- *
+ *
+ * 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.server.dhcp.options;
-import java.nio.ByteBuffer;
-
-
/**
* The Dynamic Host Configuration Protocol (DHCP) provides a framework
* for passing configuration information to hosts on a TCP/IP network.
* Configuration parameters and other control information are carried in
* tagged data items that are stored in the 'options' field of the DHCP
* message. The data items themselves are also called "options."
- *
+ *
* This abstract base class is for options that carry lists of IP addresses.
- *
+ *
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public abstract class AddressListOption extends DhcpOption
{
- private byte[] value;
-
-
- /**
- * Creates a new instance of AddressListOption.
- *
- * @param tag
- * @param value
- */
- public AddressListOption( int tag, byte[] value )
- {
- super( tag, value.length );
- this.value = value;
- }
-
-
- protected void valueToByteBuffer( ByteBuffer out )
- {
- out.put( value );
- }
}
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressOption.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressOption.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressOption.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/AddressOption.java Mon Feb 18 14:14:09 2008
@@ -6,22 +6,23 @@
* 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
- *
+ *
+ * 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.server.dhcp.options;
-import java.nio.ByteBuffer;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
/**
@@ -30,34 +31,50 @@
* Configuration parameters and other control information are carried in
* tagged data items that are stored in the 'options' field of the DHCP
* message. The data items themselves are also called "options."
- *
+ *
* This abstract base class is for options that carry a single IP address.
- *
+ *
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public abstract class AddressOption extends DhcpOption
{
- private static final int length = 4;
+ private InetAddress address;
- private byte[] value;
+
+ /*
+ * @see org.apache.directory.server.dhcp.options.DhcpOption#getData()
+ */
+ public byte[] getData()
+ {
+ return address.getAddress();
+ }
- /**
- * Creates a new instance of AddressOption.
- *
- * @param tag
- * @param value
+ /*
+ * @see org.apache.directory.server.dhcp.options.DhcpOption#setData(byte[])
*/
- public AddressOption( int tag, byte[] value )
+ public void setData( byte[] data )
+ {
+ try
+ {
+ address = InetAddress.getByAddress( data );
+ }
+ catch ( UnknownHostException e )
+ {
+ throw new IllegalArgumentException( "illegal address", e );
+ }
+ }
+
+
+ public InetAddress getAddress()
{
- super( tag, length );
- this.value = value;
+ return address;
}
- protected void valueToByteBuffer( ByteBuffer out )
+ public void setAddress( InetAddress address )
{
- out.put( value );
+ this.address = address;
}
}
Added: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/ByteOption.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/ByteOption.java?rev=628890&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/ByteOption.java (added)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/ByteOption.java Mon Feb 18 14:14:09 2008
@@ -0,0 +1,64 @@
+/*
+ * 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.server.dhcp.options;
+
+/**
+ * The Dynamic Host Configuration Protocol (DHCP) provides a framework for
+ * passing configuration information to hosts on a TCP/IP network. Configuration
+ * parameters and other control information are carried in tagged data items
+ * that are stored in the 'options' field of the DHCP message. The data items
+ * themselves are also called "options."
+ *
+ * This abstract base class is for options that carry an unsigned short value
+ * (16 bit).
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 551805 $, $Date: 2007-06-29 00:57:04 -0500 (Fr, 29 Jun 2007) $
+ *
+ */
+public abstract class ByteOption extends DhcpOption {
+ /**
+ * The byte value (represented as a short because of the unsignedness).
+ */
+ private short byteValue;
+
+ /*
+ * @see org.apache.directory.server.dhcp.options.DhcpOption#setData(byte[])
+ */
+ public void setData(byte[] data) {
+ byteValue = (short) (data[0] & 0xff);
+ }
+
+ /*
+ * @see org.apache.directory.server.dhcp.options.DhcpOption#getData()
+ */
+ public byte[] getData() {
+ return new byte[]{(byte) (byteValue & 0xff)};
+ }
+
+ public short getByteValue() {
+ return byteValue;
+ }
+
+ public void setShortValue(short shortValue) {
+ this.byteValue = shortValue;
+ }
+}
Propchange: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/ByteOption.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/DhcpOption.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/DhcpOption.java?rev=628890&r1=628889&r2=628890&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/DhcpOption.java (original)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/DhcpOption.java Mon Feb 18 14:14:09 2008
@@ -1,27 +1,99 @@
/*
- * 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.
- *
+ * Copyright 2005 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.directory.server.dhcp.options;
import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.directory.server.dhcp.options.dhcp.BootfileName;
+import org.apache.directory.server.dhcp.options.dhcp.ClientIdentifier;
+import org.apache.directory.server.dhcp.options.dhcp.DhcpMessageType;
+import org.apache.directory.server.dhcp.options.dhcp.IpAddressLeaseTime;
+import org.apache.directory.server.dhcp.options.dhcp.MaximumDhcpMessageSize;
+import org.apache.directory.server.dhcp.options.dhcp.OptionOverload;
+import org.apache.directory.server.dhcp.options.dhcp.ParameterRequestList;
+import org.apache.directory.server.dhcp.options.dhcp.RebindingTimeValue;
+import org.apache.directory.server.dhcp.options.dhcp.RenewalTimeValue;
+import org.apache.directory.server.dhcp.options.dhcp.RequestedIpAddress;
+import org.apache.directory.server.dhcp.options.dhcp.ServerIdentifier;
+import org.apache.directory.server.dhcp.options.dhcp.TftpServerName;
+import org.apache.directory.server.dhcp.options.dhcp.UnrecognizedOption;
+import org.apache.directory.server.dhcp.options.dhcp.VendorClassIdentifier;
+import org.apache.directory.server.dhcp.options.misc.DefaultFingerServers;
+import org.apache.directory.server.dhcp.options.misc.DefaultIrcServers;
+import org.apache.directory.server.dhcp.options.misc.DefaultWwwServers;
+import org.apache.directory.server.dhcp.options.misc.MobileIpHomeAgents;
+import org.apache.directory.server.dhcp.options.misc.NbddServers;
+import org.apache.directory.server.dhcp.options.misc.NetbiosNameServers;
+import org.apache.directory.server.dhcp.options.misc.NetbiosNodeType;
+import org.apache.directory.server.dhcp.options.misc.NetbiosScope;
+import org.apache.directory.server.dhcp.options.misc.NisDomain;
+import org.apache.directory.server.dhcp.options.misc.NisPlusDomain;
+import org.apache.directory.server.dhcp.options.misc.NisPlusServers;
+import org.apache.directory.server.dhcp.options.misc.NisServers;
+import org.apache.directory.server.dhcp.options.misc.NntpServers;
+import org.apache.directory.server.dhcp.options.misc.NtpServers;
+import org.apache.directory.server.dhcp.options.misc.Pop3Servers;
+import org.apache.directory.server.dhcp.options.misc.SmtpServers;
+import org.apache.directory.server.dhcp.options.misc.StdaServers;
+import org.apache.directory.server.dhcp.options.misc.StreetTalkServers;
+import org.apache.directory.server.dhcp.options.misc.VendorSpecificInformation;
+import org.apache.directory.server.dhcp.options.misc.XWindowDisplayManagers;
+import org.apache.directory.server.dhcp.options.misc.XWindowFontServers;
+import org.apache.directory.server.dhcp.options.perhost.DefaultIpTimeToLive;
+import org.apache.directory.server.dhcp.options.perhost.IpForwarding;
+import org.apache.directory.server.dhcp.options.perhost.MaximumDatagramSize;
+import org.apache.directory.server.dhcp.options.perhost.NonLocalSourceRouting;
+import org.apache.directory.server.dhcp.options.perhost.PathMtuAgingTimeout;
+import org.apache.directory.server.dhcp.options.perhost.PathMtuPlateauTable;
+import org.apache.directory.server.dhcp.options.perhost.PolicyFilter;
+import org.apache.directory.server.dhcp.options.perinterface.AllSubnetsAreLocal;
+import org.apache.directory.server.dhcp.options.perinterface.BroadcastAddress;
+import org.apache.directory.server.dhcp.options.perinterface.InterfaceMtu;
+import org.apache.directory.server.dhcp.options.perinterface.MaskSupplier;
+import org.apache.directory.server.dhcp.options.perinterface.PerformMaskDiscovery;
+import org.apache.directory.server.dhcp.options.perinterface.PerformRouterDiscovery;
+import org.apache.directory.server.dhcp.options.perinterface.RouterSolicitationAddress;
+import org.apache.directory.server.dhcp.options.perinterface.StaticRoute;
+import org.apache.directory.server.dhcp.options.tcp.TcpDefaultTimeToLive;
+import org.apache.directory.server.dhcp.options.tcp.TcpKeepaliveGarbage;
+import org.apache.directory.server.dhcp.options.tcp.TcpKeepaliveInterval;
+import org.apache.directory.server.dhcp.options.vendor.BootFileSize;
+import org.apache.directory.server.dhcp.options.vendor.CookieServers;
+import org.apache.directory.server.dhcp.options.vendor.DomainName;
+import org.apache.directory.server.dhcp.options.vendor.DomainNameServers;
+import org.apache.directory.server.dhcp.options.vendor.ExtensionsPath;
+import org.apache.directory.server.dhcp.options.vendor.HostName;
+import org.apache.directory.server.dhcp.options.vendor.ImpressServers;
+import org.apache.directory.server.dhcp.options.vendor.LogServers;
+import org.apache.directory.server.dhcp.options.vendor.LprServers;
+import org.apache.directory.server.dhcp.options.vendor.MeritDumpFile;
+import org.apache.directory.server.dhcp.options.vendor.NameServers;
+import org.apache.directory.server.dhcp.options.vendor.ResourceLocationServers;
+import org.apache.directory.server.dhcp.options.vendor.RootPath;
+import org.apache.directory.server.dhcp.options.vendor.Routers;
+import org.apache.directory.server.dhcp.options.vendor.SubnetMask;
+import org.apache.directory.server.dhcp.options.vendor.SwapServer;
+import org.apache.directory.server.dhcp.options.vendor.TimeOffset;
+import org.apache.directory.server.dhcp.options.vendor.TimeServers;
/**
@@ -36,36 +108,134 @@
*/
public abstract class DhcpOption
{
- private int tag;
- private int length;
+ /**
+ * An array of concrete implementations of DhcpOption.
+ */
+ private static Class OPTION_CLASSES[] =
+ { BootfileName.class, ClientIdentifier.class, DhcpMessageType.class, IpAddressLeaseTime.class,
+ MaximumDhcpMessageSize.class, org.apache.directory.server.dhcp.options.dhcp.Message.class,
+ OptionOverload.class, ParameterRequestList.class, RebindingTimeValue.class, RenewalTimeValue.class,
+ RequestedIpAddress.class, ServerIdentifier.class, TftpServerName.class, VendorClassIdentifier.class,
+ ClientIdentifier.class, DhcpMessageType.class, IpAddressLeaseTime.class, MaximumDhcpMessageSize.class,
+ OptionOverload.class, ParameterRequestList.class, RebindingTimeValue.class, RenewalTimeValue.class,
+ RequestedIpAddress.class, ServerIdentifier.class, TftpServerName.class, UnrecognizedOption.class,
+ VendorClassIdentifier.class, DefaultFingerServers.class, DefaultIrcServers.class, DefaultWwwServers.class,
+ MobileIpHomeAgents.class, NbddServers.class, NetbiosNameServers.class, NetbiosNodeType.class,
+ NetbiosScope.class, NisDomain.class, NisPlusDomain.class, NisPlusServers.class, NisServers.class,
+ NntpServers.class, NtpServers.class, Pop3Servers.class, SmtpServers.class, StdaServers.class,
+ StreetTalkServers.class, VendorSpecificInformation.class, XWindowDisplayManagers.class,
+ XWindowFontServers.class, DefaultIpTimeToLive.class, IpForwarding.class, MaximumDatagramSize.class,
+ NonLocalSourceRouting.class, PathMtuAgingTimeout.class, PathMtuPlateauTable.class, PolicyFilter.class,
+ AllSubnetsAreLocal.class, BroadcastAddress.class, InterfaceMtu.class, MaskSupplier.class,
+ PerformMaskDiscovery.class, PerformRouterDiscovery.class, RouterSolicitationAddress.class,
+ StaticRoute.class, TcpDefaultTimeToLive.class, TcpKeepaliveGarbage.class, TcpKeepaliveInterval.class,
+ BootFileSize.class, CookieServers.class, DomainName.class, DomainNameServers.class, ExtensionsPath.class,
+ HostName.class, ImpressServers.class, LogServers.class, LprServers.class, MeritDumpFile.class,
+ NameServers.class, ResourceLocationServers.class, RootPath.class, Routers.class, SubnetMask.class,
+ SwapServer.class, TimeOffset.class, TimeServers.class, };
+ /**
+ * A map of concrete implementations of DhcpOption indexed by tag code.
+ */
+ private static Map OPTION_CLASS_BY_CODE;
/**
- * Creates a new instance of DhcpOption.
- *
- * @param tag
- * @param length
+ * A map of tag codes indexed by OptionClass subclass.
*/
- public DhcpOption( int tag, int length )
+ private static Map CODE_BY_CLASS;
+
+ static
{
- this.tag = tag;
- this.length = length;
+ try
+ {
+ // initialize the tag-to-class and class-to-tag map
+ Map classByCode = new HashMap();
+ Map codeByClass = new HashMap();
+ for ( int i = 0; i < OPTION_CLASSES.length; i++ )
+ {
+ Class c = OPTION_CLASSES[i];
+
+ if ( !DhcpOption.class.isAssignableFrom( c ) )
+ throw new RuntimeException( "Class " + c + " is not a descendant of DhcpOption" );
+
+ DhcpOption o = ( DhcpOption ) c.newInstance();
+
+ Integer tagInt = new Integer( o.getTag() );
+ classByCode.put( tagInt, c );
+ codeByClass.put( c, tagInt );
+ }
+
+ OPTION_CLASS_BY_CODE = Collections.unmodifiableMap( classByCode );
+ CODE_BY_CLASS = Collections.unmodifiableMap( codeByClass );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( "Can't initialize option field classes", e );
+ }
}
- abstract protected void valueToByteBuffer( ByteBuffer out );
+ public static Class getClassByTag( int tag )
+ {
+ return ( Class ) OPTION_CLASS_BY_CODE.get( new Integer( tag ) );
+ }
+ public static int getTagByClass( Class c )
+ {
+ return ( ( Integer ) CODE_BY_CLASS.get( c ) ).intValue();
+ }
+
/**
- * Writes this {@link DhcpOption} to the provided {@link ByteBuffer}.
- *
- * @param out
+ * The default data array used for simple (unparsed) options.
*/
- public void writeTo( ByteBuffer out )
+ private byte[] data;
+
+
+ /**
+ * Get the option's code tag.
+ *
+ * @return
+ */
+ public abstract byte getTag();
+
+
+ /**
+ * Set the data (wire format) from a byte array. The default implementation
+ * just records the data as a byte array. Subclasses may parse the data into
+ * something more meaningful.
+ *
+ * @param data
+ */
+ public void setData( byte data[] )
+ {
+ this.data = data;
+ }
+
+
+ /**
+ * Get the data (wire format) into a byte array. Subclasses must provide an
+ * implementation which serializes the parsed data back into a byte array if
+ * they override {@link #setData(byte[])}.
+ *
+ * @return
+ */
+ public byte[] getData()
{
- out.put( ( byte ) tag );
- out.put( ( byte ) length );
+ return data;
+ }
+
+
+ public final void writeTo( ByteBuffer out )
+ {
+ out.put( getTag() );
+
+ // FIXME: handle continuation, i.e. options longer than 128 bytes?
+ byte data[] = getData();
+ if ( data.length > 255 )
+ throw new IllegalArgumentException( "Max data length: 128 bytes." );
- valueToByteBuffer( out );
+ out.put( ( byte ) data.length );
+ out.put( data );
}
}
Added: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/IntOption.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/IntOption.java?rev=628890&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/IntOption.java (added)
+++ directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/IntOption.java Mon Feb 18 14:14:09 2008
@@ -0,0 +1,63 @@
+/*
+ * 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.server.dhcp.options;
+
+/**
+ * The Dynamic Host Configuration Protocol (DHCP) provides a framework for
+ * passing configuration information to hosts on a TCP/IP network. Configuration
+ * parameters and other control information are carried in tagged data items
+ * that are stored in the 'options' field of the DHCP message. The data items
+ * themselves are also called "options."
+ *
+ * This abstract base class is for options that carry a short value (16 bit).
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 551805 $, $Date: 2007-06-29 00:57:04 -0500 (Fr, 29 Jun 2007) $
+ */
+public abstract class IntOption extends DhcpOption {
+ /**
+ * The int value (represented as a long because of the unsignedness).
+ */
+ private long intValue;
+
+ /*
+ * @see org.apache.directory.server.dhcp.options.DhcpOption#setData(byte[])
+ */
+ public void setData(byte[] data) {
+ intValue = (data[0] & 0xff) << 24 | (data[1] & 0xff) << 16
+ | (data[2] & 0xff) << 8 | (data[3] & 0xff);
+ }
+
+ /*
+ * @see org.apache.directory.server.dhcp.options.DhcpOption#getData()
+ */
+ public byte[] getData() {
+ return new byte[]{(byte) (intValue >> 24 & 0xff),
+ (byte) (intValue >> 16 & 0xff), (byte) (intValue >> 8 & 0xff),
+ (byte) (intValue & 0xff)};
+ }
+
+ public long getIntValue() {
+ return intValue;
+ }
+
+ public void setIntValue(long intValue) {
+ this.intValue = intValue;
+ }
+}
Propchange: directory/apacheds/branches/bigbang/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/options/IntOption.java
------------------------------------------------------------------------------
svn:mime-type = text/plain