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