diff --git a/server/drone_main.cpp b/server/drone_main.cpp index 726ecc4..01bfea6 100644 --- a/server/drone_main.cpp +++ b/server/drone_main.cpp @@ -20,10 +20,12 @@ along with this program. If not, see #include "drone.h" #include "../common/protocolmanager.h" +#include "settings.h" #include #include +#include #ifdef Q_OS_UNIX #include @@ -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; diff --git a/server/portmanager.cpp b/server/portmanager.cpp index 2981464..897ad4f 100644 --- a/server/portmanager.cpp +++ b/server/portmanager.cpp @@ -19,14 +19,15 @@ along with this program. If not, see #include "portmanager.h" -#include -#include - #include "bsdport.h" #include "linuxport.h" #include "pcapport.h" +#include "settings.h" #include "winpcapport.h" +#include +#include + 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; +} diff --git a/server/portmanager.h b/server/portmanager.h index 2407bf2..801fbf1 100644 --- a/server/portmanager.h +++ b/server/portmanager.h @@ -34,6 +34,9 @@ public: static PortManager* instance(); +private: + bool filterAcceptsPort(const char *name); + private: QList portList_; diff --git a/server/settings.h b/server/settings.h new file mode 100644 index 0000000..50a3b03 --- /dev/null +++ b/server/settings.h @@ -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 +*/ + +#ifndef _SETTINGS_H +#define _SETTINGS_H + +#include +#include + +extern QSettings *appSettings; + +// +// PortList Section Keys +// +const QString kPortListIncludeKey("PortList/Include"); +const QString kPortListExcludeKey("PortList/Exclude"); + +#endif