File: stun-client

package info (click to toggle)
rtpengine 13.5.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,676 kB
  • sloc: ansic: 86,764; perl: 59,422; python: 3,193; sh: 1,030; makefile: 693; asm: 211
file content (97 lines) | stat: -rwxr-xr-x 2,251 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;
use warnings;
use Socket;
use Socket6;
use Digest::SHA qw(hmac_sha1);
use Digest::CRC qw(crc32);

my ($prio, $ip, $port, $username, $pwd) = @ARGV;

my $fd;
my @dests = getaddrinfo($ip, $port, AF_UNSPEC, SOCK_DGRAM);
while (@dests >= 5) {
	my ($fam, $type, $prot, $addr, $canon, @dests) = @dests;
	if (!socket($fd, $fam, $type, $prot)) {
		undef($fd);
		next;
	}
	if (!connect($fd, $addr)) {
		undef($fd);
		next;
	}
	last;
}
$fd or die($!);

my @rand = ('A' .. 'Z', 'a' .. 'z');
my $ufrag = join('', (map {$rand[rand($#rand)]} (1 .. 10)));
my $tract = join('', (map {$rand[rand($#rand)]} (1 .. 12)));
my $control = rand() < .5;
my $tbreak = int(rand(0xffffffff)) * int(rand(0xffffffff));

print("transaction: $tract\n");
print("my username fragment: $ufrag\n");
print(($control?'controlling':'controlled')."\n");
print("tie breaker: $tbreak\n");


my $packet = '';
$packet .= attr(6, "$username:$ufrag");
$packet .= attr($control ? 0x802a : 0x8029, pack('Q', $tbreak));
$packet .= attr(0x24, pack('N', $prio));
$packet .= integrity();
$packet .= fingerprint();
$packet = header() . $packet;

send($fd, $packet, 0) or die $!;
my $buf;
recv($fd, $buf, 200, 0) or die;

my ($code, $length, $cookie, $tract2, $attrs) = unpack('nnN a12 a*', $buf);

if ($cookie == 0x2112A442 || $tract2 ne $tract) {
	printf("code: \%x\n", $code);
	while ($attrs ne '') {
		my ($type, $len, $cont);
		($type, $len, $attrs) = unpack('nn a*', $attrs);
		my $pad = 0;
		while ((($len + $pad) % 4) != 0) {
			$pad++;
		}
		($cont, $pad, $attrs) = unpack("a$len a$pad a*", $attrs);
		printf("  attr type: \%x\n", $type);
		print("  content: $cont\n");
	}
}
else {
	print("not stun: ".unpack('H*', $buf)."\n");
}

exit;


sub attr {
	my ($type, $data) = @_;
	my $len = length($data);
	while ((length($data) % 4) != 0) {
		$data .= "\0";
	}
	return pack('nn a*', $type, $len, $data);
}
sub header {
	my ($add_length) = @_;
	$add_length ||= 0;
	return pack('nnN a12', 1, length($packet) + $add_length, 0x2112A442, $tract);
}
sub integrity {
	my $h = header(24);
	my $hmac = hmac_sha1($h.$packet, $pwd);
	return attr(8, $hmac);
}
sub fingerprint {
	my $h = header(8);
	my $crc = crc32($h.$packet);
	return attr(0x8028, pack('N', ($crc ^ 0x5354554e)));
}