@Test public void testEncoder() throws Exception { DhcpMessage message = new DhcpMessage(); message.setMessageType(MessageType.DHCPACK); message.setOp(DhcpMessage.OP_BOOTREPLY); message.setHardwareAddress(new HardwareAddress(HardwareAddressType.Ethernet, new byte[]{1, 2, 3, 4, 5, 6})); InetAddress address = InetAddress.getByAddress(new byte[]{8, 9, 10, 11}); message.setAssignedClientAddress(address); message.setCurrentClientAddress(address); message.getOptions().setIntOption(IpAddressLeaseTime.class, 600); message.getOptions().setAddressOption(SubnetMask.class, "255.255.255.0"); message.getOptions().setAddressOption(ServerIdentifier.class, address); message.getOptions().setStringOption(TftpServerName.class, "123.45.67.89"); message.getOptions().setAddressOption(BroadcastAddress.class, address); message.getOptions().setStringOption(BootfileName.class, "http: DhcpMessageEncoder encoder = new DhcpMessageEncoder(); ByteBuffer buf = ByteBuffer.allocate(1024); encoder.encode(buf, message); }
public void encode(@Nonnull ByteBuffer byteBuffer, @Nonnull DhcpMessage message) throws IOException { try { 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((short) message.getSeconds()); byteBuffer.putShort(message.getFlags()); writeAddress(byteBuffer, message.getCurrentClientAddress()); writeAddress(byteBuffer, message.getAssignedClientAddress()); writeAddress(byteBuffer, message.getNextServerAddress()); writeAddress(byteBuffer, message.getRelayAgentAddress()); writeBytes(byteBuffer, (null != hardwareAddress ? hardwareAddress.getAddress() : EMPTY_HARDWARE_ADDRESS), 16); writeString(byteBuffer, message.getServerHostname(), 64); writeString(byteBuffer, message.getBootFileName(), 128); OptionsField options = message.getOptions(); if (message.getMessageType() != null) { options.add(new DhcpMessageType(message.getMessageType())); } encodeOptions(options, byteBuffer); } catch (BufferOverflowException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } }
DhcpMessageEncoder { public void encode(@Nonnull ByteBuffer byteBuffer, @Nonnull DhcpMessage message) throws IOException { try { 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((short) message.getSeconds()); byteBuffer.putShort(message.getFlags()); writeAddress(byteBuffer, message.getCurrentClientAddress()); writeAddress(byteBuffer, message.getAssignedClientAddress()); writeAddress(byteBuffer, message.getNextServerAddress()); writeAddress(byteBuffer, message.getRelayAgentAddress()); writeBytes(byteBuffer, (null != hardwareAddress ? hardwareAddress.getAddress() : EMPTY_HARDWARE_ADDRESS), 16); writeString(byteBuffer, message.getServerHostname(), 64); writeString(byteBuffer, message.getBootFileName(), 128); OptionsField options = message.getOptions(); if (message.getMessageType() != null) { options.add(new DhcpMessageType(message.getMessageType())); } encodeOptions(options, byteBuffer); } catch (BufferOverflowException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } } }
DhcpMessageEncoder { public void encode(@Nonnull ByteBuffer byteBuffer, @Nonnull DhcpMessage message) throws IOException { try { 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((short) message.getSeconds()); byteBuffer.putShort(message.getFlags()); writeAddress(byteBuffer, message.getCurrentClientAddress()); writeAddress(byteBuffer, message.getAssignedClientAddress()); writeAddress(byteBuffer, message.getNextServerAddress()); writeAddress(byteBuffer, message.getRelayAgentAddress()); writeBytes(byteBuffer, (null != hardwareAddress ? hardwareAddress.getAddress() : EMPTY_HARDWARE_ADDRESS), 16); writeString(byteBuffer, message.getServerHostname(), 64); writeString(byteBuffer, message.getBootFileName(), 128); OptionsField options = message.getOptions(); if (message.getMessageType() != null) { options.add(new DhcpMessageType(message.getMessageType())); } encodeOptions(options, byteBuffer); } catch (BufferOverflowException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } }  }
DhcpMessageEncoder { public void encode(@Nonnull ByteBuffer byteBuffer, @Nonnull DhcpMessage message) throws IOException { try { 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((short) message.getSeconds()); byteBuffer.putShort(message.getFlags()); writeAddress(byteBuffer, message.getCurrentClientAddress()); writeAddress(byteBuffer, message.getAssignedClientAddress()); writeAddress(byteBuffer, message.getNextServerAddress()); writeAddress(byteBuffer, message.getRelayAgentAddress()); writeBytes(byteBuffer, (null != hardwareAddress ? hardwareAddress.getAddress() : EMPTY_HARDWARE_ADDRESS), 16); writeString(byteBuffer, message.getServerHostname(), 64); writeString(byteBuffer, message.getBootFileName(), 128); OptionsField options = message.getOptions(); if (message.getMessageType() != null) { options.add(new DhcpMessageType(message.getMessageType())); } encodeOptions(options, byteBuffer); } catch (BufferOverflowException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } }  void encode(@Nonnull ByteBuffer byteBuffer, @Nonnull DhcpMessage message); void encodeOptions(@Nonnull OptionsField options, @Nonnull ByteBuffer message); }
DhcpMessageEncoder { public void encode(@Nonnull ByteBuffer byteBuffer, @Nonnull DhcpMessage message) throws IOException { try { 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((short) message.getSeconds()); byteBuffer.putShort(message.getFlags()); writeAddress(byteBuffer, message.getCurrentClientAddress()); writeAddress(byteBuffer, message.getAssignedClientAddress()); writeAddress(byteBuffer, message.getNextServerAddress()); writeAddress(byteBuffer, message.getRelayAgentAddress()); writeBytes(byteBuffer, (null != hardwareAddress ? hardwareAddress.getAddress() : EMPTY_HARDWARE_ADDRESS), 16); writeString(byteBuffer, message.getServerHostname(), 64); writeString(byteBuffer, message.getBootFileName(), 128); OptionsField options = message.getOptions(); if (message.getMessageType() != null) { options.add(new DhcpMessageType(message.getMessageType())); } encodeOptions(options, byteBuffer); } catch (BufferOverflowException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } catch (IndexOutOfBoundsException e) { throw new IOException("Failed to encode " + message + " into " + byteBuffer, e); } }  void encode(@Nonnull ByteBuffer byteBuffer, @Nonnull DhcpMessage message); void encodeOptions(@Nonnull OptionsField options, @Nonnull ByteBuffer message);  }