-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsdp-update
executable file
·130 lines (117 loc) · 4.38 KB
/
nsdp-update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl -w
#######################################################################
#
# NSDP firmware update script v.1.0b
# Copyright 2012 Konstantin S. Vishnivetsky
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# For contacts:
# WEB: http://www.vishnivetsky.ru
# E-mail: [email protected]
# SkyPE: kvishnivetsky
# ICQ: 328-468-511
# Phone: +7 913 774-7588
#
#######################################################################
use strict;
use utf8;
use IO::Select;
use IO::Socket;
use Data::Dumper;
#######################################################################
# !!! ATTENTION !!!!
# Some settings are empty - fill it with YOUR settings, please.
# !!! ATTENTION !!!!
############ Initial config begin #####################################
my $DEBUG = 0;
my $selfmac = ""; # HWAddr of your network interface Example: "\x00\x05\x5d\x74\x73\x3d" (in most cases you need to set it once)
my $swmac = ""; # HWAddr of your NETGEAR prosafe switch Example: "\xe0\x91\xf5\x93\x6e\x2d"
my $sw_ip = '192.168.0.239'; # IP address of your NETGEAR prosafe switch Example '192.168.0.239'
my $sw_fw = 'GS105E_v1.02.04.HEX'; # Firmware in *.hex file
#my $DST_ADDR = pack('CCCC', 255,255,255,255); # Global broadcast addr - use on single interface host
my $DST_ADDR = pack('CCCC', 255,255,255,255); # Local broadcast addr of your network - use on multiple interface host
############ Initial config end ################
my $seq = rand(65535);
printf "Initializing switch for TFTP upload\n" if $DEBUG;
FwUpdate(63321);
printf "Waiting...\n";
sleep(5);
printf "Sending file %s via TFTP to %s\n", $sw_fw, $sw_ip if $DEBUG;
`atftp -p --verbose --trace -l $sw_fw -r $sw_fw $sw_ip`;
exit 0;
sub FwUpdate {
my $SRC_PORT = shift;
my $sock = IO::Socket::INET->new(
Proto => 'udp',
LocalAddr => '0.0.0.0',
Broadcast => 1,
LocalPort => $SRC_PORT,
ReuseAddr => 1
) or die "Can't bind : $@\n";
my $select = IO::Select->new($sock);
sendFwUpdate($sock, $seq++, $selfmac, $swmac, "password");
my $isAlive = 1;
while($isAlive) {
my @ready = $select->can_read(5);
if (@ready) {
my $data = undef;
my $from = recv($sock, $data, 1500, 0) || die "recv: $!";
my($port, $from_addr) = sockaddr_in($from);
my @hexdata = unpack("C*", $data);
printf "<< %s\n", join(' ', map(sprintf("0x%.2x", $_), @hexdata)) if $DEBUG;
parseNSDP($data);
$isAlive = 0;
} else {
printf STDERR "Timeout!\n";
$sock->close();
exit -1;
}
};
$sock->close();
}
sub sendFwUpdate {
my $sock = shift;
my $seq = shift;
my $selfmac = shift;
my $swmac = shift;
my $password = shift;
my $msg = pack('nC6a6a6Sna4N',
0x0103,
0x00,0x00,0x00,0x00,0x00,0x00,
$selfmac,
$swmac,
0x0000,
$seq,
"NSDP",
0x0000);
$msg .= pack('nna'.length($password), 0x000a, length($password), $password); # TLV with password
$msg .= pack('nnC', 0x0010, 0x0001, 0x01); # DHCP mode On|Off
$msg .= pack('nn', 0xffff, 0x0000); # trailer
my @hexdata = unpack("C*", $msg);
printf ">> %s\n", join(' ', map(sprintf("0x%.2x", $_), @hexdata)) if $DEBUG;
my $dst = sockaddr_in(63322, $DST_ADDR);
$sock->send($msg, 0, $dst) or die "send: $!";
};
sub parseNSDP {
my $data = shift;
my($ctype, $reserved, $selfmac, $dstmac, $reserved1, $seq, $sign, $reserved2) = unpack('na6a6a6a2na4Nn', $data);
my @selfmac_ = unpack('C6', $selfmac);
my @dstmac_ = unpack('C6', $dstmac);
printf "ctype=%.4x\n", $ctype if $DEBUG;
printf "selfmac=%s\n", join(':', map(sprintf("%.2x", $_), @selfmac_)) if $DEBUG;
printf "dstmac=%s\n", join(':', map(sprintf("%.2x", $_), @dstmac_)) if $DEBUG;
printf "seq=%.4x\n", $seq if $DEBUG;
printf "sign=%s\n", $sign if $DEBUG;
};