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