` Universal Plug and Play ` UPnP is used in this demo to programmatically manipulate ` port forwarding entries on the user's router. ` Disable EM_MESSAGE_BOX (2) and enable EM_SAVE (1) ` so that our application can deal with the error mn Toggle Error Mode 1 mn Toggle Error Mode 2 ` 1 = Delete using port map ID ` 2 = Delete using protocol and external port #constant iDeleteMethod = 1 ` Stores ourt own port map's information sProtocol$ = "TCP" lExternalPort = 2525 ` Start UPNP and NAT ` NAT must be started after UPNP mn Start UPNP CheckError() mn Start NAT 1 CheckError() ` Display current port maps DoUpdate(0, "before adding, editing or deleting") ` Add our own port map mn Add Port Map sProtocol$,lExternalPort,4242,"192.168.1.5",0,"DarkNet (demo port forwarding entry)",1 CheckError() DoUpdate(0, "after creating a new port map") iPortMap as integer ` Check that our own port map was added correctly iPMExist = mn Port Map Exist(sProtocol$,lExternalPort) if iPMExist = 1 ` Find our port map's ID iPortMap = mn Find Port Map(sProtocol$,lExternalPort) else CheckError() print "Port map does not exist! (" + str$(iPMExist) + ")\n" wait key end endif ` Change setting of our port map mn Set Port Map Internal IP iPortMap,"192.168.1.2",1 CheckError() mn Set Port Map Internal Port iPortMap,1000,1 CheckError() mn Set Port Map Description iPortMap,"CHANGED DarkNet (demo)",1 CheckError() DoUpdate(0, "after editing our new port map") ` Delete using port map ID if iDeleteMethod = 1 mn Delete Port Map A iPortMap,1 CheckError() endif ` Delete using protocol and external port if iDeleteMethod = 2 mn Delete Port Map B sProtocol$,lExternalPort,1 CheckError() endif DoUpdate(0, "after deleting our new port map") ` Unload NAT and UPNP ` UPNP must be unloaded last mn Finish NAT 1 CheckError() mn Finish UPNP 1 CheckError() CLS print "Finished, press any key to end" end ` Check for errors and deal with them function CheckError() if mn Get Error Flag() = 1 CLS Print "An unexpected error has occurred." Print "This indicates that Universal Plug and Play is disabled" Print "or is not functioning properly on your computer or network." Print Print "Details of this error are as follows:" Print "Full error message: " + mn Get Error Full() Print Print "Operation: " + mn Get Error Operation() Print "Command: " + mn Get Error Command() Print "Code: " + str$(mn Get Error Code()) Print "Line: " + str$(mn Get Error Line()) Print "File: " + mn Get Error File() print wait key end endif endfunction ` Update NAT non blocking function DoUpdate(ReloadInfo as boolean, Title as string) if ReloadInfo = 1 ` Start update NAT action without blocking print "Updating NAT..." Action = mn Update NAT(0) CheckError() ` Wait for update to finish iResult as integer repeat iResult = mn Poll UPNP(Action) Sleep 1 until iResult <> 2 ` Loop until action is completed CheckError() endif ` Display information loaded by mnUpdateNAT CLS print "Port map information " + Title for n = 0 to mn Get Port Map Amount()-1 print str$(n) + ": Description: " + mn Get Port Map Description(n) + ", " + mn Get Port Map Internal IP(n) + ", " + str$(mn Get Port Map Internal Port(n)) next n print "Press any key to continue..." wait key endfunction