Search in sources :

Example 1 with PcapRootEventField

use of org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField in project tracecompass by tracecompass.

the class PcapEventFieldTest method setUp.

/**
 * Initialize the Packet and the Event.
 *
 * @throws BadPcapFileException
 *             Thrown when the pcap file is erroneous.
 * @throws IOException
 *             Thrown when an IO error occurs.
 * @throws BadPacketException
 *             Thrown when the packet is erroneous.
 */
@BeforeClass
public static void setUp() throws IOException, BadPcapFileException, BadPacketException {
    ByteBuffer bb = ByteBuffer.allocate(25);
    bb.order(ByteOrder.BIG_ENDIAN);
    // Version + IHL
    bb.put((byte) 0x46);
    // DSCP + ECN
    bb.put((byte) 0x9A);
    // Total length - this is randomly chosen so that we verify that the
    // packet handles wrong total length.
    bb.put((byte) 0x00);
    bb.put((byte) 0xFF);
    // Identification
    bb.put((byte) 0x0F);
    bb.put((byte) 0xF0);
    // Flags + Fragment Offset
    bb.put((byte) 0x1E);
    bb.put((byte) 0xE1);
    // Time to live
    bb.put((byte) 0xA0);
    // Protocol - Unknown
    bb.put((byte) 0xFE);
    // Header checksum - chosen randomly
    bb.put((byte) 0x33);
    bb.put((byte) 0x44);
    // Source IP - 4 bytes
    bb.put((byte) 192);
    bb.put((byte) 168);
    bb.put((byte) 1);
    bb.put((byte) 0);
    // Destination IP - 4 bytes
    bb.put((byte) 193);
    bb.put((byte) 169);
    bb.put((byte) 2);
    bb.put((byte) 1);
    // Options - 4 bytes
    bb.put((byte) 0xA2);
    bb.put((byte) 0x56);
    bb.put((byte) 0xA2);
    bb.put((byte) 0x56);
    // Payload - 1 byte
    bb.put((byte) 0xA6);
    bb.flip();
    PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
    assumeTrue(trace.exists());
    try (PcapFile file = trace.getTrace()) {
        IPv4Packet packet = new IPv4Packet(file, null, bb);
        ITmfEventField[] fieldArray = generatePacketFields(packet);
        fRegularField = new PcapEventField("Regular Field", EMPTY_STRING, fieldArray, packet);
        fRootField = new PcapRootEventField(fieldArray, packet);
    }
}
Also used : PcapRootEventField(org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField) ITmfEventField(org.eclipse.tracecompass.tmf.core.event.ITmfEventField) PcapEventField(org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventField) PcapFile(org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile) IPv4Packet(org.eclipse.tracecompass.internal.pcap.core.protocol.ipv4.IPv4Packet) PcapTestTrace(org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace) ByteBuffer(java.nio.ByteBuffer) BeforeClass(org.junit.BeforeClass)

Example 2 with PcapRootEventField

use of org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField in project tracecompass by tracecompass.

the class PcapEventFactory method createEvent.

/**
 * Method that create a PcapEvent from a packet.
 *
 * @param pcapPacket
 *            The packet to generate the event from.
 * @param pcap
 *            The pcap file to which the packet belongs.
 * @param trace
 *            The trace to which this packet belongs.
 * @return The generated PcapEvent.
 */
@Nullable
public static PcapEvent createEvent(PcapPacket pcapPacket, PcapFile pcap, PcapTrace trace) {
    long rank = pcapPacket.getIndex();
    long timestamp = pcapPacket.getTimestamp();
    PcapTimestampScale scale = pcapPacket.getTimestampScale();
    ITmfTimestamp tmfTimestamp;
    switch(scale) {
        case MICROSECOND:
            long us = trace.getTimestampTransform().transform(timestamp * 1000) / 1000;
            tmfTimestamp = TmfTimestamp.fromMicros(us);
            break;
        case NANOSECOND:
            long ns = trace.getTimestampTransform().transform(timestamp);
            tmfTimestamp = TmfTimestamp.fromNanos(ns);
            break;
        default:
            // $NON-NLS-1$
            throw new IllegalArgumentException("The timestamp precision is not valid!");
    }
    Path filePath = pcap.getPath().getFileName();
    @NonNull String fileName = (filePath == null ? EMPTY_STRING : checkNotNull(filePath.toString()));
    String dataLink = Messages.PcapEventFactory_LinkType + ':' + LinkTypeHelper.toString((int) pcapPacket.getDataLinkType());
    ITmfEventField[] fields = generatePacketFields(pcapPacket);
    ITmfEventField field = new PcapRootEventField(fields, pcapPacket);
    Packet packet = pcapPacket.getMostEcapsulatedPacket();
    if (!fEventTypes.containsKey(packet.getProtocol())) {
        String typeIdString = PcapEventType.DEFAULT_PCAP_TYPE_ID + ':' + packet.getProtocol().getShortName();
        fEventTypes.put(packet.getProtocol(), new PcapEventType(typeIdString, null));
    }
    TmfEventType eventType = fEventTypes.get(packet.getProtocol());
    if (eventType == null) {
        eventType = new TmfEventType();
    }
    return new PcapEvent(trace, rank, tmfTimestamp, dataLink, eventType, field, fileName, packet);
}
Also used : Path(java.nio.file.Path) PcapRootEventField(org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField) Packet(org.eclipse.tracecompass.internal.pcap.core.packet.Packet) PcapPacket(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket) PcapEventType(org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventType) PcapEvent(org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent) PcapTimestampScale(org.eclipse.tracecompass.internal.pcap.core.util.PcapTimestampScale) ITmfEventField(org.eclipse.tracecompass.tmf.core.event.ITmfEventField) ITmfTimestamp(org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp) NonNull(org.eclipse.jdt.annotation.NonNull) TmfEventType(org.eclipse.tracecompass.tmf.core.event.TmfEventType) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

PcapRootEventField (org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField)2 ITmfEventField (org.eclipse.tracecompass.tmf.core.event.ITmfEventField)2 ByteBuffer (java.nio.ByteBuffer)1 Path (java.nio.file.Path)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Packet (org.eclipse.tracecompass.internal.pcap.core.packet.Packet)1 IPv4Packet (org.eclipse.tracecompass.internal.pcap.core.protocol.ipv4.IPv4Packet)1 PcapPacket (org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket)1 PcapFile (org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile)1 PcapTimestampScale (org.eclipse.tracecompass.internal.pcap.core.util.PcapTimestampScale)1 PcapEvent (org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent)1 PcapEventField (org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventField)1 PcapEventType (org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventType)1 PcapTestTrace (org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace)1 TmfEventType (org.eclipse.tracecompass.tmf.core.event.TmfEventType)1 ITmfTimestamp (org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp)1 BeforeClass (org.junit.BeforeClass)1