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

Contents of /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 71 - (show annotations)
Sun Nov 18 10:36:30 2007 UTC (16 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 7816 byte(s)
 r104@brr:  dpavlin | 2007-11-18 11:36:21 +0100
 correctly extract negative seq_nr (errors)

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use IO::Socket::Multicast;
6 use Data::Dump qw/dump/;
7 use Getopt::Long;
8
9 use lib './lib';
10 use MDAP;
11
12 my $GROUP = '224.0.0.103';
13 my $PORT = '3235';
14
15 #my $debug = 0; # from MDAP
16 my $quiet = 1;
17 my $verbose = 0;
18 my $credentials = 0;
19
20 my $plugins = 1;
21
22 my $resend_search_delay = 3;
23 my $tftp_path = '/srv/tftp/';
24
25 my $flashed_cmd = 'system config led=flash';
26
27 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 "credentials" => \$credentials,
36 "exec=s" => \$flashed_cmd,
37 'plugins!' => \$plugins,
38 );
39
40 $quiet = 0 if $verbose;
41 $credentials = 1 if $debug;
42
43 # tab-delimited list of user id/passwd to try on ants
44 my @try_accounts = ( "Administrator\t" );
45
46 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 if (/^\S+\s+\S+$/) {
55 push @try_accounts, $_;
56 } else {
57 warn "invalid $passwd_path entry: $_\n";
58 }
59 }
60 print "found ", $#try_accounts + 1, " accounts to try on password protected ants",
61 $credentials ? " and display credentials" : "", "\n";
62 }
63
64 warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\nflashed to current version: $flashed_cmd\n";
65
66 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 print "# fw_build $board_name $v\n" unless $quiet;
80 return $v;
81 }
82
83 sub fw_exists {
84 my $board = shift;
85 return -e "$tftp_path/$board";
86 }
87
88 my $sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1);
89 $sock->mcast_add($GROUP) || die "Couldn't set group: $!\n";
90 $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 sub _dump_data {
105 my $data = shift;
106 $data =~ s/\s+/ /gs;
107 return substr($data,0,150);
108 }
109
110 sub mdap_send {
111 my $data = shift;
112
113 my $xor = 0;
114 map { $xor ^= ord($_) } split(//,$data);
115 $data .= sprintf('%02X', $xor);
116
117 $sock->mcast_send( $data, "${GROUP}:${PORT}" );
118 if ($debug) {
119 warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/;
120 } elsif( ! $quiet ) {
121 warn ">> ",_dump_data($data),$/;
122 }
123 }
124
125 my $ant_passwd;
126 my $ant_unknown_password;
127 my $ant_ok_password;
128 my $ant_flashing;
129
130 sub ant_credentials {
131 my $ant = shift || die "no ant?";
132 my $i = $ant_passwd->{$ant} || 0;
133 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 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 sub ant_another_passwd {
147 my $ant = shift || die "no ant?";
148
149 return 0 if ant_unknown_password( $ant );
150 return 0 if $ant_ok_password->{$ant};
151
152 $ant_passwd->{$ant}++;
153
154 if ( $ant_passwd->{$ant} > $#try_accounts ) {
155 print "$ant ant with unknown password\n";
156 $ant_unknown_password->{$ant} = 10;
157 $ant_passwd->{$ant} = 0;
158 return 0;
159 }
160 return 1;
161 }
162
163 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 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 local $SIG{ALRM} = sub {
181 mdap_send("ANT-SEARCH MDAP/1.1\r\n");
182 alarm( $resend_search_delay );
183 };
184
185 alarm( $resend_search_delay );
186
187 mdap_send("ANT-SEARCH MDAP/1.1\r\n");
188
189 while (1) {
190 my $data;
191 next unless $sock->recv($data,1024);
192
193 if ( $data =~ m#^(INFO|ANT-SEARCH|EXEC-CLI|REPLY-\S+)\s(MDAP)/(\d+\.\d+)# ) {
194
195 my ($type,$proto,$mdap_ver) = ($1,$2,$3);
196
197 my $h = ant2hash($data);
198
199 my $client_version = $h->{'MDAP-VERSION'};
200 $mdap_ver = $client_version if ($client_version);
201
202 print "<< $type $proto/$mdap_ver << ", length($data), " bytes: ",_dump_data($data),$/ unless $quiet;
203
204 warn dump( $data, $h ),$/ if ($debug);
205
206 # we are getting our own messages (since our source port
207 # is same as destination)
208 next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
209
210 my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
211 my $seq_nr = $1 if (defined $h->{'SEQ-NR'} && $h->{'SEQ-NR'} =~ m/^(-*\d)+/);
212 #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr);
213
214 my ($user_id,$user_pwd) = ant_credentials( $ant );
215
216 if ($type eq 'REPLY-ANT-SEARCH') {
217 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 } elsif ($type eq 'REPLY-INFO') {
219
220 if ( $seq_nr < 0 ) {
221 # if ( $ant_ok_password ) {
222 # $ant_ok_password->{$ant} = 0;
223 # } elsif ( ant_another_passwd( $ant ) ) {
224 if ( ant_another_passwd( $ant ) ) {
225 ($user_id,$user_pwd) = ant_credentials( $ant );
226 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 next;
229 } else {
230 $ant_ok_password->{$ant}++;
231 print "$ant credentials $user_id $user_pwd\n" if ( $credentials && $ant_ok_password->{$ant} == 1 ) || $debug;
232 }
233
234 my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
235 if ( fw_exists( $board ) ) {
236 my $build = $h->{'_BUILD'} || die "no _BUILD?";
237 my $new_build = fw_build( $board );
238 if ( $build ne $new_build ) {
239 print "+ $ant $board version $build -> $new_build\n";
240 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");
241 $ant_flashing->{$ant}++;
242 next;
243 } else {
244 once "$ant $board OK version $build",
245 $ant_unknown_password->{$ant} ? ' with unknown password' :
246 $ant_passwd->{$ant} ? ' password protected' :
247 '';
248
249 $ant_flashing->{$ant} = 0;
250 # green|red|orange|flash|off
251 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");
252 my $waiting = 0;
253 my $count = 0;
254 map {
255 $waiting++ if ($ant_flashing->{$_});
256 $count++;
257 } keys %$ant_flashing;
258 if ($waiting == 0) {
259 status "ALL $count ANTS FLASHED\n";
260 $ant_flashing = undef;
261 } else {
262 status "$waiting of $count ants still flasing\n";
263 }
264 }
265
266 if ( $plugins ) {
267 foreach my $plugin ( MDAP->plugins ) {
268 warn "## calling $plugin\n" if $debug;
269 if ( my $command = $plugin->check( $h ) ) {
270
271 warn ">> $plugin $ant $board ",
272 $h->{'ANT-NAME'}, " ",
273 $h->{'_BUILD'}, " ",
274 "\t:$command\n";
275
276 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");
277 last;
278 }
279 }
280 }
281
282 } else {
283 once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update";
284 }
285
286 } elsif ( $type eq 'REPLY-EXEC-CLI' ) {
287 print "##<< $type $seq_nr\n$data\n" if $debug;
288 if ( $seq_nr == 1 ) {
289 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");
290 forget_ant( $ant ) if ( $ant_flashing->{$ant} );
291 } elsif ( $seq_nr < 0 ) {
292 warn "EXEC-CLI failed: $data\n";
293 }
294
295 } else {
296 print "!! reply $type ignored ", dump( $h ), $/;
297 }
298
299 } else {
300 warn "<=" x 15, "\n", $data, "\n", "<=" x 15, "\n";
301 }
302 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26