2015-04-16 11:50:07 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pcapport.h"
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
#include "devicemanager.h"
|
|
|
|
#include "packetbuffer.h"
|
|
|
|
|
2015-04-16 11:50:07 -05:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
pcap_if_t *PcapPort::deviceList_ = NULL;
|
|
|
|
|
|
|
|
PcapPort::PcapPort(int id, const char *device)
|
|
|
|
: AbstractPort(id, device)
|
|
|
|
{
|
|
|
|
monitorRx_ = new PortMonitor(device, kDirectionRx, &stats_);
|
|
|
|
monitorTx_ = new PortMonitor(device, kDirectionTx, &stats_);
|
2016-11-11 09:52:07 -06:00
|
|
|
transmitter_ = new PcapTransmitter(device, streamStats_);
|
2015-04-16 11:50:07 -05:00
|
|
|
capturer_ = new PortCapturer(device);
|
2015-11-14 05:36:43 -06:00
|
|
|
emulXcvr_ = new EmulationTransceiver(device, deviceManager_);
|
2023-03-22 06:02:41 -05:00
|
|
|
txTtagStatsPoller_ = new PcapTxTtagStats(device, id);
|
2023-02-08 04:48:02 -06:00
|
|
|
rxStatsPoller_ = new PcapRxStats(device, streamStats_, id);
|
2015-04-16 11:50:07 -05:00
|
|
|
|
|
|
|
if (!monitorRx_->handle() || !monitorTx_->handle())
|
|
|
|
isUsable_ = false;
|
|
|
|
|
|
|
|
if (!deviceList_)
|
|
|
|
{
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE];
|
|
|
|
|
|
|
|
if (pcap_findalldevs(&deviceList_, errbuf) == -1)
|
|
|
|
qDebug("Error in pcap_findalldevs_ex: %s\n", errbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pcap_if_t *dev = deviceList_; dev != NULL; dev = dev->next)
|
|
|
|
{
|
|
|
|
if (strcmp(device, dev->name) == 0)
|
|
|
|
{
|
|
|
|
if (dev->name)
|
|
|
|
data_.set_name(dev->name);
|
|
|
|
if (dev->description)
|
|
|
|
data_.set_description(dev->description);
|
|
|
|
|
|
|
|
//! \todo set port IP addr also
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::init()
|
|
|
|
{
|
2018-07-26 10:23:33 -05:00
|
|
|
AbstractPort::init();
|
|
|
|
|
2015-04-16 11:50:07 -05:00
|
|
|
if (!monitorTx_->isDirectional())
|
|
|
|
transmitter_->useExternalStats(&stats_);
|
|
|
|
|
|
|
|
transmitter_->setHandle(monitorRx_->handle());
|
|
|
|
|
|
|
|
updateNotes();
|
|
|
|
|
|
|
|
monitorRx_->start();
|
|
|
|
monitorTx_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
PcapPort::~PcapPort()
|
|
|
|
{
|
|
|
|
qDebug("In %s", __FUNCTION__);
|
|
|
|
|
|
|
|
if (monitorRx_)
|
|
|
|
monitorRx_->stop();
|
|
|
|
if (monitorTx_)
|
|
|
|
monitorTx_->stop();
|
|
|
|
|
2023-03-22 06:02:41 -05:00
|
|
|
txTtagStatsPoller_->stop();
|
|
|
|
delete txTtagStatsPoller_;
|
|
|
|
|
2016-11-17 10:14:34 -06:00
|
|
|
rxStatsPoller_->stop();
|
|
|
|
delete rxStatsPoller_;
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
delete emulXcvr_;
|
2015-04-16 11:50:07 -05:00
|
|
|
delete capturer_;
|
|
|
|
delete transmitter_;
|
|
|
|
|
|
|
|
if (monitorRx_)
|
|
|
|
monitorRx_->wait();
|
|
|
|
delete monitorRx_;
|
|
|
|
|
|
|
|
if (monitorTx_)
|
|
|
|
monitorTx_->wait();
|
|
|
|
delete monitorTx_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::updateNotes()
|
|
|
|
{
|
|
|
|
QString notes;
|
|
|
|
|
|
|
|
if ((!monitorRx_->isPromiscuous()) || (!monitorTx_->isPromiscuous()))
|
|
|
|
notes.append("<li>Non Promiscuous Mode</li>");
|
|
|
|
|
|
|
|
if (!monitorRx_->isDirectional() && !hasExclusiveControl())
|
|
|
|
notes.append("<li><i>Rx Frames/Bytes</i>: Includes non Ostinato Tx pkts also (Tx by Ostinato are not included)</li>");
|
|
|
|
|
|
|
|
if (!monitorTx_->isDirectional() && !hasExclusiveControl())
|
|
|
|
notes.append("<li><i>Tx Frames/Bytes</i>: Only Ostinato Tx pkts (Tx by others NOT included)</li>");
|
|
|
|
|
|
|
|
if (notes.isEmpty())
|
|
|
|
data_.set_notes("");
|
|
|
|
else
|
|
|
|
data_.set_notes(QString("<b>Limitation(s)</b>"
|
|
|
|
"<ul>%1</ul>"
|
|
|
|
"Rx/Tx Rates are also subject to above limitation(s)").
|
|
|
|
arg(notes).toStdString());
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:49:09 -06:00
|
|
|
bool PcapPort::setTrackStreamStats(bool enable)
|
|
|
|
{
|
|
|
|
bool val = enable ? startStreamStatsTracking() : stopStreamStatsTracking();
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
AbstractPort::setTrackStreamStats(enable);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-10-12 07:41:30 -05:00
|
|
|
bool PcapPort::setRateAccuracy(AbstractPort::Accuracy accuracy)
|
|
|
|
{
|
|
|
|
if (transmitter_->setRateAccuracy(accuracy)) {
|
|
|
|
AbstractPort::setRateAccuracy(accuracy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-08 04:48:02 -06:00
|
|
|
void PcapPort::updateStreamStats()
|
|
|
|
{
|
|
|
|
// XXX: PcapTxThread already does this at the end of transmit; we
|
2023-03-22 06:02:41 -05:00
|
|
|
// just dump tx/rx stats poller debug stats here
|
2023-02-08 04:48:02 -06:00
|
|
|
|
2023-03-22 06:02:41 -05:00
|
|
|
qDebug("port %d txTtagStatsPoller: %s",
|
|
|
|
id(), qUtf8Printable(txTtagStatsPoller_->debugStats()));
|
2023-02-08 04:48:02 -06:00
|
|
|
qDebug("port %d rxStatsPoller: %s",
|
|
|
|
id(), qUtf8Printable(rxStatsPoller_->debugStats()));
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:49:09 -06:00
|
|
|
void PcapPort::startDeviceEmulation()
|
|
|
|
{
|
|
|
|
emulXcvr_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::stopDeviceEmulation()
|
|
|
|
{
|
|
|
|
emulXcvr_->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PcapPort::sendEmulationPacket(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
return emulXcvr_->transmitPacket(pktBuf);
|
|
|
|
}
|
|
|
|
|
2016-11-17 10:14:34 -06:00
|
|
|
bool PcapPort::startStreamStatsTracking()
|
|
|
|
{
|
2016-12-06 10:11:47 -06:00
|
|
|
if (!transmitter_->setStreamStatsTracking(true))
|
|
|
|
goto _tx_fail;
|
2023-03-22 06:02:41 -05:00
|
|
|
if (!txTtagStatsPoller_->start())
|
|
|
|
goto _tx_ttag_fail;
|
2016-12-06 10:11:47 -06:00
|
|
|
if (!rxStatsPoller_->start())
|
|
|
|
goto _rx_fail;
|
2017-01-09 07:27:38 -06:00
|
|
|
/*
|
|
|
|
* If RxPoller receives both IN and OUT packets, packets Tx on this
|
|
|
|
* port will also be received by it and we consider it to be a Rx (IN)
|
|
|
|
* packet incorrectly - so adjust Rx stats for this case
|
|
|
|
* XXX - ideally, RxPoller should do this adjustment, but given our
|
|
|
|
* design, it is easier to implement in transmitter
|
|
|
|
*/
|
|
|
|
transmitter_->adjustRxStreamStats(!rxStatsPoller_->isDirectional());
|
2016-12-06 10:11:47 -06:00
|
|
|
return true;
|
|
|
|
|
|
|
|
_rx_fail:
|
2023-03-22 06:02:41 -05:00
|
|
|
txTtagStatsPoller_->stop();
|
|
|
|
_tx_ttag_fail:
|
2016-12-06 10:11:47 -06:00
|
|
|
transmitter_->setStreamStatsTracking(false);
|
|
|
|
_tx_fail:
|
|
|
|
qWarning("failed to start stream stats tracking");
|
|
|
|
return false;
|
2016-11-17 10:14:34 -06:00
|
|
|
}
|
|
|
|
|
2023-03-22 06:02:41 -05:00
|
|
|
// FIXME: stop everything possible, don't revert in case of error
|
2016-11-17 10:14:34 -06:00
|
|
|
bool PcapPort::stopStreamStatsTracking()
|
|
|
|
{
|
2016-12-06 10:11:47 -06:00
|
|
|
if (!transmitter_->setStreamStatsTracking(false))
|
|
|
|
goto _tx_fail;
|
2023-03-22 06:02:41 -05:00
|
|
|
if (!txTtagStatsPoller_->stop())
|
|
|
|
goto _tx_ttag_fail;
|
2016-12-06 10:11:47 -06:00
|
|
|
if (!rxStatsPoller_->stop())
|
|
|
|
goto _rx_fail;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
_rx_fail:
|
2023-03-22 06:02:41 -05:00
|
|
|
_tx_ttag_fail:
|
|
|
|
transmitter_->setStreamStatsTracking(true); // FIXME: needed?
|
2016-12-06 10:11:47 -06:00
|
|
|
_tx_fail:
|
|
|
|
qWarning("failed to stop stream stats tracking");
|
|
|
|
return false;
|
2016-11-17 10:14:34 -06:00
|
|
|
}
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
/*
|
|
|
|
* ------------------------------------------------------------------- *
|
|
|
|
* Port Monitor
|
|
|
|
* ------------------------------------------------------------------- *
|
|
|
|
*/
|
2015-04-16 11:50:07 -05:00
|
|
|
PcapPort::PortMonitor::PortMonitor(const char *device, Direction direction,
|
|
|
|
AbstractPort::PortStats *stats)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE] = "";
|
|
|
|
bool noLocalCapture;
|
|
|
|
|
2020-05-15 08:25:03 -05:00
|
|
|
setObjectName(QString("Mon%1:%2")
|
|
|
|
.arg(direction == kDirectionRx ? "Rx" : "Tx")
|
|
|
|
.arg(device));
|
2015-04-16 11:50:07 -05:00
|
|
|
direction_ = direction;
|
|
|
|
isDirectional_ = true;
|
|
|
|
isPromisc_ = true;
|
|
|
|
noLocalCapture = true;
|
|
|
|
stats_ = stats;
|
|
|
|
stop_ = false;
|
|
|
|
|
|
|
|
_retry:
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (isPromisc_)
|
|
|
|
flags |= PCAP_OPENFLAG_PROMISCUOUS;
|
|
|
|
if (noLocalCapture)
|
|
|
|
flags |= PCAP_OPENFLAG_NOCAPTURE_LOCAL;
|
|
|
|
|
|
|
|
handle_ = pcap_open(device, 64 /* FIXME */, flags,
|
|
|
|
1000 /* ms */, NULL, errbuf);
|
|
|
|
#else
|
|
|
|
handle_ = pcap_open_live(device, 64 /* FIXME */, int(isPromisc_),
|
|
|
|
1000 /* ms */, errbuf);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (handle_ == NULL)
|
|
|
|
{
|
|
|
|
if (isPromisc_ && QString(errbuf).contains("promiscuous"))
|
|
|
|
{
|
|
|
|
qDebug("Can't set promiscuous mode, trying non-promisc %s", device);
|
|
|
|
isPromisc_ = false;
|
|
|
|
goto _retry;
|
|
|
|
}
|
|
|
|
else if (noLocalCapture && QString(errbuf).contains("loopback"))
|
|
|
|
{
|
|
|
|
qDebug("Can't set no local capture mode %s", device);
|
|
|
|
noLocalCapture = false;
|
|
|
|
goto _retry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto _open_error;
|
|
|
|
}
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// pcap_setdirection() API is not supported in Windows.
|
|
|
|
// NOTE: WinPcap 4.1.1 and above exports a dummy API that returns -1
|
|
|
|
// but since we would like to work with previous versions of WinPcap
|
|
|
|
// also, we assume the API does not exist
|
|
|
|
ret = -1;
|
|
|
|
#else
|
|
|
|
switch (direction_)
|
|
|
|
{
|
|
|
|
case kDirectionRx:
|
|
|
|
ret = pcap_setdirection(handle_, PCAP_D_IN);
|
|
|
|
break;
|
|
|
|
case kDirectionTx:
|
|
|
|
ret = pcap_setdirection(handle_, PCAP_D_OUT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -1; // avoid 'may be used uninitialized' warning
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto _set_direction_error;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
_set_direction_error:
|
|
|
|
qDebug("Error setting direction(%d) %s: %s\n", direction, device,
|
|
|
|
pcap_geterr(handle_));
|
|
|
|
isDirectional_ = false;
|
|
|
|
return;
|
|
|
|
|
|
|
|
_open_error:
|
|
|
|
qDebug("%s: Error opening port %s: %s\n", __FUNCTION__, device, errbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
PcapPort::PortMonitor::~PortMonitor()
|
|
|
|
{
|
|
|
|
if (handle_)
|
|
|
|
pcap_close(handle_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::PortMonitor::run()
|
|
|
|
{
|
|
|
|
while (!stop_)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct pcap_pkthdr *hdr;
|
|
|
|
const uchar *data;
|
|
|
|
|
|
|
|
ret = pcap_next_ex(handle_, &hdr, &data);
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
switch (direction_)
|
|
|
|
{
|
|
|
|
case kDirectionRx:
|
|
|
|
stats_->rxPkts++;
|
|
|
|
stats_->rxBytes += hdr->len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kDirectionTx:
|
|
|
|
if (isDirectional_)
|
|
|
|
{
|
|
|
|
stats_->txPkts++;
|
|
|
|
stats_->txBytes += hdr->len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \todo TODO pkt/bit rates
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
//qDebug("%s: timeout. continuing ...", __PRETTY_FUNCTION__);
|
|
|
|
continue;
|
|
|
|
case -1:
|
|
|
|
qWarning("%s: error reading packet (%d): %s",
|
|
|
|
__PRETTY_FUNCTION__, ret, pcap_geterr(handle_));
|
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
qWarning("%s: error reading packet (%d): %s",
|
|
|
|
__PRETTY_FUNCTION__, ret, pcap_geterr(handle_));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qFatal("%s: Unexpected return value %d", __PRETTY_FUNCTION__, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::PortMonitor::stop()
|
|
|
|
{
|
|
|
|
stop_ = true;
|
2020-02-12 06:44:48 -06:00
|
|
|
if (handle())
|
|
|
|
pcap_breakloop(handle());
|
2015-04-16 11:50:07 -05:00
|
|
|
}
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
/*
|
|
|
|
* ------------------------------------------------------------------- *
|
|
|
|
* Port Capturer
|
|
|
|
* ------------------------------------------------------------------- *
|
|
|
|
*/
|
2015-04-16 11:50:07 -05:00
|
|
|
PcapPort::PortCapturer::PortCapturer(const char *device)
|
|
|
|
{
|
2018-03-15 09:04:42 -05:00
|
|
|
device_ = QString::fromLatin1(device);
|
2020-05-15 08:25:03 -05:00
|
|
|
setObjectName(QString("Capture:%1").arg(device_));
|
2015-04-16 11:50:07 -05:00
|
|
|
stop_ = false;
|
|
|
|
state_ = kNotStarted;
|
|
|
|
|
|
|
|
if (!capFile_.open())
|
|
|
|
qWarning("Unable to open temp cap file");
|
|
|
|
|
2018-03-15 09:04:42 -05:00
|
|
|
qDebug("cap file = %s", qPrintable(capFile_.fileName()));
|
2015-04-16 11:50:07 -05:00
|
|
|
|
|
|
|
dumpHandle_ = NULL;
|
|
|
|
handle_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PcapPort::PortCapturer::~PortCapturer()
|
|
|
|
{
|
|
|
|
capFile_.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::PortCapturer::run()
|
|
|
|
{
|
|
|
|
int flag = PCAP_OPENFLAG_PROMISCUOUS;
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE] = "";
|
|
|
|
|
|
|
|
qDebug("In %s", __PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
if (!capFile_.isOpen())
|
|
|
|
{
|
|
|
|
qWarning("temp cap file is not open");
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
_retry:
|
2018-03-15 09:04:42 -05:00
|
|
|
handle_ = pcap_open_live(qPrintable(device_), 65535,
|
2015-04-16 11:50:07 -05:00
|
|
|
flag, 1000 /* ms */, errbuf);
|
|
|
|
|
|
|
|
if (handle_ == NULL)
|
|
|
|
{
|
|
|
|
if (flag && QString(errbuf).contains("promiscuous"))
|
|
|
|
{
|
|
|
|
qDebug("%s:can't set promiscuous mode, trying non-promisc",
|
2018-03-15 09:04:42 -05:00
|
|
|
qPrintable(device_));
|
2015-04-16 11:50:07 -05:00
|
|
|
flag = 0;
|
|
|
|
goto _retry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug("%s: Error opening port %s: %s\n", __FUNCTION__,
|
2018-03-15 09:04:42 -05:00
|
|
|
qPrintable(device_), errbuf);
|
2015-04-16 11:50:07 -05:00
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 09:04:42 -05:00
|
|
|
dumpHandle_ = pcap_dump_open(handle_, qPrintable(capFile_.fileName()));
|
2020-02-12 06:44:48 -06:00
|
|
|
if (!dumpHandle_) {
|
|
|
|
qDebug("failed to start capture: %s", pcap_geterr(handle_));
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::preRun();
|
2015-04-16 11:50:07 -05:00
|
|
|
state_ = kRunning;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct pcap_pkthdr *hdr;
|
|
|
|
const uchar *data;
|
|
|
|
|
|
|
|
ret = pcap_next_ex(handle_, &hdr, &data);
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
pcap_dump((uchar*) dumpHandle_, hdr, data);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
// timeout: just go back to the loop
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
qWarning("%s: error reading packet (%d): %s",
|
|
|
|
__PRETTY_FUNCTION__, ret, pcap_geterr(handle_));
|
|
|
|
break;
|
|
|
|
case -2:
|
2019-08-10 02:46:18 -05:00
|
|
|
qDebug("Loop/signal break or some other error");
|
|
|
|
break;
|
2015-04-16 11:50:07 -05:00
|
|
|
default:
|
2019-08-10 02:46:18 -05:00
|
|
|
qWarning("%s: Unexpected return value %d", __PRETTY_FUNCTION__, ret);
|
|
|
|
stop_ = true;
|
2015-04-16 11:50:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_)
|
|
|
|
{
|
2019-08-10 02:46:18 -05:00
|
|
|
qDebug("user requested capture stop");
|
2015-04-16 11:50:07 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::postRun();
|
|
|
|
|
2015-04-16 11:50:07 -05:00
|
|
|
pcap_dump_close(dumpHandle_);
|
|
|
|
pcap_close(handle_);
|
|
|
|
dumpHandle_ = NULL;
|
|
|
|
handle_ = NULL;
|
|
|
|
stop_ = false;
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
state_ = kFinished;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::PortCapturer::start()
|
|
|
|
{
|
|
|
|
// FIXME: return error
|
|
|
|
if (state_ == kRunning) {
|
|
|
|
qWarning("Capture start requested but is already running!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = kNotStarted;
|
|
|
|
QThread::start();
|
|
|
|
|
|
|
|
while (state_ == kNotStarted)
|
|
|
|
QThread::msleep(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapPort::PortCapturer::stop()
|
|
|
|
{
|
|
|
|
if (state_ == kRunning) {
|
|
|
|
stop_ = true;
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::stop(handle_);
|
2015-04-16 11:50:07 -05:00
|
|
|
while (state_ == kRunning)
|
|
|
|
QThread::msleep(10);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// FIXME: return error
|
|
|
|
qWarning("Capture stop requested but is not running!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PcapPort::PortCapturer::isRunning()
|
|
|
|
{
|
|
|
|
return (state_ == kRunning);
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile* PcapPort::PortCapturer::captureFile()
|
|
|
|
{
|
|
|
|
return &capFile_;
|
|
|
|
}
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ------------------------------------------------------------------- *
|
2015-11-14 05:36:43 -06:00
|
|
|
* Transmit+Receiver for Device/ProtocolEmulation
|
2015-09-14 07:49:52 -05:00
|
|
|
* ------------------------------------------------------------------- *
|
|
|
|
*/
|
2015-11-14 05:36:43 -06:00
|
|
|
PcapPort::EmulationTransceiver::EmulationTransceiver(const char *device,
|
2015-09-14 07:49:52 -05:00
|
|
|
DeviceManager *deviceManager)
|
|
|
|
{
|
2018-03-15 09:04:42 -05:00
|
|
|
device_ = QString::fromLatin1(device);
|
2020-05-15 08:25:03 -05:00
|
|
|
setObjectName(QString("EmulXcvr:%1").arg(device_));
|
2015-09-14 07:49:52 -05:00
|
|
|
deviceManager_ = deviceManager;
|
|
|
|
stop_ = false;
|
|
|
|
state_ = kNotStarted;
|
|
|
|
handle_ = NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
PcapPort::EmulationTransceiver::~EmulationTransceiver()
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
void PcapPort::EmulationTransceiver::run()
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-10-03 10:57:15 -05:00
|
|
|
int flags = PCAP_OPENFLAG_PROMISCUOUS;
|
2015-09-14 07:49:52 -05:00
|
|
|
char errbuf[PCAP_ERRBUF_SIZE] = "";
|
|
|
|
struct bpf_program bpf;
|
2015-12-14 09:34:58 -06:00
|
|
|
#if 0
|
2016-03-24 08:17:43 -05:00
|
|
|
const char *capture_filter =
|
|
|
|
"arp or icmp or icmp6 or "
|
|
|
|
"(vlan and (arp or icmp or icmp6)) or "
|
|
|
|
"(vlan and vlan and (arp or icmp or icmp6)) or "
|
|
|
|
"(vlan and vlan and vlan and (arp or icmp or icmp6)) or "
|
|
|
|
"(vlan and vlan and vlan and vlan and (arp or icmp or icmp6))";
|
2015-12-14 09:34:58 -06:00
|
|
|
/*
|
2016-03-24 08:17:43 -05:00
|
|
|
Ideally we should use the above filter, but the 'vlan' capture filter
|
2015-12-14 09:34:58 -06:00
|
|
|
in libpcap is implemented as a kludge. From the pcap-filter man page -
|
|
|
|
|
|
|
|
vlan [vlan_id]
|
|
|
|
Note that the first vlan keyword encountered in expression changes
|
|
|
|
the decoding offsets for the remainder of expression on the
|
|
|
|
assumption that the packet is a VLAN packet.
|
|
|
|
|
|
|
|
The vlan [vlan_id] expression may be used more than once, to filter on
|
|
|
|
VLAN hierarchies. Each use of that expression increments the filter
|
|
|
|
offsets by 4.
|
|
|
|
|
|
|
|
See https://ask.wireshark.org/questions/31953/unusual-behavior-with-stacked-vlan-tags-and-capture-filter
|
|
|
|
|
|
|
|
So we use the modified filter expression that works as we intend. If ever
|
|
|
|
libpcap changes their implementation, this will need to change as well.
|
|
|
|
*/
|
|
|
|
#else
|
|
|
|
const char *capture_filter =
|
2016-01-08 10:29:34 -06:00
|
|
|
"arp or icmp or icmp6 or "
|
2016-01-06 06:40:28 -06:00
|
|
|
"(vlan and (arp or icmp or icmp6)) or "
|
|
|
|
"(vlan and (arp or icmp or icmp6)) or "
|
|
|
|
"(vlan and (arp or icmp or icmp6)) or "
|
|
|
|
"(vlan and (arp or icmp or icmp6))";
|
2015-12-14 09:34:58 -06:00
|
|
|
#endif
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
const int optimize = 1;
|
2015-11-14 05:36:43 -06:00
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
qDebug("In %s", __PRETTY_FUNCTION__);
|
|
|
|
|
2015-10-03 10:57:15 -05:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
flags |= PCAP_OPENFLAG_NOCAPTURE_LOCAL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
2016-06-01 10:21:29 -05:00
|
|
|
_retry:
|
2015-10-03 10:57:15 -05:00
|
|
|
// NOCAPTURE_LOCAL needs windows only pcap_open()
|
2015-11-14 05:36:43 -06:00
|
|
|
handle_ = pcap_open(qPrintable(device_), 65535,
|
2016-01-12 07:38:01 -06:00
|
|
|
flags, 100 /* ms */, NULL, errbuf);
|
2015-10-03 10:57:15 -05:00
|
|
|
#else
|
2015-11-14 05:36:43 -06:00
|
|
|
handle_ = pcap_open_live(qPrintable(device_), 65535,
|
2016-01-12 07:38:01 -06:00
|
|
|
flags, 100 /* ms */, errbuf);
|
2015-10-03 10:57:15 -05:00
|
|
|
#endif
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
if (handle_ == NULL)
|
|
|
|
{
|
2015-10-03 10:57:15 -05:00
|
|
|
if (flags && QString(errbuf).contains("promiscuous"))
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2018-03-15 09:04:42 -05:00
|
|
|
Xnotify("Unable to set promiscuous mode on <%s> - "
|
2015-11-14 05:36:43 -06:00
|
|
|
"device emulation will not work", qPrintable(device_));
|
|
|
|
goto _exit;
|
2015-10-03 10:57:15 -05:00
|
|
|
}
|
|
|
|
#ifdef Q_OS_WIN32
|
2015-11-14 05:36:43 -06:00
|
|
|
else if ((flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
|
2015-10-03 10:57:15 -05:00
|
|
|
&& QString(errbuf).contains("loopback"))
|
|
|
|
{
|
|
|
|
qDebug("Can't set no local capture mode %s", qPrintable(device_));
|
|
|
|
flags &= ~PCAP_OPENFLAG_NOCAPTURE_LOCAL;
|
2015-09-14 07:49:52 -05:00
|
|
|
goto _retry;
|
|
|
|
}
|
2015-10-03 10:57:15 -05:00
|
|
|
#endif
|
2015-09-14 07:49:52 -05:00
|
|
|
else
|
|
|
|
{
|
2018-03-15 09:04:42 -05:00
|
|
|
Xnotify("Unable to open <%s> [%s] - device emulation will not work",
|
2015-11-14 05:36:43 -06:00
|
|
|
qPrintable(device_), errbuf);
|
2015-09-14 07:49:52 -05:00
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:17:43 -05:00
|
|
|
// TODO: for now the filter is hardcoded to accept tagged/untagged
|
|
|
|
// ARP/NDP or ICMPv4/v6; when more protocols are added, we may need
|
|
|
|
// to derive this filter based on which protocols are configured
|
|
|
|
// on the devices
|
2015-11-14 05:36:43 -06:00
|
|
|
if (pcap_compile(handle_, &bpf, capture_filter, optimize, 0) < 0)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
qWarning("%s: error compiling filter: %s", qPrintable(device_),
|
|
|
|
pcap_geterr(handle_));
|
|
|
|
goto _skip_filter;
|
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
if (pcap_setfilter(handle_, &bpf) < 0)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
qWarning("%s: error setting filter: %s", qPrintable(device_),
|
|
|
|
pcap_geterr(handle_));
|
|
|
|
goto _skip_filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
_skip_filter:
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::preRun();
|
2015-09-14 07:49:52 -05:00
|
|
|
state_ = kRunning;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct pcap_pkthdr *hdr;
|
|
|
|
const uchar *data;
|
|
|
|
|
|
|
|
ret = pcap_next_ex(handle_, &hdr, &data);
|
|
|
|
switch (ret)
|
|
|
|
{
|
2015-11-14 05:36:43 -06:00
|
|
|
case 1:
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
PacketBuffer *pktBuf = new PacketBuffer(data, hdr->caplen);
|
2016-01-08 10:29:34 -06:00
|
|
|
#if 0
|
|
|
|
for (int i = 0; i < 64; i++) {
|
|
|
|
printf("%02x ", data[i]);
|
|
|
|
if (i % 16 == 0)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
2015-09-14 07:49:52 -05:00
|
|
|
// XXX: deviceManager should free pktBuf before returning
|
|
|
|
// from this call; if it needs to process the pkt async
|
|
|
|
// it should make a copy as the pktBuf's data buffer is
|
2015-11-14 05:36:43 -06:00
|
|
|
// owned by libpcap which does not guarantee data will
|
2015-09-14 07:49:52 -05:00
|
|
|
// persist across calls to pcap_next_ex()
|
2015-11-14 05:36:43 -06:00
|
|
|
deviceManager_->receivePacket(pktBuf);
|
2015-09-14 07:49:52 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0:
|
|
|
|
// timeout: just go back to the loop
|
|
|
|
break;
|
|
|
|
case -1:
|
2015-11-14 05:36:43 -06:00
|
|
|
qWarning("%s: error reading packet (%d): %s",
|
2015-09-14 07:49:52 -05:00
|
|
|
__PRETTY_FUNCTION__, ret, pcap_geterr(handle_));
|
|
|
|
break;
|
|
|
|
case -2:
|
2019-08-10 02:46:18 -05:00
|
|
|
qDebug("Loop/signal break or some other error");
|
|
|
|
break;
|
2015-09-14 07:49:52 -05:00
|
|
|
default:
|
2019-08-10 02:46:18 -05:00
|
|
|
qWarning("%s: Unexpected return value %d", __PRETTY_FUNCTION__,
|
2015-09-14 07:49:52 -05:00
|
|
|
ret);
|
2019-08-10 02:46:18 -05:00
|
|
|
stop_ = true;
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
if (stop_)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2019-08-10 02:46:18 -05:00
|
|
|
qDebug("user requested receiver stop");
|
2015-09-14 07:49:52 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::postRun();
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
pcap_close(handle_);
|
|
|
|
handle_ = NULL;
|
|
|
|
stop_ = false;
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
state_ = kFinished;
|
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
void PcapPort::EmulationTransceiver::start()
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
if (state_ == kRunning) {
|
|
|
|
qWarning("Receive start requested but is already running!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = kNotStarted;
|
|
|
|
QThread::start();
|
|
|
|
|
|
|
|
while (state_ == kNotStarted)
|
|
|
|
QThread::msleep(10);
|
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
void PcapPort::EmulationTransceiver::stop()
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
if (state_ == kRunning) {
|
|
|
|
stop_ = true;
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::stop(handle_);
|
2015-09-14 07:49:52 -05:00
|
|
|
while (state_ == kRunning)
|
|
|
|
QThread::msleep(10);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qWarning("Receive stop requested but is not running!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
bool PcapPort::EmulationTransceiver::isRunning()
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
return (state_ == kRunning);
|
|
|
|
}
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
int PcapPort::EmulationTransceiver::transmitPacket(PacketBuffer *pktBuf)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
return pcap_sendpacket(handle_, pktBuf->data(), pktBuf->length());
|
|
|
|
}
|