31 #include <sys/types.h>
38 const char * port_separators =
":/." ;
39 char port_separator =
'.' ;
42 unsigned short GNet::Address6::family()
47 int GNet::Address6::domain()
52 void GNet::Address6::init()
54 static specific_type zero ;
55 m_inet.specific = zero ;
56 m_inet.specific.sin6_family = family() ;
57 m_inet.specific.sin6_flowinfo = 0 ;
58 m_inet.specific.sin6_port = 0 ;
59 gnet_address6_init( m_inet.specific ) ;
62 GNet::Address6::Address6(
unsigned int port )
65 m_inet.specific.sin6_addr = in6addr_any ;
66 const char * reason = setPort( m_inet , port ) ;
67 if( reason )
throw Address::Error(reason) ;
70 GNet::Address6::Address6(
unsigned int port ,
int )
73 m_inet.specific.sin6_addr = in6addr_loopback ;
74 const char * reason = setPort( m_inet , port ) ;
75 if( reason )
throw Address::Error(reason) ;
78 GNet::Address6::Address6(
const sockaddr * addr , socklen_t len )
82 throw Address::Error() ;
83 if( addr->sa_family != family() || static_cast<size_t>(len) <
sizeof(specific_type) )
84 throw Address::BadFamily() ;
86 m_inet.specific = *(
reinterpret_cast<const specific_type*
>(addr)) ;
89 GNet::Address6::Address6(
const Address6 & other )
91 m_inet.specific = other.m_inet.specific ;
94 GNet::Address6::Address6(
const std::string & host_part ,
unsigned int port )
97 const char * reason = setHostAddress( m_inet , host_part ) ;
99 reason = setPort( m_inet , port ) ;
101 throw Address::BadString( std::string(reason) +
": " + host_part ) ;
104 GNet::Address6::Address6(
const std::string & host_part ,
const std::string & port_part )
107 const char * reason = setHostAddress( m_inet , host_part ) ;
109 reason = setPort( m_inet , port_part ) ;
111 throw Address::BadString( std::string(reason) +
": [" + host_part +
"][" + port_part +
"]" ) ;
114 GNet::Address6::Address6(
const std::string & display_string )
117 const char * reason = setAddress( m_inet , display_string ) ;
119 throw Address::BadString( std::string(reason) +
": " + display_string ) ;
122 const char * GNet::Address6::setAddress( union_type & inet ,
const std::string & display_string )
124 const std::string::size_type pos = display_string.find_last_of( port_separators ) ;
125 if( pos == std::string::npos )
126 return "no port separator" ;
128 std::string host_part =
G::Str::head( display_string , pos ) ;
129 std::string port_part =
G::Str::tail( display_string , pos ) ;
131 const char * reason = setHostAddress( inet , host_part ) ;
133 reason = setPort( inet , port_part ) ;
137 const char * GNet::Address6::setHostAddress( union_type & inet ,
const std::string & host_part )
149 std::string zone =
G::Str::tail( host_part , host_part.find(
'%') , std::string() ) ;
150 std::string host_part_head =
G::Str::head( host_part , host_part.find(
'%') , host_part ) ;
152 int rc = inet_pton( family() , host_part_head.c_str() , &inet.specific.sin6_addr ) ;
154 if( rc == 1 && !zone.empty() )
158 zone_id = if_nametoindex( zone.c_str() ) ;
160 setZone( inet , zone_id ) ;
163 return rc == 1 ?
nullptr :
"invalid network address" ;
166 void GNet::Address6::setPort(
unsigned int port )
168 const char * reason = setPort( m_inet , port ) ;
170 throw Address::Error(
"invalid port number" ) ;
173 const char * GNet::Address6::setPort( union_type & inet ,
const std::string & port_part )
175 if( port_part.length() == 0U )
return "empty port string" ;
180 const char * GNet::Address6::setPort( union_type & inet ,
unsigned int port )
182 if( port > 0xFFFFU )
return "port number too big" ;
183 const g_port_t in_port =
static_cast<g_port_t
>(port) ;
184 inet.specific.sin6_port = htons( in_port ) ;
188 void GNet::Address6::setZone( union_type & inet ,
unsigned int zone_id )
190 inet.specific.sin6_scope_id = zone_id ;
195 std::string GNet::Address6::displayString()
const
197 const bool with_scope_id = false ;
198 std::ostringstream ss ;
199 ss << hostPartString() ;
201 ss <<
"%" << scopeId() ;
202 ss << port_separator << port() ;
206 std::string GNet::Address6::hostPartString()
const
208 char buffer[INET6_ADDRSTRLEN+1U] ;
209 const void * vp = & m_inet.specific.sin6_addr ;
210 const char * p = inet_ntop( family() , const_cast<void*>(vp) , buffer ,
sizeof(buffer) ) ;
212 throw Address::Error(
"inet_ntop() failure" ) ;
213 buffer[
sizeof(buffer)-1U] =
'\0' ;
214 return std::string(buffer) ;
217 bool GNet::Address6::validData(
const sockaddr * addr , socklen_t len )
219 return addr !=
nullptr && addr->sa_family == family() && len ==
sizeof(specific_type) ;
222 bool GNet::Address6::validString(
const std::string & s , std::string * reason_p )
225 const char * reason = setAddress( inet , s ) ;
226 if( reason && reason_p )
227 *reason_p = std::string(reason) ;
228 return reason == nullptr ;
231 bool GNet::Address6::validStrings(
const std::string & host_part ,
const std::string & port_part , std::string * reason_p )
234 const char * reason = setHostAddress( inet , host_part ) ;
236 reason = setPort( inet , port_part ) ;
237 if( reason && reason_p )
238 *reason_p = std::string(reason) ;
239 return reason == nullptr ;
242 bool GNet::Address6::validPort(
unsigned int port )
245 const char * reason = setPort( inet , port ) ;
246 return reason == nullptr ;
249 bool GNet::Address6::same(
const Address6 & other )
const
252 m_inet.specific.sin6_family == family() &&
253 other.m_inet.specific.sin6_family == family() &&
254 sameAddr( m_inet.specific.sin6_addr , other.m_inet.specific.sin6_addr ) &&
255 m_inet.specific.sin6_port == other.m_inet.specific.sin6_port ;
258 bool GNet::Address6::sameHostPart(
const Address6 & other )
const
261 m_inet.specific.sin6_family == family() &&
262 other.m_inet.specific.sin6_family == family() &&
263 sameAddr( m_inet.specific.sin6_addr , other.m_inet.specific.sin6_addr ) ;
266 bool GNet::Address6::sameAddr( const ::in6_addr & a , const ::in6_addr & b )
268 for(
size_t i = 0 ; i < 16U ; i++ )
270 if( a.s6_addr[i] != b.s6_addr[i] )
276 unsigned int GNet::Address6::port()
const
278 return ntohs( m_inet.specific.sin6_port ) ;
281 unsigned long GNet::Address6::scopeId()
const
283 return m_inet.specific.sin6_scope_id ;
286 const sockaddr * GNet::Address6::address()
const
288 return &m_inet.general ;
291 sockaddr * GNet::Address6::address()
293 return &m_inet.general ;
296 socklen_t GNet::Address6::length()
298 return sizeof(specific_type) ;
303 void shiftLeft(
struct in6_addr & mask )
305 bool carry_in = false ;
306 for(
int i = 15 ; i >= 0 ; i-- )
308 const unsigned char top_bit = 128U ;
309 bool carry_out = !!( mask.s6_addr[i] & top_bit ) ;
310 mask.s6_addr[i] <<= 1U ;
311 if( carry_in ) ( mask.s6_addr[i] |= 1U ) ;
312 carry_in = carry_out ;
315 void shiftLeft(
struct in6_addr & mask ,
unsigned int bits )
317 for(
unsigned int i = 0U ; i < bits ; i++ )
320 void reset(
struct in6_addr & addr )
322 for(
unsigned int i = 0 ; i < 16U ; i++ )
323 addr.s6_addr[i] = 0 ;
325 void fill(
struct in6_addr & addr )
327 for(
unsigned int i = 0 ; i < 16U ; i++ )
328 addr.s6_addr[i] = 0xff ;
330 struct in6_addr make( unsigned int lhs_hi , unsigned int lhs_lo , unsigned int rhs )
332 struct in6_addr addr ;
334 addr.s6_addr[15] = rhs ;
335 addr.s6_addr[0] = lhs_hi ;
336 addr.s6_addr[1] = lhs_lo ;
339 void applyMask(
struct in6_addr & addr ,
const struct in6_addr & mask )
341 for(
int i = 0 ; i < 16 ; i++ )
343 addr.s6_addr[i] &= mask.s6_addr[i] ;
346 struct in6_addr mask( unsigned int bits )
348 struct in6_addr addr ;
350 shiftLeft( addr , 128U - bits ) ;
353 struct in6_addr masked( const struct in6_addr & addr_in ,
const struct in6_addr & mask )
355 struct in6_addr result = addr_in ;
356 applyMask( result , mask ) ;
363 Address6 a( *
this ) ;
366 result.reserve( 128U ) ;
367 result.push_back( hostPartString() ) ;
369 struct in6_addr mask ;
372 for(
int bit = 0 ; bit < 128 ; bit++ )
374 std::ostringstream ss ;
375 ss << a.hostPartString() <<
"/" << (128-bit) ;
376 result.push_back( ss.str() ) ;
379 applyMask( a.m_inet.specific.sin6_addr , mask ) ;
384 bool GNet::Address6::isLoopback()
const
387 struct in6_addr _1 = make( 0U , 0U , 1U ) ;
388 return sameAddr( _1 , m_inet.specific.sin6_addr ) ;
391 bool GNet::Address6::isLocal( std::string & reason )
const
393 struct in6_addr addr_128 = masked( m_inet.specific.sin6_addr , mask(128U) ) ;
394 struct in6_addr addr_64 = masked( m_inet.specific.sin6_addr , mask(64U) ) ;
395 struct in6_addr addr_7 = masked( m_inet.specific.sin6_addr , mask(7U) ) ;
397 struct in6_addr _1 = make( 0U , 0U , 1U ) ;
398 struct in6_addr _fe80 = make( 0xfeU , 0x80U , 0U ) ;
399 struct in6_addr _fc00 = make( 0xfcU , 0U , 0U ) ;
402 sameAddr( _1 , addr_128 ) ||
403 sameAddr( _fe80 , addr_64 ) ||
404 sameAddr( _fc00 , addr_7 ) ;
408 std::ostringstream ss ;
409 ss << hostPartString() <<
" is not ::1/128 or in fe80::/64 or fc00::/7" ;
static std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static std::string head(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
std::vector< std::string > StringArray
A std::vector of std::strings.
static bool isNumeric(const std::string &s, bool allow_minus_sign=false)
Returns true if every character is a decimal digit.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
This file is formatted for side-by-side comparison with gaddress4.h.
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...