Click here to Skip to main content
11,583,859 members (65,148 online)
Click here to Skip to main content

STUN Client

, 20 Apr 2007 CPOL 144.6K 8.1K 65
Rate this:
Please Sign up or sign in to vote.
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

// 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)

Share

About the Author

Ivar Lumi
Estonia Estonia
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralRe: help!turn client and server Pin
Ivar Lumi29-Oct-07 21:42
memberIvar Lumi29-Oct-07 21:42 
QuestionAny executable file ? Pin
hoanglinh946628-Oct-07 0:22
memberhoanglinh946628-Oct-07 0:22 
AnswerRe: Any executable file ? Pin
Ivar Lumi28-Oct-07 0:58
memberIvar Lumi28-Oct-07 0:58 
GeneralRe: Any executable file ? Pin
hoanglinh946628-Oct-07 1:25
memberhoanglinh946628-Oct-07 1:25 
GeneralRe: Any executable file ? Pin
Ivar Lumi28-Oct-07 1:33
memberIvar Lumi28-Oct-07 1:33 
GeneralRe: Any executable file ? Pin
hoanglinh946628-Oct-07 1:40
memberhoanglinh946628-Oct-07 1:40 
GeneralRe: Any executable file ? Pin
Ivar Lumi28-Oct-07 1:41
memberIvar Lumi28-Oct-07 1:41 
GeneralRe: Any executable file ? Pin
hoanglinh946631-Oct-07 18:41
memberhoanglinh946631-Oct-07 18:41 
GeneralRe: Any executable file ? Pin
Ivar Lumi31-Oct-07 20:18
memberIvar Lumi31-Oct-07 20:18 
GeneralSample Application required Pin
kapil bhavsar19-Sep-07 23:49
memberkapil bhavsar19-Sep-07 23:49 
GeneralRe: Sample Application required Pin
Ivar Lumi20-Sep-07 0:59
memberIvar Lumi20-Sep-07 0:59 
GeneralRe: Sample Application required Pin
kapil bhavsar20-Sep-07 1:51
memberkapil bhavsar20-Sep-07 1:51 
GeneralRe: Sample Application required Pin
Ivar Lumi20-Sep-07 1:55
memberIvar Lumi20-Sep-07 1:55 
GeneralExcellent!!! Pin
hitesh_sharma3-Jul-07 8:19
memberhitesh_sharma3-Jul-07 8:19 
Hello,

Great work!! People like you make CP so good.

Thanks.

Best Regards,
Hitesh
GeneralGreat... and what about STUNT Pin
cdemez25-Apr-07 23:19
membercdemez25-Apr-07 23:19 
GeneralRe: Great... and what about STUNT Pin
Ivar Lumi26-Apr-07 0:07
memberIvar Lumi26-Apr-07 0:07 
GeneralRe: Great... and what about STUNT Pin
cdemez26-Apr-07 1:08
membercdemez26-Apr-07 1:08 
GeneralRe: Great... and what about STUNT Pin
Ivar Lumi26-Apr-07 1:23
memberIvar Lumi26-Apr-07 1:23 
GeneralRe: Great... and what about STUNT Pin
cdemez26-Apr-07 20:56
membercdemez26-Apr-07 20:56 
GeneralRe: Great... and what about STUNT Pin
Ivar Lumi26-Apr-07 21:07
memberIvar Lumi26-Apr-07 21:07 
GeneralRe: Great... and what about STUNT Pin
cdemez26-Apr-07 22:50
membercdemez26-Apr-07 22:50 
GeneralSimply Stunning! Pin
stano24-Apr-07 2:04
memberstano24-Apr-07 2:04 
GeneralRe: Simply Stunning! Pin
Ivar Lumi24-Apr-07 3:07
memberIvar Lumi24-Apr-07 3:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Ivar Lumi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid