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