Click here to Skip to main content
15,879,093 members
Articles / Desktop Programming / Win32
Article

Stunner: A STUN Library and Client

Rate me:
Please Sign up or sign in to vote.
3.58/5 (8 votes)
6 Feb 2008CPOL2 min read 68.1K   2.5K   27   10
Stunner is a simple STUN library on top of which a STUN client is built.
Image 1

Introduction

Stunner is a simple STUN library and client built on top of it in C++, which helps to determine the NAT type, STUN mapped IP and port. I did this as an exercise for myself and it's thus based upon RFC 3489. If there is enough interest, then I might work on support for the latest STUN draft. It doesn't support the shared secret requests and responses as described in RFC 3489. The main reason for this is that I don't see any STUN server, which requires or supports the shared secret requests. Again, if there is enough interest then I will add this as well. All the basic classes for parsing and sending STUN requests and responses are there, thus it should be fairly easy to build a STUN server based on the library. The library is single threaded and uses the select I/O model for WinSock functions.

Background

If you are not sure what STUN is, then please refer to RFC 3489.

Using the STUN Client

To determine the NAT type and STUN mapped IP, you can call the following functions:

C++
CStunClientHelper clientHelper ("stun.xten.com"); 
NAT_TYPE Nat = clientHelper.GetNatType (); 
SOCKADDR_IN addr; 
clientHelper.GetStunMappedAddress (&addr); 

The GetNatType function performs the tests as described in RFC 3489 and GetStunMappedAddress function gets you the public IP by sending a binding request.

Points of Interest

In case we fail to get a response for a request then we don't retry eight times as specified in RFC 3489, instead we just try three times. Moreover, we don't wait for ten seconds of receiving a response and check if all further responses to the request have the same IP and perform other validations as described in RFC 3489. The reason for this is to make the STUN client return back in proper time so that the application could use the results, however if you want the behavior described in RFC 3489 then add STRICT_IMPLEMENTATION to the preprocessor definitions. And for logging to a file, add LOG_TO_FILE to the preprocessor definitions.

History

  • 7th February, 2008: Released first version

License

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


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

Comments and Discussions

 
QuestionStunClientHelper is Incompatible with Multiple Interfaces Pin
Member 1501134611-Feb-21 11:57
Member 1501134611-Feb-21 11:57 
Questionfunction CStunBindingResponseMessage::GetChangedAddress() does not have a null check Pin
kimfeng8-Oct-16 23:30
kimfeng8-Oct-16 23:30 
Questionsame in XP Pin
Peter Eugene Coleman31-Mar-14 20:56
Peter Eugene Coleman31-Mar-14 20:56 
QuestionCompiles but doesn't run Pin
Peter Eugene Coleman31-Mar-14 20:55
Peter Eugene Coleman31-Mar-14 20:55 
Hi Hitesh, It compiles ok without error on Win7 and Visual Studio 2012 but when I try to run in in either debug or release mode it says the executable is not found and offers to rebuild it. Whether I say 'yes' or 'no' it then says that the "StunClientDemo.exe" is not recognised as an external nor an internal command.

I am hoping to get some code to open a computer that is behind a router to contacts from the internet. Does this demo have that?

Thanks in advance Hitesh.
GeneralERROR_DETECTING_NAT Pin
lok.vikram9-Aug-10 22:46
lok.vikram9-Aug-10 22:46 
Generalhi Pin
Salim_Imp11-Feb-08 23:47
Salim_Imp11-Feb-08 23:47 
GeneralRe: hi Pin
Hitesh Sharma12-Feb-08 0:58
Hitesh Sharma12-Feb-08 0:58 
GeneralRe: hi Pin
hawkgao012916-Feb-08 18:32
hawkgao012916-Feb-08 18:32 
GeneralRe: hi Pin
somi123426-Nov-08 9:11
somi123426-Nov-08 9:11 
GeneralRe: hi Pin
hawkgao012926-Nov-08 15:12
hawkgao012926-Nov-08 15:12 

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.