Seastar
High performance C++ framework for concurrent servers
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 #ifndef SEASTAR_MODULE
25 #include <iosfwd>
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <stdexcept>
29 #include <vector>
30 #endif
31 
32 #include <seastar/core/future.hh>
33 #include <seastar/core/sstring.hh>
34 #include <seastar/util/modules.hh>
35 
36 namespace seastar {
37 namespace net {
38 
39 SEASTAR_MODULE_EXPORT_BEGIN
40 
41 struct ipv4_address;
42 struct ipv6_address;
43 
44 class unknown_host : public std::invalid_argument {
45 public:
46  using invalid_argument::invalid_argument;
47 };
48 
49 class inet_address {
50 public:
51  enum class family : sa_family_t {
52  INET = AF_INET, INET6 = AF_INET6
53  };
54 private:
55  family _in_family;
56 
57  union {
58  ::in_addr _in;
59  ::in6_addr _in6;
60  };
61 
62  uint32_t _scope = invalid_scope;
63 public:
64  static constexpr uint32_t invalid_scope = std::numeric_limits<uint32_t>::max();
65 
66  inet_address() noexcept;
67  inet_address(family) noexcept;
68  inet_address(::in_addr i) noexcept;
69  inet_address(::in6_addr i, uint32_t scope = invalid_scope) noexcept;
70  // NOTE: does _not_ resolve the address. Only parses
71  // ipv4/ipv6 numerical address
72  // throws std::invalid_argument if sstring is invalid
73  inet_address(const sstring&);
74  inet_address(inet_address&&) noexcept = default;
75  inet_address(const inet_address&) noexcept = default;
76 
77  inet_address(const ipv4_address&) noexcept;
78  inet_address(const ipv6_address&, uint32_t scope = invalid_scope) noexcept;
79 
80  // throws iff ipv6
81  ipv4_address as_ipv4_address() const;
82  ipv6_address as_ipv6_address() const noexcept;
83 
84  inet_address& operator=(const inet_address&) noexcept = default;
85  bool operator==(const inet_address&) const noexcept;
86 
87  family in_family() const noexcept {
88  return _in_family;
89  }
90 
91  bool is_ipv6() const noexcept {
92  return _in_family == family::INET6;
93  }
94  bool is_ipv4() const noexcept {
95  return _in_family == family::INET;
96  }
97 
98  size_t size() const noexcept;
99  const void * data() const noexcept;
100 
101  uint32_t scope() const noexcept {
102  return _scope;
103  }
104 
105  // throws iff ipv6
106  operator ::in_addr() const;
107  operator ::in6_addr() const noexcept;
108 
109  operator ipv6_address() const noexcept;
110 
111  future<sstring> hostname() const;
112  future<std::vector<sstring>> aliases() const;
113 
114  static future<inet_address> find(const sstring&);
115  static future<inet_address> find(const sstring&, family);
116  static future<std::vector<inet_address>> find_all(const sstring&);
117  static future<std::vector<inet_address>> find_all(const sstring&, family);
118 
119  static std::optional<inet_address> parse_numerical(const sstring&);
120 
121  bool is_loopback() const noexcept;
122  bool is_addr_any() const noexcept;
123 };
124 
125 std::ostream& operator<<(std::ostream&, const inet_address&);
126 std::ostream& operator<<(std::ostream&, const inet_address::family&);
127 
128 SEASTAR_MODULE_EXPORT_END
129 }
130 }
131 
132 namespace std {
133 SEASTAR_MODULE_EXPORT
134 template<>
135 struct hash<seastar::net::inet_address> {
136  size_t operator()(const seastar::net::inet_address&) const;
137 };
138 }
139 
140 #if FMT_VERSION >= 90000
141 template <> struct fmt::formatter<seastar::net::inet_address> : fmt::ostream_formatter {};
142 template <> struct fmt::formatter<seastar::net::inet_address::family> : fmt::ostream_formatter {};
143 #endif
A representation of a possibly not-yet-computed value.
Definition: future.hh:1238
Definition: inet_address.hh:49
Definition: inet_address.hh:44
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: ipv4_address.hh:35
Definition: ipv6_address.hh:34