29 static bool is4(
const sockaddr * p )
31 return p && p->sa_family == GNet::Address4::family() ;
33 static bool is4(
const std::string & s )
36 return GNet::Address4::validString(s,&r) ;
38 static bool is4(
const std::string & s ,
unsigned int )
41 return GNet::Address4::validStrings(s,
"0",&r) ;
52 return Address( Family::ipv4() , 0U ) ;
56 m_4imp( f==Family::ipv4() ? new Address4(port) : nullptr ) ,
57 m_6imp( f!=Family::ipv4() ? new Address6(port) : nullptr )
62 m_4imp( is4(storage.p()) ? new Address4(storage.p(),storage.n()) : nullptr ) ,
63 m_6imp( !is4(storage.p()) ? new Address6(storage.p(),storage.n()) : nullptr )
68 m_4imp( is4(addr) ? new Address4(addr,len) : nullptr ) ,
69 m_6imp( !is4(addr) ? new Address6(addr,len) : nullptr )
74 m_4imp( is4(s) ? new Address4(s) : nullptr ) ,
75 m_6imp( !is4(s) ? new Address6(s) : nullptr )
80 m_4imp( is4(s,port) ? new Address4(s,port) : nullptr ) ,
81 m_6imp( !is4(s,port) ? new Address6(s,port) : nullptr )
86 m_4imp( other.m_4imp ? new Address4(*other.m_4imp) : nullptr ) ,
87 m_6imp( other.m_6imp ? new Address6(*other.m_6imp) : nullptr )
92 m_4imp( f==Family::ipv4() ? new Address4(port,loopback_overload) : nullptr ) ,
93 m_6imp( f!=Family::ipv4() ? new Address6(port,loopback_overload) : nullptr )
99 return Address( f , port , 1 ) ;
104 Address temp( addr ) ;
105 std::swap( m_4imp , temp.m_4imp ) ;
106 std::swap( m_6imp , temp.m_6imp ) ;
117 m_4imp ? m_4imp->setPort(port) : m_6imp->setPort(port) ;
123 ( m_4imp && m_4imp->isLoopback() ) ||
124 ( m_6imp && m_6imp->isLoopback() ) ;
130 ( m_4imp && m_4imp->isLocal(reason) ) ||
131 ( m_6imp && m_6imp->isLocal(reason) ) ;
137 ( m_4imp && other.m_4imp && m_4imp->same(*other.m_4imp) ) ||
138 ( m_6imp && other.m_6imp && m_6imp->same(*other.m_6imp) ) ;
143 return !( *
this == other ) ;
149 ( m_4imp && other.m_4imp && m_4imp->sameHostPart(*other.m_4imp) ) ||
150 ( m_6imp && other.m_6imp && m_6imp->sameHostPart(*other.m_6imp) ) ;
155 return m_4imp ? m_4imp->displayString() : m_6imp->displayString() ;
160 return m_4imp ? m_4imp->hostPartString() : m_6imp->hostPartString() ;
165 return Address4::validString( s , reason_p ) || Address6::validString( s , reason_p ) ;
170 return Address4::validStrings( s1 , s2 , reason_p ) || Address6::validStrings( s1 , s2 , reason_p ) ;
175 return m_4imp ? m_4imp->address() : m_6imp->address() ;
180 return m_4imp ? m_4imp->address() : m_6imp->address() ;
185 return m_4imp ? Address4::length() : Address6::length() ;
190 return m_4imp ? m_4imp->port() : m_6imp->port() ;
195 return m_4imp ? default_ : m_6imp->scopeId() ;
200 return Address4::validPort( port ) ;
205 return Address4::validData( addr , len ) || Address6::validData( addr , len ) ;
210 return m_4imp ? Address4::domain() : Address6::domain() ;
215 return m_4imp ? Family::ipv4() : Family::ipv6() ;
220 return m_4imp ? m_4imp->wildcards() : m_6imp->wildcards() ;
231 Address6::union_type u ;
238 m_imp(new AddressStorageImp)
240 m_imp->n =
sizeof(Address6::union_type) ;
250 return &(m_imp->u.general) ;
260 return &m_imp->u.general ;
272 int GNet::inet_pton_imp(
int f ,
const char * p ,
void * result )
274 if( p ==
nullptr || result ==
nullptr )
return 0 ;
275 struct addrinfo ai_hint ;
276 std::memset( &ai_hint , 0 ,
sizeof(ai_hint) ) ;
277 ai_hint.ai_family = f ;
278 ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV ;
279 struct addrinfo * ai_p = nullptr ;
280 int rc = getaddrinfo( p ,
nullptr , &ai_hint , &ai_p ) ;
281 bool ok = rc == 0 && ai_p != nullptr ;
282 if( ok && ai_p->ai_addr ==
nullptr ) { freeaddrinfo(ai_p) ;
return 1 ; }
285 struct sockaddr * sa_p = ai_p->ai_addr ;
286 if( ai_p->ai_family == AF_INET )
288 struct sockaddr_in sa = *
reinterpret_cast<struct sockaddr_in*
>(sa_p) ;
289 *
reinterpret_cast<struct in_addr*
>(result) = sa.sin_addr ;
291 else if( ai_p->ai_family == AF_INET6 )
293 struct sockaddr_in6 sa = *
reinterpret_cast<struct sockaddr_in6*
>(sa_p) ;
294 *
reinterpret_cast<struct in6_addr*
>(result) = sa.sin6_addr ;
298 freeaddrinfo( ai_p ) ;
303 #if ! GCONFIG_HAVE_INET_NTOP
305 const char * GNet::inet_ntop_imp(
int f ,
void * ap ,
char * buffer ,
size_t n )
310 std::ostringstream ss ;
311 struct in_addr a = *
reinterpret_cast<struct in_addr*
>(ap) ;
312 ss << inet_ntoa( a ) ;
315 else if( f == AF_INET6 )
317 struct in6_addr a = *
reinterpret_cast<struct in6_addr*
>(ap) ;
318 std::ostringstream ss ;
319 const char * sep =
":" ;
320 const char * hexmap =
"0123456789abcdef" ;
321 for(
int i = 0 ; i < 16 ; i++ , sep = *sep ?
"" :
":" )
323 unsigned int n =
static_cast<unsigned int>(a.s6_addr[i]) % 256U ;
324 ss << sep << hexmap[(n>>4U)%16U] << hexmap[(n&15U)%16U] ;
328 for( std::string::size_type pos = s.find(
":0") ; pos != std::string::npos ; pos = s.find(
":0",pos) )
330 if( s.find(
":0:",pos) == std::string::npos )
331 s.erase( pos+1U , 1U ) ;
335 std::string run =
":0:0:0:0:0:0:0:0:" ;
336 while( run.length() >= 3U )
338 std::string::size_type pos = s.find( run ) ;
339 if( pos != std::string::npos )
341 std::string::size_type r = 2U ;
342 if( pos == 0U ) r++ ;
343 if( (pos + run.length()) == s.length() ) r++ ;
344 s.replace( pos , run.length() , std::string(
"::::").substr(0U,r) ) ;
347 run.erase( 0U , 2U ) ;
350 s.erase( s.length()-1U , 1U ) ;
356 if( n <= s.length() )
return nullptr ;
357 std::strncpy( buffer , s.c_str() , n ) ;
static Address loopback(Family, unsigned int port=0U)
Returns a loopback address.
Family family() const
Returns the address family.
static bool validData(const sockaddr *, socklen_t len)
Returns true if the sockaddr data is valid.
The GNet::Address class encapsulates a TCP/UDP transport address.
bool isLoopback() const
Returns true if this is a loopback 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.
std::vector< std::string > StringArray
A std::vector of std::strings.
socklen_t * p2()
Returns the length pointer for accept()/getsockname()/getpeername() to write into.
sockaddr * p1()
Returns the sockaddr pointer for accept()/getsockname()/getpeername() to write into.
A pimple-pattern implementation class used by GNet::AddressStorage.
const sockaddr * address() const
Returns the sockaddr address.
bool sameHostPart(const Address &other) const
Returns true if the two addresses have the same host part (ie.
unsigned int port() const
Returns port part of the address.
bool operator==(const Address &) const
Comparison operator.
const sockaddr * p() const
Returns the pointer.
AddressStorage()
Default constructor.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
G::StringArray wildcards() const
Returns an ordered list of wildcard strings that match this address.
socklen_t length() const
Returns the size of the sockaddr address. See address().
std::string hostPartString() const
Returns a string which represents the network address for debugging and diagnostics purposes...
unsigned long scopeId(unsigned long default_=0UL) const
Returns the scope-id.
static bool validString(const std::string &display_string, std::string *reason=nullptr)
Returns true if the transport-address display string is valid.
int domain() const
Returns the address 'domain', eg. PF_INET.
~AddressStorage()
Destructor.
static bool validPort(unsigned int n)
Returns true if the port number is within the valid range.
A type-safe enumerator for IP address family.
This file is formatted for side-by-side comparison with gaddress4.h.
bool isLocal(std::string &reason) const
Returns true if this seems to be a local address (for some definition of 'local').
void operator=(const Address &addr)
Assignment operator.
Address(const Address &)
Copy constructor.
This file is formatted for side-by-side comparison with gaddress6.h.
void setPort(unsigned int port)
Sets the port number.
socklen_t n() const
Returns the length.
static bool supports(Family)
Returns true if the implementation supports the given address family.
bool operator!=(const Address &) const
Comparison operator.
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...