Software Defined Networking

Get Packet Destination IP

 
ssrirama
Advisor

Get Packet Destination IP

Hi,

 

I have an application that registers itself as a packet listener on the controller (by implementing SequencedPacketListener). When the SPL event() method is triggered, I want to get the destination IP address and destination port of the packet passed in (from the MessageContext object). How can I get this information?

 

EDIT:

I did find the PathDiagnosticService interface which has a .getPath() method. Looks like what I want. Can I use this the same way the ControllerService interface is used?  By this I mean can I simply say "protected volatile PathDiagnosticService pds;" then later invoke it's methods like "pds.findPacketModel(...)" or will that return a NullPointerException because pds wasn't instantiated?

5 REPLIES 5
sepbot
Advisor

Re: Get Packet Destination IP

Try this:

 

Packet packet = Codec.decodeEthernet(context.getPacketIn().getData());
Ip ip = (Ip) packet.get(ProtocolId.IP);
IpAddress dst = ip.dstAddr();

 

ssrirama
Advisor

Re: Get Packet Destination IP

Haven't tested it yet, but I'll post if it doesn't work.

 

One question about the code you supplied, why use Codec specifically to decode the packet? Wouldn't context.decodedPacket() work?

ssrirama
Advisor

Re: Get Packet Destination IP

The code you've supplied causes a NullPointerException on the line:

IpAddress dst = ip.dstAddr();

 

Full code used in that method was:

@Override
    public boolean event(MessageContext messageContext) {
        Packet packet = messageContext.decodedPacket();
        Ip ip = (Ip) packet.get(ProtocolId.IP);
        IpAddress dst = ip.dstAddr();
        return false;
    }

 

 

sepbot
Advisor

Re: Get Packet Destination IP

If the packet only goes up to layer 2, then it will not have any IP information which might be the cause of the NullPointerException. Try puttin a check like this and see if it works:

 

if (packet.has(ProtocolId.IP) || packet.has(ProtocolId.IPV6)) {

}

 

sdnindia
Trusted Contributor

Re: Get Packet Destination IP

Hello ssrirama,

 

Did you try as suggested in earlier post?

 

Please let us know if your problem is solved or you are still facing the issue.

 

Thanks,

HP SDN Team