]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/src/net/inet_address.cc
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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2016 ScyllaDB.
23 #include <arpa/inet.h>
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>
32 seastar::net::inet_address::inet_address()
33 : inet_address(::in_addr
{ 0, })
36 seastar::net::inet_address::inet_address(::in_addr i
)
37 : _in_family(family::INET
), _in(i
) {
40 seastar::net::inet_address::inet_address(::in6_addr i
)
41 : _in_family(family::INET6
), _in6(i
) {
44 seastar::net::inet_address::inet_address(const sstring
& addr
)
45 : inet_address([&addr
] {
47 if (::inet_pton(AF_INET
, addr
.c_str(), &in
._in
)) {
48 in
._in_family
= family::INET
;
51 if (::inet_pton(AF_INET6
, addr
.c_str(), &in
._in6
)) {
52 in
._in_family
= family::INET6
;
55 throw std::invalid_argument(addr
);
59 seastar::net::inet_address::inet_address(const ipv4_address
& in
)
60 : inet_address(::in_addr
{hton(in
.ip
)})
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");
68 return ipv4_address(ntoh(_in
.s_addr
));
71 bool seastar::net::inet_address::operator==(const inet_address
& o
) const {
72 if (o
._in_family
!= _in_family
) {
77 return _in
.s_addr
== o
._in
.s_addr
;
79 return std::equal(std::begin(_in6
.s6_addr
), std::end(_in6
.s6_addr
),
80 std::begin(o
._in6
.s6_addr
));
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");
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");
100 size_t seastar::net::inet_address::size() const {
101 switch (_in_family
) {
103 return sizeof(::in_addr
);
105 return sizeof(::in6_addr
);
111 const void * seastar::net::inet_address::data() const {
116 std::ostream
& seastar::net::operator<<(std::ostream
& os
, const inet_address
& addr
) {
118 return os
<< inet_ntop(int(addr
.in_family()), addr
.data(), buffer
, sizeof(buffer
));
121 std::ostream
& seastar::net::operator<<(std::ostream
& os
, const inet_address::family
& f
) {
123 case inet_address::family::INET
:
126 case inet_address::family::INET6
:
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
)