From 38ff401f11addce463ddc4035f7a9305edd3adc0 Mon Sep 17 00:00:00 2001 From: Luke De Mouy Date: Mon, 11 Jan 2016 14:17:02 -0700 Subject: [PATCH] Copy the functionality from the original utility into the Firewall class, in particular adds in the following functionality: Start, Stop, and Restart the firewall, and see if it's running Open or Close a port, and get a list of the ports that are currently open. UI code was removed, and functions were modified as appropriate. --- src/library/sysadm-firewall.cpp | 94 ++++++++++++++++++++++++++++++++- src/library/sysadm-firewall.h | 54 ++++++++++++++++++- 2 files changed, 146 insertions(+), 2 deletions(-) diff --git a/src/library/sysadm-firewall.cpp b/src/library/sysadm-firewall.cpp index 3efd387..ce33cb6 100644 --- a/src/library/sysadm-firewall.cpp +++ b/src/library/sysadm-firewall.cpp @@ -70,9 +70,63 @@ PortInfo Firewall::LookUpPort(int portNumber, QString portType) } -Firewall::Firewall() +void Firewall::OpenPort(int port, QString type) { + openports << QString::number(port)+"::::"+type; + SaveOpenPorts(); +} + +void Firewall::ClosePort(int port, QString type) +{ + openports.removeAll( QString::number(port)+"::::"+type); + SaveOpenPorts(); +} + +QVector Firewall::OpenPorts() +{ + QVector returnValue = QVector(openports.length()); + + for(int i=0; iclose(); delete services; } + +void Firewall::LoadOpenPorts() +{ + openports.clear(); + QFile file("/etc/ipfw.openports"); + if( file.open(QIODevice::ReadOnly) ){ + QTextStream in(&file); + while( !in.atEnd() ){ + QString line = in.readLine(); + if(line.startsWith("#") || line.simplified().isEmpty()){ continue; } + //File format: " " (nice and simple) + openports << line.section(" ",1,1)+"::::"+line.section(" ",0,0); + } + file.close(); + } + openports.sort(); //order them in ascending port order +} + +void Firewall::SaveOpenPorts() +{ + //Convert to file format + openports.sort(); //make sure they are still sorted by port + QStringList fileout; + for(int i=0; i OpenPorts(); + + ///#endsection + + ///#section: firewall commands + /** + * @brief Checks to see if the firewall is running + * @return true if the firewall is running, false if not + */ + bool IsRunning(); + /** + * @brief Starts the firewall + */ + void Start(); + /** + * @brief Stops the firewall + */ + void Stop(); + /** + * @brief Restarts the firewall + */ + void Restart(); + ///#endsection + + ///#section: ctors dtors Firewall(); ~Firewall(); + ///#endsection private: void readServicesFile(); QStringList* portStrings; + + QStringList openports; + + void LoadOpenPorts(); + void SaveOpenPorts(); }; } #endif // PORTLOOKUP_H