1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 
17 #pragma once
18 
19 #include <exception>
20 #include <string>
21 #include <utility>
22 
23 #include <folly/CPortability.h>
24 #include <folly/detail/IPAddress.h>
25 #include <folly/lang/Exception.h>
26 
27 namespace folly {
28 
29 /**
30  * Error codes for non-throwing interface of IPAddress family of functions.
31  */
32 enum class IPAddressFormatError { INVALID_IP, UNSUPPORTED_ADDR_FAMILY };
33 
34 /**
35  * Wraps error from parsing IP/MASK string
36  */
37 enum class CIDRNetworkError {
38   INVALID_DEFAULT_CIDR,
39   INVALID_IP_SLASH_CIDR,
40   INVALID_IP,
41   INVALID_CIDR,
42   CIDR_MISMATCH,
43 };
44 
45 /**
46  * Exception for invalid IP addresses.
47  */
48 class FOLLY_EXPORT IPAddressFormatException : public std::runtime_error {
49  public:
50   using std::runtime_error::runtime_error;
51 };
52 
53 class FOLLY_EXPORT InvalidAddressFamilyException
54     : public IPAddressFormatException {
55  public:
InvalidAddressFamilyException(const char * msg)56   explicit InvalidAddressFamilyException(const char* msg)
57       : IPAddressFormatException{msg} {}
InvalidAddressFamilyException(const std::string & msg)58   explicit InvalidAddressFamilyException(const std::string& msg) noexcept
59       : IPAddressFormatException{msg} {}
InvalidAddressFamilyException(sa_family_t family)60   explicit InvalidAddressFamilyException(sa_family_t family) noexcept
61       : InvalidAddressFamilyException(
62             "Address family " + detail::familyNameStr(family) +
63             " is not AF_INET or AF_INET6") {}
64 };
65 
66 } // namespace folly
67