Esempio n. 1
0
  @Override
  public void initialize(PcapIf pcapIf) {
    packets = new ArrayList<JPacket>();

    // extract source MAC from injection interface
    byte[] srcMac;
    try {
      srcMac = pcapIf.getHardwareAddress();
    } catch (IOException ioe) {
      log.error("Error obtaining injection interface address: ", ioe);
      return;
    }

    // extract a source IPv4 address from injection interface
    byte[] srcIp = NetUtils.getLinkLocal6Addr(pcapIf);
    if (srcIp == null) {
      log.error("Failed to find a IPv6 link-local source address");
      return;
    }

    // get a list of pcap files we'll use for injection
    String[] pcapFiles = JNetPcapUtils.getPcapFilenames(PCAP_DIR);

    // packet headers
    Ethernet eth = new Ethernet();
    Ip6 ip = new Ip6();
    Icmp6 icmp = new Icmp6();

    // load pcap files one-by-one
    for (String pcapFile : pcapFiles) {
      PcapPacket packet = JNetPcapUtils.pcapPacketFromFile(pcapFile);
      if (packet == null) {
        log.warn("No packet found in [{}]", pcapFile);
        continue;
      }

      // validate packet
      if (!packet.hasHeader(eth) || !packet.hasHeader(ip) || !packet.hasHeader(icmp)) {
        log.error("Invalid packet type in [{}]", pcapFile);
        continue;
      }

      // set the source MAC/IP in the packet
      eth.source(srcMac);
      ip.setByteArray(8, srcIp);

      // set destination mac/ip
      for (String[] dst : dstMacIps) {
        eth.destination(NetUtils.getMacBytes(dst[0]));
        ip.setByteArray(24, NetUtils.getAddressBytes(dst[1]));

        // calculate checksums
        eth.calculateChecksum();
        icmp.calculateChecksum();

        // make a deep copy of the packet and add it to the list
        packets.add(new PcapPacket(packet));
      }
    }
  }