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