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