Sponge
CS144's user-space TCP library
|
Wrapper around IPv4 addresses and DNS operations. More...
#include <address.hh>
Classes | |
class | Raw |
Wrapper around sockaddr_storage. More... | |
Public Member Functions | |
Address (const sockaddr *addr, const std::size_t size) | |
Construct from a sockaddr *. More... | |
Address (const std::string &hostname, const std::string &service) | |
Construct by resolving a hostname and servicename. More... | |
Address (const std::string &ip, const std::uint16_t port=0) | |
Construct from dotted-quad string ("18.243.0.1") and numeric port. More... | |
bool | operator!= (const Address &other) const |
bool | operator== (const Address &other) const |
Equality comparison. More... | |
Low-level operations | |
socklen_t | size () const |
Size of the underlying address storage. More... | |
operator const sockaddr * () const | |
Const pointer to the underlying socket address storage. More... | |
Private Member Functions | |
Address (const std::string &node, const std::string &service, const addrinfo &hints) | |
Constructor from ip/host, service/port, and hints to the resolver. More... | |
Private Attributes | |
Raw | _address {} |
A wrapped sockaddr_storage containing the address. More... | |
socklen_t | _size |
Size of the wrapped address. More... | |
Conversions | |
std::pair< std::string, uint16_t > | ip_port () const |
Dotted-quad IP address string ("18.243.0.1") and numeric port. More... | |
std::string | ip () const |
Dotted-quad IP address string ("18.243.0.1"). More... | |
uint16_t | port () const |
Numeric port (host byte order). More... | |
uint32_t | ipv4_numeric () const |
Numeric IP address as an integer (i.e., in host byte order). More... | |
std::string | to_string () const |
Human-readable string, e.g., "8.8.8.8:53". More... | |
static Address | from_ipv4_numeric (const uint32_t ip_address) |
Create an Address from a 32-bit raw numeric IP address. More... | |
Wrapper around IPv4 addresses and DNS operations.
For example, you can do DNS lookups:
or you can specify an IP address and port number:
Once you have an address, you can convert it to other useful representations, e.g.,
Definition at line 13 of file address.hh.
|
private |
Constructor from ip/host, service/port, and hints to the resolver.
[in] | node | is the hostname or dotted-quad address |
[in] | service | is the service name or numeric string |
[in] | hints | are criteria for resolving the supplied name |
Definition at line 45 of file address.cc.
Address::Address | ( | const std::string & | hostname, |
const std::string & | service | ||
) |
Construct by resolving a hostname and servicename.
[in] | hostname | to resolve |
[in] | service | name (from /etc/services , e.g., "http" is port 80) |
Definition at line 80 of file address.cc.
Address::Address | ( | const std::string & | ip, |
const std::uint16_t | port = 0 |
||
) |
Construct from dotted-quad string ("18.243.0.1") and numeric port.
[in] | ip | address as a dotted quad ("1.1.1.1") |
[in] | port | number |
Definition at line 85 of file address.cc.
Address::Address | ( | const sockaddr * | addr, |
const std::size_t | size | ||
) |
Construct from a sockaddr *.
[in] | addr | points to a raw socket address |
[in] | size | is addr 's length |
Definition at line 22 of file address.cc.
Create an Address from a 32-bit raw numeric IP address.
Definition at line 119 of file address.cc.
|
inline |
Dotted-quad IP address string ("18.243.0.1").
Definition at line 51 of file address.hh.
Dotted-quad IP address string ("18.243.0.1") and numeric port.
Definition at line 90 of file address.cc.
uint32_t Address::ipv4_numeric | ( | ) | const |
Numeric IP address as an integer (i.e., in host byte order).
Definition at line 108 of file address.cc.
|
inline |
Const pointer to the underlying socket address storage.
Definition at line 68 of file address.hh.
|
inline |
Definition at line 43 of file address.hh.
bool Address::operator== | ( | const Address & | other | ) | const |
Equality comparison.
Definition at line 128 of file address.cc.
|
inline |
Numeric port (host byte order).
Definition at line 53 of file address.hh.
|
inline |
Size of the underlying address storage.
Definition at line 66 of file address.hh.
string Address::to_string | ( | ) | const |
Human-readable string, e.g., "8.8.8.8:53".
Definition at line 103 of file address.cc.
|
private |
A wrapped sockaddr_storage containing the address.
Definition at line 26 of file address.hh.
|
private |
Size of the wrapped address.
Definition at line 25 of file address.hh.