Skip to content
Snippets Groups Projects
Commit 16a8c079 authored by Gregor Maier's avatar Gregor Maier
Browse files

Merge pull request #229 from maiergre/addrspace

Some smaller updates (IDevice, IPacket, ForwardingBase)
parents e2ff3bf5 d5f7b440
No related branches found
No related tags found
No related merge requests found
Showing
with 317 additions and 59 deletions
...@@ -73,6 +73,14 @@ public interface IDevice { ...@@ -73,6 +73,14 @@ public interface IDevice {
* @return an array containing all unique attachment points for the device * @return an array containing all unique attachment points for the device
*/ */
public SwitchPort[] getAttachmentPoints(boolean includeError); public SwitchPort[] getAttachmentPoints(boolean includeError);
/**
* Returns all unique VLAN IDs for the device that were observed on
* the given switch port
* @param swp the switch port to query
* @return an array containing the unique VLAN IDs
*/
public Short[] getSwitchPortVlanIds(SwitchPort swp);
/** /**
* Get the most recent timestamp for this device * Get the most recent timestamp for this device
......
...@@ -33,6 +33,7 @@ import net.floodlightcontroller.devicemanager.IDevice; ...@@ -33,6 +33,7 @@ import net.floodlightcontroller.devicemanager.IDevice;
import net.floodlightcontroller.devicemanager.IEntityClass; import net.floodlightcontroller.devicemanager.IEntityClass;
import net.floodlightcontroller.devicemanager.SwitchPort; import net.floodlightcontroller.devicemanager.SwitchPort;
import static net.floodlightcontroller.devicemanager.SwitchPort.ErrorStatus.*; import static net.floodlightcontroller.devicemanager.SwitchPort.ErrorStatus.*;
import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.topology.ITopologyService; import net.floodlightcontroller.topology.ITopologyService;
/** /**
...@@ -342,6 +343,21 @@ public class Device implements IDevice { ...@@ -342,6 +343,21 @@ public class Device implements IDevice {
return vals.toArray(new SwitchPort[vals.size()]); return vals.toArray(new SwitchPort[vals.size()]);
} }
@Override
public Short[] getSwitchPortVlanIds(SwitchPort swp) {
TreeSet<Short> vals = new TreeSet<Short>();
for (Entity e : entities) {
if (e.switchDPID == swp.getSwitchDPID()
&& e.switchPort == swp.getPort()) {
if (e.getVlan() == null)
vals.add(Ethernet.VLAN_UNTAGGED);
else
vals.add(e.getVlan());
}
}
return vals.toArray(new Short[vals.size()]);
}
@Override @Override
public Date getLastSeen() { public Date getLastSeen() {
......
...@@ -213,7 +213,7 @@ public class Entity implements Comparable<Entity> { ...@@ -213,7 +213,7 @@ public class Entity implements Comparable<Entity> {
@Override @Override
public String toString() { public String toString() {
return "Entity [macAddress=" + HexString.toHexString(macAddress) return "Entity [macAddress=" + HexString.toHexString(macAddress, 6)
+ ", ipv4Address=" + ", ipv4Address="
+ IPv4.fromIPv4Address(ipv4Address==null ? 0 : ipv4Address.intValue()) + ", vlan=" + vlan + ", switchDPID=" + IPv4.fromIPv4Address(ipv4Address==null ? 0 : ipv4Address.intValue()) + ", vlan=" + vlan + ", switchDPID="
+ switchDPID + ", switchPort=" + switchPort + "]"; + switchDPID + ", switchPort=" + switchPort + "]";
......
...@@ -291,11 +291,11 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule { ...@@ -291,11 +291,11 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
@Override @Override
public void init(FloodlightModuleContext context) throws FloodlightModuleException { public void init(FloodlightModuleContext context) throws FloodlightModuleException {
this.setFloodlightProvider(context.getServiceImpl(IFloodlightProviderService.class)); this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
this.setDeviceManager(context.getServiceImpl(IDeviceService.class)); this.deviceManager = context.getServiceImpl(IDeviceService.class);
this.setRoutingEngine(context.getServiceImpl(IRoutingService.class)); this.routingEngine = context.getServiceImpl(IRoutingService.class);
this.setTopology(context.getServiceImpl(ITopologyService.class)); this.topology = context.getServiceImpl(ITopologyService.class);
this.setCounterStore(context.getServiceImpl(ICounterStoreService.class)); this.counterStore = context.getServiceImpl(ICounterStoreService.class);
} }
@Override @Override
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package net.floodlightcontroller.packet; package net.floodlightcontroller.packet;
/** /**
* *
* @author David Erickson (daviderickson@cs.stanford.edu) * @author David Erickson (daviderickson@cs.stanford.edu)
...@@ -85,4 +86,21 @@ public abstract class BasePacket implements IPacket { ...@@ -85,4 +86,21 @@ public abstract class BasePacket implements IPacket {
return false; return false;
return true; return true;
} }
}
@Override
public Object clone() {
IPacket pkt;
try {
pkt = this.getClass().newInstance();
} catch (Exception e) {
throw new RuntimeException("Could not clone packet");
}
// TODO: we are using serialize()/deserialize() to perform the
// cloning. Not the most efficient way but simple. We can revisit
// if we hit performance problems.
byte[] data = this.serialize();
pkt.deserialize(this.serialize(), 0, data.length);
pkt.setParent(this.parent);
return pkt;
}
}
\ No newline at end of file
...@@ -63,4 +63,10 @@ public interface IPacket { ...@@ -63,4 +63,10 @@ public interface IPacket {
* @return the deserialized data * @return the deserialized data
*/ */
public IPacket deserialize(byte[] data, int offset, int length); public IPacket deserialize(byte[] data, int offset, int length);
/** Clone this packet and its payload packet but not its parent.
*
* @return
*/
public Object clone();
} }
...@@ -20,9 +20,9 @@ package net.floodlightcontroller.routing; ...@@ -20,9 +20,9 @@ package net.floodlightcontroller.routing;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
import net.floodlightcontroller.core.FloodlightContext; import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService; import net.floodlightcontroller.core.IFloodlightProviderService;
...@@ -35,6 +35,7 @@ import net.floodlightcontroller.devicemanager.IDeviceListener; ...@@ -35,6 +35,7 @@ import net.floodlightcontroller.devicemanager.IDeviceListener;
import net.floodlightcontroller.devicemanager.IDeviceService; import net.floodlightcontroller.devicemanager.IDeviceService;
import net.floodlightcontroller.devicemanager.SwitchPort; import net.floodlightcontroller.devicemanager.SwitchPort;
import net.floodlightcontroller.packet.Ethernet; import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.packet.IPacket;
import net.floodlightcontroller.routing.IRoutingService; import net.floodlightcontroller.routing.IRoutingService;
import net.floodlightcontroller.routing.IRoutingDecision; import net.floodlightcontroller.routing.IRoutingDecision;
import net.floodlightcontroller.routing.Route; import net.floodlightcontroller.routing.Route;
...@@ -178,6 +179,7 @@ public abstract class ForwardingBase implements ...@@ -178,6 +179,7 @@ public abstract class ForwardingBase implements
(OFFlowMod) floodlightProvider.getOFMessageFactory() (OFFlowMod) floodlightProvider.getOFMessageFactory()
.getMessage(OFType.FLOW_MOD); .getMessage(OFType.FLOW_MOD);
OFActionOutput action = new OFActionOutput(); OFActionOutput action = new OFActionOutput();
action.setMaxLength((short)0xffff);
List<OFAction> actions = new ArrayList<OFAction>(); List<OFAction> actions = new ArrayList<OFAction>();
actions.add(action); actions.add(action);
...@@ -208,9 +210,6 @@ public abstract class ForwardingBase implements ...@@ -208,9 +210,6 @@ public abstract class ForwardingBase implements
// set buffer id if it is the source switch // set buffer id if it is the source switch
if (1 == indx) { if (1 == indx) {
//fm.setMatch(match);
fm.setBufferId(bufferId);
//fm.setMatch(wildcard(match, sw, wildcard_hints));
// Set the flag to request flow-mod removal notifications only for the // Set the flag to request flow-mod removal notifications only for the
// source switch. The removal message is used to maintain the flow // source switch. The removal message is used to maintain the flow
// cache. Don't set the flag for ARP messages - TODO generalize check // cache. Don't set the flag for ARP messages - TODO generalize check
...@@ -244,6 +243,8 @@ public abstract class ForwardingBase implements ...@@ -244,6 +243,8 @@ public abstract class ForwardingBase implements
// Push the packet out the source switch // Push the packet out the source switch
if (sw.getId() == pinSwitch) { if (sw.getId() == pinSwitch) {
// TODO: Instead of doing a packetOut here we could also
// send a flowMod with bufferId set....
pushPacket(sw, match, pi, outPort, cntx); pushPacket(sw, match, pi, outPort, cntx);
srcSwitchIncluded = true; srcSwitchIncluded = true;
} }
...@@ -268,6 +269,72 @@ public abstract class ForwardingBase implements ...@@ -268,6 +269,72 @@ public abstract class ForwardingBase implements
} }
return match.clone(); return match.clone();
} }
/**
* Pushes a packet-out to a switch. If bufferId != BUFFER_ID_NONE we
* assume that the packetOut switch is the same as the packetIn switch
* and we will use the bufferId
* Caller needs to make sure that inPort and outPort differs
* @param packet packet data to send
* @param sw switch from which packet-out is sent
* @param bufferId bufferId
* @param inPort input port
* @param outPort output port
* @param cntx context of the packet
*/
public void pushPacket(IPacket packet,
IOFSwitch sw,
int bufferId,
short inPort,
short outPort,
FloodlightContext cntx) {
if (log.isTraceEnabled()) {
log.trace("PacketOut srcSwitch={} inPort={} outPort={}",
new Object[] {sw, inPort, outPort});
}
OFPacketOut po =
(OFPacketOut) floodlightProvider.getOFMessageFactory()
.getMessage(OFType.PACKET_OUT);
// set actions
List<OFAction> actions = new ArrayList<OFAction>();
actions.add(new OFActionOutput(outPort, (short) 0xffff));
po.setActions(actions)
.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH);
short poLength =
(short) (po.getActionsLength() + OFPacketOut.MINIMUM_LENGTH);
// set buffer_id, in_port
po.setBufferId(bufferId);
po.setInPort(inPort);
// set data - only if buffer_id == -1
if (po.getBufferId() == OFPacketOut.BUFFER_ID_NONE) {
if (packet == null) {
log.error("BufferId is set but no packet data is null. " +
"Cannot send packetOut. " +
"srcSwitch={} inPort={} outPort={}",
new Object[] {sw, inPort, outPort});
return;
}
byte[] packetData = packet.serialize();
poLength += packetData.length;
po.setPacketData(packetData);
}
po.setLength(poLength);
try {
counterStore.updatePktOutFMCounterStore(sw, po);
sw.write(po, cntx);
} catch (IOException e) {
log.error("Failure writing packet out", e);
}
}
/** /**
* Pushes a packet-out to a switch. The assumption here is that * Pushes a packet-out to a switch. The assumption here is that
...@@ -311,7 +378,7 @@ public abstract class ForwardingBase implements ...@@ -311,7 +378,7 @@ public abstract class ForwardingBase implements
// set actions // set actions
List<OFAction> actions = new ArrayList<OFAction>(); List<OFAction> actions = new ArrayList<OFAction>();
actions.add(new OFActionOutput(outport, (short) 0)); actions.add(new OFActionOutput(outport, (short) 0xffff));
po.setActions(actions) po.setActions(actions)
.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH); .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH);
...@@ -339,20 +406,20 @@ public abstract class ForwardingBase implements ...@@ -339,20 +406,20 @@ public abstract class ForwardingBase implements
} }
} }
/** /**
* Write packetout message to sw with output actions to one or more * Write packetout message to sw with output actions to one or more
* output ports with inPort/outPorts passed in. * output ports with inPort/outPorts passed in.
* Note that the packet in could be from a different switch. * @param packetData
* @param pi
* @param sw * @param sw
* @param inPort * @param inPort
* @param ports * @param ports
* @param cntx * @param cntx
*/ */
public void PacketOutMultiPort(OFPacketIn pi, public void packetOutMultiPort(byte[] packetData,
IOFSwitch sw, IOFSwitch sw,
short inPort, short inPort,
HashSet<Integer> outPorts, Set<Integer> outPorts,
FloodlightContext cntx) { FloodlightContext cntx) {
//setting actions //setting actions
List<OFAction> actions = new ArrayList<OFAction>(); List<OFAction> actions = new ArrayList<OFAction>();
...@@ -379,7 +446,6 @@ public abstract class ForwardingBase implements ...@@ -379,7 +446,6 @@ public abstract class ForwardingBase implements
// data (note buffer_id is always BUFFER_ID_NONE) and length // data (note buffer_id is always BUFFER_ID_NONE) and length
short poLength = (short)(po.getActionsLength() + short poLength = (short)(po.getActionsLength() +
OFPacketOut.MINIMUM_LENGTH); OFPacketOut.MINIMUM_LENGTH);
byte[] packetData = pi.getPacketData();
poLength += packetData.length; poLength += packetData.length;
po.setPacketData(packetData); po.setPacketData(packetData);
po.setLength(poLength); po.setLength(poLength);
...@@ -388,8 +454,8 @@ public abstract class ForwardingBase implements ...@@ -388,8 +454,8 @@ public abstract class ForwardingBase implements
counterStore.updatePktOutFMCounterStore(sw, po); counterStore.updatePktOutFMCounterStore(sw, po);
if (log.isTraceEnabled()) { if (log.isTraceEnabled()) {
log.trace("write broadcast packet on switch-id={} " + log.trace("write broadcast packet on switch-id={} " +
"interaces={} packet-in={} packet-out={}", "interfaces={} packet-out={}",
new Object[] {sw.getId(), outPorts, pi, po}); new Object[] {sw.getId(), outPorts, po});
} }
sw.write(po, cntx); sw.write(po, cntx);
...@@ -397,6 +463,32 @@ public abstract class ForwardingBase implements ...@@ -397,6 +463,32 @@ public abstract class ForwardingBase implements
log.error("Failure writing packet out", e); log.error("Failure writing packet out", e);
} }
} }
/**
* @see packetOutMultiPort
* Accepts a PacketIn instead of raw packet data. Note that the inPort
* and switch can be different than the packet in switch/port
*/
public void packetOutMultiPort(OFPacketIn pi,
IOFSwitch sw,
short inPort,
Set<Integer> outPorts,
FloodlightContext cntx) {
packetOutMultiPort(pi.getPacketData(), sw, inPort, outPorts, cntx);
}
/**
* @see packetOutMultiPort
* Accepts an IPacket instead of raw packet data. Note that the inPort
* and switch can be different than the packet in switch/port
*/
public void packetOutMultiPort(IPacket packet,
IOFSwitch sw,
short inPort,
Set<Integer> outPorts,
FloodlightContext cntx) {
packetOutMultiPort(packet.serialize(), sw, inPort, outPorts, cntx);
}
protected boolean isInBroadcastCache(IOFSwitch sw, OFPacketIn pi, protected boolean isInBroadcastCache(IOFSwitch sw, OFPacketIn pi,
FloodlightContext cntx) { FloodlightContext cntx) {
...@@ -486,40 +578,6 @@ public abstract class ForwardingBase implements ...@@ -486,40 +578,6 @@ public abstract class ForwardingBase implements
} }
/**
* @param floodlightProvider the floodlightProvider to set
*/
public void setFloodlightProvider(IFloodlightProviderService floodlightProvider) {
this.floodlightProvider = floodlightProvider;
}
/**
* @param routingEngine the routingEngine to set
*/
public void setRoutingEngine(IRoutingService routingEngine) {
this.routingEngine = routingEngine;
}
/**
* @param deviceManager
* the deviceManager to set
*/
public void setDeviceManager(IDeviceService deviceManager) {
this.deviceManager = deviceManager;
}
/**
* @param topology
* the topology to set
*/
public void setTopology(ITopologyService topology) {
this.topology = topology;
}
public void setCounterStore(ICounterStoreService counterStore) {
this.counterStore = counterStore;
}
@Override @Override
public void deviceAdded(IDevice device) { public void deviceAdded(IDevice device) {
// NOOP // NOOP
......
...@@ -18,4 +18,4 @@ net.floodlightcontroller.counter.NullCounterStore ...@@ -18,4 +18,4 @@ net.floodlightcontroller.counter.NullCounterStore
net.floodlightcontroller.threadpool.ThreadPool net.floodlightcontroller.threadpool.ThreadPool
net.floodlightcontroller.ui.web.StaticWebRoutable net.floodlightcontroller.ui.web.StaticWebRoutable
net.floodlightcontroller.virtualnetwork.VirtualNetworkFilter net.floodlightcontroller.virtualnetwork.VirtualNetworkFilter
net.floodlightcontroller.devicemanager.internal.DefaultEntityClassifier net.floodlightcontroller.devicemanager.internal.DefaultEntityClassifier
\ No newline at end of file
...@@ -1380,4 +1380,30 @@ public class DeviceManagerImplTest extends FloodlightTestCase { ...@@ -1380,4 +1380,30 @@ public class DeviceManagerImplTest extends FloodlightTestCase {
Arrays.sort(ips); Arrays.sort(ips);
assertArrayEquals(new Integer[] { 2, 42, 4242 }, ips); assertArrayEquals(new Integer[] { 2, 42, 4242 }, ips);
} }
// TODO: this test should really go into a separate class that collects
// unit tests for Device
@Test
public void testGetSwitchPortVlanId() {
Entity entity1 = new Entity(1L, (short)1, null, 10L, 1, new Date());
Entity entity2 = new Entity(1L, null, null, 10L, 1, new Date());
Entity entity3 = new Entity(1L, (short)3, null, 1L, 1, new Date());
Entity entity4 = new Entity(1L, (short)42, null, 1L, 1, new Date());
Entity[] entities = new Entity[] { entity1, entity2,
entity3, entity4
};
Device d = new Device(null,1L, Arrays.asList(entities), null);
SwitchPort swp1x1 = new SwitchPort(1L, 1);
SwitchPort swp1x2 = new SwitchPort(1L, 2);
SwitchPort swp2x1 = new SwitchPort(2L, 1);
SwitchPort swp10x1 = new SwitchPort(10L, 1);
assertArrayEquals(new Short[] { -1, 1},
d.getSwitchPortVlanIds(swp10x1));
assertArrayEquals(new Short[] { 3, 42},
d.getSwitchPortVlanIds(swp1x1));
assertArrayEquals(new Short[0],
d.getSwitchPortVlanIds(swp1x2));
assertArrayEquals(new Short[0],
d.getSwitchPortVlanIds(swp2x1));
}
} }
...@@ -215,7 +215,7 @@ public class ForwardingTest extends FloodlightTestCase { ...@@ -215,7 +215,7 @@ public class ForwardingTest extends FloodlightTestCase {
packetOut.setBufferId(this.packetIn.getBufferId()) packetOut.setBufferId(this.packetIn.getBufferId())
.setInPort(this.packetIn.getInPort()); .setInPort(this.packetIn.getInPort());
List<OFAction> poactions = new ArrayList<OFAction>(); List<OFAction> poactions = new ArrayList<OFAction>();
poactions.add(new OFActionOutput((short) 3, (short) 0)); poactions.add(new OFActionOutput((short) 3, (short) 0xffff));
packetOut.setActions(poactions) packetOut.setActions(poactions)
.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH) .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH)
.setPacketData(testPacketSerialized) .setPacketData(testPacketSerialized)
...@@ -274,7 +274,7 @@ public class ForwardingTest extends FloodlightTestCase { ...@@ -274,7 +274,7 @@ public class ForwardingTest extends FloodlightTestCase {
// Expected Flow-mods // Expected Flow-mods
OFMatch match = new OFMatch(); OFMatch match = new OFMatch();
match.loadFromPacket(testPacketSerialized, (short) 1); match.loadFromPacket(testPacketSerialized, (short) 1);
OFActionOutput action = new OFActionOutput((short)3, (short)0); OFActionOutput action = new OFActionOutput((short)3, (short)0xffff);
List<OFAction> actions = new ArrayList<OFAction>(); List<OFAction> actions = new ArrayList<OFAction>();
actions.add(action); actions.add(action);
...@@ -310,9 +310,9 @@ public class ForwardingTest extends FloodlightTestCase { ...@@ -310,9 +310,9 @@ public class ForwardingTest extends FloodlightTestCase {
for (OFMessage m: msglist) { for (OFMessage m: msglist) {
if (m instanceof OFFlowMod) if (m instanceof OFFlowMod)
assertTrue(m.equals(fm1)); assertEquals(fm1, m);
else if (m instanceof OFPacketOut) else if (m instanceof OFPacketOut)
assertTrue(m.equals(packetOut)); assertEquals(packetOut, m);
} }
OFMessage m = wc2.getValue(); OFMessage m = wc2.getValue();
...@@ -356,7 +356,7 @@ public class ForwardingTest extends FloodlightTestCase { ...@@ -356,7 +356,7 @@ public class ForwardingTest extends FloodlightTestCase {
// Expected Flow-mods // Expected Flow-mods
OFMatch match = new OFMatch(); OFMatch match = new OFMatch();
match.loadFromPacket(testPacketSerialized, (short) 1); match.loadFromPacket(testPacketSerialized, (short) 1);
OFActionOutput action = new OFActionOutput((short)3, (short)0); OFActionOutput action = new OFActionOutput((short)3, (short)0xffff);
List<OFAction> actions = new ArrayList<OFAction>(); List<OFAction> actions = new ArrayList<OFAction>();
actions.add(action); actions.add(action);
......
package net.floodlightcontroller.packet;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class PacketTest {
protected IPacket pkt1, pkt2, pkt3, pkt4;
protected IPacket dummyPkt;
protected IPacket[] packets;
@Before
public void setUp() {
this.pkt1 = new Ethernet()
.setDestinationMACAddress("00:11:22:33:44:55")
.setSourceMACAddress("00:44:33:22:11:00")
.setEtherType(Ethernet.TYPE_IPv4)
.setPayload(
new IPv4()
.setTtl((byte) 128)
.setSourceAddress("192.168.1.1")
.setDestinationAddress("192.168.1.2")
.setPayload(new UDP()
.setSourcePort((short) 5000)
.setDestinationPort((short) 5001)
.setPayload(new Data(new byte[] {0x01}))));
this.pkt2 = new Ethernet()
.setSourceMACAddress("00:44:33:22:11:01")
.setDestinationMACAddress("00:11:22:33:44:55")
.setEtherType(Ethernet.TYPE_ARP)
.setVlanID((short)5)
.setPayload(
new ARP()
.setHardwareType(ARP.HW_TYPE_ETHERNET)
.setProtocolType(ARP.PROTO_TYPE_IP)
.setHardwareAddressLength((byte) 6)
.setProtocolAddressLength((byte) 4)
.setOpCode(ARP.OP_REPLY)
.setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:01"))
.setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1"))
.setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55"))
.setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2")));
this.pkt3 = new Ethernet()
.setSourceMACAddress("00:44:33:22:11:01")
.setDestinationMACAddress("00:11:22:33:44:55")
.setEtherType(Ethernet.TYPE_ARP)
.setPayload(
new ARP()
.setHardwareType(ARP.HW_TYPE_ETHERNET)
.setProtocolType(ARP.PROTO_TYPE_IP)
.setHardwareAddressLength((byte) 6)
.setProtocolAddressLength((byte) 4)
.setOpCode(ARP.OP_REPLY)
.setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:01"))
.setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1"))
.setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55"))
.setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2")));
this.pkt4 = new Ethernet()
.setDestinationMACAddress("FF:FF:FF:FF:FF:FF")
.setSourceMACAddress("00:11:33:55:77:01")
.setEtherType(Ethernet.TYPE_IPv4)
.setPayload(
new IPv4()
.setTtl((byte) 128)
.setSourceAddress("192.168.10.1")
.setDestinationAddress("192.168.255.255")
.setPayload(new UDP()
.setSourcePort((short) 5000)
.setDestinationPort((short) 5001)
.setPayload(new Data(new byte[] {0x01}))));
this.dummyPkt = new IPv4()
.setTtl((byte) 32)
.setSourceAddress("1.2.3.4")
.setDestinationAddress("5.6.7.8");
this.packets = new IPacket[] { pkt1, pkt2, pkt3, pkt4 };
}
protected void doTestClone(IPacket pkt) {
if (pkt.getPayload() != null)
doTestClone(pkt.getPayload());
IPacket newPkt = (IPacket)pkt.clone();
assertSame(pkt.getClass(), newPkt.getClass());
assertNotSame(pkt, newPkt);
assertSame(pkt.getParent(), newPkt.getParent());
assertEquals(pkt, newPkt);
assertEquals(pkt.getPayload(), newPkt.getPayload());
if (pkt.getPayload() != null)
assertNotSame(pkt.getPayload(), newPkt.getPayload());
if (pkt instanceof Ethernet) {
Ethernet eth = (Ethernet)pkt;
Ethernet newEth = (Ethernet)newPkt;
newEth.setDestinationMACAddress(new byte[] { 1,2,3,4,5,6});
assertEquals(false, newEth.getDestinationMAC()
.equals(eth.getDestinationMAC()));
assertEquals(false, newPkt.equals(pkt));
}
if (pkt instanceof ARP) {
ARP arp = (ARP)pkt;
ARP newArp = (ARP)newPkt;
newArp.setSenderProtocolAddress(new byte[] {1,2,3,4});
assertEquals(false, newArp.getSenderProtocolAddress()
.equals(arp.getSenderProtocolAddress()));
assertEquals(false, newPkt.equals(pkt));
}
byte[] dummyData = dummyPkt.serialize().clone();
newPkt = (IPacket)pkt.clone();
newPkt.deserialize(dummyData, 0, dummyData.length);
assertEquals(false, newPkt.equals(pkt));
}
@Test
public void testClone() {
for (IPacket pkt: packets) {
doTestClone(pkt);
}
}
}
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