dcsimg
Results 1 to 17 of 17

Thread: Easy UPnP NAT Traversal

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Lightbulb Easy UPnP NAT Traversal

    Yes, the lightbulb has come on here.

    A while back I posted PortMapper - UPnP NAT Traversal Class.

    Well I'm not sure how I missed it, but as it happens even that is more effort than really required. It turns out there are more VB-friendly interfaces available, and they work even in XP if the user has the necessary components installed. This MS KB article helps XPers get up to speed:

    How to enable the Universal Plug and Play (UPnP) in Windows XP so that you can connect to Games for Windows LIVE

    Vista and Win7 generally install the necessary items with no special effort on the user's part.


    The NATUPnP Typelib

    Well it turns out there is another typelib you can use. The result is that you can add and remove "port mappings" ("port forwardings?") and get the external IP address of your router with relative ease.

    With more effort you can do things like enumerate the existing port mappings to avoid potential collisions and be notified is the external IP address changes on you during a run. These aren't needed by most programs though, so we can keep things simple here.

    See: Network Address Translation Traversal Interfaces for details.

    Stripping out the UI management logic, the attached demo is doing just a few things:


    Bare Bones: Adding a Mapping & Retrieving External IP
    Code:
    Private Sub cmdAdd_Click()
        Dim UPnPNAT As NATUPNPLib.UPnPNAT
        Dim StaticPortMapping As NATUPNPLib.IStaticPortMapping
        
        Set UPnPNAT = New NATUPNPLib.UPnPNAT
        On Error Resume Next
        Set StaticPortMapping = _
            UPnPNAT.StaticPortMappingCollection.Add(CLng(txtExtPort.Text), _
                                                    "UDP", _
                                                    CLng(txtIntPort.Text), _
                                                    Winsock.LocalIP, _
                                                    True, _
                                                    "Demo Port Mapping")
        If Err Then
            MsgBox CStr(Err.Number)
        Else
            On Error GoTo 0
            MsgBox "Success!"
            ExternalIPAddress = StaticPortMapping.ExternalIPAddress
    End Sub

    Bare Bones: Removing a Port Mapping
    Code:
    Private Sub cmdRemove_Click()
        Dim UPnPNAT As NATUPNPLib.UPnPNAT
        
        Set UPnPNAT = New NATUPNPLib.UPnPNAT
        On Error Resume Next
        UPnPNAT.StaticPortMappingCollection.Remove CLng(txtExtPort.Text), "UDP"
        If Err Then
            MsgBox CStr(Err.Number)
        Else
            On Error GoTo 0
            MsgBox "Success!"
        End If
    End Sub

    The Demo

    In the demo program attached here, you specify your external and internal port numbers. Then click Add to add the mapping, and click Ping as many times as you like to send yourself a UDP "ping" message via the external IP address and external port. Finally click Remove to remove the mapping.

    Your router's logs should show these add/remove actions too.

    Pretty basic.


    Note

    Firewall rules on the PC are another story. Yes, there is another set of interfaces for opening access to your program. Yes, they require admin rights in order to succeed - unlike port mapping.

    The recommendation is that you set up the firewall rules required during installation. Port mappings should be more dynamic though, since small routers normally support a fairly limited total number of mappings. Depending on how you use the ports the user will generally get a firewall prompt on the first run anyway.
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    Oh yes: Tested on XP, Vista, and Windows 7.

    And the "ping numbers" are just random numbers sent as "ping" text for argument's sake (and uniqueness).

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    Hmm, I went back and looked at my notes on the other approach and it seems I had known about this and tried it. It looks like it was unreliable though. Perhaps I wasn't using it right before or I'm just "lucky" this time around.

    Perhaps somebody else can shed some light on this. The notes I retained aren't detailed and I'm no longer sure where I got in trouble before using this approach.

    Sure seems to be working now though!

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    20

    Exclamation Re: Easy UPnP NAT Traversal

    When I clicked Add, it says error 'Object Variable Or with Block Variable Not set' at line Set StaticPortMapping = UPnPNAT.StaticPortMappingCollection.Add(CLng(txtExtPort.Text), _
    "TCP", _
    CLng(txtIntPort.Text), _
    Winsock.LocalIP, _
    True, _
    "FAS")

    I am using Windows 7 64-Bit.The projetc reference is set to c:\Windows\system32\hnetcfg.dll\2.
    I login to internet using WAN Miniport PPPOE.If the Router/Gateway does not support UPnP does this Error raise?. The error displayed in your program is "Add failed, error 0000005B".

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    20

    Exclamation Re: Easy UPnP NAT Traversal

    When I clicked Add, it says error 'Object Variable Or with Block Variable Not set' at line
    Code:
    Set StaticPortMapping = UPnPNAT.StaticPortMappingCollection.Add(CLng(txtExtPort.Text), _
                                                        "TCP", _
                                                        CLng(txtIntPort.Text), _
                                                        Winsock.LocalIP, _
                                                        True, _
                                                        "FAS")
    I am using Windows 7 64-Bit.The projetc reference is set to c:\Windows\system32\hnetcfg.dll\2.
    I login to internet using WAN Miniport PPPOE.If the Router/Gateway does not support UPnP does this Error raise?. The error displayed in your program is "Add failed, error 0000005B".[/QUOTE]

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    Quote Originally Posted by tonydsouza1987 View Post
    When I clicked Add, it says error 'Object Variable Or with Block Variable Not set' at line
    Code:
    Set StaticPortMapping = UPnPNAT.StaticPortMappingCollection.Add(CLng(txtExtPort.Text), _
                                                        "TCP", _
                                                        CLng(txtIntPort.Text), _
                                                        Winsock.LocalIP, _
                                                        True, _
                                                        "FAS")
    I am using Windows 7 64-Bit.The projetc reference is set to c:\Windows\system32\hnetcfg.dll\2.
    I login to internet using WAN Miniport PPPOE.If the Router/Gateway does not support UPnP does this Error raise?. The error displayed in your program is "Add failed, error 0000005B".
    Error &H5B (91) is the same exception as "Object Variable Or with Block Variable Not set."

    Yes, this exception is raised if UPnP is unsupported or turned off at the router. You might also see it if a firewall is blocking UPnP traffic.

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    20

    Question Re: Easy UPnP NAT Traversal

    Thanks for your reply.....
    I have allowed the following programs in Firewall :
    1. Network Discovery
    2. Windows Live Communications Platform(SSDP)
    3. Windows Live communications Platform(UPnP)
    4. Routing and Remote Access


    I have also turned On 'Network Discovery' on home and public networks.
    Still the same error.
    The thing is I want VB client to connect my SQL Server from outside the network.So the solution is I need to manually Port Forward on router right??
    By the way how this UTorrent works on my PC?How my PC gets Incoming connections routed Via Utorrent?

    Within my LAN Network VB Client connects to SQL Server, Outside it doesnt.
    Any other soultion for this to work from outside my LAN Network?I dont have access(username/pwd) to my Mikrotik router.So I htought I would use your code and port forward on router.. This doesnt work...

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    Network Discovery is the one you need opened up.

    If your router has UPnP disabled this code won't help. You'd need access to turn it on or to manually map the required ports inbound to your SQL Server instance (default is TCP 1433).

    I won't comment on the morality of most uses of BitTorrent clients, but yours probably uses a "hole punching" technique not easily achieved using VB.

  9. #9
    Junior Member Shadow-GK's Avatar
    Join Date
    Jul 2009
    Posts
    25

    Re: Easy UPnP NAT Traversal

    OK, here's the deal. My home network is set up like this: Internet->Verizon router->wireless router. When I run the program it says Add successful. Then I try Ping but nothing happens. Then I remove and it says Remove successful. But the weirdest thing is that for external IP it shows the ip of my wireless router, not my actual external ip!! What do I do? Also the firewall on my pc is disabled if anything)

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    Why do you have a NAT router behind a NAT router though? That's pretty... strange.

    In your case you should be using a wireless access point, not a router. Can you disable the routing in this wireless device to make it work as a pass-through access point?

    If so then it becomes a question of whether or not the Verison router accepts UPnP configuration. if not there isn't anything you can do.

  11. #11
    Junior Member Shadow-GK's Avatar
    Join Date
    Jul 2009
    Posts
    25

    Re: Easy UPnP NAT Traversal

    Yeah I figured that would be the best way. I just have this setup cause verizon router didn't have wireless.
    Thanks for the advice.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    Those home "wireless router" devices are sold at high volume so they are cheap even though they incorporate about three network devices in one: a router, a switch, and a wireless bridge/access point.

    What you really need in your case is a separate wireless access point device. These aren't as popular though so they tend to cost as much as (or more than) a consumer wireless router, but they can be found for about the same price if you shop around.

    Cisco, Linksys, D-Link, ZyXEL, and TrendNET are just a few of the brands that you can find out there.

    You need to know what you're buying though.

  13. #13
    Junior Member Shadow-GK's Avatar
    Join Date
    Jul 2009
    Posts
    25

    Re: Easy UPnP NAT Traversal

    Also, have you tried udp hole punching? I don't understand why there are no examples of it anywhere...

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    I don't think hole-punching techniques can be done in VB without the help of a public server.

    Even then it probably won't work if you have totem-poled multiple NAT routers. You are not supposed to do that.
    Last edited by dilettante; Feb 11th, 2012 at 03:48 PM.

  15. #15
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    783

    Re: Easy UPnP NAT Traversal

    dilettante;

    I know this post is old, but it intrigues me. It actually adds the port forwarding and returns the external IP. But when I try to remove the port forward, it returns Error 80070002. There is not a whole lot of info available on this error, and the closest I can come to it is:
    System.IO.FileNotFoundException

    Any ideas?

    J.A. Coutts

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: Easy UPnP NAT Traversal

    Well 0x80070002 is ERROR_FILE_NOT_FOUND. But I suspect this error code is being used in a broad, generic sense. What it probably means is that access for that action was denied by your router.

    I just tried the sample program without even recompiling it, as a standard user. Works just fine and this is on a newer OS with a different router than I was using back then.

  17. #17
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    783

    Re: Easy UPnP NAT Traversal

    Quote Originally Posted by dilettante View Post
    Well 0x80070002 is ERROR_FILE_NOT_FOUND. But I suspect this error code is being used in a broad, generic sense. What it probably means is that access for that action was denied by your router.
    That's kinda the conclusion I came to as well, although I have no way of verifying it. I have had very little success at getting any of the other API calls to work. I was hoping to use this control to recover the external IP address, but I have given up and found another way.

    J.A. Coutts

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width