From 062cadb1245ee5905224f22c93194e820cbf816b Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Tue, 26 Nov 2019 12:06:45 -0700 Subject: [PATCH] Minor amount of code cleanup in Pcap IO source --- src/iosource/pcap/Source.cc | 56 ++++++++++--------------------------- src/iosource/pcap/Source.h | 1 - 2 files changed, 15 insertions(+), 42 deletions(-) diff --git a/src/iosource/pcap/Source.cc b/src/iosource/pcap/Source.cc index 0c8efd643d..47df453ff0 100644 --- a/src/iosource/pcap/Source.cc +++ b/src/iosource/pcap/Source.cc @@ -24,10 +24,10 @@ PcapSource::PcapSource(const std::string& path, bool is_live) { props.path = path; props.is_live = is_live; - pd = 0; + pd = nullptr; memset(¤t_hdr, 0, sizeof(current_hdr)); memset(&last_hdr, 0, sizeof(last_hdr)); - last_data = 0; + last_data = nullptr; } void PcapSource::Open() @@ -44,8 +44,8 @@ void PcapSource::Close() return; pcap_close(pd); - pd = 0; - last_data = 0; + pd = nullptr; + last_data = nullptr; Closed(); } @@ -53,18 +53,15 @@ void PcapSource::Close() void PcapSource::OpenLive() { char errbuf[PCAP_ERRBUF_SIZE]; - char tmp_errbuf[PCAP_ERRBUF_SIZE]; // Determine interface if not specified. if ( props.path.empty() ) { pcap_if_t* devs; - if ( pcap_findalldevs(&devs, tmp_errbuf) < 0 ) + if ( pcap_findalldevs(&devs, errbuf) < 0 ) { - snprintf(errbuf, sizeof(errbuf), - "pcap_findalldevs: %s", tmp_errbuf); - Error(errbuf); + Error(fmt("pcap_findalldevs: %s", errbuf)); return; } @@ -75,30 +72,26 @@ void PcapSource::OpenLive() if ( props.path.empty() ) { - snprintf(errbuf, sizeof(errbuf), - "pcap_findalldevs: empty device name"); - Error(errbuf); + Error("pcap_findalldevs: empty device name"); return; } } else { - snprintf(errbuf, sizeof(errbuf), - "pcap_findalldevs: no devices found"); - Error(errbuf); + Error("pcap_findalldevs: no devices found"); return; } } // Determine network and netmask. uint32_t net; - if ( pcap_lookupnet(props.path.c_str(), &net, &props.netmask, tmp_errbuf) < 0 ) + if ( pcap_lookupnet(props.path.c_str(), &net, &props.netmask, errbuf) < 0 ) { // ### The lookup can fail if no address is assigned to // the interface; and libpcap doesn't have any useful notion // of error codes, just error std::strings - how bogus - so we // just kludge around the error :-(. - // sprintf(errbuf, "pcap_lookupnet %s", tmp_errbuf); + // sprintf(errbuf, "pcap_lookupnet %s", errbuf); // return; props.netmask = 0xffffff00; } @@ -156,7 +149,7 @@ void PcapSource::OpenLive() } #ifdef HAVE_LINUX - if ( pcap_setnonblock(pd, 1, tmp_errbuf) < 0 ) + if ( pcap_setnonblock(pd, 1, errbuf) < 0 ) { PcapError("pcap_setnonblock"); return; @@ -167,14 +160,9 @@ void PcapSource::OpenLive() Info(fmt("pcap bufsize = %d\n", ((struct pcap *) pd)->bufsize)); #endif - props.selectable_fd = pcap_fileno(pd); - - SetHdrSize(); - - if ( ! pd ) - // Was closed, couldn't get header size. - return; + props.selectable_fd = pcap_get_selectable_fd(pd); + props.link_type = pcap_datalink(pd); props.is_live = true; Opened(props); @@ -192,18 +180,14 @@ void PcapSource::OpenOffline() return; } - SetHdrSize(); - - if ( ! pd ) - // Was closed, unknown link layer type. - return; - props.selectable_fd = fileno(pcap_file(pd)); if ( props.selectable_fd < 0 ) InternalError("OS does not support selectable pcap fd"); + props.link_type = pcap_datalink(pd); props.is_live = false; + Opened(props); } @@ -339,16 +323,6 @@ void PcapSource::PcapError(const char* where) Close(); } -void PcapSource::SetHdrSize() - { - if ( ! pd ) - return; - - char errbuf[PCAP_ERRBUF_SIZE]; - - props.link_type = pcap_datalink(pd); - } - iosource::PktSrc* PcapSource::Instantiate(const std::string& path, bool is_live) { return new PcapSource(path, is_live); diff --git a/src/iosource/pcap/Source.h b/src/iosource/pcap/Source.h index b9ef1e6da1..df57d6612d 100644 --- a/src/iosource/pcap/Source.h +++ b/src/iosource/pcap/Source.h @@ -28,7 +28,6 @@ private: void OpenLive(); void OpenOffline(); void PcapError(const char* where = 0); - void SetHdrSize(); Properties props; Stats stats;