Debian Codegraph @

Source/Core/Common/PcapFile.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/PcapFile.h"
5
6 #include <chrono>
7
8 #include "Common/CommonTypes.h"
9 #include "Common/IOFile.h"
10
11 namespace Common
12 {
13 namespace
14 {
15 const u32 PCAP_MAGIC = 0xa1b2c3d4;
16 const u16 PCAP_VERSION_MAJOR = 2;
17 const u16 PCAP_VERSION_MINOR = 4;
18 const u32 PCAP_CAPTURE_LENGTH = 65535;
19
20 // Designed to be directly written into the PCAP file. The PCAP format is
21 // endian independent, so this works just fine.
22 #pragma pack(push, 1)
23 struct PCAPHeader
24 {
25 u32 magic_number;
26 u16 version_major;
27 u16 version_minor;
28 s32 tz_offset; // Offset in seconds from the GMT timezone.
29 u32 ts_accuracy; // In practice, 0.
30 u32 capture_length; // Size at which we truncate packets.
31 u32 data_link_type;
32 };
33
34 struct PCAPRecordHeader
35 {
36 u32 ts_sec;
37 u32 ts_usec;
38 u32 size_in_file; // Size after eventual truncation.
39 u32 real_size; // Size before eventual truncation.
40 };
41 #pragma pack(pop)
42
43 } // namespace
44
45 void PCAP::AddHeader(u32 link_type)
46 {
47 PCAPHeader hdr = {PCAP_MAGIC, PCAP_VERSION_MAJOR, PCAP_VERSION_MINOR, 0,
48 0, PCAP_CAPTURE_LENGTH, link_type};
49 m_fp->WriteBytes(&hdr, sizeof(hdr));
50 }
51
52 // Not thread-safe, concurrency between multiple calls to IOFile::WriteBytes.
53 void PCAP::AddPacket(const u8* bytes, size_t size)
54 {
55 std::chrono::system_clock::time_point now(std::chrono::system_clock::now());
56 auto ts = now.time_since_epoch();
57 PCAPRecordHeader rec_hdr = {
58 (u32)std::chrono::duration_cast<std::chrono::seconds>(ts).count(),
59 (u32)(std::chrono::duration_cast<std::chrono::microseconds>(ts).count() % 1000000), (u32)size,
60 (u32)size};
61 m_fp->WriteBytes(&rec_hdr, sizeof(rec_hdr));
62 m_fp->WriteBytes(bytes, size);
63 }
64 } // namespace Common