]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/net/inet_address.hh
import 15.2.0 Octopus source
[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();
62 inet_address(family);
63 inet_address(::in_addr i);
64 inet_address(::in6_addr i, uint32_t scope = invalid_scope);
65 // NOTE: does _not_ resolve the address. Only parses
66 // ipv4/ipv6 numerical address
67 inet_address(const sstring&);
68 inet_address(inet_address&&) = default;
69 inet_address(const inet_address&) = default;
70
71 inet_address(const ipv4_address&);
72 inet_address(const ipv6_address&, uint32_t scope = invalid_scope);
73
74 // throws iff ipv6
75 ipv4_address as_ipv4_address() const;
76 ipv6_address as_ipv6_address() const;
77
78 inet_address& operator=(const inet_address&) = default;
79 bool operator==(const inet_address&) const;
80
81 family in_family() const {
82 return _in_family;
83 }
84
85 bool is_ipv6() const {
86 return _in_family == family::INET6;
87 }
88 bool is_ipv4() const {
89 return _in_family == family::INET;
90 }
91
92 size_t size() const;
93 const void * data() const;
94
95 uint32_t scope() const {
96 return _scope;
97 }
98
99 operator ::in_addr() const;
100 operator ::in6_addr() const;
101
102 operator ipv6_address() const;
103
104 future<sstring> hostname() const;
105 future<std::vector<sstring>> aliases() const;
106
107 static future<inet_address> find(const sstring&);
108 static future<inet_address> find(const sstring&, family);
109 static future<std::vector<inet_address>> find_all(const sstring&);
110 static future<std::vector<inet_address>> find_all(const sstring&, family);
111
112 static compat::optional<inet_address> parse_numerical(const sstring&);
113 };
114
115 std::ostream& operator<<(std::ostream&, const inet_address&);
116 std::ostream& operator<<(std::ostream&, const inet_address::family&);
117
118 }
119 }
120
121 namespace std {
122 template<>
123 struct hash<seastar::net::inet_address> {
124 size_t operator()(const seastar::net::inet_address&) const;
125 };
126 }