Click here to Skip to main content
Click here to Skip to main content

STUN Client

By , 20 Apr 2007
 
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
No Biography provided

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

 

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

You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: server issues PinmemberIvar Lumi9 Dec '12 - 20:14 
Questioni want this coding in ios can u provide source code for this or c/C++ library PinmemberMember 847073623 Jan '12 - 1:57 
i want this coding in ios can u provide source code for this c/C++ library
AnswerRe: i want this coding in ios can u provide source code for this or c/C++ library PinmemberIvar Lumi23 Jan '12 - 2:00 
GeneralThanks for the article and source code Pinmembercwienands8 Oct '10 - 5:32 
Really makes a 'UDP hole punch' solution much easier.
QuestionHow i connect to remote machine using Stun Pinmembersr_dusad16 Apr '10 - 19:49 
Hi
 
Thanks for writing a very good article on Stun client.
I get the public ip and port using your code, but i don't understand yet how to connect with other machine.
can i use the returned socket from stun to coonect with other machine or i have to do something else ?
 
Pls help me
Thanks in advance
AnswerRe: How i connect to remote machine using Stun PinmemberIvar Lumi17 Apr '10 - 19:24 
AnswerRe: How i connect to remote machine using Stun Pinmemberlok.vikram26 Aug '10 - 1:32 
GeneralRe: How i connect to remote machine using Stun PinmemberIvar Lumi26 Aug '10 - 20:16 
AnswerRe: How i connect to remote machine using Stun Pinmembercwienands8 Oct '10 - 5:23 
GeneralRe: How i connect to remote machine using Stun PinmemberIvar Lumi9 Oct '10 - 3:25 
QuestionISP internet connection failes after runing the client Pinmemberzugan27 May '09 - 7:33 
Thanks for the nice article.
few seconds after I run the client ,the internet connection disconnects.
I am connected with adsl modem through windows xp firewall.
 
Do you have any idea why this happens?
 
thanks
AnswerRe: ISP internet connection failes after runing the client PinmemberIvar Lumi27 May '09 - 8:58 
GeneralWCF interop STUN Pinmemberngocdonghack200119 Mar '09 - 4:10 
Dear sir,
 
Your post is great but do you have any ideal about WCF? Does anyone know how to make this work with WCF?
GeneralRe: WCF interop STUN PinmemberIvar Lumi19 Mar '09 - 4:29 
GeneralRe: WCF interop STUN Pinmemberngocdonghack200119 Mar '09 - 15:00 
Thanks for quick ans!
 
Could you please explain more about this and how to use it with WCF?
I've looked for this solution in a long time and really need this!
GeneralRe: WCF interop STUN PinmemberIvar Lumi19 Mar '09 - 20:36 
GeneralRe: WCF interop STUN Pinmemberngocdonghack200120 Mar '09 - 5:46 
GeneralRe: WCF interop STUN PinmvpUwe Keim17 Apr '10 - 7:47 
GeneralRe: WCF interop STUN Pinmembercwienands8 Oct '10 - 5:26 
QuestionCan someone post Windows XP executable of STUN Client? Pinmembersomi123426 Nov '08 - 10:52 
I was unable to build the release configuration on Windows XP machine. Can someone post Windows XP executable of STUN Client?
Generaldifferent results on different servers Pinmemberprince prince16 Jul '08 - 13:52 
Hi,
I tried testing your code with the folowing servers:
stun.xten.net it gives me RestrictedCone while it should give FullCone.
stunserver.org give me FullCone
jstun.javawi.de it gives udpblocked
 
how can I know assure it is working properly? normally it gives me
GeneralRe: different results on different servers PinmemberIvar Lumi17 Jul '08 - 6:45 
GeneralRe: different results on different servers Pinmemberprince prince21 Jul '08 - 3:46 
GeneralRe: different results on different servers PinmemberIvar Lumi21 Jul '08 - 7:31 
GeneralSTUN client on WM5 Pinmemberprince prince27 Jun '08 - 0:25 
Hi,
would this project work fine if I try to recompile for compact framework ?if not what modifications do I need. I understand that the forms are not the same since WM5 is based on WinCE but would the client work fine?
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Ivar Lumi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid