Results 1 to 17 of 17

Thread: Easy UPnP NAT Traversal

Threaded View

  1. #1

    Thread Starter
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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

Posting Permissions

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



Click Here to Expand Forum to Full Width