]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp
Replace deprecated usleep with POSIX-compliant nanosleep
[stg.git] / projects / stargazer / plugins / capture / ipq_linux / ipq_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 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 */
20
21 #include <signal.h>
22 #include <cerrno>
23 #include <netinet/in.h>
24 #include <linux/netfilter.h>
25
26 #include "stg/raw_ip_packet.h"
27 #include "stg/traffcounter.h"
28 #include "stg/plugin_creator.h"
29 #include "ipq_cap.h"
30
31 extern "C"
32 {
33 #include "libipq.h"
34 }
35
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 PLUGIN_CREATOR<IPQ_CAP> icc;
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 PLUGIN * GetPlugin()
44 {
45 return icc.GetPlugin();
46 }
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 const std::string IPQ_CAP::GetVersion() const
51 {
52 return "ipq_cap v.1.2";
53 }
54 //-----------------------------------------------------------------------------
55 IPQ_CAP::IPQ_CAP()
56     : ipq_h(NULL),
57       errorStr(),
58       thread(),
59       nonstop(false),
60       isRunning(false),
61       capSock(-1),
62       traffCnt(NULL),
63       buf()
64 {
65 memset(buf, 0, BUFSIZE);
66 }
67 //-----------------------------------------------------------------------------
68 int IPQ_CAP::Start()
69 {
70 if (isRunning)
71     return 0;
72 if (IPQCapOpen() < 0)
73     {
74     errorStr = "Cannot open socket!";
75     printfd(__FILE__, "Cannot open socket\n");
76     return -1;
77     }
78 nonstop = true;
79 if (pthread_create(&thread, NULL, Run, this) == 0)
80     {
81     return 0;
82     }
83 errorStr = "Cannot create thread.";
84 printfd(__FILE__, "Cannot create thread\n");
85 return -1;
86 }
87 //-----------------------------------------------------------------------------
88 int IPQ_CAP::Stop()
89 {
90 if (!isRunning)
91     return 0;
92 nonstop = false;
93 //5 seconds to thread stops itself
94 for (int i = 0; i < 25; i++)
95     {
96     if (!isRunning)
97         break;
98     struct timespec ts = {0, 200000000};
99     nanosleep(&ts, NULL);
100     }
101 //after 5 seconds waiting thread still running. now killing it
102 if (isRunning)
103     {
104     if (pthread_kill(thread, SIGINT))
105         {
106         errorStr = "Cannot kill thread.";
107         return -1;
108         }
109     for (int i = 0; i < 25 && isRunning; ++i)
110         {
111         struct timespec ts = {0, 200000000};
112         nanosleep(&ts, NULL);
113         }
114     if (isRunning)
115         {
116         printfd(__FILE__, "Thread not stopped\n");
117         }
118     else
119         {
120         pthread_join(thread, NULL);
121         }
122     }
123 IPQCapClose();
124 return 0;
125 }
126 //-----------------------------------------------------------------------------
127 void * IPQ_CAP::Run(void * d)
128 {
129 RAW_PACKET raw_packet;
130
131 IPQ_CAP * dc = (IPQ_CAP *)d;
132 dc->isRunning = true;
133 memset(&raw_packet, 0, sizeof(raw_packet));
134 raw_packet.dataLen = -1;
135 while (dc->nonstop)
136     {
137     int status = dc->IPQCapRead(&raw_packet, 68);
138     if (status == -1 ||
139         status == -2 ||
140         status == -3 ||
141         status == -4)
142         continue;
143     dc->traffCnt->Process(raw_packet);
144     }
145 dc->isRunning = false;
146 return NULL;
147 }
148 //-----------------------------------------------------------------------------
149 int IPQ_CAP::IPQCapOpen()
150 {
151 ipq_h = ipq_create_handle(0, PF_INET);
152 if (ipq_h == NULL)
153     {
154     ipq_destroy_handle(ipq_h);
155     errorStr = "Cannot create ipq handle!";
156     return -1;
157     }
158 int status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
159 if (status < 0)
160     {
161     ipq_destroy_handle(ipq_h);
162     errorStr = "Cannot set IPQ_COPY_PACKET mode!";
163     return -1;
164     }
165 return 0;
166 }
167 //-----------------------------------------------------------------------------
168 int IPQ_CAP::IPQCapClose()
169 {
170 ipq_destroy_handle(ipq_h);
171 return 0;
172 }
173 //-----------------------------------------------------------------------------
174 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
175 {
176 memset(buf, 0, BUFSIZE);
177 int status = ipq_read(ipq_h, buf, BUFSIZE, 1);
178 if (status == 0)
179     return -4;
180 if (errno == EINTR)
181     return -3;
182 if (status < 0)
183     return -1;
184 if (ipq_message_type(buf) != IPQM_PACKET)
185     return -2;
186 static ipq_packet_msg_t * m = ipq_get_packet(buf);
187 memcpy(buffer, m->payload, blen);
188 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
189 return 0;
190 }
191 //-----------------------------------------------------------------------------