forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinet_address.hh
91 lines (77 loc) · 2.44 KB
/
inet_address.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <seastar/net/ipv4_address.hh>
#include <seastar/net/inet_address.hh>
#include <seastar/net/socket_defs.hh>
#include <iosfwd>
#include <optional>
#include <functional>
#include "bytes.hh"
#include "seastarx.hh"
namespace gms {
class inet_address {
private:
net::inet_address _addr;
public:
inet_address() = default;
inet_address(int32_t ip) noexcept
: inet_address(uint32_t(ip)) {
}
explicit inet_address(uint32_t ip) noexcept
: _addr(net::ipv4_address(ip)) {
}
inet_address(const net::inet_address& addr) noexcept : _addr(addr) {}
inet_address(const socket_address& sa) noexcept
: inet_address(sa.addr())
{}
const net::inet_address& addr() const noexcept {
return _addr;
}
inet_address(const inet_address&) = default;
operator const seastar::net::inet_address&() const noexcept {
return _addr;
}
// throws std::invalid_argument if sstring is invalid
inet_address(const sstring& addr) {
// FIXME: We need a real DNS resolver
if (addr == "localhost") {
_addr = net::ipv4_address("127.0.0.1");
} else {
_addr = net::inet_address(addr);
}
}
bytes_view bytes() const noexcept {
return bytes_view(reinterpret_cast<const int8_t*>(_addr.data()), _addr.size());
}
// TODO remove
uint32_t raw_addr() const {
return addr().as_ipv4_address().ip;
}
sstring to_sstring() const;
friend inline bool operator==(const inet_address& x, const inet_address& y) noexcept {
return x._addr == y._addr;
}
friend inline bool operator!=(const inet_address& x, const inet_address& y) noexcept {
using namespace std::rel_ops;
return x._addr != y._addr;
}
friend inline bool operator<(const inet_address& x, const inet_address& y) noexcept {
return x.bytes() < y.bytes();
}
friend struct std::hash<inet_address>;
using opt_family = std::optional<net::inet_address::family>;
static future<inet_address> lookup(sstring, opt_family family = {}, opt_family preferred = {});
};
std::ostream& operator<<(std::ostream& os, const inet_address& x);
}
namespace std {
template<>
struct hash<gms::inet_address> {
size_t operator()(gms::inet_address a) const noexcept { return std::hash<net::inet_address>()(a._addr); }
};
}