11<?php namespace peer \net ;
22
3+ use lang \FormatException ;
4+
35/**
46 * IPv4 address
57 *
911class Inet4Address implements InetAddress {
1012
1113 /**
12- * Convert IPv4 address from dotted form into a long
14+ * Convert IPv4 address from dotted form into a long. Supports hexadecimal and
15+ * octal notations. Yes, 0177.0.0.1 and 0x7F.0.0.1 are both equivalent with
16+ * 127.0.0.1 - localhost!
1317 *
14- * @param string ip
15- * @return int
18+ * @param string $ip
19+ * @return int
20+ * @throws lang.FormatException
1621 */
1722 protected static function ip2long ($ ip ) {
1823 $ i = 0 ; $ addr = 0 ; $ count = 0 ;
1924 foreach (explode ('. ' , $ ip ) as $ byte ) {
20- if (++$ count > 4 )
21- throw new \lang \FormatException ('Given IP string has more than 4 blocks: [ ' .$ ip .'] ' );
25+ if (++$ count > 4 ) {
26+ throw new FormatException ('Given IP string has more than 4 blocks: [ ' .$ ip .'] ' );
27+ }
28+
29+ $ l = strlen ($ byte );
30+ $ n = -1 ;
31+ if ($ l > 1 && '0 ' === $ byte [0 ]) {
32+ if (('x ' === $ byte [1 ] || 'X ' === $ byte [1 ]) && $ l === strspn ($ byte , '0123456789aAbBcCdDeEfF ' , 2 ) + 2 ) {
33+ $ n = hexdec ($ byte );
34+ } else if ($ l === strspn ($ byte , '0123456789 ' )) {
35+ $ n = octdec ($ byte );
36+ }
37+ } else if ($ l > 0 && $ l === strspn ($ byte , '0123456789 ' )) {
38+ $ n = (int )$ byte ;
39+ }
2240
23- if (!is_numeric ($ byte ) || $ byte < 0 || $ byte > 255 )
24- throw new \lang \FormatException ('Invalid format of ip address: [ ' .$ ip .'] ' );
41+ if ($ n < 0 || $ n > 255 ) {
42+ throw new FormatException ('Invalid format of IP address: [ ' .$ ip .'] ' );
43+ }
2544
26- $ addr |= ($ byte << (8 * (3 - $ i ++)));
45+ $ addr |= ($ n << (8 * (3 - $ i ++)));
2746 }
2847 return $ addr ;
2948 }
3049
3150 /**
3251 * Constructor
3352 *
34- * @param string address
35- * @throws lang.FormatException in case address is illegal
53+ * @param string $ address
54+ * @throws lang.FormatException in case address is illegal
3655 */
3756 public function __construct ($ address ) {
3857 $ this ->addr = self ::ip2long ($ address );
@@ -71,7 +90,7 @@ public function asString() {
7190 * @return bool
7291 */
7392 public function isLoopback () {
74- return $ this ->addr >> 8 == 0x7F0000 ;
93+ return $ this ->addr >> 8 === 0x7F0000 ;
7594 }
7695
7796 /**
@@ -86,18 +105,18 @@ public function inSubnet(Network $net) {
86105
87106 $ addrn = $ net ->getAddress ()->addr ;
88107 $ mask = $ net ->getNetmask ();
89- return $ this ->addr >> (32 - $ mask ) == $ addrn >> (32 - $ mask );
108+ return $ this ->addr >> (32 - $ mask ) === $ addrn >> (32 - $ mask );
90109 }
91110
92111 /**
93112 * Create a subnet of this address, with the specified size.
94113 *
95114 * @param int subnetSize
96- * @return Network
115+ * @return peer.net. Network
97116 * @throws lang.IllegalArgumentException in case the subnetSize is not correct
98117 */
99118 public function createSubnet ($ subnetSize ) {
100- $ addr = $ this ->addr & (0xFFFFFFFF << (32 - $ subnetSize ));
119+ $ addr = $ this ->addr & (0xFFFFFFFF << (32 - $ subnetSize ));
101120 return new Network (new Inet4Address (long2ip ($ addr )), $ subnetSize );
102121 }
103122
0 commit comments