blob: 73469cb81b87a37284b210f6ad94c93bbe4d715b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#include <stdexcept>
#include "port_number.hpp"
#include "logger/logger_set.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;
static const std::string METHOD_NAME = "Check settings";
//TODO общее использование полей
auto find = line.find( PORT_FIELD );
if( find == line.end() )
{
logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "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 )
{
logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Port doesn't entered by numbers" );
return false;
}
catch( std::out_of_range const& ex )
{
logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Port out of range" );
return false;
}
catch( ... )
{
logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Port doesn't entered by numbers" );
return false;
}
return host_as_int >= MIN_PORT_NUMBER && host_as_int <= MAX_PORT_NUMBER;
}
}
|