aitap has asked for the wisdom of the Perl Monks concerning the following question:
Hi, Monks!
I'm writing an application which is going to use UDP-based protocol and STUN as a way to build connections between users. I found STUN::RFC_5389 on CPAN. But how do I actually use it?
My code is:
When I then try to send some data to the received address and port (using nc -u <ip address> <port>), nothing shows up there. I even tried sniffing with Wireshark, but there is no incoming traffic.#!/usr/bin/perl use warnings; use strict; use STUN::RFC_5389; use IO::Socket::INET; die "$0 <server> [port]\n" unless $#ARGV >= 0; my ($server,$port) = @ARGV; $port = $port // 3478; my $request = Client STUN::RFC_5389 { request => 1 }; my $lport = 10000+(int rand 55536); my $socket = new IO::Socket::INET ( LocalPort => $lport, PeerAddr => $server, PeerPort => $port, Proto => 'udp', ) || die "connect: $@\n"; send($socket,$request,0); my $response; recv($socket,$response,1024,0); close($socket); my $mapped_address = ${Client STUN::RFC_5389 $response}{attributes}->{ +'MAPPED-ADDRESS'}; print $mapped_address->{address},":",$mapped_address->{port},"\n"; my $listen = new IO::Socket::INET ( LocalPort => $lport, Proto => 'udp', ) || die "bind: $@\n"; my $buf; $|=1; while ($listen->recv($buf,1)) { print $buf } die "recv: $!\n";
It looks that I should somehow open the socket, send the STUN request from it, receive it and stay working with it without closing. After reading UDP: Message Passing I have rewritten the code:
but it hangs on the first recv(). I tried to enable non-blocking mode (using Fcntl), sleep for some time, then recv 1024 bytes and print received data, and nothing showed up. I guess that I'm doing something wrong again.#!/usr/bin/perl use warnings; use strict; use STUN::RFC_5389; use Socket; die "$0 <server> [port]\n" unless $#ARGV >= 0; my ($server,$port) = @ARGV; $port = $port // 3478; $|=1; my $request = Client STUN::RFC_5389 { request => 1 }; my $lport = 10000+(int rand 55536); my $lpaddr = sockaddr_in($port, inet_aton("0.0.0.0")) || die "sockaddr +_in: $!\n"; print "socket: "; socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) && print "\ +n" || die "$!\n"; print "bind: "; bind(SOCKET, $lpaddr) && print "\n" || die "$!\n"; my $siaddr = inet_aton($server) || die "inet_aton: $!\n"; my $spaddr = sockaddr_in($port, $siaddr) || die "sockaddr_in: $!\n"; print "send: "; defined(send(SOCKET, 0, 0, $spaddr)) && print "\n" || die "$!\n"; my $response; print "recv: "; recv(SOCKET, $response, 1024, 0) && print "\n" || die "$!\n"; my $mapped_address = ${Client STUN::RFC_5389 $response}{attributes}->{ +'MAPPED-ADDRESS'}; print $mapped_address->{address},":",$mapped_address->{port},"\n"; print "Listening\n"; my $buf; $|=1; while (recv(SOCKET,$buf,1,0)) { print $buf } die "recv: $!\n";
How do I use STUN properly?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to use STUN? And other UDP-related problems.
by Neighbour (Friar) on Jul 23, 2012 at 12:19 UTC | |
by aitap (Curate) on Jul 23, 2012 at 12:52 UTC | |
|
Re: How to use STUN? And other UDP-related problems.
by bulk88 (Priest) on Jul 23, 2012 at 23:48 UTC |