Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 74f6b6aa authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "RootCanal: Extract pcap writing logic outside hci_sniffer" am:...

Merge "RootCanal: Extract pcap writing logic outside hci_sniffer" am: 62f86912 am: 2d61f04f am: 90c9c20d am: 33fc56a2 am: d30db5ff

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2129432



Change-Id: I9104eece69d9ab55c9cb7d496b233c8cdf0f3400
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 630ab914 d30db5ff
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <chrono>
#include <cstdint>
#include <limits>
#include <ostream>

namespace rootcanal::pcap {

using namespace std::literals;

static void WriteHeader(std::ostream& output, uint32_t linktype) {
  // https://tools.ietf.org/id/draft-gharris-opsawg-pcap-00.html#name-file-header
  uint32_t magic_number = 0xa1b2c3d4;
  uint16_t major_version = 2;
  uint16_t minor_version = 4;
  uint32_t reserved1 = 0;
  uint32_t reserved2 = 0;
  uint32_t snaplen = std::numeric_limits<uint32_t>::max();

  output.write((char*)&magic_number, 4);
  output.write((char*)&major_version, 2);
  output.write((char*)&minor_version, 2);
  output.write((char*)&reserved1, 4);
  output.write((char*)&reserved2, 4);
  output.write((char*)&snaplen, 4);
  output.write((char*)&linktype, 4);
}

static void WriteRecordHeader(std::ostream& output, uint32_t length) {
  auto time = std::chrono::system_clock::now().time_since_epoch();

  // https://tools.ietf.org/id/draft-gharris-opsawg-pcap-00.html#name-packet-record
  uint32_t seconds = time / 1s;
  uint32_t microseconds = (time % 1s) / 1ms;
  uint32_t captured_packet_length = length;
  uint32_t original_packet_length = length;

  output.write((char*)&seconds, 4);
  output.write((char*)&microseconds, 4);
  output.write((char*)&captured_packet_length, 4);
  output.write((char*)&original_packet_length, 4);
}

}  // namespace rootcanal::pcap
+7 −37
Original line number Diff line number Diff line
@@ -16,65 +16,35 @@

#include "hci_sniffer.h"

#include <chrono>
#include <limits>

using namespace std::literals;
#include "pcap.h"

namespace rootcanal {

static void writeHeader(std::ostream* stream) {
  // https://tools.ietf.org/id/draft-gharris-opsawg-pcap-00.xml#file-header
  uint32_t magic_number = 0xa1b2c3d4;
  uint16_t major_version = 2;
  uint16_t minor_version = 4;
  uint32_t reserved1 = 0;
  uint32_t reserved2 = 0;
  uint32_t snaplen = std::numeric_limits<uint32_t>::max();
  uint32_t linktype = 201;  // http://www.tcpdump.org/linktypes.html
                            // LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR

  stream->write((char*)&magic_number, 4);
  stream->write((char*)&major_version, 2);
  stream->write((char*)&minor_version, 2);
  stream->write((char*)&reserved1, 4);
  stream->write((char*)&reserved2, 4);
  stream->write((char*)&snaplen, 4);
  stream->write((char*)&linktype, 4);
}

HciSniffer::HciSniffer(std::shared_ptr<HciTransport> transport,
                       std::shared_ptr<std::ostream> outputStream)
    : transport_(transport), start_(std::chrono::steady_clock::now()) {
    : transport_(transport) {
  SetOutputStream(outputStream);
}

void HciSniffer::SetOutputStream(std::shared_ptr<std::ostream> outputStream) {
  output_ = outputStream;
  if (output_) {
    writeHeader(output_.get());
    uint32_t linktype = 201;  // http://www.tcpdump.org/linktypes.html
                              // LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR

    pcap::WriteHeader(*output_, linktype);
  }
}

void HciSniffer::AppendRecord(PacketDirection packet_direction,
                              PacketType packet_type,
                              const std::vector<uint8_t>& packet) {
  auto time = std::chrono::steady_clock::now() - start_;

  // https://tools.ietf.org/id/draft-gharris-opsawg-pcap-00.xml#rfc.section.5
  uint32_t seconds = time / 1s;
  uint32_t microseconds = (time % 1s) / 1ms;
  uint32_t captured_packet_length = 4 + 1 + packet.size();
  uint32_t original_packet_length = captured_packet_length;
  pcap::WriteRecordHeader(*output_, 4 + 1 + packet.size());

  // http://www.tcpdump.org/linktypes.html LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR
  uint32_t direction = static_cast<uint32_t>(packet_direction);
  uint8_t idc = static_cast<uint8_t>(packet_type);

  output_->write((char*)&seconds, 4);
  output_->write((char*)&microseconds, 4);
  output_->write((char*)&captured_packet_length, 4);
  output_->write((char*)&original_packet_length, 4);
  output_->write((char*)&direction, 4);
  output_->write((char*)&idc, 1);
  output_->write((char*)packet.data(), packet.size());
+0 −2
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

#pragma once

#include <chrono>
#include <cstdint>
#include <fstream>
#include <memory>
@@ -73,7 +72,6 @@ class HciSniffer : public HciTransport {

  std::shared_ptr<std::ostream> output_;
  std::shared_ptr<HciTransport> transport_;
  std::chrono::time_point<std::chrono::steady_clock> start_;
};

}  // namespace rootcanal