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