From: Matteo Nastasi (mop) Date: Thu, 3 Sep 2015 06:22:34 +0000 (+0200) Subject: add ip2int and int2ip functions to be able to store on pg IPV4 ip addresses X-Git-Tag: v5.0.0~29 X-Git-Url: http://mop.ddnsfree.com/gitweb/?a=commitdiff_plain;h=860fbd36c87a6da4a71ef5e42d661c555bb3b3d6;p=brisk.git add ip2int and int2ip functions to be able to store on pg IPV4 ip addresses --- diff --git a/web/Obj/brisk.phh b/web/Obj/brisk.phh index 6c18990..f221518 100644 --- a/web/Obj/brisk.phh +++ b/web/Obj/brisk.phh @@ -345,6 +345,20 @@ $G_PG_cons_n = 345; /* printf("\n"); */ /* } */ +function ip2int($s) +{ + $v = ip2long($s); + if (PHP_INT_SIZE == 4) + return ($v); + + return ( ($v & 0x80000000 ? 0xffffffff00000000 : 0x00) | $v ); +} + +function int2ip($i) +{ + return long2ip($i & 0xffffffff); +} + function nickserv_msg($dt, $msg) { return sprintf('chatt_sub("%s",[0x040003,"%s"],"%s");', $dt, NICKSERV, $msg); } diff --git a/web/Obj/dbase_pgsql.phh b/web/Obj/dbase_pgsql.phh index 2b4467c..b882c63 100644 --- a/web/Obj/dbase_pgsql.phh +++ b/web/Obj/dbase_pgsql.phh @@ -122,6 +122,11 @@ class BriskDB return ($res); } + function last_error() + { + return pg_last_error($this->dbconn->db); + } + function users_load() { } diff --git a/webtest/test_db.php b/webtest/test_db.php index a656631..8817058 100644 --- a/webtest/test_db.php +++ b/webtest/test_db.php @@ -56,7 +56,7 @@ function main() { } succ("COMMIT"); - $arr_ip = array("1.1.1.1", "127.0.0.1", "255.255.255.255", "255.255.0.0" ); + $arr_ip = array("1.1.1.1", "127.0.0.1", "255.255.255.255", "255.255.0.0", "201.102.123.111", "5.9.11.13" ); if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) { fail("DROP TABLE test_ip"); @@ -75,16 +75,15 @@ function main() { succ("CREATE TABLE test_ip"); foreach ($arr_ip as $i) { - $v = $v_or = ip2long($i); - if (PHP_INT_SIZE == 8 && $v & 0x80000000) - $v = 0xffffffff00000000 | $v_or; + $v = ip2int($i); - if ($bdb->query(sprintf("INSERT INTO test_ip (ip, atime) VALUES (CAST(%d AS integer), '1999-01-08 04:05:06');", $v)) == FALSE) { + $msg = sprintf("  INSERT INTO test_ip [%s] val: [%d (%x)]", $i, $v, $v); + if ($bdb->query(sprintf("INSERT INTO test_ip (ip, atime) VALUES (%d, '1999-01-08 04:05:06');", $v)) == FALSE) { printf("%s
\n", $bdb->last_error()); - fail("INSERT INTO test_ip ".$i." V: ".$v." V_or: ".$v_or); + fail($msg); break; } - succ("INSERT INTO test_ip ".$i." V: ".$v." V_or: ".$v_or); + succ($msg); } if (($ip_pg = $bdb->query(sprintf("SELECT * FROM test_ip ORDER BY code;"))) == FALSE) { @@ -97,18 +96,15 @@ function main() { for ($r = 0 ; $r < pg_numrows($ip_pg) ; $r++) { $ip_obj = pg_fetch_object($ip_pg, $r); - if (PHP_INT_SIZE == 8) - $v = $ip_obj->ip & 0x00000000ffffffff; - else - $v = $ip_obj->ip; + $v = int2ip($ip_obj->ip); - if ($arr_ip[$r] != long2ip($v)) { - fail(sprintf(" Expected: %s, retrieved: %s", $arr_ip[$r], long2ip($v))); + if ($arr_ip[$r] != $v) { + fail(sprintf("  Expected: %s, retrieved: %s", $arr_ip[$r], $v)); } else { - succ(sprintf(" Expected: %s", $arr_ip[$r])); + succ(sprintf("  Expected: %s", $arr_ip[$r])); } - // printf("RET IP: %d V: %d IP: %s
\n", $ip_obj->ip, $v, long2ip($v)); + // printf("RET IP: %d IP: %s
\n", $ip_obj->ip, $v)); }