forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinet_address.hh
72 lines (66 loc) · 1.97 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
/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#pragma once
#include "types.hh"
#include "net/ip.hh"
#include "util/serialization.hh"
#include <sstream>
namespace gms {
class inet_address {
private:
// FIXME: ipv6
net::ipv4_address _addr;
public:
inet_address() = default;
inet_address(int32_t ip)
: _addr(uint32_t(ip)) {
}
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::ipv4_address(addr);
}
}
uint32_t raw_addr() const {
return _addr.ip;
}
bool is_broadcast_address() {
return _addr == net::ipv4::broadcast_address();
}
void serialize(bytes::iterator& out) const {
int8_t inet_address_size = sizeof(inet_address);
serialize_int8(out, inet_address_size);
serialize_int32(out, _addr.ip);
}
static inet_address deserialize(bytes_view& v) {
int8_t inet_address_size = read_simple<int8_t>(v);
assert(inet_address_size == sizeof(inet_address));
return inet_address(read_simple<int32_t>(v));
}
size_t serialized_size() const {
return serialize_int8_size + serialize_int32_size;
}
friend inline bool operator==(const inet_address& x, const inet_address& y) {
return x._addr == y._addr;
}
friend inline bool operator!=(const inet_address& x, const inet_address& y) {
return x._addr != y._addr;
}
friend inline bool operator<(const inet_address& x, const inet_address& y) {
return x._addr.ip < y._addr.ip;
}
friend inline std::ostream& operator<<(std::ostream& os, const inet_address& x) {
return os << x._addr;
}
friend struct std::hash<inet_address>;
};
}
namespace std {
template<>
struct hash<gms::inet_address> {
size_t operator()(gms::inet_address a) const { return std::hash<net::ipv4_address>()(a._addr); }
};
}