21#ifndef G_NET_ADDRESS_H
22#define G_NET_ADDRESS_H
36 class AddressStorage ;
37 class AddressStorageImp ;
64 G_EXCEPTION( Error ,
"address error" ) ;
65 G_EXCEPTION( BadString ,
"invalid address" ) ;
66 G_EXCEPTION_CLASS( BadFamily ,
"unsupported address family" ) ;
86 Address(
const sockaddr * addr , socklen_t len ) ;
90 Address(
const sockaddr * addr , socklen_t len ,
bool fixup ) ;
115 static Address parse(
const std::string & display_string ) ;
123 static Address parse(
const std::string & host_part_string ,
unsigned int port ) ;
127 static Address parse(
const std::string & host_part_string ,
const std::string &
port ) ;
131 static bool isFamilyLocal(
const std::string & display_string ) ;
144 const sockaddr *
address()
const ;
155 std::string
displayString(
bool with_scope_id =
false )
const ;
168 unsigned int port()
const;
171 static int domain( Family ) ;
181 static bool validPort(
unsigned int n ) ;
185 static bool validString(
const std::string & display_string , std::string * reason =
nullptr ) ;
189 static bool validString(
const std::string & display_string ,
NotLocal , std::string * reason =
nullptr ) ;
193 static bool validStrings(
const std::string & ip ,
const std::string & port_string , std::string * reason =
nullptr ) ;
198 static bool validData(
const sockaddr * , socklen_t len ) ;
210 bool setZone(
const std::string & ) ;
221 unsigned long scopeId(
unsigned long default_ = 0UL )
const ;
231 unsigned int bits()
const ;
247 bool isLocal( std::string & reason )
const ;
258 bool same(
const Address & ,
bool ipv6_compare_with_scope )
const ;
271 Address( Family ,
unsigned int ,
int ) ;
272 explicit Address(
const std::string & display_string ) ;
274 Address(
const std::string & ip ,
const std::string &
port ) ;
275 Address(
const std::string & ip ,
unsigned int port ) ;
278 std::unique_ptr<Address4> m_ipv4 ;
279 std::unique_ptr<Address6> m_ipv6 ;
280 std::unique_ptr<AddressLocal> m_local ;
313 const sockaddr *
p()
const ;
316 socklen_t
n()
const ;
326 std::unique_ptr<AddressStorageImp> m_imp ;
A helper class for calling accept(), getsockname() and getpeername() and hiding the definition of soc...
sockaddr * p1()
Returns the sockaddr pointer for accept()/getsockname()/getpeername() to write into.
const sockaddr * p() const
Returns the pointer, typically set via p1().
socklen_t * p2()
Returns the length pointer for accept()/getsockname()/getpeername() to write into.
AddressStorage()
Default constructor, with n() reflecting the size of the largest supported address type.
~AddressStorage()
Destructor.
socklen_t n() const
Returns the length, typically modified via p2().
The GNet::Address class encapsulates a TCP/UDP transport address.
bool setZone(const std::string &)
Sets the zone.
Address(Address &&) noexcept
Move constructor.
Address & setScopeId(unsigned long)
Sets the scope-id.
bool is4() const
Returns true if family() is ipv4.
Family family() const
Returns the address family enumeration.
Address(const Address &)
Copy constructor.
int af() const
Returns the address family number such as AF_INET or AFINET6.
bool operator!=(const Address &) const
Comparison operator.
bool isLinkLocal() const
Returns true if this is a link-local address.
bool operator==(const Address &) const
Comparison operator.
std::string queryString() const
Returns a string that can be used as a prefix for rDNS or DNSBL queries.
G::StringArray wildcards() const
Returns an ordered list of wildcard strings that match this address.
static bool isFamilyLocal(const std::string &display_string)
Returns true if the given address display string looks will parse to Family::local and Family::local ...
static bool validData(const sockaddr *, socklen_t len)
Returns true if the sockaddr data is valid.
std::string hostPartString(bool raw=false) const
Returns a string which represents the network address.
static bool supports(Family)
Returns true if the implementation supports the given address family.
static Address loopback(Family, unsigned int port=0U)
Returns a loopback address.
static bool validPort(unsigned int n)
Returns true if the port number is within the valid range.
bool isUniqueLocal() const
Returns true if this is a locally administered address.
static bool validStrings(const std::string &ip, const std::string &port_string, std::string *reason=nullptr)
Returns true if the combined network-address string and port string is valid.
void swap(Address &other) noexcept
Swaps this with other.
socklen_t length() const
Returns the size of the sockaddr address. See address().
bool isLocal(std::string &reason) const
Returns true if this seems to be a 'local' address, ie.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
bool isAny() const
Returns true if this is the address family's 'any' address.
bool is6() const
Returns true if family() is ipv6.
static Address parse(const std::string &display_string)
Factory function for any address family.
static bool validString(const std::string &display_string, std::string *reason=nullptr)
Returns true if the transport-address display string is valid.
bool sameHostPart(const Address &other) const
Returns true if the two addresses have the same host part (ie.
unsigned int bits() const
Returns the number of leading bits set, relevant only to netmask addresses.
std::string displayString(bool with_scope_id=false) const
Returns a string which represents the transport address.
unsigned int port() const
Returns port part of the address.
bool isLoopback() const
Returns true if this is a loopback address.
const sockaddr * address() const
Returns the sockaddr address.
unsigned long scopeId(unsigned long default_=0UL) const
Returns the scope-id.
Address & setPort(unsigned int port)
Sets the port number.
static int domain(Family)
Returns the address 'domain' for the given family, eg.
bool same(const Address &, bool ipv6_compare_with_scope) const
Comparison function.
std::vector< std::string > StringArray
A std::vector of std::strings.
Overload discriminator for Address::supports()
Overload discriminator for Address::parse()