]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp
Initial adding of netfilter_queue capturer.
[stg.git] / projects / stargazer / plugins / capture / nfqueue / nfqueue.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 : Maxim Mamontov <faust@stargazer.dp.ua>
19 */
20
21 #include "nfqueue.h"
22
23 #include "stg/traffcounter.h"
24 #include "stg/plugin_creator.h"
25 #include "stg/common.h"
26 #include "stg/raw_ip_packet.h"
27
28 #include <signal.h>
29
30 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
33 namespace
34 {
35 PLUGIN_CREATOR<NFQ_CAP> ncc;
36 }
37
38 extern "C" PLUGIN * GetPlugin();
39 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 PLUGIN * GetPlugin()
43 {
44 return ncc.GetPlugin();
45 }
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 std::string NFQ_CAP::GetVersion() const
50 {
51 return "cap_nfqueue v.1.0";
52 }
53 //-----------------------------------------------------------------------------
54 NFQ_CAP::NFQ_CAP()
55     : errorStr(),
56       thread(),
57       nonstop(false),
58       isRunning(false),
59       traffCnt(NULL),
60       logger(GetPluginLogger(GetStgLogger(), "cap_nfqueue"))
61 {
62 }
63 //-----------------------------------------------------------------------------
64 int NFQ_CAP::ParseSettings()
65 {
66 return 0;
67 }
68 //-----------------------------------------------------------------------------
69 int NFQ_CAP::Start()
70 {
71 if (isRunning)
72     return 0;
73
74 nonstop = true;
75
76 if (pthread_create(&thread, NULL, Run, this))
77     {
78     errorStr = "Cannot create thread.";
79     logger("Cannot create thread.");
80     printfd(__FILE__, "Cannot create thread\n");
81     return -1;
82     }
83
84 return 0;
85 }
86 //-----------------------------------------------------------------------------
87 int NFQ_CAP::Stop()
88 {
89 if (!isRunning)
90     return 0;
91
92 nonstop = false;
93
94 //5 seconds to thread stops itself
95 for (int i = 0; i < 25 && isRunning; i++)
96     {
97     struct timespec ts = {0, 200000000};
98     nanosleep(&ts, NULL);
99     }
100 //after 5 seconds waiting thread still running. now killing it
101 if (isRunning)
102     {
103     if (pthread_kill(thread, SIGUSR1))
104         {
105         errorStr = "Cannot kill thread.";
106         logger("Cannot send signal to 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         errorStr = "NFQ_CAP not stopped.";
117         logger("Cannot stop thread.");
118         printfd(__FILE__, "Cannot stop thread\n");
119         return -1;
120         }
121     }
122
123 pthread_join(thread, NULL);
124
125 return 0;
126 }
127 //-----------------------------------------------------------------------------
128 void * NFQ_CAP::Run(void * d)
129 {
130 sigset_t signalSet;
131 sigfillset(&signalSet);
132 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
133
134 NFQ_CAP * dc = static_cast<NFQ_CAP *>(d);
135 dc->isRunning = true;
136
137 while (dc->nonstop)
138     {
139     }
140
141 dc->isRunning = false;
142 return NULL;
143 }