Examples of Pcap


Examples of org.jnetpcap.Pcap

   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public void _testScanFileJBHandler() throws IOException {
    StringBuilder errbuf = new StringBuilder();
    final Pcap pcap = Pcap.openOffline("tests/test-l2tp.pcap", errbuf);

    final JPacket packet = new PcapPacket(Type.POINTER);
    final JScanner scanner = new JScanner();

    long start = System.currentTimeMillis();
    final TextFormatter out = new TextFormatter();

    pcap.loop(Pcap.LOOP_INFINATE, new JBufferHandler<String>() {
      int i = 0;

      public void nextPacket(PcapHeader header, JBuffer buffer, String user) {

        if (i == 200) {
          pcap.breakloop();
          return;
        }

        System.out.println("\nPacket #" + i);

        packet.peer(buffer);

        scanner.scan(packet, JProtocol.ETHERNET_ID);
        // try {
        out.setFrameIndex(i++);
        // out.format(packet);
        // System.out.println(packet.toString());
        // } catch (IOException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
      }

    }, "");

    long end = System.currentTimeMillis();

    System.out.printf("time=%d ms\n", (end - start));

    pcap.close();
  }
View Full Code Here

Examples of org.jnetpcap.Pcap

   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public void testScanFileJPHandler() throws IOException {
    StringBuilder errbuf = new StringBuilder();
    final Pcap pcap = Pcap.openOffline("tests/test-vlan.pcap", errbuf);

    // long start = System.currentTimeMillis();
    @SuppressWarnings("unused")
    final TextFormatter out = new TextFormatter();
    @SuppressWarnings("unused")
    final JScanner scanner = new JScanner();

    pcap.loop(Pcap.LOOP_INFINATE, JProtocol.ETHERNET_ID,
        new JPacketHandler<String>() {
          @SuppressWarnings("unused")
          int i = 0;

          public void nextPacket(JPacket packet, String user) {

            // scanner.scan(packet, JProtocol.ETHERNET_ID);
            // try {
            // out.setFrameIndex(i++);
            // out.format(packet);
            // } catch (IOException e) {
            // e.printStackTrace();
            // }
          }

        }, "");

    // long end = System.currentTimeMillis();

    // System.out.printf("time=%d ms\n", (end - start));

    pcap.close();
  }
View Full Code Here

Examples of org.jnetpcap.Pcap

    long base = 0;

    for (int i = 0; i < COUNT; i++) {

      for (final String fname : files) {
        Pcap pcap = Pcap.openOffline(DIR.toString() + "/" + fname, errbuf);
        assertNotNull(errbuf.toString(), pcap);

        pcap.loop(Pcap.LOOP_INFINATE, new JPacketHandler<Pcap>() {

          public void nextPacket(JPacket packet, Pcap user) {
            assertNotNull(packet);

            count++;
            b += packet.size();
            // h += packet.getState().getHeaderCount();

          }

        }, pcap);

        pcap.close();
      }

      /*
       * Skip 1 iteration to allow all the files to be opened and any allocated
       * resources to end up as a memory base.
View Full Code Here

Examples of org.jnetpcap.Pcap

    });

    for (int i = 0; i < COUNT; i++) {
      for (String fname : files) {
        Pcap pcap = Pcap.openOffline(DIR.toString() + "/" + fname, errbuf);
        assertNotNull(errbuf.toString(), pcap);

        pcap.loop(Pcap.LOOP_INFINATE, new PcapPacketHandler<Pcap>() {

          public void nextPacket(PcapPacket packet, Pcap user) {
            assertNotNull(packet);

          }

        }, pcap);

        pcap.close();
      }

      System.out.printf(".");
      System.out.flush();
    }
View Full Code Here

Examples of org.jnetpcap.Pcap

    ProcMem pm = new ProcMem();
    long base = 0;

    for (int i = 0; i < COUNT; i++) {
      for (final String fname : files) {
        Pcap pcap = Pcap.openOffline(DIR.toString() + "/" + fname, errbuf);
        assertNotNull(errbuf.toString(), pcap);

        pcap.loop(Pcap.LOOP_INFINATE, new JBufferHandler<Pcap>() {

          public void nextPacket(PcapHeader header, JBuffer buffer, Pcap user) {
            count++;
            b += buffer.size();
          }

        }, pcap);

        pcap.close();
      }

      /*
       * Skip 1 iteration to allow all the files to be opened and any allocated
       * resources to end up as a memory base.
View Full Code Here

Examples of org.jnetpcap.Pcap

    /***************************************************************************
     * First, open offline file
     **************************************************************************/
    StringBuilder errbuf = new StringBuilder();

    final Pcap pcap = Pcap.openOffline(file, errbuf);
    if (pcap == null) {
      System.err.println(errbuf.toString());
      return null;
    }

    /***************************************************************************
     * Second, setup a packet we're going to copy the captured contents into.
     * Allocate 2K native memory block to hold both state and buffer. Notice
     * that the packet has to be marked "final" in order for the JPacketHandler
     * to be able to access that variable from within the loop.
     **************************************************************************/
    final PcapPacket result = new PcapPacket(2 * 1024);

    /***************************************************************************
     * Third, Enter our loop and count packets until we reach the index of the
     * packet we are looking for.
     **************************************************************************/
    try {
      pcap.loop(Pcap.LOOP_INFINATE, new JBufferHandler<Pcap>() {
        int i = 0;

        public void nextPacket(PcapHeader header, JBuffer buffer, Pcap pcap) {

          /*********************************************************************
           * Forth, once we reach our packet transfer the capture data from our
           * temporary, shared packet, to our preallocated permanent packet. The
           * method transferStateAndDataTo will do a deep copy of the packet
           * contents and state to the destination packet. The copy is done
           * natively with memcpy. The packet content in destination packet is
           * layout in memory as follows. At the front of the buffer is the
           * packet_state_t structure followed immediately by the packet data
           * buffer and its size is adjusted to the exact size of the temporary
           * buffer. The remainder of the allocated memory block is unused, but
           * needed to be allocated large enough to hold a decent size packet.
           * To break out of the Pcap.loop we call Pcap.breakLoop().
           ********************************************************************/
          if (i++ == index) {
            PcapPacket packet = new PcapPacket(header, buffer);
            packet.scan(JRegistry.mapDLTToId(pcap.datalink()));
            System.out.println(packet.getState().toDebugString());
            packet.transferStateAndDataTo(result);

            pcap.breakloop();
            return;
          }
        }

      }, pcap);
    } finally {

      /*************************************************************************
       * Lastly, we close the pcap handle and return our result :)
       ************************************************************************/
      pcap.close();
    }

    return result;
  }
View Full Code Here

Examples of org.jnetpcap.Pcap

    /***************************************************************************
     * First, open offline file
     **************************************************************************/
    StringBuilder errbuf = new StringBuilder();

    final Pcap pcap = Pcap.openOffline(file, errbuf);
    assertNotNull(errbuf.toString());

    if (filter != null) {
      PcapBpfProgram prog = new PcapBpfProgram();
      if (pcap.compile(prog, filter, 0, 0xffffff00) != Pcap.OK) {
        System.err.printf("pcap filter %s: %s\n", pcap.getErr(), filter);
        return null;
      }
      pcap.setFilter(prog);
    }

    final Exchanger<PcapPacket> barrier = new Exchanger<PcapPacket>();

    /***************************************************************************
     * Third, Enter our loop and count packets until we reach the index of the
     * packet we are looking for.
     **************************************************************************/

    final PcapTask<Pcap> task = new PcapTask<Pcap>(pcap, end - start, pcap) {

      public void run() {
        try {
          barrier.exchange(null);
        } catch (InterruptedException e1) {
        }

        this.result = pcap.loop(end - start, new PcapPacketHandler<Pcap>() {
          int i = 0;

          public void nextPacket(PcapPacket packet, Pcap pcap) {

            assertNotNull(packet);
View Full Code Here

Examples of org.jnetpcap.Pcap

    final int size = (int) getFileSizeAggregate(files);

    final JPcapRecordBuffer buf = new JPcapRecordBuffer(size);

    for (final String fname : files) {
      Pcap pcap = Pcap.openOffline(fname, errbuf);
      assertNotNull(errbuf.toString(), pcap);

      try {
        pcap.loop(Pcap.LOOP_INFINATE, new JBufferHandler<String>() {
          private final int index = 1;

          public void nextPacket(PcapHeader header, JBuffer buffer, String fname) {
            buf.append(header, buffer);
          }
        },
            fname);
      } catch (RuntimeException e) {
        e.printStackTrace();
        throw e;
      } finally {
        pcap.close();
      }
    }

    buf.close();
View Full Code Here

Examples of org.jnetpcap.Pcap

    if (Pcap.findAllDevs(alldevs, errbuf) != Pcap.OK) {
      throw new IllegalStateException(errbuf.toString());
    }

    Pcap pcap =
        Pcap.openLive(alldevs.get(0).getName(),
            Pcap.DEFAULT_SNAPLEN,
            Pcap.DEFAULT_PROMISC,
            Pcap.DEFAULT_TIMEOUT,
            errbuf);
    if (pcap == null) {
      throw new IllegalArgumentException(errbuf.toString());
    }

    pcap.loop((int) count, handler, pcap);
  }
View Full Code Here

Examples of org.jnetpcap.Pcap

    /***************************************************************************
     * First, open offline file
     **************************************************************************/
    StringBuilder errbuf = new StringBuilder();

    final Pcap pcap = Pcap.openOffline(fname, errbuf);
    if (pcap == null) {
      System.err.println(errbuf.toString());
      return null;
    }

View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.