Feature (contd.): Device Emulation - Got rid of a bunch of FIXMEs and all trailing whitespace (in the code added for this feature)

This commit is contained in:
Srivats P 2015-11-14 17:06:43 +05:30
parent 7daf75c95a
commit 3a5396c865
9 changed files with 94 additions and 105 deletions

View File

@ -30,6 +30,9 @@ class StreamBase;
class PacketBuffer;
class QIODevice;
// TODO: send notification back to client(s)
#define notify qWarning
class AbstractPort
{
public:

View File

@ -32,7 +32,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
const quint64 kBcastMac = 0xffffffffffffULL;
// FIXME: add lock to protect deviceGroupList_ operations?
// XXX: Port owning DeviceManager already uses locks, so we don't use any
// locks within DeviceManager to protect deviceGroupList_ et.al.
DeviceManager::DeviceManager(AbstractPort *parent)
{
@ -137,8 +138,6 @@ int DeviceManager::deviceCount()
void DeviceManager::getDeviceList(
OstProto::PortDeviceList *deviceList)
{
int count = 0;
foreach(Device *device, deviceList_) {
OstEmul::Device *dev =
deviceList->AddExtension(OstEmul::port_device);
@ -160,7 +159,11 @@ void DeviceManager::receivePacket(PacketBuffer *pktBuf)
// We assume pkt is ethernet
// TODO: extend for other link layer types
// FIXME: validate before extracting if the offset is within pktLen
// All frames we are interested in should be at least 32 bytes
if (pktBuf->length() < 32) {
qWarning("short frame of %d bytes, skipping ...", pktBuf->length());
return;
}
// Extract dstMac
dstMac = qFromBigEndian<quint32>(pktData + offset);

View File

@ -602,9 +602,12 @@ _invalid_version:
/*
* ===================================================================
* Device Emulation
* FIXME: Locking for these functions is at Port level, should it be
* moved to inside DeviceManager instead? In other words, are
* streams/ports and devices independent?
* ===================================================================
* XXX: Streams and Devices are largely non-overlapping from a RPC
* point of view but they *do* intersect e.g. when a stream is trying to
* find its associated device and info from that device such as src/dst
* mac addresses. For this reason, both set of RPCs share the common
* port level locking
* ===================================================================
*/
void MyService::getDeviceGroupIdList(
@ -699,10 +702,8 @@ void MyService::addDeviceGroup(
devMgr = portInfo[portId]->deviceManager();
#if 0 // FIXME: needed?
if (portInfo[portId]->isTransmitOn())
goto _port_busy;
#endif
portLock[portId]->lockForWrite();
for (int i = 0; i < request->device_group_id_size(); i++)
@ -723,11 +724,9 @@ void MyService::addDeviceGroup(
done->Run();
return;
#if 0 // FIXME: needed?
_port_busy:
controller->SetFailed("Port Busy");
goto _exit;
#endif
_invalid_port:
controller->SetFailed("invalid portid");
@ -752,10 +751,8 @@ void MyService::deleteDeviceGroup(
devMgr = portInfo[portId]->deviceManager();
#if 0 // FIXME: needed?
if (portInfo[portId]->isTransmitOn())
goto _port_busy;
#endif
portLock[portId]->lockForWrite();
for (int i = 0; i < request->device_group_id_size(); i++)
@ -767,11 +764,9 @@ void MyService::deleteDeviceGroup(
done->Run();
return;
#if 0 // FIXME: needed?
_port_busy:
controller->SetFailed("Port Busy");
goto _exit;
#endif
_invalid_port:
controller->SetFailed("invalid portid");
_exit:
@ -795,28 +790,22 @@ void MyService::modifyDeviceGroup(
devMgr = portInfo[portId]->deviceManager();
#if 0 // FIXME: needed?
if (portInfo[portId]->isTransmitOn())
goto _port_busy;
#endif
portLock[portId]->lockForWrite();
for (int i = 0; i < request->device_group_size(); i++)
devMgr->modifyDeviceGroup(&request->device_group(i));
portLock[portId]->unlock();
// FIXME: check for overlaps between devices?
//! \todo(LOW): fill-in response "Ack"????
done->Run();
return;
#if 0 // FIXME: needed?
_port_busy:
controller->SetFailed("Port Busy");
goto _exit;
#endif
_invalid_port:
controller->SetFailed("invalid portid");
_exit:

View File

@ -84,7 +84,7 @@ PcapPort::PcapPort(int id, const char *device)
monitorTx_ = new PortMonitor(device, kDirectionTx, &stats_);
transmitter_ = new PortTransmitter(device);
capturer_ = new PortCapturer(device);
receiver_ = new PortReceiver(device, deviceManager_);
emulXcvr_ = new EmulationTransceiver(device, deviceManager_);
if (!monitorRx_->handle() || !monitorTx_->handle())
isUsable_ = false;
@ -137,7 +137,7 @@ PcapPort::~PcapPort()
if (monitorTx_)
monitorTx_->stop();
delete receiver_;
delete emulXcvr_;
delete capturer_;
delete transmitter_;
@ -174,17 +174,17 @@ void PcapPort::updateNotes()
void PcapPort::startDeviceEmulation()
{
receiver_->start();
emulXcvr_->start();
}
void PcapPort::stopDeviceEmulation()
{
receiver_->stop();
emulXcvr_->stop();
}
int PcapPort::sendEmulationPacket(PacketBuffer *pktBuf)
{
return receiver_->transmitPacket(pktBuf);
return emulXcvr_->transmitPacket(pktBuf);
}
@ -895,10 +895,10 @@ QFile* PcapPort::PortCapturer::captureFile()
/*
* ------------------------------------------------------------------- *
* Port Receiver
* Transmit+Receiver for Device/ProtocolEmulation
* ------------------------------------------------------------------- *
*/
PcapPort::PortReceiver::PortReceiver(const char *device,
PcapPort::EmulationTransceiver::EmulationTransceiver(const char *device,
DeviceManager *deviceManager)
{
device_ = QString::fromAscii(device);
@ -908,12 +908,12 @@ PcapPort::PortReceiver::PortReceiver(const char *device,
handle_ = NULL;
}
PcapPort::PortReceiver::~PortReceiver()
PcapPort::EmulationTransceiver::~EmulationTransceiver()
{
stop();
}
void PcapPort::PortReceiver::run()
void PcapPort::EmulationTransceiver::run()
{
int flags = PCAP_OPENFLAG_PROMISCUOUS;
char errbuf[PCAP_ERRBUF_SIZE] = "";
@ -942,11 +942,9 @@ _retry:
{
if (flags && QString(errbuf).contains("promiscuous"))
{
// FIXME: warn user that device emulation will not work
qDebug("%s:can't set promiscuous mode, trying non-promisc",
qPrintable(device_));
flags &= ~PCAP_OPENFLAG_PROMISCUOUS;
goto _retry;
notify("Unable to set promiscuous mode on <%s> - "
"device emulation will not work", qPrintable(device_));
goto _exit;
}
#ifdef Q_OS_WIN32
else if ((flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
@ -959,9 +957,8 @@ _retry:
#endif
else
{
// FIXME: warn user that device emulation will not work
qDebug("%s: Error opening port %s: %s\n", __FUNCTION__,
device_.toAscii().constData(), errbuf);
notify("Unable to open <%s> [%s] - device emulation will not work",
qPrintable(device_), errbuf);
goto _exit;
}
}
@ -1031,9 +1028,8 @@ _exit:
state_ = kFinished;
}
void PcapPort::PortReceiver::start()
void PcapPort::EmulationTransceiver::start()
{
// FIXME: return error
if (state_ == kRunning) {
qWarning("Receive start requested but is already running!");
return;
@ -1046,7 +1042,7 @@ void PcapPort::PortReceiver::start()
QThread::msleep(10);
}
void PcapPort::PortReceiver::stop()
void PcapPort::EmulationTransceiver::stop()
{
if (state_ == kRunning) {
stop_ = true;
@ -1054,18 +1050,17 @@ void PcapPort::PortReceiver::stop()
QThread::msleep(10);
}
else {
// FIXME: return error
qWarning("Receive stop requested but is not running!");
return;
}
}
bool PcapPort::PortReceiver::isRunning()
bool PcapPort::EmulationTransceiver::isRunning()
{
return (state_ == kRunning);
}
int PcapPort::PortReceiver::transmitPacket(PacketBuffer *pktBuf)
int PcapPort::EmulationTransceiver::transmitPacket(PacketBuffer *pktBuf)
{
return pcap_sendpacket(handle_, pktBuf->data(), pktBuf->length());
}

View File

@ -225,12 +225,11 @@ protected:
volatile State state_;
};
// FIXME: rename? not just a 'receiver' but also 'transmitter'!
class PortReceiver: public QThread
class EmulationTransceiver: public QThread
{
public:
PortReceiver(const char *device, DeviceManager *deviceManager);
~PortReceiver();
EmulationTransceiver(const char *device, DeviceManager *deviceManager);
~EmulationTransceiver();
void run();
void start();
void stop();
@ -260,7 +259,7 @@ protected:
private:
PortTransmitter *transmitter_;
PortCapturer *capturer_;
PortReceiver *receiver_;
EmulationTransceiver *emulXcvr_;
static pcap_if_t *deviceList_;
};