]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
97e9b17119a7eb8fb505579b82d0abeb20d1f995
[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     usleep(200000);
119     }
120 //after 5 seconds waiting thread still running. now killing it
121 if (isRunning)
122     {
123     if (pthread_kill(thread, SIGUSR1))
124         {
125         errorStr = "Cannot kill thread.";
126         return -1;
127         }
128     for (int i = 0; i < 25 && isRunning; ++i)
129         usleep(200000);
130     if (isRunning)
131         {
132         errorStr = "ETHER_CAP not stopped.";
133         printfd(__FILE__, "Cannot stop thread\n");
134         return -1;
135         }
136     else
137         {
138         pthread_join(thread, NULL);
139         }
140     }
141
142 EthCapClose();
143 return 0;
144 }
145 //-----------------------------------------------------------------------------
146 void * ETHER_CAP::Run(void * d)
147 {
148 ETHER_CAP * dc = (ETHER_CAP *)d;
149 dc->isRunning = true;
150
151 struct ETH_IP
152 {
153 uint16_t    ethHdr[8];
154 RAW_PACKET  rp;
155 char        padding[4];
156 char        padding1[8];
157 };
158
159 ETH_IP * ethIP;
160
161 char ethip[sizeof(ETH_IP)];
162
163 memset(&ethip, 0, sizeof(ETH_IP));
164
165 ethIP = (ETH_IP *)&ethip;
166 ethIP->rp.dataLen = -1;
167
168 char * iface = NULL;
169
170 while (dc->nonstop)
171     {
172     if (dc->EthCapRead(&ethip, 68 + 14, &iface))
173         {
174         continue;
175         }
176
177     if (ethIP->ethHdr[7] != 0x8)
178         continue;
179
180     dc->traffCnt->Process(ethIP->rp);
181     }
182
183 dc->isRunning = false;
184 return NULL;
185 }
186 //-----------------------------------------------------------------------------
187 int ETHER_CAP::EthCapOpen()
188 {
189 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
190 return capSock;
191 }
192 //-----------------------------------------------------------------------------
193 int ETHER_CAP::EthCapClose()
194 {
195 close(capSock);
196 return 0;
197 }
198 //-----------------------------------------------------------------------------
199 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
200 {
201 struct sockaddr_ll  addr;
202 int addrLen, res;
203
204 if (!WaitPackets(capSock))
205     {
206     return ENODATA;
207     }
208
209 addrLen = sizeof(addr);
210
211 res = recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen);
212
213 if (-1 == res)
214     {
215     if (errno != EINTR)
216         {
217         printfd(__FILE__, "Error on recvfrom: '%s'\n", strerror(errno));
218         }
219     return ENODATA;
220     }
221
222 return 0;
223 }