13 #ifndef __PROCESS_ADDRESS_HPP__
14 #define __PROCESS_ADDRESS_HPP__
22 #include <arpa/inet.h>
25 #include <glog/logging.h>
33 #include <boost/variant.hpp>
35 #include <boost/functional/hash.hpp>
75 return hostname.
get();
78 operator sockaddr_storage()
const
81 sockaddr_storage storage;
85 memset(&sockaddr.storage, 0,
sizeof(sockaddr_storage));
88 sockaddr.in.sin_family = AF_INET;
89 sockaddr.in.sin_addr =
ip.
in().get();
90 sockaddr.in.sin_port = htons(
port);
93 sockaddr.in6.sin6_family = AF_INET6;
94 sockaddr.in6.sin6_addr =
ip.
in6().get();
95 sockaddr.in6.sin6_port = htons(
port);
100 return sockaddr.storage;
128 return !(*
this == that);
141 stream << address.
ip <<
":" << address.
port;
167 : inet::
Address(net::IPv4(in.sin_addr), ntohs(in.sin_port)) {}
192 : inet::
Address(net::IPv6(in6.sin6_addr), ntohs(in6.sin6_port)) {}
208 const size_t PATH_LENGTH =
sizeof(un.sun_path);
210 if (path.length() >= PATH_LENGTH) {
211 return Error(
"Path too long, must be less than " +
215 un.sun_family = AF_UNIX;
216 memcpy(un.sun_path, path.c_str(), path.length() + 1);
229 if (sockaddr.un.sun_path[0] ==
'\0') {
230 return '\0' + std::string(sockaddr.un.sun_path + 1);
233 return std::string(sockaddr.un.sun_path);
236 operator sockaddr_storage()
const
238 return sockaddr.storage;
248 std::ostream& stream,
259 std::ostream& stream,
262 std::string path = address.
path();
263 if (!path.empty() && path[0] ==
'\0') {
266 return stream << path;
270 #endif // __WINDOWS__
289 #endif // __WINDOWS__
296 switch (storage.ss_family) {
300 #endif // __WINDOWS__
306 return Error(
"Unsupported family: " +
stringify(storage.ss_family));
316 return address.get();
324 inet6::
Address>(std::move(address)) {}
325 #endif // __WINDOWS__
333 inet6::
Address>(std::move(address)) {}
341 inet6::
Address>(std::move(address)) {}
350 #endif // __WINDOWS__
365 return sizeof(sockaddr_un);
367 #endif // __WINDOWS__
369 return sizeof(sockaddr_in);
372 return sizeof(sockaddr_in6);
377 operator sockaddr_storage()
const
382 return (sockaddr_storage) address;
384 #endif // __WINDOWS__
386 return (sockaddr_storage)
address;
389 return (sockaddr_storage)
address;
402 template <
typename AddressType>
437 return Error(
"Unexpected address family");
440 return Error(
"Unexpected address family");
443 #endif // __WINDOWS__
456 return Error(
"Unexpected address family");
458 #endif // __WINDOWS__
463 return Error(
"Unexpected address family");
478 return Error(
"Unexpected address family");
480 #endif // __WINDOWS__
482 return Error(
"Unexpected address family");
502 return Error(
"Unexpected address family");
504 #endif // __WINDOWS__
549 boost::hash_combine(seed, address.
port);
557 : hash<process::network::inet::Address>
563 : hash<process::network::inet::Address>
568 #endif // __PROCESS_ADDRESS_HPP__
static Address ANY_ANY()
Definition: address.hpp:158
sockaddr_storage storage
Definition: address.hpp:252
static IPv6 ANY()
Definition: ip.hpp:386
static Try< Address > create(const sockaddr_storage &storage)
Definition: address.hpp:294
std::string path() const
Definition: address.hpp:227
Definition: errorbase.hpp:35
Address(const sockaddr_in6 &in6)
Definition: address.hpp:191
#define ABORT(...)
Definition: abort.hpp:40
Address(const sockaddr_in &in)
Definition: address.hpp:166
Address(inet6::Address address)
Definition: address.hpp:335
Try< struct in_addr > in() const
Definition: ip.hpp:124
std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: address.hpp:258
Try< Address > address(int_fd s)
Returns the Address with the assigned ip and assigned port.
Definition: network.hpp:79
auto visit(Fs &&...fs) const -> decltype(boost::apply_visitor(overload(std::forward< Fs >(fs)...), std::declval< boost::variant< T, Ts...> & >()))
Definition: variant.hpp:65
static IPv4 ANY()
Definition: ip.hpp:342
bool operator<(const Address &that) const
Definition: address.hpp:103
bool operator!=(const Address &that) const
Definition: address.hpp:126
bool isAny() const
Definition: ip.hpp:157
result_type operator()(const argument_type &address) const
Definition: address.hpp:545
Definition: address.hpp:277
static Address LOOPBACK_ANY()
Definition: address.hpp:153
Address(const net::IP &_ip, uint16_t _port)
Definition: address.hpp:55
size_t size() const
Definition: address.hpp:360
bool operator>(const Address &that) const
Definition: address.hpp:112
Try< std::string > getHostname(const IP &ip)
Definition: net.hpp:45
bool operator==(const Address &that) const
Definition: address.hpp:241
Try< AddressType > convert(Try< Address > &&address)
Definition: address.hpp:426
static Try< Address > create(const std::string &path)
Definition: address.hpp:204
static Address LOOPBACK_ANY()
Definition: address.hpp:178
Address(const inet::Address &address)
Definition: address.hpp:311
Try< std::string > hostname() const
Returns the hostname of this address's IP.
Definition: address.hpp:65
#define CHECK_SOME(expression)
Definition: check.hpp:44
static IPv4 LOOPBACK()
Definition: ip.hpp:337
Address(const net::IPv6 &ip, uint16_t port)
Definition: address.hpp:188
bool operator==(const Address &that) const
Definition: address.hpp:121
uint16_t port
Definition: address.hpp:135
process::network::inet::Address argument_type
Definition: address.hpp:543
Definition: variant.hpp:47
Address(unix::Address address)
Definition: address.hpp:320
Try< std::string > hostname()
Definition: net.hpp:154
static Address ANY_ANY()
Definition: address.hpp:183
static Try error(const E &e)
Definition: try.hpp:42
Definition: address.hpp:201
static IPv6 LOOPBACK()
Definition: ip.hpp:381
friend std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: address.hpp:258
Definition: address.hpp:52
size_t result_type
Definition: address.hpp:541
Address(inet4::Address address)
Definition: address.hpp:327
Address(const sockaddr_un &un)
Definition: address.hpp:221
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Family family() const
Definition: address.hpp:343
bool isError() const
Definition: try.hpp:71
Try< struct in6_addr > in6() const
Definition: ip.hpp:134
Address(const net::IPv4 &ip, uint16_t port)
Definition: address.hpp:163
int family() const
Definition: ip.hpp:118
sockaddr_un un
Definition: address.hpp:253
Definition: address.hpp:150
std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: address.hpp:139
std::string stringify(int flags)
net::IP ip
Definition: address.hpp:134
Definition: address.hpp:175
Family
Definition: address.hpp:286
const T & get() const
Definition: try.hpp:73