Implemented drone application settings - .ini format on all platforms; settings read at startup, not saved back. In other words, user needs to hand write settings in the .ini file. Currently, the only setting implemented is a portlist filter as include/exclude lists of patterns

This commit is contained in:
Srivats P. 2014-12-31 20:16:30 +05:30
parent 1bc4efe48b
commit 8369c18b35
4 changed files with 116 additions and 7 deletions

View File

@ -20,10 +20,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "drone.h"
#include "../common/protocolmanager.h"
#include "settings.h"
#include <google/protobuf/stubs/common.h>
#include <QCoreApplication>
#include <QFile>
#ifdef Q_OS_UNIX
#include <signal.h>
@ -33,6 +35,7 @@ extern ProtocolManager *OstProtocolManager;
extern char *version;
extern char *revision;
QSettings *appSettings;
int myport;
void cleanup(int /*signum*/)
@ -44,10 +47,7 @@ int main(int argc, char *argv[])
{
int exitCode = 0;
QCoreApplication app(argc, argv);
Drone *drone = new Drone();
OstProtocolManager = new ProtocolManager();
app.setApplicationName(drone->objectName());
Drone *drone;
// TODO: command line options
// -v (--version)
@ -56,6 +56,28 @@ int main(int argc, char *argv[])
if (argc > 1)
myport = atoi(argv[1]);
app.setApplicationName("Drone");
app.setOrganizationName("Ostinato");
/* (Portable Mode) If we have a .ini file in the same directory as the
executable, we use that instead of the platform specific location
and format for the settings */
QString portableIni = QCoreApplication::applicationDirPath()
+ "/drone.ini";
if (QFile::exists(portableIni))
appSettings = new QSettings(portableIni, QSettings::IniFormat);
else
appSettings = new QSettings(QSettings::IniFormat,
QSettings::UserScope,
app.organizationName(),
app.applicationName().toLower());
if (QFile::exists(appSettings->fileName()))
qDebug("Read settings from %s", qPrintable(appSettings->fileName()));
drone = new Drone();
OstProtocolManager = new ProtocolManager();
if (!drone->init())
{
exitCode = -1;

View File

@ -19,14 +19,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "portmanager.h"
#include <QtGlobal>
#include <pcap.h>
#include "bsdport.h"
#include "linuxport.h"
#include "pcapport.h"
#include "settings.h"
#include "winpcapport.h"
#include <QStringList>
#include <pcap.h>
PortManager *PortManager::instance_ = NULL;
PortManager::PortManager()
@ -49,6 +50,18 @@ PortManager::PortManager()
if (device->description)
qDebug(" (%s)\n", device->description);
#if defined(Q_OS_WIN32)
if (!filterAcceptsPort(device->description))
#else
if (!filterAcceptsPort(device->name))
#endif
{
qDebug("%s (%s) rejected by filter. Skipping!",
device->name, device->description);
i--;
continue;
}
#if defined(Q_OS_WIN32)
port = new WinPcapPort(i, device->name);
#elif defined(Q_OS_LINUX)
@ -92,3 +105,40 @@ PortManager* PortManager::instance()
return instance_;
}
bool PortManager::filterAcceptsPort(const char *name)
{
QRegExp pattern;
QStringList includeList = appSettings->value(kPortListIncludeKey)
.toStringList();
QStringList excludeList = appSettings->value(kPortListExcludeKey)
.toStringList();
pattern.setPatternSyntax(QRegExp::Wildcard);
// An empty (or missing) includeList accepts all ports
// NOTE: A blank "IncludeList=" is read as a stringlist with one
// string which is empty => treat it same as an empty stringlist
if (includeList.isEmpty()
|| (includeList.size() == 1 && includeList.at(0).isEmpty()))
goto _include_pass;
foreach (QString str, includeList) {
pattern.setPattern(str);
if (pattern.exactMatch(name))
goto _include_pass;
}
// IncludeList is not empty and port did not match a pattern
return false;
_include_pass:
foreach (QString str, excludeList) {
pattern.setPattern(str);
if (pattern.exactMatch(name))
return false;
}
// Port did not match a pattern in ExcludeList
return true;
}

View File

@ -34,6 +34,9 @@ public:
static PortManager* instance();
private:
bool filterAcceptsPort(const char *name);
private:
QList<AbstractPort*> portList_;

34
server/settings.h Normal file
View File

@ -0,0 +1,34 @@
/*
Copyright (C) 2014 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 _SETTINGS_H
#define _SETTINGS_H
#include <QSettings>
#include <QString>
extern QSettings *appSettings;
//
// PortList Section Keys
//
const QString kPortListIncludeKey("PortList/Include");
const QString kPortListExcludeKey("PortList/Exclude");
#endif