]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_freebsd/ether_cap.h
6e651d35696a50d25e0d1e4f03a1ec32d7f42708
[stg.git] / projects / stargazer / plugins / capture / ether_freebsd / ether_cap.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  $Revision: 1.11 $
19  $Date: 2009/06/23 11:32:27 $
20  $Author: faust $
21  */
22
23 /*
24 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
25 */
26
27 #ifndef ETHER_CAP_H
28 #define ETHER_CAP_H
29
30 #include <string>
31 #include <vector>
32 #include <pthread.h>
33
34 #ifdef FREE_BSD5
35 #include <inttypes.h>
36 #endif
37
38 #ifdef FREE_BSD
39 #include <sys/inttypes.h>
40 #endif
41
42 #include "base_plugin.h"
43 #include "base_settings.h"
44 #include "../../../traffcounter.h"
45
46 using namespace std;
47
48 extern "C" BASE_PLUGIN * GetPlugin();
49
50 #define BUFF_LEN    (128)
51
52 //-----------------------------------------------------------------------------
53 struct BPF_DATA
54 {
55     BPF_DATA()
56         {
57         fd = 0;
58         p = NULL;
59         r = 0;
60         sum = 0;
61         memset(buffer, 0, BUFF_LEN);
62         bh = NULL;
63         canRead = 1;
64         iface = "";
65         //memset(&polld, 0, sizeof(pollfd));
66         };
67
68     BPF_DATA(const BPF_DATA & bd)
69         {
70         fd = bd.fd;
71         p = bd.p;
72         r = bd.r;
73         sum = bd.sum;
74         memcpy(buffer, bd.buffer, BUFF_LEN);
75         bh = bd.bh;
76         canRead = bd.canRead;
77         iface = bd.iface;
78         //memcpy(&polld, &bd.polld, sizeof(pollfd));
79         };
80
81 int             fd;
82 uint8_t *       p;
83 int             r;
84 int             sum;
85 uint8_t         buffer[BUFF_LEN];
86 struct bpf_hdr * bh;
87 int             canRead;
88 string          iface;
89 //pollfd          polld;
90 };
91 //-----------------------------------------------------------------------------
92 class BPF_CAP_SETTINGS
93 {
94 public:
95     virtual         ~BPF_CAP_SETTINGS(){};
96     const string&   GetStrError() const { return errorStr; }
97     int             ParseSettings(const MODULE_SETTINGS & s);
98     string          GetIface(unsigned int num);
99
100 private:
101     vector<string>  iface;
102     mutable string  errorStr;
103 };
104 //-----------------------------------------------------------------------------
105 class BPF_CAP :public BASE_PLUGIN
106 {
107 public:
108                         BPF_CAP();
109     virtual             ~BPF_CAP(){};
110
111     void                SetUsers(USERS *){};
112     void                SetTariffs(TARIFFS *){};
113     void                SetAdmins(ADMINS *){};
114     void                SetTraffcounter(TRAFFCOUNTER * tc);
115     void                SetStore(BASE_STORE *){};
116     void                SetStgSettings(const SETTINGS *){};
117
118     int                 Start();
119     int                 Stop();
120     int                 Reload() { return 0; };
121     bool                IsRunning();
122
123     void                SetSettings(const MODULE_SETTINGS & s);
124     int                 ParseSettings();
125
126     const string      & GetStrError() const;
127     const string        GetVersion() const;
128     uint16_t            GetStartPosition() const;
129     uint16_t            GetStopPosition() const;
130
131 private:
132     static void *       Run(void *);
133     int                 BPFCapOpen();
134     //int                 BPFCapOpen(int n);
135     int                 BPFCapOpen(BPF_DATA * bd);
136     int                 BPFCapClose();
137     int                 BPFCapRead(char * buffer, int blen, char ** iface);
138     int                 BPFCapRead(char * buffer, int blen, char ** iface, BPF_DATA * bd);
139
140     BPF_CAP_SETTINGS    capSettings;
141
142     mutable string      errorStr;
143
144     vector<BPF_DATA>    bpfData;
145     vector<pollfd>      polld;
146
147     pthread_t           thread;
148     bool                nonstop;
149     bool                isRunning;
150     int                 capSock;
151     MODULE_SETTINGS     settings;
152
153     TRAFFCOUNTER *      traffCnt;
154 };
155 //-----------------------------------------------------------------------------
156
157 #endif //ETHER_CAP_H
158