Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
/*
 * Overridden IOFMessageListener's receive() function.
 */
@Override
public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
	switch (msg.getType()) {
	case PACKET_IN:
		/* Retrieve the deserialized packet in message */
		Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);

		/* Various getters and setters are exposed in Ethernet */
		MacAddress srcMac = eth.getSourceMACAddress();
		VlanVid vlanId = VlanVid.ofVlan(eth.getVlanID());

		/* 
		 * Check the ethertype of the Ethernet frame and retrieve the appropriate payload.
		 * Note the shallow equality check. EthType caches and reuses instances for valid types.
		 */
		if (eth.getEtherType() == EthType.IPv4) {
			/* We got an IPv4 packet; get the payload from Ethernet */
			IPv4 ipv4 = (IPv4) eth.getPayload();
			
			/* Various getters and setters are exposed in IPv4 */
			byte[] ipOptions = ipv4.getOptions();
			IPv4Address dstIp = ipv4.getDestinationAddress();
			
			/* 
			 * Check the IP protocol version of the IPv4 packet's payload.
			 * Note the deep equality check. Unlike EthType, IpProtocol does
			 * not cache valid/common types;
thus, all instances are unique.
			 */
			if (ipv4.getProtocol().equals( == IpProtocol.TCP)) {
				/* We got a TCP packet; get the payload from IPv4 */
				TCP tcp = (TCP) ipv4.getPayload();
 
				/* Various getters and setters are exposed in TCP */
				TransportPort srcPort = tcp.getSourcePort();
				TransportPort dstPort = tcp.getDestinationPort();
				short flags = tcp.getFlags();
				
				/* Your logic here! */
			} else if (ipv4.getProtocol().equals( == IpProtocol.UDP)) {
				/* We got a UDP packet; get the payload from IPv4 */
				UDP udp = (UDP) ipv4.getPayload();
 
				/* Various getters and setters are exposed in UDP */
				TransportPort srcPort = udp.getSourcePort();
				TransportPort dstPort = udp.getDestinationPort();
				
				/* Your logic here! */
			}

		} else if (eth.getEtherType() == EthType.ARP) {
			/* We got an ARP packet; get the payload from Ethernet */
			ARP arp = (ARP) eth.getPayload();

			/* Various getters and setters are exposed in ARP */
			boolean gratuitous = arp.isGratuitous();

		} else {
			/* Unhandled ethertype */
		}
		break;
	default:
		break;
	}
	return Command.CONTINUE;
}

...