PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
20130302 - Auto port forward for your UPnP router (i00 Nat Forwarder)-VBForums
Results 1 to 3 of 3

Thread: 20130302 - Auto port forward for your UPnP router (i00 Nat Forwarder)

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,337

    Lightbulb 20130302 - Auto port forward for your UPnP router (i00 Nat Forwarder)

    Download

    DO NOT MIRROR
    DO NOT LINK DIRECTLY TO THIS FILE - LINK TO THIS POST INSTEAD

    20130302 - Download

    Total Downloads:

    Downloads per day:


    About

    I wrote this application as I thought there were several things lacking with router port forwards, such as the ability to map port forwards to computer names rather than IP addresses, thus limiting the computer to have a static IP. There are also other advantages such as portability, ease of use etc.

    A summary of advantages are listed below:
    • Allows mapping to computer names as well as IP addresses.
    • Allows computers to have dynamic IP addresses and still have port forwarding (thus allowing easy connectivity to multiple "other" networks without having to change your IP or set it in your router).
    • Circumvents Netgear's restriction on not allowing port redirecting - Netgear routers do not allow you to forward a port on a different external port to the port that it is being hosted on through their interface.
    • Portability - Allows you to take your computer from one network to another, and coupled with a dyndns client have everything "just work" on your new network, imagine being able to move your web server from one network to another without having to change anything!


    The only requirement for this app is a UPnP enabled router.

    I have tested this on Server 2012, but it should work on anything Vista + ... and may work on previous OS' as well.

    I have tested the software on the following brands of router: Netgear, Billion.
    Let me know if it works on your brand too!

    Donations
    None yet... be the first!

    Donate Here - and be sure to put "i00 Nat Forwarder", your user name (or other alias), and if you want the amount disclosed in the description field

    Screen Shots

    Name:  NAT.png
Views: 1689
Size:  21.4 KB


    Method
    The UPnP access is done with the NATUPNPLib.dll that comes with Windows, this requires the SSDP Discovery Service to be enabled (NAT Forwarder will ask you to start it if it is not running when it starts).

    NAT Forwarder can be installed as a service through the interface and automatically checks that ports are open at the interval specified in the settings page.

    The main function of the project is, of course, to automate port forwards through UPnP routers. To do this "NATUPnP 1.0 Type Library" was used. To implement UPnP; first make sure that the "SSDP Discovery Service" is running, to do this click Start then Run, type services.msc and click OK.
    Find the "SSDP Discovery Service" in the list and make sure its status is running, if it is stoped right click on it and select start; if it is disabled, right click on it, select properties set the startup type to automatic, click OK, then right click on the service and click start.

    In your project add a reference to the COM library: "NATUPnP 1.0 Type Library"

    You should now have the ability to manage port forwards from your project.

    To make things easier the below class can be used to manage port forwards:

    vb Code:
    1. 'i00 .Net NAT Forwarder
    2. 'Ši00 Productions All rights reserved
    3. 'Created by Kris Bennett
    4. '----------------------------------------------------------------------------------------------------
    5. 'All property in this file is and remains the property of i00 Productions, regardless of its usage,
    6. 'unless stated otherwise in writing from i00 Productions.
    7. '
    8. 'i00 is not and shall not be held accountable for any damages directly or indirectly caused by the
    9. 'use or miss-use of this product.
    10.  
    11. Public Class UPnP
    12.     Implements IDisposable
    13.  
    14.     'Variables for the COM objects we are going to use
    15.     Private UPnPNAT As NATUPNPLib.UPnPNAT
    16.     Public Mappings As NATUPNPLib.IStaticPortMappingCollection
    17.  
    18.     'Returns true if UPnP is enabled
    19.     Dim mc_Enabled As Boolean
    20.     Public ReadOnly Property UPnPEnabled() As Boolean
    21.         Get
    22.             Return mc_Enabled
    23.         End Get
    24.     End Property
    25.  
    26.     'List of protocols that we can port forward to (used in functions)
    27.     Public Enum Protocol
    28.         TCP
    29.         UDP
    30.     End Enum
    31.  
    32. #Region "Constructors / Destructors"
    33.  
    34.     Public Sub New()
    35.         'create our COM object to manage port forwards
    36.         UPnPNAT = New NATUPNPLib.UPnPNAT
    37.  
    38.         'check that mappings can be obtained from the router and load the mapping COM object to our Mappings variable
    39.         Me.GetMappings()
    40.     End Sub
    41.  
    42.     'check that mappings can be obtained from the router and load the mapping COM object to our Mappings variable
    43.     Private Sub GetMappings()
    44.         Try
    45.             'try to get the static port mappings from the router... using the NATUPNPLib.UPnPNAT COM library...
    46.             Mappings = UPnPNAT.StaticPortMappingCollection()
    47.             If Mappings Is Nothing Then
    48.                 'no static mapping object returned - UPnP is not enabled
    49.                 mc_Enabled = False
    50.             Else
    51.                 'UPnP is enabled and working
    52.                 mc_Enabled = True
    53.             End If
    54.         Catch ex As NotImplementedException
    55.             mc_Enabled = False
    56.         End Try
    57.     End Sub
    58.  
    59.     Private disposedValue As Boolean = False 'To detect redundant calls
    60.  
    61.     'Remove the com objects from memory...
    62.     Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    63.         If Not Me.disposedValue Then
    64.             If Mappings IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(Mappings)
    65.             If UPnPNAT IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(UPnPNAT)
    66.         End If
    67.         Me.disposedValue = True
    68.     End Sub
    69.  
    70.     Public Sub Dispose() Implements IDisposable.Dispose
    71.         Dispose(True)
    72.         GC.SuppressFinalize(Me)
    73.     End Sub
    74.  
    75. #End Region
    76.  
    77. #Region "Router functions"
    78.  
    79.     'Adds a port forwarding rule to the router...
    80.     Public Sub Add(ByVal InternalIP As String, ByVal InternalPort As Integer, ByVal ExternalPort As Integer, ByVal Protocol As Protocol, ByVal Description As String)
    81.         'check that UPnP is enabled
    82.         If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
    83.         'check that the port isnot already mapped
    84.         If Exists(ExternalPort, Protocol) Then Throw New Exception("Mapping already exists")
    85.         'add the port mapping
    86.         Mappings.Add(ExternalPort, Protocol.ToString(), InternalPort, InternalIP, True, Description)
    87.     End Sub
    88.  
    89.     'Removes a port mapping rule from the router...
    90.     Public Sub Remove(ByVal ExternalPort As Integer, ByVal Protocol As Protocol)
    91.         'check that UPnP is enabled
    92.         If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
    93.         'check that the port isnot already mapped
    94.         If Not Exists(ExternalPort, Protocol) Then Throw New ArgumentException("Mapping does not exist")
    95.         'remove the port mapping
    96.         Mappings.Remove(ExternalPort, Protocol.ToString)
    97.     End Sub
    98.  
    99.     'See if the
    100.     Public Function Exists(ByVal ExternalPort As Integer, ByVal Protocol As Protocol) As Boolean
    101.         'check that UPnP is enabled
    102.         If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
    103.         'go through each port that is mapped in our UPnP enabled router to see if the port has been mapped...
    104.         For Each mapping In Mappings.OfType(Of NATUPNPLib.IStaticPortMapping)()
    105.             'if the external port and protocol are what we are looking for then we are mapped...
    106.             If mapping.ExternalPort.Equals(ExternalPort) AndAlso mapping.Protocol.ToString.Equals(Protocol.ToString) Then Return True
    107.         Next
    108.         'we do not have this mapping rule if we got this far
    109.         Return False
    110.     End Function
    111.  
    112.     Public Function Exists(ByVal ExternalPort As Integer, ByVal Protocol As Protocol, ByVal InternalIP As String, ByVal InternalPort As Integer, ByVal Description As String) As Boolean
    113.         'check that UPnP is enabled
    114.         If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
    115.         ' Begin checking
    116.         For Each mapping In Mappings.OfType(Of NATUPNPLib.IStaticPortMapping)()
    117.             'if the external port, protocol, IP, internal port, and description are what we are looking for then we are mapped...
    118.             If mapping.ExternalPort.Equals(ExternalPort) AndAlso mapping.Protocol.ToString.Equals(Protocol.ToString) AndAlso mapping.InternalClient.Equals(InternalIP) AndAlso mapping.InternalPort.Equals(InternalPort) AndAlso mapping.Description.Equals(Description) Then Return True
    119.         Next
    120.         'we do not have this mapping rule if we got this far
    121.         Return False
    122.     End Function
    123.  
    124. #End Region
    125.  
    126. End Class

    We now have an easy to use wrapper for easy port forwards, some examples of how to use this wrapper are listed below:

    To check UPnP is enabled:
    vb Code:
    1. Using UPnP As New UPnP
    2.     Return UPnP.UPnPEnabled
    3. End Using

    To add a port map:
    vb Code:
    1. Using UPnP As New UPnP
    2.         UPnP.Add(InternalIP As String, InternalPort As Integer, ExternalPort As Integer, Protocol As Protocol, Description As String)
    3.     End Using

    To remove a port map:
    vb Code:
    1. Using UPnP As New UPnP
    2.     UPnP.Remove(ExternalPort As Integer, Protocol As Protocol)
    3. End Using

    To check if a port is mapped:
    vb Code:
    1. Using UPnP As New UPnP
    2.     Return Exists(ExternalPort As Integer, Protocol As Protocol)
    3. End Using
    4.  
    5. 'OR:
    6.  
    7. Using UPnP As New UPnP
    8.     Return Exists(ExternalPort As Integer, Protocol As Protocol, InternalIP As String, InternalPort As Integer, Description As String)
    9. End Using

    To show a list of port mappings from the router:
    vb Code:
    1. Using UPnP As New UPnP
    2.     For Each mapping In UPnP.Mappings.OfType(Of NATUPNPLib.IStaticPortMapping)()
    3.         Debug.Print "Local IP: " & mapping.InternalClient & vbcrlf & _
    4.                     "Local Port: " & mapping.InternalPort & vbcrlf & _
    5.                     "External Port: " & mapping.ExternalPort & vbcrlf & _
    6.                     "Description: " & mapping.Description & vbcrlf & _
    7.                     "Portocol: " & mapping.prot.ToString() & vbcrlf & _
    8.                     "----------"
    9.     Next
    10. End Using

    Version Changes
    20130302

    • Initial Release


    Thanks

    Thanks for downloading... Also please provide feedback, rate this thread and say thanks if this helped you

    Suggestions on possible improvements are much appreciated

    Also I extend a special thanks to the users who thanked / rated this post.

    Thanks again
    Kris

  2. #2
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: 20130302 - Auto port forward for your UPnP router (i00 Nat Forwarder)

    hope there is similar source to vb6
    Last edited by ladoo; Mar 3rd, 2013 at 09:23 PM.

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,337

    Re: 20130302 - Auto port forward for your UPnP router (i00 Nat Forwarder)

    Quote Originally Posted by ladoo View Post
    hope there is similar source to vb6
    EDIT: you can find something like this for VB6 here:

    http://www.vbforums.com/showthread.p...raversal-Class

    Kris

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