| 1 | # | ||||||||||||||||||||
| 2 | #$Id: Watch.pm,v 2.3 2003/10/28 11:09:59 edelrio Exp $ | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # Net::DHCP::Watch | ||||||||||||||||||||
| 5 | # | ||||||||||||||||||||
| 6 | package Net::DHCP::Watch; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | ▶ | use Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use Net::hostent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 2 | ▶ | use IO::Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | require Exporter; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 2 | @ISA = qw(Exporter); | |||||||||||||||||||
| 20 | 2 | @EXPORT = qw(); | |||||||||||||||||||
| 21 | 2 | @EXPORT_OK = qw(); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | $VERSION = do { my @r=(q$Revision: 2.3 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r }; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # | ||||||||||||||||||||
| 26 | # new | ||||||||||||||||||||
| 27 | # | ||||||||||||||||||||
| 28 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 29 | 1 | my $proto = shift; | |||||||||||||||||||
| 30 | 1 | my $params = shift; | |||||||||||||||||||
| 31 | 1 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 32 | 1 | my $self = {}; | |||||||||||||||||||
| 33 | 1 | bless($self, $class); | |||||||||||||||||||
| 34 | 1 | $self->init($params); | |||||||||||||||||||
| 35 | 1 | return $self; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | # | ||||||||||||||||||||
| 38 | # init: initalize parameters. | ||||||||||||||||||||
| 39 | # | ||||||||||||||||||||
| 40 | 1 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=7 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||
| 41 | 1 | my $self = shift; | |||||||||||||||||||
| 42 | 1 | my $params = shift; | |||||||||||||||||||
| 43 | 1 | my $h; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # test if server hostname given is known (name or IP) | ||||||||||||||||||||
| 46 | 1 | $self->{Server} = $params->{server}; | |||||||||||||||||||
| 47 | 1 | ▶ | unless ( $h = gethost($self->{Server}) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | carp "Can not resolve: ",$self->{Server}; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # test if client hostname given is known (name or IP) | ||||||||||||||||||||
| 52 | # and keep only the first IP address. | ||||||||||||||||||||
| 53 | 1 | $self->{Client} = $params->{client}; | |||||||||||||||||||
| 54 | 1 | ▶ | unless ( $h = gethost($self->{Client}) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | carp "Can not resolve: ",$self->{Client}; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 1 | $self->{Client} = $h->addr_list->[0]; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | # test if ethernet address is either an array of six bytes or | ||||||||||||||||||||
| 61 | # a string of hex bytes separated by ':' | ||||||||||||||||||||
| 62 | 1 | $self->{Ether} = $params->{ether}; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | if ( $self->{Ether} =~ m/^([0-9a-f]{1,2}:)+[0-9a-f]{1,2}$/i ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 65 | 1 | my @eth = map( hex, split(':', $self->{Ether}) ); | |||||||||||||||||||
| 66 | 1 | $self->{Ether} = \@eth; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | elsif ( scalar($self->{Ether}) != 6 ) { | ||||||||||||||||||||
| 69 | 0 | croak "Not a good ethernet addres: ",$params->{ether}; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # can we use alarm() ? | ||||||||||||||||||||
| 73 | 1 | ▶ | if ( $Config{d_alarm} eq 'define' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 1 | $self->{_Alarm} = 1; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||
| 77 | 0 | carp "No alarm() function, network operation may hang"; | |||||||||||||||||||
| 78 | 0 | $self->{_Alarm} = 0; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # set the timeout (alarm) | ||||||||||||||||||||
| 82 | 1 | ▶ | $self->{TimeOut} = $params->{timeout} || 10; | ||||||||||||||||||
|
Condition50%1/2
$params->{'timeout'} || 10
MC/DC0%0/1
$params->{'timeout'} || 10
$params->{'timeout'}
Truth table50%1/2
A
A= $params->{'timeout'}
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # initialize status result to zero | ||||||||||||||||||||
| 85 | $self->{Last} = { | ||||||||||||||||||||
| 86 | 1 | Ok => 0, | |||||||||||||||||||
| 87 | Bad => 0, | ||||||||||||||||||||
| 88 | Time => '0000-00-00 00:00:00 GMT' | ||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||
| 90 | 1 | return; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # | ||||||||||||||||||||
| 94 | # watch: opens the udp socket to the server | ||||||||||||||||||||
| 95 | # | ||||||||||||||||||||
| 96 | 1 | ▶ | sub watch { | ||||||||||||||||||
|
Subroutine
watch: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||
| 97 | 1 | my $self = shift; | |||||||||||||||||||
| 98 | 1 | ▶ | if ( $self->{Watcher} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | carp "Already watching."; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||
| 102 | $self->{Watcher} = new IO::Socket::INET( | ||||||||||||||||||||
| 103 | PeerAddr => $self->{Server}, | ||||||||||||||||||||
| 104 | PeerPort => 'bootps(67)', | ||||||||||||||||||||
| 105 | LocalAddr => inet_ntoa($self->{Client}), | ||||||||||||||||||||
| 106 | LocalPort => 'bootpc(68)', | ||||||||||||||||||||
| 107 | Proto => 'udp', | ||||||||||||||||||||
| 108 | Timeout => $self->{TimeOut} | ||||||||||||||||||||
| 109 | ) | ||||||||||||||||||||
| 110 | 1 | ▶ | or carp "Can not watch: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | 1 | return $self->{Watcher}; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # | ||||||||||||||||||||
| 116 | # status: returns the present status | ||||||||||||||||||||
| 117 | # | ||||||||||||||||||||
| 118 | 1 | ▶ | sub status { | ||||||||||||||||||
|
Subroutine
status: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 119 | 1 | my $self = shift; | |||||||||||||||||||
| 120 | # now the watch/unwatch cycle is carried by status. | ||||||||||||||||||||
| 121 | 1 | ▶ | $self->watch unless( $self->{Watcher} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 1 | ▶ | $self->dhcp_query or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 1 | $self->unwatch; | |||||||||||||||||||
| 124 | 1 | return $self->{Last}; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # | ||||||||||||||||||||
| 128 | # dhcp_query: sends an udp packet containig a DHCP message | ||||||||||||||||||||
| 129 | # of type DHCPDISCOVER and listens to the reply. The random transaction id | ||||||||||||||||||||
| 130 | # must match. | ||||||||||||||||||||
| 131 | # | ||||||||||||||||||||
| 132 | 1 | ▶ | sub dhcp_query { | ||||||||||||||||||
|
Subroutine
dhcp_query: called CC=10 SCAR=26.9
Pod
undocumented
| |||||||||||||||||||||
| 133 | 1 | my $self = shift; | |||||||||||||||||||
| 134 | 1 | my $reply; # holdspace for udp reply | |||||||||||||||||||
| 135 | # | ||||||||||||||||||||
| 136 | # Test if socket is ok | ||||||||||||||||||||
| 137 | # | ||||||||||||||||||||
| 138 | 1 | ▶ | unless ( $self->{Watcher} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 0 | carp "Not watching yet!"; | |||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||
| 141 | }; | ||||||||||||||||||||
| 142 | # | ||||||||||||||||||||
| 143 | # Transaction ID | ||||||||||||||||||||
| 144 | # | ||||||||||||||||||||
| 145 | 1 | my $xid = int(rand(2**32-1)); | |||||||||||||||||||
| 146 | # | ||||||||||||||||||||
| 147 | # DHCP Message: Fixed-Format + Options | ||||||||||||||||||||
| 148 | # (see Droms & Lemon, 1999, Apendixes C and D). | ||||||||||||||||||||
| 149 | # | ||||||||||||||||||||
| 150 | my @fields = ( | ||||||||||||||||||||
| 151 | # op | ||||||||||||||||||||
| 152 | 1, | ||||||||||||||||||||
| 153 | # htype | ||||||||||||||||||||
| 154 | 1, | ||||||||||||||||||||
| 155 | # hlen | ||||||||||||||||||||
| 156 | 6, | ||||||||||||||||||||
| 157 | # hops | ||||||||||||||||||||
| 158 | 0, | ||||||||||||||||||||
| 159 | # xid | ||||||||||||||||||||
| 160 | $xid, | ||||||||||||||||||||
| 161 | # secs | ||||||||||||||||||||
| 162 | 0, | ||||||||||||||||||||
| 163 | # flags | ||||||||||||||||||||
| 164 | 0, | ||||||||||||||||||||
| 165 | # ciaddr | ||||||||||||||||||||
| 166 | $self->{Client}, | ||||||||||||||||||||
| 167 | # yiaddr | ||||||||||||||||||||
| 168 | 0, | ||||||||||||||||||||
| 169 | # siaddr | ||||||||||||||||||||
| 170 | 0, | ||||||||||||||||||||
| 171 | # giaddr | ||||||||||||||||||||
| 172 | 0, | ||||||||||||||||||||
| 173 | # chaddr | ||||||||||||||||||||
| 174 | 1 | @{ $self->{Ether} }, | |||||||||||||||||||
| 175 | 0, 0, 0, 0, 0, 0, | ||||||||||||||||||||
| 176 | 0, 0, 0, 0, | ||||||||||||||||||||
| 177 | # sname | ||||||||||||||||||||
| 178 | "\0", | ||||||||||||||||||||
| 179 | # file | ||||||||||||||||||||
| 180 | "\0", | ||||||||||||||||||||
| 181 | # Magic cookie (RFC) | ||||||||||||||||||||
| 182 | 99,130,83,99, | ||||||||||||||||||||
| 183 | # option1 = DHCP-Message | ||||||||||||||||||||
| 184 | 53, | ||||||||||||||||||||
| 185 | # length1 = 1 | ||||||||||||||||||||
| 186 | 1, | ||||||||||||||||||||
| 187 | # value1 = DHCPREQUEST | ||||||||||||||||||||
| 188 | 3 | ||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||
| 190 | 1 | my $query = pack( | |||||||||||||||||||
| 191 | # It's horrible, but it works | ||||||||||||||||||||
| 192 | 'CCCCNnna4NNNCCCCCCCCCCCCCCCCa64a128C*', | ||||||||||||||||||||
| 193 | @fields | ||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||
| 195 | 1 | my $serv_address; | |||||||||||||||||||
| 196 | # I/O eval block | ||||||||||||||||||||
| 197 | 1 | eval { | |||||||||||||||||||
| 198 | # SIG handling for alarm() | ||||||||||||||||||||
| 199 | 0 | ▶ | local $SIG{ALRM} = sub { die "Alarm timeout\n" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 200 | # Send query | ||||||||||||||||||||
| 201 | alarm($self->{TimeOut}) | ||||||||||||||||||||
| 202 | 1 | ▶ | if $self->{_Alarm}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | 1 | $self->{Watcher}->send($query, 0); | |||||||||||||||||||
| 204 | alarm(0) | ||||||||||||||||||||
| 205 | 1 | ▶ | if $self->{_Alarm}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | # Get reply | ||||||||||||||||||||
| 207 | alarm($self->{TimeOut}) | ||||||||||||||||||||
| 208 | 1 | ▶ | if $self->{_Alarm}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 209 | 1 | $serv_address = $self->{Watcher}->recv($reply, 1024, 0); | |||||||||||||||||||
| 210 | alarm(0) | ||||||||||||||||||||
| 211 | 1 | ▶ | if $self->{_Alarm}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 212 | }; | ||||||||||||||||||||
| 213 | # Die if not alarm | ||||||||||||||||||||
| 214 | 1 | ▶ | if($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 215 | 0 | ▶ | carp $@ unless $@ =~ /alarm/i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | # Verify | ||||||||||||||||||||
| 218 | # be sure $ret_xid is not equal to $xid | ||||||||||||||||||||
| 219 | 1 | my $ret_xid = !$xid; | |||||||||||||||||||
| 220 | 1 | ▶ | if ( $reply ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 221 | 0 | $ret_xid = unpack('x4N',$reply); | |||||||||||||||||||
| 222 | } | ||||||||||||||||||||
| 223 | # only if we've got a reply and the reply was correct all is ok. | ||||||||||||||||||||
| 224 | 1 | ▶ | if ( $ret_xid == $xid ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | # Increment Ok count (max: 2**31-1) | ||||||||||||||||||||
| 226 | 0 | $self->{Last}->{Ok} %= 2147483647; | |||||||||||||||||||
| 227 | 0 | $self->{Last}->{Ok}++; | |||||||||||||||||||
| 228 | # Zero Bad | ||||||||||||||||||||
| 229 | 0 | $self->{Last}->{Bad} = 0; | |||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | else { | ||||||||||||||||||||
| 232 | # Zero ok | ||||||||||||||||||||
| 233 | 1 | $self->{Last}->{Ok} = 0; | |||||||||||||||||||
| 234 | # Increment Bad count (max: 2**31-1) | ||||||||||||||||||||
| 235 | 1 | $self->{Last}->{Bad} %= 2147483647; | |||||||||||||||||||
| 236 | 1 | $self->{Last}->{Bad}++; | |||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | # Get present time (GMT) | ||||||||||||||||||||
| 239 | 1 | $self->{Last}->{Time} = gmtime; | |||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | # | ||||||||||||||||||||
| 242 | # close: just closes socket. | ||||||||||||||||||||
| 243 | # | ||||||||||||||||||||
| 244 | 2 | ▶ | sub unwatch { | ||||||||||||||||||
|
Subroutine
unwatch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 245 | 2 | my $self = shift; | |||||||||||||||||||
| 246 | 2 | delete $self->{Watcher}; | |||||||||||||||||||
| 247 | } | ||||||||||||||||||||
| 248 | # | ||||||||||||||||||||
| 249 | # Cleanup | ||||||||||||||||||||
| 250 | # | ||||||||||||||||||||
| 251 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 252 | 1 | my $self = shift; | |||||||||||||||||||
| 253 | 1 | $self->unwatch; | |||||||||||||||||||
| 254 | } | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 2 | 1; | |||||||||||||||||||
| 257 | __END__ | ||||||||||||||||||||