ostinato/server/pcapsession.h
Srivats P 64d1525f50 Fix infinite loop when stopping capture etc.
On some platforms and/or some libpcap verisons, libpcap doesn't support a
timeout which makes interactive stop not possible. So we now use a UNIX
signal to break out. Obviously this works only on *nix platforms - which
includes MacOS. For now the problem is not seen on Windows with WinPCAP,
so we should be fine. May need to revisit when we add Npcap support.

Fixes #215, #234
2019-08-10 13:26:04 +05:30

83 lines
1.8 KiB
C++

/*
Copyright (C) 2019 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/>
*/
#ifndef _PCAP_SESSION_H
#define _PCAP_SESSION_H
#include <QThread>
#include <pcap.h>
#ifdef Q_OS_UNIX
#include <QHash>
#include <pthread.h>
class ThreadId {
public:
ThreadId() {
id_ = pthread_self();
}
ThreadId(pthread_t id) {
id_ = id;
}
pthread_t nativeId() {
return id_;
}
uint hash() const {
QByteArray t((const char*)(&id_), sizeof(id_));
return qHash(t);
}
bool operator==(const ThreadId &other) const {
return (pthread_equal(id_, other.id_) != 0);
}
private:
pthread_t id_;
};
inline uint qHash(const ThreadId &key)
{
return key.hash();
}
class PcapSession: public QThread
{
protected:
void preRun();
void postRun();
void stop(pcap_t *handle);
private:
static void signalBreakHandler(int /*signum*/);
ThreadId thread_;
static QHash<ThreadId, bool> signalSeen_;
};
#else
class PcapSession: public QThread
{
protected:
void preRun() {};
void postRun() {};
void stop(pcap_t *handle) {
qDebug("calling breakloop with handle %p", handle);
pcap_breakloop(handle);
}
};
#endif
#endif