IPAddressException.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  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. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <exception>
  18. #include <string>
  19. #include <utility>
  20. #include <folly/CPortability.h>
  21. #include <folly/detail/IPAddress.h>
  22. namespace folly {
  23. /**
  24. * Error codes for non-throwing interface of IPAddress family of functions.
  25. */
  26. enum class IPAddressFormatError { INVALID_IP, UNSUPPORTED_ADDR_FAMILY };
  27. /**
  28. * Wraps error from parsing IP/MASK string
  29. */
  30. enum class CIDRNetworkError {
  31. INVALID_DEFAULT_CIDR,
  32. INVALID_IP_SLASH_CIDR,
  33. INVALID_IP,
  34. INVALID_CIDR,
  35. CIDR_MISMATCH,
  36. };
  37. /**
  38. * Exception for invalid IP addresses.
  39. */
  40. class FOLLY_EXPORT IPAddressFormatException : public std::exception {
  41. public:
  42. explicit IPAddressFormatException(std::string msg) noexcept
  43. : msg_(std::move(msg)) {}
  44. IPAddressFormatException(const IPAddressFormatException&) = default;
  45. IPAddressFormatException(IPAddressFormatException&&) = default;
  46. IPAddressFormatException& operator=(const IPAddressFormatException&) =
  47. default;
  48. IPAddressFormatException& operator=(IPAddressFormatException&&) = default;
  49. ~IPAddressFormatException() noexcept override {}
  50. const char* what() const noexcept override {
  51. return msg_.c_str();
  52. }
  53. private:
  54. std::string msg_;
  55. };
  56. class FOLLY_EXPORT InvalidAddressFamilyException
  57. : public IPAddressFormatException {
  58. public:
  59. explicit InvalidAddressFamilyException(std::string msg) noexcept
  60. : IPAddressFormatException(std::move(msg)) {}
  61. explicit InvalidAddressFamilyException(sa_family_t family) noexcept
  62. : InvalidAddressFamilyException(
  63. "Address family " + detail::familyNameStr(family) +
  64. " is not AF_INET or AF_INET6") {}
  65. InvalidAddressFamilyException(const InvalidAddressFamilyException&) = default;
  66. InvalidAddressFamilyException(InvalidAddressFamilyException&&) = default;
  67. InvalidAddressFamilyException& operator=(
  68. const InvalidAddressFamilyException&) = default;
  69. InvalidAddressFamilyException& operator=(InvalidAddressFamilyException&&) =
  70. default;
  71. };
  72. } // namespace folly