Search in sources :

Example 6 with ICMPEcho

use of org.onlab.packet.ICMPEcho in project onos by opennetworkinglab.

the class OpenstackTroubleshootManager method constructIcmpPacket.

/**
 * Constructs an ICMP packet with given source and destination IP/MAC.
 *
 * @param srcIp     source IP address
 * @param dstIp     destination IP address
 * @param srcMac    source MAC address
 * @param dstMac    destination MAC address
 * @param icmpId    ICMP identifier
 * @param icmpSeq   ICMP sequence number
 * @return an ethernet frame which contains ICMP payload
 */
private Ethernet constructIcmpPacket(IpAddress srcIp, IpAddress dstIp, MacAddress srcMac, MacAddress dstMac, short icmpId, short icmpSeq) {
    // Ethernet frame
    Ethernet ethFrame = new Ethernet();
    ethFrame.setEtherType(TYPE_IPV4);
    ethFrame.setSourceMACAddress(srcMac);
    ethFrame.setDestinationMACAddress(dstMac);
    // IP packet
    IPv4 iPacket = new IPv4();
    iPacket.setDestinationAddress(dstIp.toString());
    iPacket.setSourceAddress(srcIp.toString());
    iPacket.setTtl(TTL);
    iPacket.setProtocol(IPv4.PROTOCOL_ICMP);
    // ICMP packet
    ICMP icmp = new ICMP();
    icmp.setIcmpType(TYPE_ECHO_REQUEST).setIcmpCode(TYPE_ECHO_REQUEST).resetChecksum();
    // ICMP ECHO packet
    ICMPEcho icmpEcho = new ICMPEcho();
    icmpEcho.setIdentifier(icmpId).setSequenceNum(icmpSeq);
    ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
    try {
        icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(), 0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH));
    } catch (DeserializationException e) {
        log.warn("Failed to deserialize ICMP ECHO REQUEST packet");
    }
    ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize());
    try {
        iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(), 0, byteBufferIcmp.array().length));
    } catch (DeserializationException e) {
        log.warn("Failed to deserialize ICMP packet");
    }
    ethFrame.setPayload(iPacket);
    return ethFrame;
}
Also used : ICMPEcho(org.onlab.packet.ICMPEcho) Ethernet(org.onlab.packet.Ethernet) IPv4(org.onlab.packet.IPv4) ByteBuffer(java.nio.ByteBuffer) DeserializationException(org.onlab.packet.DeserializationException) ICMP(org.onlab.packet.ICMP)

Aggregations

ICMPEcho (org.onlab.packet.ICMPEcho)6 ICMP (org.onlab.packet.ICMP)5 ByteBuffer (java.nio.ByteBuffer)2 DeserializationException (org.onlab.packet.DeserializationException)2 Ethernet (org.onlab.packet.Ethernet)2 IPv4 (org.onlab.packet.IPv4)2