#include "config.h"#include <sys/types.h>#include <time.h>#include <errno.h>#include <signal.h>#include <stdlib.h>#include <unistd.h>#include "NetVar.h"#include "Sessions.h"#include "Event.h"#include "Timer.h"#include "Var.h"#include "Logger.h"#include "Net.h"#include "TCP_Rewriter.h"#include "Anon.h"#include "PacketSort.h"#include "Serializer.h"#include "setsignal.h"Include dependency graph for Net.cc:

Go to the source code of this file.
Functions | |
| int | select (int, fd_set *, fd_set *, fd_set *, struct timeval *) |
| PList (PktSrc) pkt_srcs | |
| RETSIGTYPE | watchdog (int) |
| void | net_init (name_list &interfaces, name_list &readfiles, const char *writefile, const char *transformed_writefile, const char *filter, const char *secondary_filter, int do_watchdog) |
| void | net_packet_dispatch (double t, const struct pcap_pkthdr *hdr, const u_char *pkt, int hdr_size, PktSrc *src_ps, PacketSortElement *pkt_elem) |
| int | process_packet_sorter (double lastest_packet_time) |
| void | net_packet_arrival (double t, const struct pcap_pkthdr *hdr, const u_char *pkt, int hdr_size, PktSrc *src_ps) |
| void | net_run () |
| void | net_get_final_stats () |
| void | net_finish (int drain_events) |
| void | net_delete () |
| int | net_packet_match (BPF_Program *fp, const u_char *pkt, u_int len, u_int caplen) |
| void | net_suspend_processing () |
| void | net_continue_processing () |
Variables | |
| PktDumper * | pkt_dumper = 0 |
| PktDumper * | pkt_transformed_dumper = 0 |
| PacketDumper * | transformed_pkt_dump = 0 |
| PacketDumper * | source_pkt_dump = 0 |
| int | transformed_pkt_dump_MTU = 1514 |
| int | reading_live = 0 |
| int | reading_traces = 0 |
| int | have_pending_timers = 0 |
| int | pseudo_realtime = 0 |
| char * | user_pcap_filter = 0 |
| bool | using_communication = false |
| double | network_time = 0.0 |
| double | processing_start_time = 0.0 |
| double | bro_start_time = 0.0 |
| double | last_watchdog_proc_time = 0.0 |
| bool | terminating = false |
| PacketSortGlobalPQ * | packet_sorter = 0 |
| const struct pcap_pkthdr * | current_hdr = 0 |
| const u_char * | current_pkt = 0 |
| int | current_dispatched = 0 |
| PktSrc * | current_pktsrc = 0 |
| int | _processing_suspended = 0 |
|
|
Definition at line 571 of file Net.cc. References _processing_suspended, bro_logger, and Logger::Log(). Referenced by RemoteSerializer::ProcessPhaseDone().
00572 {
00573 if ( _processing_suspended == 1 )
00574 bro_logger->Log("processing continued");
00575 --_processing_suspended;
00576 }
|
|
|
Definition at line 525 of file Net.cc. References ip_anonymizer, NUM_ADDR_ANONYMIZATION_METHODS, packet_sorter, sessions, and transformed_pkt_dump. Referenced by main(), and termination_signal().
00526 {
00527 delete sessions;
00528 delete packet_sorter;
00529
00530 // Can't put this in net_finish() because packets might be
00531 // dumped when connections are deleted.
00532 if ( transformed_pkt_dump )
00533 delete transformed_pkt_dump;
00534
00535 for ( int i = 0; i < NUM_ADDR_ANONYMIZATION_METHODS; ++i )
00536 delete ip_anonymizer[i];
00537 }
|
|
|
Definition at line 497 of file Net.cc. References NetSessions::Done(), NetSessions::Drain(), EventMgr::Drain(), mgr, num_packets_cleaned, num_packets_held, pkt_dumper, pkt_transformed_dumper, and sessions. Referenced by done_with_network(), and watchdog().
00498 {
00499 if ( drain_events )
00500 {
00501 if ( sessions )
00502 sessions->Drain();
00503
00504 mgr.Drain();
00505
00506 if ( sessions )
00507 sessions->Done();
00508 }
00509
00510 delete pkt_dumper;
00511 delete pkt_transformed_dumper;
00512
00513 // fprintf(stderr, "uhash: %d/%d\n", hash_cnt_uhash, hash_cnt_all);
00514
00515 #ifdef DEBUG
00516 extern int reassem_seen_bytes, reassem_copied_bytes;
00517 // DEBUG_MSG("Reassembly (TCP and IP/Frag): %d bytes seen, %d bytes copied\n",
00518 reassem_seen_bytes, reassem_copied_bytes);
00519
00520 extern int num_packets_held, num_packets_cleaned;
00521 // DEBUG_MSG("packets clean up: %d/%d\n", num_packets_cleaned, num_packets_held);
00522 #endif
00523 }
|
|
|
Definition at line 481 of file Net.cc. References PktSrc::Stats::dropped, PktSrc::Interface(), PktSrc::IsLive(), loop_over_list, PktSrc::Stats::received, and PktSrc::Statistics(). Referenced by net_run(), termination_signal(), and watchdog().
00482 {
00483 loop_over_list(pkt_srcs, i)
00484 {
00485 PktSrc* ps = pkt_srcs[i];
00486
00487 if ( ps->IsLive() )
00488 {
00489 struct PktSrc::Stats s;
00490 ps->Statistics(&s);
00491 fprintf(stderr, "%d packets received on interface %s, %d dropped\n",
00492 s.received, ps->Interface(), s.dropped);
00493 }
00494 }
00495 }
|
|
||||||||||||||||||||||||||||||||
|
Definition at line 155 of file Net.cc. References PktSrc::AddSecondaryTablePrograms(), PktSrc::ErrorMsg(), PktDumper::ErrorMsg(), init_ip_addr_anonymizers(), init_net_var(), io_sources, ip_anonymizer, PktDumper::IsError(), IOSource::IsOpen(), NUM_ADDR_ANONYMIZATION_METHODS, packet_sort_window, packet_sorter, PktDumper::PcapDumper(), pkt_dumper, pkt_transformed_dumper, prog, pseudo_realtime, reading_live, reading_traces, IOSourceRegistry::Register(), sessions, setsignal(), source_pkt_dump, transformed_pkt_dump, TYPE_FILTER_SECONDARY, watchdog(), watchdog_interval, and writefile. Referenced by main().
00159 {
00160 init_net_var();
00161
00162 if ( readfiles.length() > 0 )
00163 {
00164 reading_live = pseudo_realtime;
00165 reading_traces = 1;
00166
00167 for ( int i = 0; i < readfiles.length(); ++i )
00168 {
00169 PktFileSrc* ps = new PktFileSrc(readfiles[i], filter);
00170
00171 if ( ! ps->IsOpen() )
00172 {
00173 fprintf(stderr, "%s: problem with trace file %s - %s\n",
00174 prog, readfiles[i], ps->ErrorMsg());
00175 exit(1);
00176 }
00177 else
00178 {
00179 pkt_srcs.append(ps);
00180 io_sources.Register(ps);
00181 }
00182
00183 if ( secondary_filter )
00184 {
00185 // We use a second PktFileSrc for the
00186 // secondary path.
00187 PktFileSrc* ps = new PktFileSrc(readfiles[i],
00188 secondary_filter,
00189 TYPE_FILTER_SECONDARY);
00190
00191 if ( ! ps->IsOpen() )
00192 {
00193 fprintf(stderr, "%s: problem with trace file %s - %s\n",
00194 prog, readfiles[i],
00195 ps->ErrorMsg());
00196 exit(1);
00197 }
00198 else
00199 {
00200 pkt_srcs.append(ps);
00201 io_sources.Register(ps);
00202 }
00203
00204 ps->AddSecondaryTablePrograms();
00205 }
00206 }
00207 }
00208
00209 else if ( interfaces.length() > 0 )
00210 {
00211 reading_live = 1;
00212 reading_traces = 0;
00213
00214 for ( int i = 0; i < interfaces.length(); ++i )
00215 {
00216 PktInterfaceSrc* ps =
00217 new PktInterfaceSrc(interfaces[i], filter);
00218
00219 if ( ! ps->IsOpen() )
00220 {
00221 fprintf(stderr, "%s: problem with interface %s - %s\n",
00222 prog, interfaces[i], ps->ErrorMsg());
00223 exit(1);
00224 }
00225 else
00226 {
00227 pkt_srcs.append(ps);
00228 io_sources.Register(ps);
00229 }
00230
00231 if ( secondary_filter )
00232 {
00233 PktInterfaceSrc* ps =
00234 new PktInterfaceSrc(interfaces[i],
00235 filter, TYPE_FILTER_SECONDARY);
00236
00237 if ( ! ps->IsOpen() )
00238 {
00239 fprintf(stderr, "%s: problem with interface %s - %s\n",
00240 prog, interfaces[i],
00241 ps->ErrorMsg());
00242 exit(1);
00243 }
00244 else
00245 {
00246 pkt_srcs.append(ps);
00247 io_sources.Register(ps);
00248 }
00249
00250 ps->AddSecondaryTablePrograms();
00251 }
00252 }
00253 }
00254
00255 else
00256 // have_pending_timers = 1, possibly. We don't set
00257 // that here, though, because at this point we don't know
00258 // whether the user's bro_init() event will indeed set
00259 // a timer.
00260 reading_traces = reading_live = 0;
00261
00262 if ( writefile )
00263 {
00264 // ### This will fail horribly if there are multiple
00265 // interfaces with different-lengthed media.
00266 pkt_dumper = new PktDumper(writefile);
00267 if ( pkt_dumper->IsError() )
00268 {
00269 fprintf(stderr, "%s: can't open write file \"%s\" - %s\n",
00270 prog, writefile, pkt_dumper->ErrorMsg());
00271 exit(1);
00272 }
00273 }
00274
00275 if ( transformed_writefile )
00276 {
00277 pkt_transformed_dumper = new PktDumper(transformed_writefile);
00278 if ( pkt_transformed_dumper->IsError() )
00279 {
00280 fprintf(stderr, "%s: can't open trace transformation write file \"%s\" - %s\n",
00281 prog, writefile,
00282 pkt_transformed_dumper->ErrorMsg());
00283 exit(1);
00284 }
00285
00286 transformed_pkt_dump =
00287 new PacketDumper(pkt_transformed_dumper->PcapDumper());
00288
00289 // If both -A and -w are specified, -A will be the transformed
00290 // trace file and -w will be the source packet trace file.
00291 // Otherwise the packets will go to the same file.
00292 if ( pkt_dumper )
00293 source_pkt_dump =
00294 new PacketDumper(pkt_dumper->PcapDumper());
00295 }
00296
00297 else if ( pkt_dumper )
00298 transformed_pkt_dump =
00299 new PacketDumper(pkt_dumper->PcapDumper());
00300
00301 if ( anonymize_ip_addr )
00302 init_ip_addr_anonymizers();
00303 else
00304 for ( int i = 0; i < NUM_ADDR_ANONYMIZATION_METHODS; ++i )
00305 ip_anonymizer[i] = 0;
00306
00307 if ( packet_sort_window > 0 )
00308 packet_sorter = new PacketSortGlobalPQ();
00309
00310 sessions = new NetSessions();
00311
00312 if ( do_watchdog )
00313 {
00314 // Set up the watchdog to make sure we don't wedge.
00315 (void) setsignal(SIGALRM, watchdog);
00316 (void) alarm(watchdog_interval);
00317 }
00318 }
|
|
||||||||||||||||||||||||
|
Definition at line 373 of file Net.cc. References PacketSortGlobalPQ::Add(), net_packet_dispatch(), packet_sorter, and process_packet_sorter(). Referenced by PktSrc::Process().
00376 {
00377 if ( packet_sorter )
00378 {
00379 // Note that when we enable packet sorter, there will
00380 // be a small window between the time packet arrives
00381 // to Bro and when it is processed ("dispatched"). We
00382 // define network_time to be the latest timestamp for
00383 // packets *dispatched* so far (usually that's the
00384 // timestamp of the current packet).
00385
00386 // Add the packet to the packet_sorter.
00387 packet_sorter->Add(
00388 new PacketSortElement(src_ps, t, hdr, pkt, hdr_size));
00389
00390 // Do we have any packets to dispatch from packet_sorter?
00391 process_packet_sorter(t);
00392 }
00393 else
00394 // Otherwise we dispatch the packet immediately
00395 net_packet_dispatch(t, hdr, pkt, hdr_size, src_ps, 0);
00396 }
|
|
||||||||||||||||||||||||||||
|
Definition at line 320 of file Net.cc. References TimerMgr::Advance(), current_dispatched, current_hdr, current_pkt, current_pktsrc, NetSessions::DispatchPacket(), EventMgr::Drain(), max_timer_expires, mgr, network_time, processing_start_time, segment_logger, sessions, and timer_mgr. Referenced by net_packet_arrival(), and process_packet_sorter().
00323 {
00324 // network_time never goes back.
00325 if ( t > network_time )
00326 network_time = t;
00327
00328 processing_start_time = t;
00329
00330 SegmentProfiler(segment_logger, "expiring-timers");
00331 current_dispatched =
00332 timer_mgr->Advance(network_time, max_timer_expires);
00333
00334 current_hdr = hdr;
00335 current_pkt = pkt;
00336 current_pktsrc = src_ps;
00337
00338 sessions->DispatchPacket(t, hdr, pkt, hdr_size, src_ps, pkt_elem);
00339 mgr.Drain();
00340
00341 current_hdr = 0; // done with these
00342 current_pkt = 0;
00343 current_pktsrc = 0;
00344
00345 processing_start_time = 0.0; // = "we're not processing now"
00346 current_dispatched = 0;
00347 }
|
|
||||||||||||||||||||
|
Definition at line 554 of file Net.cc. References bpf_program::bf_insns, bpf_filter(), BPF_Program::GetProgram(), and len. Referenced by NetSessions::NextPacketSecondary().
00556 {
00557 // NOTE: I don't like too much un-const'ing the pkt variable.
00558 return bpf_filter(fp->GetProgram()->bf_insns, (u_char*) pkt, len, caplen);
00559 }
|
|
|
Definition at line 398 of file Net.cc. References TimerMgr::Advance(), current_dispatched, current_time(), EventMgr::Drain(), PacketSortGlobalPQ::Empty(), IOSourceRegistry::FindSoonest(), have_pending_timers, io_sources, max_timer_expires, mgr, net_get_final_stats(), network_time, packet_sort_window, packet_sorter, IOSource::Process(), process_packet_sorter(), processing_start_time, reading_live, reading_traces, signal_val, IOSourceRegistry::Size(), TimerMgr::Size(), termination_signal(), timer_mgr, and using_communication. Referenced by main().
00399 {
00400 while ( io_sources.Size() || have_pending_timers )
00401 {
00402 double ts;
00403 IOSource* src =
00404 io_sources.Size() ? io_sources.FindSoonest(&ts) : 0;
00405
00406 if ( src )
00407 src->Process(); // which will call net_packet_arrival()
00408
00409 else if ( reading_live )
00410 {
00411 double ct = current_time();
00412 if ( packet_sorter && ! packet_sorter->Empty() )
00413 process_packet_sorter(ct);
00414 else
00415 {
00416 // Take advantage of the lull to get up to
00417 // date on timers and events.
00418 network_time = ct;
00419 timer_mgr->Advance(network_time, max_timer_expires);
00420 }
00421 }
00422
00423 else if ( have_pending_timers )
00424 {
00425 // Take advantage of the lull to get up to
00426 // date on timers and events. Because we only
00427 // have timers as sources, going to sleep here
00428 // doesn't risk blocking on other inputs.
00429 network_time = current_time();
00430 timer_mgr->Advance(network_time, max_timer_expires);
00431
00432 // Avoid busy-waiting - pause for 100 ms.
00433 // We pick a sleep value of 100 msec that buys
00434 // us a lot of idle time, but doesn't delay near-term
00435 // timers too much. (Delaying them somewhat is okay,
00436 // since Bro timers are not high-precision anyway.)
00437 if ( ! using_communication )
00438 usleep(100000);
00439
00440 // Flawfinder says about usleep:
00441 //
00442 // This C routine is considered obsolete (as opposed
00443 // to the shell command by the same name). The
00444 // interaction of this function with SIGALRM and
00445 // other timer functions such as sleep(), alarm(),
00446 // setitimer(), and nanosleep() is unspecified.
00447 // Use nanosleep(2) or setitimer(2) instead.
00448 }
00449
00450 mgr.Drain();
00451
00452 processing_start_time = 0.0; // = "we're not processing now"
00453 current_dispatched = 0;
00454
00455 // Should we put the signal handling into an IOSource?
00456 extern void termination_signal();
00457 if ( signal_val == SIGTERM || signal_val == SIGINT )
00458 // We received a signal while processing the
00459 // current packet and its related events.
00460 termination_signal();
00461
00462 if ( ! reading_traces )
00463 // Check whether we have timers scheduled for
00464 // the future on which we need to wait.
00465 have_pending_timers = timer_mgr->Size() > 0;
00466 }
00467
00468 if ( packet_sorter )
00469 {
00470 // Drain packets remaining in the packet sorter.
00471 process_packet_sorter(network_time + packet_sort_window + 100);
00472 }
00473
00474 // Get the final statistics now, and not when net_finish() is
00475 // called, since that might happen quite a bit in the future
00476 // due to expiring pending timers, and we don't want to ding
00477 // for any packets dropped beyond this point.
00478 net_get_final_stats();
00479 }
|
|
|
Definition at line 564 of file Net.cc. References _processing_suspended, bro_logger, and Logger::Log(). Referenced by RemoteSerializer::HandshakeDone().
00565 {
00566 if ( _processing_suspended == 0 )
00567 bro_logger->Log("processing suspended");
00568 ++_processing_suspended;
00569 }
|
|
|
|
|
|
Definition at line 349 of file Net.cc. References PacketSortElement::Hdr(), PacketSortElement::HdrSize(), net_packet_dispatch(), packet_sort_window, packet_sorter, PacketSortElement::Pkt(), PacketSortGlobalPQ::RemoveMin(), PacketSortElement::Src(), and PacketSortElement::TimeStamp(). Referenced by net_packet_arrival(), and net_run().
00350 {
00351 if ( ! packet_sorter )
00352 return 0;
00353
00354 double min_t = lastest_packet_time - packet_sort_window;
00355
00356 int num_pkts_dispatched = 0;
00357 PacketSortElement* pkt_elem;
00358
00359 // Dispatch packets in the packet_sorter until timestamp min_t.
00360 // It's possible that zero or multiple packets are dispatched.
00361 while ( (pkt_elem = packet_sorter->RemoveMin(min_t)) != 0 )
00362 {
00363 net_packet_dispatch(pkt_elem->TimeStamp(),
00364 pkt_elem->Hdr(), pkt_elem->Pkt(),
00365 pkt_elem->HdrSize(), pkt_elem->Src(),
00366 pkt_elem);
00367 ++num_pkts_dispatched;
00368 }
00369
00370 return num_pkts_dispatched;
00371 }
|
|
||||||||||||||||||||||||
|
Referenced by ChunkedIOFd::CanRead(), IOSourceRegistry::FindSoonest(), ChunkedIOFd::ReadChunk(), DNS_Mgr::Resolve(), and SocketComm::Run(). |
|
|
Definition at line 94 of file Net.cc. References bro_logger, current_dispatched, current_hdr, current_pkt, current_time(), PktDumper::Dump(), int, last_watchdog_proc_time, Logger::Log(), net_finish(), net_get_final_stats(), pkt_dumper, processing_start_time, RETSIGTYPE, RETSIGVAL, run_time(), safe_snprintf(), and watchdog_interval. Referenced by net_init().
00095 {
00096 if ( processing_start_time != 0.0 )
00097 {
00098 // The signal arrived while we're processing a packet and/or
00099 // its corresponding event queue. Check whether we've been
00100 // spending too much time, which we take to mean we've wedged.
00101
00102 // Note that it's subtle how exactly to test this. In
00103 // processing_start_time we have the timestamp of the packet
00104 // we're currently working on. But that *doesn't* mean that
00105 // we began work on the packet at that time; we could have
00106 // begun at a much later time, depending on how long the
00107 // packet filter waited (to fill its buffer) before handing
00108 // up this packet. So what we require is that the current
00109 // processing_start_time matches the processing_start_time we
00110 // observed last time the watchdog went off. If so, then
00111 // we've been working on the current packet for at least
00112 // watchdog_interval seconds.
00113
00114 if ( processing_start_time == last_watchdog_proc_time )
00115 {
00116 // snprintf() calls alloc/free routines if you use %f!
00117 // We need to avoid doing that given we're in a single
00118 // handler and the allocation routines are not
00119 // reentrant.
00120
00121 double ct = current_time();
00122
00123 int int_ct = int(ct);
00124 int frac_ct = int((ct - int_ct) * 1e6);
00125
00126 int int_pst = int(processing_start_time);
00127 int frac_pst =
00128 int((processing_start_time - int_pst) * 1e6);
00129
00130 char msg[512];
00131 safe_snprintf(msg, sizeof(msg),
00132 "**watchdog timer expired, t = %d.%06d, start = %d.%06d, dispatched = %d",
00133 int_ct, frac_ct, int_pst, frac_pst,
00134 current_dispatched);
00135
00136 bro_logger->Log(msg);
00137 run_time("watchdog timer expired");
00138
00139 if ( current_hdr && pkt_dumper)
00140 pkt_dumper->Dump(current_hdr, current_pkt);
00141 net_get_final_stats();
00142 net_finish(0);
00143
00144 abort();
00145 exit(1);
00146 }
00147 }
00148
00149 last_watchdog_proc_time = processing_start_time;
00150
00151 (void) alarm(watchdog_interval);
00152 return RETSIGVAL;
00153 }
|
|
|
Definition at line 562 of file Net.cc. Referenced by net_continue_processing(), net_is_processing_suspended(), and net_suspend_processing(). |
|
|
Definition at line 83 of file Net.cc. Referenced by main(), and RemoteSerializer::PeerConnected(). |
|
|
Definition at line 91 of file Net.cc. Referenced by net_packet_dispatch(), net_run(), and watchdog(). |
|
|
Definition at line 89 of file Net.cc. Referenced by net_packet_dispatch(), and watchdog(). |
|
|
Definition at line 90 of file Net.cc. Referenced by net_packet_dispatch(), and watchdog(). |
|
|
Definition at line 92 of file Net.cc. Referenced by net_packet_dispatch(). |
|
|
|
|
|
Definition at line 84 of file Net.cc. Referenced by watchdog(). |
|
|
|
Definition at line 87 of file Net.cc. Referenced by net_delete(), net_init(), net_packet_arrival(), net_run(), and process_packet_sorter(). |
|
|
Definition at line 65 of file Net.cc. Referenced by NetSessions::DumpPacket(), get_src_pkt_writer(), net_finish(), net_init(), and watchdog(). |
|
|
Definition at line 66 of file Net.cc. Referenced by net_finish(), and net_init(). |
|
|
Definition at line 82 of file Net.cc. Referenced by net_packet_dispatch(), net_run(), sig_handler(), and watchdog(). |
|
|
Definition at line 77 of file Net.cc. Referenced by PktSrc::ExtractNextPacket(), PktSrc::GetFds(), RemoteSerializer::Init(), main(), net_init(), NetSessions::NetSessions(), and PktSrc::NextTimestamp(). |
|
|
Definition at line 74 of file Net.cc. Referenced by TCP_Contents::AckReceived(), main(), net_init(), net_run(), and NetSessions::NetSessions(). |
|
|
Definition at line 75 of file Net.cc. Referenced by RemoteSerializer::Init(), main(), net_init(), net_run(), and PktSrc::Statistics(). |
|
|
Definition at line 71 of file Net.cc. Referenced by TCP_Connection::Init(), and net_init(). |
|
|
Definition at line 85 of file Net.cc. Referenced by IncrementalWriteTimer::Dispatch(), done_with_network(), UDP_NetbiosSSN::ExpireTimer(), UDP_DNS::ExpireTimer(), and terminate_bro(). |
|
|
Definition at line 69 of file Net.cc. Referenced by get_trace_rewriter(), TCP_Connection::Init(), net_delete(), and net_init(). |
|
|
Definition at line 72 of file Net.cc. Referenced by TCP_Connection::Init(). |
|
|
Definition at line 78 of file Net.cc. Referenced by main(). |
|
1.3.5