Branch data Line data Source code
1 : : // $Id: PktSrc.h 6916 2009-09-24 20:48:36Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef pktsrc_h
6 : : #define pktsrc_h
7 : :
8 : : #include "Dict.h"
9 : : #include "Expr.h"
10 : : #include "BPF_Program.h"
11 : : #include "IOSource.h"
12 : : #include "RemoteSerializer.h"
13 : :
14 : : #define BRO_PCAP_ERRBUF_SIZE PCAP_ERRBUF_SIZE + 256
15 : :
16 : : extern "C" {
17 : : #include <pcap.h>
18 : : }
19 : :
20 [ # # ][ # # ]: 6 : declare(PDict,BPF_Program);
21 : :
22 : : // Whether a PktSrc object is used by the normal filter structure or the
23 : : // secondary-path structure.
24 : : typedef enum {
25 : : TYPE_FILTER_NORMAL, // the normal filter
26 : : TYPE_FILTER_SECONDARY, // the secondary-path filter
27 : : } PktSrc_Filter_Type;
28 : :
29 : :
30 : : // {filter,event} tuples conforming the secondary path.
31 : : class SecondaryEvent {
32 : : public:
33 : 0 : SecondaryEvent(const char* arg_filter, Func* arg_event)
34 : : {
35 : 0 : filter = arg_filter;
36 : 0 : event = arg_event;
37 : 0 : }
38 : :
39 : 0 : const char* Filter() { return filter; }
40 : 0 : Func* Event() { return event; }
41 : :
42 : : private:
43 : : const char* filter;
44 : : Func* event;
45 : : };
46 : :
47 : 2 : declare(PList,SecondaryEvent);
48 : : typedef PList(SecondaryEvent) secondary_event_list;
49 : :
50 : :
51 : :
52 : : class SecondaryPath {
53 : : public:
54 : : SecondaryPath();
55 : : ~SecondaryPath();
56 : :
57 : 0 : secondary_event_list& EventTable() { return event_list; }
58 : 1 : const char* Filter() { return filter; }
59 : :
60 : : private:
61 : : secondary_event_list event_list;
62 : : // OR'ed union of all SecondaryEvent filters
63 : : char* filter;
64 : : };
65 : :
66 : : // Main secondary-path object.
67 : : extern SecondaryPath* secondary_path;
68 : :
69 : :
70 : : // {program, {filter,event}} tuple table.
71 : : class SecondaryProgram {
72 : : public:
73 : 0 : SecondaryProgram(BPF_Program* arg_program, SecondaryEvent* arg_event)
74 : : {
75 : 0 : program = arg_program;
76 : 0 : event = arg_event;
77 : 0 : }
78 : :
79 : : ~SecondaryProgram();
80 : :
81 : 0 : BPF_Program* Program() { return program; }
82 : 0 : SecondaryEvent* Event() { return event; }
83 : :
84 : : private:
85 : : // Associated program.
86 : : BPF_Program *program;
87 : :
88 : : // Event that is run in case the program is matched.
89 : : SecondaryEvent* event;
90 : : };
91 : :
92 : 1 : declare(PList,SecondaryProgram);
93 : : typedef PList(SecondaryProgram) secondary_program_list;
94 : :
95 : :
96 : :
97 : : class PktSrc : public IOSource {
98 : : public:
99 : : ~PktSrc();
100 : :
101 : : // IOSource interface
102 : : bool IsReady();
103 : : void GetFds(int* read, int* write, int* except);
104 : : double NextTimestamp(double* local_network_time);
105 : : void Process();
106 : 21347 : const char* Tag() { return "PktSrc"; }
107 : :
108 : 0 : const char* ErrorMsg() const { return errbuf; }
109 : 1 : void ClearErrorMsg() { *errbuf ='\0'; }
110 : :
111 : : // Returns the packet last processed; false if there is no
112 : : // current packet available.
113 : : bool GetCurrentPacket(const pcap_pkthdr** hdr, const u_char** pkt);
114 : :
115 : 0 : int HdrSize() const { return hdr_size; }
116 : : int DataLink() const { return datalink; }
117 : :
118 : : void ConsumePacket() { data = 0; }
119 : :
120 : 3 : int IsLive() const { return interface != 0; }
121 : :
122 : : pcap_t* PcapHandle() const { return pd; }
123 : 0 : int LinkType() const { return pcap_datalink(pd); }
124 : :
125 : : const char* ReadFile() const { return readfile; }
126 : 0 : const char* Interface() const { return interface; }
127 : 21347 : PktSrc_Filter_Type FilterType() const { return filter_type; }
128 : : void AddSecondaryTablePrograms();
129 : 0 : const secondary_program_list& ProgramTable() const
130 : 0 : { return program_list; }
131 : :
132 : : // Signal packet source that processing was suspended and is now going
133 : : // to be continued.
134 : : void ContinueAfterSuspend();
135 : :
136 : : // Only valid in pseudo-realtime mode.
137 : 0 : double CurrentPacketTimestamp() { return current_pseudo; }
138 : : double CurrentPacketWallClock();
139 : :
140 : : struct Stats {
141 : : unsigned int received; // pkts received (w/o drops)
142 : : unsigned int dropped; // pkts dropped
143 : : unsigned int link; // total packets on link
144 : : // (not always not available)
145 : : };
146 : :
147 : : virtual void Statistics(Stats* stats);
148 : :
149 : : // Precompiles a filter and associates the given index with it.
150 : : // Returns true on success, 0 if a problem occurred.
151 : : virtual int PrecompileFilter(int index, const char* filter);
152 : :
153 : : // Activates the filter with the given index.
154 : : // Returns true on success, 0 if a problem occurred.
155 : : virtual int SetFilter(int index);
156 : :
157 : : protected:
158 : : PktSrc();
159 : :
160 : : static const int PCAP_TIMEOUT = 20;
161 : :
162 : : void SetHdrSize();
163 : :
164 : : virtual void Close();
165 : :
166 : : // Returns 1 on success, 0 on time-out/gone dry.
167 : : virtual int ExtractNextPacket();
168 : :
169 : : // Checks if the current packet has a pseudo-time <= current_time.
170 : : // If yes, returns pseudo-time, otherwise 0.
171 : : double CheckPseudoTime();
172 : :
173 : : double current_timestamp;
174 : : double next_timestamp;
175 : :
176 : : // Only set in pseudo-realtime mode.
177 : : double first_timestamp;
178 : : double first_wallclock;
179 : : double current_wallclock;
180 : : double current_pseudo;
181 : :
182 : : struct pcap_pkthdr hdr;
183 : : const u_char* data; // contents of current packet
184 : : const u_char* last_data; // same, but unaffected by consuming
185 : : int hdr_size;
186 : : int datalink;
187 : : double next_sync_point; // For trace synchronziation in pseudo-realtime
188 : :
189 : : char* interface; // nil if not reading from an interface
190 : : char* readfile; // nil if not reading from a file
191 : :
192 : : pcap_t* pd;
193 : : int selectable_fd;
194 : : uint32 netmask;
195 : : char errbuf[BRO_PCAP_ERRBUF_SIZE];
196 : :
197 : : Stats stats;
198 : :
199 : : PDict(BPF_Program) filters; // precompiled filters
200 : :
201 : : PktSrc_Filter_Type filter_type; // normal path or secondary path
202 : : secondary_program_list program_list;
203 : : };
204 : :
205 [ # # ][ # # ]: 0 : class PktInterfaceSrc : public PktSrc {
206 : : public:
207 : : PktInterfaceSrc(const char* interface, const char* filter,
208 : : PktSrc_Filter_Type ft=TYPE_FILTER_NORMAL);
209 : : };
210 : :
211 [ # # ][ # # ]: 0 : class PktFileSrc : public PktSrc {
212 : : public:
213 : : PktFileSrc(const char* readfile, const char* filter,
214 : : PktSrc_Filter_Type ft=TYPE_FILTER_NORMAL);
215 : : };
216 : :
217 : :
218 : : extern int get_link_header_size(int dl);
219 : :
220 : : class PktDumper {
221 : : public:
222 : : PktDumper(const char* file = 0, bool append = false);
223 : 0 : ~PktDumper() { Close(); }
224 : :
225 : : bool Open(const char* file = 0);
226 : : bool Close();
227 : : bool Dump(const struct pcap_pkthdr* hdr, const u_char* pkt);
228 : :
229 : 0 : pcap_dumper_t* PcapDumper() { return dumper; }
230 : :
231 : 0 : const char* FileName() const { return filename; }
232 : 0 : bool IsError() const { return is_error; }
233 : 0 : const char* ErrorMsg() const { return errbuf; }
234 : :
235 : : // This heuristic will horribly fail if we're using packets
236 : : // with different link layers. (If we can't derive a reasonable value
237 : : // from the packet sources, our fall-back is Ethernet.)
238 : : int HdrSize() const
239 : : { return get_link_header_size(pcap_datalink(pd)); }
240 : :
241 : : // Network time when dump file was opened.
242 : 0 : double OpenTime() const { return open_time; }
243 : :
244 : : private:
245 : : void InitPd();
246 : : void Error(const char* str);
247 : :
248 : : static const int FNBUF_LEN = 1024;
249 : : char filename[FNBUF_LEN];
250 : :
251 : : bool append;
252 : : pcap_dumper_t* dumper;
253 : : pcap_t* pd;
254 : : double open_time;
255 : :
256 : : bool is_error;
257 : : char errbuf[BRO_PCAP_ERRBUF_SIZE];
258 : : };
259 : :
260 : : #endif
|