return msg.toDatagramPacket();
}
protected boolean processResponse(int transactionID, DatagramPacket packet) throws IOException {
DHCPMessage msg = new DHCPMessage(packet);
BOOTPHeader hdr = msg.getHeader();
if (hdr.getOpcode() != BOOTPHeader.BOOTREPLY) {
// Not a response
return false;
}
if (hdr.getTransactionID() != transactionID) {
// Not for me
return false;
}
// debug the DHCP message
if (log.isDebugEnabled()) {
log.debug("Got Client IP address : " + hdr.getClientIPAddress());
log.debug("Got Your IP address : " + hdr.getYourIPAddress());
log.debug("Got Server IP address : " + hdr.getServerIPAddress());
log.debug("Got Gateway IP address : " + hdr.getGatewayIPAddress());
for (int n = 1; n < 255; n++) {
byte[] value = msg.getOption(n);
if (value != null) {
if (value.length == 1) {
log.debug("Option " + n + " : " + (int) (value[0]));
} else if (value.length == 2) {
log.debug("Option " + n + " : " + ((value[0] << 8) | value[1]));
} else if (value.length == 4) {
log.debug("Option " + n + " : " +
InetAddress.getByAddress(value).toString());
} else {
log.debug("Option " + n + " : " + new String(value));
}
}
}
}
switch (msg.getMessageType()) {
case DHCPMessage.DHCPOFFER:
byte[] serverID = msg.getOption(DHCPMessage.SERVER_IDENTIFIER_OPTION);
byte[] requestedIP = hdr.getYourIPAddress().getAddress();
hdr = new BOOTPHeader(
BOOTPHeader.BOOTREQUEST, transactionID, 0,
hdr.getClientIPAddress(), hdr.getClientHwAddress());
msg = new DHCPMessage(hdr, DHCPMessage.DHCPREQUEST);
msg.setOption(DHCPMessage.REQUESTED_IP_ADDRESS_OPTION, requestedIP);
msg.setOption(DHCPMessage.SERVER_IDENTIFIER_OPTION, serverID);
packet = msg.toDatagramPacket();
packet.setAddress(IPv4Address.BROADCAST_ADDRESS);