Sponge
CS144's user-space TCP library
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Address Class Reference

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_tip_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...
 

Detailed Description

Wrapper around IPv4 addresses and DNS operations.

For example, you can do DNS lookups:

const Address google_webserver("www.google.com", "https");

or you can specify an IP address and port number:

const Address a_dns_server("18.71.0.151", 53);

Once you have an address, you can convert it to other useful representations, e.g.,

Definition at line 13 of file address.hh.

Constructor & Destructor Documentation

◆ Address() [1/4]

Address::Address ( const std::string node,
const std::string service,
const addrinfo &  hints 
)
private

Constructor from ip/host, service/port, and hints to the resolver.

Parameters
[in]nodeis the hostname or dotted-quad address
[in]serviceis the service name or numeric string
[in]hintsare criteria for resolving the supplied name

Definition at line 45 of file address.cc.

◆ Address() [2/4]

Address::Address ( const std::string hostname,
const std::string service 
)

Construct by resolving a hostname and servicename.

Parameters
[in]hostnameto resolve
[in]servicename (from /etc/services, e.g., "http" is port 80)

Definition at line 80 of file address.cc.

◆ Address() [3/4]

Address::Address ( const std::string ip,
const std::uint16_t  port = 0 
)

Construct from dotted-quad string ("18.243.0.1") and numeric port.

Parameters
[in]ipaddress as a dotted quad ("1.1.1.1")
[in]portnumber

Definition at line 85 of file address.cc.

◆ Address() [4/4]

Address::Address ( const sockaddr *  addr,
const std::size_t  size 
)

Construct from a sockaddr *.

Parameters
[in]addrpoints to a raw socket address
[in]sizeis addr's length

Definition at line 22 of file address.cc.

Member Function Documentation

◆ from_ipv4_numeric()

Address Address::from_ipv4_numeric ( const uint32_t  ip_address)
static

Create an Address from a 32-bit raw numeric IP address.

Definition at line 119 of file address.cc.

◆ ip()

std::string Address::ip ( ) const
inline

Dotted-quad IP address string ("18.243.0.1").

Definition at line 51 of file address.hh.

◆ ip_port()

pair< string, uint16_t > Address::ip_port ( ) const

Dotted-quad IP address string ("18.243.0.1") and numeric port.

Definition at line 90 of file address.cc.

◆ ipv4_numeric()

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.

◆ operator const sockaddr *()

Address::operator const sockaddr * ( ) const
inline

Const pointer to the underlying socket address storage.

Definition at line 68 of file address.hh.

◆ operator!=()

bool Address::operator!= ( const Address other) const
inline

Definition at line 43 of file address.hh.

◆ operator==()

bool Address::operator== ( const Address other) const

Equality comparison.

Definition at line 128 of file address.cc.

◆ port()

uint16_t Address::port ( ) const
inline

Numeric port (host byte order).

Definition at line 53 of file address.hh.

◆ size()

socklen_t Address::size ( ) const
inline

Size of the underlying address storage.

Definition at line 66 of file address.hh.

◆ to_string()

string Address::to_string ( ) const

Human-readable string, e.g., "8.8.8.8:53".

Definition at line 103 of file address.cc.

Member Data Documentation

◆ _address

Raw Address::_address {}
private

A wrapped sockaddr_storage containing the address.

Definition at line 26 of file address.hh.

◆ _size

socklen_t Address::_size
private

Size of the wrapped address.

Definition at line 25 of file address.hh.


The documentation for this class was generated from the following files:
a_dns_server
const Address a_dns_server("18.71.0.151", 53)
google_webserver
const Address google_webserver("www.google.com", "https")
a_dns_server_numeric
const uint32_t a_dns_server_numeric
Definition: address_example_3.cc:1
Address
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
Address::ipv4_numeric
uint32_t ipv4_numeric() const
Numeric IP address as an integer (i.e., in host byte order).
Definition: address.cc:108