pf-tools/pf-tools: Feature: adding ip_type when resolving IP in ...

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Tue Nov 2 08:00:24 UTC 2010


details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/1ba822e796db
changeset: 913:1ba822e796db
user:      "Christophe Caillet <quadchris at free.fr>"
date:      Fri Oct 22 19:31:17 2010 +0200
description:
Feature: adding ip_type when resolving IP in filter_privateresolve

diffstat:

1 file changed, 1 insertion(+), 1 deletion(-)
lib/PFTools/Utils.pm |    2 +-

diffs (171 lines):

diff -r c85304f3069d -r 1ba822e796db filters/filter_privateresolve
--- a/filters/filter_privateresolve	Fri Oct 22 12:13:37 2010 +0200
+++ b/filters/filter_privateresolve	Fri Oct 22 19:31:17 2010 +0200
@@ -39,6 +39,7 @@
     'input|i=s',
     'output|o=s',
     'sep=s',
+    'ip=s',
     'site|s=s',
     'store=s',
     'type|t=s',
@@ -51,6 +52,7 @@
     'host'  => hostname,
     'sep'   => ' ',
     'type'  => 'cnf',
+    'ip'    => 'ipv4',
 };
 
 my $program = basename $PROGRAM_NAME;
@@ -69,6 +71,8 @@
                              e.g. /etc/pf-tools.conf (optional)
     --store                : file where global structure datas are in
                              storable format (optional)
+ --ip                      : specify here the ip_type for resolution default
+                           : is ipv4
  -z --zone                 : zone on which we want to filter input
  -t --type                 : type for resolution. Allowed values are cnf
                              (from global configuration structure) and dns
@@ -117,7 +121,8 @@
     $PF_CONFIG,
     $options->{'sep'},
     $GLOBAL_STRUCT,
-    $options->{'type'}
+    $options->{'type'},
+    $options->{'ip'}
 );
 
 # FIXME: functions with too many parameters should be converted
diff -r c85304f3069d -r 1ba822e796db lib/PFTools/Structqueries.pm
--- a/lib/PFTools/Structqueries.pm	Fri Oct 22 12:13:37 2010 +0200
+++ b/lib/PFTools/Structqueries.pm	Fri Oct 22 19:31:17 2010 +0200
@@ -43,6 +43,7 @@
     Get_mode_from_hostprops
     Get_pkgtype_from_hostname
     Get_host_config_from_CONFIG
+    Get_vlan_config_from_CONFIG
     Resolv_hostname_from_GLOBAL
 );
 
@@ -275,6 +276,17 @@
     return $host_config;
 }
 
+sub Get_vlan_config_from_CONFIG {
+    my ( $vlan, $global_config, $site ) = @_;
+
+    unless( $site ) {
+        carp q{ERROR: $site MUST BE defined};
+        return;
+    }
+    my $site_part = $global_config->{'SITE'}->{'BY_NAME'}->{$site};
+    return $site_part->{'NETWORK'}->{'BY_NAME'}->{$vlan};
+}
+
 sub Get_pkgtype_from_hostname {
     my ( $hostname, $global_config, $site ) = @_;
 
@@ -332,15 +344,16 @@
 }
 
 sub Resolv_hostname_from_GLOBAL {
-    my ( $hostname, $global_config, $site, $zone, $hosttype ) = @_;
+    my ( $hostname, $ip_type, $global_config, $site, $zone, $hosttype ) = @_;
     my $resolved = [];
 
+    my $zone_key = ( $ip_type eq 'ipv6' ) ? 'ZONE6' : 'ZONE';
     $hostname =~ m{\A ([^.]+)(\.([^.]+))? \Z}xms;
     my ( $hostshort, $hostvlan ) = ( $1, $3 );
     my $zone_part
-        = $global_config->{'ZONE'}->{'BY_NAME'}->{$zone}->{'BY_SITE'}
+        = $global_config->{$zone_key}->{'BY_NAME'}->{$zone}->{'BY_SITE'}
         ->{$site};
-    if ( $hostname =~ m{\A (network|netmask|broadcast|gateway) }xms ) {
+    if( $hostname =~ m{\A (network|netmask|broadcast|gateway) }xms ) {
         return if ( $hostvlan && !defined $zone_part->{$hostvlan} );
         my ( $type, $field )
             = split( m{\s+}, $zone_part->{$hostvlan}->{$hostshort} );
diff -r c85304f3069d -r 1ba822e796db lib/PFTools/Utils.pm
--- a/lib/PFTools/Utils.pm	Fri Oct 22 12:13:37 2010 +0200
+++ b/lib/PFTools/Utils.pm	Fri Oct 22 19:31:17 2010 +0200
@@ -454,8 +454,12 @@
 }
 
 sub Resolv {
-    my ( $type_resolve, $hostname, $global_config, $site, $hosttype ) = @_;
+    my ( $type_resolve, $ip_type, $hostname, $global_config, $site, $hosttype ) = @_;
 
+    unless( $ip_type =~ m{\A ipv(4|6) \Z}xms ) {
+        carp qq{ERROR: Invalid ip_type $ip_type};
+        return;
+    }
     unless( $type_resolve =~ m{\A (cnf|dns) \Z}xms ) {
         carp qq{ERROR: Invalid type_resolv $type_resolve};
         return;
@@ -475,7 +479,7 @@
         $hostname =~ m{\A ([^.]+)(\.([^.]+))? \Z}xms;
         my ( $hostshort, $hostvlan ) = ( $1, $3 );
         if ( !defined $hosttype
-            && $hostname !~ m{\A (network|netmask|broadcast|gateway)}xms )
+            && $hostshort !~ m{\A (network|netmask|broadcast|gateway|prefix)}xms )
         {
             $hosttype = Get_hosttype_from_hostname(
                 $hostshort, $global_config, $site
@@ -485,8 +489,17 @@
                 return;
             }
         }
+        elsif( $hostshort =~ m{\A prefix \Z}xms ) {
+            my $vlan_def = Get_vlan_config_from_CONFIG(
+                $hostvlan, $global_config, $site
+            );
+            my $netblock = Get_netblock_from_vlan( $ip_type, $vlan_def );
+            my $prefix   = $netblock->cidr();
+            $prefix      =~ s{\A [^/]+\/([\d]+) \Z}{$1}xms;
+            return [ $prefix ];
+        }
         return Resolv_hostname_from_GLOBAL(
-            $hostname, $global_config, $site, $zone, $hosttype
+            $hostname, $ip_type, $global_config, $site, $zone, $hosttype
         );
     }
     else {
@@ -495,7 +508,7 @@
 }
 
 sub __Search_and_resolve_IP {
-    my ( $hostname, $site, $line, $separator, $type_resolve, $hash_subst,
+    my ( $hostname, $ip_type, $site, $line, $separator, $type_resolve, $hash_subst,
         $global_config )
         = @_;
 
@@ -522,9 +535,9 @@
             $match2 =~ s{HOSTNAME}{$hostname};
             $match2 =~ s{POPNAME}{$hash_subst->{'POPNAME'}}xmsg;
             my $resolved = Resolv(
-                $type_resolve, $match2, $global_config, $site
+                $type_resolve, $ip_type, $match2, $global_config, $site
             );
-            if( scalar $resolved ) {
+            if( scalar @{$resolved} ) {
                 if( $separator eq "DUPLICATE" ) {
                     my $templine = "";
                     my $templine2;
@@ -603,7 +616,7 @@
 
 sub Search_and_replace {
     my ( $hostname, $site, $input_file, $type_replace, $pf_config, $separator,
-        $global_config, $type_resolve )
+        $global_config, $type_resolve, $ip_type )
         = @_;
     my $result = [];
 
@@ -631,7 +644,7 @@
     foreach my $line ( @src ) {
         if( $type_replace eq 'resolver' ) {
             $line = __Search_and_resolve_IP(
-                $hostname, $site, $line, $separator,
+                $hostname, $ip_type, $site, $line, $separator,
                 $type_resolve, $subst, $global_config
             );
         }



More information about the pf-tools-commits mailing list