Example #1
0
  /**
   * @param snaplen
   * @param dlt
   * @param bpfExpression
   * @param mode
   * @param netmask
   * @return a {@link org.pcap4j.core.BpfProgram BpfProgram} object.
   * @throws PcapNativeException
   */
  public static BpfProgram compileFilter(
      int snaplen,
      DataLinkType dlt,
      String bpfExpression,
      BpfCompileMode mode,
      Inet4Address netmask)
      throws PcapNativeException {
    if (dlt == null || bpfExpression == null || mode == null || netmask == null) {
      StringBuilder sb = new StringBuilder();
      sb.append("dlt: ")
          .append(dlt)
          .append(" bpfExpression: ")
          .append(bpfExpression)
          .append(" mode: ")
          .append(mode)
          .append(" netmask: ")
          .append(netmask);
      throw new NullPointerException(sb.toString());
    }

    bpf_program prog = new bpf_program();
    int rc =
        NativeMappings.pcap_compile_nopcap(
            snaplen,
            dlt.value(),
            prog,
            bpfExpression,
            mode.getValue(),
            ByteArrays.getInt(ByteArrays.toByteArray(netmask), 0));
    if (rc < 0) {
      throw new PcapNativeException("Failed to compile the BPF expression: " + bpfExpression, rc);
    }
    return new BpfProgram(prog, bpfExpression);
  }
Example #2
0
 /**
  * @param dlt
  * @return a short description of that data link type.
  * @throws PcapNativeException
  */
 public static String dataLinkTypeToDescription(DataLinkType dlt) throws PcapNativeException {
   if (dlt == null) {
     StringBuilder sb = new StringBuilder();
     sb.append("dlt: ").append(dlt);
     throw new NullPointerException(sb.toString());
   }
   return dataLinkValToDescription(dlt.value());
 }
Example #3
0
  /**
   * @param name a data link type name, which is a DLT_ name with the DLT_ removed.
   * @return a {@link org.pcap4j.packet.namednumber.DataLinkType DataLinkType} object.
   * @throws PcapNativeException
   */
  public static DataLinkType dataLinkNameToVal(String name) throws PcapNativeException {
    if (name == null) {
      StringBuilder sb = new StringBuilder();
      sb.append("name: ").append(name);
      throw new NullPointerException(sb.toString());
    }

    int rc = NativeMappings.pcap_datalink_name_to_val(name);
    if (rc < 0) {
      throw new PcapNativeException(
          "Failed to convert the data link name to the value: " + name, rc);
    }
    return DataLinkType.getInstance(rc);
  }
Example #4
0
  /**
   * @param dlt
   * @param snaplen Snapshot length, which is the number of bytes captured for each packet.
   * @return a new PcapHandle object.
   * @throws PcapNativeException
   */
  public static PcapHandle openDead(DataLinkType dlt, int snaplen) throws PcapNativeException {
    if (dlt == null) {
      StringBuilder sb = new StringBuilder();
      sb.append("dlt: ").append(dlt);
      throw new NullPointerException(sb.toString());
    }

    Pointer handle = NativeMappings.pcap_open_dead(dlt.value(), snaplen);
    if (handle == null) {
      StringBuilder sb = new StringBuilder(50);
      sb.append("Failed to open a PcapHandle. dlt: ")
          .append(dlt)
          .append(" snaplen: ")
          .append(snaplen);
      throw new PcapNativeException(sb.toString());
    }

    return new PcapHandle(handle);
  }