use of org.onlab.packet.DHCP in project onos by opennetworkinglab.
the class DhcpTest method longOptionTest.
/**
* Test option with option length > 128.
*/
@Test
public void longOptionTest() throws Exception {
byte[] data = Resources.toByteArray(Dhcp6RelayTest.class.getResource(LONG_OPT));
DHCP dhcp = DHCP.deserializer().deserialize(data, 0, data.length);
assertEquals(2, dhcp.getOptions().size());
DhcpOption hostnameOption = dhcp.getOption(DHCP.DHCPOptionCode.OptionCode_HostName);
DhcpOption endOption = dhcp.getOption(DHCP.DHCPOptionCode.OptionCode_END);
assertNotNull(hostnameOption);
assertNotNull(endOption);
// Host name contains 200 "A"
StringBuilder hostnameBuilder = new StringBuilder();
IntStream.range(0, 200).forEach(i -> hostnameBuilder.append("A"));
String hostname = hostnameBuilder.toString();
assertEquals((byte) 200, hostnameOption.getLength());
assertArrayEquals(hostname.getBytes(Charsets.US_ASCII), hostnameOption.getData());
}
use of org.onlab.packet.DHCP in project onos by opennetworkinglab.
the class DhcpTest method testDeserializeRequest.
/**
* Tests deserialize discover packet.
*/
@Test
public void testDeserializeRequest() throws Exception {
byte[] data = Resources.toByteArray(Dhcp6RelayTest.class.getResource(REQUEST));
Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length);
DHCP dhcp = (DHCP) eth.getPayload().getPayload().getPayload();
assertEquals(DHCP.OPCODE_REQUEST, dhcp.getOpCode());
assertEquals(HW_TYPE, dhcp.getHardwareType());
assertEquals(HW_ADDR_LEN, dhcp.getHardwareAddressLength());
assertEquals(HOPS, dhcp.getHops());
assertEquals(XID, dhcp.getTransactionId());
assertEquals(SECS, dhcp.getSeconds());
assertEquals(FLAGS, dhcp.getFlags());
assertEquals(NO_IP, dhcp.getClientIPAddress());
assertEquals(NO_IP, dhcp.getYourIPAddress());
assertEquals(NO_IP, dhcp.getServerIPAddress());
assertEquals(GW_IP.toInt(), dhcp.getGatewayIPAddress());
assertTrue(Arrays.equals(CLIENT_HW_ADDR.toBytes(), dhcp.getClientHardwareAddress()));
assertEquals(EMPTY, dhcp.getServerName());
assertEquals(EMPTY, dhcp.getBootFileName());
assertEquals(7, dhcp.getOptions().size());
DhcpOption option = dhcp.getOptions().get(0);
assertEquals(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue(), option.code);
assertEquals(1, option.length);
assertEquals(DHCP.MsgType.DHCPREQUEST.getValue(), (int) option.getData()[0]);
option = dhcp.getOptions().get(1);
assertEquals(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue(), option.code);
assertEquals(4, option.length);
assertArrayEquals(SERVER_IP.toOctets(), option.getData());
option = dhcp.getOptions().get(2);
assertEquals(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue(), option.code);
assertEquals(4, option.length);
assertArrayEquals(CLIENT_IP.toOctets(), option.getData());
option = dhcp.getOptions().get(3);
assertEquals(DHCP.DHCPOptionCode.OptionCode_HostName.getValue(), option.code);
assertEquals(9, option.length);
assertArrayEquals(HOSTNAME.getBytes(Charsets.US_ASCII), option.getData());
option = dhcp.getOptions().get(4);
assertEquals(DHCP.DHCPOptionCode.OptionCode_RequestedParameters.getValue(), option.code);
assertEquals(13, option.length);
assertArrayEquals(new byte[] { 1, 28, 2, 3, 15, 6, 119, 12, 44, 47, 26, 121, 42 }, option.getData());
option = dhcp.getOptions().get(5);
assertTrue(option instanceof DhcpRelayAgentOption);
DhcpRelayAgentOption relayAgentOption = (DhcpRelayAgentOption) option;
assertEquals(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue(), relayAgentOption.code);
assertEquals(12, relayAgentOption.length);
DhcpOption subOption = relayAgentOption.getSubOption(DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID.getValue());
assertEquals(10, subOption.getLength());
assertArrayEquals(CIRCUIT_ID.getBytes(Charsets.US_ASCII), subOption.getData());
option = dhcp.getOptions().get(6);
assertEquals(DHCP.DHCPOptionCode.OptionCode_END.getValue(), option.code);
assertEquals(0, option.length);
}
use of org.onlab.packet.DHCP in project onos by opennetworkinglab.
the class OpenstackSwitchingDhcpHandlerTest method validatePacket.
/**
* Validates the contents of the packet sent by the OpenstackSwitchingDhcpHandler.
*
* @param packet Ethernet packet received
*/
private void validatePacket(Ethernet packet) {
DHCP dhcpPacket = (DHCP) packet.getPayload().getPayload().getPayload();
assertEquals(MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()), CLIENT_HOST.mac());
assertEquals(Ip4Address.valueOf(dhcpPacket.getYourIPAddress()), Ip4Address.valueOf(EXPECTED_IP));
assertEquals(dhcpPacket.getTransactionId(), TRANSACTION_ID);
if (testMsgType == DHCP.MsgType.DHCPDISCOVER) {
assertEquals(dhcpPacket.getPacketType(), DHCP.MsgType.DHCPOFFER);
}
if (testMsgType == DHCP.MsgType.DHCPREQUEST) {
assertEquals(dhcpPacket.getPacketType(), DHCP.MsgType.DHCPACK);
}
}
use of org.onlab.packet.DHCP in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method processDhcpPacket.
@Override
public void processDhcpPacket(PacketContext context, BasePacket payload) {
checkNotNull(payload, "DHCP payload can't be null");
checkState(payload instanceof DHCP, "Payload is not a DHCP");
DHCP dhcpPayload = (DHCP) payload;
if (!configured()) {
log.warn("Missing default DHCP relay server config. Abort packet processing");
return;
}
ConnectPoint inPort = context.inPacket().receivedFrom();
checkNotNull(dhcpPayload, "Can't find DHCP payload");
Ethernet packet = context.inPacket().parsed();
DHCP.MsgType incomingPacketType = dhcpPayload.getOptions().stream().filter(dhcpOption -> dhcpOption.getCode() == OptionCode_MessageType.getValue()).map(DhcpOption::getData).map(data -> DHCP.MsgType.getType(data[0])).findFirst().orElse(null);
checkNotNull(incomingPacketType, "Can't get message type from DHCP payload {}", dhcpPayload);
Set<Interface> receivingInterfaces = interfaceService.getInterfacesByPort(inPort);
// ignore the packets if dhcp client interface is not configured on onos.
if (receivingInterfaces.isEmpty()) {
log.warn("Virtual interface is not configured on {}", inPort);
return;
}
switch(incomingPacketType) {
case DHCPDISCOVER:
// Add the gateway IP as virtual interface IP for server to understand
// the lease to be assigned and forward the packet to dhcp server.
List<InternalPacket> ethernetClientPacket = processDhcpPacketFromClient(context, packet, receivingInterfaces);
for (InternalPacket internalPacket : ethernetClientPacket) {
log.debug("DHCPDISCOVER from {} Forward to server", inPort);
writeRequestDhcpRecord(inPort, packet, dhcpPayload);
forwardPacket(internalPacket);
}
break;
case DHCPOFFER:
// reply to dhcp client.
InternalPacket ethernetPacketOffer = processDhcpPacketFromServer(context, packet);
if (ethernetPacketOffer != null) {
writeResponseDhcpRecord(ethernetPacketOffer.getPacket(), dhcpPayload);
sendResponseToClient(ethernetPacketOffer, dhcpPayload);
}
break;
case DHCPREQUEST:
// add the gateway ip as virtual interface ip for server to understand
// the lease to be assigned and forward the packet to dhcp server.
List<InternalPacket> ethernetPacketRequest = processDhcpPacketFromClient(context, packet, receivingInterfaces);
for (InternalPacket internalPacket : ethernetPacketRequest) {
log.debug("DHCPDISCOVER from {} Forward to server", inPort);
writeRequestDhcpRecord(inPort, packet, dhcpPayload);
forwardPacket(internalPacket);
}
break;
case DHCPDECLINE:
break;
case DHCPACK:
// reply to dhcp client.
InternalPacket ethernetPacketAck = processDhcpPacketFromServer(context, packet);
if (ethernetPacketAck != null) {
writeResponseDhcpRecord(ethernetPacketAck.getPacket(), dhcpPayload);
handleDhcpAck(ethernetPacketAck.getPacket(), dhcpPayload);
sendResponseToClient(ethernetPacketAck, dhcpPayload);
}
break;
case DHCPNAK:
break;
case DHCPRELEASE:
// TODO: release the ip address from client
break;
case DHCPINFORM:
break;
case DHCPFORCERENEW:
break;
case DHCPLEASEQUERY:
handleLeaseQueryMsg(context, packet, dhcpPayload);
break;
case DHCPLEASEACTIVE:
handleLeaseQueryActivateMsg(packet, dhcpPayload);
break;
case DHCPLEASEUNASSIGNED:
case DHCPLEASEUNKNOWN:
handleLeaseQueryUnknown(packet, dhcpPayload);
break;
default:
break;
}
}
use of org.onlab.packet.DHCP in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method setDhcpServerConfigs.
public void setDhcpServerConfigs(Collection<DhcpServerConfig> configs, List<DhcpServerInfo> serverInfoList) {
if (configs.size() == 0) {
// no config to update
return;
}
Boolean isConfigValid = false;
for (DhcpServerConfig serverConfig : configs) {
if (serverConfig.getDhcpServerIp4().isPresent()) {
isConfigValid = true;
break;
}
}
if (!isConfigValid) {
log.warn("No IP V4 server address found.");
// No IP V6 address found
return;
}
// if (!serverInfoList.isEmpty()) {
for (DhcpServerInfo oldServerInfo : serverInfoList) {
log.info("In for (DhcpServerInfo oldServerInfo : serverInfoList) {");
// remove old server info
// DhcpServerInfo oldServerInfo = serverInfoList.remove(0);
// stop monitoring gateway or server
oldServerInfo.getDhcpGatewayIp4().ifPresent(gatewayIp -> {
hostService.stopMonitoringIp(gatewayIp);
});
oldServerInfo.getDhcpServerIp4().ifPresent(serverIp -> {
hostService.stopMonitoringIp(serverIp);
cancelDhcpPacket(serverIp);
});
}
// Create new server info according to the config
serverInfoList.clear();
for (DhcpServerConfig serverConfig : configs) {
log.debug("Create new server info according to the config");
DhcpServerInfo newServerInfo = new DhcpServerInfo(serverConfig, DhcpServerInfo.Version.DHCP_V4);
checkState(newServerInfo.getDhcpServerConnectPoint().isPresent(), "Connect point not exists");
checkState(newServerInfo.getDhcpServerIp4().isPresent(), "IP of DHCP server not exists");
log.debug("DHCP server connect point: {}", newServerInfo.getDhcpServerConnectPoint().orElse(null));
log.debug("DHCP server IP: {}", newServerInfo.getDhcpServerIp4().orElse(null));
Ip4Address serverIp = newServerInfo.getDhcpServerIp4().get();
Ip4Address ipToProbe;
if (newServerInfo.getDhcpGatewayIp4().isPresent()) {
ipToProbe = newServerInfo.getDhcpGatewayIp4().get();
} else {
ipToProbe = newServerInfo.getDhcpServerIp4().orElse(null);
}
log.info("Probe_IP {}", ipToProbe);
String hostToProbe = newServerInfo.getDhcpGatewayIp4().map(ip -> "gateway").orElse("server");
log.debug("Probing to resolve {} IP {}", hostToProbe, ipToProbe);
hostService.startMonitoringIp(ipToProbe);
Set<Host> hosts = hostService.getHostsByIp(ipToProbe);
if (!hosts.isEmpty()) {
Host host = hosts.iterator().next();
newServerInfo.setDhcpConnectVlan(host.vlan());
newServerInfo.setDhcpConnectMac(host.mac());
}
// Add new server info
synchronized (this) {
// serverInfoList.clear();
serverInfoList.add(newServerInfo);
}
requestDhcpPacket(serverIp);
}
}
Aggregations