CfgTie/TieNet.pm

filter: Statementstmt 2.4% 2 / 83 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod n/a totaltotal 1.6% 2 / 120 CC 12 SCAR 18.9
CC
12
Cov
2%
EXISTS
12.0
EXISTS
12.0
TIEHASH
12.0
CRAP 6.6
« CfgTie/TieHost.pm ↑ Summary CfgTie/TieProto.pm »
1 #!/usr/bin/perl -Tw
2 #Copyright 1998-1999, Randall Maas. All rights reserved. This program is free
3 #software; you can redistribute it and/or modify it under the same terms as
4 #PERL itself.
5  
6 package CfgTie::TieNet;
7  
8 =head1 NAME
9  
10 CfgTie::TieNet -- A module to tie in the net database
11  
12 =head1 SYNOPSIS
13  
14 tie %net,'CfgTie::TieNet'
15  
16 =head1 DESCRIPTION
17  
18 This is a straightforward hash tie that allows us to access the net database
19 sanely.
20  
21 =head2 Ties
22  
23 There are two ties available for programmers:
24  
25 =over 1
26  
27 =item C<tie %net,'CfgTie::TieNet'>
28  
29 C<$net{$name}> will return a hash reference of the named net information
30  
31 =item C<tie %net_addr,'CfgTIe::TieNet_addr'>
32  
33 C<$net_addr{$addr}> will return a hash reference for the specified network
34 address.
35  
36 =back
37  
38 =head2 Structure of hash
39  
40 Any given net entry has the following information assoicated with it:
41  
42 =over 1
43  
44 =item C<Name>
45  
46 net name
47  
48 =item C<Aliases>
49  
50 A list reference for other names for this net
51  
52 =item C<AddrType>
53  
54 The type of address
55  
56 =item C<Addr>
57  
58 The address
59  
60 =back
61  
62 Additionally, the programmer can set any other associated key, but this
63 information will only available to the running Perl script.
64  
65 =head1 See Also
66  
67 L<CfgTie::Cfgfile>,
68 L<CfgTie::TieAliases>, L<CfgTie::TieGeneric>, L<CfgTie::TieGroup>,
69 L<CfgTie::TieHost>, L<CfgTie::TieNamed>, L<CfgTie::TiePh>,
70 L<CfgTie::TieProto>, L<CfgTie::TieServ>, L<CfgTie::TieShadow>,
71 L<CfgTie::TieUser>
72  
73 =head1 Caveats
74  
75 The current version does cache some net information.
76  
77 =head1 Author
78  
79 Randall Maas (L<randym@acm.org>)
80  
81 =cut
82  
83 sub TIEHASH
84 0 ▶{
Subroutine TIEHASH: not called CC=1 SCAR=6.9
85 0 my $self = shift;
86 0 my $node = {};
87 0 return bless $node, $self;
88 }
89  
90 sub FIRSTKEY
91 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
92 0 my $self = shift;
93  
94 #Rewind outselves to the beginning.
95 0 setnetent 1;
96  
97 0 &NEXTKEY($self);
98 }
99  
100 sub NEXTKEY
101 0 ▶{
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
102 0 my $self = shift;
103  
104 #Get the next net id in the database.
105 # Get the information from the system and store it for later
106 0 my @x = getnetent;
107 0 ▶ if (! scalar @x) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
108  
109 0 &CfgTie::TieNet_rec'TIEHASH(0,@x);
110 0 return $x[0]; #Corresponds to the name
111 }
112  
113 sub EXISTS
114 0 ▶{
Subroutine EXISTS: not called CC=3 SCAR=24.8
115 0 my ($self,$name) = shift;
116 0 ▶ if (exists $CfgTie::TieNet_rec'by_name{$name}) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieNet_rec::by_name{$name}) 0 0 0
117  
118 # Get the information from the system and store it for later
119 0 my @x = getnetbyname $name;
120 0 ▶ if (! scalar @x) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
121  
122 0 &CfgTie::TieNet_rec'TIEHASH(0,@x);
123 0 return 1;
124 }
125  
126 sub FETCH
127 0 ▶{
Subroutine FETCH: not called CC=2 SCAR=17.9
128 0 my ($self, $name) = @_;
129  
130 #check out our cache first
131 0 ▶ if (exists $CfgTie::TieNet_rec'by_name{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieNet_rec::by_name{$name}) 0 0 0
132 0 {return $CfgTie::TieNet_rec'by_name{$name};}
133  
134 0 my %X;
135 0 tie %X, 'CfgTie::TieNet_rec', getnetbyname $name;
136 0 return bless %X;
137 }
138  
139 #Bug creating, deleting nets is not supported yet.
140 1 1;
141  
142 package CfgTie::TieNet_addr;
143  
144 sub TIEHASH
145 0 ▶{
Subroutine TIEHASH: not called CC=1 SCAR=6.9
146 0 my $self = shift;
147 0 my $node = {};
148 0 return bless $node, $self;
149 }
150  
151 sub FIRSTKEY
152 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
153 0 my $self = shift;
154  
155 #Rewind outselves to the beginning.
156 0 setnetent 1;
157  
158 0 &NEXTKEY($self);
159 }
160  
161 sub NEXTKEY
162 0 ▶{
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
163 0 my $self = shift;
164  
165 #Get the next net id in the database.
166 # Get the information from the system and store it for later
167 0 my @x = getnetent;
168 0 ▶ if (! scalar @x) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
169  
170 0 &CfgTie::TieNet_rec'TIEHASH(0,@x);
171 0 return $x[5]; #Corresponds to the id
172 }
173  
174 sub EXISTS
175 0 ▶{
Subroutine EXISTS: not called CC=3 SCAR=24.8
176 0 my ($self,$addr) = shift;
177 0 ▶ if (exists $CfgTie::TieNet_rec'by_addr{$addr}) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieNet_rec::by_addr{$addr}) 0 0 0
178  
179 # Get the information from the system and store it for later
180 0 my @x = getnetbyaddr $addr, AF_INET;
181 0 ▶ if (! scalar @x) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
182  
183 0 &CfgTie::TieNet_rec'TIEHASH(0,@x);
184 0 return 1;
185 }
186  
187 sub FETCH
188 0 ▶{
Subroutine FETCH: not called CC=2 SCAR=17.9
189 0 my ($self,$addr) = @_;
190  
191 #check out our cache first
192 0 ▶ if (exists $CfgTie::TieNet_rec'by_addr{$addr})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieNet_rec::by_addr{$addr}) 0 0 0
193 0 {return $CfgTie::TieNet_rec'by_addr{$addr};}
194  
195 0 my %X;
196 0 tie %X, 'CfgTie::TieNet_rec', getnetbyaddr($addr, AF_INET);
197 0 return bless %X;
198 }
199  
200 #Bug creating and modifying nets is not supported yet.
201  
202 package CfgTie::TieNet_rec;
203 # A package used by both net_addr and net to retain record information about
204 # a hoost. This is the only way to access netmod.
205  
206 #Two hashes are used for look up
207 # $by_name{$name}
208 # $by_addr{$addr}
209  
210 sub TIEHASH
211 0 ▶{
Subroutine TIEHASH: not called CC=3 SCAR=24.8
212 # Ties a single net to a register...
213 0 my $self = shift;
214 0 my $Name = shift;
215 0 my $Node;
216  
217 0 ▶ if (exists $by_name{$Name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $by_name{$Name}) 0 0 0
218 {
219 #Just update our record...
220 0 $Node = $by_name{$Name};
221 }
222 # else create it
223  
224 0 my $Aliases;
225 0 $Node->{Name} =$Name;
226 0 ($Aliases, $Node->{AddrType}, $Node->{Addr}) = @_;
227 0 $Node->{Aliases} = [split ',',$Aliases];
228  
229 #Cross reference the names
230 0 my $I;
231 0 foreach $I ($Name, $Node->{Aliases})
232 0 {$by_name{$I} = $Node;}
233  
234 0 $by_addr{$Node->{Addr}} = $Node;
235  
236 0 return bless $Node, $self;
237 }
238  
239 sub FIRSTKEY
240 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
241 0 my $self = shift;
242 0 my $a = keys %self;
243 0 return scalar each %self;
244 }
245  
246 sub NEXTKEY
247 0 ▶{
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
248 0 my $self = shift;
249 0 return scalar each %self;
250 }
251  
252 sub EXISTS
253 0 ▶{
Subroutine EXISTS: not called CC=1 SCAR=6.9
254 0 my ($self,$key) = shift;
255 0 return exists $self{$key};
256 }
257  
258 sub FETCH
259 0 ▶{
Subroutine FETCH: not called CC=2 SCAR=17.9
260 0 my $self = shift;
261 0 my $key = shift;
262  
263 0 ▶ if (exists $self{$key}) {return $self{$key};}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self{$key}) 0 0 0
264 }
265  
266 sub STORE
267 0 ▶{
Subroutine STORE: not called CC=1 SCAR=6.9
268 # Changes a setting for the specified net... we basically call netmod
269 0 my ($self,$key,$val) = @_;
270  
271 #Extra setting that will be lost... 8(
272 0 $self{$key}=$val;
273 }
274  
275 sub DELETE
276 0 ▶{
Subroutine DELETE: not called CC=1 SCAR=6.9
277 #Deletes a net setting
278 0 my ($self, $key) = @_;
279  
280 #Just remove our local copy
281 0 delete $self{$key};
282 }
283 1 1;
« CfgTie/TieHost.pm ↑ Summary CfgTie/TieProto.pm »