1 /** 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * SPDX-License-Identifier: Apache-2.0. 4 */ 5 6 #include <aws/core/utils/DNS.h> 7 #include <aws/core/utils/StringUtils.h> 8 9 namespace Aws 10 { 11 namespace Utils 12 { IsValidDnsLabel(const Aws::String & label)13 bool IsValidDnsLabel(const Aws::String& label) 14 { 15 // Valid DNS hostnames are composed of valid DNS labels separated by a period. 16 // Valid DNS labels are characterized by the following: 17 // 1- Only contains alphanumeric characters and/or dashes 18 // 2- Cannot start or end with a dash 19 // 3- Have a maximum length of 63 characters (the entirety of the domain name should be less than 255 bytes) 20 21 if (label.empty()) 22 return false; 23 24 if (label.size() > 63) 25 return false; 26 27 if (!StringUtils::IsAlnum(label.front())) 28 return false; // '-' is not acceptable as the first character 29 30 if (!StringUtils::IsAlnum(label.back())) 31 return false; // '-' is not acceptable as the last character 32 33 for (size_t i = 1, e = label.size() - 1; i < e; ++i) 34 { 35 auto c = label[i]; 36 if (c != '-' && !StringUtils::IsAlnum(c)) 37 return false; 38 } 39 40 return true; 41 } 42 IsValidHost(const Aws::String & host)43 bool IsValidHost(const Aws::String& host) 44 { 45 // Valid DNS hostnames are composed of valid DNS labels separated by a period. 46 auto labels = StringUtils::Split(host, '.'); 47 if (labels.empty()) 48 { 49 return false; 50 } 51 52 return !std::any_of(labels.begin(), labels.end(), [](const Aws::String& label){ return !IsValidDnsLabel(label); }); 53 } 54 } 55 } 56