]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/src/net/inet_address.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / src / net / inet_address.cc
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 #include <ostream>
23 #include <arpa/inet.h>
24
25 #include <seastar/net/inet_address.hh>
26 #include <seastar/net/socket_defs.hh>
27 #include <seastar/net/dns.hh>
28 #include <seastar/net/ip.hh>
29
30 namespace seastar {
31
32 seastar::net::inet_address::inet_address()
33 : inet_address(::in_addr{ 0, })
34 {}
35
36 seastar::net::inet_address::inet_address(::in_addr i)
37 : _in_family(family::INET), _in(i) {
38 }
39
40 seastar::net::inet_address::inet_address(::in6_addr i)
41 : _in_family(family::INET6), _in6(i) {
42 }
43
44 seastar::net::inet_address::inet_address(const sstring& addr)
45 : inet_address([&addr] {
46 inet_address in;
47 if (::inet_pton(AF_INET, addr.c_str(), &in._in)) {
48 in._in_family = family::INET;
49 return in;
50 }
51 if (::inet_pton(AF_INET6, addr.c_str(), &in._in6)) {
52 in._in_family = family::INET6;
53 return in;
54 }
55 throw std::invalid_argument(addr);
56 }())
57 {}
58
59 seastar::net::inet_address::inet_address(const ipv4_address& in)
60 : inet_address(::in_addr{hton(in.ip)})
61 {}
62
63 seastar::net::ipv4_address seastar::net::inet_address::as_ipv4_address() const {
64 if (_in_family != family::INET) {
65 // TODO: ipv4-compatible ipv6
66 throw std::invalid_argument("Not an IPv4 address");
67 }
68 return ipv4_address(ntoh(_in.s_addr));
69 }
70
71 bool seastar::net::inet_address::operator==(const inet_address& o) const {
72 if (o._in_family != _in_family) {
73 return false;
74 }
75 switch (_in_family) {
76 case family::INET:
77 return _in.s_addr == o._in.s_addr;
78 case family::INET6:
79 return std::equal(std::begin(_in6.s6_addr), std::end(_in6.s6_addr),
80 std::begin(o._in6.s6_addr));
81 default:
82 return false;
83 }
84 }
85
86 seastar::net::inet_address::operator const ::in_addr&() const {
87 if (_in_family != family::INET) {
88 throw std::invalid_argument("Not an ipv4 address");
89 }
90 return _in;
91 }
92
93 seastar::net::inet_address::operator const ::in6_addr&() const {
94 if (_in_family != family::INET6) {
95 throw std::invalid_argument("Not an ipv6 address");
96 }
97 return _in6;
98 }
99
100 size_t seastar::net::inet_address::size() const {
101 switch (_in_family) {
102 case family::INET:
103 return sizeof(::in_addr);
104 case family::INET6:
105 return sizeof(::in6_addr);
106 default:
107 return 0;
108 }
109 }
110
111 const void * seastar::net::inet_address::data() const {
112 return &_in;
113 }
114
115
116 std::ostream& seastar::net::operator<<(std::ostream& os, const inet_address& addr) {
117 char buffer[64];
118 return os << inet_ntop(int(addr.in_family()), addr.data(), buffer, sizeof(buffer));
119 }
120
121 std::ostream& seastar::net::operator<<(std::ostream& os, const inet_address::family& f) {
122 switch (f) {
123 case inet_address::family::INET:
124 os << "INET";
125 break;
126 case inet_address::family::INET6:
127 os << "INET6";
128 break;
129 default:
130 break;
131 }
132 return os;
133 }
134
135 std::ostream& operator<<(std::ostream& os, const socket_address& a) {
136 return os << seastar::net::inet_address(a.as_posix_sockaddr_in().sin_addr)
137 << ":" << ntohs(a.u.in.sin_port)
138 ;
139 }
140
141 }
142