]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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; | |
9f95a23c | 37 | struct ipv6_address; |
11fdf7f2 TL |
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: | |
9f95a23c | 46 | enum class family : sa_family_t { |
11fdf7f2 TL |
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 | }; | |
9f95a23c TL |
56 | |
57 | uint32_t _scope = invalid_scope; | |
11fdf7f2 | 58 | public: |
9f95a23c | 59 | static constexpr uint32_t invalid_scope = std::numeric_limits<uint32_t>::max(); |
11fdf7f2 | 60 | |
f67539c2 TL |
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; | |
11fdf7f2 TL |
65 | // NOTE: does _not_ resolve the address. Only parses |
66 | // ipv4/ipv6 numerical address | |
f67539c2 | 67 | // throws std::invalid_argument if sstring is invalid |
11fdf7f2 | 68 | inet_address(const sstring&); |
f67539c2 TL |
69 | inet_address(inet_address&&) noexcept = default; |
70 | inet_address(const inet_address&) noexcept = default; | |
11fdf7f2 | 71 | |
f67539c2 TL |
72 | inet_address(const ipv4_address&) noexcept; |
73 | inet_address(const ipv6_address&, uint32_t scope = invalid_scope) noexcept; | |
11fdf7f2 TL |
74 | |
75 | // throws iff ipv6 | |
76 | ipv4_address as_ipv4_address() const; | |
f67539c2 | 77 | ipv6_address as_ipv6_address() const noexcept; |
11fdf7f2 | 78 | |
f67539c2 TL |
79 | inet_address& operator=(const inet_address&) noexcept = default; |
80 | bool operator==(const inet_address&) const noexcept; | |
11fdf7f2 | 81 | |
f67539c2 | 82 | family in_family() const noexcept { |
11fdf7f2 TL |
83 | return _in_family; |
84 | } | |
85 | ||
f67539c2 | 86 | bool is_ipv6() const noexcept { |
9f95a23c TL |
87 | return _in_family == family::INET6; |
88 | } | |
f67539c2 | 89 | bool is_ipv4() const noexcept { |
9f95a23c TL |
90 | return _in_family == family::INET; |
91 | } | |
92 | ||
f67539c2 TL |
93 | size_t size() const noexcept; |
94 | const void * data() const noexcept; | |
11fdf7f2 | 95 | |
f67539c2 | 96 | uint32_t scope() const noexcept { |
9f95a23c TL |
97 | return _scope; |
98 | } | |
99 | ||
f67539c2 | 100 | // throws iff ipv6 |
9f95a23c | 101 | operator ::in_addr() const; |
f67539c2 | 102 | operator ::in6_addr() const noexcept; |
9f95a23c | 103 | |
f67539c2 | 104 | operator ipv6_address() const noexcept; |
11fdf7f2 TL |
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); | |
9f95a23c | 113 | |
f67539c2 | 114 | static std::optional<inet_address> parse_numerical(const sstring&); |
11fdf7f2 TL |
115 | }; |
116 | ||
117 | std::ostream& operator<<(std::ostream&, const inet_address&); | |
118 | std::ostream& operator<<(std::ostream&, const inet_address::family&); | |
119 | ||
120 | } | |
121 | } | |
9f95a23c TL |
122 | |
123 | namespace std { | |
124 | template<> | |
125 | struct hash<seastar::net::inet_address> { | |
126 | size_t operator()(const seastar::net::inet_address&) const; | |
127 | }; | |
128 | } |