コード例 #1
0
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (!(obj instanceof PcapNetworkInterface)) {
      return false;
    }

    PcapNetworkInterface other = (PcapNetworkInterface) obj;
    return this.name.equals(other.getName()) && this.local == other.isLocal();
  }
コード例 #2
0
ファイル: Pcaps.java プロジェクト: Mehrkat/pcap4j
  /**
   * @return a list of PcapNetworkInterfaces.
   * @throws PcapNativeException
   */
  public static List<PcapNetworkInterface> findAllDevs() throws PcapNativeException {
    PointerByReference alldevsPP = new PointerByReference();
    PcapErrbuf errbuf = new PcapErrbuf();

    int rc = NativeMappings.pcap_findalldevs(alldevsPP, errbuf);
    if (rc != 0) {
      StringBuilder sb = new StringBuilder(50);
      sb.append("Return code: ").append(rc).append(", Message: ").append(errbuf);
      throw new PcapNativeException(sb.toString(), rc);
    }
    if (errbuf.length() != 0) {
      logger.warn("{}", errbuf);
    }

    Pointer alldevsp = alldevsPP.getValue();
    if (alldevsp == null) {
      logger.info("No NIF was found.");
      return Collections.<PcapNetworkInterface>emptyList();
    }

    pcap_if pcapIf = new pcap_if(alldevsp);

    List<PcapNetworkInterface> ifList = new ArrayList<PcapNetworkInterface>();
    for (pcap_if pif = pcapIf; pif != null; pif = pif.next) {
      ifList.add(PcapNetworkInterface.newInstance(pif, true));
    }

    NativeMappings.pcap_freealldevs(pcapIf.getPointer());

    logger.info("{} NIF(s) found.", ifList.size());
    return ifList;
  }
コード例 #3
0
ファイル: Pcaps.java プロジェクト: Mehrkat/pcap4j
  /**
   * @param name
   * @return a PcapNetworkInterface.
   * @throws PcapNativeException
   */
  public static PcapNetworkInterface getDevByName(String name) throws PcapNativeException {
    if (name == null) {
      StringBuilder sb = new StringBuilder();
      sb.append("name: ").append(name);
      throw new NullPointerException(sb.toString());
    }

    List<PcapNetworkInterface> allDevs = findAllDevs();
    for (PcapNetworkInterface pif : allDevs) {
      if (pif.getName().equals(name)) {
        return pif;
      }
    }

    return null;
  }
コード例 #4
0
ファイル: GetNextPacketEx.java プロジェクト: pjsg/pcap4j
  public static void main(String[] args) throws PcapNativeException, NotOpenException {
    String filter = args.length != 0 ? args[0] : "";

    System.out.println(COUNT_KEY + ": " + COUNT);
    System.out.println(READ_TIMEOUT_KEY + ": " + READ_TIMEOUT);
    System.out.println(SNAPLEN_KEY + ": " + SNAPLEN);
    System.out.println("\n");

    PcapNetworkInterface nif;
    try {
      nif = new NifSelector().selectNetworkInterface();
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }

    if (nif == null) {
      return;
    }

    System.out.println(nif.getName() + "(" + nif.getDescription() + ")");

    PcapHandle handle = nif.openLive(SNAPLEN, PromiscuousMode.PROMISCUOUS, READ_TIMEOUT);

    handle.setFilter(filter, BpfCompileMode.OPTIMIZE);

    int num = 0;
    while (true) {
      try {
        Packet packet = handle.getNextPacketEx();
        Timestamp ts = new Timestamp(handle.getTimestampInts() * 1000L);
        ts.setNanos(handle.getTimestampMicros() * 1000);

        System.out.println(ts);
        System.out.println(packet);
        num++;
        if (num >= COUNT) {
          break;
        }
      } catch (TimeoutException e) {
      } catch (EOFException e) {
        e.printStackTrace();
      }
    }

    handle.close();
  }
コード例 #5
0
ファイル: Pcaps.java プロジェクト: Mehrkat/pcap4j
  /**
   * @param addr
   * @return a PcapNetworkInterface.
   * @throws PcapNativeException
   */
  public static PcapNetworkInterface getDevByAddress(InetAddress addr) throws PcapNativeException {
    if (addr == null) {
      StringBuilder sb = new StringBuilder();
      sb.append("addr: ").append(addr);
      throw new NullPointerException(sb.toString());
    }

    List<PcapNetworkInterface> allDevs = findAllDevs();
    for (PcapNetworkInterface pif : allDevs) {
      for (PcapAddress paddr : pif.getAddresses()) {
        if (paddr.getAddress().equals(addr)) {
          return pif;
        }
      }
    }

    return null;
  }