]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_nf/cap_nf.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / capture / cap_nf / cap_nf.h
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 Date: 16.05.2008
19 */
20
21 /*
22 * Author : Maxim Mamontov <faust@stg.dp.ua>
23 */
24
25 /*
26 $Revision: 1.5 $
27 $Date: 2009/12/13 12:56:07 $
28 $Author: faust $
29 */
30 #pragma once
31
32 #include "stg/plugin.h"
33 #include "stg/module_settings.h"
34 #include "stg/logger.h"
35
36 #include <string>
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wshadow"
39 #include <jthread.hpp>
40 #pragma GCC diagnostic pop
41 #include <cstdint>
42
43 #include <unistd.h> // close
44
45 #define VERSION "cap_nf v. 0.4"
46 #define START_POS 40
47 #define STOP_POS 40
48
49 namespace STG
50 {
51
52 class Users;
53 class Tariffs;
54 struct Admins;
55 struct TraffCounter;
56 struct Store;
57 struct Settings;
58
59 class NF_CAP : public Plugin
60 {
61     public:
62         NF_CAP();
63
64         void            SetTraffcounter(TraffCounter * tc) override { traffCnt = tc; }
65         void            SetSettings(const ModuleSettings & s) override { settings = s; }
66         int             ParseSettings() override;
67
68         int             Start() override;
69         int             Stop() override;
70         int             Reload(const ModuleSettings & /*ms*/) override { return 0; }
71
72         bool            IsRunning() override { return m_threadTCP.joinable() || m_threadUDP.joinable(); }
73         const std::string & GetStrError() const override { return errorStr; }
74         std::string     GetVersion() const override { return VERSION; }
75         uint16_t        GetStartPosition() const override { return START_POS; }
76         uint16_t        GetStopPosition() const override { return STOP_POS; }
77
78     private:
79         NF_CAP(const NF_CAP & rvalue);
80         NF_CAP & operator=(const NF_CAP & rvalue);
81
82         TraffCounter * traffCnt;
83         ModuleSettings settings;
84         std::jthread m_threadTCP;
85         std::jthread m_threadUDP;
86         bool stoppedTCP;
87         bool stoppedUDP;
88         uint16_t portT;
89         uint16_t portU;
90         int sockTCP;
91         int sockUDP;
92         mutable std::string errorStr;
93         PluginLogger logger;
94
95         void RunUDP(std::stop_token token) noexcept;
96         void RunTCP(std::stop_token token) noexcept;
97         void ParseBuffer(uint8_t * buf, ssize_t size);
98
99         bool OpenTCP();
100         bool OpenUDP();
101         void CloseTCP() { close(sockTCP); }
102         void CloseUDP() { close(sockUDP); }
103 };
104
105 }