]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_nf/cap_nf.h
bcb8b743619ac4658a49bb130a6b50bd5909a188
[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 #ifndef __CAP_NF_H__
31 #define __CAP_NF_H__
32
33 #include <pthread.h>
34
35 #include <string>
36
37 #include "stg/os_int.h"
38 #include "stg/plugin.h"
39 #include "stg/module_settings.h"
40 #include "stg/logger.h"
41
42 #define VERSION "cap_nf v. 0.4"
43 #define START_POS 40
44 #define STOP_POS 40
45
46 class USERS;
47 class USER;
48 class TARIFFS;
49 class ADMINS;
50 class TRAFFCOUNTER;
51 class STORE;
52 class SETTINGS;
53
54 struct NF_HEADER {
55     uint16_t version;   // Protocol version
56     uint16_t count;     // Flows count
57     uint32_t uptime;    // System uptime
58     uint32_t timestamp; // UNIX timestamp
59     uint32_t nsecs;     // Residual nanoseconds
60     uint32_t flowSeq;   // Sequence counter
61     uint8_t  eType;     // Engine type
62     uint8_t  eID;       // Engine ID
63     uint16_t sInterval; // Sampling mode and interval
64 };
65
66 struct NF_DATA {
67     uint32_t srcAddr;   // Flow source address
68     uint32_t dstAddr;   // Flow destination address
69     uint32_t nextHop;   // IP addres on next hop router
70     uint16_t inSNMP;    // SNMP index of input iface
71     uint16_t outSNMP;   // SNMP index of output iface
72     uint32_t packets;   // Packets in flow
73     uint32_t octets;    // Total number of bytes in flow
74     uint32_t timeStart; // Uptime on first packet in flow
75     uint32_t timeFinish;// Uptime on last packet in flow
76     uint16_t srcPort;   // Flow source port
77     uint16_t dstPort;   // Flow destination port
78     uint8_t  pad1;      // 1-byte padding
79     uint8_t  TCPFlags;  // Cumulative OR of TCP flags
80     uint8_t  proto;     // IP protocol type (tcp, udp, etc.)
81     uint8_t  tos;       // IP Type of Service (ToS)
82     uint16_t srcAS;     // Source BGP autonomous system number
83     uint16_t dstAS;     // Destination BGP autonomus system number
84     uint8_t  srcMask;   // Source address mask in "slash" notation
85     uint8_t  dstMask;   // Destination address mask in "slash" notation
86     uint16_t pad2;      // 2-byte padding
87 };
88
89 #define BUF_SIZE (sizeof(NF_HEADER) + 30 * sizeof(NF_DATA))
90
91 class NF_CAP : public PLUGIN {
92 public:
93     NF_CAP();
94     ~NF_CAP();
95
96     void            SetTraffcounter(TRAFFCOUNTER * tc) { traffCnt = tc; }
97     void            SetSettings(const MODULE_SETTINGS & s) { settings = s; }
98     int             ParseSettings();
99
100     int             Start();
101     int             Stop();
102     int             Reload() { return 0; }
103     bool            IsRunning() { return runningTCP || runningUDP; }
104     const std::string & GetStrError() const { return errorStr; }
105     std::string     GetVersion() const { return VERSION; }
106     uint16_t        GetStartPosition() const { return START_POS; }
107     uint16_t        GetStopPosition() const { return STOP_POS; }
108
109 private:
110     NF_CAP(const NF_CAP & rvalue);
111     NF_CAP & operator=(const NF_CAP & rvalue);
112
113     TRAFFCOUNTER * traffCnt;
114     MODULE_SETTINGS settings;
115     pthread_t tidTCP;
116     pthread_t tidUDP;
117     bool runningTCP;
118     bool runningUDP;
119     bool stoppedTCP;
120     bool stoppedUDP;
121     uint16_t portT;
122     uint16_t portU;
123     int sockTCP;
124     int sockUDP;
125     mutable std::string errorStr;
126     PLUGIN_LOGGER logger;
127
128     static void * RunUDP(void *);
129     static void * RunTCP(void *);
130     void ParseBuffer(uint8_t * buf, ssize_t size);
131
132     bool OpenTCP();
133     bool OpenUDP();
134     void CloseTCP() { close(sockTCP); }
135     void CloseUDP() { close(sockUDP); }
136 };
137
138 #endif