public static void main(String args[]) throws Exception {
    // chapter 2.2-4
    // initiate packet capture device
    final int snaplen = Pcap.DEFAULT_SNAPLEN;
    final int flags = Pcap.MODE_PROMISCUOUS;
    final int timeout = Pcap.DEFAULT_TIMEOUT;
    final StringBuilder errbuf = new StringBuilder();
    Pcap pcap = Pcap.openLive(args[0], snaplen, flags, timeout, errbuf);
    if (pcap == null) {
      System.out.println("Error while opening device for capture: " + errbuf.toString());
      return;
    }

    // Get local address
    e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
      n = (NetworkInterface) e.nextElement();
      if (args[0].equals(n.getDisplayName())) {
        ee = n.getInetAddresses();
        mymac = n.getHardwareAddress();
        while (ee.hasMoreElements()) {
          inet = (InetAddress) ee.nextElement();
          System.out.println(n.getDisplayName() + " " + inet);
        }
      }
    }
    // Get IPv4 manually instead of looping through all IP's
    // myinet = inet.getAddress();

    // packet handler for packet capture
    pcap.loop(Integer.parseInt(args[1]), pcappackethandler, "pressure");
    pcap.close();
  }
示例#2
0
  public static void main(String[] args) throws InterruptedException {
    SynthManager s = new SynthManager();
    s.start();

    List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
    StringBuilder errbuf = new StringBuilder(); // For any error msgs

    int r = Pcap.findAllDevs(alldevs, errbuf);
    if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
      System.err.printf("Can't read list of devices, error is %s", errbuf.toString());
      return;
    }

    System.out.println("Network devices found:");

    int i = 0;
    for (PcapIf device : alldevs) {
      String description =
          (device.getDescription() != null) ? device.getDescription() : "No description available";
      System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description);
    }

    System.out.print("\nPlease choose one of the above (only the number): ");
    Scanner scan = new Scanner(System.in);
    int deviceNum = scan.nextInt();

    PcapIf device = alldevs.get(deviceNum); // We know we have atleast 1 device
    System.out.printf(
        "\nChoosing '%s':\n",
        (device.getDescription() != null) ? device.getDescription() : device.getName());

    int snaplen = 64 * 1024; // Capture all packets, no trucation
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
    int timeout = 10 * 1000; // 10 seconds in millis
    Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);

    if (pcap == null) {
      System.err.printf("Error while opening device for capture: " + errbuf.toString());
      return;
    }

    PcapPacketHandler<String> jpacketHandler =
        new PcapPacketHandler<String>() {

          public void nextPacket(PcapPacket packet, String user) {

            try {
              byte[] sourceIp = packet.getHeader(new Ip4()).source();
              s.playSound(new int[] {sourceIp[0], sourceIp[1], sourceIp[2], sourceIp[3]});
            } catch (NullPointerException e) {

            }
          }
        };

    pcap.loop(-1, jpacketHandler, "");

    pcap.close();
  }
示例#3
0
  /**
   * We ignore the argument and open the {@link #device#getName() } instead.
   *
   * @param pathOrDevice Argument ignored.
   * @return Pcap handle to the NIC/device that belongs to this ImportItem.
   */
  @Override
  protected Pcap getHandle(String pathOrDevice) {
    Pcap handle = null;

    int snaplen = getImporter().getPreferences().snaplen;
    int mode = getImporter().getPreferences().mode;
    int timeout = getImporter().getPreferences().timeout;

    try {
      handle = Pcap.openLive(device.getName(), snaplen, mode, timeout, errorBuffer);
    } catch (UnsatisfiedLinkError err) {
      Logger.getLogger(PCAPImport.class.getName())
          .log(Level.SEVERE, "Importing PCAP is disabled.", err);
    }

    return handle;
  }