use of net.floodlightcontroller.packet.DHCPOption in project open-kilda by telstra.
the class PacketFactory method DhcpDiscoveryRequestEthernet.
/**
* Generates a DHCP request Ethernet frame.
* @param hostMac The host MAC address of for the request.
* @returnAn An Ethernet frame that contains a DHCP request packet.
*/
public static Ethernet DhcpDiscoveryRequestEthernet(MacAddress hostMac) {
List<DHCPOption> optionList = new ArrayList<DHCPOption>();
byte[] requestValue = new byte[4];
requestValue[0] = requestValue[1] = requestValue[2] = requestValue[3] = 0;
DHCPOption requestOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()).setLength((byte) 4).setData(requestValue);
byte[] msgTypeValue = new byte[1];
// DHCP request
msgTypeValue[0] = 1;
DHCPOption msgTypeOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()).setLength((byte) 1).setData(msgTypeValue);
byte[] reqParamValue = new byte[4];
// subnet mask
reqParamValue[0] = 1;
// Router
reqParamValue[1] = 3;
// Domain Name Server
reqParamValue[2] = 6;
// NTP Server
reqParamValue[3] = 42;
DHCPOption reqParamOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_RequestedParameters.getValue()).setLength((byte) 4).setData(reqParamValue);
byte[] clientIdValue = new byte[7];
// Ethernet
clientIdValue[0] = 1;
System.arraycopy(hostMac.getBytes(), 0, clientIdValue, 1, 6);
DHCPOption clientIdOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_ClientID.getValue()).setLength((byte) 7).setData(clientIdValue);
DHCPOption endOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()).setLength((byte) 0).setData(null);
optionList.add(requestOption);
optionList.add(msgTypeOption);
optionList.add(reqParamOption);
optionList.add(clientIdOption);
optionList.add(endOption);
Ethernet requestPacket = new Ethernet();
requestPacket.setSourceMACAddress(hostMac.getBytes()).setDestinationMACAddress(broadcastMac).setEtherType(EthType.IPv4).setPayload(new IPv4().setVersion((byte) 4).setDiffServ((byte) 0).setIdentification((short) 100).setFlags((byte) 0).setFragmentOffset((short) 0).setTtl((byte) 250).setProtocol(IpProtocol.UDP).setChecksum((short) 0).setSourceAddress(0).setDestinationAddress(broadcastIp).setPayload(new UDP().setSourcePort(UDP.DHCP_CLIENT_PORT).setDestinationPort(UDP.DHCP_SERVER_PORT).setChecksum((short) 0).setPayload(new DHCP().setOpCode(DHCP.OPCODE_REQUEST).setHardwareType(DHCP.HWTYPE_ETHERNET).setHardwareAddressLength((byte) 6).setHops((byte) 0).setTransactionId(0x00003d1d).setSeconds((short) 0).setFlags((short) 0).setClientIPAddress(IPv4Address.NONE).setYourIPAddress(IPv4Address.NONE).setServerIPAddress(IPv4Address.NONE).setGatewayIPAddress(IPv4Address.NONE).setClientHardwareAddress(hostMac).setOptions(optionList))));
return requestPacket;
}