├── .gitignore ├── .travis.yml ├── CIDRmatch └── CIDRmatch.php ├── LICENSE.txt ├── README.md ├── composer.json └── tests ├── CIDRmatchTest └── CIDRmatchTest.php ├── bootstrap.php └── phpunit.xml /.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | vendor/ 3 | composer.lock 4 | .project 5 | .settings 6 | .DS_Store 7 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | dist: xenial 3 | php: 4 | - '5.6' 5 | - '7.1' 6 | - '7.2' 7 | 8 | before_script: 9 | - composer install 10 | 11 | script: 12 | - cd ./tests/ 13 | - ../vendor/bin/phpunit 14 | 15 | notifications: 16 | email: false -------------------------------------------------------------------------------- /CIDRmatch/CIDRmatch.php: -------------------------------------------------------------------------------- 1 | IPv4Match($ip, $subnet, $mask); 46 | break; 47 | case 'v6': 48 | if ($mask === null) { 49 | $mask = 128; 50 | } 51 | 52 | return $this->IPv6Match($ip, $subnet, $mask); 53 | break; 54 | } 55 | } 56 | 57 | // inspired by: http://stackoverflow.com/questions/7951061/matching-ipv6-address-to-a-cidr-subnet 58 | private function IPv6MaskToByteArray($subnetMask) 59 | { 60 | $addr = str_repeat("f", intdiv($subnetMask, 4)); 61 | switch ($subnetMask % 4) { 62 | case 0: 63 | break; 64 | case 1: 65 | $addr .= "8"; 66 | break; 67 | case 2: 68 | $addr .= "c"; 69 | break; 70 | case 3: 71 | $addr .= "e"; 72 | break; 73 | } 74 | $addr = str_pad($addr, 32, '0'); 75 | $addr = pack("H*", $addr); 76 | 77 | return $addr; 78 | } 79 | 80 | // inspired by: http://stackoverflow.com/questions/7951061/matching-ipv6-address-to-a-cidr-subnet 81 | private function IPv6Match($address, $subnetAddress, $subnetMask) 82 | { 83 | if (!filter_var($subnetAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || $subnetMask === NULL || $subnetMask === "" || $subnetMask < 0 || $subnetMask > 128) { 84 | return false; 85 | } 86 | $subnet = inet_pton($subnetAddress); 87 | $addr = inet_pton($address); 88 | 89 | $binMask = $this->IPv6MaskToByteArray($subnetMask); 90 | 91 | return ($addr & $binMask) == $subnet; 92 | } 93 | 94 | // inspired by: http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5 95 | private function IPv4Match($address, $subnetAddress, $subnetMask) 96 | { 97 | if (!filter_var($subnetAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || $subnetMask === NULL || $subnetMask === "" || $subnetMask < 0 || $subnetMask > 32) { 98 | return false; 99 | } 100 | 101 | $address = ip2long($address); 102 | $subnetAddress = ip2long($subnetAddress); 103 | $mask = -1 << (32 - $subnetMask); 104 | $subnetAddress &= $mask; # nb: in case the supplied subnet wasn't correctly aligned 105 | return ($address & $mask) == $subnetAddress; 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (http://tholu.mit-license.org/) 2 | Copyright © 2022 Thomas Lutz 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://api.travis-ci.com/tholu/php-cidr-match.svg?branch=master)](https://app.travis-ci.com/github/tholu/php-cidr-match) 2 | 3 | # CIDR match 4 | 5 | CIDRmatch is a library to match an IP to an IP range in CIDR notation (IPv4 and IPv6). 6 | 7 | **NOTE:** Symfony2 already does everything this library here does with its IpUtils module. 8 | Unfortunately I discovered this only after I finished working on this library. 9 | 10 | ## Usage 11 | 12 | ``` 13 | $cidrMatch = new CIDRmatch(); 14 | $cidrMatch->match($ip, $cidr); 15 | ``` 16 | 17 | ## Tests 18 | 19 | ``` 20 | ./vendor/bin/phpunit tests/CIDRmatchTest 21 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "tholu/php-cidr-match", 3 | "type" : "library", 4 | "description" : "CIDRmatch is a library to match an IP to an IP range in CIDR notation (IPv4 and IPv6).", 5 | "keywords" : [ 6 | "PHP", 7 | "IPv4", 8 | "IPv6", 9 | "CIDR" 10 | ], 11 | "license" : "MIT", 12 | "authors" : [{ 13 | "name" : "Thomas Lutz", 14 | "email" : "thomaslutz.de@gmail.com", 15 | "homepage" : "https://github.com/tholu", 16 | "role" : "Creator, Developer, Maintainer" 17 | } 18 | ], 19 | "require" : { 20 | "php" : ">=5.4" 21 | }, 22 | "require-dev" : { 23 | "squizlabs/php_codesniffer": "3.6.2", 24 | "composer/composer": ">2", 25 | "phpunit/phpunit" : "4.8.36" 26 | }, 27 | "autoload" : { 28 | "psr-0" : { 29 | "CIDRmatch\\" : "" 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /tests/CIDRmatchTest/CIDRmatchTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($cidrMatch->match('104.132.31.99', '104.132.0.0/14')); 19 | $this->assertTrue($cidrMatch->match('74.125.60.99', '74.125.0.0/16')); 20 | 21 | } 22 | 23 | /** 24 | * Ensure that IPv4 addresses don't match with invalid input 25 | */ 26 | public function testIPv4NoMatch() 27 | { 28 | $cidrMatch = new CIDRmatch(); 29 | $this->assertFalse($cidrMatch->match('Not an IP address', '104.132.0.0/14')); 30 | $this->assertFalse($cidrMatch->match('104.132.31.99', 'Not an IP address/14')); 31 | $this->assertFalse($cidrMatch->match('104.132.31.99', '104.132.0.0/33')); 32 | $this->assertFalse($cidrMatch->match('104.132.31.99', '104.132.0.0/')); 33 | $this->assertFalse($cidrMatch->match('104.132.31.99', '104.132.0.0')); 34 | } 35 | 36 | /** 37 | * Ensure that IPv6 addresses match 38 | */ 39 | public function testIPv6Match() 40 | { 41 | $cidrMatch = new CIDRmatch(); 42 | $this->assertTrue($cidrMatch->match('2001:0db8:85a3:08d3:1319:8a2e:0370:7347', '2001:0db8:85a3:08d3::/64')); 43 | $this->assertTrue($cidrMatch->match('2a00:1450:400c:c04::6a', '2a00:1450::/32')); 44 | $this->assertTrue($cidrMatch->match('2001:0db8:85a3:08d3:1319:8a2e:0370:7347', '2001:0db8:85a3:08d3:1319:8a2e:0370:7347')); 45 | } 46 | 47 | /** 48 | * Ensure that IPv6 addresses don't match with invalid input 49 | */ 50 | public function testIPv6NoMatch() 51 | { 52 | $cidrMatch = new CIDRmatch(); 53 | $this->assertFalse($cidrMatch->match('Not an IP address', '2001:0db8:85a3:08d3::/64')); 54 | $this->assertFalse($cidrMatch->match('2001:0db8:85a3:08d3:1319:8a2e:0370:7347', 'Not an IP address/64')); 55 | $this->assertFalse($cidrMatch->match('2001:0db8:85a3:08d3:1319:8a2e:0370:7347', '2001:0db8:85a3:08d3::/129')); 56 | $this->assertFalse($cidrMatch->match('2001:0db8:85a3:08d3:1319:8a2e:0370:7347', '2001:0db8:85a3:08d3::/')); 57 | $this->assertFalse($cidrMatch->match('2001:0db8:85a3:08d3:1319:8a2e:0370:7347', '2001:0db8:85a3:08d3::')); 58 | } 59 | 60 | /** 61 | * Ensure that case from issue #2 (https://github.com/tholu/php-cidr-match/issues/2) works 62 | */ 63 | public function testIssue2() { 64 | $cidrMatch = new CIDRmatch(); 65 | 66 | $addr = '192.168.1.1'; 67 | $this->assertTrue($cidrMatch->match($addr, '192.168.1.0/24')); 68 | $this->assertFalse($cidrMatch->match($addr, '1.2.3.4/1')); 69 | $this->assertFalse($cidrMatch->match($addr, '192.168.1.1/33')); // invalid subnet 70 | $this->assertTrue($cidrMatch->match($addr, '0.0.0.0/0')); 71 | $this->assertFalse($cidrMatch->match($addr, '256.256.256/0')); // invalid CIDR notation 72 | 73 | $addr = '10.5.5.1'; 74 | $this->assertTrue($cidrMatch->match($addr, '10.0.0.1/8')); 75 | $this->assertTrue($cidrMatch->match($addr, '10.0.0.10/8')); 76 | $this->assertTrue($cidrMatch->match($addr, '10.5.5.0/16')); 77 | $this->assertFalse($cidrMatch->match($addr, '10.4.5.0/16')); 78 | $this->assertTrue($cidrMatch->match($addr, '10.4.5.0/15')); 79 | 80 | $this->assertTrue($cidrMatch->match('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65')); 81 | $this->assertFalse($cidrMatch->match('2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65')); 82 | $this->assertFalse($cidrMatch->match('2a01:198:603:0:396e:4789:8e99:890f', '::1')); 83 | $this->assertFalse($cidrMatch->match('0:0:603:0:396e:4789:8e99:0001', '::1')); 84 | $this->assertFalse($cidrMatch->match('}__test|O:21:"JDatabaseDriverMysqli":3:{s:2', '::1')); 85 | } 86 | 87 | /** 88 | * Ensure that case from issue #3 (https://github.com/tholu/php-cidr-match/issues/3) works 89 | */ 90 | public function testIssue3() { 91 | $cidrMatch = new CIDRmatch(); 92 | $this->assertFalse($cidrMatch->match('91.124.36.116', '2a02:6b8:0:1a00::/56')); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15 | 16 | 17 | 18 | ./CIDRmatchTest 19 | 20 | 21 | 22 | 23 | 24 | ../CIDRmatch 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | --------------------------------------------------------------------------------