2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2009/12/13 13:45:13 $
30 #include "ether_cap.h"
32 #include "stg/common.h"
33 #include "stg/raw_ip_packet.h"
34 #include "stg/traffcounter.h"
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
45 #include <sys/types.h>
47 #include <linux/if_ether.h>
48 #include <linux/if_packet.h>
49 #include <sys/ioctl.h>
54 extern "C" STG::Plugin* GetPlugin()
56 static ETHER_CAP plugin;
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 std::string ETHER_CAP::GetVersion() const
64 return "cap_ether v.1.2";
66 //-----------------------------------------------------------------------------
67 ETHER_CAP::ETHER_CAP()
71 logger(STG::PluginLogger::get("cap_ether"))
74 //-----------------------------------------------------------------------------
75 int ETHER_CAP::Start()
82 errorStr = "Cannot open socket!";
83 printfd(__FILE__, "Cannot open socket\n");
87 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
91 //-----------------------------------------------------------------------------
97 m_thread.request_stop();
99 //5 seconds to thread stops itself
100 for (int i = 0; i < 25 && isRunning; i++)
102 struct timespec ts = {0, 200000000};
103 nanosleep(&ts, NULL);
105 //after 5 seconds waiting thread still running. now killing it
114 //-----------------------------------------------------------------------------
115 void ETHER_CAP::Run(std::stop_token token)
118 sigfillset(&signalSet);
119 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
131 char ethip[sizeof(ETH_IP)];
133 memset(ðip, 0, sizeof(ETH_IP));
135 ETH_IP * ethIP = static_cast<ETH_IP *>(static_cast<void *>(ðip));
136 ethIP->rp.dataLen = -1;
140 while (!token.stop_requested())
142 if (EthCapRead(ðip, 68 + 14, &iface))
147 if (ethIP->ethHdr[7] != 0x8)
150 traffCnt->process(ethIP->rp);
155 //-----------------------------------------------------------------------------
156 int ETHER_CAP::EthCapOpen()
158 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
160 logger("Cannot create socket: %s", strerror(errno));
163 //-----------------------------------------------------------------------------
164 int ETHER_CAP::EthCapClose()
169 //-----------------------------------------------------------------------------
170 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
172 struct sockaddr_ll addr;
175 if (!WaitPackets(capSock))
180 addrLen = sizeof(addr);
182 if (recvfrom(capSock, static_cast<char*>(buffer) + 2, blen, 0, reinterpret_cast<sockaddr *>(&addr), &addrLen) < 0)
184 logger("recvfrom error: %s", strerror(errno));