Changeset 905


Ignore:
Timestamp:
08/11/17 17:43:47 (7 years ago)
Author:
Kris Deugau
Message:

/trunk

Extend RPC search to cover CIDR cases

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/search-rpc.cgi

    r903 r905  
    143143  _commoncheck(\%args, 'n');
    144144
    145   my $sql = q(SELECT s.cidr,s.custid,s.type,s.description,a.dispname FROM searchme s JOIN alloctypes a ON s.type = a.type);
    146 
    147145  my @fields;
    148146  my @vals;
     
    150148
    151149  my %mt = (EXACT => '=',
     150        EQUAL => '=',
    152151        NOT => '!~',
     152        # CIDR options
     153        MASK => 'MASK',
     154        WITHIN => '<<=',
     155        CONTAINS => '>>=',
    153156        );
    154157
     
    158161    push @matchtypes, '=';
    159162  }
     163
     164  ## CIDR query options.
    160165  if ($args{cidr}) {
    161     # Magic!
    162   }
     166    $args{cidr} =~ s/^\s*(.+)\s*$/$1/g;
     167    # strip matching type substring, if any - only applies to full-CIDR
     168    my ($mnote) = $args{cidr} =~ /^(\w+):/;
     169    $args{cidr} =~ s/^$mnote:// if $mnote;
     170
     171    if ($args{cidr} eq '') { # We has a blank CIDR.  Ignore it.
     172    } elsif ($args{cidr} =~ /\//) {
     173      my ($net,$maskbits) = split /\//, $args{cidr};
     174      if ($args{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
     175        # Full CIDR match.
     176        push @fields, 's.cidr';
     177        push @vals, $args{cidr};
     178        if ($mnote =~ /(EQUAL|EXACT|CONTAINS|WITHIN)/) {
     179          push @matchtypes, $mt{$1};
     180        } else { # default to exact match
     181          push @matchtypes, '=';
     182        }
     183      } elsif ($args{cidr} =~ /^(\d{1,3}\.){2}\d{1,3}\/\d{2}$/) {
     184        # Partial match;  beginning of subnet and maskbits are provided
     185        # Show any blocks with the leading octet(s) and that masklength
     186        # eg 192.168.179/26 should show all /26 subnets in 192.168.179
     187        # Need some more magic for bare /nn searches:
     188        push @fields, 's.cidr','masklen(s.cidr)';
     189        push @vals, "$net.0/24", $maskbits;
     190        push @matchtypes, '<<=','=';
     191      }
     192    } elsif ($args{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
     193      # Specific IP address match.  Will show the parent chain down to the final allocation.
     194      push @fields, 's.cidr';
     195      push @vals, $args{cidr};
     196      push @matchtypes, '>>=';
     197    } elsif ($args{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) {
     198      # 1, 2, or 3 leading octets in CIDR
     199      push @fields, 'text(s.cidr)';
     200      push @vals, "$args{cidr}\%";
     201      push @matchtypes, 'LIKE';  # hmm
     202    } else {
     203      # do nothing.
     204      ##fixme  we'll ignore this to clear out the references to legacy code.
     205    } # done with CIDR query options.
     206
     207  } # args{cidr}
     208
    163209  foreach (qw(custid description notes city) ) {
    164210    if ($args{$_}) {
     
    166212      if ($args{$_} =~ /^(EXACT|NOT):/) {
    167213        push @matchtypes, $mt{$1};
    168         $args{$_} =~ s/^(?:EXACT|NOT)://;
     214        $args{$_} =~ s/^$1://;
    169215      } else {
    170216        push @matchtypes, '~*';
     
    174220  }
    175221
     222  my $sql = q(SELECT s.cidr,s.custid,s.type,s.description,s.city,a.dispname FROM searchme s JOIN alloctypes a ON s.type = a.type);
    176223  my @sqlcriteria;
    177224  for (my $i = 0; $i <= $#fields; $i++) {
     
    185232  return $result;
    186233} # rpc_search()
    187 
Note: See TracChangeset for help on using the changeset viewer.