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