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:

#!/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";
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.

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:

#!/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";
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.

How do I use STUN properly?


In reply to How to use STUN? And other UDP-related problems. by aitap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.