]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ipq_linux/.#ipq_cap.cpp.1.3
В плагине захвата трафика через IPQ реализована инициализация членов
[stg.git] / projects / stargazer / plugins / capture / ipq_linux / .#ipq_cap.cpp.1.3
1 #include <signal.h>
2 #include <netinet/in.h>
3 #include <linux/netfilter.h>
4
5 #include "ipq_cap.h"
6 #include "raw_ip_packet.h"
7 #include "libipq.h"
8
9 class IPQ_CAP_CREATOR
10 {
11 private:
12     IPQ_CAP * ic;
13
14 public:
15     IPQ_CAP_CREATOR()
16         {
17         printf("constructor IPQ_CAP_CREATOR\n");
18         ic = new IPQ_CAP();
19         };
20     ~IPQ_CAP_CREATOR()
21         {
22         printf("destructor IPQ_CAP_CREATOR\n");
23         if (ic)
24             delete ic;
25         };
26
27     BASE_PLUGIN * GetCapturer()
28         {
29         return ic;
30         };
31 };
32 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 IPQ_CAP_CREATOR icc;
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 BASE_PLUGIN * GetPlugin()
40 {
41 return icc.GetCapturer();
42 }
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 const string IPQ_CAP::GetVersion() const
47 {
48 return "ipq_cap v.1.1";
49 }
50 //-----------------------------------------------------------------------------
51 IPQ_CAP::IPQ_CAP()
52 {
53 isRunning = false;
54 nonstop = false;
55 }
56 //-----------------------------------------------------------------------------
57 void IPQ_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
58 {
59 traffCnt = tc;
60 }
61 //-----------------------------------------------------------------------------
62 const string & IPQ_CAP::GetStrError() const
63 {
64 return errorStr;
65 }
66 //-----------------------------------------------------------------------------
67 int IPQ_CAP::Start()
68 {
69 if (isRunning)
70     return 0;
71 printfd(__FILE__, "IPQ_CAP::Start()\n");
72 if (IPQCapOpen() < 0)
73     {
74     errorStr = "Cannot open socket!";
75     return -1;
76     }
77 nonstop = true;
78 if (pthread_create(&thread, NULL, Run, this) == 0)
79     {
80     return 0;
81     }
82 errorStr = "Cannot create thread.";
83 return -1;
84 }
85 //-----------------------------------------------------------------------------
86 int IPQ_CAP::Stop()
87 {
88 if (!isRunning)
89     return 0;
90 IPQCapClose();
91 nonstop = false;
92 //5 seconds to thread stops itself
93 for (int i = 0; i < 25; i++)
94     {
95     if (!isRunning)
96         break;
97     usleep(200000);
98     }
99 //after 5 seconds waiting thread still running. now killing it
100 if (isRunning)
101     {
102     if (pthread_kill(thread, SIGINT))
103         {
104         errorStr = "Cannot kill thread.";
105         return -1;
106         }
107     }
108 return 0;
109 }
110 //-----------------------------------------------------------------------------
111 bool IPQ_CAP::IsRunning()
112 {
113 return isRunning;
114 }
115 //-----------------------------------------------------------------------------
116 void * IPQ_CAP::Run(void * d)
117 {
118 RAW_PACKET raw_packet;
119 int status;
120
121 sleep(2);
122 IPQ_CAP * dc = (IPQ_CAP *)d;
123 dc->isRunning = true;
124 memset(&raw_packet, 0, sizeof(raw_packet));
125 raw_packet.dataLen = -1;
126 while (dc->nonstop)
127     {
128     status=dc->IPQCapRead(&raw_packet, 68);
129     if(status==-1||status==-2)
130         continue;
131     dc->traffCnt->Process(raw_packet);
132     }
133 dc->isRunning = false;
134 return NULL;
135 }
136 //-----------------------------------------------------------------------------
137 uint16_t IPQ_CAP::GetStartPosition() const
138 {
139 return 0;
140 }
141 //-----------------------------------------------------------------------------
142 uint16_t IPQ_CAP::GetStopPosition() const
143 {
144 return 0;
145 }
146 //-----------------------------------------------------------------------------
147 int IPQ_CAP::IPQCapOpen()
148 {
149 int status;
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 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 int status;
177 static ipq_packet_msg_t *m;
178
179 memset(buf, 0, BUFSIZE);
180 status = ipq_read(ipq_h, buf, BUFSIZE, 0);
181 if (status < 0)
182     return -1;
183 if (ipq_message_type(buf) != IPQM_PACKET)
184     return -2;
185 m = ipq_get_packet(buf);
186 memcpy(buffer, m->payload, blen);
187 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
188 return 0;
189 }
190 //-----------------------------------------------------------------------------