Some more OpenSSL 1.1.0 compat APIs
[folly.git] / folly / IPAddressException.h
1 /*
2  * Copyright 2017 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
17 #pragma once
18
19 #include <exception>
20 #include <string>
21 #include <utility>
22
23 #include <folly/detail/IPAddress.h>
24
25 namespace folly {
26
27 /**
28  * Exception for invalid IP addresses.
29  */
30 class IPAddressFormatException : public std::exception {
31  public:
32   explicit IPAddressFormatException(std::string msg) noexcept
33       : msg_(std::move(msg)) {}
34   IPAddressFormatException(const IPAddressFormatException&) = default;
35   IPAddressFormatException(IPAddressFormatException&&) = default;
36   IPAddressFormatException& operator=(const IPAddressFormatException&) =
37       default;
38   IPAddressFormatException& operator=(IPAddressFormatException&&) = default;
39
40   ~IPAddressFormatException() noexcept override {}
41   const char* what(void) const noexcept override {
42     return msg_.c_str();
43   }
44
45  private:
46   std::string msg_;
47 };
48
49 class InvalidAddressFamilyException : public IPAddressFormatException {
50  public:
51   explicit InvalidAddressFamilyException(std::string msg) noexcept
52       : IPAddressFormatException(std::move(msg)) {}
53   explicit InvalidAddressFamilyException(sa_family_t family) noexcept
54       : InvalidAddressFamilyException(
55             "Address family " + detail::familyNameStr(family) +
56             " is not AF_INET or AF_INET6") {}
57   InvalidAddressFamilyException(const InvalidAddressFamilyException&) = default;
58   InvalidAddressFamilyException(InvalidAddressFamilyException&&) = default;
59   InvalidAddressFamilyException& operator=(
60       const InvalidAddressFamilyException&) = default;
61   InvalidAddressFamilyException& operator=(InvalidAddressFamilyException&&) =
62       default;
63 };
64
65 }  // folly