/[mdap]/mdap-server.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 9 by dpavlin, Sun Apr 22 16:40:49 2007 UTC revision 24 by dpavlin, Mon Apr 23 23:11:30 2007 UTC
# Line 3  Line 3 
3  use strict;  use strict;
4  use IO::Socket::Multicast;  use IO::Socket::Multicast;
5  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
6    use Getopt::Long;
7    
8  use constant GROUP => '224.0.0.103';  my $GROUP = '224.0.0.103';
9  use constant PORT  => '3235';  my $PORT  = '3235';
10    
11  my $debug = shift @ARGV;  my $debug = 0;
12    my $quiet = 1;
13    my $verbose = 0;
14    
15  my $resend_search_delay = 3;  my $resend_search_delay = 3;
16  my $tftp_path = '/srv/tftp/';  my $tftp_path = '/srv/tftp/';
17    
18    GetOptions(
19            "port=i"        => \$PORT,
20            "group=s"       => \$GROUP,
21            "debug!"        => \$debug,
22            "quiet!"        => \$quiet,
23            "verbose!"      => \$verbose,
24            "search=i"      => \$resend_search_delay,
25            "tftp=s"        => \$tftp_path,
26    );
27    
28    $quiet = 0 if $verbose;
29    
30    # tab-delimited list of user id/passwd to try on ants
31    my @try_accounts = ( "Administrator\t" );
32    
33    my $passwd_path = $0;
34    $passwd_path =~ s/[^\/]+$/passwd/;
35    
36    if (-e $passwd_path) {
37            open(my $fh, $passwd_path) || die "can't open $passwd_path: $!";
38            while(<$fh>) {
39                    chomp;
40                    next if /^#/ || /^$/ || /^\s+$/;
41                    if (/^\S+\t\S+$/) {
42                            push @try_accounts, $_;
43                    } else {
44                            warn "invalid $passwd_path entry: $_\n";
45                    }
46            }
47            print "found ", $#try_accounts + 1, " accounts to try on password protected ants\n";
48    }
49    
50  warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n";  warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n";
51    
52  sub fw {  sub fw {
# Line 27  sub fw { Line 62  sub fw {
62  sub fw_build {  sub fw_build {
63          my $board_name = shift || return 0;          my $board_name = shift || return 0;
64          my $v = join('.', unpack('CCCC',fw($board_name,0x20,4)) );          my $v = join('.', unpack('CCCC',fw($board_name,0x20,4)) );
65          print "# fw_build $board_name $v\n";          print "# fw_build $board_name $v\n" unless $quiet;
66          return $v;          return $v;
67  }  }
68    
# Line 36  sub fw_exists { Line 71  sub fw_exists {
71          return -e "$tftp_path/$board";          return -e "$tftp_path/$board";
72  }  }
73    
74  my $sock = IO::Socket::Multicast->new(LocalPort=>PORT,ReuseAddr=>1);  my $sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1);
75  $sock->mcast_add(GROUP) || die "Couldn't set group: $!\n";  $sock->mcast_add($GROUP) || die "Couldn't set group: $!\n";
76  $sock->mcast_ttl(1);  $sock->mcast_ttl(1);
77    
78  sub ant2hash {  sub ant2hash {
# Line 54  sub ant2hash { Line 89  sub ant2hash {
89    
90  sub mdap_send {  sub mdap_send {
91          my $data = shift;          my $data = shift;
92          $sock->mcast_send( "${data}", GROUP . ':' . PORT );  
93            my $xor = 0;
94            map { $xor ^= ord($_) } split(//,$data);
95            $data .= sprintf('%02X', $xor);
96    
97            $sock->mcast_send( $data, "${GROUP}:${PORT}" );
98          if ($debug) {          if ($debug) {
99                  warn ">> ", dump( $data ), $/;                  warn ">> ", dump( $data ), $/;
100          } else {          } elsif( ! $quiet ) {
101                  $data =~ s/\s+/ /gi;                  $data =~ s/\s+/ /gi;
102                  warn ">> ", substr($data,0,70), $/;                  warn ">> ", substr($data,0,70), $/;
103          }          }
104  }  }
105    
106    my $ant_passwd;
107    
108    sub ant_credentials {
109            my $ant = shift || die "no ant?";
110            my $i = $ant_passwd->{$ant} || 0;
111            my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);
112            #warn "ant $ant as [$i] $user_id / $user_pwd\n";
113            return ($user_id,$user_pwd);
114    }
115    
116    my $ant_unknown_password;
117    my $ant_ok_password;
118    
119    sub ant_another_passwd {
120            my $ant = shift || die "no ant?";
121            return 0 if ( $ant_unknown_password->{$ant} );
122    
123            $ant_passwd->{$ant}++;
124    
125            if ( $ant_passwd->{$ant} > $#try_accounts ) {
126                    print "$ant ant with unknown password\n";
127                    $ant_unknown_password->{$ant}++;
128                    $ant_passwd->{$ant} = 0;
129                    return 0;
130            }
131            return 1;
132    }
133    
134    my $once;
135    
136    sub once {
137            my $m = join('', @_);
138            $once->{$m}++;
139            print $m if ($once->{$m} == 1);
140    }
141    
142  local $SIG{ALRM} = sub {  local $SIG{ALRM} = sub {
143          mdap_send("ANT-SEARCH MDAP/1.1\r\n46");          mdap_send("ANT-SEARCH MDAP/1.1\r\n");
144          alarm( $resend_search_delay );          alarm( $resend_search_delay );
145  };  };
146    
147  alarm( $resend_search_delay );  alarm( $resend_search_delay );
148    
149  mdap_send("ANT-SEARCH MDAP/1.1\r\n46");  mdap_send("ANT-SEARCH MDAP/1.1\r\n");
150    
151  while (1) {  while (1) {
152          my $data;          my $data;
# Line 85  while (1) { Line 161  while (1) {
161                  my $client_version = $h->{'MDAP-VERSION'};                  my $client_version = $h->{'MDAP-VERSION'};
162                  $mdap_ver = $client_version if ($client_version);                  $mdap_ver = $client_version if ($client_version);
163    
164                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n";                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet;
165    
166                  warn dump($h),$/ if ($debug);                  warn dump($h),$/ if ($debug);
167    
168                  # we are getting our own INFO messages                  # we are getting our own messages (since our source port
169                    # is same as destination)
170                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
171    
172                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
173                    my $seq_nr = $h->{'SEQ-NR'};
174                    #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr);
175    
176                    my ($user_id,$user_pwd) = ant_credentials( $ant );
177    
178                  if ($type eq 'REPLY-ANT-SEARCH') {                  if ($type eq 'REPLY-ANT-SEARCH') {
179                          mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:Administrator\r\nUSER-PWD:\r\n22");                          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} );
180                  } elsif ($type eq 'REPLY-INFO') {                  } elsif ($type eq 'REPLY-INFO') {
181    
182                            if ( $seq_nr < 0 ) {
183                                    if ( $ant_ok_password ) {
184                                            $ant_ok_password->{$ant} = 0;
185                                    } elsif ( ant_another_passwd( $ant ) ) {
186                                            ($user_id,$user_pwd) = ant_credentials( $ant );
187                                            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");
188                                    }
189                                    next;
190                            } else {
191                                    $ant_ok_password->{$ant}++;
192                            }
193    
194                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
195                          if ( fw_exists( $board ) ) {                          if ( fw_exists( $board ) ) {
196                                  if ( $h->{'_BUILD'} ne fw_build( $board ) ) {                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";
197                                          print "UPDATE STEP 1 on ant $ant\n";                                  my $new_build = fw_build( $board );
198                                          mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:Administrator\r\nUSER-PWD:\r\n1F");                                  if ( $build ne $new_build ) {
199                                            print "+ $ant version $build -> $new_build\n";
200                                            mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
201                                  } else {                                  } else {
202                                          print "OK ant $ant allready updated...\n";                                          once "$ant OK version $build",
203                                                    $ant_unknown_password->{$ant} ? ' with unknown password' :
204                                                    $ant_ok_password->{$ant} ? ' password protected' :
205                                                    '',
206                                                    "\n";
207                                  }                                  }
208                          } else {                          } else {
209                                  print "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n";                                  once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n";
210                          }                          }
211                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {
212                                  print "UPDATE STEP 2 on ant $ant\n";                                  print "+ $ant bootp mode re-flash started\n";
213                                  mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:Administrator\r\nUSER-PWD:\r\n5F");                                  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");
214                                    $ant_unknown_password->{$ant} = 0;
215                                    $ant_passwd->{$ant} = 0;
216                                    $ant_ok_password->{$ant} = 0;
217                  } else {                  } else {
218                          print "!! reply ignored ", dump( $h ), $/;                          print "!! reply ignored ", dump( $h ), $/;
219                  }                  }

Legend:
Removed from v.9  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26