/[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 17 by dpavlin, Mon Apr 23 17:42:42 2007 UTC
# Line 12  my $debug = shift @ARGV; Line 12  my $debug = shift @ARGV;
12  my $resend_search_delay = 3;  my $resend_search_delay = 3;
13  my $tftp_path = '/srv/tftp/';  my $tftp_path = '/srv/tftp/';
14    
15    # tab-delimited list of user id/passwd to try on ants
16    my @try_accounts = ( "Administrator\t" );
17    
18    my $passwd_path = $0;
19    $passwd_path =~ s/[^\/]+$/passwd/;
20    
21    if (-e $passwd_path) {
22            open(my $fh, $passwd_path) || die "can't open $passwd_path: $!";
23            while(<$fh>) {
24                    chomp;
25                    next if /^#/ || /^$/ || /^\s+$/;
26                    if (/^\S+\t\S+$/) {
27                            push @try_accounts, $_;
28                    } else {
29                            warn "invalid $passwd_path entry: $_\n";
30                    }
31            }
32            print "found ", $#try_accounts, " accounts to try on password protected ants\n";
33    }
34    
35  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";
36    
37  sub fw {  sub fw {
# Line 54  sub ant2hash { Line 74  sub ant2hash {
74    
75  sub mdap_send {  sub mdap_send {
76          my $data = shift;          my $data = shift;
77          $sock->mcast_send( "${data}", GROUP . ':' . PORT );  
78            my $xor = 0;
79            map { $xor ^= ord($_) } split(//,$data);
80            $data .= sprintf('%02X', $xor);
81    
82            $sock->mcast_send( $data, GROUP . ':' . PORT );
83          if ($debug) {          if ($debug) {
84                  warn ">> ", dump( $data ), $/;                  warn ">> ", dump( $data ), $/;
85          } else {          } else {
# Line 63  sub mdap_send { Line 88  sub mdap_send {
88          }          }
89  }  }
90    
91    my $ant_passwd_try;
92    
93    sub ant_credentials {
94            my $ant = shift || die "no ant?";
95            my $i = $ant_passwd_try->{$ant} || 0;
96            my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);
97            #warn "ant $ant as [$i] $user_id / $user_pwd\n";
98            return ($user_id,$user_pwd);
99    }
100    
101    sub ant_another_passwd {
102            my $ant = shift || die "no ant?";
103            $ant_passwd_try->{$ant}++;
104            $ant_passwd_try->{$ant} = 0 if ( $ant_passwd_try->{$ant} > $#try_accounts );
105    }
106    
107  local $SIG{ALRM} = sub {  local $SIG{ALRM} = sub {
108          mdap_send("ANT-SEARCH MDAP/1.1\r\n46");          mdap_send("ANT-SEARCH MDAP/1.1\r\n");
109          alarm( $resend_search_delay );          alarm( $resend_search_delay );
110  };  };
111    
112  alarm( $resend_search_delay );  alarm( $resend_search_delay );
113    
114  mdap_send("ANT-SEARCH MDAP/1.1\r\n46");  mdap_send("ANT-SEARCH MDAP/1.1\r\n");
115    
116  while (1) {  while (1) {
117          my $data;          my $data;
# Line 89  while (1) { Line 130  while (1) {
130    
131                  warn dump($h),$/ if ($debug);                  warn dump($h),$/ if ($debug);
132    
133                  # we are getting our own INFO messages                  # we are getting our own messages (since our source port
134                    # is same as destination)
135                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
136    
137                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
138    
139                    my ($user_id,$user_pwd) = ant_credentials( $ant );
140    
141                  if ($type eq 'REPLY-ANT-SEARCH') {                  if ($type eq 'REPLY-ANT-SEARCH') {
142                          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");
143                  } elsif ($type eq 'REPLY-INFO') {                  } elsif ($type eq 'REPLY-INFO') {
144    
145                            if ($h->{'SEQ-NR'} < 0) {
146                                    warn "!! password protected ant $ant, skipping\n";
147                                    ant_another_passwd( $ant );
148                                    next;
149                            }
150    
151                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
152                          if ( fw_exists( $board ) ) {                          if ( fw_exists( $board ) ) {
153                                  if ( $h->{'_BUILD'} ne fw_build( $board ) ) {                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";
154                                          print "UPDATE STEP 1 on ant $ant\n";                                  my $new_build = fw_build( $board );
155                                          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 ) {
156                                            print "UPDATE STEP 1 on ant $ant version $build -> $new_build\n";
157                                            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");
158                                  } else {                                  } else {
159                                          print "OK ant $ant allready updated...\n";                                          print "OK ant $ant allready updated...\n";
160                                  }                                  }
# Line 110  while (1) { Line 163  while (1) {
163                          }                          }
164                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {
165                                  print "UPDATE STEP 2 on ant $ant\n";                                  print "UPDATE STEP 2 on ant $ant\n";
166                                  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");
167                  } else {                  } else {
168                          print "!! reply ignored ", dump( $h ), $/;                          print "!! reply ignored ", dump( $h ), $/;
169                  }                  }

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

  ViewVC Help
Powered by ViewVC 1.1.26