File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,9 @@ class CIDR
7
7
protected string $ prefix ;
8
8
protected int $ length ;
9
9
10
+ // network address
10
11
protected int $ start ;
12
+ // broadcast address
11
13
protected int $ end ;
12
14
13
15
/**
@@ -49,7 +51,7 @@ protected function parseOrFail(string $cidrNotation): void
49
51
$ prefix = intval ($ parts [1 ]);
50
52
51
53
if ($ prefix < 0 || $ prefix > 32 ) {
52
- throw new \InvalidArgumentException ('Invalid CIDR: ' . $ cidrNotation );
54
+ throw new \InvalidArgumentException ('Invalid CIDR prefix : ' . $ cidrNotation );
53
55
}
54
56
55
57
$ start = ip2long ($ address );
@@ -58,14 +60,15 @@ protected function parseOrFail(string $cidrNotation): void
58
60
throw new \InvalidArgumentException ('Invalid IP address: ' . $ address );
59
61
}
60
62
61
- $ prefixLength = pow (2 , (32 - $ prefix )) - 1 ;
62
- $ end = $ start + $ prefixLength ;
63
+ $ mask = -1 << (32 - $ prefix );
64
+ $ networkAddress = ip2long ($ address ) & $ mask ;
65
+ $ broadcastAddress = $ networkAddress | ~$ mask ;
63
66
64
67
$ this ->prefix = $ address ;
65
68
$ this ->length = $ prefix ;
66
69
67
- $ this ->start = $ start ;
68
- $ this ->end = $ end ;
70
+ $ this ->start = $ networkAddress ;
71
+ $ this ->end = $ broadcastAddress ;
69
72
}
70
73
71
74
public function getFirstAddress (): string
You can’t perform that action at this time.
0 commit comments