/[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

Annotation of /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 86 - (hide annotations)
Wed Dec 5 23:13:56 2007 UTC (15 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 7863 byte(s)
execute plugins even if there is no firmware on disk for device
1 dpavlin 2 #!/usr/bin/perl
2    
3 dpavlin 41 use warnings;
4 dpavlin 2 use strict;
5     use IO::Socket::Multicast;
6     use Data::Dump qw/dump/;
7 dpavlin 19 use Getopt::Long;
8 dpavlin 2
9 dpavlin 47 use lib './lib';
10 dpavlin 49 use MDAP;
11 dpavlin 47
12 dpavlin 19 my $GROUP = '224.0.0.103';
13     my $PORT = '3235';
14 dpavlin 2
15 dpavlin 64 #my $debug = 0; # from MDAP
16 dpavlin 19 my $quiet = 1;
17     my $verbose = 0;
18 dpavlin 31 my $credentials = 0;
19 dpavlin 8
20 dpavlin 49 my $plugins = 1;
21    
22 dpavlin 2 my $resend_search_delay = 3;
23 dpavlin 7 my $tftp_path = '/srv/tftp/';
24 dpavlin 2
25 dpavlin 39 my $flashed_cmd = 'system config led=flash';
26    
27 dpavlin 19 GetOptions(
28     "port=i" => \$PORT,
29     "group=s" => \$GROUP,
30     "debug!" => \$debug,
31     "quiet!" => \$quiet,
32     "verbose!" => \$verbose,
33     "search=i" => \$resend_search_delay,
34     "tftp=s" => \$tftp_path,
35 dpavlin 31 "credentials" => \$credentials,
36 dpavlin 39 "exec=s" => \$flashed_cmd,
37 dpavlin 49 'plugins!' => \$plugins,
38 dpavlin 19 );
39    
40     $quiet = 0 if $verbose;
41 dpavlin 32 $credentials = 1 if $debug;
42 dpavlin 19
43 dpavlin 17 # tab-delimited list of user id/passwd to try on ants
44     my @try_accounts = ( "Administrator\t" );
45 dpavlin 14
46 dpavlin 17 my $passwd_path = $0;
47     $passwd_path =~ s/[^\/]+$/passwd/;
48    
49     if (-e $passwd_path) {
50     open(my $fh, $passwd_path) || die "can't open $passwd_path: $!";
51     while(<$fh>) {
52     chomp;
53     next if /^#/ || /^$/ || /^\s+$/;
54 dpavlin 37 if (/^\S+\s+\S+$/) {
55 dpavlin 17 push @try_accounts, $_;
56     } else {
57     warn "invalid $passwd_path entry: $_\n";
58     }
59     }
60 dpavlin 39 print "found ", $#try_accounts + 1, " accounts to try on password protected ants",
61     $credentials ? " and display credentials" : "", "\n";
62 dpavlin 17 }
63    
64 dpavlin 39 warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\nflashed to current version: $flashed_cmd\n";
65 dpavlin 9
66 dpavlin 7 sub fw {
67     my ($board, $offset,$len) = @_;
68     open(my $fh, "$tftp_path/$board") || die "Can't open image $tftp_path/$board: $!";
69     my $b;
70     seek($fh, $offset, 0) || die "can't seek to $offset: $!";
71     read($fh, $b, $len) || die "can't read $len bytes from $offset: $!";
72     close($fh);
73     return $b;
74     }
75    
76     sub fw_build {
77     my $board_name = shift || return 0;
78     my $v = join('.', unpack('CCCC',fw($board_name,0x20,4)) );
79 dpavlin 19 print "# fw_build $board_name $v\n" unless $quiet;
80 dpavlin 7 return $v;
81     }
82    
83     sub fw_exists {
84     my $board = shift;
85     return -e "$tftp_path/$board";
86     }
87    
88 dpavlin 19 my $sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1);
89     $sock->mcast_add($GROUP) || die "Couldn't set group: $!\n";
90 dpavlin 2 $sock->mcast_ttl(1);
91    
92     sub ant2hash {
93     my $data = shift;
94     my $hash;
95     map {
96     if ( m/:/ ) {
97     my ($n,$v) = split(/:/,$_,2);
98     $hash->{$n} = $v;
99     }
100     } split(/[\n\r]/, $data);
101     return $hash;
102     }
103    
104 dpavlin 69 sub _dump_data {
105     my $data = shift;
106     $data =~ s/\s+/ /gs;
107     return substr($data,0,150);
108     }
109    
110 dpavlin 2 sub mdap_send {
111     my $data = shift;
112 dpavlin 14
113     my $xor = 0;
114     map { $xor ^= ord($_) } split(//,$data);
115     $data .= sprintf('%02X', $xor);
116    
117 dpavlin 19 $sock->mcast_send( $data, "${GROUP}:${PORT}" );
118 dpavlin 9 if ($debug) {
119 dpavlin 32 warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/;
120 dpavlin 19 } elsif( ! $quiet ) {
121 dpavlin 69 warn ">> ",_dump_data($data),$/;
122 dpavlin 9 }
123 dpavlin 2 }
124    
125 dpavlin 20 my $ant_passwd;
126 dpavlin 28 my $ant_unknown_password;
127     my $ant_ok_password;
128     my $ant_flashing;
129 dpavlin 17
130     sub ant_credentials {
131     my $ant = shift || die "no ant?";
132 dpavlin 20 my $i = $ant_passwd->{$ant} || 0;
133 dpavlin 17 my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);
134     #warn "ant $ant as [$i] $user_id / $user_pwd\n";
135     return ($user_id,$user_pwd);
136     }
137    
138 dpavlin 25 sub ant_unknown_password {
139     my $ant = shift || die "no ant?";
140     if ( $ant_unknown_password->{$ant} ) {
141     $ant_unknown_password->{$ant}--;
142     }
143     return $ant_unknown_password->{$ant};
144     }
145    
146 dpavlin 17 sub ant_another_passwd {
147     my $ant = shift || die "no ant?";
148 dpavlin 20
149 dpavlin 25 return 0 if ant_unknown_password( $ant );
150 dpavlin 28 return 0 if $ant_ok_password->{$ant};
151 dpavlin 25
152 dpavlin 20 $ant_passwd->{$ant}++;
153    
154     if ( $ant_passwd->{$ant} > $#try_accounts ) {
155 dpavlin 23 print "$ant ant with unknown password\n";
156 dpavlin 26 $ant_unknown_password->{$ant} = 10;
157 dpavlin 24 $ant_passwd->{$ant} = 0;
158 dpavlin 20 return 0;
159     }
160     return 1;
161 dpavlin 17 }
162    
163 dpavlin 27 sub forget_ant {
164     my $ant = shift || die "no ant?";
165     delete $ant_unknown_password->{$ant};
166     delete $ant_passwd->{$ant};
167     delete $ant_ok_password->{$ant};
168     }
169    
170 dpavlin 30 my $status = '';
171    
172     sub status {
173     my $m = join('', @_);
174     if ($m ne $status) {
175     print $m;
176     $status = $m;
177     }
178     }
179    
180 dpavlin 2 local $SIG{ALRM} = sub {
181 dpavlin 14 mdap_send("ANT-SEARCH MDAP/1.1\r\n");
182 dpavlin 2 alarm( $resend_search_delay );
183     };
184    
185     alarm( $resend_search_delay );
186    
187 dpavlin 14 mdap_send("ANT-SEARCH MDAP/1.1\r\n");
188 dpavlin 7
189 dpavlin 2 while (1) {
190     my $data;
191     next unless $sock->recv($data,1024);
192    
193 dpavlin 9 if ( $data =~ m#^(INFO|ANT-SEARCH|EXEC-CLI|REPLY-\S+)\s(MDAP)/(\d+\.\d+)# ) {
194 dpavlin 2
195 dpavlin 9 my ($type,$proto,$mdap_ver) = ($1,$2,$3);
196 dpavlin 2
197     my $h = ant2hash($data);
198    
199 dpavlin 9 my $client_version = $h->{'MDAP-VERSION'};
200     $mdap_ver = $client_version if ($client_version);
201 dpavlin 7
202 dpavlin 72 # we are getting our own messages (since our source port
203     # is same as destination)
204     next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
205    
206 dpavlin 69 print "<< $type $proto/$mdap_ver << ", length($data), " bytes: ",_dump_data($data),$/ unless $quiet;
207 dpavlin 2
208 dpavlin 32 warn dump( $data, $h ),$/ if ($debug);
209 dpavlin 8
210 dpavlin 9 my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
211 dpavlin 71 my $seq_nr = $1 if (defined $h->{'SEQ-NR'} && $h->{'SEQ-NR'} =~ m/^(-*\d)+/);
212 dpavlin 24 #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr);
213 dpavlin 9
214 dpavlin 17 my ($user_id,$user_pwd) = ant_credentials( $ant );
215    
216 dpavlin 2 if ($type eq 'REPLY-ANT-SEARCH') {
217 dpavlin 25 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 );
218 dpavlin 2 } elsif ($type eq 'REPLY-INFO') {
219 dpavlin 15
220 dpavlin 24 if ( $seq_nr < 0 ) {
221 dpavlin 27 # if ( $ant_ok_password ) {
222     # $ant_ok_password->{$ant} = 0;
223     # } elsif ( ant_another_passwd( $ant ) ) {
224     if ( ant_another_passwd( $ant ) ) {
225 dpavlin 24 ($user_id,$user_pwd) = ant_credentials( $ant );
226 dpavlin 20 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");
227     }
228 dpavlin 15 next;
229 dpavlin 24 } else {
230     $ant_ok_password->{$ant}++;
231 dpavlin 32 print "$ant credentials $user_id $user_pwd\n" if ( $credentials && $ant_ok_password->{$ant} == 1 ) || $debug;
232 dpavlin 15 }
233    
234 dpavlin 7 my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
235     if ( fw_exists( $board ) ) {
236 dpavlin 14 my $build = $h->{'_BUILD'} || die "no _BUILD?";
237 dpavlin 83 $build =~ s/^(\d+)\.(\d+)\.(\d+)\.(\d+).*$/$1.$2.$3.$4/;
238 dpavlin 14 my $new_build = fw_build( $board );
239     if ( $build ne $new_build ) {
240 dpavlin 69 print "+ $ant $board version $build -> $new_build\n";
241 dpavlin 14 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");
242 dpavlin 33 $ant_flashing->{$ant}++;
243 dpavlin 67 next;
244 dpavlin 7 } else {
245 dpavlin 69 once "$ant $board OK version $build",
246 dpavlin 24 $ant_unknown_password->{$ant} ? ' with unknown password' :
247 dpavlin 33 $ant_passwd->{$ant} ? ' password protected' :
248 dpavlin 58 '';
249 dpavlin 32
250 dpavlin 28 $ant_flashing->{$ant} = 0;
251 dpavlin 32 # green|red|orange|flash|off
252 dpavlin 39 mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:$flashed_cmd\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
253 dpavlin 28 my $waiting = 0;
254 dpavlin 29 my $count = 0;
255 dpavlin 28 map {
256     $waiting++ if ($ant_flashing->{$_});
257 dpavlin 29 $count++;
258 dpavlin 28 } keys %$ant_flashing;
259 dpavlin 30 if ($waiting == 0) {
260 dpavlin 69 status "ALL $count ANTS FLASHED\n";
261 dpavlin 30 $ant_flashing = undef;
262     } else {
263 dpavlin 69 status "$waiting of $count ants still flasing\n";
264 dpavlin 29 }
265 dpavlin 7 }
266 dpavlin 47
267 dpavlin 86 } else {
268     once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update";
269     }
270 dpavlin 58
271 dpavlin 86 if ( $plugins ) {
272     foreach my $plugin ( MDAP->plugins ) {
273     warn "## calling $plugin\n" if $debug;
274     if ( my $command = $plugin->check( $h ) ) {
275    
276     warn ">> $plugin $ant $board ",
277     $h->{'ANT-NAME'}, " ",
278     $h->{'_BUILD'}, " ",
279     "\t:$command\n";
280    
281     mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:$command\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
282     last;
283 dpavlin 64 }
284 dpavlin 49 }
285 dpavlin 47 }
286    
287 dpavlin 32 } elsif ( $type eq 'REPLY-EXEC-CLI' ) {
288 dpavlin 71 print "##<< $type $seq_nr\n$data\n" if $debug;
289 dpavlin 32 if ( $seq_nr == 1 ) {
290 dpavlin 64 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");
291 dpavlin 33 forget_ant( $ant ) if ( $ant_flashing->{$ant} );
292 dpavlin 47 } elsif ( $seq_nr < 0 ) {
293 dpavlin 58 warn "EXEC-CLI failed: $data\n";
294 dpavlin 32 }
295 dpavlin 39
296 dpavlin 2 } else {
297 dpavlin 32 print "!! reply $type ignored ", dump( $h ), $/;
298 dpavlin 2 }
299    
300     } else {
301 dpavlin 8 warn "<=" x 15, "\n", $data, "\n", "<=" x 15, "\n";
302 dpavlin 2 }
303     }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26