PcapFile.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2014 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // PCAP is a standard file format for network capture files. This also extends
  4. // to any capture of packetized intercommunication data. This file provides a
  5. // class called PCAP which is a very light wrapper around the file format,
  6. // allowing only creating a new PCAP capture file and appending packets to it.
  7. //
  8. // Example use:
  9. // PCAP pcap(new IOFile("test.pcap", "wb"));
  10. // pcap.AddPacket(pkt); // pkt is automatically casted to u8*
  11. #pragma once
  12. #include <cstddef>
  13. #include <memory>
  14. #include "Common/CommonTypes.h"
  15. #include "Common/IOFile.h"
  16. namespace Common
  17. {
  18. class PCAP final
  19. {
  20. public:
  21. enum class LinkType : u32
  22. {
  23. Ethernet = 1, // IEEE 802.3 Ethernet
  24. User = 147, // Reserved for internal use
  25. };
  26. // Takes ownership of the file object. Assumes the file object is already
  27. // opened in write mode.
  28. explicit PCAP(File::IOFile* fp, LinkType link_type = LinkType::User) : m_fp(fp)
  29. {
  30. AddHeader(static_cast<u32>(link_type));
  31. }
  32. template <typename T>
  33. void AddPacket(const T& obj)
  34. {
  35. AddPacket(reinterpret_cast<const u8*>(&obj), sizeof(obj));
  36. }
  37. void AddPacket(const u8* bytes, size_t size);
  38. private:
  39. void AddHeader(u32 link_type);
  40. std::unique_ptr<File::IOFile> m_fp;
  41. };
  42. } // namespace Common