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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2016 ScyllaDB.
25 #include <sys/types.h>
26 #include <netinet/in.h>
30 #include <seastar/core/future.hh>
31 #include <seastar/core/sstring.hh>
39 class unknown_host : public std::invalid_argument {
41 using invalid_argument::invalid_argument;
46 enum class family : sa_family_t {
47 INET = AF_INET, INET6 = AF_INET6
57 uint32_t _scope = invalid_scope;
59 static constexpr uint32_t invalid_scope = std::numeric_limits<uint32_t>::max();
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;
72 inet_address(const ipv4_address&) noexcept;
73 inet_address(const ipv6_address&, uint32_t scope = invalid_scope) noexcept;
76 ipv4_address as_ipv4_address() const;
77 ipv6_address as_ipv6_address() const noexcept;
79 inet_address& operator=(const inet_address&) noexcept = default;
80 bool operator==(const inet_address&) const noexcept;
82 family in_family() const noexcept {
86 bool is_ipv6() const noexcept {
87 return _in_family == family::INET6;
89 bool is_ipv4() const noexcept {
90 return _in_family == family::INET;
93 size_t size() const noexcept;
94 const void * data() const noexcept;
96 uint32_t scope() const noexcept {
101 operator ::in_addr() const;
102 operator ::in6_addr() const noexcept;
104 operator ipv6_address() const noexcept;
106 future<sstring> hostname() const;
107 future<std::vector<sstring>> aliases() const;
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);
114 static std::optional<inet_address> parse_numerical(const sstring&);
116 bool is_loopback() const noexcept;
117 bool is_addr_any() const noexcept;
120 std::ostream& operator<<(std::ostream&, const inet_address&);
121 std::ostream& operator<<(std::ostream&, const inet_address::family&);
128 struct hash<seastar::net::inet_address> {
129 size_t operator()(const seastar::net::inet_address&) const;
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 {};