directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r124632 - incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io
Date Sat, 08 Jan 2005 06:06:17 GMT
Author: erodriguez
Date: Fri Jan  7 22:06:17 2005
New Revision: 124632

URL: http://svn.apache.org/viewcvs?view=rev&rev=124632
Log:
Working DHCP PDU encoding and decoding.
Added:
   incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/
   incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageDecoder.java
   incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageEncoder.java
   incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsDecoder.java
   incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsEncoder.java

Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageDecoder.java?view=auto&rev=124632
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageDecoder.java
Fri Jan  7 22:06:17 2005
@@ -0,0 +1,80 @@
+/*
+ *   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.dhcp.io;
+
+import java.nio.ByteBuffer;
+
+import org.apache.dhcp.DhcpException;
+import org.apache.dhcp.messages.DhcpMessage;
+import org.apache.dhcp.messages.DhcpMessageModifier;
+
+
+public class DhcpMessageDecoder
+{
+	/**
+	 * Convert a specified byte array into a DhcpMessage.
+	 * 
+	 * @return a DhcpMessage.
+	 * @param buffer ByteBuffer to convert to a DhcpMessage object
+	 */
+	public DhcpMessage decode(ByteBuffer buffer) throws DhcpException
+	{
+		DhcpMessageModifier modifier = new DhcpMessageModifier();
+		
+		modifier.setOpCode( buffer.get() );
+		modifier.setHardwareAddressType( buffer.get() );
+		modifier.setHardwareAddressLength( buffer.get() );
+		modifier.setHardwareOptions( buffer.get() );
+		
+		modifier.setTransactionId( buffer.getInt() );
+		modifier.setSeconds( buffer.getShort() );
+		modifier.setFlags( buffer.getShort() );
+		
+		byte[] nextFourBytes = new byte[4];
+		
+		buffer.get( nextFourBytes );
+		modifier.setActualClientAddress( nextFourBytes );
+		
+		buffer.get( nextFourBytes );
+		modifier.setAssignedClientAddress( nextFourBytes );
+		
+		buffer.get( nextFourBytes );
+		modifier.setNextServerAddress( nextFourBytes );
+		
+		buffer.get( nextFourBytes );
+		modifier.setRelayAgentAddress( nextFourBytes );
+		
+		byte[] clientHardwareAddress = new byte[16];
+		buffer.get( clientHardwareAddress );
+		modifier.setClientHardwareAddress( clientHardwareAddress );
+		
+		byte[] serverHostname = new byte[64];
+		buffer.get( serverHostname );
+		modifier.setServerHostname( serverHostname );
+		
+		byte[] bootFileName = new byte[128];
+		buffer.get( bootFileName );
+		modifier.setBootFileName( bootFileName );
+
+		DhcpOptionsDecoder decoder = new DhcpOptionsDecoder();
+		modifier.setOptions( decoder.decode( buffer ) );
+		
+		return modifier.getDhcpMessage();
+	}
+}
+

Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageEncoder.java?view=auto&rev=124632
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpMessageEncoder.java
Fri Jan  7 22:06:17 2005
@@ -0,0 +1,61 @@
+/*
+ *   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.dhcp.io;
+
+import java.nio.ByteBuffer;
+
+import org.apache.dhcp.messages.DhcpMessage;
+import org.apache.dhcp.options.OptionsField;
+
+
+public class DhcpMessageEncoder
+{
+	/**
+	 * Converts a DhcpMessage object to a byte array.
+	 * 
+	 * @return a byte array with information from DhcpMessage object.
+	 */
+	public ByteBuffer encode(DhcpMessage message)
+	{
+		ByteBuffer buffer = ByteBuffer.allocate(1500);
+		
+		buffer.put( (byte)message.getMessageType().getOrdinal() );
+		buffer.put( message.getHardwareAddressType() );
+		buffer.put( message.getHardwareAddressLength() );
+		buffer.put( message.getHardwareOptions() );
+		buffer.putInt( message.getTransactionId() );
+		buffer.putShort( message.getSeconds() );
+		buffer.putShort( message.getFlags() );
+		buffer.put( message.getActualClientAddress() );
+		buffer.put( message.getAssignedClientAddress() );
+		buffer.put( message.getNextServerAddress() );
+		buffer.put( message.getRelayAgentAddress() );
+		buffer.put( message.getClientHardwareAddress() );
+		buffer.put( message.getServerHostname() );
+		buffer.put( message.getBootFileName() );
+		
+		OptionsField options = message.getOptions();
+		
+		DhcpOptionsEncoder optionsEncoder = new DhcpOptionsEncoder();
+		
+		optionsEncoder.encode( options, buffer );
+		
+		return buffer;
+	}
+}
+

Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsDecoder.java?view=auto&rev=124632
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsDecoder.java
Fri Jan  7 22:06:17 2005
@@ -0,0 +1,105 @@
+/*
+ *   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.dhcp.io;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.dhcp.DhcpException;
+import org.apache.dhcp.options.DhcpOption;
+import org.apache.dhcp.options.OptionsField;
+import org.apache.dhcp.options.dhcp.DhcpMessageType;
+import org.apache.dhcp.options.dhcp.IpAddressLeaseTime;
+import org.apache.dhcp.options.dhcp.ParameterRequestList;
+import org.apache.dhcp.options.dhcp.RequestedIpAddress;
+import org.apache.dhcp.options.dhcp.ServerIdentifier;
+import org.apache.dhcp.options.vendor.DomainName;
+import org.apache.dhcp.options.vendor.DomainNameServerOption;
+import org.apache.dhcp.options.vendor.EndOption;
+import org.apache.dhcp.options.vendor.PadOption;
+import org.apache.dhcp.options.vendor.SubnetMask;
+import org.apache.dhcp.options.vendor.TimeOffset;
+
+
+public class DhcpOptionsDecoder
+{
+	private static final byte[] VENDOR_MAGIC_COOKIE =
+			{ (byte) 99, (byte) 130, (byte) 83, (byte) 99 };
+	
+	public OptionsField decode( 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 ( message.get( message.position() ) != (byte) 255)
+		{
+			code = message.get();
+			length = message.get();
+			value = new byte[ length ];
+			message.get( value );
+			
+			options.add( getInstance( code, value ) );
+		}
+		
+		return options;
+	}
+	
+	private DhcpOption getInstance( int tag, byte[] value)
+		throws DhcpException
+	{
+		switch (tag)
+		{
+			case 0:
+				return new PadOption();
+			case 1:
+				return new EndOption();
+			case 2:
+				return new SubnetMask( value );
+			case 3:
+				return new TimeOffset( value );
+			case 6:
+				return new DomainNameServerOption( value );
+			case 15:
+				return new DomainName( value );
+			case 50:
+				return new RequestedIpAddress( value );
+			case 51:
+				return new IpAddressLeaseTime( value );
+			case 53:
+				return new DhcpMessageType( value );
+			case 54:
+				return new ServerIdentifier( value );
+			case 55:
+				return new ParameterRequestList( value );
+			default:
+				throw new DhcpException( "Unsupported or bad option code:  " + tag );
+		}
+	}
+}
+

Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsEncoder.java?view=auto&rev=124632
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/io/DhcpOptionsEncoder.java
Fri Jan  7 22:06:17 2005
@@ -0,0 +1,47 @@
+/*
+ *   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.dhcp.io;
+
+import java.nio.ByteBuffer;
+
+import org.apache.dhcp.options.DhcpOption;
+import org.apache.dhcp.options.OptionsField;
+import org.apache.dhcp.options.vendor.EndOption;
+
+
+public class DhcpOptionsEncoder
+{
+	private static final byte[] VENDOR_MAGIC_COOKIE =
+			{ (byte) 99, (byte) 130, (byte) 83, (byte) 99 };
+	
+    public void encode( OptionsField options, ByteBuffer message )
+    {
+    	message.put( VENDOR_MAGIC_COOKIE );
+
+		DhcpOption[] optionsArray = options.toArray();
+		
+		for ( int ii=0; ii < optionsArray.length; ii++ )
+		{
+			optionsArray[ ii ].writeTo( message );
+		}
+		
+		DhcpOption endOption = new EndOption();
+		endOption.writeTo( message );
+	}
+}
+

Mime
View raw message