2019-08-10 02:46:18 -05:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
{
|
2023-03-23 05:12:41 -05:00
|
|
|
public:
|
|
|
|
QString debugStats();
|
|
|
|
|
2019-08-10 02:46:18 -05:00
|
|
|
protected:
|
2023-03-23 05:12:41 -05:00
|
|
|
bool clearDebugStats();
|
|
|
|
|
2019-08-10 02:46:18 -05:00
|
|
|
void preRun();
|
|
|
|
void postRun();
|
2023-03-23 05:12:41 -05:00
|
|
|
void stop();
|
|
|
|
|
|
|
|
pcap_t *handle_{nullptr};
|
2019-08-10 02:46:18 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void signalBreakHandler(int /*signum*/);
|
|
|
|
|
|
|
|
ThreadId thread_;
|
|
|
|
static QHash<ThreadId, bool> signalSeen_;
|
2023-03-23 05:12:41 -05:00
|
|
|
|
|
|
|
struct pcap_stat lastPcapStats_;
|
2019-08-10 02:46:18 -05:00
|
|
|
};
|
|
|
|
#else
|
|
|
|
class PcapSession: public QThread
|
|
|
|
{
|
2023-03-23 05:12:41 -05:00
|
|
|
public:
|
|
|
|
QString debugStats();
|
|
|
|
|
2019-08-10 02:46:18 -05:00
|
|
|
protected:
|
2023-03-23 05:12:41 -05:00
|
|
|
bool clearDebugStats();
|
|
|
|
|
2019-08-10 02:46:18 -05:00
|
|
|
void preRun() {};
|
|
|
|
void postRun() {};
|
2023-03-23 05:12:41 -05:00
|
|
|
void stop() {
|
|
|
|
qDebug("calling breakloop with handle %p", handle_);
|
|
|
|
pcap_breakloop(handle_);
|
2019-08-10 02:46:18 -05:00
|
|
|
}
|
2023-03-23 05:12:41 -05:00
|
|
|
|
|
|
|
pcap_t *handle_{nullptr};
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct pcap_stat lastPcapStats_;
|
2019-08-10 02:46:18 -05:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|