summaryrefslogtreecommitdiff
path: root/src/checker/errors/settings/port_number.cpp
blob: 485b4c72ecdf19ff30b95b132926cc99b6859790 (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
#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;

		//TODO общее использование полей
		auto find = line.find( PORT_FIELD );
		if( find == line.end() )
		{
            logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "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( GetMethodName(), "Port doesn't entered by numbers" );
			return false;
		}
		catch( std::out_of_range const& ex )
		{
            logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port out of range" );
			return false;
		}
		catch( ... )
		{
            logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port doesn't entered by numbers" );
			return false;
		}
		return host_as_int >= MIN_PORT_NUMBER && host_as_int <= MAX_PORT_NUMBER;
	}
}