4 IPSet is a PHP library for matching IP addresses against a set of CIDR
7 Here is how you use it:
10 // At startup, calculate the optimized data structure for the set:
11 $ipset = new IPSet( array(
17 // Runtime check against cached set (returns bool):
18 if ( $ipset->match( $ip ) ) {
23 In rough benchmarking, this takes about 80% more time than `in_array()` checks
24 on a short (a couple hundred at most) array of addresses. It's fast either way
25 at those levels, though, and IPSet would scale better than in_array if the
26 array were much larger.
28 For mixed-family CIDR sets, however, this code gives well over 100x speedup vs
29 iterating `IP::isInRange()` over an array of CIDR specs.
31 The basic implementation is two separate binary trees (IPv4 and IPv6) as nested
32 php arrays with keys named 0 and 1. The values false and true are terminal
33 match-fail and match-success, otherwise the value is a deeper node in the tree.
35 A simple depth-compression scheme is also implemented: whole-byte tree
36 compression at whole-byte boundaries only, where no branching occurs during
37 that whole byte of depth. A compressed node has keys 'comp' (the byte to
38 compare) and 'next' (the next node to recurse into if 'comp' matched successfully).
40 For example, given these inputs:
47 The v4 tree would look like:
62 (multi-byte compression nodes were attempted as well, but were
63 a net loss in my test scenarios due to additional match complexity)
68 Copyright 2014, 2015 Brandon Black <blblack@gmail.com>
70 This program is free software; you can redistribute it and/or modify
71 it under the terms of the GNU General Public License as published by
72 the Free Software Foundation; either version 2 of the License, or
73 (at your option) any later version.
75 This program is distributed in the hope that it will be useful,
76 but WITHOUT ANY WARRANTY; without even the implied warranty of
77 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78 GNU General Public License for more details.
80 You should have received a copy of the GNU General Public License along
81 with this program; if not, write to the Free Software Foundation, Inc.,
82 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
83 <http://www.gnu.org/copyleft/gpl.html>