-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCiscoSTUN.pm
156 lines (126 loc) · 3.55 KB
/
CiscoSTUN.pm
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
package CiscoSTUN;
# CiscoSTUN.pm
use strict;
use warnings;
use Config::IniFiles;
use IO::Socket::IP;
use Term::ANSIColor;
# Needed for FAP:
use FindBin 1.51 qw( $RealBin );
use lib $RealBin;
# Use custom version of FAP:
use Quantar;
use constant MaxLen => 1024; # Max Socket Buffer length.
use constant Read_Timeout => 0.003;
our $STUN_ID;
my $Verbose = 1;
my $ServerSocket;
use constant Port => 1994; # Cisco STUN port is 1994;
my $Connected = 0;
my $Sel;
my $ClientSocket;
my $ClientAddr;
my $ClientPort;
my $ClientIP;
my $DataIndex = 0;
my @Data = [];
##################################################################
# Cisco STUN ####################################################
##################################################################
sub Init {
my ($ConfigFile) = @_;
print color('green'), "Init Cisco STUN.\n", color('reset');
my $cfg = Config::IniFiles->new( -file => $ConfigFile);
$STUN_ID = sprintf("%x", hex($cfg->val('STUN', 'STUN_ID')));
$Verbose =$cfg->val('STUN', 'Verbose');
print " Stun ID = 0x$STUN_ID\n";
print " Verbose = $Verbose\n";
if ($STUN_ID < 1 or $STUN_ID >255) {
die "STUN_ID must be between 1 and 255.\n";
}
$ServerSocket = IO::Socket::IP->new (
#LocalHost => '172.31.7.162',
LocalPort => Port,
Proto => 'tcp',
Listen => SOMAXCONN,
ReuseAddr =>1,
Blocking => 0
) || die " cannot create CiscoUSTUN_ServerSocket $!\n";
print " Server waiting for client connection on port " . Port . ".\n";
# Set timeouts -- may not really be needed
$ServerSocket->timeout(1);
#IO::Socket::Timeout->enable_timeouts_on($ServerSocket);
#$ServerSocket->read_timeout(0.0001);
#$ServerSocket->write_timeout(0.0001);
$DataIndex = 0;
@Data = [];
print "----------------------------------------------------------------------\n";
}
sub Open {
if(($ClientSocket, $ClientAddr) = $ServerSocket->accept()) {
my ($Port, $Client_IP) = sockaddr_in($ClientAddr);
$ClientIP = inet_ntoa($Client_IP);
print color('green'),"CiscoSTUN Connected to client " . inet_ntoa($Client_IP) .
":" . Port . "\n", color('reset');
$ClientSocket->autoflush(1);
$Sel = IO::Select->new($ClientSocket);
$Connected = 1;
return 1;
} else {
print color('yellow'), "CiscoSTUN can not connect.\n", color('reset');
$Connected = 0;
return 0;
}
}
sub Disconnect {
$ServerSocket->close();
}
sub Tx {
my ($Buffer) = @_;
my $STUN_Header = chr(0x08) . chr(0x31) . chr(0x00) . chr(0x00) . chr(0x00) .
chr(length($Buffer)) . chr($STUN_ID); # STUN Header.
my $Data = $STUN_Header . $Buffer;
if ($Connected) {
$ClientSocket->send($Data);
if ($Verbose) { print color('green'), "STUN_Tx sent:\n", color('reset');}
if ($Verbose >= 3) {
print color('magenta');
P25Link::Bytes_2_HexString($Data);
print color('reset');
}
}
}
sub Events {
# Cisco STUN TCP Receiver.
if ($Connected == 1) {
for my $fh ($Sel->can_read(Read_Timeout)) {
my $RemoteHost = $fh->recv(my $Buffer, MaxLen);
if ($Verbose and $RemoteHost) {
print "RemoteHost = $RemoteHost\n";
}
if (length($Buffer) > 7) {
#my $RemoteHost = $SClientSocket->recv(my $Buffer, $MaxLen);
if ($Verbose >= 2) {
print " $RemoteHost STUN Rx Buffer len(" . length($Buffer) . ")\n";
}
if ($Verbose >= 3) {
print color('cyan');
P25Link::Bytes_2_HexString(substr($Buffer, 7, length($Buffer) - 7));
print color('reset');
}
Quantar::HDLC_Rx(substr($Buffer, 7, length($Buffer) - 7));
}
}
}
}
sub GetSTUN_ID {
return($STUN_ID);
}
sub GetSTUN_Connected {
return $Connected;
}
sub Verbose {
my ($Value) = @_;
$Verbose = $Value;
}
1;