11#include <sys/eventfd.h>
12#include <netinet/in.h>
13#include <linux/if_packet.h>
14#include <net/ethernet.h>
45 static void*
run(
void*
arg);
69 std::atomic<bool> _running;
96 if (_running.load()) {
103 _running.store(
true, std::memory_order_release);
114 _running.store(
false, std::memory_order_release);
125 if (_receive_thread != 0) {
139void SocketEngine::setUpSocket() {
146 throw std::runtime_error(
"Failed to create SocketEngine::_sock_fd!");
155 std::memset(&
ifr, 0,
sizeof(
ifr));
160 perror(
"ioctl SIOCGIFINDEX");
161 throw std::runtime_error(
"Failed to retrieve interface index!");
168 std::memset(&
ifr, 0,
sizeof(
ifr));
172 perror(
"ioctl SIOCGIFHWADDR");
173 throw std::runtime_error(
"Failed to retrieve MAC address!");
181 std::memset(&
sll, 0,
sizeof(
sll));
188 throw std::runtime_error(
"Failed to bind SocketEngine::_sock_fd to interface!");
194void SocketEngine::setUpEpoll() {
201 throw std::runtime_error(
"Failed to create SocketEngine::_ep_fd!");
211 throw std::runtime_error(
"Failed to bind SocketEngine::_sock_fd to epoll!");
219 perror(
"epoll_ctl stop_ev");
220 throw std::runtime_error(
"Failed to bind SocketEngine::_stop_ev to epoll!");
230 if (
frame ==
nullptr) {
237 db<SocketEngine>(
ERR) <<
"[SocketEngine] Attempted to send frame with invalid size: " << size <<
"\n";
243 db<SocketEngine>(
ERR) <<
"[SocketEngine] Attempted to send while engine is stopping/stopped\n";
270 while (
engine->running()) {
285 for (
int i = 0;
i <
n; ++
i) {
293 }
else if (
fd ==
engine->_stop_ev) {
307void SocketEngine::receive() {
323 if (bytes_received < 0) {
333 db<SocketEngine>(
ERR) <<
"[SocketEngine] [receive()] Received undersized frame (" << bytes_received <<
" bytes)\n";
341 this->handle(&
frame,
static_cast<unsigned int>(bytes_received));
345 return _running.load(std::memory_order_acquire);
static std::string mac_to_string(Address addr)
Definition ethernet.h:36
static constexpr unsigned int MAC_SIZE
Definition ethernet.h:13
static constexpr unsigned int HEADER_SIZE
Definition ethernet.h:26
Definition socketEngine.h:27
int _sock_fd
Definition socketEngine.h:61
virtual ~SocketEngine()
Definition socketEngine.h:83
Ethernet::Address _mac_address
Definition socketEngine.h:64
int send(Ethernet::Frame *frame, unsigned int size)
Definition socketEngine.h:226
const bool running()
Definition socketEngine.h:344
int _ep_fd
Definition socketEngine.h:62
int _if_index
Definition socketEngine.h:63
SocketEngine()
Definition socketEngine.h:75
static void * run(void *arg)
Definition socketEngine.h:263
const Ethernet::Address & mac_address()
Definition socketEngine.h:348
static const char * INTERFACE()
Definition socketEngine.h:30
void stop()
Definition socketEngine.h:109
void start()
Definition socketEngine.h:93
@ INF
Definition debug.h:208
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
@ ERR
Definition debug.h:162
@ TRC
Definition debug.h:231
@ WRN
Definition debug.h:185
std::uint8_t bytes[MAC_SIZE]
Definition ethernet.h:17