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