DNS.cpp 1.8 KB

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