/**
   * Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
   *
   * @param buffer buffer containing both state and data in the form
   *     <pre>
   * +--------------+-------------+
   * | packet state | packet data |
   * +--------------+-------------+
   * </pre>
   */
  public JMemoryPacket(JBuffer buffer) {
    super(POINTER);

    header.setWirelen(buffer.size());

    final int len = buffer.size();
    JBuffer b = getMemoryBuffer(len);

    b.transferFrom(buffer); // Make a buffer to buffer copy

    peer(b, 0, len);

    header.setWirelen(len);
  }
  /**
   * Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that
   * buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are
   * then peered with the internal buffer containing the copy of the supplied buffer
   *
   * @param buffer buffer containing both state and data in the form
   *     <pre>
   *          +--------------+-------------+
   *          | packet state | packet data |
   *          +--------------+-------------+
   * </pre>
   *
   * @return number of bytes copied
   */
  public int transferStateAndDataFrom(JBuffer buffer) {
    final int len = buffer.size();
    JBuffer b = getMemoryBuffer(len);

    b.transferFrom(buffer);

    return peerStateAndData(b, 0);
  }
 /**
  * Initialize the network packet from the pcap packet.
  *
  * @param packet The captured packet in pcap format.
  */
 private void parsePcapPacket(final PcapPacket packet) {
   this.length = packet.getTotalSize();
   this.timestamp = new Date(packet.getCaptureHeader().timestampInMillis());
   final Tcp tcp = new Tcp();
   if (packet.hasHeader(tcp)) {
     packet.getHeader(tcp);
     this.destinationPort = tcp.destination();
     this.sourcePort = tcp.source();
     this.isAck = tcp.flags_ACK();
     this.isRst = tcp.flags_RST();
     this.isSyn = tcp.flags_SYN();
   }
   final Ip4 ip = new Ip4();
   if (packet.hasHeader(ip)) {
     packet.getHeader(ip);
     this.destinationIp = FormatUtils.ip(ip.destination());
     this.sourceIp = FormatUtils.ip(ip.source());
   }
   final JBuffer storage = new JBuffer(JMemory.Type.POINTER);
   final JBuffer buffer = tcp.peerPayloadTo(storage);
   if (buffer != null) {
     this.data = buffer.getByteArray(0, buffer.size());
   }
 }
Beispiel #4
0
 /**
  * Header length.
  *
  * @param buffer the buffer
  * @param offset the offset
  * @return the int
  */
 @HeaderLength
 public static int headerLength(JBuffer buffer, int offset) {
   return buffer.size() - offset;
 }