2828
2929#include < openvpn/io/io.hpp>
3030
31- #include < openvpn/common/size.hpp>
3231#include < openvpn/common/exception.hpp>
3332#include < openvpn/common/endian.hpp>
3433#include < openvpn/common/ostream.hpp>
35- #include < openvpn/common/socktypes.hpp>
3634#include < openvpn/common/ffs.hpp>
3735#include < openvpn/common/hexstr.hpp>
38- #include < openvpn/common/hash.hpp>
3936#include < openvpn/addr/iperr.hpp>
4037
41- namespace openvpn {
42- namespace IP {
38+ namespace openvpn ::IP {
4339class Addr ;
4440}
4541
4642// Fundamental classes for representing an IPv4 IP address.
4743
48- namespace IPv4 {
44+ namespace openvpn :: IPv4 {
4945
5046OPENVPN_EXCEPTION (ipv4_exception);
5147
@@ -67,7 +63,7 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
6763 return 4 ;
6864 }
6965
70- bool defined () const
66+ static constexpr bool defined ()
7167 {
7268 return true ;
7369 }
@@ -77,31 +73,30 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
7773 return addr;
7874 }
7975
80- static Addr from_in_addr (const struct in_addr *in4)
76+ static Addr from_in_addr (const in_addr *in4)
8177 {
8278 Addr ret;
8379 ret.u .addr = ntohl (in4->s_addr );
8480 return ret;
8581 }
8682
87- struct in_addr to_in_addr () const
83+ in_addr to_in_addr () const
8884 {
89- struct in_addr ret;
85+ in_addr ret;
9086 ret.s_addr = htonl (u.addr );
9187 return ret;
9288 }
9389
94- static Addr from_sockaddr (const struct sockaddr_in *sa)
90+ static Addr from_sockaddr (const sockaddr_in *sa)
9591 {
9692 Addr ret;
9793 ret.u .addr = ntohl (sa->sin_addr .s_addr );
9894 return ret;
9995 }
10096
101- struct sockaddr_in to_sockaddr (const unsigned short port = 0 ) const
97+ sockaddr_in to_sockaddr (const unsigned short port = 0 ) const
10298 {
103- struct sockaddr_in ret;
104- std::memset (&ret, 0 , sizeof (ret));
99+ sockaddr_in ret = {};
105100 ret.sin_family = AF_INET;
106101 ret.sin_port = htons (port);
107102 ret.sin_addr .s_addr = htonl (u.addr );
@@ -129,7 +124,7 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
129124
130125 void to_byte_string (unsigned char *bytestr) const
131126 {
132- *( base_type *) bytestr = ntohl (u.addr );
127+ *reinterpret_cast < base_type *>( bytestr) = ntohl (u.addr );
133128 }
134129
135130 std::uint32_t to_uint32_net () const // return value in net byte order
@@ -140,27 +135,25 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
140135 static Addr from_ulong (unsigned long ul)
141136 {
142137 Addr ret;
143- ret.u .addr = ( base_type)ul ;
138+ ret.u .addr = static_cast < base_type>(ul) ;
144139 return ret;
145140 }
146141
147- // return *this as a unsigned long
148142 unsigned long to_ulong () const
149143 {
150- return ( unsigned long ) u.addr ;
144+ return u.addr ;
151145 }
152146
153147 static Addr from_long (long ul)
154148 {
155149 Addr ret;
156- ret.u .addr = ( base_type)(signed_base_type)ul ;
150+ ret.u .addr = static_cast < base_type>(ul) ;
157151 return ret;
158152 }
159153
160- // return *this as a long
161154 long to_long () const
162155 {
163- return ( long )(signed_base_type) u.addr ;
156+ return u.addr ;
164157 }
165158
166159 static Addr from_bytes (const unsigned char *bytes) // host byte order
@@ -199,16 +192,14 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
199192 return ret;
200193 }
201194
202- // build a netmask using given prefix_len
203195 static Addr netmask_from_prefix_len (const unsigned int prefix_len)
204196 {
205197 Addr ret;
206198 ret.u .addr = prefix_len_to_netmask (prefix_len);
207199 return ret;
208200 }
209201
210- // build a netmask using given extent
211- Addr netmask_from_extent () const
202+ Addr netmask_from_this_as_extent () const
212203 {
213204 const int lb = find_last_set (u.addr - 1 );
214205 return netmask_from_prefix_len (SIZE - lb);
@@ -267,7 +258,7 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
267258 if (len < 1 || len > 8 )
268259 throw ipv4_exception (" parse hex error" );
269260 size_t di = (len - 1 ) >> 1 ;
270- for (int i = (len & 1 ) ? -1 : 0 ; i < int (len); i += 2 )
261+ for (int i = (len & 1 ) ? -1 : 0 ; i < static_cast < int > (len); i += 2 )
271262 {
272263 const size_t idx = base + i;
273264 const int bh = (i >= 0 ) ? parse_hex_char (s[idx]) : 0 ;
@@ -302,17 +293,17 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
302293 std::string arpa () const
303294 {
304295 std::ostringstream os;
305- os << int (u.bytes [Endian::e4 (0 )]) << ' .'
306- << int (u.bytes [Endian::e4 (1 )]) << ' .'
307- << int (u.bytes [Endian::e4 (2 )]) << ' .'
308- << int (u.bytes [Endian::e4 (3 )]) << " .in-addr.arpa" ;
296+ os << static_cast < int > (u.bytes [Endian::e4 (0 )]) << ' .'
297+ << static_cast < int > (u.bytes [Endian::e4 (1 )]) << ' .'
298+ << static_cast < int > (u.bytes [Endian::e4 (2 )]) << ' .'
299+ << static_cast < int > (u.bytes [Endian::e4 (3 )]) << " .in-addr.arpa" ;
309300 return os.str ();
310301 }
311302
312303 static Addr from_asio (const openvpn_io::ip::address_v4 &asio_addr)
313304 {
314305 Addr ret;
315- ret.u .addr = (std:: uint32_t ) asio_addr.to_uint ();
306+ ret.u .addr = asio_addr.to_uint ();
316307 return ret;
317308 }
318309
@@ -338,7 +329,7 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
338329 Addr operator +(const long delta) const
339330 {
340331 Addr ret;
341- ret.u .addr = u.addr + (std:: uint32_t ) delta;
332+ ret.u .addr = u.addr + static_cast <base_type>( delta) ;
342333 return ret;
343334 }
344335
@@ -473,8 +464,7 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
473464 const int ret = prefix_len_32 (u.addr );
474465 if (ret >= 0 )
475466 return ret;
476- else
477- throw ipv4_exception (" malformed netmask" );
467+ throw ipv4_exception (" malformed netmask" );
478468 }
479469
480470 int prefix_len_nothrow () const
@@ -501,40 +491,36 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
501491 const unsigned int hl = host_len ();
502492 if (hl < SIZE)
503493 return 1 << hl;
504- else if (hl == SIZE)
494+ if (hl == SIZE)
505495 return 0 ;
506- else
507- throw ipv4_exception (" extent overflow" );
496+ throw ipv4_exception (" extent overflow" );
508497 }
509498
510499 // convert netmask in addr to prefix_len, will return -1 on error
511500 static int prefix_len_32 (const std::uint32_t addr)
512501 {
513502 if (addr == ~std::uint32_t (0 ))
514503 return 32 ;
515- else if (addr == 0 )
504+ if (addr == 0 )
516505 return 0 ;
517- else
506+ unsigned int high = 32 ;
507+ unsigned int low = 1 ;
508+ for (unsigned int i = 0 ; i < 5 ; ++i)
518509 {
519- unsigned int high = 32 ;
520- unsigned int low = 1 ;
521- for (unsigned int i = 0 ; i < 5 ; ++i)
522- {
523- const unsigned int mid = (high + low) / 2 ;
524- const IPv4::Addr::base_type test = prefix_len_to_netmask_unchecked (mid);
525- if (addr == test)
526- return mid;
527- else if (addr > test)
528- low = mid;
529- else
530- high = mid;
531- }
532- return -1 ;
510+ const unsigned int mid = (high + low) / 2 ;
511+ const IPv4::Addr::base_type test = prefix_len_to_netmask_unchecked (mid);
512+ if (addr == test)
513+ return mid;
514+ if (addr > test)
515+ low = mid;
516+ else
517+ high = mid;
533518 }
519+ return -1 ;
534520 }
535521
536522 // address size in bits
537- static unsigned int size ()
523+ static constexpr unsigned int size ()
538524 {
539525 return SIZE;
540526 }
@@ -596,20 +582,18 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
596582 }
597583
598584 private:
599- static base_type prefix_len_to_netmask_unchecked (const unsigned int prefix_len)
585+ static base_type prefix_len_to_netmask_unchecked (const unsigned int prefix_len) noexcept
600586 {
601587 if (prefix_len)
602588 return ~((1u << (SIZE - prefix_len)) - 1 );
603- else
604- return 0 ;
589+ return 0 ;
605590 }
606591
607592 static base_type prefix_len_to_netmask (const unsigned int prefix_len)
608593 {
609594 if (prefix_len <= SIZE)
610595 return prefix_len_to_netmask_unchecked (prefix_len);
611- else
612- throw ipv4_exception (" bad prefix len" );
596+ throw ipv4_exception (" bad prefix len" );
613597 }
614598
615599 union {
@@ -619,8 +603,7 @@ class Addr // NOTE: must be union-legal, so default constructor does not initial
619603};
620604
621605OPENVPN_OSTREAM (Addr, to_string)
622- } // namespace IPv4
623- } // namespace openvpn
606+ } // namespace openvpn::IPv4
624607
625608#ifdef USE_OPENVPN_HASH
626609OPENVPN_HASH_METHOD (openvpn::IPv4::Addr, hashval);
0 commit comments