]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp
ASN.1 sources moved to library
[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       nonstop(false),
58       isRunning(false),
59       capSock(-1),
60       traffCnt(NULL)
61 {
62 memset(buf, 0, BUFSIZE);
63 }
64 //-----------------------------------------------------------------------------
65 int IPQ_CAP::Start()
66 {
67 if (isRunning)
68     return 0;
69 if (IPQCapOpen() < 0)
70     {
71     errorStr = "Cannot open socket!";
72     printfd(__FILE__, "Cannot open socket\n");
73     return -1;
74     }
75 nonstop = true;
76 if (pthread_create(&thread, NULL, Run, this) == 0)
77     {
78     return 0;
79     }
80 errorStr = "Cannot create thread.";
81 printfd(__FILE__, "Cannot create thread\n");
82 return -1;
83 }
84 //-----------------------------------------------------------------------------
85 int IPQ_CAP::Stop()
86 {
87 if (!isRunning)
88     return 0;
89 nonstop = false;
90 //5 seconds to thread stops itself
91 for (int i = 0; i < 25; i++)
92     {
93     if (!isRunning)
94         break;
95     usleep(200000);
96     }
97 //after 5 seconds waiting thread still running. now killing it
98 if (isRunning)
99     {
100     if (pthread_kill(thread, SIGINT))
101         {
102         errorStr = "Cannot kill thread.";
103         return -1;
104         }
105     for (int i = 0; i < 25 && isRunning; ++i)
106         {
107         usleep(200000);
108         }
109     if (isRunning)
110         {
111         printfd(__FILE__, "Thread not stopped\n");
112         }
113     else
114         {
115         pthread_join(thread, NULL);
116         }
117     }
118 IPQCapClose();
119 return 0;
120 }
121 //-----------------------------------------------------------------------------
122 void * IPQ_CAP::Run(void * d)
123 {
124 RAW_PACKET raw_packet;
125
126 IPQ_CAP * dc = (IPQ_CAP *)d;
127 dc->isRunning = true;
128 memset(&raw_packet, 0, sizeof(raw_packet));
129 raw_packet.dataLen = -1;
130 while (dc->nonstop)
131     {
132     int status = dc->IPQCapRead(&raw_packet, 68);
133     if (status == -1 ||
134         status == -2 ||
135         status == -3 ||
136         status == -4)
137         continue;
138     dc->traffCnt->Process(raw_packet);
139     }
140 dc->isRunning = false;
141 return NULL;
142 }
143 //-----------------------------------------------------------------------------
144 int IPQ_CAP::IPQCapOpen()
145 {
146 ipq_h = ipq_create_handle(0, PF_INET);
147 if (ipq_h == NULL)
148     {
149     ipq_destroy_handle(ipq_h);
150     errorStr = "Cannot create ipq handle!";
151     return -1;
152     }
153 int status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
154 if (status < 0)
155     {
156     ipq_destroy_handle(ipq_h);
157     errorStr = "Cannot set IPQ_COPY_PACKET mode!";
158     return -1;
159     }
160 return 0;
161 }
162 //-----------------------------------------------------------------------------
163 int IPQ_CAP::IPQCapClose()
164 {
165 ipq_destroy_handle(ipq_h);
166 return 0;
167 }
168 //-----------------------------------------------------------------------------
169 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
170 {
171 memset(buf, 0, BUFSIZE);
172 int status = ipq_read(ipq_h, buf, BUFSIZE, 1);
173 if (status == 0)
174     return -4;
175 if (errno == EINTR)
176     return -3;
177 if (status < 0)
178     return -1;
179 if (ipq_message_type(buf) != IPQM_PACKET)
180     return -2;
181 static ipq_packet_msg_t * m = ipq_get_packet(buf);
182 memcpy(buffer, m->payload, blen);
183 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
184 return 0;
185 }
186 //-----------------------------------------------------------------------------