Yarilo v0.9.9
WPA2 Decrypter & Packet Analyzer
Loading...
Searching...
No Matches
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 <iostream>
7#include <mutex>
8#include <optional>
9#include <queue>
10#include <tins/packet.h>
11#include <tins/tins.h>
12
13namespace yarilo {
14
19public:
23 PacketChannel() : closed(false) {}
24
29 void send(std::unique_ptr<Tins::Packet> pkt) {
30 std::unique_lock<std::mutex> send_lock(send_mtx);
31 {
32 std::unique_lock<std::mutex> lock(mtx);
33 decrypted_packets.push(std::move(pkt));
34 }
35 cv.notify_one();
36 }
37
43 std::optional<std::unique_ptr<Tins::Packet>> receive() {
44 std::unique_lock<std::mutex> lock(mtx);
45 cv.wait(lock, [this] { return !decrypted_packets.empty() || closed; });
46 if (closed)
47 return std::nullopt;
48
49 std::unique_ptr<Tins::Packet> value = std::move(decrypted_packets.front());
50 decrypted_packets.pop();
51
52 return value;
53 }
54
58 void close() {
59 closed = true;
60 cv.notify_one();
61 }
62
67 bool is_closed() { return closed; }
68
73 bool is_empty() { return decrypted_packets.empty(); }
74
79 size_t len() { return decrypted_packets.size(); }
80
86 std::unique_lock<std::mutex> lock_send() {
87 return std::unique_lock(send_mtx);
88 }
89
94 void unlock_send() { send_mtx.unlock(); }
95
96private:
97 std::queue<std::unique_ptr<Tins::Packet>> decrypted_packets;
98 std::mutex mtx;
99 std::mutex send_mtx;
100 std::condition_variable cv;
101 std::atomic<bool> closed;
102};
103
104} // namespace yarilo
105
106#endif // SNIFF_CHANNEL
Thread-friendly blocking channel for sending packets.
Definition channel.h:18
void send(std::unique_ptr< Tins::Packet > pkt)
Definition channel.h:29
void unlock_send()
Definition channel.h:94
std::optional< std::unique_ptr< Tins::Packet > > receive()
Definition channel.h:43
void close()
Definition channel.h:58
bool is_empty()
Definition channel.h:73
std::unique_lock< std::mutex > lock_send()
Definition channel.h:86
PacketChannel()
Definition channel.h:23
size_t len()
Definition channel.h:79
bool is_closed()
Definition channel.h:67
Definition access_point.cpp:22