]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/net/inet_address.hh
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / include / seastar / net / inet_address.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2016 ScyllaDB.
20 */
21
22 #pragma once
23
24 #include <iosfwd>
25 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <stdexcept>
28 #include <vector>
29
30 #include <seastar/core/future.hh>
31 #include <seastar/core/sstring.hh>
32
33 namespace seastar {
34 namespace net {
35
36 struct ipv4_address;
37 struct ipv6_address;
38
39 class unknown_host : public std::invalid_argument {
40 public:
41 using invalid_argument::invalid_argument;
42 };
43
44 class inet_address {
45 public:
46 enum class family : sa_family_t {
47 INET = AF_INET, INET6 = AF_INET6
48 };
49 private:
50 family _in_family;
51
52 union {
53 ::in_addr _in;
54 ::in6_addr _in6;
55 };
56
57 uint32_t _scope = invalid_scope;
58 public:
59 static constexpr uint32_t invalid_scope = std::numeric_limits<uint32_t>::max();
60
61 inet_address() noexcept;
62 inet_address(family) noexcept;
63 inet_address(::in_addr i) noexcept;
64 inet_address(::in6_addr i, uint32_t scope = invalid_scope) noexcept;
65 // NOTE: does _not_ resolve the address. Only parses
66 // ipv4/ipv6 numerical address
67 // throws std::invalid_argument if sstring is invalid
68 inet_address(const sstring&);
69 inet_address(inet_address&&) noexcept = default;
70 inet_address(const inet_address&) noexcept = default;
71
72 inet_address(const ipv4_address&) noexcept;
73 inet_address(const ipv6_address&, uint32_t scope = invalid_scope) noexcept;
74
75 // throws iff ipv6
76 ipv4_address as_ipv4_address() const;
77 ipv6_address as_ipv6_address() const noexcept;
78
79 inet_address& operator=(const inet_address&) noexcept = default;
80 bool operator==(const inet_address&) const noexcept;
81
82 family in_family() const noexcept {
83 return _in_family;
84 }
85
86 bool is_ipv6() const noexcept {
87 return _in_family == family::INET6;
88 }
89 bool is_ipv4() const noexcept {
90 return _in_family == family::INET;
91 }
92
93 size_t size() const noexcept;
94 const void * data() const noexcept;
95
96 uint32_t scope() const noexcept {
97 return _scope;
98 }
99
100 // throws iff ipv6
101 operator ::in_addr() const;
102 operator ::in6_addr() const noexcept;
103
104 operator ipv6_address() const noexcept;
105
106 future<sstring> hostname() const;
107 future<std::vector<sstring>> aliases() const;
108
109 static future<inet_address> find(const sstring&);
110 static future<inet_address> find(const sstring&, family);
111 static future<std::vector<inet_address>> find_all(const sstring&);
112 static future<std::vector<inet_address>> find_all(const sstring&, family);
113
114 static std::optional<inet_address> parse_numerical(const sstring&);
115
116 bool is_loopback() const noexcept;
117 bool is_addr_any() const noexcept;
118 };
119
120 std::ostream& operator<<(std::ostream&, const inet_address&);
121 std::ostream& operator<<(std::ostream&, const inet_address::family&);
122
123 }
124 }
125
126 namespace std {
127 template<>
128 struct hash<seastar::net::inet_address> {
129 size_t operator()(const seastar::net::inet_address&) const;
130 };
131 }
132
133 #if FMT_VERSION >= 90000
134 template <> struct fmt::formatter<seastar::net::inet_address> : fmt::ostream_formatter {};
135 template <> struct fmt::formatter<seastar::net::inet_address::family> : fmt::ostream_formatter {};
136 #endif