uvco 0.1
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Attributes | Private Attributes | List of all members
uvco::AddressHandle Class Reference

AddressHandle is a light-weight wrapper around a struct sockaddr_in(6), and is therefore cheap to copy. More...

#include <name_resolution.h>

Classes

struct  NtopHelper_
 A helper for calling inet_ntop(3). More...
 

Public Member Functions

 AddressHandle ()=default
 
 AddressHandle (const AddressHandle &)=default
 
 AddressHandle (AddressHandle &&)=default
 
AddressHandleoperator= (const AddressHandle &)=default
 
AddressHandleoperator= (AddressHandle &&)=default
 
 ~AddressHandle ()=default
 
 AddressHandle (std::span< const uint8_t > ipv4_or_6, uint16_t port, uint32_t v6scope=0)
 
 AddressHandle (uint32_t ipv4, uint16_t port)
 
 AddressHandle (std::string_view ip, uint16_t port, uint32_t v6scope=0)
 
 AddressHandle (const struct addrinfo *ai)
 
 AddressHandle (const struct sockaddr *sa)
 
std::string address () const
 
uint16_t port () const
 
int family () const
 Family is either AF_INET or AF_INET6.
 
const struct sockaddr * sockaddr () const
 
std::string toString () const
 

Static Public Attributes

static constexpr size_t ipv4Length = 4
 
static constexpr size_t ipv6Length = 16
 

Private Attributes

std::variant< struct sockaddr_in, struct sockaddr_in6 > addr_
 

Detailed Description

AddressHandle is a light-weight wrapper around a struct sockaddr_in(6), and is therefore cheap to copy.

It can be constructed from different forms of TCP/IP addresses, and also supports formatting an address to a string.

In order to resolve a DNS hostname, use the Resolver class.

Constructor & Destructor Documentation

◆ AddressHandle() [1/8]

uvco::AddressHandle::AddressHandle ( )
default

◆ AddressHandle() [2/8]

uvco::AddressHandle::AddressHandle ( const AddressHandle )
default

◆ AddressHandle() [3/8]

uvco::AddressHandle::AddressHandle ( AddressHandle &&  )
default

◆ ~AddressHandle()

uvco::AddressHandle::~AddressHandle ( )
default

◆ AddressHandle() [4/8]

uvco::AddressHandle::AddressHandle ( std::span< const uint8_t >  ipv4_or_6,
uint16_t  port,
uint32_t  v6scope = 0 
)
31 {
32 if (ipv4_or_6.size() == ipv4Length) {
33 struct sockaddr_in addr {};
34 struct in_addr ipAddr {};
35 ipAddr.s_addr = *(uint32_t *)ipv4_or_6.data();
36
37 addr.sin_family = AF_INET;
38 addr.sin_port = htons(port);
39 addr.sin_addr = ipAddr;
40 addr_ = addr;
41 } else if (ipv4_or_6.size() == ipv6Length) {
42 struct sockaddr_in6 addr {};
43 struct in6_addr ipAddr {};
44
45 std::copy(ipv4_or_6.begin(), ipv4_or_6.end(),
46 static_cast<uint8_t *>(ipAddr.s6_addr));
47
48 addr.sin6_family = AF_INET6;
49 addr.sin6_port = htons(port);
50 addr.sin6_addr = ipAddr;
51 addr.sin6_scope_id = v6scope;
52 addr_ = addr;
53 } else {
54 throw UvcoException("Invalid address size for IPv4/6 address!");
55 }
56}
static constexpr size_t ipv4Length
Definition name_resolution.h:42
std::variant< struct sockaddr_in, struct sockaddr_in6 > addr_
Definition name_resolution.h:73
uint16_t port() const
Definition name_resolution.cc:67
static constexpr size_t ipv6Length
Definition name_resolution.h:43

◆ AddressHandle() [5/8]

uvco::AddressHandle::AddressHandle ( uint32_t  ipv4,
uint16_t  port 
)
122 : AddressHandle{std::span<const uint8_t>{(uint8_t *)(&ipv4), 4}, port} {}
AddressHandle()=default

◆ AddressHandle() [6/8]

uvco::AddressHandle::AddressHandle ( std::string_view  ip,
uint16_t  port,
uint32_t  v6scope = 0 
)
92 {
93 if (ip.contains(':')) {
94 struct in6_addr ipAddr {};
95 uv_status status = inet_pton(AF_INET6, ip.data(), &ipAddr);
96 if (status != 1) {
97 throw UvcoException(fmt::format("invalid IPv6 address: {}", ip));
98 }
99
100 struct sockaddr_in6 addr {};
101 addr.sin6_family = AF_INET6;
102 addr.sin6_addr = ipAddr;
103 addr.sin6_port = htons(port);
104 addr.sin6_scope_id = v6scope;
105 addr_ = addr;
106 } else {
107 struct in_addr ipAddr;
108 uv_status status = inet_pton(AF_INET, ip.data(), &ipAddr);
109 if (status != 1) {
110 throw UvcoException(fmt::format("invalid IPv4 address: {}", ip));
111 }
112
113 struct sockaddr_in addr {};
114 addr.sin_family = AF_INET;
115 addr.sin_addr = ipAddr;
116 addr.sin_port = htons(port);
117 addr_ = addr;
118 }
119}
int uv_status
Result of a libuv operation, an errno error code.
Definition internal_utils.h:22

◆ AddressHandle() [7/8]

uvco::AddressHandle::AddressHandle ( const struct addrinfo *  ai)
explicit
124 {
125 if (ai->ai_family == AF_INET) {
126 BOOST_ASSERT(ai->ai_addrlen >= sizeof(struct sockaddr_in));
127 addr_ = *(struct sockaddr_in *)ai->ai_addr;
128 } else if (ai->ai_family == AF_INET6) {
129 BOOST_ASSERT(ai->ai_addrlen >= sizeof(struct sockaddr_in6));
130 addr_ = *(struct sockaddr_in6 *)ai->ai_addr;
131 }
132}

◆ AddressHandle() [8/8]

uvco::AddressHandle::AddressHandle ( const struct sockaddr sa)
explicit
134 {
135 int af = sa->sa_family;
136 if (af == AF_INET) {
137 const auto *addr = (struct sockaddr_in *)sa;
138 addr_ = *addr;
139 } else if (af == AF_INET6) {
140 const auto *addr = (struct sockaddr_in6 *)sa;
141 addr_ = *addr;
142 } else {
143 throw UvcoException(fmt::format("unknown address family {}", af));
144 }
145}

Member Function Documentation

◆ address()

std::string uvco::AddressHandle::address ( ) const
inline
60 {
61 return std::visit(NtopHelper_{}, addr_);
62 }

◆ family()

int uvco::AddressHandle::family ( ) const

Family is either AF_INET or AF_INET6.

76 {
77 if (addr_.index() == 0)
78 return AF_INET;
79 if (addr_.index() == 1)
80 return AF_INET6;
81 throw UvcoException("family(): unknown address variant!");
82}

◆ operator=() [1/2]

AddressHandle & uvco::AddressHandle::operator= ( AddressHandle &&  )
default

◆ operator=() [2/2]

AddressHandle & uvco::AddressHandle::operator= ( const AddressHandle )
default

◆ port()

uint16_t uvco::AddressHandle::port ( ) const
67 {
68 if (addr_.index() == 0) {
69 const auto &addr = std::get<0>(addr_);
70 return ntohs(addr.sin_port);
71 } else {
72 const auto &addr = std::get<1>(addr_);
73 return ntohs(addr.sin6_port);
74 }
75}

◆ sockaddr()

const struct sockaddr * uvco::AddressHandle::sockaddr ( ) const

The inner sockaddr struct. May refer to either a sockaddr_in or a sockaddr_in6, depending on family().

83 {
84 return std::visit(
85 [](const auto &sockaddr) -> const struct sockaddr * {
86 return (const struct sockaddr *)&sockaddr;
87 },
88 addr_);
89}
const struct sockaddr * sockaddr() const
Definition name_resolution.cc:83

◆ toString()

std::string uvco::AddressHandle::toString ( ) const
58 {
59 if (family() == AF_INET) {
60 return fmt::format("{}:{}", address(), port());
61 }
62 if (family() == AF_INET6) {
63 return fmt::format("[{}]:{}", address(), port());
64 }
65 return {};
66}
std::string address() const
Definition name_resolution.h:60
int family() const
Family is either AF_INET or AF_INET6.
Definition name_resolution.cc:76

Member Data Documentation

◆ addr_

std::variant<struct sockaddr_in, struct sockaddr_in6> uvco::AddressHandle::addr_
private

◆ ipv4Length

constexpr size_t uvco::AddressHandle::ipv4Length = 4
staticconstexpr

◆ ipv6Length

constexpr size_t uvco::AddressHandle::ipv6Length = 16
staticconstexpr

The documentation for this class was generated from the following files: