Skip to content
Snippets Groups Projects
Commit 804966b7 authored by Ryan Izard's avatar Ryan Izard
Browse files

Initial DHCP server code commit. This was a test of how easy is is to port...

Initial DHCP server code commit. This was a test of how easy is is to port v0.91- code to v1.0. I have not tested this module yet. TODO: Write unit tests for the dhcpserver module.
parent fd4f5b0f
No related branches found
No related tags found
No related merge requests found
package net.floodlightcontroller.dhcpserver;
import org.projectfloodlight.openflow.types.IPv4Address;
import org.projectfloodlight.openflow.types.MacAddress;
/**
* The class representing a DHCP Binding -- MAC and IP.
* It also contains important information regarding the lease status
* --active
* --inactive
* the lease type of the binding
* --dynamic
* --fixed/static
* and the lease times
* --start time in seconds
* --duration in seconds
*
* @author Ryan Izard (rizard@g.clemson.edu)
*/
public class DHCPBinding {
private MacAddress MAC = MacAddress.NONE;
private IPv4Address IP = IPv4Address.NONE;
private boolean LEASE_STATUS;
private boolean PERMANENT_LEASE;
private long LEASE_START_TIME_SECONDS;
private long LEASE_DURATION_SECONDS;
protected DHCPBinding(IPv4Address ip, MacAddress mac) {
this.setMACAddress(mac);
this.setIPv4Addresss(ip);
this.setLeaseStatus(false);
}
public IPv4Address getIPv4Address() {
return IP;
}
public MacAddress getMACAddress() {
return MAC;
}
private void setIPv4Addresss(IPv4Address ip) {
IP = ip;
}
public void setMACAddress(MacAddress mac) {
MAC = mac;
}
public boolean isActiveLease() {
return LEASE_STATUS;
}
public void setStaticIPLease(boolean staticIP) {
PERMANENT_LEASE = staticIP;
}
public boolean isStaticIPLease() {
return PERMANENT_LEASE;
}
public void setLeaseStatus(boolean status) {
LEASE_STATUS = status;
}
public boolean isLeaseExpired() {
long currentTime = System.currentTimeMillis();
if ((currentTime / 1000) >= (LEASE_START_TIME_SECONDS + LEASE_DURATION_SECONDS)) {
return true;
} else {
return false;
}
}
protected void setLeaseStartTimeSeconds() {
LEASE_START_TIME_SECONDS = System.currentTimeMillis() / 1000;
}
protected void setLeaseDurationSeconds(long time) {
LEASE_DURATION_SECONDS = time;
}
protected void clearLeaseTimes() {
LEASE_START_TIME_SECONDS = 0;
LEASE_DURATION_SECONDS = 0;
}
protected boolean cancelLease() {
this.clearLeaseTimes();
this.setLeaseStatus(false);
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((IP == null) ? 0 : IP.hashCode());
result = prime
* result
+ (int) (LEASE_DURATION_SECONDS ^ (LEASE_DURATION_SECONDS >>> 32));
result = prime
* result
+ (int) (LEASE_START_TIME_SECONDS ^ (LEASE_START_TIME_SECONDS >>> 32));
result = prime * result + (LEASE_STATUS ? 1231 : 1237);
result = prime * result + ((MAC == null) ? 0 : MAC.hashCode());
result = prime * result + (PERMANENT_LEASE ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DHCPBinding other = (DHCPBinding) obj;
if (IP == null) {
if (other.IP != null)
return false;
} else if (!IP.equals(other.IP))
return false;
if (LEASE_DURATION_SECONDS != other.LEASE_DURATION_SECONDS)
return false;
if (LEASE_START_TIME_SECONDS != other.LEASE_START_TIME_SECONDS)
return false;
if (LEASE_STATUS != other.LEASE_STATUS)
return false;
if (MAC == null) {
if (other.MAC != null)
return false;
} else if (!MAC.equals(other.MAC))
return false;
if (PERMANENT_LEASE != other.PERMANENT_LEASE)
return false;
return true;
}
}
\ No newline at end of file
package net.floodlightcontroller.dhcpserver;
import java.util.ArrayList;
import org.projectfloodlight.openflow.types.IPv4Address;
import org.projectfloodlight.openflow.types.MacAddress;
import org.slf4j.Logger;
import net.floodlightcontroller.dhcpserver.DHCPBinding;
/**
* The class representing a DHCP Pool.
* This class is essentially a list of DHCPBinding objects containing IP, MAC, and lease status information.
*
* @author Ryan Izard (rizard@g.clemson.edu)
*/
public class DHCPPool {
protected Logger log;
private volatile static ArrayList<DHCPBinding> DHCP_POOL = new ArrayList<DHCPBinding>();
private volatile int POOL_SIZE;
private volatile int POOL_AVAILABILITY;
private volatile boolean POOL_FULL;
private volatile IPv4Address STARTING_ADDRESS;
private final MacAddress UNASSIGNED_MAC = MacAddress.NONE;
// Need to write this to handle subnets later...
// This assumes startingIPv4Address can handle size addresses
/**
* Constructor for a DHCPPool of DHCPBinding's. Each DHCPBinding object is initialized with a
* null MAC address and the lease is set to inactive (i.e. false).
* @param {@code byte[]} startingIPv4Address: The lowest IP address to lease.
* @param {@code integer} size: (startingIPv4Address + size) is the highest IP address to lease.
* @return none
*/
public DHCPPool(IPv4Address startingIPv4Address, int size, Logger log) {
this.log = log;
int IPv4AsInt = startingIPv4Address.getInt();
this.setPoolSize(size);
this.setPoolAvailability(size);
STARTING_ADDRESS = startingIPv4Address;
for (int i = 0; i < size; i++) {
DHCP_POOL.add(new DHCPBinding(IPv4Address.of(IPv4AsInt + i), UNASSIGNED_MAC));
}
}
private void setPoolFull(boolean full) {
POOL_FULL = full;
}
private boolean isPoolFull() {
return POOL_FULL;
}
private void setPoolSize(int size) {
POOL_SIZE = size;
}
private int getPoolSize() {
return POOL_SIZE;
}
private int getPoolAvailability() {
return POOL_AVAILABILITY;
}
private void setPoolAvailability(int size) {
POOL_AVAILABILITY = size;
}
/**
* Gets the DHCPBinding object from the DHCPPool containing {@code byte[]} ip
* @param {@code byte[]} ip: The IPv4 address to match in a DHCPBinding
* @return {@code DHCPBinding}: The matching DHCPBinding object or null if ip is not found
*/
public DHCPBinding getDHCPbindingFromIPv4(IPv4Address ip) {
if (ip == null) return null;
for (DHCPBinding binding : DHCP_POOL) {
if (binding.getIPv4Address().equals(ip)) {
return binding;
}
}
return null;
}
/**
* Gets the DHCPBinding object from the DHCPPool containing {@code byte[]} mac
* @param {@code byte[]} mac: The MAC address to match in in a DHCPBinding
* @return {@code DHCPBinding}: The matching DHCPBinding object or null if mac is not found
*/
public DHCPBinding getDHCPbindingFromMAC(MacAddress mac) {
if (mac == null) return null;
for (DHCPBinding binding : DHCP_POOL) {
if (binding.getMACAddress().equals(mac)) {
return binding;
}
}
return null;
}
/**
* Gets the lease status of a particular IPv4 address, {@code byte[]} ip
* @param {@code byte[]} ip: The IPv4 address of which to check the lease status
* @return {@code boolean}: true if lease is active, false if lease is inactive/expired
*/
public boolean isIPv4Leased(IPv4Address ip) {
DHCPBinding binding = this.getDHCPbindingFromIPv4(ip);
if (binding != null) return binding.isActiveLease();
else return false;
}
/**
* Assigns a MAC address to the IP address of the DHCPBinding object in the DHCPPool object.
* This method also sets the lease to active (i.e. true) when the assignment is made.
* @param {@code DHCPBinding} binding: The DHCPBinding object in which to set the MAC
* @param {@code byte[]} mac: The MAC address to set in the DHCPBinding object
* @param {@code long}: The time in seconds for which the lease will be valid
* @return none
*/
public void setDHCPbinding(DHCPBinding binding, MacAddress mac, int time) {
int index = DHCP_POOL.indexOf(binding);
binding.setMACAddress(mac);
binding.setLeaseStatus(true);
this.setPoolAvailability(this.getPoolAvailability() - 1);
DHCP_POOL.set(index, binding);
if (this.getPoolAvailability() == 0) setPoolFull(true);
binding.setLeaseStartTimeSeconds();
binding.setLeaseDurationSeconds(time);
}
/**
* Completely removes the DHCPBinding object with IP address {@code byte[]} ip from the DHCPPool
* @param {@code byte[]} ip: The IP address to remove from the pool. This address will not be available
* for lease after removal.
* @return none
*/
public void removeIPv4FromDHCPPool(IPv4Address ip) {
if (ip == null || getDHCPbindingFromIPv4(ip) == null) return;
if (ip.equals(STARTING_ADDRESS)) {
DHCPBinding lowest = null;
// Locate the lowest address (other than ip), which will be the new starting address
for (DHCPBinding binding : DHCP_POOL) {
if (lowest == null) {
lowest = binding;
} else if (binding.getIPv4Address().getInt() < lowest.getIPv4Address().getInt()
&& !binding.getIPv4Address().equals(ip))
{
lowest = binding;
}
}
// lowest is new starting address
STARTING_ADDRESS = lowest.getIPv4Address();
}
DHCP_POOL.remove(this.getDHCPbindingFromIPv4(ip));
this.setPoolSize(this.getPoolSize() - 1);
this.setPoolAvailability(this.getPoolAvailability() - 1);
if (this.getPoolAvailability() == 0) this.setPoolFull(true);
}
/**
* Adds an IP address to the DHCPPool if the address is not already present. If present, nothing is added to the DHCPPool.
* @param {@code byte[]} ip: The IP address to attempt to add to the DHCPPool
* @return {@code DHCPBinding}: Reference to the DHCPBinding object if successful, null if unsuccessful
*/
public DHCPBinding addIPv4ToDHCPPool(IPv4Address ip) {
DHCPBinding binding = null;
if (this.getDHCPbindingFromIPv4(ip) == null) {
if (ip.getInt() < STARTING_ADDRESS.getInt()) {
STARTING_ADDRESS = ip;
}
binding = new DHCPBinding(ip, null);
DHCP_POOL.add(binding);
this.setPoolSize(this.getPoolSize() + 1);
this.setPoolFull(false);
}
return binding;
}
/**
* Determines if there are available leases in this DHCPPool.
* @return {@code boolean}: true if there are addresses available, false if the DHCPPool is full
*/
public boolean hasAvailableAddresses() {
if (isPoolFull() || getPoolAvailability() == 0) return false;
else return true;
}
/**
* Returns an available address (DHCPBinding) for lease.
* If this MAC is configured for a static/fixed IP, that DHCPBinding will be returned.
* If this MAC has had a lease before and that same lease is available, that DHCPBinding will be returned.
* If not, then an attempt to return an address that has not been active before will be made.
* If there are no addresses that have not been used, then the lowest currently inactive address will be returned.
* If all addresses are being used, then null will be returned.
* @param {@code byte[]): MAC address of the device requesting the lease
* @return {@code DHCPBinding}: Reference to the DHCPBinding object if successful, null if unsuccessful
*/
public DHCPBinding getAnyAvailableLease(MacAddress mac) {
if (isPoolFull()) return null;
DHCPBinding usedBinding = null;
usedBinding = this.getDHCPbindingFromMAC(mac);
if (usedBinding != null) return usedBinding;
for (DHCPBinding binding : DHCP_POOL) {
if (!binding.isActiveLease()
&& binding.getMACAddress().equals(UNASSIGNED_MAC))
{
return binding;
} else if (!binding.isActiveLease() && usedBinding == null && !binding.isStaticIPLease()) {
usedBinding = binding;
}
}
return usedBinding;
}
/**
* Returns a specific available IP address binding for lease. The MAC and IP will be queried
* against the DHCP pool. (1) If the MAC is found in an available, fixed binding, and that binding
* is not for the provided IP, the fixed binding associated with the MAC will be returned. (2) If the
* IP is found in an available, fixed binding, and that binding also contains the MAC address provided,
* then the binding will be returned -- this is true only if the IP and MAC result in the same available,
* fixed binding. (3) If the IP is found in the pool and it is available and not fixed, then its
* binding will be returned. (4) If the IP provided does not match any available entries or is invalid,
* null will be returned. If this is the case, run getAnyAvailableLease(mac) to resolve.
* @param {@code byte[]}: The IP address on which to try and obtain a lease
* @param {@code byte[]}: The MAC address on which to try and obtain a lease.
* @return {@code DHCPBinding}: Reference to the DHCPBinding object if successful, null if unsuccessful.
*/
public DHCPBinding getSpecificAvailableLease(IPv4Address ip, MacAddress mac) {
if (ip == null || mac == null || isPoolFull()) return null;
DHCPBinding binding = this.getDHCPbindingFromIPv4(ip);
DHCPBinding binding2 = this.getDHCPbindingFromMAC(mac);
// For all of the following, the binding is also determined to be inactive:
// If configured, we must return a fixed binding for a MAC address even if it's requesting another IP
if (binding2 != null && !binding2.isActiveLease() && binding2.isStaticIPLease() && binding != binding2) {
if (log != null) log.info("Fixed DHCP entry for MAC trumps requested IP. Returning binding for MAC");
return binding2;
// If configured, we must return a fixed binding for an IP if the binding is fixed to the provided MAC (ideal static request case)
} else if (binding != null && !binding.isActiveLease() && binding.isStaticIPLease() && mac.equals(binding.getMACAddress())) {
if (log != null) log.info("Found matching fixed DHCP entry for IP with MAC. Returning binding for IP with MAC");
return binding;
// The IP and MAC are not a part of a fixed binding, so return the binding of the requested IP
} else if (binding != null && !binding.isActiveLease() && !binding.isStaticIPLease()) {
if (log != null) log.info("No fixed DHCP entry for IP or MAC found. Returning dynamic binding for IP.");
return binding;
// Otherwise, the binding is fixed for both MAC and IP and this MAC does not match either, so we can't return it as available
} else {
if (log != null) log.debug("Invalid IP address request or IP is actively leased...check for any available lease to resolve");
return null;
}
}
/**
* Tries to renew an IP lease.
* @param {@code byte[]}: The IP address on which to try and renew a lease
* @param {@code long}: The time in seconds for which the lease will be valid
* @return {@code DHCPBinding}: True on success, false if unknown IP address
*/
public boolean renewLease(IPv4Address ip, int time) {
DHCPBinding binding = this.getDHCPbindingFromIPv4(ip);
if (binding != null) {
binding.setLeaseStartTimeSeconds();
binding.setLeaseDurationSeconds(time);
binding.setLeaseStatus(true);
return true;
}
return false;
}
/**
* Cancel an IP lease.
* @param {@code byte[]}: The IP address on which to try and cancel a lease
* @return {@code boolean}: True on success, false if unknown IP address
*/
public boolean cancelLeaseOfIPv4(IPv4Address ip) {
DHCPBinding binding = this.getDHCPbindingFromIPv4(ip);
if (binding != null) {
binding.clearLeaseTimes();
binding.setLeaseStatus(false);
this.setPoolAvailability(this.getPoolAvailability() + 1);
this.setPoolFull(false);
return true;
}
return false;
}
/**
* Cancel an IP lease.
* @param {@code byte[]}: The MAC address on which to try and cancel a lease
* @return {@code boolean}: True on success, false if unknown IP address
*/
public boolean cancelLeaseOfMAC(MacAddress mac) {
DHCPBinding binding = getDHCPbindingFromMAC(mac);
if (binding != null) {
binding.clearLeaseTimes();
binding.setLeaseStatus(false);
this.setPoolAvailability(this.getPoolAvailability() + 1);
this.setPoolFull(false);
return true;
}
return false;
}
/**
* Make the addresses of expired leases available and reset the lease times.
* @return {@code ArrayList<DHCPBinding>}: A list of the bindings that are now available
*/
public ArrayList<DHCPBinding> cleanExpiredLeases() {
ArrayList<DHCPBinding> newAvailableLeases = new ArrayList<DHCPBinding>();
for (DHCPBinding binding : DHCP_POOL) {
// isLeaseExpired() automatically excludes configured static leases
if (binding.isLeaseExpired() && binding.isActiveLease()) {
this.cancelLeaseOfIPv4(binding.getIPv4Address());
this.setPoolAvailability(this.getPoolAvailability() + 1);
this.setPoolFull(false);
newAvailableLeases.add(binding);
}
}
return newAvailableLeases;
}
/**
* Used to set a particular IP binding in the pool as a fixed/static IP lease.
* This method does not set the lease as active, but instead reserves that IP
* for only the MAC provided. To set the lease as active, the methods getAnyAvailableLease()
* or getSpecificAvailableLease() will return the correct binding given the same
* MAC provided to this method is used to bind the lease later on.
* @param {@code byte[]}: The IP address to set as static/fixed.
* @param {@code byte[]}: The MAC address to match to the IP address ip when
* an address is requested from the MAC mac
* @return {@code boolean}: True upon success; false upon failure (e.g. no IP found)
*/
public boolean configureFixedIPLease(IPv4Address ip, MacAddress mac) {
DHCPBinding binding = this.getDHCPbindingFromIPv4(ip);
if (binding != null) {
binding.setMACAddress(mac);
binding.setStaticIPLease(true);
binding.setLeaseStatus(false);
return true;
} else {
return false;
}
}
}
\ No newline at end of file
This diff is collapsed.
package net.floodlightcontroller.dhcpserver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import org.projectfloodlight.openflow.protocol.OFFlowAdd;
import org.projectfloodlight.openflow.protocol.OFPortDesc;
import org.projectfloodlight.openflow.protocol.action.OFAction;
import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
import org.projectfloodlight.openflow.protocol.match.Match;
import org.projectfloodlight.openflow.protocol.match.MatchField;
import org.projectfloodlight.openflow.types.DatapathId;
import org.projectfloodlight.openflow.types.EthType;
import org.projectfloodlight.openflow.types.IpProtocol;
import org.projectfloodlight.openflow.types.OFBufferId;
import org.projectfloodlight.openflow.types.OFPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.IOFSwitch;
import net.floodlightcontroller.core.IOFSwitchListener;
import net.floodlightcontroller.core.PortChangeType;
import net.floodlightcontroller.core.internal.IOFSwitchService;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
import net.floodlightcontroller.packet.UDP;
import net.floodlightcontroller.staticflowentry.IStaticFlowEntryPusherService;
// Adding a comment to test a new commit
public class DHCPSwitchFlowSetter implements IFloodlightModule, IOFSwitchListener {
protected static Logger log;
protected IFloodlightProviderService floodlightProvider;
protected IStaticFlowEntryPusherService sfp;
protected IOFSwitchService switchService;
@Override
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
return null;
}
@Override
public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
return null;
}
@Override
public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Collection<Class<? extends IFloodlightService>> l =
new ArrayList<Class<? extends IFloodlightService>>();
l.add(IFloodlightProviderService.class);
l.add(IStaticFlowEntryPusherService.class);
l.add(IOFSwitchService.class);
return l;
}
@Override
public void init(FloodlightModuleContext context)
throws FloodlightModuleException {
floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
log = LoggerFactory.getLogger(DHCPServer.class);
sfp = context.getServiceImpl(IStaticFlowEntryPusherService.class);
switchService = context.getServiceImpl(IOFSwitchService.class);
}
@Override
public void startUp(FloodlightModuleContext context) {
}
@Override
public void switchAdded(DatapathId dpid) {
/* Insert static flows on all ports of the switch to redirect
* DHCP client --> DHCP DHCPServer traffic to the controller.
* DHCP client's operate on UDP port 67
*/
IOFSwitch sw = switchService.getSwitch(dpid);
OFFlowAdd.Builder flow = sw.getOFFactory().buildFlowAdd();
Match.Builder match = sw.getOFFactory().buildMatch();
ArrayList<OFAction> actionList = new ArrayList<OFAction>();
OFActionOutput.Builder action = sw.getOFFactory().actions().buildOutput();
for (OFPortDesc port : sw.getPorts()) {
match.setExact(MatchField.IN_PORT, port.getPortNo());
match.setExact(MatchField.ETH_TYPE, EthType.IPv4);
match.setExact(MatchField.IP_PROTO, IpProtocol.UDP);
match.setExact(MatchField.UDP_SRC, UDP.DHCP_CLIENT_PORT);
action.setMaxLen(0xffFFffFF);
action.setPort(OFPort.CONTROLLER);
actionList.add(action.build());
flow.setBufferId(OFBufferId.NO_BUFFER);
flow.setHardTimeout(0);
flow.setIdleTimeout(0);
flow.setOutPort(OFPort.CONTROLLER);
flow.setActions(actionList);
flow.setMatch(match.build());
flow.setPriority(32767);
sfp.addFlow("dhcp-port---" + port.getPortNo().getPortNumber() + "---(" + port.getName() + ")", flow.build(), sw.getId());
}
}
@Override
public void switchRemoved(DatapathId switchId) {
}
@Override
public void switchActivated(DatapathId switchId) {
}
@Override
public void switchPortChanged(DatapathId switchId, OFPortDesc port, PortChangeType type) {
}
@Override
public void switchChanged(DatapathId switchId) {
}
}
\ No newline at end of file
......@@ -23,6 +23,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.projectfloodlight.openflow.types.IPv4Address;
import org.projectfloodlight.openflow.types.MacAddress;
/**
*
* @author David Erickson (daviderickson@cs.stanford.edu)
......@@ -92,11 +95,11 @@ public class DHCP extends BasePacket {
protected int transactionId;
protected short seconds;
protected short flags;
protected int clientIPAddress;
protected int yourIPAddress;
protected int serverIPAddress;
protected int gatewayIPAddress;
protected byte[] clientHardwareAddress;
protected IPv4Address clientIPAddress;
protected IPv4Address yourIPAddress;
protected IPv4Address serverIPAddress;
protected IPv4Address gatewayIPAddress;
protected MacAddress clientHardwareAddress;
protected String serverName;
protected String bootFileName;
protected List<DHCPOption> options = new ArrayList<DHCPOption>();
......@@ -209,14 +212,14 @@ public class DHCP extends BasePacket {
/**
* @return the clientIPAddress
*/
public int getClientIPAddress() {
public IPv4Address getClientIPAddress() {
return clientIPAddress;
}
/**
* @param clientIPAddress the clientIPAddress to set
*/
public DHCP setClientIPAddress(int clientIPAddress) {
public DHCP setClientIPAddress(IPv4Address clientIPAddress) {
this.clientIPAddress = clientIPAddress;
return this;
}
......@@ -224,14 +227,14 @@ public class DHCP extends BasePacket {
/**
* @return the yourIPAddress
*/
public int getYourIPAddress() {
public IPv4Address getYourIPAddress() {
return yourIPAddress;
}
/**
* @param yourIPAddress the yourIPAddress to set
*/
public DHCP setYourIPAddress(int yourIPAddress) {
public DHCP setYourIPAddress(IPv4Address yourIPAddress) {
this.yourIPAddress = yourIPAddress;
return this;
}
......@@ -239,14 +242,14 @@ public class DHCP extends BasePacket {
/**
* @return the serverIPAddress
*/
public int getServerIPAddress() {
public IPv4Address getServerIPAddress() {
return serverIPAddress;
}
/**
* @param serverIPAddress the serverIPAddress to set
*/
public DHCP setServerIPAddress(int serverIPAddress) {
public DHCP setServerIPAddress(IPv4Address serverIPAddress) {
this.serverIPAddress = serverIPAddress;
return this;
}
......@@ -254,14 +257,14 @@ public class DHCP extends BasePacket {
/**
* @return the gatewayIPAddress
*/
public int getGatewayIPAddress() {
public IPv4Address getGatewayIPAddress() {
return gatewayIPAddress;
}
/**
* @param gatewayIPAddress the gatewayIPAddress to set
*/
public DHCP setGatewayIPAddress(int gatewayIPAddress) {
public DHCP setGatewayIPAddress(IPv4Address gatewayIPAddress) {
this.gatewayIPAddress = gatewayIPAddress;
return this;
}
......@@ -269,14 +272,14 @@ public class DHCP extends BasePacket {
/**
* @return the clientHardwareAddress
*/
public byte[] getClientHardwareAddress() {
public MacAddress getClientHardwareAddress() {
return clientHardwareAddress;
}
/**
* @param clientHardwareAddress the clientHardwareAddress to set
*/
public DHCP setClientHardwareAddress(byte[] clientHardwareAddress) {
public DHCP setClientHardwareAddress(MacAddress clientHardwareAddress) {
this.clientHardwareAddress = clientHardwareAddress;
return this;
}
......@@ -381,13 +384,13 @@ public class DHCP extends BasePacket {
bb.putInt(this.transactionId);
bb.putShort(this.seconds);
bb.putShort(this.flags);
bb.putInt(this.clientIPAddress);
bb.putInt(this.yourIPAddress);
bb.putInt(this.serverIPAddress);
bb.putInt(this.gatewayIPAddress);
bb.put(this.clientHardwareAddress);
if (this.clientHardwareAddress.length < 16) {
for (int i = 0; i < (16 - this.clientHardwareAddress.length); ++i) {
bb.putInt(this.clientIPAddress.getInt());
bb.putInt(this.yourIPAddress.getInt());
bb.putInt(this.serverIPAddress.getInt());
bb.putInt(this.gatewayIPAddress.getInt());
bb.put(this.clientHardwareAddress.getBytes());
if (this.clientHardwareAddress.getLength() < 16) {
for (int i = 0; i < (16 - this.clientHardwareAddress.getLength()); ++i) {
bb.put((byte) 0x0);
}
}
......@@ -447,14 +450,14 @@ public class DHCP extends BasePacket {
this.transactionId = bb.getInt();
this.seconds = bb.getShort();
this.flags = bb.getShort();
this.clientIPAddress = bb.getInt();
this.yourIPAddress = bb.getInt();
this.serverIPAddress = bb.getInt();
this.gatewayIPAddress = bb.getInt();
this.clientIPAddress = IPv4Address.of(bb.getInt());
this.yourIPAddress = IPv4Address.of(bb.getInt());
this.serverIPAddress = IPv4Address.of(bb.getInt());
this.gatewayIPAddress = IPv4Address.of(bb.getInt());
int hardwareAddressLength = 0xff & this.hardwareAddressLength;
this.clientHardwareAddress = new byte[hardwareAddressLength];
bb.get(this.clientHardwareAddress);
byte[] tmpMac = new byte[hardwareAddressLength];
bb.get(tmpMac);
this.clientHardwareAddress = MacAddress.of(tmpMac); /* the assumption here is that we only have MAC address HW addresses */
for (int i = hardwareAddressLength; i < 16; ++i)
bb.get();
this.serverName = readString(bb, 64);
......
......@@ -27,6 +27,7 @@ import org.projectfloodlight.openflow.protocol.OFPacketInReason;
import org.projectfloodlight.openflow.protocol.OFVersion;
import org.projectfloodlight.openflow.protocol.match.MatchField;
import org.projectfloodlight.openflow.types.EthType;
import org.projectfloodlight.openflow.types.IPv4Address;
import org.projectfloodlight.openflow.types.IpProtocol;
import org.projectfloodlight.openflow.types.MacAddress;
import org.projectfloodlight.openflow.types.OFPort;
......@@ -163,11 +164,11 @@ public class PacketFactory {
.setTransactionId(0x00003d1d)
.setSeconds((short)0)
.setFlags((short)0)
.setClientIPAddress(0)
.setYourIPAddress(0)
.setServerIPAddress(0)
.setGatewayIPAddress(0)
.setClientHardwareAddress(hostMac.getBytes())
.setClientIPAddress(IPv4Address.NONE)
.setYourIPAddress(IPv4Address.NONE)
.setServerIPAddress(IPv4Address.NONE)
.setGatewayIPAddress(IPv4Address.NONE)
.setClientHardwareAddress(hostMac)
.setOptions(optionList))));
return requestPacket;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment