' Universal Plug and Play ' UPnP is used in this demo to programmatically manipulate ' port forwarding entries on the user's router. Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Threading Module Module1 Function CheckError() If mn.GetErrorFlag() = 1 Then Console.Clear() Console.WriteLine("An unexpected error has occurred. This indicates that Universal Plug and Play is disabled or is not functioning properly on your computer or network.") Console.WriteLine() Console.WriteLine("Details of this error are as follows:") Console.WriteLine("Full error message: " + mn.GetErrorFull()) Console.WriteLine() Console.WriteLine("Operation: " + mn.GetErrorOperation()) Console.WriteLine("Command: " + mn.GetErrorCommand()) Console.WriteLine("Code: " + mn.GetErrorCode().ToString()) Console.WriteLine("Line: " + mn.GetErrorLine().ToString()) Console.WriteLine("File: " + mn.GetErrorFile()) Console.WriteLine() Console.Read() End If Return 0 End Function ' Update NAT non blocking Function DoUpdate(ByVal ReloadInfo As Boolean, ByVal Title As String) If ReloadInfo = True Then ' Start update NAT action without blocking Console.WriteLine("Updating NAT...") Dim Action As Long = mn.UpdateNAT(False) CheckError() ' Wait for update to finish Dim iResult As Integer Do iResult = mn.PollUPNP(Action) Thread.Sleep(1) Loop While iResult = 2 ' Loop until action is completed CheckError() End If ' Display information loaded by mnUpdateNAT Console.Clear() Console.WriteLine("Port map information " + Title) For n = 0 To mn.GetPortMapAmount() - 1 Console.WriteLine(n.ToString() + ": Description: " + mn.GetPortMapDescription(n) + ", " + mn.GetPortMapInternalIP(n) + ", " + mn.GetPortMapInternalPort(n).ToString()) Next n Console.ReadKey() Return 0 End Function Sub Main() ' Disable EM_MESSAGE_BOX (2) and enable EM_SAVE (1) ' so that our application can deal with the error mn.ToggleErrorMode(1) mn.ToggleErrorMode(2) ' 1 = Delete using port map ID ' 2 = Delete using protocol and external port Dim iDeleteMethod As Integer = 2 ' Stores ourt own port map's information Dim sProtocol As String = "TCP" Dim lExternalPort As Long = 2525 ' Start UPNP and NAT ' NAT must be started after UPNP mn.StartUPNP() CheckError() mn.StartNAT(True) CheckError() ' Display current port maps DoUpdate(False, "before adding, editing or deleting") ' Add our own port map mn.AddPortMap(sProtocol, lExternalPort, 4242, "192.168.1.5", False, "DarkNet (demo port forwarding entry)", True) CheckError() DoUpdate(False, "after creating a new port map") Dim iPortMap As UInteger ' Check that our own port map was added correctly Dim iPMExist As UInteger = mn.PortMapExist(sProtocol, lExternalPort) If iPMExist = 1 Then ' Find our port map's ID iPortMap = mn.FindPortMap(sProtocol, lExternalPort) Else CheckError() Console.WriteLine("Port map does not exist! (" + iPMExist + ")") Return End If ' Change setting of our port map mn.SetPortMapInternalIP(iPortMap, "192.168.1.2", True) CheckError() mn.SetPortMapInternalPort(iPortMap, 1000, True) CheckError() mn.SetPortMapDescription(iPortMap, "CHANGED DarkNet (demo)", True) CheckError() DoUpdate(False, "after editing our new port map") ' Delete using port map ID If iDeleteMethod = 1 Then mn.DeletePortMapA(iPortMap, True) CheckError() End If ' Delete using protocol and external port If iDeleteMethod = 2 Then mn.DeletePortMapB(sProtocol, lExternalPort, True) CheckError() End If DoUpdate(False, "after deleting our new port map") ' Unload NAT and UPNP ' UPNP must be unloaded last mn.FinishNAT(True) CheckError() mn.FinishUPNP(True) CheckError() Console.Clear() Console.WriteLine("Finished, press any key to exit") Console.ReadKey() End Sub End Module