]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/net/IPAddress.h
2 * Copyright (c) 2017 Uber Technologies, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef JAEGERTRACING_NET_IPADDRESS_H
18 #define JAEGERTRACING_NET_IPADDRESS_H
28 #include <system_error>
31 #include "jaegertracing/Compilers.h"
39 #include <arpa/inet.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
51 namespace jaegertracing
{
56 static std::pair
<std::string
, int> parse(const std::string
& hostPort
)
58 const auto colonPos
= hostPort
.find(':');
59 const auto ip
= hostPort
.substr(0, colonPos
);
61 if (colonPos
!= std::string::npos
) {
62 const auto portStr
= hostPort
.substr(colonPos
+ 1);
63 std::istringstream
iss(portStr
);
68 return std::make_pair(ip
, port
);
71 static IPAddress
v4(const std::string
& hostPort
)
73 auto result
= parse(hostPort
);
74 return v4(result
.first
, result
.second
);
77 static IPAddress
v4(const std::string
& ip
, int port
)
79 return versionFromString(ip
, port
, AF_INET
);
82 static IPAddress
v6(const std::string
& ip
, int port
)
84 return versionFromString(ip
, port
, AF_INET6
);
87 static IPAddress
localIP(int family
);
91 , _addrLen(sizeof(::sockaddr_in
))
93 std::memset(&_addr
, 0, sizeof(_addr
));
96 IPAddress(const ::sockaddr_storage
& addr
, ::socklen_t addrLen
)
102 IPAddress(const ::sockaddr
& addr
, ::socklen_t addrLen
)
105 std::memcpy(&_addr
, &addr
, addrLen
);
108 explicit IPAddress(const ::sockaddr_in
& addr
)
109 : IPAddress(reinterpret_cast<const ::sockaddr
&>(addr
), sizeof(addr
))
113 explicit IPAddress(const ::sockaddr_in6
& addr
)
114 : IPAddress(reinterpret_cast<const ::sockaddr
&>(addr
), sizeof(addr
))
118 bool operator==(const IPAddress
& rhs
) const
120 if (_addrLen
!= rhs
._addrLen
) {
123 return std::memcmp(&_addr
, &rhs
._addr
, _addrLen
) == 0;
126 const ::sockaddr_storage
& addr() const { return _addr
; }
128 ::socklen_t
addrLen() const { return _addrLen
; }
130 void print(std::ostream
& out
) const
132 out
<< "{ family=" << family();
133 const auto addrStr
= host();
134 if (!addrStr
.empty()) {
135 out
<< ", addr=" << addrStr
;
137 out
<< ", port=" << port() << " }";
140 std::string
authority() const
142 const auto portNum
= port();
144 return host() + ':' + std::to_string(portNum
);
149 std::string
host() const
151 std::array
<char, INET6_ADDRSTRLEN
> buffer
;
152 const auto af
= family();
153 const auto* addrStr
= ::inet_ntop(
157 static_cast<const void*>(
158 &reinterpret_cast<const ::sockaddr_in
&>(_addr
).sin_addr
))
160 static_cast<const void*>(
161 &reinterpret_cast<const ::sockaddr_in6
&>(_addr
).sin6_addr
)),
164 return addrStr
? addrStr
: "";
169 if (family() == AF_INET
) {
171 reinterpret_cast<const ::sockaddr_in
&>(_addr
).sin_port
);
173 return ntohs(reinterpret_cast<const ::sockaddr_in6
&>(_addr
).sin6_port
);
178 if (_addrLen
== sizeof(::sockaddr_in
)) {
181 assert(_addrLen
== sizeof(::sockaddr_in6
));
186 static IPAddress
versionFromString(const std::string
& ip
, int port
, int family
);
188 ::sockaddr_storage _addr
;
189 ::socklen_t _addrLen
;
192 struct AddrInfoDeleter
: public std::function
<void(::addrinfo
*)> {
193 void operator()(::addrinfo
* addrInfo
) const { ::freeaddrinfo(addrInfo
); }
196 std::unique_ptr
<::addrinfo
, AddrInfoDeleter
> resolveAddress(
197 const std::string
& host
, int port
, int family
, int type
= SOCK_STREAM
);
200 } // namespace jaegertracing
202 inline std::ostream
& operator<<(std::ostream
& out
,
203 const jaegertracing::net::IPAddress
& addr
)
209 #endif // JAEGERTRACING_NET_IPADDRESS_H