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 PacketBuffer;
class QIODevice; class QIODevice;
// TODO: send notification back to client(s)
#define notify qWarning
class AbstractPort class AbstractPort
{ {
public: public:

View File

@ -32,7 +32,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
const quint64 kBcastMac = 0xffffffffffffULL; 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) DeviceManager::DeviceManager(AbstractPort *parent)
{ {
@ -137,8 +138,6 @@ int DeviceManager::deviceCount()
void DeviceManager::getDeviceList( void DeviceManager::getDeviceList(
OstProto::PortDeviceList *deviceList) OstProto::PortDeviceList *deviceList)
{ {
int count = 0;
foreach(Device *device, deviceList_) { foreach(Device *device, deviceList_) {
OstEmul::Device *dev = OstEmul::Device *dev =
deviceList->AddExtension(OstEmul::port_device); deviceList->AddExtension(OstEmul::port_device);
@ -160,7 +159,11 @@ void DeviceManager::receivePacket(PacketBuffer *pktBuf)
// We assume pkt is ethernet // We assume pkt is ethernet
// TODO: extend for other link layer types // 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 // Extract dstMac
dstMac = qFromBigEndian<quint32>(pktData + offset); dstMac = qFromBigEndian<quint32>(pktData + offset);

View File

@ -602,9 +602,12 @@ _invalid_version:
/* /*
* =================================================================== * ===================================================================
* Device Emulation * Device Emulation
* FIXME: Locking for these functions is at Port level, should it be * ===================================================================
* moved to inside DeviceManager instead? In other words, are * XXX: Streams and Devices are largely non-overlapping from a RPC
* streams/ports and devices independent? * 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( void MyService::getDeviceGroupIdList(
@ -699,10 +702,8 @@ void MyService::addDeviceGroup(
devMgr = portInfo[portId]->deviceManager(); devMgr = portInfo[portId]->deviceManager();
#if 0 // FIXME: needed?
if (portInfo[portId]->isTransmitOn()) if (portInfo[portId]->isTransmitOn())
goto _port_busy; goto _port_busy;
#endif
portLock[portId]->lockForWrite(); portLock[portId]->lockForWrite();
for (int i = 0; i < request->device_group_id_size(); i++) for (int i = 0; i < request->device_group_id_size(); i++)
@ -723,11 +724,9 @@ void MyService::addDeviceGroup(
done->Run(); done->Run();
return; return;
#if 0 // FIXME: needed?
_port_busy: _port_busy:
controller->SetFailed("Port Busy"); controller->SetFailed("Port Busy");
goto _exit; goto _exit;
#endif
_invalid_port: _invalid_port:
controller->SetFailed("invalid portid"); controller->SetFailed("invalid portid");
@ -752,10 +751,8 @@ void MyService::deleteDeviceGroup(
devMgr = portInfo[portId]->deviceManager(); devMgr = portInfo[portId]->deviceManager();
#if 0 // FIXME: needed?
if (portInfo[portId]->isTransmitOn()) if (portInfo[portId]->isTransmitOn())
goto _port_busy; goto _port_busy;
#endif
portLock[portId]->lockForWrite(); portLock[portId]->lockForWrite();
for (int i = 0; i < request->device_group_id_size(); i++) for (int i = 0; i < request->device_group_id_size(); i++)
@ -767,11 +764,9 @@ void MyService::deleteDeviceGroup(
done->Run(); done->Run();
return; return;
#if 0 // FIXME: needed?
_port_busy: _port_busy:
controller->SetFailed("Port Busy"); controller->SetFailed("Port Busy");
goto _exit; goto _exit;
#endif
_invalid_port: _invalid_port:
controller->SetFailed("invalid portid"); controller->SetFailed("invalid portid");
_exit: _exit:
@ -795,28 +790,22 @@ void MyService::modifyDeviceGroup(
devMgr = portInfo[portId]->deviceManager(); devMgr = portInfo[portId]->deviceManager();
#if 0 // FIXME: needed?
if (portInfo[portId]->isTransmitOn()) if (portInfo[portId]->isTransmitOn())
goto _port_busy; goto _port_busy;
#endif
portLock[portId]->lockForWrite(); portLock[portId]->lockForWrite();
for (int i = 0; i < request->device_group_size(); i++) for (int i = 0; i < request->device_group_size(); i++)
devMgr->modifyDeviceGroup(&request->device_group(i)); devMgr->modifyDeviceGroup(&request->device_group(i));
portLock[portId]->unlock(); portLock[portId]->unlock();
// FIXME: check for overlaps between devices?
//! \todo(LOW): fill-in response "Ack"???? //! \todo(LOW): fill-in response "Ack"????
done->Run(); done->Run();
return; return;
#if 0 // FIXME: needed?
_port_busy: _port_busy:
controller->SetFailed("Port Busy"); controller->SetFailed("Port Busy");
goto _exit; goto _exit;
#endif
_invalid_port: _invalid_port:
controller->SetFailed("invalid portid"); controller->SetFailed("invalid portid");
_exit: _exit:

View File

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

View File

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