All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pcap4j.packet.IpPacket Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha.6
Show newest version
/*_##########################################################################
  _##
  _##  Copyright (C) 2014  Pcap4J.org
  _##
  _##########################################################################
*/

package org.pcap4j.packet;

import org.pcap4j.packet.factory.PacketFactories;
import org.pcap4j.packet.factory.PacketFactory;
import org.pcap4j.packet.namednumber.EtherType;
import org.pcap4j.packet.namednumber.IpVersion;
import org.pcap4j.packet.namednumber.NotApplicable;
import org.pcap4j.util.ByteArrays;

/**
 * @author Kaito Yamada
 * @since pcap4j 1.3.0
 */
public final class IpPacket extends AbstractPacket {

  /**
   *
   */
  private static final long serialVersionUID = -1;

  /**
   * A static factory method.
   * This method validates the arguments by {@link ByteArrays#validateBounds(byte[], int, int)},
   * which may throw exceptions undocumented here.
   *
   * @param rawData rawData
   * @param offset offset
   * @param length length
   * @return a new Packet object representing an IP (v4 or v6) packet.
   * @throws IllegalRawDataException if parsing the raw data fails.
   */
  public static Packet newPacket(
    byte[] rawData, int offset, int length
  ) throws IllegalRawDataException {
    ByteArrays.validateBounds(rawData, offset, length);

    int ipVersion = (rawData[offset] >> 4) & 0x0f;
    PacketFactory factory
      = PacketFactories.getFactory(Packet.class, EtherType.class);
    if (ipVersion == IpVersion.IPV4.value().intValue()) {
      return factory.newInstance(rawData, offset, length, EtherType.IPV4);
    }
    if (ipVersion == IpVersion.IPV6.value().intValue()) {
      return factory.newInstance(rawData, offset, length, EtherType.IPV6);
    }
    else {
      return PacketFactories.getFactory(Packet.class, NotApplicable.class)
               .newInstance(rawData, offset, length, NotApplicable.UNKNOWN);
    }
  }

  private IpPacket() { throw new AssertionError(); }

  @Override
  public Builder getBuilder() {
    throw new UnsupportedOperationException();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy