add tryCreateNetwork()
[folly.git] / folly / IPAddressException.h
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
18 #include <exception>
19 #include <string>
20 #include <utility>
21
22 #include <folly/CPortability.h>
23 #include <folly/detail/IPAddress.h>
24
25 namespace folly {
26
27 /**
28  * Error codes for non-throwing interface of IPAddress family of functions.
29  */
30 enum class IPAddressFormatError { INVALID_IP, UNSUPPORTED_ADDR_FAMILY };
31
32 /**
33  * Wraps error from parsing IP/MASK string
34  */
35 enum class CIDRNetworkError {
36   INVALID_DEFAULT_CIDR,
37   INVALID_IP_SLASH_CIDR,
38   INVALID_IP,
39   INVALID_CIDR,
40   CIDR_MISMATCH,
41 };
42
43 /**
44  * Exception for invalid IP addresses.
45  */
46 class FOLLY_EXPORT IPAddressFormatException : public std::exception {
47  public:
48   explicit IPAddressFormatException(std::string msg) noexcept
49       : msg_(std::move(msg)) {}
50   IPAddressFormatException(const IPAddressFormatException&) = default;
51   IPAddressFormatException(IPAddressFormatException&&) = default;
52   IPAddressFormatException& operator=(const IPAddressFormatException&) =
53       default;
54   IPAddressFormatException& operator=(IPAddressFormatException&&) = default;
55
56   ~IPAddressFormatException() noexcept override {}
57   const char* what() const noexcept override {
58     return msg_.c_str();
59   }
60
61  private:
62   std::string msg_;
63 };
64
65 class FOLLY_EXPORT InvalidAddressFamilyException
66     : public IPAddressFormatException {
67  public:
68   explicit InvalidAddressFamilyException(std::string msg) noexcept
69       : IPAddressFormatException(std::move(msg)) {}
70   explicit InvalidAddressFamilyException(sa_family_t family) noexcept
71       : InvalidAddressFamilyException(
72             "Address family " + detail::familyNameStr(family) +
73             " is not AF_INET or AF_INET6") {}
74   InvalidAddressFamilyException(const InvalidAddressFamilyException&) = default;
75   InvalidAddressFamilyException(InvalidAddressFamilyException&&) = default;
76   InvalidAddressFamilyException& operator=(
77       const InvalidAddressFamilyException&) = default;
78   InvalidAddressFamilyException& operator=(InvalidAddressFamilyException&&) =
79       default;
80 };
81
82 } // namespace folly