File Coverage

blib/lib/DNS/Zone.pm
Criterion Covered Total %
statement 38 107 35.5
branch 5 36 13.8
condition 2 21 9.5
subroutine 10 18 55.5
pod 0 12 0.0
total 55 194 28.3


line stmt bran cond sub pod time code
1             #!/usr/local/bin/perl -w
2             ######################################################################
3             #
4             # DNS/Zone.pm
5             #
6             # $Id: Zone.pm,v 1.7 2003/02/04 15:22:12 awolf Exp $
7             # $Revision: 1.7 $
8             # $Author: awolf $
9             # $Date: 2003/02/04 15:22:12 $
10             #
11             # Copyright (C)2001-2003 Andy Wolf. All rights reserved.
12             #
13             # This library is free software; you can redistribute it and/or
14             # modify it under the same terms as Perl itself.
15             #
16             ######################################################################
17              
18             package DNS::Zone;
19              
20 2     2   765 no warnings 'portable';
  2         4  
  2         72  
21 2     2   25 use 5.6.0;
  2         6  
  2         73  
22 2     2   25 use strict;
  2         3  
  2         62  
23 2     2   9 use warnings;
  2         3  
  2         54  
24              
25 2     2   10 use vars qw($AUTOLOAD);
  2         4  
  2         2589  
26              
27             my $VERSION = '0.85';
28             my $REVISION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
29              
30             sub new {
31 1     1 0 9 my($pkg, $name) = @_;
32 1   33     6 my $class = ref($pkg) || $pkg;
33              
34 1         7 my $self = {
35             '_ID' => undef,
36             'NAME' => $name,
37             'LABELS' => [],
38             };
39            
40 1         4 bless $self, $class;
41            
42 1         6 return $self;
43             }
44              
45             # The id shall only be used to search if
46             # the backend allows to use ids more
47             # efficiently. Setting this attribute
48             # should only be done when reading/writing
49             # from/to the backend (e.g. database)
50             ########################################
51             sub id {
52 0     0 0 0 my($self, $id) = @_;
53            
54 0 0       0 $self->{'_ID'} = $id if($id);
55            
56 0         0 return($self->{'_ID'});
57             }
58              
59             sub name {
60 0     0 0 0 my($self, $name) = @_;
61            
62 0 0       0 $self->{'NAME'} = $name if($name);
63            
64 0         0 return($self->{'NAME'});
65             }
66              
67             #May be used to store a reference to some super
68             #object like a master server.
69             sub master {
70 0     0 0 0 my($self, $ref) = @_;
71            
72 0 0       0 $self->{'MASTER'} = $ref if($ref);
73            
74 0         0 return $self->{'MASTER'};
75             }
76              
77             sub add {
78 4     4 0 6 my($self, $label) = @_;
79            
80 4         7 push @{ $self->{'LABELS'} }, ($label);
  4         10  
81            
82 4         25 return $label;
83             }
84              
85             sub delete {
86 0     0 0 0 my($self, $record) = @_;
87            
88 0         0 my $found = 0;
89            
90 0         0 foreach my $label ($self->labels()) {
91 0         0 my @array = $label->records();
92              
93 0         0 for (my $i=0 ; $array[$i] ; $i++) {
94 0 0       0 if($array[$i] == $record) {
95 0         0 $found = 1;
96 0         0 splice @array, $i, 1;
97             }
98             }
99              
100 0         0 $label->records(@array);
101             }
102            
103 0 0       0 return $found ? $self : undef;
104             }
105              
106             sub label {
107 15     15 0 22 my($self, $ref) = @_;
108 15         17 my $label;
109              
110 15 50 33     74 if(exists $ref->{'NAME'} && $ref->{'NAME'}) {
    0 0        
111 15         36 for ($self->labels()) {
112 29 100       76 $label = $_ if($_->label() eq $ref->{'NAME'});
113             }
114             }
115             elsif(exists $ref->{'ID'} && $ref->{'ID'}) {
116 0         0 for ($self->labels()) {
117 0 0       0 $label = $_ if($_->id() eq $ref->{'ID'});
118             }
119             }
120            
121 15         1092 return $label;
122             }
123            
124             sub labels {
125 16     16 0 23 my($self, @labels) = @_;
126            
127 16 50       32 $self->{'LABELS'} = \@labels if(scalar @labels);
128            
129 16 50       50 my @result = @{ $self->{'LABELS'} } if(ref($self->{'LABELS'}) eq 'ARRAY');
  16         38  
130              
131 16         48 return @result;
132             }
133              
134             sub sort {
135 0     0 0   my($self) = @_;
136              
137 0           my @result = sort {
138 0           my(@a) = reverse split /\./, $a->label();
139 0           my(@b) = reverse split /\./, $b->label();
140            
141 0   0       for(my $i=0 ; $a[$i] || $b[$i] ; $i++) {
142 0 0 0       if($a[$i] && $b[$i]) {
    0 0        
    0 0        
143 0 0         return ($a[$i] cmp $b[$i]) if($a[$i] cmp $b[$i]);
144             }
145             elsif($a[$i] && !$b[$i]) {
146 0           return 1;
147             }
148             elsif(!$a[$i] && $b[$i]) {
149 0           return -1;
150             }
151             else {
152 0           return 0;
153             }
154             }
155            
156 0           return 0;
157             } $self->labels();
158              
159 0           $self->labels(@result);
160            
161 0           return $self;
162             }
163              
164             sub dump {
165 0     0 0   my($self) = @_;
166              
167 0           my %ttl_hash;
168 0           my $labellength = 0;
169 0           for my $label ($self->sort()->labels()) {
170 0           my $length = length $label->label();
171 0 0         $labellength = $length if($length > $labellength);
172            
173 0           my @records = $label->records();
174            
175 0           for (@records) {
176 0           my $ttl = $_->ttl();
177            
178 0 0         if(exists $ttl_hash{$ttl}) {
179 0           $ttl_hash{$ttl} += 1;
180             }
181             else {
182 0           $ttl_hash{$ttl} = 1;
183             }
184             }
185             }
186              
187 0           my $ttl_default = 0;
188 0           my $ttl_max = 0;
189 0           for (keys %ttl_hash) {
190 0 0         $ttl_default = $_ if($ttl_hash{$_} > $ttl_max);
191             }
192            
193 0           my $origin = $self->name();
194            
195 0           print '$TTL ', "$ttl_default\n";
196 0           print '$ORIGIN ', "$origin\.\n";
197            
198 0           foreach my $label ($self->labels()) {
199 0           print "\n";
200 0           $label->dump("%-" . $labellength . "s", $origin, $ttl_default);
201             }
202              
203 0           return $self;
204             }
205              
206             sub toXML {
207 0     0 0   my($self) = @_;
208 0           my $result;
209            
210 0           $result .= qq(\n);
211 0           $result .= qq(\n) . $self->name() . qq(\n);
212              
213 0           map { $result .= $_->toXML() } $self->labels();
  0            
214              
215 0           $result .= qq(\n);
216              
217 0           return $result;
218             }
219              
220             sub debug {
221 0     0 0   my($self) = @_;
222              
223 0           eval {
224 2     2   2247 use Data::Dumper;
  2         18620  
  2         263  
225            
226 0           print Dumper($self);
227             };
228            
229 0           return $self;
230             }
231              
232             1;
233              
234             __END__