r4005 The keys 'db_first' and 'db_last' were returned back into the $netinfo arrays
authorAlexey Andriyanov <alan@al-an.info>
Thu, 18 Nov 2010 11:47:05 +0000 (11:47 +0000)
committerAlexey Andriyanov <alan@al-an.info>
Thu, 18 Nov 2010 11:47:05 +0000 (11:47 +0000)
after being reduced by previous commit. Some incomatibilities appeared
in Yandex local scripts depending on those keys.

inc/database.php
inc/functions.php

index d217217..03bbb9d 100644 (file)
@@ -322,10 +322,14 @@ function listCells ($realm, $parent_id = 0)
                        $ret[$entity_id]['ip_bin'] = ip2long ($ret[$entity_id]['ip']);
                        $ret[$entity_id]['mask_bin'] = binMaskFromDec ($ret[$entity_id]['mask']);
                        $ret[$entity_id]['mask_bin_inv'] = binInvMaskFromDec ($ret[$entity_id]['mask']);
+                       $ret[$entity_id]['db_first'] = sprintf ('%u', 0x00000000 + $ret[$entity_id]['ip_bin'] & $ret[$entity_id]['mask_bin']);
+                       $ret[$entity_id]['db_last'] = sprintf ('%u', 0x00000000 + $ret[$entity_id]['ip_bin'] | ($ret[$entity_id]['mask_bin_inv']));
                        break;
                case 'ipv6net':
                        $ret[$entity_id]['ip_bin'] = new IPv6Address ($ret[$entity_id]['ip_bin']);
                        $ret[$entity_id]['ip'] = $ret[$entity_id]['ip_bin']->format();
+                       $ret[$entity_id]['db_first'] = $ret[$entity_id]['ip_bin']->get_first_subnet_address($ret[$entity_id]['mask']);
+                       $ret[$entity_id]['db_last'] = $ret[$entity_id]['ip_bin']->get_last_subnet_address($ret[$entity_id]['mask']);
                        break;
                default:
                        break;
@@ -402,10 +406,14 @@ function spotEntity ($realm, $id)
                $ret['ip_bin'] = ip2long ($ret['ip']);
                $ret['mask_bin'] = binMaskFromDec ($ret['mask']);
                $ret['mask_bin_inv'] = binInvMaskFromDec ($ret['mask']);
+               $ret['db_first'] = sprintf ('%u', 0x00000000 + $ret['ip_bin'] & $ret['mask_bin']);
+               $ret['db_last'] = sprintf ('%u', 0x00000000 + $ret['ip_bin'] | ($ret['mask_bin_inv']));
                break;
        case 'ipv6net':
                $ret['ip_bin'] = new IPv6Address ($ret['ip_bin']);
                $ret['ip'] = $ret['ip_bin']->format();
+               $ret['db_first'] = $ret['ip_bin']->get_first_subnet_address($ret['mask']);
+               $ret['db_last'] = $ret['ip_bin']->get_last_subnet_address($ret['mask']);
                break;
        default:
                break;
index 3db14ec..200c61c 100644 (file)
@@ -1845,11 +1845,16 @@ function iptree_embed (&$node, $pfx)
        // split?
        if (!isset ($node['right']))
        {
+               // Fill in db_first/db_last to make it possible to run scanIPv4Space() on the node.
                $node['left']['mask'] = $node['mask'] + 1;
                $node['left']['ip_bin'] = $node['ip_bin'];
+               $node['left']['db_first'] = sprintf ('%u', $node['left']['ip_bin']);
+               $node['left']['db_last'] = sprintf ('%u', $node['left']['ip_bin'] | binInvMaskFromDec ($node['left']['mask']));
 
                $node['right']['mask'] = $node['mask'] + 1;
                $node['right']['ip_bin'] = $node['ip_bin'] + binInvMaskFromDec ($node['mask'] + 1) + 1;
+               $node['right']['db_first'] = sprintf ('%u', $node['right']['ip_bin']);
+               $node['right']['db_last'] = sprintf ('%u', $node['right']['ip_bin'] | binInvMaskFromDec ($node['right']['mask']));
        }
 
        // repeat!
@@ -1879,15 +1884,19 @@ function ipv6tree_embed (&$node, $pfx)
        {
                $node['left']['mask'] = $node['mask'] + 1;
                $node['left']['ip_bin'] = $node['ip_bin'];
+               $node['left']['db_first'] = $node['ip_bin']->get_first_subnet_address ($node['mask'] + 1);
+               $node['left']['db_last'] = $node['ip_bin']->get_last_subnet_address ($node['mask'] + 1);
 
                $node['right']['mask'] = $node['mask'] + 1;
                $node['right']['ip_bin'] = $node['ip_bin']->get_last_subnet_address ($node['mask'] + 1)->next();
+               $node['right']['db_first'] = $node['right']['ip_bin'];
+               $node['right']['db_last'] = $node['right']['ip_bin']->get_last_subnet_address ($node['mask'] + 1);
        }
 
        // repeat!
-       if (($node['left']['ip_bin']->get_first_subnet_address ($node['left']['mask'])) == ($pfx['ip_bin']->get_first_subnet_address ($node['left']['mask'])))
+       if ($node['left']['db_first'] == $pfx['ip_bin']->get_first_subnet_address ($node['left']['mask']))
                $self ($node['left'], $pfx);
-       elseif (($node['right']['ip_bin']->get_first_subnet_address ($node['right']['mask'])) == ($pfx['ip_bin']->get_first_subnet_address ($node['left']['mask'])))
+       elseif ($node['right']['db_first'] == $pfx['ip_bin']->get_first_subnet_address ($node['left']['mask']))
                $self ($node['right'], $pfx);
        else
                throw new RackTablesError ('cannot decide between left and right', RackTablesError::INTERNAL);
@@ -1934,26 +1943,21 @@ function loadOwnIPv4Addresses (&$node)
 {
        $toscan = array();
        $node['addrt'] = 0;
-       if (empty ($node['kids']))
+       if (!isset ($node['kids']) or !count ($node['kids']))
        {
-               $mask_bin = binMaskFromDec ($node['mask']);
-               $mask_bin_inv = binInvMaskFromDec ($node['mask']);
-               $db_first = sprintf ('%u', 0x00000000 + $node['ip_bin'] & $mask_bin);
-               $db_last  = sprintf ('%u', 0x00000000 + $node['ip_bin'] | $mask_bin_inv);
-               $node['addrt'] = $mask_bin_inv + 1;
-               $toscan[] = array ('i32_first' => $db_first, 'i32_last' => $db_last);
+               $toscan[] = array ('i32_first' => $node['db_first'], 'i32_last' => $node['db_last']);
+               $node['addrt'] = $node['db_last'] - $node['db_first'] + 1;
        }
        else
+       {
+               $node['addrt'] = 0;
                foreach ($node['kids'] as $nested)
                        if (!isset ($nested['id'])) // spare
                        {
-                               $mask_bin = binMaskFromDec ($nested['mask']);
-                               $mask_bin_inv = binInvMaskFromDec ($nested['mask']);
-                               $db_first = sprintf ('%u', 0x00000000 + $nested['ip_bin'] & $mask_bin);
-                               $db_last  = sprintf ('%u', 0x00000000 + $nested['ip_bin'] | $mask_bin_inv);
-                               $node['addrt'] += $mask_bin_inv + 1;
-                               $toscan[] = array ('i32_first' => $db_first, 'i32_last' => $db_last);
+                               $toscan[] = array ('i32_first' => $nested['db_first'], 'i32_last' => $nested['db_last']);
+                               $node['addrt'] += $node['db_last'] - $node['db_first'] + 1;
                        }
+       }
        $node['addrlist'] = scanIPv4Space ($toscan);
        $node['addrc'] = count ($node['addrlist']);
 }