Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PcapPlusPlus interface concurrency #123

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/inputs/pcap/PcapInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,49 @@ void PcapInputStream::start()
}

if (_cur_pcap_source == PcapSource::libpcap) {
pcpp::PcapLiveDevice *pcapDevice;
// extract pcap live device by interface name or IP address
if (interfaceIP4.isValid() || interfaceIP6.isValid()) {
if (interfaceIP4.isValid()) {
_pcapDevice = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP4);
pcapDevice = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP4);
} else {
_pcapDevice = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP6);
pcapDevice = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP6);
}
if (_pcapDevice == nullptr) {
if (pcapDevice == nullptr) {
throw PcapException("Couldn't find interface by provided IP: " + TARGET);
}
} else {
_pcapDevice = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(TARGET);
if (_pcapDevice == nullptr) {
pcapDevice = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(TARGET);
if (pcapDevice == nullptr) {
throw PcapException("Couldn't find interface by provided name: " + TARGET);
}
}

// PcapPlusPlus upstream incompatibility note: this block requires ns1 fork of PcapPlusPlus until upstream
// makes PcapLiveDevice constructor pubic
pcap_if_t *interfaceList;
char errbuf[PCAP_ERRBUF_SIZE];
int err = pcap_findalldevs(&interfaceList, errbuf);
if (err < 0) {
throw PcapException("Error searching for pcap devices: " + std::string(errbuf));
}

pcap_if_t *currInterface = interfaceList;
while (currInterface != NULL) {
if (currInterface->name != pcapDevice->getName()) {
currInterface = currInterface->next;
continue;
}
_pcapDevice = std::make_unique<pcpp::PcapLiveDevice>(currInterface, true, true, true);
break;
}

pcap_freealldevs(interfaceList);
if (_pcapDevice == nullptr) {
throw PcapException("Couldn't find interface by provided name: " + TARGET);
}
// end upstream PcapPlusPlus incompatibility block

_get_hosts_from_libpcap_iface();
_open_libpcap_iface(config_get<std::string>("bpf"));
} else if (_cur_pcap_source == PcapSource::af_packet) {
Expand Down
2 changes: 1 addition & 1 deletion src/inputs/pcap/PcapInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PcapInputStream : public visor::InputStream
PcapSource _cur_pcap_source{PcapSource::unknown};

// libpcap source
pcpp::PcapLiveDevice *_pcapDevice = nullptr; // non owning
std::unique_ptr<pcpp::PcapLiveDevice> _pcapDevice;
bool _pcapFile = false;

#ifdef __linux__
Expand Down