Loading tools/rootcanal/include/pcap.h 0 → 100644 +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*)µseconds, 4); output.write((char*)&captured_packet_length, 4); output.write((char*)&original_packet_length, 4); } } // namespace rootcanal::pcap tools/rootcanal/model/hci/hci_sniffer.cc +7 −37 Original line number Diff line number Diff line Loading @@ -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*)µseconds, 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()); Loading tools/rootcanal/model/hci/hci_sniffer.h +0 −2 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ #pragma once #include <chrono> #include <cstdint> #include <fstream> #include <memory> Loading Loading @@ -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 Loading
tools/rootcanal/include/pcap.h 0 → 100644 +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*)µseconds, 4); output.write((char*)&captured_packet_length, 4); output.write((char*)&original_packet_length, 4); } } // namespace rootcanal::pcap
tools/rootcanal/model/hci/hci_sniffer.cc +7 −37 Original line number Diff line number Diff line Loading @@ -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*)µseconds, 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()); Loading
tools/rootcanal/model/hci/hci_sniffer.h +0 −2 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ #pragma once #include <chrono> #include <cstdint> #include <fstream> #include <memory> Loading Loading @@ -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