Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

STUN Client

Rate me:
Please Sign up or sign in to vote.
4.83/5 (36 votes)
20 Apr 2007CPOL 319.1K   14.8K   85   88
STUN client C# implementation with sample application
Screenshot - stun.jpg

Introduction

STUN - Simple Traversal of User Datagram Protocol (UDP) through Network Address Translators (NATs). In few words, it just helps you to map your local computer IP:port to public IP:port.

STUN working idea is pretty simple. The client just sends a UDP packet out to the STUN server and the server answers back with IP:port you connected. STUN does three tests to detect the NAT type.

In test I, the client sends a STUN Binding Request to a server, 
without any flags set in the CHANGE-REQUEST attribute, 
and without the RESPONSE-ADDRESS attribute. This causes the server 
to send the response back to the address and port that the request came from.
           
In test II, the client sends a Binding Request with both the 
"change IP" and "change port" flags from the CHANGE-REQUEST attribute set.  
              
In test III, the client sends a Binding Request with only the "change port" flag set.
                          
                                    +--------+
                                    |  Test  |
                                    |   I    |
                                    +--------+
                                         |
                                         |
                                         V
                                        /\              /\
                                     N /  \ Y          /  \ Y             +--------+
                      UDP     <-------/Resp\--------->/ IP \------------->|  Test  |
                      Blocked         \ ?  /          \Same/              |   II   |
                                       \  /            \? /               +--------+
                                        \/              \/                    |
                                                         | N                  |
                                                         |                    V
                                                         V                    /\
                                                     +--------+  Sym.      N /  \
                                                     |  Test  |  UDP    <---/Resp\
                                                     |   II   |  Firewall   \ ?  /
                                                     +--------+              \  /
                                                         |                    \/
                                                         V                     |Y
                              /\                         /\                    |
               Symmetric  N  /  \       +--------+   N  /  \                   V
                  NAT  <--- / IP \<-----|  Test  |<--- /Resp\               Open
                            \Same/      |   I    |     \ ?  /               Internet
                             \? /       +--------+      \  /
                              \/                         \/
                              |                           |Y
                              |                           |
                              |                           V
                              |                           Full
                              |                           Cone
                              V              /\
                          +--------+        /  \ Y
                          |  Test  |------>/Resp\---->Restricted
                          |   III  |       \ ?  /
                          +--------+        \  /
                                             \/
                                              |N
                                              |       Port
                                              +------>Restricted

/// <summary>
/// UDP is always blocked.
/// </summary>
UdpBlocked,

/// <summary>
/// No NAT, public IP, no firewall.
/// </summary>
OpenInternet,

/// <summary>
/// No NAT, public IP, but symmetric UDP firewall.
/// </summary>
SymmetricUdpFirewall,

/// <summary>
/// A full cone NAT is one where all requests from the same internal 
/// IP address and port are mapped to the same external IP address and port.
/// Furthermore, any external host can send a packet to the internal host, 
/// by sending a packet to the mapped external address.
/// </summary>
FullCone,

/// <summary>
/// A restricted cone NAT is one where all requests from the same
/// internal IP address and port are mapped to the same external IP address and port.
///  Unlike a full cone NAT, an external host (with IP address X) 
/// can send a packet to the internal host only if the internal host 
/// had previously sent a packet to IP address X.
/// </summary>
RestrictedCone,

/// <summary>
/// A port restricted cone NAT is like a restricted cone NAT, but the restriction 
/// includes port numbers. Specifically, an external host can send a packet, 
/// with source IP address X and source port P, to the internal host only if 
/// the internal host had previously sent a packet to IP address X and port P.
/// </summary>
PortRestrictedCone,

/// <summary>
/// A symmetric NAT is one where all requests 
/// from the same internal IP address and port, 
/// to a specific destination IP address and port, are mapped to the same external 
/// IP address and port.  If the same host sends a packet with the same source address 
/// and port, but to a different destination, a different mapping is used. 
/// Furthermore, only the external host that
/// receives a packet can send a UDP packet back to the internal host.
/// </summary>
Symmetric

Using the Code

C#
// Create new socket for STUN client.
Socket socket = new Socket
    (AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any,0));

// Query STUN server
STUN_Result result = STUN_Client.Query("stunserver.org",3478,socket);
if(result.NetType != STUN_NetType.UdpBlocked){
    // UDP blocked or !!!! bad STUN server
}
else{
    IPEndPoint publicEP = result.PublicEndPoint;
    // Do your stuff
}

History

  • 20.04.2007 - Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Estonia Estonia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldifferent results on different servers Pin
prince prince16-Jul-08 13:52
prince prince16-Jul-08 13:52 
GeneralRe: different results on different servers Pin
Ivar Lumi17-Jul-08 6:45
Ivar Lumi17-Jul-08 6:45 
GeneralRe: different results on different servers Pin
prince prince21-Jul-08 3:46
prince prince21-Jul-08 3:46 
GeneralRe: different results on different servers Pin
Ivar Lumi21-Jul-08 7:31
Ivar Lumi21-Jul-08 7:31 
GeneralSTUN client on WM5 Pin
prince prince27-Jun-08 0:25
prince prince27-Jun-08 0:25 
GeneralRe: STUN client on WM5 Pin
Ivar Lumi27-Jun-08 4:14
Ivar Lumi27-Jun-08 4:14 
GeneralAny server help create tunnel between 2 peer behind NAT Pin
hoanglinh94668-Jun-08 23:32
hoanglinh94668-Jun-08 23:32 
GeneralFirewall is not blocking UDP but Stun Client demo program shows UDP is blocked Pin
Srein5-Jun-08 13:47
Srein5-Jun-08 13:47 
I have STUN server and running the stun client demo program from outside the firewall. I get the NAT type as UdpBlocked but I have Wireshark running on the firewall machine and it shows UDP is transmitted. What do you think the problem? Thanks for the excellent program
GeneralRe: Firewall is not blocking UDP but Stun Client demo program shows UDP is blocked Pin
Ivar Lumi5-Jun-08 19:34
Ivar Lumi5-Jun-08 19:34 
GeneralI want to make a file transfer program !! Help me Pin
hoanglinh94664-Jun-08 21:30
hoanglinh94664-Jun-08 21:30 
GeneralRe: I want to make a file transfer program !! Help me Pin
Ivar Lumi5-Jun-08 4:47
Ivar Lumi5-Jun-08 4:47 
QuestionCan it make port mapping with STUN ? Pin
Bigbermusa30-May-08 4:21
Bigbermusa30-May-08 4:21 
AnswerRe: Can it make port mapping with STUN ? Pin
Ivar Lumi30-May-08 5:24
Ivar Lumi30-May-08 5:24 
GeneralRe: Can it make port mapping with STUN ? Pin
Bigbermusa30-May-08 6:13
Bigbermusa30-May-08 6:13 
GeneralRe: Can it make port mapping with STUN ? Pin
Ivar Lumi30-May-08 7:14
Ivar Lumi30-May-08 7:14 
GeneralRe: Can it make port mapping with STUN ? Pin
Bigbermusa30-May-08 7:29
Bigbermusa30-May-08 7:29 
GeneralRe: Can it make port mapping with STUN ? Pin
Ivar Lumi30-May-08 7:39
Ivar Lumi30-May-08 7:39 
GeneralRe: Can it make port mapping with STUN ? Pin
Bigbermusa30-May-08 8:09
Bigbermusa30-May-08 8:09 
GeneralRe: Can it make port mapping with STUN ? Pin
Ivar Lumi30-May-08 9:12
Ivar Lumi30-May-08 9:12 
GeneralRe: Can it make port mapping with STUN ? Pin
Bigbermusa30-May-08 10:00
Bigbermusa30-May-08 10:00 
GeneralRe: Can it make port mapping with STUN ? Pin
Ivar Lumi30-May-08 10:05
Ivar Lumi30-May-08 10:05 
GeneralRe: Can it make port mapping with STUN ? Pin
Bigbermusa30-May-08 10:29
Bigbermusa30-May-08 10:29 
GeneralRe: Can it make port mapping with STUN ? Pin
Ivar Lumi30-May-08 19:38
Ivar Lumi30-May-08 19:38 
GeneralRe: Can it make port mapping with STUN ? Pin
Bigbermusa30-May-08 19:46
Bigbermusa30-May-08 19:46 
GeneralSTUN - Small typo Pin
Migounette23-May-08 23:26
Migounette23-May-08 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.