aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/DNS.cpp
blob: ce588150e2fe691f083b3e4218aaf45e4c256513 (plain) (blame)
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
55
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/core/utils/DNS.h>
#include <aws/core/utils/StringUtils.h>

namespace Aws
{
    namespace Utils
    {
        bool IsValidDnsLabel(const Aws::String& label)
        {
            // Valid DNS hostnames are composed of valid DNS labels separated by a period.
            // Valid DNS labels are characterized by the following:
            // 1- Only contains alphanumeric characters and/or dashes
            // 2- Cannot start or end with a dash
            // 3- Have a maximum length of 63 characters (the entirety of the domain name should be less than 255 bytes)

            if (label.empty())
                return false;

            if (label.size() > 63)
                return false;

            if (!StringUtils::IsAlnum(label.front()))
                return false; // '-' is not acceptable as the first character

            if (!StringUtils::IsAlnum(label.back()))
                return false; // '-' is not acceptable as the last  character

            for (size_t i = 1, e = label.size() - 1; i < e; ++i)
            {
                auto c = label[i];
                if (c != '-' && !StringUtils::IsAlnum(c))
                    return false;
            }

            return true;
        }

        bool IsValidHost(const Aws::String& host)
        {
            // Valid DNS hostnames are composed of valid DNS labels separated by a period.
            auto labels = StringUtils::Split(host, '.');
            if (labels.empty()) 
            {
                return false;
            }

            return !std::any_of(labels.begin(), labels.end(), [](const Aws::String& label){ return !IsValidDnsLabel(label); });
        }
    }
}