-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsdp-discover
executable file
·194 lines (174 loc) · 4.77 KB
/
nsdp-discover
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl -w
#######################################################################
#
# NSDP discovery 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;
use NSDP;
package NSDP;
my $sock = IO::Socket::INET->new(
Proto => 'udp',
LocalAddr => '0.0.0.0',
Broadcast => 1,
LocalPort => 63321,
ReuseAddr => 1
) or die "Can't bind : $@\n";
my $select = IO::Select->new($sock);
my $seq = rand(65535);
my $DEBUG = 0;
my $selfmac = "\x0e\x69\x95\x38\x97\x69";
my $swmac = "\x00\x00\x00\x00\x00\x00";
my $DST_ADDR = pack('CCCC', 172,16,0,255);
my %DEVICES = ();
setDebug($DEBUG);
##############################################
# Stage 1: sending request for MAC and models#
##############################################
printf "Discover sent, waiting for responses...\n" if $DEBUG;
my @info = (
TLV_MAC,
TLV_MODEL,
);
NSDP_send($sock, $DST_ADDR, $selfmac, $swmac, $seq++, join('', map(pack('nn', $_, 0x0000), @info)));
my $isAlive = 1;
while($isAlive) {
my @ready = $select->can_read(1);
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;
NSDP_ParseHeader($data);
my ($resp_mac, $resp_model);
my $offset = 32;
my $done = 0;
while(!$done) {
my ($type, $len) = unpack('nn', substr($data, $offset)); $offset += 4;
if (defined($type) && defined($len)) {
my $val = substr($data, $offset, $len); $offset += $len;
if ($type == TLV_MODEL) { # Model Name
$val =~ s/ +//;
$resp_model = $val;
next;
}
if ($type == TLV_MAC) { # MAC
$resp_mac = $val;
next;
}
} else {
$done = 1;
$DEVICES{$resp_mac} = $resp_model;
}
}
} else {
$isAlive = 0;
}
};
################################################
# Stage 2: sending individual requests for info#
################################################
foreach my $device(keys(%DEVICES)) {
printf "Sending request to %s (%s)\n", join(':', map(sprintf("%.2x", $_), unpack('C*', $device))), $DEVICES{$device} if $DEBUG;
if ($DEVICES{$device} eq 'FS116E') {
@info = (
TLV_MAC,
TLV_MODEL,
TLV_NAME,
)
} else {
@info = (
TLV_MAC,
TLV_MODEL,
TLV_FWVER,
TLV_IP,
TLV_MASK,
TLV_GW,
TLV_NAME,
)
}
NSDP_send($sock, $DST_ADDR, $selfmac, $device, $seq++, join('', map(pack('nn', $_, 0x0000), @info)));
}
$isAlive = 1;
while($isAlive) {
my @ready = $select->can_read(1);
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;
NSDP_ParseHeader($data);
my $offset = 32;
my $done = 0;
while(!$done) {
my ($type, $len) = unpack("nn", substr($data, $offset)); $offset += 4;
if (defined($type) && defined($len)) {
my $val = substr($data, $offset, $len); $offset += $len;
if ($type == TLV_MODEL) { # Model Name
$val =~ s/ +//;
printf "%s ", $val;
next;
}
if ($type == TLV_NAME) { # Device Name
$val =~ s/ +//;
printf "%s ", $val;
next;
}
if ($type == TLV_MAC) { # MAC
printf "%s ", join(':', map(sprintf("%.2x", $_), unpack("C*", $val)));
next;
}
if ($type == TLV_IP) { # IP
printf "%s ", join('.', unpack("C*", $val));
next;
}
if ($type == TLV_MASK) { # MASK
printf "%s ", join('.', unpack("C*", $val));
next;
}
if ($type == TLV_GW) { # GW
printf "%s ", join('.', unpack("C*", $val));
next;
}
if ($type == TLV_FWVER) { # Device Firmware Version
printf "%s ", $val;
next;
}
} else {
print "\n";
$done = 1;
}
}
} else {
$isAlive = 0;
}
};
$sock->close();
exit;