From d9aa2e43a017d8c65d152dd05076df1e57aa78c9 Mon Sep 17 00:00:00 2001 From: "Srivats P." Date: Sat, 17 Jan 2009 10:13:46 +0000 Subject: [PATCH] Changes for successful compilation in Linux. PCAP/Winpcap functions changed to use those which are common on both PCAP and WinPCAP. Some additional WinPCAP only functions (such as the pcap_sendqueue_xxx) which we intend to use have been added into pcapextra.c which will be used in case of Linux --- client/dumpview.h | 2 +- client/ostinato.pro | 4 +-- client/portgrouplist.h | 2 +- rpc/pbhelper.h | 2 +- server/drone.pro | 14 ++++---- server/myservice.cpp | 75 ++++++++++++++++++++++++++++++++++++------ server/myservice.h | 7 ++-- 7 files changed, 82 insertions(+), 24 deletions(-) diff --git a/client/dumpview.h b/client/dumpview.h index cdbcde7..e362103 100644 --- a/client/dumpview.h +++ b/client/dumpview.h @@ -26,7 +26,7 @@ protected slots: void paintEvent(QPaintEvent *event); private: - void DumpView::populateDump(QByteArray &dump, int &selOfs, int &selSize, + void populateDump(QByteArray &dump, int &selOfs, int &selSize, QModelIndex parent = QModelIndex()); bool inline isPrintable(char c) {if ((c > 48) && (c < 126)) return true; else return false; } diff --git a/client/ostinato.pro b/client/ostinato.pro index 4b1cb69..7e6c89e 100644 --- a/client/ostinato.pro +++ b/client/ostinato.pro @@ -1,8 +1,8 @@ TEMPLATE = app CONFIG += qt debug QT += network -INCLUDEPATH += "c:\msys\1.0\local\include" "..\rpc\" -LIBS += -L"c:\msys\1.0\local\lib" -lprotobuf -L"..\rpc\debug" -lpbrpc +INCLUDEPATH += "../rpc/" +LIBS += -lprotobuf -L"../rpc/" -lpbrpc RESOURCES += ostinato.qrc HEADERS += \ dumpview.h \ diff --git a/client/portgrouplist.h b/client/portgrouplist.h index aadd24d..89256eb 100644 --- a/client/portgrouplist.h +++ b/client/portgrouplist.h @@ -26,7 +26,7 @@ class PortGroupList : public QObject { // Methods public: - PortGroupList::PortGroupList(); + PortGroupList(); PortModel* getPortModel() { return &mPortGroupListModel; } diff --git a/rpc/pbhelper.h b/rpc/pbhelper.h index 21afefa..e3d85b0 100644 --- a/rpc/pbhelper.h +++ b/rpc/pbhelper.h @@ -4,7 +4,7 @@ #include #include -#include +#include class PbHelper { diff --git a/server/drone.pro b/server/drone.pro index 3642823..315a0e2 100644 --- a/server/drone.pro +++ b/server/drone.pro @@ -1,15 +1,17 @@ TEMPLATE = app -CONFIG += qt +CONFIG += qt debug QT += network DEFINES += HAVE_REMOTE WPCAP -INCLUDEPATH += "c:\msys\1.0\local\include" -INCLUDEPATH += "C:\DevelLibs\WpdPack\Include" -INCLUDEPATH += "..\rpc" -LIBS += -L"C:\DevelLibs\WpdPack\Lib" -lwpcap -lpacket -LIBS += -L"..\rpc\debug" -lpbrpc +INCLUDEPATH += "../rpc" +win32:LIBS += -lwpcap -lpacket +unix:LIBS += -lpcap +win32:LIBS += -L"../rpc/debug" -lpbrpc +unix:LIBS += -L"../rpc" -lpbrpc HEADERS += drone.h FORMS += drone.ui SOURCES += drone_main.cpp drone.cpp SOURCES += myservice.cpp +unix:SOURCES += pcapextra.cpp + SOURCES += "..\common\protocol.pb.cc" diff --git a/server/myservice.cpp b/server/myservice.cpp index 3296a86..5f69adc 100644 --- a/server/myservice.cpp +++ b/server/myservice.cpp @@ -487,8 +487,8 @@ PortInfo::PortInfo(uint id, pcap_if_t *dev) char errbuf[PCAP_ERRBUF_SIZE]; this->dev = dev; - devHandle = pcap_open(dev->name, 65536, PCAP_OPENFLAG_PROMISCUOUS , 1000, - NULL, errbuf); + devHandle = pcap_open_live(dev->name, 0, PCAP_OPENFLAG_PROMISCUOUS , + 1000 /*ms*/, errbuf); if (devHandle == NULL) { qDebug("Error opening port %s: %s\n", @@ -509,8 +509,16 @@ PortInfo::PortInfo(uint id, pcap_if_t *dev) } d.mutable_port_id()->set_id(id); - d.set_name(QString("eth%1").arg(id).toAscii().constData()); - d.set_description(dev->description); +#ifdef Q_OS_WIN32 + d.set_name(QString("if%1").arg(id).toAscii().constData()); +#else + if (dev->name) + d.set_name(dev->name); + else + d.set_name(QString("if%1").arg(id).toAscii().constData()); +#endif + if (dev->description) + d.set_description(dev->description); d.set_is_enabled(true); // FIXME(MED):check d.set_is_oper_up(true); // FIXME(MED):check d.set_is_exclusive_control(false); // FIXME(MED): check @@ -635,9 +643,18 @@ void PortInfo::startTransmit() // - adjust for it if (bytes) bytes -= pkts * sizeof(pcap_pkthdr); - +#ifdef Q_OS_WIN32 + // Update pcapExtra counters - port TxStats will be updated in the + // 'stats callback' function so that both Rx and Tx stats are updated + // together pcapExtra.txPkts += pkts; pcapExtra.txBytes += bytes; +#else + // We don't have a regular stats callback function here, so update + // Port TxStats directly here + stats.txPkts += pkts; + stats.txBytes += bytes; +#endif } void PortInfo::stopTransmit() @@ -673,9 +690,12 @@ PortInfo::PortMonitor::PortMonitor(PortInfo *port) #endif } +#ifdef Q_OS_WIN32 void PortInfo::PortMonitor::callback(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data) { + // This is the WinPcap Callback - which is a 'stats mode' callback + uint usec; PortInfo *port = (PortInfo*) state; @@ -723,7 +743,7 @@ void PortInfo::PortMonitor::callback(u_char *state, port->monitor.oidData->Oid = OID_GEN_RCV_OK; if (PacketRequest(port->devHandle->adapter, 0, port->monitor.oidData)) { - if (port->monitor.oidData->Length <= sizeof(port->stats.txPkts)) + if (port->monitor.oidData->Length <= sizeof(port->stats.rxPktsNic)) memcpy((void*)&port->stats.rxPktsNic, (void*)port->monitor.oidData->Data, port->monitor.oidData->Length); @@ -731,14 +751,50 @@ void PortInfo::PortMonitor::callback(u_char *state, port->monitor.oidData->Oid = OID_GEN_XMIT_OK; if (PacketRequest(port->devHandle->adapter, 0, port->monitor.oidData)) { - if (port->monitor.oidData->Length <= sizeof(port->stats.txPkts)) + if (port->monitor.oidData->Length <= sizeof(port->stats.txPktsNic)) memcpy((void*)&port->stats.txPktsNic, (void*)port->monitor.oidData->Data, port->monitor.oidData->Length); } #endif } +#else +void PortInfo::PortMonitor::callback(u_char *state, + const struct pcap_pkthdr *header, const u_char *pkt_data) +{ + // This is the LibPcap Callback - which is a 'capture mode' callback + // This callback is called once for EVERY packet + uint usec; + PortInfo *port = (PortInfo*) state; + + quint64 pkts; + quint64 bytes; + + // Update RxStats and RxRates using PCAP data + usec = (header->ts.tv_sec - port->lastTs.tv_sec) * 1000000 + + (header->ts.tv_usec - port->lastTs.tv_usec); + // TODO(rate) +#if 0 + port->stats.rxPps = (pkts * 1000000) / usec; + port->stats.rxBps = (bytes * 1000000) / usec; +#endif + + // Note: For a 'capture callback' PCAP reported bytes DOES NOT include + // ETH_FRAME_HDR_SIZE - so don't adjust for it + port->stats.rxPkts++; + port->stats.rxBytes += header->len; + + // NOTE: Port TxStats are updated by Port Transmit Function itself + // since this callback is called only when a packet is received + + //! \TODO TxRates + + // Store curr timestamp as last timestamp + port->lastTs.tv_sec = header->ts.tv_sec; + port->lastTs.tv_usec = header->ts.tv_usec; +} +#endif void PortInfo::PortMonitor::run() { int ret; @@ -747,8 +803,7 @@ void PortInfo::PortMonitor::run() /* Start the main loop */ ret = pcap_loop(port->devHandle, -1, &PortInfo::PortMonitor::callback, - (PUCHAR) port); - //ret = pcap_loop(fp, -1, &updateStats, (PUCHAR)&st_ts); + (u_char*) port); switch(ret) { @@ -802,7 +857,7 @@ MyService::MyService(AbstractHost *host) alldevs = NULL; LOG("Retrieving the device list from the local machine\n"); - if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1) + if (pcap_findalldevs(&alldevs, errbuf) == -1) { LOG("Error in pcap_findalldevs_ex: %s\n", errbuf); goto _fail; diff --git a/server/myservice.h b/server/myservice.h index a14524d..c0679fb 100644 --- a/server/myservice.h +++ b/server/myservice.h @@ -15,7 +15,8 @@ #include #include "../rpc/pbhelper.h" - +#include "pcapextra.h" + #ifdef Q_OS_WIN32 #include #endif @@ -41,7 +42,7 @@ class StreamInfo quint8 protocol, quint16 len); quint32 ipv4CksumPartial(uchar *buf, int len); quint16 ipv4Cksum(uchar *buf, int len, quint32 partialSum = 0); - int StreamInfo::makePacket(uchar *buf, int bufMaxSize, int n); + int makePacket(uchar *buf, int bufMaxSize, int n); public: bool operator < (const StreamInfo &s) const { return(d.core().ordinal() < s.d.core().ordinal()); } @@ -117,7 +118,7 @@ class PortInfo QList streamList; public: - PortInfo::PortInfo(uint id, pcap_if_t *dev); + PortInfo(uint id, pcap_if_t *dev); uint id() { return d.port_id().id(); } bool isDirty() { return isSendQueueDirty; } void setDirty(bool dirty) { isSendQueueDirty = dirty; }