summaryrefslogtreecommitdiff
path: root/src/checker/errors/settings/port_number.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/checker/errors/settings/port_number.cpp')
-rw-r--r--src/checker/errors/settings/port_number.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/checker/errors/settings/port_number.cpp b/src/checker/errors/settings/port_number.cpp
new file mode 100644
index 0000000..b02eec4
--- /dev/null
+++ b/src/checker/errors/settings/port_number.cpp
@@ -0,0 +1,48 @@
+#include <stdexcept>
+
+#include "port_number.hpp"
+#include "management/logger.hpp"
+
+namespace smtp::checker::errors::settings
+{
+ bool PortNumber::Check( const manage::SettingsFileDataType& line ) const
+ {
+ static const std::string PORT_FIELD = "port";
+ static constexpr int MIN_PORT_NUMBER = 0;
+ static constexpr int MAX_PORT_NUMBER = 65535;
+
+ //TODO общее использование полей
+ auto find = line.find( PORT_FIELD );
+ if( find == line.end() )
+ {
+ manage::Logger::LogError( "Port doesn't found" );
+ return false;
+ }
+ auto host_as_string = find->second;
+ if( host_as_string.empty())
+ {
+ return true;
+ }
+ int host_as_int{};
+ try
+ {
+ host_as_int = std::stoi( host_as_string );
+ }
+ catch( std::invalid_argument const& ex )
+ {
+ manage::Logger::LogError( "Port doesn't entered by numbers" );
+ return false;
+ }
+ catch( std::out_of_range const& ex )
+ {
+ manage::Logger::LogError( "Port out of range" );
+ return false;
+ }
+ catch( ... )
+ {
+ manage::Logger::LogError( "Port doesn't entered by numbers" );
+ return false;
+ }
+ return host_as_int >= MIN_PORT_NUMBER && host_as_int <= MAX_PORT_NUMBER;
+ }
+}