/[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 41 - (show annotations)
Fri Nov 16 10:25:01 2007 UTC (16 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 7168 byte(s)
 r44@brr:  dpavlin | 2007-11-16 11:24:58 +0100
 added warnings

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26