31 #include <sys/types.h>
38 const char * port_separators =
":/" ;
39 char port_separator =
':' ;
42 unsigned short GNet::Address4::family()
47 int GNet::Address4::domain()
52 void GNet::Address4::init()
54 static specific_type zero ;
55 m_inet.specific = zero ;
56 m_inet.specific.sin_family = family() ;
57 m_inet.specific.sin_port = 0 ;
62 GNet::Address4::Address4(
unsigned int port )
65 m_inet.specific.sin_addr.s_addr = htonl(INADDR_ANY);
66 const char * reason = setPort( m_inet , port ) ;
67 if( reason )
throw Address::Error(reason) ;
70 GNet::Address4::Address4(
unsigned int port ,
int )
73 m_inet.specific.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
74 const char * reason = setPort( m_inet , port ) ;
75 if( reason )
throw Address::Error(reason) ;
78 GNet::Address4::Address4(
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::Address4::Address4(
const Address4 & other )
91 m_inet.specific = other.m_inet.specific ;
94 GNet::Address4::Address4(
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::Address4::Address4(
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::Address4::Address4(
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::Address4::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::Address4::setHostAddress( union_type & inet ,
const std::string & host_part )
140 if( !Address4::format(host_part) )
141 return "invalid ipv4 network address format" ;
143 int rc = inet_pton( family() , host_part.c_str() , &inet.specific.sin_addr ) ;
144 return rc == 1 ?
nullptr :
"invalid ipv4 network address" ;
147 void GNet::Address4::setPort(
unsigned int port )
149 const char * reason = setPort( m_inet , port ) ;
151 throw Address::Error(
"invalid port number" ) ;
154 const char * GNet::Address4::setPort( union_type & inet ,
const std::string & port_part )
156 if( port_part.length() == 0U )
return "empty port string" ;
161 const char * GNet::Address4::setPort( union_type & inet ,
unsigned int port )
163 if( port > 0xFFFFU )
return "port number too big" ;
164 const g_port_t in_port =
static_cast<g_port_t
>(port) ;
165 inet.specific.sin_port = htons( in_port ) ;
169 std::string GNet::Address4::displayString()
const
171 std::ostringstream ss ;
172 ss << hostPartString() ;
173 ss << port_separator << port() ;
177 std::string GNet::Address4::hostPartString()
const
179 char buffer[INET_ADDRSTRLEN+1U] ;
180 const void * vp = & m_inet.specific.sin_addr ;
181 const char * p = inet_ntop( family() , const_cast<void*>(vp) , buffer ,
sizeof(buffer) ) ;
183 throw Address::Error(
"inet_ntop() failure" ) ;
184 buffer[
sizeof(buffer)-1U] =
'\0' ;
185 return std::string(buffer) ;
188 bool GNet::Address4::validData(
const sockaddr * addr , socklen_t len )
190 return addr !=
nullptr && addr->sa_family == family() && len ==
sizeof(specific_type) ;
193 bool GNet::Address4::validString(
const std::string & s , std::string * reason_p )
196 const char * reason = setAddress( inet , s ) ;
197 if( reason && reason_p )
198 *reason_p = std::string(reason) ;
199 return reason == nullptr ;
202 bool GNet::Address4::validStrings(
const std::string & host_part ,
const std::string & port_part , std::string * reason_p )
205 const char * reason = setHostAddress( inet , host_part ) ;
207 reason = setPort( inet , port_part ) ;
208 if( reason && reason_p )
209 *reason_p = std::string(reason) ;
210 return reason == nullptr ;
213 bool GNet::Address4::validPort(
unsigned int port )
216 const char * reason = setPort( inet , port ) ;
217 return reason == nullptr ;
220 bool GNet::Address4::same(
const Address4 & other )
const
223 m_inet.specific.sin_family == family() &&
224 other.m_inet.specific.sin_family == family() &&
225 sameAddr( m_inet.specific.sin_addr , other.m_inet.specific.sin_addr ) &&
226 m_inet.specific.sin_port == other.m_inet.specific.sin_port ;
229 bool GNet::Address4::sameHostPart(
const Address4 & other )
const
232 m_inet.specific.sin_family == family() &&
233 other.m_inet.specific.sin_family == family() &&
234 sameAddr( m_inet.specific.sin_addr , other.m_inet.specific.sin_addr ) ;
237 bool GNet::Address4::sameAddr( const ::in_addr & a , const ::in_addr & b )
239 return a.s_addr == b.s_addr ;
242 unsigned int GNet::Address4::port()
const
244 return ntohs( m_inet.specific.sin_port ) ;
247 const sockaddr * GNet::Address4::address()
const
249 return & m_inet.general ;
252 sockaddr * GNet::Address4::address()
254 return & m_inet.general ;
257 socklen_t GNet::Address4::length()
259 return sizeof(specific_type) ;
264 std::string ip_string = hostPartString() ;
267 result.reserve( 38U ) ;
268 result.push_back( ip_string ) ;
274 G_ASSERT( part.size() == 4U ) ;
275 if( part.size() != 4U ||
289 std::string part_0_1_2 = part[0] ;
290 part_0_1_2.append( 1U ,
'.' ) ;
291 part_0_1_2.append( part[1] ) ;
292 part_0_1_2.append( 1U ,
'.' ) ;
293 part_0_1_2.append( part[2] ) ;
294 part_0_1_2.append( 1U ,
'.' ) ;
296 std::string part_0_1 = part[0] ;
297 part_0_1.append( 1U ,
'.' ) ;
298 part_0_1.append( part[1] ) ;
299 part_0_1.append( 1U ,
'.' ) ;
301 std::string part_0 = part[0] ;
302 part_0.append( 1U ,
'.' ) ;
304 const std::string empty ;
306 add( result , part_0_1_2 , n3 & 0xff ,
"/32" ) ;
307 add( result , part_0_1_2 , n3 & 0xfe ,
"/31" ) ;
308 add( result , part_0_1_2 , n3 & 0xfc ,
"/30" ) ;
309 add( result , part_0_1_2 , n3 & 0xf8 ,
"/29" ) ;
310 add( result , part_0_1_2 , n3 & 0xf0 ,
"/28" ) ;
311 add( result , part_0_1_2 , n3 & 0xe0 ,
"/27" ) ;
312 add( result , part_0_1_2 , n3 & 0xc0 ,
"/26" ) ;
313 add( result , part_0_1_2 , n3 & 0x80 ,
"/25" ) ;
314 add( result , part_0_1_2 , 0 ,
"/24" ) ;
315 add( result , part_0_1_2 ,
"*" ) ;
316 add( result , part_0_1 , n2 & 0xfe ,
".0/23" ) ;
317 add( result , part_0_1 , n2 & 0xfc ,
".0/22" ) ;
318 add( result , part_0_1 , n2 & 0xfc ,
".0/21" ) ;
319 add( result , part_0_1 , n2 & 0xf8 ,
".0/20" ) ;
320 add( result , part_0_1 , n2 & 0xf0 ,
".0/19" ) ;
321 add( result , part_0_1 , n2 & 0xe0 ,
".0/18" ) ;
322 add( result , part_0_1 , n2 & 0xc0 ,
".0/17" ) ;
323 add( result , part_0_1 , 0 ,
".0/16" ) ;
324 add( result , part_0_1 ,
"*.*" ) ;
325 add( result , part_0 , n1 & 0xfe ,
".0.0/15" ) ;
326 add( result , part_0 , n1 & 0xfc ,
".0.0/14" ) ;
327 add( result , part_0 , n1 & 0xf8 ,
".0.0/13" ) ;
328 add( result , part_0 , n1 & 0xf0 ,
".0.0/12" ) ;
329 add( result , part_0 , n1 & 0xe0 ,
".0.0/11" ) ;
330 add( result , part_0 , n1 & 0xc0 ,
".0.0/10" ) ;
331 add( result , part_0 , n1 & 0x80 ,
".0.0/9" ) ;
332 add( result , part_0 , 0 ,
".0.0/8" ) ;
333 add( result , part_0 ,
"*.*.*" ) ;
334 add( result , empty , n0 & 0xfe ,
".0.0.0/7" ) ;
335 add( result , empty , n0 & 0xfc ,
".0.0.0/6" ) ;
336 add( result , empty , n0 & 0xf8 ,
".0.0.0/5" ) ;
337 add( result , empty , n0 & 0xf0 ,
".0.0.0/4" ) ;
338 add( result , empty , n0 & 0xe0 ,
".0.0.0/3" ) ;
339 add( result , empty , n0 & 0xc0 ,
".0.0.0/2" ) ;
340 add( result , empty , n0 & 0x80 ,
".0.0.0/1" ) ;
341 add( result , empty , 0 ,
".0.0.0/0" ) ;
342 add( result , empty ,
"*.*.*.*" ) ;
347 void GNet::Address4::add(
G::StringArray & result ,
const std::string & head ,
unsigned int n ,
const char * tail )
349 std::string s = head ;
352 result.push_back( s ) ;
355 void GNet::Address4::add(
G::StringArray & result ,
const std::string & head ,
const char * tail )
357 result.push_back( head + tail ) ;
360 bool GNet::Address4::format( std::string s )
364 if( s.empty() || s.find_first_not_of(
"0123456789.") != std::string::npos ||
365 std::count(s.begin(),s.end(),
'.') != 3U || s.at(0U) ==
'.' ||
366 s.at(s.length()-1U) ==
'.' || s.find(
"..") != std::string::npos )
369 unsigned int n = 0U ;
370 for( std::string::iterator pp = s.begin() ; pp != s.end() ; ++pp )
372 n = (*pp) ==
'.' ? 0U : ( ( n * 10U ) + (static_cast<unsigned int>(*pp)-
static_cast<unsigned int>(
'0')) ) ;
379 bool GNet::Address4::isLoopback()
const
382 return ( ntohl(m_inet.specific.sin_addr.s_addr) >> 24 ) == 127U ;
385 bool GNet::Address4::isLocal( std::string & reason )
const
396 std::ostringstream ss ;
397 ss << hostPartString() <<
" is not a loopback address" ;
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.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
This file is formatted for side-by-side comparison with gaddress6.h.
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
static void splitIntoFields(const std::string &in, StringArray &out, const std::string &separators, char escape= '\0', bool remove_escapes=true)
Splits the string into fields.