]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
Move projects back into subfolder.
[stg.git] / projects / stargazer / plugins / capture / ether_linux / ether_cap.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18 Date: 18.09.2002
19 */
20
21 /*
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 */
24
25 /*
26 $Revision: 1.23 $
27 $Date: 2009/12/13 13:45:13 $
28 */
29
30 #include "ether_cap.h"
31
32 #include "stg/common.h"
33 #include "stg/raw_ip_packet.h"
34 #include "stg/traffcounter.h"
35
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
39 #include <cerrno>
40 #include <csignal>
41
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <linux/if_ether.h>
48 #include <linux/if_packet.h>
49 #include <sys/ioctl.h>
50 #include <net/if.h>
51
52 //#define CAP_DEBUG 1
53
54 extern "C" STG::Plugin* GetPlugin()
55 {
56     static ETHER_CAP plugin;
57     return &plugin;
58 }
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 std::string ETHER_CAP::GetVersion() const
63 {
64 return "cap_ether v.1.2";
65 }
66 //-----------------------------------------------------------------------------
67 ETHER_CAP::ETHER_CAP()
68     : nonstop(false),
69       isRunning(false),
70       capSock(-1),
71       traffCnt(NULL),
72       logger(STG::PluginLogger::get("cap_ether"))
73 {
74 }
75 //-----------------------------------------------------------------------------
76 int ETHER_CAP::Start()
77 {
78 if (isRunning)
79     return 0;
80
81 if (EthCapOpen() < 0)
82     {
83     errorStr = "Cannot open socket!";
84     printfd(__FILE__, "Cannot open socket\n");
85     return -1;
86     }
87
88 nonstop = true;
89
90 if (pthread_create(&thread, NULL, Run, this))
91     {
92     errorStr = "Cannot create thread.";
93     logger("Cannot create thread.");
94     printfd(__FILE__, "Cannot create thread\n");
95     return -1;
96     }
97
98 return 0;
99 }
100 //-----------------------------------------------------------------------------
101 int ETHER_CAP::Stop()
102 {
103 if (!isRunning)
104     return 0;
105
106 nonstop = false;
107
108 //5 seconds to thread stops itself
109 for (int i = 0; i < 25 && isRunning; i++)
110     {
111     struct timespec ts = {0, 200000000};
112     nanosleep(&ts, NULL);
113     }
114 //after 5 seconds waiting thread still running. now killing it
115 if (isRunning)
116     {
117     if (pthread_kill(thread, SIGUSR1))
118         {
119         errorStr = "Cannot kill thread.";
120         logger("Cannot send signal to thread.");
121         return -1;
122         }
123     for (int i = 0; i < 25 && isRunning; ++i)
124         {
125         struct timespec ts = {0, 200000000};
126         nanosleep(&ts, NULL);
127         }
128     if (isRunning)
129         {
130         errorStr = "ETHER_CAP not stopped.";
131         logger("Cannot stop thread.");
132         printfd(__FILE__, "Cannot stop thread\n");
133         return -1;
134         }
135     else
136         {
137         pthread_join(thread, NULL);
138         }
139     }
140
141 EthCapClose();
142 return 0;
143 }
144 //-----------------------------------------------------------------------------
145 void * ETHER_CAP::Run(void * d)
146 {
147 sigset_t signalSet;
148 sigfillset(&signalSet);
149 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
150
151 ETHER_CAP * dc = static_cast<ETHER_CAP *>(d);
152 dc->isRunning = true;
153
154 struct ETH_IP
155 {
156 uint16_t    ethHdr[8];
157 STG::RawPacket  rp;
158 char        padding[4];
159 char        padding1[8];
160 };
161
162 char ethip[sizeof(ETH_IP)];
163
164 memset(&ethip, 0, sizeof(ETH_IP));
165
166 ETH_IP * ethIP = static_cast<ETH_IP *>(static_cast<void *>(&ethip));
167 ethIP->rp.dataLen = -1;
168
169 char * iface = NULL;
170
171 while (dc->nonstop)
172     {
173     if (dc->EthCapRead(&ethip, 68 + 14, &iface))
174         {
175         continue;
176         }
177
178     if (ethIP->ethHdr[7] != 0x8)
179         continue;
180
181     dc->traffCnt->process(ethIP->rp);
182     }
183
184 dc->isRunning = false;
185 return NULL;
186 }
187 //-----------------------------------------------------------------------------
188 int ETHER_CAP::EthCapOpen()
189 {
190 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
191 if (capSock < 0)
192     logger("Cannot create socket: %s", strerror(errno));
193 return capSock;
194 }
195 //-----------------------------------------------------------------------------
196 int ETHER_CAP::EthCapClose()
197 {
198 close(capSock);
199 return 0;
200 }
201 //-----------------------------------------------------------------------------
202 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
203 {
204 struct sockaddr_ll  addr;
205 int addrLen;
206
207 if (!WaitPackets(capSock))
208     {
209     return ENODATA;
210     }
211
212 addrLen = sizeof(addr);
213
214 if (recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen) < 0)
215     {
216     logger("recvfrom error: %s", strerror(errno));
217     return ENODATA;
218     }
219
220 return 0;
221 }