Click here to Skip to main content
Licence CPOL
First Posted 20 Apr 2007
Views 72,208
Downloads 1,923
Bookmarked 45 times

STUN Client

By Ivar Lumi | 20 Apr 2007
STUN client C# implementation with sample application
1 vote, 5.0%
1

2

3
4 votes, 20.0%
4
15 votes, 75.0%
5
4.78/5 - 20 votes
1 removed
μ 4.63, σa 1.68 [?]
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)

About the Author

Ivar Lumi



Estonia Estonia

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioni want this coding in ios can u provide source code for this or c/C++ library PinmemberMember 84707362:57 23 Jan '12  
AnswerRe: i want this coding in ios can u provide source code for this or c/C++ library PinmemberIvar Lumi3:00 23 Jan '12  
GeneralThanks for the article and source code Pinmembercwienands6:32 8 Oct '10  
QuestionHow i connect to remote machine using Stun Pinmembersr_dusad20:49 16 Apr '10  
AnswerRe: How i connect to remote machine using Stun PinmemberIvar Lumi20:24 17 Apr '10  
AnswerRe: How i connect to remote machine using Stun Pinmemberlok.vikram2:32 26 Aug '10  
GeneralRe: How i connect to remote machine using Stun PinmemberIvar Lumi21:16 26 Aug '10  
AnswerRe: How i connect to remote machine using Stun Pinmembercwienands6:23 8 Oct '10  
GeneralRe: How i connect to remote machine using Stun PinmemberIvar Lumi4:25 9 Oct '10  
QuestionISP internet connection failes after runing the client Pinmemberzugan8:33 27 May '09  
AnswerRe: ISP internet connection failes after runing the client PinmemberIvar Lumi9:58 27 May '09  
GeneralWCF interop STUN Pinmemberngocdonghack20015:10 19 Mar '09  
GeneralRe: WCF interop STUN PinmemberIvar Lumi5:29 19 Mar '09  
GeneralRe: WCF interop STUN Pinmemberngocdonghack200116:00 19 Mar '09  
GeneralRe: WCF interop STUN PinmemberIvar Lumi21:36 19 Mar '09  
GeneralRe: WCF interop STUN Pinmemberngocdonghack20016:46 20 Mar '09  
GeneralRe: WCF interop STUN PinmvpUwe Keim8:47 17 Apr '10  
GeneralRe: WCF interop STUN Pinmembercwienands6:26 8 Oct '10  
QuestionCan someone post Windows XP executable of STUN Client? Pinmembersomi123411:52 26 Nov '08  
Generaldifferent results on different servers Pinmemberprince prince14:52 16 Jul '08  
GeneralRe: different results on different servers PinmemberIvar Lumi7:45 17 Jul '08  
GeneralRe: different results on different servers Pinmemberprince prince4:46 21 Jul '08  
GeneralRe: different results on different servers PinmemberIvar Lumi8:31 21 Jul '08  
GeneralSTUN client on WM5 Pinmemberprince prince1:25 27 Jun '08  
GeneralRe: STUN client on WM5 PinmemberIvar Lumi5:14 27 Jun '08  

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.

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