SatCat5
file_pcap.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
19 
20 #pragma once
21 
22 #include <hal_posix/file_io.h>
23 #include <satcat5/datetime.h>
24 
25 // Buffer size must be large enough for one full-size Ethernet frame.
26 #ifndef SATCAT5_PCAP_BUFFSIZE
27 #define SATCAT5_PCAP_BUFFSIZE 1600
28 #endif
29 
30 namespace satcat5 {
31  namespace io {
35  constexpr u16 LINKTYPE_ETHERNET = 1; // Ethernet (Default)
36  constexpr u16 LINKTYPE_USER0 = 147; // User-defined type #0
37  constexpr u16 LINKTYPE_AOS = 222; // CCSDS-AOS Space Data Link
38  constexpr u16 LINKTYPE_USER(u16 x) // User-defined type #0-15
39  { return LINKTYPE_USER0 + x; } // Reserved for private use
41 
44  public:
46  explicit ReadPcap(const char* filename = 0);
47 
50  void open(const char* filename);
51  inline void open(const std::string& filename)
52  { open(filename.c_str()); }
54 
56  inline void close() {m_file.close();}
57 
59  void read_finalize() override;
60 
61  protected:
62  // Read specific block formats.
63  void pcap_hdr(); // Start-of-file header
64  bool pcap_dat(); // Packet record
65  bool pcapng_blk(); // Read next block (any type)
66  void pcapng_shb(); // Section header block
67  void pcapng_idb(); // Interface description block
68  void pcapng_spb(); // Simple packet block
69  void pcapng_epb(); // Enhanced packet block
70  void pcapng_skip(); // Any unsupported block
71 
72  // Shortcuts for reading from the file.
73  inline u16 file_rd16()
74  { return m_mode_be ? m_file.read_u16() : m_file.read_u16l(); }
75  inline u32 file_rd32()
76  { return m_mode_be ? m_file.read_u32() : m_file.read_u32l(); }
77 
78  // Internal state and working buffer.
80  bool m_mode_be; // Big-endian file?
81  bool m_mode_ng; // PCAPNG format?
82  bool m_mode_pc; // PCAPNG format?
83  u32 m_trim; // Ignore FCS bytes?
84  u8 m_buff[SATCAT5_PCAP_BUFFSIZE];
85  };
86 
89  public:
93  explicit WritePcap(bool pcapng = true);
94 
97  void open(const char* filename, u16 type = LINKTYPE_ETHERNET);
98  inline void open(const std::string& filename, u16 type = LINKTYPE_ETHERNET)
99  { open(filename.c_str(), type); }
101 
103  inline void close()
104  { m_file.close(); }
105 
110  u32 add_interface(const char* name, u16 type = LINKTYPE_ETHERNET);
111 
114  inline void set_interface(u32 id)
115  { m_interface_next = id; }
116 
120  { m_pass = wr; }
121 
123  bool write_finalize() override;
124 
125  protected:
126  // Internal state and working buffer.
127  satcat5::datetime::Clock m_clock;
129  satcat5::io::Writeable* m_pass;
130  const bool m_mode_ng; // PCAPNG format?
131  u32 m_interface_count, m_interface_next;
132  u8 m_buff[SATCAT5_PCAP_BUFFSIZE];
133  };
134 
144  public:
149  const char* label, u16 type = LINKTYPE_ETHERNET);
150 
152  bool write_finalize() override;
153 
154  protected:
155  satcat5::io::WritePcap* const m_pcap;
156  const u32 m_id;
157  };
158  }
159 }
Real-time clock for tracking date/time.
Definition: datetime.h:169
Ephemeral Readable interface for a simple array.
Definition: io_readable.h:206
Ephemeral Writeable interface for a simple array.
Definition: io_writeable.h:127
Read bytes or packets from a file.
Definition: file_io.h:48
Write bytes or packets to a file.
Definition: file_io.h:16
Read packet stream from a file.
Definition: file_pcap.h:43
void open(const char *filename)
Open the specified file.
Definition: file_pcap.cc:51
void close()
Close the current file, if any.
Definition: file_pcap.h:56
void open(const std::string &filename)
Open the specified file.
Definition: file_pcap.h:51
ReadPcap(const char *filename=0)
Open the input file and autodetect format.
Definition: file_pcap.cc:40
void read_finalize() override
Override end-of-packet handling.
Definition: file_pcap.cc:88
Store packet stream to a file.
Definition: file_pcap.h:88
u32 add_interface(const char *name, u16 type=LINKTYPE_ETHERNET)
Write a new interface description block (PCAPNG only).
Definition: file_pcap.cc:317
void close()
Close the current file, if any.
Definition: file_pcap.h:103
void set_passthrough(satcat5::io::Writeable *wr)
Passthrough mode carbon-copies each packet to another Writeable object, using the internal working bu...
Definition: file_pcap.h:119
void open(const char *filename, u16 type=LINKTYPE_ETHERNET)
Open the specified file, and optionally specify LinkType.
Definition: file_pcap.cc:285
void open(const std::string &filename, u16 type=LINKTYPE_ETHERNET)
Open the specified file, and optionally specify LinkType.
Definition: file_pcap.h:98
void set_interface(u32 id)
Set interface ID number for the next call to write_finalize.
Definition: file_pcap.h:114
WritePcap(bool pcapng=true)
Create the capture object and set PCAP or PCAPNG mode.
Definition: file_pcap.cc:274
bool write_finalize() override
Override end-of-packet handling.
Definition: file_pcap.cc:343
Helper object for writing capture files with multiple sources.
Definition: file_pcap.h:143
WritePcapInterface(satcat5::io::WritePcap *pcap, const char *label, u16 type=LINKTYPE_ETHERNET)
Constructor adds a new interface to a packet-capture.
Definition: file_pcap.cc:394
bool write_finalize() override
Override end-of-packet handling.
Definition: file_pcap.cc:402
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Wrapper class for forwarding writes to another object.
Definition: io_writeable.h:218
Real-time clock conversion functions.
constexpr u16 LINKTYPE_AOS
Define selected LinkType codes from the official registry: https://www.tcpdump.org/linktypes....
Definition: file_pcap.h:37
constexpr u16 LINKTYPE_USER(u16 x)
Define selected LinkType codes from the official registry: https://www.tcpdump.org/linktypes....
Definition: file_pcap.h:38
constexpr u16 LINKTYPE_ETHERNET
Define selected LinkType codes from the official registry: https://www.tcpdump.org/linktypes....
Definition: file_pcap.h:35
constexpr u16 LINKTYPE_USER0
Define selected LinkType codes from the official registry: https://www.tcpdump.org/linktypes....
Definition: file_pcap.h:36