kaitoy / pcap4j

A Java library for capturing, crafting, and sending packets.
https://www.pcap4j.org/
Other
1.17k stars 309 forks source link

NullPointerException when including packet factory module #76

Closed acrophy closed 7 years ago

acrophy commented 7 years ago

I've encountered a "NullPointerException" when I try to read packets from a public trace without payload. Since I only care about the origin length and timestamp, I don't literally instruct the packets to be parsed(i.e. I only use PcapHandle rather than Packet). Yet, the exception still occurs when it tries to parse the packets.

I suppose packets are automatically parsed when packet factory module is included. When I remove the packet factory module in the pom file, the exception is gone. However, I do intend to inspect into packet headers in the future. So, please help me to deal with the exception.

Below is my code snippet which aims to do the byte count & packet count per millisecond:

        PcapHandle handle;
        handle = Pcaps.openOffline(PCAP_FILE, PcapHandle.TimestampPrecision.NANO);
        long baseTimeMillis = 0, curTimeMillis;
        int[] bytesCountPerMilli = new int[500000];
        int[] pktCountPerMilli = new int[500000];
        int num = 0;

        if (handle.getNextPacket() != null) {
           baseTimeMillis = handle.getTimestamp().getTime();
           num = 0;
           bytesCountPerMilli[num] = handle.getOriginalLength();
           pktCountPerMilli[num] = 1;
        }

        while (true) {
            try {
                handle.getNextPacketEx();
            } catch (EOFException e) {
                System.out.println("End of file!!!");
                break;
            }
            curTimeMillis = handle.getTimestamp().getTime();
            if (curTimeMillis > baseTimeMillis)  {
                baseTimeMillis = curTimeMillis;
                num ++;
                bytesCountPerMilli[num] = handle.getOriginalLength();
                pktCountPerMilli[num] = 1;
            } else {
                bytesCountPerMilli[num] += handle.getOriginalLength();
                pktCountPerMilli[num] ++;
            }
        }
        handle.close();

Below is the error message:

Exception in thread "main" java.lang.NullPointerException: packet: [IPv4 Header (20 bytes)]
  Version: 4 (IPv4)
  IHL: 5 (20 [bytes])
  TOS: [precedence: 0 (Routine)] [tos: 0 (Default)] [mbz: 0]
  Total length: 40 [bytes]
  Identification: 7740
  Flags: (Reserved, Don't Fragment, More Fragment) = (false, true, false)
  Fragment offset: 0 (0 [bytes])
  TTL: 57
  Protocol: 6 (TCP)
  Header checksum: 0x72b2
  Source address: /244.3.160.239
  Destination address: /90.218.193.20
 packet.getHeader(): [IPv4 Header (20 bytes)]
  Version: 4 (IPv4)
  IHL: 5 (20 [bytes])
  TOS: [precedence: 0 (Routine)] [tos: 0 (Default)] [mbz: 0]
  Total length: 40 [bytes]
  Identification: 7740
  Flags: (Reserved, Don't Fragment, More Fragment) = (false, true, false)
  Fragment offset: 0 (0 [bytes])
  TTL: 57
  Protocol: 6 (TCP)
  Header checksum: 0x72b2
  Source address: /244.3.160.239
  Destination address: /90.218.193.20
 packet.getPayload(): null
    at org.pcap4j.util.IcmpV4Helper.makePacketForInvokingPacketField(IcmpV4Helper.java:35)
    at org.pcap4j.packet.IcmpV4InvokingPacketPacket.<init>(IcmpV4InvokingPacketPacket.java:76)
    at org.pcap4j.packet.IcmpV4DestinationUnreachablePacket.<init>(IcmpV4DestinationUnreachablePacket.java:66)
    at org.pcap4j.packet.IcmpV4DestinationUnreachablePacket.newPacket(IcmpV4DestinationUnreachablePacket.java:50)
    at org.pcap4j.packet.factory.StaticIcmpV4TypePacketFactory$2.newInstance(StaticIcmpV4TypePacketFactory.java:56)
    at org.pcap4j.packet.factory.AbstractStaticPacketFactory.newInstance(AbstractStaticPacketFactory.java:46)
    at org.pcap4j.packet.factory.AbstractStaticPacketFactory.newInstance(AbstractStaticPacketFactory.java:23)
    at org.pcap4j.packet.IcmpV4CommonPacket.<init>(IcmpV4CommonPacket.java:57)
    at org.pcap4j.packet.IcmpV4CommonPacket.newPacket(IcmpV4CommonPacket.java:47)
    at org.pcap4j.packet.factory.StaticIpNumberPacketFactory$2.newInstance(StaticIpNumberPacketFactory.java:55)
    at org.pcap4j.packet.factory.AbstractStaticPacketFactory.newInstance(AbstractStaticPacketFactory.java:46)
    at org.pcap4j.packet.factory.AbstractStaticPacketFactory.newInstance(AbstractStaticPacketFactory.java:23)
    at org.pcap4j.packet.IpV4Packet.<init>(IpV4Packet.java:94)
    at org.pcap4j.packet.IpV4Packet.newPacket(IpV4Packet.java:61)
    at org.pcap4j.packet.factory.StaticEtherTypePacketFactory$1.newInstance(StaticEtherTypePacketFactory.java:37)
    at org.pcap4j.packet.factory.StaticEtherTypePacketFactory.newInstance(StaticEtherTypePacketFactory.java:111)
    at org.pcap4j.packet.factory.StaticEtherTypePacketFactory.newInstance(StaticEtherTypePacketFactory.java:24)
    at org.pcap4j.packet.Dot1qVlanTagPacket.<init>(Dot1qVlanTagPacket.java:56)
    at org.pcap4j.packet.Dot1qVlanTagPacket.newPacket(Dot1qVlanTagPacket.java:46)
    at org.pcap4j.packet.factory.StaticEtherTypePacketFactory$3.newInstance(StaticEtherTypePacketFactory.java:65)
    at org.pcap4j.packet.factory.StaticEtherTypePacketFactory.newInstance(StaticEtherTypePacketFactory.java:111)
    at org.pcap4j.packet.factory.StaticEtherTypePacketFactory.newInstance(StaticEtherTypePacketFactory.java:24)
    at org.pcap4j.packet.EthernetPacket.<init>(EthernetPacket.java:102)
    at org.pcap4j.packet.EthernetPacket.newPacket(EthernetPacket.java:61)
    at org.pcap4j.packet.factory.StaticDataLinkTypePacketFactory$1.newInstance(StaticDataLinkTypePacketFactory.java:39)
    at org.pcap4j.packet.factory.AbstractStaticPacketFactory.newInstance(AbstractStaticPacketFactory.java:46)
    at org.pcap4j.packet.factory.AbstractStaticPacketFactory.newInstance(AbstractStaticPacketFactory.java:23)
    at org.pcap4j.core.PcapHandle.getNextPacketEx(PcapHandle.java:699)
    at cn.edu.sjtu.omnilab.TrafficAnalyzerFromPcapFile.main(TrafficAnalyzerFromPcapFile.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
kaitoy commented 7 years ago

It looks Pcap4J's bug. I've already found the cause. I will fix it very soon. BTW, if you are only interested in timestamps and orig lengths, you can use raw packet API getNextRawPacket() and getNextRawPacketEx() instead. Packet dissection doesn't happen in these methods, so the error won't happen.

kaitoy commented 7 years ago

I made the fix. Please try the snapshot build: https://oss.sonatype.org/content/repositories/snapshots/org/pcap4j/pcap4j-distribution/1.7.1-SNAPSHOT/pcap4j-distribution-1.7.1-20161124.163248-2-bin.zip

acrophy commented 7 years ago

I’ve tried the snapshot, and it works fine. Thanks a lot!

在 2016年11月25日,00:39,Kaito Yamada notifications@github.com 写道:

I made the fix. Please try the snapshot build: https://oss.sonatype.org/content/repositories/snapshots/org/pcap4j/pcap4j-distribution/1.7.1-SNAPSHOT/pcap4j-distribution-1.7.1-20161124.163248-2-bin.zip https://oss.sonatype.org/content/repositories/snapshots/org/pcap4j/pcap4j-distribution/1.7.1-SNAPSHOT/pcap4j-distribution-1.7.1-20161124.163248-2-bin.zip — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kaitoy/pcap4j/issues/76#issuecomment-262814601, or mute the thread https://github.com/notifications/unsubscribe-auth/AA3qgtMM0_eOr8scKHLgq6kDbhfjhnJ0ks5rBb3VgaJpZM4K7Ruu.

mengjianjian commented 1 month ago

I run the first demo DefragmentEcho with src code ,but there come some errors,

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.pcap4j.packet.IpV4Packet.getHeader()" because the return value of "org.pcap4j.packet.Packet.get(java.lang.Class)" is null at org.pcap4j.sample/org.pcap4j.sample.DefragmentEcho.main(DefragmentEcho.java:36)