3 * brisk - Obj/ipclass.phh
5 * Copyright (C) 2015 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
29 function IPClassItem($ipset)
32 $elem = split("/", $ipset, 2);
34 $mask = (int)$elem[1];
38 $this->mask = ((1<<($mask))-1) << (32 - $mask);
39 $this->addr = ip2long($addr) & $this->mask;
41 fprintf(STDERR, "New ipclass item: %x (%x)\n", $this->addr, $this->mask);
46 // fprintf(STDERR, "IP: %x, ADDR: %x, MASK: %x -> (%d)\n",
47 // $ip, $this->addr, $this->mask, ((ip2long($ip) & $this->mask) == $this->addr));
48 return (($ip & $this->mask) == $this->addr);
60 static function create($ip_in=NULL)
62 $thiz = new IPClass();
65 $thiz->update($ip_in);
71 function update($ip_in)
75 $this->ipcl = array();
76 for ($i = 0 ; $i < count($ip_in) ; $i++) {
77 $this->ipcl[$i] = new IPClassItem($ip_in[$i]);
83 if ($this->ipcl != NULL) {
84 $ct = count($this->ipcl);
85 for ($i = 0 ; $i < $ct ; $i++) {
86 unset($this->ipcl[$i]);
92 function check($ip_str)
94 $ip = ip2long($ip_str);
96 for ($i = 0 ; $i < count($this->ipcl) ; $i++) {
97 if ($this->ipcl[$i]->match($ip)) {
98 fprintf(STDERR, "ban_list[%d] = %x (%x) MATCH\n", $i,
99 $this->ipcl[$i]->addr, $this->ipcl[$i]->mask);