13 #ifndef __PROCESS_ADDRESS_HPP__ 14 #define __PROCESS_ADDRESS_HPP__ 23 #include <arpa/inet.h> 26 #include <glog/logging.h> 35 #include <boost/functional/hash.hpp> 81 return hostname.
get();
84 operator sockaddr_storage()
const 87 sockaddr_storage storage;
91 memset(&sockaddr.storage, 0,
sizeof(sockaddr_storage));
94 sockaddr.in.sin_family = AF_INET;
95 sockaddr.in.sin_addr =
ip.
in().get();
96 sockaddr.in.sin_port = htons(
port);
99 sockaddr.in6.sin6_family = AF_INET6;
100 sockaddr.in6.sin6_addr =
ip.
in6().get();
101 sockaddr.in6.sin6_port = htons(
port);
106 return sockaddr.storage;
134 return !(*
this == that);
147 stream << address.
ip <<
":" << address.
port;
173 : inet::
Address(
net::IPv4(in.sin_addr), ntohs(in.sin_port)) {}
198 : inet::
Address(
net::IPv6(in6.sin6_addr), ntohs(in6.sin6_port)) {}
214 const size_t PATH_LENGTH =
sizeof(un.sun_path);
216 if (path.length() >= PATH_LENGTH) {
217 return Error(
"Path too long, must be less than " +
221 un.sun_family = AF_UNIX;
222 memcpy(un.sun_path, path.c_str(), path.length() + 1);
225 un, path.length() + offsetof(
struct sockaddr_un, sun_path) + 1);
240 if (_length.isNone()) {
241 CHECK(un.sun_path[0] != 0)
242 <<
"Cannot automatically determine size of abstract socket address";
244 length = ::strlen(un.sun_path) + offsetof(sockaddr_un, sun_path) + 1;
246 CHECK(_length.get() <=
sizeof(
struct sockaddr_un));
247 length = _length.get();
258 return std::string(sockaddr.un.sun_path, path_length());
261 operator sockaddr_storage()
const 263 return sockaddr.storage;
268 return length == that.length &&
269 !memcmp(sockaddr.un.sun_path, that.sockaddr.
un.sun_path, path_length());
274 std::ostream& stream,
279 size_t path_length()
const 281 if (length ==
sizeof(sa_family_t)) {
284 }
else if (sockaddr.un.sun_path[0] ==
'\0') {
286 return length -
sizeof(sa_family_t);
289 return length - offsetof(
struct sockaddr_un, sun_path) - 1;
306 std::ostream& stream,
310 if (!path.empty() && path[0] ==
'\0') {
313 return stream << path;
317 #endif // __WINDOWS__ 336 #endif // __WINDOWS__ 345 const sockaddr_storage& storage,
348 switch (storage.ss_family) {
354 if (length.isNone()) {
355 return Error(
"Need length to create unix address from sockaddr");
358 #endif // __WINDOWS__ 364 return Error(
"Unsupported family: " +
stringify(storage.ss_family));
373 switch (address->sa_family) {
379 if (length >
sizeof(
struct sockaddr_un)) {
380 return Error(
"Invalid size for AF_UNIX sockaddr: " +
382 stringify(
sizeof(
struct sockaddr_un)) +
" expected");
384 return unix::Address(*((
const sockaddr_un*)address), length);
385 #endif // __WINDOWS__ 387 if (length <
sizeof(
struct sockaddr_in)) {
388 return Error(
"Invalid size for AF_INET sockaddr: " +
390 stringify(
sizeof(
struct sockaddr_in)) +
" expected");
394 if (length <
sizeof(
struct sockaddr_in6)) {
395 return Error(
"Invalid size for AF_INET6 sockaddr: " +
397 stringify(
sizeof(
struct sockaddr_in6)) +
" expected");
401 return Error(
"Unsupported family: " +
stringify(address->sa_family));
411 return address.get();
420 #endif // __WINDOWS__ 445 #endif // __WINDOWS__ 460 return address.
size();
462 #endif // __WINDOWS__ 464 return sizeof(sockaddr_in);
467 return sizeof(sockaddr_in6);
472 operator sockaddr_storage()
const 477 return (sockaddr_storage) address;
479 #endif // __WINDOWS__ 481 return (sockaddr_storage)
address;
484 return (sockaddr_storage)
address;
497 template <
typename AddressType>
532 return Error(
"Unexpected address family");
535 return Error(
"Unexpected address family");
538 #endif // __WINDOWS__ 551 return Error(
"Unexpected address family");
553 #endif // __WINDOWS__ 558 return Error(
"Unexpected address family");
573 return Error(
"Unexpected address family");
575 #endif // __WINDOWS__ 577 return Error(
"Unexpected address family");
597 return Error(
"Unexpected address family");
599 #endif // __WINDOWS__ 644 boost::hash_combine(seed, address.
port);
652 : hash<process::network::inet::Address>
657 struct hash<process::network::inet6::Address>
658 : hash<process::network::inet::Address>
663 #endif // __PROCESS_ADDRESS_HPP__ static Address ANY_ANY()
Definition: address.hpp:164
sockaddr_storage storage
Definition: address.hpp:294
static IPv6 ANY()
Definition: ip.hpp:386
std::string path() const
Definition: address.hpp:256
Definition: errorbase.hpp:36
Address(const sockaddr_in6 &in6)
Definition: address.hpp:197
Definition: option.hpp:29
#define ABORT(...)
Definition: abort.hpp:40
Address(const sockaddr_in &in)
Definition: address.hpp:172
T & get()&
Definition: try.hpp:80
Address(inet6::Address address)
Definition: address.hpp:430
Try< struct in_addr > in() const
Definition: ip.hpp:124
Address(const sockaddr_un &un, Option< socklen_t > _length=None())
Definition: address.hpp:235
Try< Address > address(int_fd s)
Returns the Address with the assigned ip and assigned port.
Definition: network.hpp:79
static IPv4 ANY()
Definition: ip.hpp:342
bool operator<(const Address &that) const
Definition: address.hpp:109
bool operator!=(const Address &that) const
Definition: address.hpp:132
bool isAny() const
Definition: ip.hpp:157
result_type operator()(const argument_type &address) const
Definition: address.hpp:640
Definition: type_utils.hpp:619
Definition: address.hpp:324
static Address LOOPBACK_ANY()
Definition: address.hpp:159
Address(const net::IP &_ip, uint16_t _port)
Definition: address.hpp:55
size_t size() const
Definition: address.hpp:455
bool operator>(const Address &that) const
Definition: address.hpp:118
Try< std::string > getHostname(const IP &ip)
Definition: net.hpp:45
static Try< Address > create(const sockaddr_storage &storage, Option< socklen_t > length=None())
Definition: address.hpp:344
bool operator==(const Address &that) const
Definition: address.hpp:266
Try< AddressType > convert(Try< Address > &&address)
Definition: address.hpp:521
static Try< Address > create(const std::string &path)
Definition: address.hpp:210
static Address LOOPBACK_ANY()
Definition: address.hpp:184
Address(const inet::Address &address)
Definition: address.hpp:406
#define CHECK_SOME(expression)
Definition: check.hpp:50
static IPv4 LOOPBACK()
Definition: ip.hpp:337
Address(const net::IPv6 &ip, uint16_t port)
Definition: address.hpp:194
bool operator==(const Address &that) const
Definition: address.hpp:127
uint16_t port
Definition: address.hpp:141
process::network::inet::Address argument_type
Definition: address.hpp:638
Definition: variant.hpp:47
Address(unix::Address address)
Definition: address.hpp:415
Try< std::string > hostname()
Definition: net.hpp:154
static Address ANY_ANY()
Definition: address.hpp:189
static Try error(const E &e)
Definition: try.hpp:43
Definition: address.hpp:207
size_t size() const
Definition: address.hpp:251
static IPv6 LOOPBACK()
Definition: ip.hpp:381
Definition: address.hpp:52
size_t result_type
Definition: address.hpp:636
Address(inet4::Address address)
Definition: address.hpp:422
Family family() const
Definition: address.hpp:438
bool isError() const
Definition: try.hpp:78
Try< struct in6_addr > in6() const
Definition: ip.hpp:134
Definition: executor.hpp:48
Address(const net::IPv4 &ip, uint16_t port)
Definition: address.hpp:169
int family() const
Definition: ip.hpp:118
static Try< Address > create(const sockaddr *address, size_t length)
Definition: address.hpp:369
sockaddr_un un
Definition: address.hpp:295
Try< std::string > lookup_hostname() const
Returns the hostname of this address's IP, using a reverse DNS lookup for remote addresses or the loc...
Definition: address.hpp:71
Definition: address.hpp:156
std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: address.hpp:145
std::string stringify(int flags)
net::IP ip
Definition: address.hpp:140
Definition: address.hpp:181
Family
Definition: address.hpp:333