| 1 | /* |
| 2 | * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <aws/core/utils/DNS.h> |
| 17 | #include <aws/core/utils/StringUtils.h> |
| 18 | |
| 19 | namespace Aws |
| 20 | { |
| 21 | namespace Utils |
| 22 | { |
| 23 | bool IsValidDnsLabel(const Aws::String& label) |
| 24 | { |
| 25 | // Valid DNS hostnames are composed of valid DNS labels separated by a period. |
| 26 | // Valid DNS labels are characterized by the following: |
| 27 | // 1- Only contains alphanumeric characters and/or dashes |
| 28 | // 2- Cannot start or end with a dash |
| 29 | // 3- Have a maximum length of 63 characters (the entirety of the domain name should be less than 255 bytes) |
| 30 | |
| 31 | if (label.empty()) |
| 32 | return false; |
| 33 | |
| 34 | if (label.size() > 63) |
| 35 | return false; |
| 36 | |
| 37 | if (!StringUtils::IsAlnum(label.front())) |
| 38 | return false; // '-' is not acceptable as the first character |
| 39 | |
| 40 | if (!StringUtils::IsAlnum(label.back())) |
| 41 | return false; // '-' is not acceptable as the last character |
| 42 | |
| 43 | for (size_t i = 1, e = label.size() - 1; i < e; ++i) |
| 44 | { |
| 45 | auto c = label[i]; |
| 46 | if (c != '-' && !StringUtils::IsAlnum(c)) |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool IsValidHost(const Aws::String& host) |
| 54 | { |
| 55 | // Valid DNS hostnames are composed of valid DNS labels separated by a period. |
| 56 | auto labels = StringUtils::Split(host, '.'); |
| 57 | if (labels.empty()) |
| 58 | { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return !std::any_of(labels.begin(), labels.end(), [](const Aws::String& label){ return !IsValidDnsLabel(label); }); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |