/**
   * Method write.
   *
   * @param packet PcapPacket
   * @throws IOException * @see org.krakenapps.pcap.PcapOutputStream#write(PcapPacket) * @see
   *     org.krakenapps.pcap.PcapOutputStream#write(PcapPacket)
   */
  public void write(PcapPacket packet) throws IOException {
    PacketHeader packetHeader = packet.getPacketHeader();

    int tsSec = packetHeader.getTsSec();
    int tsUsec = packetHeader.getTsUsec();
    int inclLen = packetHeader.getInclLen();
    int origLen = packetHeader.getOrigLen();

    addInt(tsSec);
    addInt(tsUsec);
    addInt(inclLen);
    addInt(origLen);

    Buffer payload = packet.getPacketData();

    try {
      payload.mark();
      while (true) {
        list.add(payload.get());
      }
    } catch (BufferUnderflowException e) {
      // LOG.debug("Ignorable exception while writing packet", e);
      payload.reset();
    }

    cachedPacketNum++;
    if (cachedPacketNum == MAX_CACHED_PACKET_NUMBER) {
      flush();
    }
  }
Exemplo n.º 2
0
  private static TcpPacket parse(
      IpPacket p, InetAddress source, InetAddress destination, int tcpLength, Buffer data)
      throws BufferUnderflowException {
    TcpPacket s = new TcpPacket();

    s.ipPacket = p;
    s.tcpLength = tcpLength;
    s.sourceAddr = source;
    s.destinationAddr = destination;
    s.srcPort = data.getUnsignedShort();
    s.dstPort = data.getUnsignedShort();

    s.sessionKey = new TcpSessionKeyImpl(s.sourceAddr, s.destinationAddr, s.srcPort, s.dstPort);

    s.seq = data.getInt();
    s.ack = data.getInt();
    s.parseDataOffsetAndFlags(data);
    s.window = data.getUnsignedShort();
    s.checksum = data.getUnsignedShort();
    s.urgentPointer = data.getUnsignedShort();
    s.parseOptions(data);
    s.parseData(data);

    return s;
  }
Exemplo n.º 3
0
 @Override
 public SmbData parseResponse(SmbHeader h, Buffer b, SmbSession session) {
   WritePrintFileResponse data = new WritePrintFileResponse();
   data.setWordCount(b.get());
   data.setByteCount(ByteOrderConverter.swap(b.getShort()));
   return data;
 }
Exemplo n.º 4
0
  @Test
  public void bufferAddTest2() {
    Buffer b = new ChainBuffer();
    Buffer b1 = new ChainBuffer();

    setData(b, b1);
    b.addLast(b1);
    printResult(b);
  }
Exemplo n.º 5
0
 private void printResult(Buffer b) {
   int capa = b.readableBytes();
   byte[] t = new byte[capa];
   b.gets(t);
   System.out.println();
   for (byte t1 : t) {
     System.out.printf("%d:", t1);
   }
 }
Exemplo n.º 6
0
  @Test
  public void bufferAddTest5() {
    Buffer b = new ChainBuffer();
    Buffer b1 = new ChainBuffer();

    setData(b, b1);
    byte[] temp = new byte[11];
    b1.gets(temp);
    b1.discardReadBytes();
    b.addLast(b1);
    printResult(b);
  }
Exemplo n.º 7
0
 @Override
 public TransStruct parse(Buffer b, SmbSession session) {
   creationDate = ByteOrderConverter.swap(b.getShort());
   creationTime = ByteOrderConverter.swap(b.getShort());
   lastAccessDate = ByteOrderConverter.swap(b.getShort());
   lastaccessTime = ByteOrderConverter.swap(b.getShort());
   lastWriteDate = ByteOrderConverter.swap(b.getShort());
   lastWriteTime = ByteOrderConverter.swap(b.getShort());
   reserved = new byte[10];
   b.gets(reserved);
   return this;
 }
Exemplo n.º 8
0
  private void parseOptions(Buffer dataBuffer) {
    int headerLength = dataOffset * 4;
    if (headerLength <= 20) return;

    int optionLength = headerLength - 20;
    options = new byte[optionLength];

    for (int i = 0; i < optionLength; i++) options[i] = dataBuffer.get();

    if ((optionLength % 4) == 0) return;

    for (int i = 0; i < (optionLength % 4); i++) padding[i] = dataBuffer.get();
  }
Exemplo n.º 9
0
  @Test
  public void bufferAddTest() {
    Buffer b = new ChainBuffer();
    Buffer b1 = new ChainBuffer();

    setData(b, b1);
    /* beforehand fetch to 2 elements */
    b1.get();
    b1.get();
    b1.discardReadBytes();
    b.addLast(b1);
    printResult(b);
  }
Exemplo n.º 10
0
  @Test
  public void bufferAddTest4() {
    Buffer b = new ChainBuffer();
    Buffer b1 = new ChainBuffer();

    setData(b, b1);
    byte[] temp = new byte[12];
    b1.gets(temp);
    /* case: Out of range then don't discard. */
    b1.discardReadBytes();
    b.addLast(b1);
    printResult(b);
  }
Exemplo n.º 11
0
  @Override
  public Buffer getBuffer() {
    ByteBuffer hbuf = ByteBuffer.allocate(20); // hardcoded header size
    hbuf.putShort((short) srcPort);
    hbuf.putShort((short) dstPort);
    hbuf.putInt(seq);
    hbuf.putInt(ack);
    hbuf.put((byte) (dataOffset << 4));
    hbuf.put((byte) flags);
    hbuf.putShort((short) window);
    hbuf.putShort((short) checksum);
    hbuf.putShort((short) urgentPointer);

    Buffer buf = new ChainBuffer(hbuf.array());
    buf.addLast(data);
    return buf;
  }
Exemplo n.º 12
0
  private void setData(Buffer b1, Buffer b2) {
    byte[] t = new byte[] {101, 102, 103, 104};
    byte[] t1 = new byte[] {105, 106, 107, 108, 109};
    byte[] t2 = new byte[] {110, 111, 112};

    byte[] r = new byte[] {1, 2, 3, 4};
    byte[] r1 = new byte[] {5, 6, 7, 8, 9};
    byte[] r2 = new byte[] {10, 11, 12};

    if (b1 != null) {
      b1.addLast(t);
      b1.addLast(t1);
      b1.addLast(t2);
    }

    if (b2 != null) {
      b2.addLast(r);
      b2.addLast(r1);
      b2.addLast(r2);
    }
  }
Exemplo n.º 13
0
  private void parseData(Buffer dataBuffer) throws BufferUnderflowException {
    /* set length of data */
    dataLength = tcpLength - (dataOffset * 4);

    if (dataLength > 0) {
      try {
        dataBuffer.discardReadBytes();
        data = dataBuffer;
      } catch (BufferUnderflowException e) {
        data = null;
        isJumbo = true;
      }
    }
  }
Exemplo n.º 14
0
 @Override
 public TransStruct parse(Buffer b, SmbSession session) {
   creationTime = ByteOrderConverter.swap(b.getLong());
   lastAccessTime = ByteOrderConverter.swap(b.getLong());
   lastWriteTime = ByteOrderConverter.swap(b.getLong());
   lastChangeTime = ByteOrderConverter.swap(b.getLong());
   extFileAttributes = ExtFileAttributes.parse(ByteOrderConverter.swap(b.getInt()));
   reserved = ByteOrderConverter.swap(b.getInt());
   return this;
 }
Exemplo n.º 15
0
  @Test
  public void bufferAddTest3() {
    Buffer b = new ChainBuffer();
    Buffer b1 = new ChainBuffer();

    setData(b, b1);
    b1.get();
    b1.get();
    b1.get();
    b1.get();
    b1.discardReadBytes();
    b.addLast(b1);
    printResult(b);
  }
Exemplo n.º 16
0
 @Override
 public SmbData parseRequest(SmbHeader h, Buffer b, SmbSession session) {
   WritePrintFileRequest data = new WritePrintFileRequest();
   byte[] buff;
   data.setWordCount(b.get());
   data.setFid(ByteOrderConverter.swap(b.getShort()));
   data.setByteCount(ByteOrderConverter.swap(b.getShort()));
   if (b.readableBytes() != data.getByteCount()) {
     data.setMalformed(true);
     return data;
   }
   data.setBufferFormat(b.get());
   data.setDataLength(ByteOrderConverter.swap(b.getShort()));
   buff = new byte[data.getDataLength()];
   b.gets(buff);
   data.setData(buff);
   return data;
 }
Exemplo n.º 17
0
    @Override
    public TcpPacket build() {
      // resolve all values
      if (dstIp == null) throw new IllegalStateException("destination ip not found");

      if (srcIp == null) {
        PcapDeviceMetadata metadata = PcapDeviceManager.getDeviceMetadata(dstIp);
        if (metadata == null)
          throw new IllegalArgumentException(
              "route not found for destination " + dstIp.getHostAddress());

        srcIp = metadata.getInet4Address();
      }

      if (srcPort == null) srcPort = 40000;

      if (dstPort == null) throw new IllegalStateException("destination port not found");

      // set
      TcpPacket p = new TcpPacket();
      p.sourceAddr = srcIp;
      p.destinationAddr = dstIp;
      p.srcPort = srcPort;
      p.dstPort = dstPort;
      p.seq = seq;
      p.ack = ack;
      p.dataOffset = 5; // default header size 20 (5 * 4)
      p.flags = flags;
      p.window = window;
      p.data = data;
      if (data != null) p.dataLength = data.readableBytes();

      p.tcpLength = (p.dataOffset * 4) + p.dataLength;
      p.checksum = TcpChecksum.sum(p);
      return p;
    }
Exemplo n.º 18
0
 private void parseDataOffsetAndFlags(Buffer dataBuffer) {
   byte dataOffsetAndReserved = dataBuffer.get();
   byte reservedAndFlags = dataBuffer.get();
   dataOffset = ((dataOffsetAndReserved >> 4) & 0x0f) & 0xff;
   flags = (reservedAndFlags & 0x3f) & 0xff;
 }