File indexing completed on 2024-06-23 05:27:53

0001 /*
0002     SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #ifndef CAPTURE_H
0008 #define CAPTURE_H
0009 
0010 #include <atomic>
0011 #include <condition_variable>
0012 #include <deque>
0013 #include <mutex>
0014 #include <thread>
0015 
0016 #include "Packet.h"
0017 
0018 class pcap;
0019 
0020 class Capture
0021 {
0022 public:
0023     Capture(const std::string &interface = std::string{});
0024     ~Capture();
0025 
0026     bool start();
0027     std::string lastError() const;
0028     void reportStatistics();
0029     Packet nextPacket();
0030 
0031     void handlePacket(const struct pcap_pkthdr *header, const uint8_t *data);
0032 
0033 private:
0034     void loop();
0035     bool checkError(int result);
0036 
0037     std::string m_interface;
0038     std::string m_error;
0039     std::thread m_thread;
0040     std::mutex m_mutex;
0041     std::condition_variable m_condition;
0042     std::deque<Packet> m_queue;
0043 
0044     int m_packetCount = 0;
0045     int m_droppedPackets = 0;
0046 
0047     pcap *m_pcap = nullptr;
0048 };
0049 
0050 #endif // CAPTURE_H