コード例 #1
0
ファイル: DltRawTest.java プロジェクト: Mehrkat/pcap4j
  @Test
  public void testRead() throws Exception {
    String pcapFile =
        new StringBuilder()
            .append(resourceDirPath)
            .append("/")
            .append(getClass().getSimpleName())
            .append(".pcap")
            .toString();
    PcapHandle ph = Pcaps.openOffline(pcapFile);
    StringBuilder sb = new StringBuilder(1000);
    sb.append(ph.getNextPacket().toString())
        .append(System.getProperty("line.separator"))
        .append(ph.getNextPacket().toString());
    ph.close();

    FileReader fr =
        new FileReader(
            new StringBuilder()
                .append(resourceDirPath)
                .append("/")
                .append(getClass().getSimpleName())
                .append(".log")
                .toString());
    BufferedReader fbr = new BufferedReader(fr);
    StringReader sr = new StringReader(sb.toString());
    BufferedReader sbr = new BufferedReader(sr);

    String line;
    while ((line = fbr.readLine()) != null) {
      assertEquals(line, sbr.readLine());
    }

    assertNull(sbr.readLine());

    fbr.close();
    fr.close();
    sr.close();
    sbr.close();
  }
コード例 #2
0
ファイル: DltRawTest.java プロジェクト: Mehrkat/pcap4j
  @Test
  public void testDump() throws Exception {
    String dumpFile =
        new StringBuilder()
            .append(tmpDirPath)
            .append("/")
            .append(getClass().getSimpleName())
            .append(".pcap")
            .toString();
    PcapHandle handle = Pcaps.openDead(DataLinkType.RAW, 65536);
    PcapDumper dumper = handle.dumpOpen(dumpFile);
    dumper.dump(ipV4, 0, 0);
    dumper.dump(ipV6, 0, 0);
    dumper.close();
    handle.close();

    FileInputStream in1 =
        new FileInputStream(
            new StringBuilder()
                .append(resourceDirPath)
                .append("/")
                .append(getClass().getSimpleName())
                .append(".pcap")
                .toString());
    FileInputStream in2 = new FileInputStream(dumpFile);

    byte[] buffer1 = new byte[100];
    byte[] buffer2 = new byte[100];
    int size;
    while ((size = in1.read(buffer1)) != -1) {
      assertEquals(size, in2.read(buffer2));
      assertArrayEquals(buffer1, buffer2);
    }

    in1.close();
    in2.close();
  }
コード例 #3
0
  public static void main(String[] args)
      throws PcapNativeException, NotOpenException, InterruptedException {
    final PcapHandle pcapHandleReadOffline = Pcaps.openOffline(PCAP_FILE_READ);

    pcapDumper = pcapHandleReadOffline.dumpOpen(PCAP_FILE_WRITE);

    PacketListener packetListener =
        new PacketListener() {
          IPv4AddressRandomizer ipV4randomizer = new IPv4AddressRandomizer();
          IPv6AddressRandomizer ipV6randomizer = new IPv6AddressRandomizer();

          public void gotPacket(Packet fullPacket) {
            long timestampInts = pcapHandleReadOffline.getTimestampInts();
            int timestampMicros = pcapHandleReadOffline.getTimestampMicros();

            UdpPacket udpPacket = fullPacket.get(UdpPacket.class);
            fullPacket.getHeader();

            if (udpPacket == null) return;
            if (udpPacket.getHeader().getDstPort().value() != 2055)
              return; // discard all packets which are not IPFIX

            byte[] rawUpdPacketBytes = udpPacket.getRawData();
            byte[] onlyIPFIXbytes = new byte[rawUpdPacketBytes.length - 8];
            System.arraycopy(rawUpdPacketBytes, 8, onlyIPFIXbytes, 0, rawUpdPacketBytes.length - 8);

            try {
              MessageHeader messageHeader = MessageHeader.parse(onlyIPFIXbytes);
              List<SetHeader> setHeaders = messageHeader.getSetHeaders();
              for (SetHeader currentSetHeader : setHeaders) {
                List<DataRecord> dataRecords = currentSetHeader.getDataRecords();
                for (DataRecord currentDataRecord : dataRecords) {

                  try {
                    boolean foundIPv6 = false;
                    boolean foundIPv4 = false;
                    if (currentDataRecord instanceof L2IPDataRecord) {
                      L2IPDataRecord l2IPDataRecord = (L2IPDataRecord) currentDataRecord;
                      if (Utility.isConfigured(l2IPDataRecord.getDestinationIPv6Address())) {
                        foundIPv6 = true;
                      }
                      if (Utility.isConfigured(l2IPDataRecord.getSourceIPv6Address())) {
                        foundIPv6 = true;
                      }
                      if (Utility.isConfigured(l2IPDataRecord.getDestinationIPv4Address())) {
                        foundIPv4 = true;
                      }
                      if (Utility.isConfigured(l2IPDataRecord.getSourceIPv4Address())) {
                        foundIPv4 = true;
                      }
                      Inet4Address realDestinationIpv4 = l2IPDataRecord.getDestinationIPv4Address();
                      Inet4Address realSourceIpv4 = l2IPDataRecord.getSourceIPv4Address();

                      Inet6Address realDestinationIpv6 = l2IPDataRecord.getDestinationIPv6Address();
                      Inet6Address realSourceIpv6 = l2IPDataRecord.getSourceIPv6Address();

                      Inet4Address fakeDestinationIpv4 = realDestinationIpv4;
                      Inet4Address fakeSourceIpv4 = realSourceIpv4;

                      Inet6Address fakeDestinationIpv6 = realDestinationIpv6;
                      Inet6Address fakeSourceIpv6 = realSourceIpv6;

                      if (foundIPv4) {
                        fakeDestinationIpv4 =
                            (Inet4Address) ipV4randomizer.randomize(realDestinationIpv4);
                        fakeSourceIpv4 = (Inet4Address) ipV4randomizer.randomize(realSourceIpv4);

                        l2IPDataRecord.setDestinationIPv4Address(fakeDestinationIpv4);
                        l2IPDataRecord.setSourceIPv4Address(fakeSourceIpv4);
                      }

                      if (foundIPv6) {
                        fakeSourceIpv6 = (Inet6Address) ipV6randomizer.randomize(realSourceIpv6);
                        fakeDestinationIpv6 =
                            (Inet6Address) ipV6randomizer.randomize(realDestinationIpv6);

                        l2IPDataRecord.setDestinationIPv6Address(fakeDestinationIpv6);
                        l2IPDataRecord.setSourceIPv6Address(fakeSourceIpv6);
                      }
                    }
                  } catch (Exception e) {
                    e.printStackTrace();
                  }
                }
              }

              Packet.Builder packetBuilderUDP = fullPacket.get(UdpPacket.class).getBuilder();
              UnknownPacket.Builder unknownPacketBuilder = new UnknownPacket.Builder();

              unknownPacketBuilder.rawData(messageHeader.getBytes());
              packetBuilderUDP.payloadBuilder(unknownPacketBuilder);

              Packet.Builder packetBuilderIPv4 = fullPacket.get(IpV4Packet.class).getBuilder();
              packetBuilderIPv4.payloadBuilder(packetBuilderUDP);

              Packet.Builder packetBuilderEthernet =
                  fullPacket.get(EthernetPacket.class).getBuilder();
              packetBuilderEthernet.payloadBuilder(packetBuilderIPv4);

              Packet newPacket = packetBuilderEthernet.build();
              pcapDumper.dump(newPacket, timestampInts, timestampMicros);

              if (onlyIPFIXbytes.length != messageHeader.getBytes().length) {
                System.out.println(
                    "Lenght: OnlyIPFIX: "
                        + onlyIPFIXbytes.length
                        + " : Generated: "
                        + messageHeader.getBytes().length);
              }
            } catch (HeaderParseException e) {
              e.printStackTrace();
            } catch (HeaderBytesException e) {
              e.printStackTrace();
            } catch (NotOpenException e) {
              e.printStackTrace();
            }
          }
        };

    pcapHandleReadOffline.loop(-1, packetListener);
    pcapHandleReadOffline.close();

    pcapDumper.close();
  }