@Test public void testParseDiscover() throws Exception { ByteBuffer requestByteBuffer = getByteBufferFromFile("DHCPDISCOVER.pdu"); DhcpMessageDecoder decoder = new DhcpMessageDecoder(); DhcpMessage dhcpRequest = decoder.decode(requestByteBuffer); print(dhcpRequest); }
@Nonnull public DhcpMessage decode(@Nonnull ByteBuffer buffer) throws DhcpException, IOException { try { byte op = buffer.get(); 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(); InetAddress ciaddr = decodeAddress(buffer); InetAddress yiaddr = decodeAddress(buffer); InetAddress siaddr = decodeAddress(buffer); InetAddress giaddr = decodeAddress(buffer); byte[] chaddr = decodeBytes(buffer, 16); String sname = decodeString(buffer, 64); String file = decodeString(buffer, 128); OptionsField options = decodeOptions(buffer); DhcpMessageType mto = options.get(DhcpMessageType.class); return new DhcpMessage(null != mto ? mto.getMessageType() : null, op, new HardwareAddress(htype, hlen, chaddr), hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, sname, file, options); } catch (BufferUnderflowException e) { throw new IOException("Failed to decode " + buffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to decode " + buffer, e); } }
DhcpMessageDecoder { @Nonnull public DhcpMessage decode(@Nonnull ByteBuffer buffer) throws DhcpException, IOException { try { byte op = buffer.get(); 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(); InetAddress ciaddr = decodeAddress(buffer); InetAddress yiaddr = decodeAddress(buffer); InetAddress siaddr = decodeAddress(buffer); InetAddress giaddr = decodeAddress(buffer); byte[] chaddr = decodeBytes(buffer, 16); String sname = decodeString(buffer, 64); String file = decodeString(buffer, 128); OptionsField options = decodeOptions(buffer); DhcpMessageType mto = options.get(DhcpMessageType.class); return new DhcpMessage(null != mto ? mto.getMessageType() : null, op, new HardwareAddress(htype, hlen, chaddr), hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, sname, file, options); } catch (BufferUnderflowException e) { throw new IOException("Failed to decode " + buffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to decode " + buffer, e); } } }
DhcpMessageDecoder { @Nonnull public DhcpMessage decode(@Nonnull ByteBuffer buffer) throws DhcpException, IOException { try { byte op = buffer.get(); 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(); InetAddress ciaddr = decodeAddress(buffer); InetAddress yiaddr = decodeAddress(buffer); InetAddress siaddr = decodeAddress(buffer); InetAddress giaddr = decodeAddress(buffer); byte[] chaddr = decodeBytes(buffer, 16); String sname = decodeString(buffer, 64); String file = decodeString(buffer, 128); OptionsField options = decodeOptions(buffer); DhcpMessageType mto = options.get(DhcpMessageType.class); return new DhcpMessage(null != mto ? mto.getMessageType() : null, op, new HardwareAddress(htype, hlen, chaddr), hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, sname, file, options); } catch (BufferUnderflowException e) { throw new IOException("Failed to decode " + buffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to decode " + buffer, e); } }  }
DhcpMessageDecoder { @Nonnull public DhcpMessage decode(@Nonnull ByteBuffer buffer) throws DhcpException, IOException { try { byte op = buffer.get(); 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(); InetAddress ciaddr = decodeAddress(buffer); InetAddress yiaddr = decodeAddress(buffer); InetAddress siaddr = decodeAddress(buffer); InetAddress giaddr = decodeAddress(buffer); byte[] chaddr = decodeBytes(buffer, 16); String sname = decodeString(buffer, 64); String file = decodeString(buffer, 128); OptionsField options = decodeOptions(buffer); DhcpMessageType mto = options.get(DhcpMessageType.class); return new DhcpMessage(null != mto ? mto.getMessageType() : null, op, new HardwareAddress(htype, hlen, chaddr), hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, sname, file, options); } catch (BufferUnderflowException e) { throw new IOException("Failed to decode " + buffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to decode " + buffer, e); } }  @Nonnull DhcpMessage decode(@Nonnull ByteBuffer buffer); }
DhcpMessageDecoder { @Nonnull public DhcpMessage decode(@Nonnull ByteBuffer buffer) throws DhcpException, IOException { try { byte op = buffer.get(); 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(); InetAddress ciaddr = decodeAddress(buffer); InetAddress yiaddr = decodeAddress(buffer); InetAddress siaddr = decodeAddress(buffer); InetAddress giaddr = decodeAddress(buffer); byte[] chaddr = decodeBytes(buffer, 16); String sname = decodeString(buffer, 64); String file = decodeString(buffer, 128); OptionsField options = decodeOptions(buffer); DhcpMessageType mto = options.get(DhcpMessageType.class); return new DhcpMessage(null != mto ? mto.getMessageType() : null, op, new HardwareAddress(htype, hlen, chaddr), hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, sname, file, options); } catch (BufferUnderflowException e) { throw new IOException("Failed to decode " + buffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to decode " + buffer, e); } }  @Nonnull DhcpMessage decode(@Nonnull ByteBuffer buffer);  }