Yarilo  v0.9
WPA2 Decrypter & Packet Analyzer
channel.h
Go to the documentation of this file.
1 #ifndef SNIFF_CHANNEL
2 #define SNIFF_CHANNEL
3 
4 #include <atomic>
5 #include <condition_variable>
6 #include <mutex>
7 #include <optional>
8 #include <queue>
9 #include <tins/packet.h>
10 
11 namespace yarilo {
12 
17 public:
21  PacketChannel() : closed(false) {}
22 
27  void send(std::unique_ptr<Tins::Packet> pkt) {
28  std::unique_lock<std::mutex> send_lock(send_mtx);
29  {
30  std::unique_lock<std::mutex> lock(mtx);
31  decrypted_packets.push(std::move(pkt));
32  }
33  cv.notify_one();
34  }
35 
41  std::optional<std::unique_ptr<Tins::Packet>> receive() {
42  std::unique_lock<std::mutex> lock(mtx);
43  cv.wait(lock, [this] { return !decrypted_packets.empty() || closed; });
44  if (closed)
45  return std::nullopt;
46 
47  std::unique_ptr<Tins::Packet> value = std::move(decrypted_packets.front());
48  decrypted_packets.pop();
49  return value;
50  }
51 
55  void close() {
56  closed = true;
57  cv.notify_one();
58  }
59 
64  bool is_closed() { return closed; }
65 
70  bool is_empty() { return decrypted_packets.empty(); }
71 
76  size_t len() { return decrypted_packets.size(); }
77 
83  std::unique_lock<std::mutex> lock_send() {
84  return std::unique_lock(send_mtx);
85  }
86 
91  void unlock_send() { send_mtx.unlock(); }
92 
93 private:
94  std::queue<std::unique_ptr<Tins::Packet>> decrypted_packets;
95  std::mutex mtx;
96  std::mutex send_mtx;
97  std::condition_variable cv;
98  std::atomic<bool> closed;
99 };
100 
101 } // namespace yarilo
102 
103 #endif // SNIFF_CHANNEL
Thread-friendly blocking channel for sending packets.
Definition: channel.h:16
std::optional< std::unique_ptr< Tins::Packet > > receive()
Definition: channel.h:41
void send(std::unique_ptr< Tins::Packet > pkt)
Definition: channel.h:27
void unlock_send()
Definition: channel.h:91
void close()
Definition: channel.h:55
std::unique_lock< std::mutex > lock_send()
Definition: channel.h:83
bool is_empty()
Definition: channel.h:70
PacketChannel()
Definition: channel.h:21
size_t len()
Definition: channel.h:76
bool is_closed()
Definition: channel.h:64
Definition: access_point.cpp:22