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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26