|
Sponge
CS144's user-space TCP library
|
Go to the documentation of this file.
10 #include <system_error>
15 Address::Raw::operator sockaddr *() {
return reinterpret_cast<sockaddr *
>(&storage); }
18 Address::Raw::operator
const sockaddr *()
const {
return reinterpret_cast<const sockaddr *
>(&storage); }
35 const char *
name() const noexcept
override {
return "gai_error_category"; }
39 string message(
const int return_value)
const noexcept
override {
return gai_strerror(return_value); }
45 Address::Address(
const string &node,
const string &service,
const addrinfo &hints) : _size() {
47 addrinfo *resolved_address =
nullptr;
50 const int gai_ret = getaddrinfo(node.
c_str(), service.
c_str(), &hints, &resolved_address);
56 if (resolved_address ==
nullptr) {
57 throw runtime_error(
"getaddrinfo returned successfully but with no results");
61 auto addrinfo_deleter = [](addrinfo *
const x) { freeaddrinfo(x); };
62 unique_ptr<addrinfo, decltype(addrinfo_deleter)> wrapped_address(resolved_address,
move(addrinfo_deleter));
65 *
this =
Address(wrapped_address->ai_addr, wrapped_address->ai_addrlen);
71 static inline addrinfo
make_hints(
const int ai_flags,
const int ai_family) {
73 hints.ai_flags = ai_flags;
74 hints.ai_family = ai_family;
104 const auto ip_and_port =
ip_port();
105 return ip_and_port.first +
":" +
::to_string(ip_and_port.second);
110 throw runtime_error(
"ipv4_numeric called on non-IPV4 address");
113 sockaddr_in ipv4_addr{};
116 return be32toh(ipv4_addr.sin_addr.s_addr);
120 sockaddr_in ipv4_addr{};
121 ipv4_addr.sin_family = AF_INET;
122 ipv4_addr.sin_addr.s_addr = htobe32(ip_address);
124 return {
reinterpret_cast<sockaddr *
>(&ipv4_addr),
sizeof(ipv4_addr)};
Error category for getaddrinfo and getnameinfo failures.
std::system_error plus the name of what was being attempted
static Address from_ipv4_numeric(const uint32_t ip_address)
Create an Address from a 32-bit raw numeric IP address.
Wrapper around IPv4 addresses and DNS operations.
uint32_t ipv4_numeric() const
Numeric IP address as an integer (i.e., in host byte order).
socklen_t _size
Size of the wrapped address.
Raw _address
A wrapped sockaddr_storage containing the address.
sockaddr_storage storage
The wrapped struct itself.
bool operator==(const Address &other) const
Equality comparison.
string message(const int return_value) const noexcept override
An error message.
static addrinfo make_hints(const int ai_flags, const int ai_family)
Build a struct addrinfo containing hints for getaddrinfo(3).
const char * name() const noexcept override
The name of the wrapped error.
Address(const std::string &node, const std::string &service, const addrinfo &hints)
Constructor from ip/host, service/port, and hints to the resolver.
socklen_t size() const
Size of the underlying address storage.
uint16_t port() const
Numeric port (host byte order).
std::string to_string() const
Human-readable string, e.g., "8.8.8.8:53".
std::string ip() const
Dotted-quad IP address string ("18.243.0.1").
std::pair< std::string, uint16_t > ip_port() const
Dotted-quad IP address string ("18.243.0.1") and numeric port.