--- mdap-server.pl 2007/04/23 23:11:30 24 +++ mdap-server.pl 2007/04/24 17:02:41 32 @@ -11,6 +11,7 @@ my $debug = 0; my $quiet = 1; my $verbose = 0; +my $credentials = 0; my $resend_search_delay = 3; my $tftp_path = '/srv/tftp/'; @@ -23,9 +24,11 @@ "verbose!" => \$verbose, "search=i" => \$resend_search_delay, "tftp=s" => \$tftp_path, + "credentials" => \$credentials, ); $quiet = 0 if $verbose; +$credentials = 1 if $debug; # tab-delimited list of user id/passwd to try on ants my @try_accounts = ( "Administrator\t" ); @@ -38,7 +41,7 @@ while(<$fh>) { chomp; next if /^#/ || /^$/ || /^\s+$/; - if (/^\S+\t\S+$/) { + if (/^\S+\s\S+$/) { push @try_accounts, $_; } else { warn "invalid $passwd_path entry: $_\n"; @@ -96,7 +99,7 @@ $sock->mcast_send( $data, "${GROUP}:${PORT}" ); if ($debug) { - warn ">> ", dump( $data ), $/; + warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/; } elsif( ! $quiet ) { $data =~ s/\s+/ /gi; warn ">> ", substr($data,0,70), $/; @@ -104,6 +107,9 @@ } my $ant_passwd; +my $ant_unknown_password; +my $ant_ok_password; +my $ant_flashing; sub ant_credentials { my $ant = shift || die "no ant?"; @@ -113,24 +119,38 @@ return ($user_id,$user_pwd); } -my $ant_unknown_password; -my $ant_ok_password; +sub ant_unknown_password { + my $ant = shift || die "no ant?"; + if ( $ant_unknown_password->{$ant} ) { + $ant_unknown_password->{$ant}--; + } + return $ant_unknown_password->{$ant}; +} sub ant_another_passwd { my $ant = shift || die "no ant?"; - return 0 if ( $ant_unknown_password->{$ant} ); + + return 0 if ant_unknown_password( $ant ); + return 0 if $ant_ok_password->{$ant}; $ant_passwd->{$ant}++; if ( $ant_passwd->{$ant} > $#try_accounts ) { print "$ant ant with unknown password\n"; - $ant_unknown_password->{$ant}++; + $ant_unknown_password->{$ant} = 10; $ant_passwd->{$ant} = 0; return 0; } return 1; } +sub forget_ant { + my $ant = shift || die "no ant?"; + delete $ant_unknown_password->{$ant}; + delete $ant_passwd->{$ant}; + delete $ant_ok_password->{$ant}; +} + my $once; sub once { @@ -139,6 +159,16 @@ print $m if ($once->{$m} == 1); } +my $status = ''; + +sub status { + my $m = join('', @_); + if ($m ne $status) { + print $m; + $status = $m; + } +} + local $SIG{ALRM} = sub { mdap_send("ANT-SEARCH MDAP/1.1\r\n"); alarm( $resend_search_delay ); @@ -163,7 +193,7 @@ print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet; - warn dump($h),$/ if ($debug); + warn dump( $data, $h ),$/ if ($debug); # we are getting our own messages (since our source port # is same as destination) @@ -176,19 +206,21 @@ my ($user_id,$user_pwd) = ant_credentials( $ant ); if ($type eq 'REPLY-ANT-SEARCH') { - mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n") unless ( $ant_unknown_password->{$ant} ); + mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n") unless ant_unknown_password( $ant ); } elsif ($type eq 'REPLY-INFO') { if ( $seq_nr < 0 ) { - if ( $ant_ok_password ) { - $ant_ok_password->{$ant} = 0; - } elsif ( ant_another_passwd( $ant ) ) { +# if ( $ant_ok_password ) { +# $ant_ok_password->{$ant} = 0; +# } elsif ( ant_another_passwd( $ant ) ) { + if ( ant_another_passwd( $ant ) ) { ($user_id,$user_pwd) = ant_credentials( $ant ); mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); } next; } else { $ant_ok_password->{$ant}++; + print "$ant credentials $user_id $user_pwd\n" if ( $credentials && $ant_ok_password->{$ant} == 1 ) || $debug; } my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?"; @@ -204,18 +236,37 @@ $ant_ok_password->{$ant} ? ' password protected' : '', "\n"; + + $ant_flashing->{$ant} = 0; + # green|red|orange|flash|off + mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:system config led=flash\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); + my $waiting = 0; + my $count = 0; + map { + $waiting++ if ($ant_flashing->{$_}); + $count++; + } keys %$ant_flashing; + if ($waiting == 0) { + status "ALL $count ANTS FLASHED to $build\n"; + $ant_flashing = undef; + } else { + status "$waiting of $count ants still flasing upto $build\n"; + } } } else { once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n"; } - } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) { - print "+ $ant bootp mode re-flash started\n"; + } elsif ( $type eq 'REPLY-EXEC-CLI' ) { + if ( $seq_nr == 1 ) { + print "+ $ant bootp mode re-flash started\n" if ($verbose); mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); - $ant_unknown_password->{$ant} = 0; - $ant_passwd->{$ant} = 0; - $ant_ok_password->{$ant} = 0; + forget_ant( $ant ); + $ant_flashing->{$ant}++; + } elsif ( $seq_nr == 2 ) { + print "+ $ant led\n" if ($verbose); + } } else { - print "!! reply ignored ", dump( $h ), $/; + print "!! reply $type ignored ", dump( $h ), $/; } } else {