Exemple #1
0
  private TcpSackOption(byte[] rawData, int offset, int length) throws IllegalRawDataException {
    if (length < 2) {
      StringBuilder sb = new StringBuilder(50);
      sb.append("The raw data length must be more than 1. rawData: ")
          .append(ByteArrays.toHexString(rawData, " "))
          .append(", offset: ")
          .append(offset)
          .append(", length: ")
          .append(length);
      throw new IllegalRawDataException(sb.toString());
    }
    if (rawData[offset] != kind.value()) {
      StringBuilder sb = new StringBuilder(100);
      sb.append("The kind must be: ")
          .append(kind.valueAsString())
          .append(" rawData: ")
          .append(ByteArrays.toHexString(rawData, " "))
          .append(", offset: ")
          .append(offset)
          .append(", length: ")
          .append(length);
      throw new IllegalRawDataException(sb.toString());
    }

    this.length = rawData[1 + offset];
    int lengthFieldAsInt = getLengthAsInt();
    if (lengthFieldAsInt < 2) {
      throw new IllegalRawDataException(
          "The value of length field must be  more than 1 but: " + lengthFieldAsInt);
    }

    if ((lengthFieldAsInt - 2) % (INT_SIZE_IN_BYTES * 2) != 0) {
      StringBuilder sb = new StringBuilder(100);
      sb.append("The value of length field must be an integer multiple of 8 octets long but: ")
          .append(lengthFieldAsInt);
      throw new IllegalRawDataException(sb.toString());
    }
    if (length < lengthFieldAsInt) {
      StringBuilder sb = new StringBuilder(100);
      sb.append("rawData is too short. length field: ")
          .append(lengthFieldAsInt)
          .append(", rawData: ")
          .append(ByteArrays.toHexString(rawData, " "))
          .append(", offset: ")
          .append(offset)
          .append(", length: ")
          .append(length);
      throw new IllegalRawDataException(sb.toString());
    }

    for (int i = 2; i < lengthFieldAsInt; i += INT_SIZE_IN_BYTES * 2) {
      sacks.add(
          new Sack(
              ByteArrays.getInt(rawData, i + offset),
              ByteArrays.getInt(rawData, i + INT_SIZE_IN_BYTES + offset)));
    }
  }
Exemple #2
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);
  }
    private IcmpV6NeighborAdvertisementHeader(byte[] rawData, int offset, int length)
        throws IllegalRawDataException {
      if (length < OPTIONS_OFFSET) {
        StringBuilder sb = new StringBuilder(120);
        sb.append("The raw data must be more than ")
            .append(OPTIONS_OFFSET - 1)
            .append("bytes")
            .append(" to build this header. raw data: ")
            .append(ByteArrays.toHexString(rawData, " "))
            .append(", offset: ")
            .append(offset)
            .append(", length: ")
            .append(length);
        throw new IllegalRawDataException(sb.toString());
      }

      int tmp = ByteArrays.getInt(rawData, R_S_O_RESERVED_OFFSET + offset);
      this.routerFlag = (tmp & 0x80000000) != 0;
      this.solicitedFlag = (tmp & 0x40000000) != 0;
      this.overrideFlag = (tmp & 0x20000000) != 0;
      this.reserved = 0x1FFFFFFF & tmp;
      this.targetAddress = ByteArrays.getInet6Address(rawData, TARGET_ADDRESS_OFFSET + offset);
      this.options = new ArrayList<IpV6NeighborDiscoveryOption>();
      int currentOffsetInHeader = OPTIONS_OFFSET;
      while (currentOffsetInHeader < length) {
        IpV6NeighborDiscoveryOptionType type =
            IpV6NeighborDiscoveryOptionType.getInstance(rawData[currentOffsetInHeader + offset]);
        IpV6NeighborDiscoveryOption newOne;
        try {
          newOne =
              PacketFactories.getFactory(
                      IpV6NeighborDiscoveryOption.class, IpV6NeighborDiscoveryOptionType.class)
                  .newInstance(
                      rawData,
                      currentOffsetInHeader + offset,
                      length - currentOffsetInHeader,
                      type);
        } catch (Exception e) {
          break;
        }

        options.add(newOne);
        currentOffsetInHeader += newOne.length();
      }
    }
  private IpV4InternetTimestampOptionTimestampsWithAddresses(byte[] rawData, int offset, int length)
      throws IllegalRawDataException {
    if ((length % INT_SIZE_IN_BYTES) != 0) {
      StringBuilder sb = new StringBuilder(100);
      sb.append("The raw data length must be an integer multiple of 4 octets long." + " rawData: ")
          .append(ByteArrays.toHexString(rawData, " "))
          .append(", offset: ")
          .append(offset)
          .append(", length: ")
          .append(length);
      throw new IllegalRawDataException(sb.toString());
    }

    this.timestampsWithAddresses = new ArrayList<TimestampWithAddress>();
    for (int i = 0; i < length; i += INT_SIZE_IN_BYTES * 2) {
      Inet4Address address = ByteArrays.getInet4Address(rawData, i + offset);
      Integer timestamp = null;
      if (i + INT_SIZE_IN_BYTES < length) {
        timestamp = ByteArrays.getInt(rawData, i + INT_SIZE_IN_BYTES + offset);
      }
      timestampsWithAddresses.add(new TimestampWithAddress(address, timestamp));
    }
  }