summaryrefslogtreecommitdiff
path: root/src/checker/errors/settings/port_number.cpp
blob: b0d96612d152e9214a6bbd081485453e58764d4a (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
50
51
52
53
54
#include <stdexcept>

#include "port_number.hpp"
#include "logger/logger_set.hpp"

namespace smtp::checker::errors::settings
{
    bool PortNumber::Check( manage::SettingsFileDataType const& 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;
		}
        auto result = host_as_int >= MIN_PORT_NUMBER && host_as_int <= MAX_PORT_NUMBER;
        if( !result )
        {
            logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port doesn't entered by numbers" );
        }
        return result;
	}
}