From 37c6b2a7c9caea90cdb9cb7a8f663a3ed5dece7e Mon Sep 17 00:00:00 2001 From: Luke De Mouy Date: Wed, 6 Jan 2016 12:59:00 -0700 Subject: [PATCH] Adds in the LookUpPort function call that returns a struct containing information about a port including it's description and keyword. SSH and the web port are current placeholders for the recommended ports list --- src/library/portlookup.cpp | 73 ++++++++++++++++++++++++++++++++++++++ src/library/portlookup.h | 37 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/library/portlookup.cpp create mode 100644 src/library/portlookup.h diff --git a/src/library/portlookup.cpp b/src/library/portlookup.cpp new file mode 100644 index 0000000..aac5d84 --- /dev/null +++ b/src/library/portlookup.cpp @@ -0,0 +1,73 @@ +#include "portlookup.h" +#include +using namespace sysadm; +PortLookUp::PortInfo PortLookUp::LookUpPort(int portNumber) +{ + //Make sure that the port is valid + if (portNumber < 0 || portNumber > 65535) + { + PortInfo returnValue; + returnValue.Port = -1; + returnValue.Description = "Port out of bounds"; + return returnValue; + } + + PortInfo returnValue; + //the port number is valid so set it + returnValue.Port = portNumber; + + for(int i = 0; i < recommendedPortListSize; i++) + { + if (portNumber == recommendedPorts[i]) + { + returnValue.Recommended = true; + } + } + + + // /etc/services contains a file that lists the various port numbers + // and their descriptions + QFile* services = new QFile("/etc/services"); + services->open(QFile::ReadOnly); + while(!services->atEnd()) + { + QString line = services->readLine(); + + //jump down past the comments + if(line[0] == '#') + continue; + + //remove all of the extraneous whitespace in the line + line = line.simplified(); + //Split across spaces since it's now whitespace delimited + QStringList lineList = line.split(' '); + + //Ports are stored in the file as number/type so take the left hand side + int port = lineList.at(1).split('/').at(0).toInt(); + + //obviously if it's not the port we want, go to the next line + if (port != portNumber) + continue; + + //the keyword associated with the port is the first element in a line + returnValue.Keyword = lineList.at(0); + + //if the size of the list is less than 3 then there is no description + if(lineList.size() > 2) + { + QString description = lineList.at(2); + //String the description back together from the end of the list + for(int i = 3; i < lineList.size(); i++) + { + description += " " + lineList.at(i); + } + returnValue.Description = description; + } + } + services->close(); + + + return returnValue; + +} + diff --git a/src/library/portlookup.h b/src/library/portlookup.h new file mode 100644 index 0000000..4022146 --- /dev/null +++ b/src/library/portlookup.h @@ -0,0 +1,37 @@ +//=========================================== +// PC-BSD source code +// Copyright (c) 2015, PC-BSD Software/iXsystems +// Available under the 3-clause BSD license +// See the LICENSE file for full details + +#ifndef PORTLOOKUP_H +#define PORTLOOKUP_H +#include +namespace sysadm +{ +namespace PortLookUp +{ + static int recommendedPorts[] = {5,22}; + const int recommendedPortListSize = 2; + + struct PortInfo{ + int Port; + QString Keyword; + QString Description; + bool Recommended; + }; + + /** + * @description Returns a structure containing information about the port + * including its port type, keyword, description, and whether it's a + * recommended port + * + * @parameter portNumber a port number between 1 and 2^16 + * + * @ErrorConditions Port Number is set to -1 and a description of the error is stored in the description variable + */ + static PortInfo LookUpPort(int portNumber); +} +} +#endif // PORTLOOKUP_H +