Minor amount of code cleanup in Pcap IO source

This commit is contained in:
Tim Wojtulewicz 2019-11-26 12:06:45 -07:00
parent a00d11e44d
commit 062cadb124
2 changed files with 15 additions and 42 deletions

View file

@ -24,10 +24,10 @@ PcapSource::PcapSource(const std::string& path, bool is_live)
{ {
props.path = path; props.path = path;
props.is_live = is_live; props.is_live = is_live;
pd = 0; pd = nullptr;
memset(&current_hdr, 0, sizeof(current_hdr)); memset(&current_hdr, 0, sizeof(current_hdr));
memset(&last_hdr, 0, sizeof(last_hdr)); memset(&last_hdr, 0, sizeof(last_hdr));
last_data = 0; last_data = nullptr;
} }
void PcapSource::Open() void PcapSource::Open()
@ -44,8 +44,8 @@ void PcapSource::Close()
return; return;
pcap_close(pd); pcap_close(pd);
pd = 0; pd = nullptr;
last_data = 0; last_data = nullptr;
Closed(); Closed();
} }
@ -53,18 +53,15 @@ void PcapSource::Close()
void PcapSource::OpenLive() void PcapSource::OpenLive()
{ {
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
char tmp_errbuf[PCAP_ERRBUF_SIZE];
// Determine interface if not specified. // Determine interface if not specified.
if ( props.path.empty() ) if ( props.path.empty() )
{ {
pcap_if_t* devs; pcap_if_t* devs;
if ( pcap_findalldevs(&devs, tmp_errbuf) < 0 ) if ( pcap_findalldevs(&devs, errbuf) < 0 )
{ {
snprintf(errbuf, sizeof(errbuf), Error(fmt("pcap_findalldevs: %s", errbuf));
"pcap_findalldevs: %s", tmp_errbuf);
Error(errbuf);
return; return;
} }
@ -75,30 +72,26 @@ void PcapSource::OpenLive()
if ( props.path.empty() ) if ( props.path.empty() )
{ {
snprintf(errbuf, sizeof(errbuf), Error("pcap_findalldevs: empty device name");
"pcap_findalldevs: empty device name");
Error(errbuf);
return; return;
} }
} }
else else
{ {
snprintf(errbuf, sizeof(errbuf), Error("pcap_findalldevs: no devices found");
"pcap_findalldevs: no devices found");
Error(errbuf);
return; return;
} }
} }
// Determine network and netmask. // Determine network and netmask.
uint32_t net; 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 lookup can fail if no address is assigned to
// the interface; and libpcap doesn't have any useful notion // the interface; and libpcap doesn't have any useful notion
// of error codes, just error std::strings - how bogus - so we // of error codes, just error std::strings - how bogus - so we
// just kludge around the error :-(. // just kludge around the error :-(.
// sprintf(errbuf, "pcap_lookupnet %s", tmp_errbuf); // sprintf(errbuf, "pcap_lookupnet %s", errbuf);
// return; // return;
props.netmask = 0xffffff00; props.netmask = 0xffffff00;
} }
@ -156,7 +149,7 @@ void PcapSource::OpenLive()
} }
#ifdef HAVE_LINUX #ifdef HAVE_LINUX
if ( pcap_setnonblock(pd, 1, tmp_errbuf) < 0 ) if ( pcap_setnonblock(pd, 1, errbuf) < 0 )
{ {
PcapError("pcap_setnonblock"); PcapError("pcap_setnonblock");
return; return;
@ -167,14 +160,9 @@ void PcapSource::OpenLive()
Info(fmt("pcap bufsize = %d\n", ((struct pcap *) pd)->bufsize)); Info(fmt("pcap bufsize = %d\n", ((struct pcap *) pd)->bufsize));
#endif #endif
props.selectable_fd = pcap_fileno(pd); props.selectable_fd = pcap_get_selectable_fd(pd);
SetHdrSize();
if ( ! pd )
// Was closed, couldn't get header size.
return;
props.link_type = pcap_datalink(pd);
props.is_live = true; props.is_live = true;
Opened(props); Opened(props);
@ -192,18 +180,14 @@ void PcapSource::OpenOffline()
return; return;
} }
SetHdrSize();
if ( ! pd )
// Was closed, unknown link layer type.
return;
props.selectable_fd = fileno(pcap_file(pd)); props.selectable_fd = fileno(pcap_file(pd));
if ( props.selectable_fd < 0 ) if ( props.selectable_fd < 0 )
InternalError("OS does not support selectable pcap fd"); InternalError("OS does not support selectable pcap fd");
props.link_type = pcap_datalink(pd);
props.is_live = false; props.is_live = false;
Opened(props); Opened(props);
} }
@ -339,16 +323,6 @@ void PcapSource::PcapError(const char* where)
Close(); 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) iosource::PktSrc* PcapSource::Instantiate(const std::string& path, bool is_live)
{ {
return new PcapSource(path, is_live); return new PcapSource(path, is_live);

View file

@ -28,7 +28,6 @@ private:
void OpenLive(); void OpenLive();
void OpenOffline(); void OpenOffline();
void PcapError(const char* where = 0); void PcapError(const char* where = 0);
void SetHdrSize();
Properties props; Properties props;
Stats stats; Stats stats;