Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
public class DhcpMessageDecoder
{

/** The minimum size of a DHCP message in bytes. */
private static final int MIN_DHCP_MESSAGE_SIZE = 241;

/**
* Convert a byte buffer into a DhcpMessage.
*
Expand All @@ -55,6 +58,11 @@ public DhcpMessage decode( ByteBuffer buffer ) throws DhcpException
{
try
{
if ( buffer.remaining() < MIN_DHCP_MESSAGE_SIZE )
{
throw new BufferUnderflowException();
}

byte op = buffer.get();

short htype = ( short ) ( buffer.get() & 0xff );
Expand Down Expand Up @@ -94,8 +102,12 @@ public DhcpMessage decode( ByteBuffer buffer ) throws DhcpException
* @param len
* @return
*/
private static byte[] decodeBytes( ByteBuffer buffer, int len )
private static byte[] decodeBytes( ByteBuffer buffer, int len ) throws BufferUnderflowException
{
if ( buffer.remaining() < len )
{
throw new BufferUnderflowException();
}
byte[] bytes = new byte[len];
buffer.get( bytes );
return bytes;
Expand All @@ -106,15 +118,15 @@ private static byte[] decodeBytes( ByteBuffer buffer, int len )
* @param buffer
* @return
*/
private static String decodeString( ByteBuffer buffer, int len )
private static String decodeString( ByteBuffer buffer, int len ) throws BufferUnderflowException
{
byte[] bytes = new byte[len];
buffer.get( bytes );

// find zero-terminator
int slen = 0;

while ( bytes[slen] != 0 )
while ( slen < len && bytes[slen] != 0 )
{
slen++;
}
Expand All @@ -137,8 +149,12 @@ private static String decodeString( ByteBuffer buffer, int len )
* @return
* @throws UnknownHostException
*/
private static InetAddress decodeAddress( ByteBuffer buffer )
private static InetAddress decodeAddress( ByteBuffer buffer ) throws BufferUnderflowException
{
if ( buffer.remaining() < 4 )
{
throw new BufferUnderflowException();
}
byte[] addr = new byte[4];
buffer.get( addr );

Expand All @@ -159,6 +175,11 @@ private static InetAddress decodeAddress( ByteBuffer buffer )

public OptionsField decodeOptions( ByteBuffer message ) throws DhcpException
{
if ( message.remaining() < 4 )
{
throw new BufferUnderflowException();
}

byte[] magicCookie = new byte[4];
message.get( magicCookie );

Expand All @@ -173,7 +194,7 @@ public OptionsField decodeOptions( ByteBuffer message ) throws DhcpException

OptionsField options = new OptionsField();

while ( true )
while ( message.hasRemaining() )
{
code = message.get();

Expand All @@ -187,13 +208,23 @@ public OptionsField decodeOptions( ByteBuffer message ) throws DhcpException
break;
}

if ( !message.hasRemaining() )
{
throw new BufferUnderflowException();
}

length = message.get();

if ( length < 0 )
{
throw new DhcpException( I18n.err( I18n.ERR_36009_NEGATIVE_INDEX_IN_BUFFER ) );
}

if ( message.remaining() < length )
{
throw new BufferUnderflowException();
}

value = new byte[length];
message.get( value );

Expand Down