'i00 .Net NAT Forwarder
'©i00 Productions All rights reserved
'Created by Kris Bennett
'----------------------------------------------------------------------------------------------------
'All property in this file is and remains the property of i00 Productions, regardless of its usage,
'unless stated otherwise in writing from i00 Productions.
'
'i00 is not and shall not be held accountable for any damages directly or indirectly caused by the
'use or miss-use of this product.
Public Class UPnP
Implements IDisposable
'Variables for the COM objects we are going to use
Private UPnPNAT As NATUPNPLib.UPnPNAT
Public Mappings As NATUPNPLib.IStaticPortMappingCollection
'Returns true if UPnP is enabled
Dim mc_Enabled As Boolean
Public ReadOnly Property UPnPEnabled() As Boolean
Get
Return mc_Enabled
End Get
End Property
'List of protocols that we can port forward to (used in functions)
Public Enum Protocol
TCP
UDP
End Enum
#Region "Constructors / Destructors"
Public Sub New()
'create our COM object to manage port forwards
UPnPNAT = New NATUPNPLib.UPnPNAT
'check that mappings can be obtained from the router and load the mapping COM object to our Mappings variable
Me.GetMappings()
End Sub
'check that mappings can be obtained from the router and load the mapping COM object to our Mappings variable
Private Sub GetMappings()
Try
'try to get the static port mappings from the router... using the NATUPNPLib.UPnPNAT COM library...
Mappings = UPnPNAT.StaticPortMappingCollection()
If Mappings Is Nothing Then
'no static mapping object returned - UPnP is not enabled
mc_Enabled = False
Else
'UPnP is enabled and working
mc_Enabled = True
End If
Catch ex As NotImplementedException
mc_Enabled = False
End Try
End Sub
Private disposedValue As Boolean = False 'To detect redundant calls
'Remove the com objects from memory...
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If Mappings IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(Mappings)
If UPnPNAT IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(UPnPNAT)
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
#Region "Router functions"
'Adds a port forwarding rule to the router...
Public Sub Add(ByVal InternalIP As String, ByVal InternalPort As Integer, ByVal ExternalPort As Integer, ByVal Protocol As Protocol, ByVal Description As String)
'check that UPnP is enabled
If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
'check that the port isnot already mapped
If Exists(ExternalPort, Protocol) Then Throw New Exception("Mapping already exists")
'add the port mapping
Mappings.Add(ExternalPort, Protocol.ToString(), InternalPort, InternalIP, True, Description)
End Sub
'Removes a port mapping rule from the router...
Public Sub Remove(ByVal ExternalPort As Integer, ByVal Protocol As Protocol)
'check that UPnP is enabled
If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
'check that the port isnot already mapped
If Not Exists(ExternalPort, Protocol) Then Throw New ArgumentException("Mapping does not exist")
'remove the port mapping
Mappings.Remove(ExternalPort, Protocol.ToString)
End Sub
'See if the
Public Function Exists(ByVal ExternalPort As Integer, ByVal Protocol As Protocol) As Boolean
'check that UPnP is enabled
If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
'go through each port that is mapped in our UPnP enabled router to see if the port has been mapped...
For Each mapping In Mappings.OfType(Of NATUPNPLib.IStaticPortMapping)()
'if the external port and protocol are what we are looking for then we are mapped...
If mapping.ExternalPort.Equals(ExternalPort) AndAlso mapping.Protocol.ToString.Equals(Protocol.ToString) Then Return True
Next
'we do not have this mapping rule if we got this far
Return False
End Function
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
'check that UPnP is enabled
If Not UPnPEnabled Then Throw New Exception("Could not find a UPnP enabled router")
' Begin checking
For Each mapping In Mappings.OfType(Of NATUPNPLib.IStaticPortMapping)()
'if the external port, protocol, IP, internal port, and description are what we are looking for then we are mapped...
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
Next
'we do not have this mapping rule if we got this far
Return False
End Function
#End Region
End Class