Results 1 to 32 of 32

Thread: VB.NET and UPnP?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    VB.NET and UPnP?

    working on an FPS and i'm getting stuck when it comes to making it "EASY" for people to connect.

    it seems that to have their router automatically open a port (?) i will have to use UPnP.

    (the ports i need to open are a simple 45000 and 45001.)

    does anyone have an example of how to use UPnP in VB.NET 2010?

    is it used to open ports (such as UDP) or is it a separate protocol?
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  2. #2

    Re: VB.NET and UPnP?

    There is a way to do it, but you need .NET 3.5 I believe to use the COM object properly. Since you're using 2010, that shouldn't be an issue.

    You need to add a reference to NATUPnP as a COM object in Visual Studio. I use this class to handle my UPnP options:

    Code:
    Public Class UPnP
            Implements IDisposable
    
            Private upnpnat As NATUPNPLib.UPnPNAT
            Private staticMapping As NATUPNPLib.IStaticPortMappingCollection
            Private dynamicMapping As NATUPNPLib.IDynamicPortMappingCollection
    
            Private staticEnabled As Boolean = True
            Private dynamicEnabled As Boolean = True
    
            ''' <summary>
            ''' The different supported protocols
            ''' </summary>
            ''' <remarks></remarks>
            Public Enum Protocol
    
                ''' <summary>
                ''' Transmission Control Protocol
                ''' </summary>
                ''' <remarks></remarks>
                TCP
    
                ''' <summary>
                ''' User Datagram Protocol
                ''' </summary>
                ''' <remarks></remarks>
                UDP
    
            End Enum
    
            ''' <summary>
            ''' Returns if UPnP is enabled.
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property UPnPEnabled As Boolean
                Get
                    Return staticEnabled = True OrElse dynamicEnabled = True
                End Get
            End Property
    
            ''' <summary>
            ''' The UPnP Managed Class
            ''' </summary>
            ''' <remarks></remarks>
            Public Sub New()
    
                'Create the new NAT Class
                upnpnat = New NATUPNPLib.UPnPNAT
    
                'generate the static mappings
                Me.GetStaticMappings()
                Me.GetDynamicMappings()
    
            End Sub
    
            ''' <summary>
            ''' Returns all static port mappings
            ''' </summary>
            ''' <remarks></remarks>
            Private Sub GetStaticMappings()
                Try
                    staticMapping = upnpnat.StaticPortMappingCollection()
                Catch ex As NotImplementedException
                    staticEnabled = False
                End Try
            End Sub
    
            ''' <summary>
            ''' Returns all dynamic port mappings
            ''' </summary>
            ''' <remarks></remarks>
            Private Sub GetDynamicMappings()
                Try
                    dynamicMapping = upnpnat.DynamicPortMappingCollection()
                Catch ex As NotImplementedException
                    dynamicEnabled = False
                End Try
            End Sub
    
            ''' <summary>
            ''' Adds a port mapping to the UPnP enabled device.
            ''' </summary>
            ''' <param name="localIP">The local IP address to map to.</param>
            ''' <param name="Port">The port to forward.</param>
            ''' <param name="prot">The protocol of the port [TCP/UDP]</param>
            ''' <param name="desc">A small description of the port.</param>
            ''' <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception>
            ''' <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception>
            ''' <exception cref="ArgumentException">This exception is thrown when any of the supplied arguments are invalid.</exception>
            ''' <remarks></remarks>
            Public Sub Add(ByVal localIP As String, ByVal Port As Integer, ByVal prot As Protocol, ByVal desc As String)
    
                ' Begin utilizing
                If Exists(Port, prot) Then Throw New ArgumentException("This mapping already exists!", "Port;prot")
    
                ' Check
                If Not IsPrivateIP(localIP) Then Throw New ArgumentException("This is not a local IP address!", "localIP")
    
                ' Final check!
                If Not staticEnabled Then Throw New ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization.")
    
                ' Okay, continue on
                staticMapping.Add(Port, prot.ToString(), Port, localIP, True, desc)
    
            End Sub
    
            ''' <summary>
            ''' Removes a port mapping from the UPnP enabled device.
            ''' </summary>
            ''' <param name="Port">The port to remove.</param>
            ''' <param name="prot">The protocol of the port [TCP/UDP]</param>
            ''' <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception>
            ''' <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception>
            ''' <exception cref="ArgumentException">This exception is thrown when the port [or protocol] is invalid.</exception>
            ''' <remarks></remarks>
            Public Sub Remove(ByVal Port As Integer, ByVal Prot As Protocol)
    
                ' Begin utilizing
                If Not Exists(Port, Prot) Then Throw New ArgumentException("This mapping doesn't exist!", "Port;prot")
    
                ' Final check!
                If Not staticEnabled Then Throw New ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization.")
    
                ' Okay, continue on
                staticMapping.Remove(Port, Prot.ToString)
    
            End Sub
    
            ''' <summary>
            ''' Checks to see if a port exists in the mapping.
            ''' </summary>
            ''' <param name="Port">The port to check.</param>
            ''' <param name="prot">The protocol of the port [TCP/UDP]</param>
            ''' <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception>
            ''' <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception>
            ''' <exception cref="ArgumentException">This exception is thrown when the port [or protocol] is invalid.</exception>
            ''' <remarks></remarks>
            Public Function Exists(ByVal Port As Integer, ByVal Prot As Protocol) As Boolean
    
                ' Final check!
                If Not staticEnabled Then Throw New ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization.")
    
                ' Begin checking
                For Each mapping As NATUPNPLib.IStaticPortMapping In staticMapping
    
                    ' Compare
                    If mapping.ExternalPort.Equals(Port) AndAlso mapping.Protocol.ToString.Equals(Prot.ToString) Then Return True
    
                Next
    
                'Nothing!
                Return False
    
            End Function
    
            ''' <summary>
            ''' Attempts to locate the local IP address of this computer.
            ''' </summary>
            ''' <returns>String</returns>
            ''' <remarks></remarks>
            Public Shared Function LocalIP() As String
                Dim IPList As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
                For Each IPaddress In IPList.AddressList
                    If (IPaddress.AddressFamily = Sockets.AddressFamily.InterNetwork) AndAlso IsPrivateIP(IPaddress.ToString()) Then
                        Return IPaddress.ToString
                    End If
                Next
                Return String.Empty
            End Function
    
            ''' <summary>
            ''' Checks to see if an IP address is a local IP address.
            ''' </summary>
            ''' <param name="CheckIP">The IP address to check.</param>
            ''' <returns>Boolean</returns>
            ''' <remarks></remarks>
            Private Shared Function IsPrivateIP(ByVal CheckIP As String) As Boolean
                Dim Quad1, Quad2 As Integer
    
                Quad1 = CInt(CheckIP.Substring(0, CheckIP.IndexOf(".")))
                Quad2 = CInt(CheckIP.Substring(CheckIP.IndexOf(".") + 1).Substring(0, CheckIP.IndexOf(".")))
                Select Case Quad1
                    Case 10
                        Return True
                    Case 172
                        If Quad2 >= 16 And Quad2 <= 31 Then Return True
                    Case 192
                        If Quad2 = 168 Then Return True
                End Select
                Return False
            End Function
    
            ''' <summary>
            ''' Disposes of the UPnP class
            ''' </summary>
            ''' <param name="disposing">True or False makes no difference.</param>
            ''' <remarks></remarks>
            Protected Overridable Sub Dispose(disposing As Boolean)
                Marshal.ReleaseComObject(staticMapping)
                Marshal.ReleaseComObject(dynamicMapping)
                Marshal.ReleaseComObject(upnpnat)
            End Sub
    
            ''' <summary>
            ''' Dispose!
            ''' </summary>
            ''' <remarks></remarks>
            Public Sub Dispose() Implements IDisposable.Dispose
                Dispose(True)
                GC.SuppressFinalize(Me)
            End Sub
    
            ''' <summary>
            ''' Prints out some debugging information to use.
            ''' </summary>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function Print() As List(Of String)
    
                ' Return list
                Dim L As New List(Of String)
    
                ' Loop through all the data after a check
                If staticEnabled Then
                    For Each mapping As NATUPNPLib.IStaticPortMapping In staticMapping
    
                        ' Add some initial data
                        L.Add("--------------------------------------")
    
                        'Grab the rest
                        L.Add(String.Format("IP: {0}", mapping.InternalClient))
                        L.Add(String.Format("Port: {0}", mapping.InternalPort))
                        L.Add(String.Format("Description: {0}", mapping.Description))
    
                    Next
                End If
    
                'Finisher
                L.Add("--------------------------------------")
    
                ' Give it back
                Return L
    
            End Function
    
        End Class
    I should probably put this up on the Code bank somewhere...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    Thanks formlesstree4

    is it forward compatible with .net 4.0?
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  4. #4

    Re: VB.NET and UPnP?

    Quote Originally Posted by Waxy View Post
    Thanks formlesstree4

    is it forward compatible with .net 4.0?
    I wrote the class in .NET 4 so I would like to think so!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    Ok, i THINK i understand this.
    So UPnP is used to map ports on UPnP enabled devices.

    so, if i'm using this right, then...

    Code:
       Function This() As Boolean
            Dim MyUPnPMap As New ClientUPnP
            If MyUPnPMap.Exists(45000, ClientUPnP.Protocol.UDP) Then
                Return False 'already in use
            Else
                MyUPnPMap.Add(ClientUPnP.LocalIP, 45000, ClientUPnP.Protocol.UDP, "Dust Client")
                Return True 'successfully added
            End If
    
        End Function
    (i hate being such a Network Programming noob. it took me forever just to get UDP over the internet! ugh.)
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  6. #6

    Re: VB.NET and UPnP?

    That should work; make sure LocalIP is proper though before utilizing it; it's a guesswork system I use inside the class.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    Thank you very much, Formless
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    Revising an old thread.
    I've been in hiatus for a while.

    i've added it to my project. the client APPEARS to be working correctly, but i'm getting a crash in the server (no, no client working on current machine, just the server by itself)
    it's the same machine i was testing the client on.

    here's the error:
    Code:
    Irrlicht Engine version 1.8.0-alpha
    Microsoft Windows 7 Ultimate Edition  (Build 7600)
    Irrlicht Lime version 0.9.1
     >> CLOSE PORT....
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >> OPEN PORT...
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >>   Port not in use. Adding...
     >>               IS Private IP(192.168.1.13) ....
     >>                      TRUE - Quad2=168
     >> UPNP ADD:  Sub Add(LocalIP=192.168.1.13, Port=45000, prot=UDP, desc=Dust Server)
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >>         passed If Exists(45000, 1)
     >>               IS Private IP(192.168.1.13) ....
     >>                      TRUE - Quad2=168
     >>        passed IF NOT IsPrivateIP(192.168.1.13)
     >>        passed 'If Not staticEnabled'
     >>        adding UPNP with:
     >>        staticMapping.Add(45000, UDP, 45000, 192.168.1.13, True,Dust Server)
     >>  ---ERROR:System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
       at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
       at DustServer.ServerUPnP.Add(String localIP, Int32 Port, Protocol prot, String desc) in E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustServer\ServerUPnP.vb:line 128
     >> Starting Virtual Grapics Device...
    suggestions?
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  9. #9

    Re: VB.NET and UPnP?

    I have no idea what that error actually is. Perhaps the device is rejecting the mapping? I've not really seen such an error before.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    K. i'll keep up the research.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    Still trying to find the bug.
    ANY AND ALL ideas (even stupid ones) are welcome.

    LINK TO ERROR OUTPUT (image)


    FIRST CALL IN MODULE2.BAS:
    OpenPort Code:
    1. Public Sub Main()
    2.         MyPath = Application.StartupPath
    3.         'being PNP
    4.         ' msg("attempting ClosePort")
    5.         '   ClosePort(MyPort)
    6.         ' msg("attempting OpenPort")
    7.         OpenPort(MyPort)
    8.  
    9.         ServerGameInfo.GameType = eGameType.FFA
    10.         ServerGameInfo.Gamestate = eGameState.WAITING
    11.         ServerSettings.MaxPing = 500


    OPEN PORT FUNCTION IN MODULE2.BAS:
    OpenPort Code:
    1. Function OpenPort(ByVal SubjectPort As Integer) As Boolean
    2.         Dim MyUPnPMap As New ServerUPnP
    3.         msg("OPEN PORT...", System.ConsoleColor.Black, System.ConsoleColor.Cyan)
    4.         msg("   TCP...", System.ConsoleColor.Black, System.ConsoleColor.Cyan)
    5.         msg("     mapping....")
    6.         If MyUPnPMap.Exists(SubjectPort, ServerUPnP.Protocol.TCP) Then
    7.             msg(" EXISTS=True; RETURNING FALSE!")
    8.             Return False 'already in use
    9.         Else
    10.             msg("  Port not in use. Adding...")
    11.             MyUPnPMap.Add(ServerUPnP.LocalIP, SubjectPort, ServerUPnP.Protocol.TCP, "Dust Server")
    12.             Return True 'successfully added
    13.         End If
    14.         'msg("   UDP...", System.ConsoleColor.Black, System.ConsoleColor.Cyan)
    15.  
    16.         'If MyUPnPMap.Exists(SubjectPort, ServerUPnP.Protocol.UDP) Then
    17.         '    msg(" EXISTS=True; RETURNING FALSE!")
    18.         '    Return False 'already in use
    19.         'Else
    20.         '    msg("  Port not in use. Adding...")
    21.         '    MyUPnPMap.Add(ServerUPnP.LocalIP, SubjectPort, ServerUPnP.Protocol.UDP, "Dust Server")
    22.         '    Return True 'successfully added
    23.         'End If
    24.  
    25.     End Function



    UPNP CLASS:
    staticMapping.Add(Port, prot.ToString(), Port, localIP, True, desc) Code:
    1. Imports System.Net
    2. Imports System.Runtime.InteropServices
    3. Imports System.Windows.Forms
    4.  
    5. Public Class ServerUPnP
    6.  
    7.     Implements IDisposable
    8.  
    9.     Private upnpnat As NATUPNPLib.UPnPNAT
    10.     Private staticMapping As NATUPNPLib.IStaticPortMappingCollection
    11.     Private dynamicMapping As NATUPNPLib.IDynamicPortMappingCollection
    12.  
    13.     Private staticEnabled As Boolean = True
    14.     Private dynamicEnabled As Boolean = True
    15.  
    16.     ''' <summary>
    17.     ''' The different supported protocols
    18.     ''' </summary>
    19.     ''' <remarks></remarks>
    20.     Public Enum Protocol
    21.  
    22.         ''' <summary>
    23.         ''' Transmission Control Protocol
    24.         ''' </summary>
    25.         ''' <remarks></remarks>
    26.         TCP
    27.  
    28.         ''' <summary>
    29.         ''' User Datagram Protocol
    30.         ''' </summary>
    31.         ''' <remarks></remarks>
    32.         UDP
    33.  
    34.     End Enum
    35.  
    36.     ''' <summary>
    37.     ''' Returns if UPnP is enabled.
    38.     ''' </summary>
    39.     ''' <value></value>
    40.     ''' <returns></returns>
    41.     ''' <remarks></remarks>
    42.     Public ReadOnly Property UPnPEnabled As Boolean
    43.         Get
    44.             Return staticEnabled = True OrElse dynamicEnabled = True
    45.         End Get
    46.     End Property
    47.  
    48.     ''' <summary>
    49.     ''' The UPnP Managed Class
    50.     ''' </summary>
    51.     ''' <remarks></remarks>
    52.     Public Sub New()
    53.  
    54.         'Create the new NAT Class
    55.         upnpnat = New NATUPNPLib.UPnPNAT
    56.  
    57.         'generate the static mappings
    58.         Me.GetStaticMappings()
    59.         Me.GetDynamicMappings()
    60.  
    61.     End Sub
    62.  
    63.     ''' <summary>
    64.     ''' Returns all static port mappings
    65.     ''' </summary>
    66.     ''' <remarks></remarks>
    67.     Private Sub GetStaticMappings()
    68.         Try
    69.             staticMapping = upnpnat.StaticPortMappingCollection()
    70.         Catch ex As NotImplementedException
    71.             staticEnabled = False
    72.         End Try
    73.     End Sub
    74.  
    75.     ''' <summary>
    76.     ''' Returns all dynamic port mappings
    77.     ''' </summary>
    78.     ''' <remarks></remarks>
    79.     Private Sub GetDynamicMappings()
    80.         Try
    81.             dynamicMapping = upnpnat.DynamicPortMappingCollection()
    82.         Catch ex As NotImplementedException
    83.             dynamicEnabled = False
    84.         End Try
    85.     End Sub
    86.  
    87.     ''' <summary>
    88.     ''' Adds a port mapping to the UPnP enabled device.
    89.     ''' </summary>
    90.     ''' <param name="localIP">The local IP address to map to.</param>
    91.     ''' <param name="Port">The port to forward.</param>
    92.     ''' <param name="prot">The protocol of the port [TCP/UDP]</param>
    93.     ''' <param name="desc">A small description of the port.</param>
    94.     ''' <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception>
    95.     ''' <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception>
    96.     ''' <exception cref="ArgumentException">This exception is thrown when any of the supplied arguments are invalid.</exception>
    97.     ''' <remarks></remarks>
    98.     Public Sub Add(ByVal localIP As String, ByVal Port As Integer, ByVal prot As Protocol, ByVal desc As String)
    99.         msg(" BEGINNING UPNP ADD IN CLASS:  Sub Add(LocalIP=" & localIP & ", Port=" & Port.ToString & ", prot=" & prot.ToString & ", desc=" & desc & ")")
    100.         ' Begin utilizing
    101.         If Exists(Port, prot) Then
    102.             msg("       EXISTS = TRUE. THROWING EXCEPTION: 'This mapping already exists!', " & Port.ToString & ";" & prot.ToString)
    103.             Throw New ArgumentException("This mapping already exists!", "Port;prot")
    104.             'Remove(Port, Protocol.UDP)
    105.         Else
    106.             msg("        passed If Exists(" & Port.ToString & ", " & prot & ")")
    107.         End If
    108.  
    109.         ' Check
    110.         If Not IsPrivateIP(localIP) Then
    111.             msg("       IsPrivateIP(LocalIp) = False. THROWING EXCEPTION: 'This is not a local IP address!'," & localIP)
    112.             Throw New ArgumentException("This is not a local IP address!", "localIP")
    113.         Else
    114.             msg("       passed IF NOT IsPrivateIP(" & localIP & ")")
    115.         End If
    116.  
    117.         ' Final check!
    118.         If Not staticEnabled Then
    119.             msg("       staticEnabled=FALSE - THROWING EXCEPTION: 'UPnP is not enabled, or there was an error with UPnP Initialization.'")
    120.             Throw New ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization.")
    121.         Else
    122.             msg("       passed 'If Not staticEnabled'")
    123.         End If
    124.         msg("       adding UPNP with:")
    125.         msg("       staticMapping.Add(" & Port.ToString & ", " & prot.ToString() & ", " & Port.ToString & ", " & localIP & ", True," & desc & ")")
    126.         ' Okay, continue on
    127.         Try
    128.             Application.DoEvents()
    129.             Application.DoEvents()
    130.             Application.DoEvents()
    131.  
    132.             'This is the line 132 in the output display. (+ 2 lines for edit in forum box. )
    133.             staticMapping.Add(Port, prot.ToString(), Port, localIP, True, desc)
    134.  
    135.  
    136.             Application.DoEvents()
    137.             Application.DoEvents()
    138.             Application.DoEvents()
    139.  
    140.         Catch ex As Exception
    141.             MsgBox(ex.ToString)
    142.             msg(" ---ERROR:" & ex.ToString)
    143.         End Try
    144.     End Sub
    145.  
    146.     ''' <summary>
    147.     ''' Removes a port mapping from the UPnP enabled device.
    148.     ''' </summary>
    149.     ''' <param name="Port">The port to remove.</param>
    150.     ''' <param name="prot">The protocol of the port [TCP/UDP]</param>
    151.     ''' <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception>
    152.     ''' <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception>
    153.     ''' <exception cref="ArgumentException">This exception is thrown when the port [or protocol] is invalid.</exception>
    154.     ''' <remarks></remarks>
    155.     Public Sub Remove(ByVal Port As Integer, ByVal Prot As Protocol)
    156.         Try
    157.             ' Begin utilizing
    158.             If Not Exists(Port, Prot) Then
    159.                 'Throw New ArgumentException("This mapping doesn't exist!", "Port;prot")
    160.                 Exit Sub
    161.             End If
    162.  
    163.             ' Final check!
    164.             If Not staticEnabled Then Throw New ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization.")
    165.         Catch
    166.         End Try
    167.  
    168.         ' Okay, continue on
    169.         staticMapping.Remove(Port, Prot.ToString)
    170.  
    171.     End Sub
    172.  
    173.     ''' <summary>
    174.     ''' Checks to see if a port exists in the mapping.
    175.     ''' </summary>
    176.     ''' <param name="Port">The port to check.</param>
    177.     ''' <param name="prot">The protocol of the port [TCP/UDP]</param>
    178.     ''' <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception>
    179.     ''' <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception>
    180.     ''' <exception cref="ArgumentException">This exception is thrown when the port [or protocol] is invalid.</exception>
    181.     ''' <remarks></remarks>
    182.     Public Function Exists(ByVal Port As Integer, ByVal Prot As Protocol) As Boolean
    183.  
    184.         msg("Checking Function Exists(port=" & Port.ToString & ", Prot=" & Prot.ToString & ") ....")
    185.  
    186.  
    187.         ' Final check!
    188.         If Not staticEnabled Then
    189.             msg("    staticEnabled = False")
    190.             Throw New ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization.")
    191.         End If
    192.  
    193.         ' Begin checking
    194.         For Each mapping As NATUPNPLib.IStaticPortMapping In staticMapping
    195.  
    196.             ' Compare
    197.             If mapping.ExternalPort.Equals(Port) AndAlso mapping.Protocol.ToString.Equals(Prot.ToString) Then
    198.                 msg(" ' Begin Checking For Each mapping As NATUPNPLib.IStaticPortMapping In staticMapping' returned TRUE")
    199.                 Return True
    200.             End If
    201.  
    202.  
    203.         Next
    204.  
    205.         msg(" Mapping For-Each check passed.")
    206.         msg(" Returning FALSE")
    207.         msg(" ")
    208.         'Nothing!
    209.         Return False
    210.  
    211.     End Function
    212.  
    213.     ''' <summary>
    214.     ''' Attempts to locate the local IP address of this computer.
    215.     ''' </summary>
    216.     ''' <returns>String</returns>
    217.     ''' <remarks></remarks>
    218.     Public Shared Function LocalIP() As String
    219.         Dim IPList As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
    220.         For Each IPaddress In IPList.AddressList
    221.             If (IPaddress.AddressFamily = Sockets.AddressFamily.InterNetwork) AndAlso IsPrivateIP(IPaddress.ToString()) Then
    222.                 Return IPaddress.ToString
    223.             End If
    224.         Next
    225.         Return String.Empty
    226.     End Function
    227.  
    228.     ''' <summary>
    229.     ''' Checks to see if an IP address is a local IP address.
    230.     ''' </summary>
    231.     ''' <param name="CheckIP">The IP address to check.</param>
    232.     ''' <returns>Boolean</returns>
    233.     ''' <remarks></remarks>
    234.     Private Shared Function IsPrivateIP(ByVal CheckIP As String) As Boolean
    235.         Dim Quad1, Quad2 As Integer
    236.         msg("              IS Private IP(" & CheckIP & ") ....")
    237.         Quad1 = CInt(CheckIP.Substring(0, CheckIP.IndexOf(".")))
    238.         Quad2 = CInt(CheckIP.Substring(CheckIP.IndexOf(".") + 1).Substring(0, CheckIP.IndexOf(".")))
    239.         Select Case Quad1
    240.             Case 10
    241.                 msg("                     TRUE - 10")
    242.                 Return True
    243.             Case 172
    244.                 If Quad2 >= 16 And Quad2 <= 31 Then
    245.                     msg("                     TRUE - 172, quad2>=16 && quad2<=31")
    246.                     Return True
    247.                 End If
    248.  
    249.             Case 192
    250.                 If Quad2 = 168 Then
    251.                     msg("                     TRUE - Quad2=168")
    252.                     Return True
    253.                 End If
    254.  
    255.         End Select
    256.         msg("                     Returning FALSE")
    257.  
    258.         Return False
    259.     End Function
    260.  
    261.     ''' <summary>
    262.     ''' Disposes of the UPnP class
    263.     ''' </summary>
    264.     ''' <param name="disposing">True or False makes no difference.</param>
    265.     ''' <remarks></remarks>
    266.     Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    267.         Marshal.ReleaseComObject(staticMapping)
    268.         Marshal.ReleaseComObject(dynamicMapping)
    269.         Marshal.ReleaseComObject(upnpnat)
    270.     End Sub
    271.  
    272.     ''' <summary>
    273.     ''' Dispose!
    274.     ''' </summary>
    275.     ''' <remarks></remarks>
    276.     Public Sub Dispose() Implements IDisposable.Dispose
    277.         Dispose(True)
    278.         GC.SuppressFinalize(Me)
    279.     End Sub
    280.  
    281.     ''' <summary>
    282.     ''' Prints out some debugging information to use.
    283.     ''' </summary>
    284.     ''' <returns></returns>
    285.     ''' <remarks></remarks>
    286.     Public Function Print() As List(Of String)
    287.  
    288.         ' Return list
    289.         Dim L As New List(Of String)
    290.  
    291.         ' Loop through all the data after a check
    292.         If staticEnabled Then
    293.             For Each mapping As NATUPNPLib.IStaticPortMapping In staticMapping
    294.  
    295.                 ' Add some initial data
    296.                 L.Add("--------------------------------------")
    297.  
    298.                 'Grab the rest
    299.                 L.Add(String.Format("IP: {0}", mapping.InternalClient))
    300.                 L.Add(String.Format("Port: {0}", mapping.InternalPort))
    301.                 L.Add(String.Format("Description: {0}", mapping.Description))
    302.                 msg(" ")
    303.  
    304.                 msg(String.Format("IP: {0}", mapping.InternalClient))
    305.                 msg(String.Format("Port: {0}", mapping.InternalPort))
    306.                 msg(String.Format("Description: {0}", mapping.Description))
    307.                 msg(" ")
    308.                 msg(" ")
    309.  
    310.  
    311.  
    312.             Next
    313.         End If
    314.  
    315.         'Finisher
    316.         L.Add("--------------------------------------")
    317.  
    318.         ' Give it back
    319.         Return L
    320.  
    321.     End Function
    322.  
    323. End Class
    Last edited by Waxy; Jan 3rd, 2012 at 03:04 AM.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  12. #12

    Re: VB.NET and UPnP?

    Okay, try calling the Print() function to make sure that's working.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    PRINT Code:
    1. ================ version 1.8.0-alpha
    2. Microsoft Windows 7 Ultimate Edition  (Build 7600)
    3. ---------- version 0.9.1
    4.  >> OPEN PORT...
    5.  >>  PRINT() check...
    6.  >>
    7.  >> IP: 192.168.1.15
    8.  >> Port: 55474
    9.  >> Description: BitTorrent (TCP)
    10.  >>
    11.  >>
    12.  >>
    13.  >> IP: 192.168.1.15
    14.  >> Port: 55474
    15.  >> Description: BitTorrent (UDP)
    16.  >>
    17.  >>
    18.  >>
    19.  >> IP: 192.168.1.27
    20.  >> Port: 53814
    21.  >> Description: Teredo
    22.  >>
    23.  >>
    24.  >>
    25.  >> IP: 192.168.1.31
    26.  >> Port: 42416
    27.  >> Description: uTorrent (UDP)
    28.  >>
    29.  >>
    30.  >>
    31.  >> IP: 192.168.1.31
    32.  >> Port: 63909
    33.  >> Description: Teredo
    34.  >>
    35.  >>
    36.  >>
    37.  >> IP: 192.168.1.16
    38.  >> Port: 55184
    39.  >> Description: Teredo
    40.  >>
    41.  >>
    42.  >>
    43.  >> IP: 192.168.1.13
    44.  >> Port: 25824
    45.  >> Description: LimeUDP27408B0DD8
    46.  >>
    47.  >>
    48.  >>
    49.  >> IP: 192.168.1.13
    50.  >> Port: 25824
    51.  >> Description: LimeTCP27408B0DD8
    52.  >>
    53.  >>
    54.  >>
    55.  >> IP: 192.168.1.15
    56.  >> Port: 42416
    57.  >> Description: uTorrent (TCP)
    58.  >>
    59.  >>
    60.  >>
    61.  >> IP: 192.168.1.15
    62.  >> Port: 42416
    63.  >> Description: uTorrent (UDP)
    64.  >>
    65.  >>
    66.  >>
    67.  >> IP: 192.168.1.15
    68.  >> Port: 61634
    69.  >> Description: Teredo
    70.  >>
    71.  >>
    72.  >>
    73.  >> IP: 192.168.1.35
    74.  >> Port: 42416
    75.  >> Description: uTorrent (TCP)
    76.  >>
    77.  >>
    78.  >>
    79.  >> IP: 192.168.1.35
    80.  >> Port: 42416
    81.  >> Description: uTorrent (UDP)
    82.  >>
    83.  >>
    84.  >>
    85.  >> IP: 192.168.1.36
    86.  >> Port: 49356
    87.  >> Description: Teredo
    88.  >>
    89.  >>
    90.  >>
    91.  >> IP: 192.168.1.10
    92.  >> Port: 53585
    93.  >> Description: Teredo
    94.  >>
    95.  >>
    96.  >>
    97.  >> IP: 192.168.1.10
    98.  >> Port: 59903
    99.  >> Description: Teredo
    100.  >>
    101.  >>
    102.  >>
    103.  >> IP: 192.168.1.13
    104.  >> Port: 22450
    105.  >> Description: BitTorrent (TCP)
    106.  >>
    107.  >>
    108.  >>
    109.  >> IP: 192.168.1.13
    110.  >> Port: 22450
    111.  >> Description: BitTorrent (UDP)
    112.  >>
    113.  >>
    114.  >>
    115.  >> IP: 192.168.1.10
    116.  >> Port: 54990
    117.  >> Description: Teredo
    118.  >>
    119.  >>
    120.  >>
    121.  >> IP: 192.168.1.10
    122.  >> Port: 53385
    123.  >> Description: Teredo
    124.  >>
    125.  >>
    126.  >>
    127.  >> IP: 192.168.1.10
    128.  >> Port: 62077
    129.  >> Description: Teredo
    130.  >>
    131.  >>
    132.  >>
    133.  >> IP: 192.168.1.10
    134.  >> Port: 55945
    135.  >> Description: Teredo
    136.  >>
    137.  >>
    138.  >>
    139.  >> IP: 192.168.1.10
    140.  >> Port: 55705
    141.  >> Description: Teredo
    142.  >>
    143.  >>
    144.  >>
    145.  >> IP: 192.168.1.10
    146.  >> Port: 65424
    147.  >> Description: Teredo
    148.  >>
    149.  >>
    150.  >>
    151.  >> IP: 192.168.1.10
    152.  >> Port: 63500
    153.  >> Description: Teredo
    154.  >>
    155.  >>
    156.  >>
    157.  >> IP: 192.168.1.10
    158.  >> Port: 59997
    159.  >> Description: Teredo
    160.  >>
    161.  >>
    162.  >>
    163.  >> IP: 192.168.1.10
    164.  >> Port: 53252
    165.  >> Description: Teredo
    166.  >>
    167.  >>
    168.  >>
    169.  >> IP: 192.168.1.10
    170.  >> Port: 61529
    171.  >> Description: Teredo
    172.  >>
    173.  >>
    174.  >>
    175.  >> IP: 192.168.1.10
    176.  >> Port: 49508
    177.  >> Description: Teredo
    178.  >>
    179.  >>
    180.  >>
    181.  >> IP: 192.168.1.13
    182.  >> Port: 6783
    183.  >> Description: SplashtopStreamer
    184.  >>
    185.  >>
    186.  >>
    187.  >> IP: 192.168.1.13
    188.  >> Port: 6784
    189.  >> Description: SplashtopStreamer
    190.  >>
    191.  >>
    192.  >>
    193.  >> IP: 192.168.1.13
    194.  >> Port: 6785
    195.  >> Description: SplashtopStreamer
    196.  >>
    197.  >>
    198.  >>    TCP...
    199.  >>      mapping....
    200.  >> Checking Function Exists(port=45000, Prot=TCP) ....
    201.  >>  Mapping For-Each check passed.
    202.  >>  Returning FALSE
    203.  >>
    204.  >>   Port not in use. Adding...
    205.  >>               IS Private IP(192.168.1.13) ....
    206.  >>                      TRUE - Quad2=168
    207.  >>  BEGINNING UPNP ADD IN CLASS:  Sub Add(LocalIP=192.168.1.13, Port=45000, prot=TCP, desc=Dust Server)
    208.  >> Checking Function Exists(port=45000, Prot=TCP) ....
    209.  >>  Mapping For-Each check passed.
    210.  >>  Returning FALSE
    211.  >>
    212.  >>         passed If Exists(45000, 0)
    213.  >>               IS Private IP(192.168.1.13) ....
    214.  >>                      TRUE - Quad2=168
    215.  >>        passed IF NOT IsPrivateIP(192.168.1.13)
    216.  >>        passed 'If Not staticEnabled'
    217.  >>        adding UPNP with:
    218.  >>        staticMapping.Add(45000, TCP, 45000, 192.168.1.13, True,Dust Server)
    219.  >>  ---ERROR:System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
    220.    at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
    221.    at DustServer.ServerUPnP.Add(String localIP, Int32 Port, Protocol prot, String desc) in E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustServer\ServerUPnP.vb:line 132
    222.  >> Starting Virtual Grapics Device...
    223. Could not load mesh, because file could not be opened: : 20kdm2.bsp
    224.  >> Loading Map...
    225. Could not create archive for: E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustClient\Bin\Release/bin/defaultmedia/map-20kdm2.pk3
    226. Could not open file of texture: E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustClient\Bin\Release/bin/defaultmedia/particle.bmp
    227.  >>
    228.  >> Informing Master Server/Server Browsers....
    229.  >> OK
    230.  >> MASTER SERVER says:  Server Acknowledged.
    231.  
    232.  >> Opening Network Port for clients...
    233.  >> Server started...
    234.  >>
    235.  >> Public Sub StartListener()
    236.  >>
    237.  >> ________________
    238.  >> CHANGING MAP TO Mayatowna1
    239. Loaded texture: E:/Program Files/Microsoft Visual Studio 10.0/My Documents/Projects/DUST/DustClient/Bin/Release/Bin/maps/Mayatowna1/Ground_ground2b.jpg
    240. Loaded texture: E:/Program Files/Microsoft Visual Studio 10.0/My Documents/Projects/DUST/DustClient/Bin/Release/Bin/maps/Mayatowna1/Ground_ground2b-bmp.jpg
    241. Could not lock texture for making normal map.
    242. Loaded texture: E:/Program Files/Microsoft Visual Studio 10.0/My Documents/Projects/DUST/DustClient/Bin/Release/Bin/maps/Mayatowna1/floor_bluewoody1.jpg
    243. Loaded mesh: E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustClient\Bin\Release\Bin\maps\Mayatowna1\Mayatowna1.obj
    244. Needed 100ms to create OctreeTriangleSelector.(1311 nodes, 72401 polys)
    245.  >> ________________
    246.  >> MAP LOADED AND SERVER READY FOR PLAYERS!
    247.  >>
    248.  >>
    249.  >> Players/Pings:
    250.  >>
    251.  >> Players/Pings:
    252.  >>
    253.  >> Players/Pings:
    254.  >>
    255.  >> Players/Pings:
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  14. #14

    Re: VB.NET and UPnP?

    Show me all the variables you're using when adding the port mapping. I want to make sure everything is right.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    staticMapping.Add(45000, TCP, 45000, 192.168.1.13, True,"Dust Server")

    .Add Code:
    1. >>   Port not in use. Adding...
    2.  >>               IS Private IP(192.168.1.13) ....
    3.  >>                      TRUE - Quad2=168
    4.  >>  BEGINNING UPNP ADD IN CLASS:  Sub Add(LocalIP=192.168.1.13, Port=45000, prot=TCP, desc=Dust Server)
    5.  >> Checking Function Exists(port=45000, Prot=TCP) ....
    6.  >>  Mapping For-Each check passed.
    7.  >>  Returning FALSE
    8.  >>
    9.  >>         passed If Exists(45000, 0)
    10.  >>               IS Private IP(192.168.1.13) ....
    11.  >>                      TRUE - Quad2=168
    12.  >>        passed IF NOT IsPrivateIP(192.168.1.13)
    13.  >>        passed 'If Not staticEnabled'
    14.  >>        adding UPNP with:
    15.  >>        staticMapping.Add(45000, TCP, 45000, 192.168.1.13, True,Dust Server)
    16.  >>  ---ERROR:System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
    17.    at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
    18.    at DustServer.ServerUPnP.Add(String localIP, Int32 Port, Protocol prot, String desc) in E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustServer\ServerUPnP.vb:line 132


    output on the fly with the lines added to the UPNP class:

    msg Code:
    1. Public Sub Add(ByVal localIP As String, ByVal Port As Integer, ByVal prot As Protocol, ByVal desc As String)
    2.         msg(" BEGINNING UPNP ADD IN CLASS:  Sub Add(LocalIP=" & localIP & ", Port=" & Port.ToString & ", prot=" & prot.ToString & ", desc=" & desc & ")")
    3.         ' Begin utilizing
    4.         If Exists(Port, prot) Then
    5.             msg("       EXISTS = TRUE. THROWING EXCEPTION: 'This mapping already exists!', " & Port.ToString & ";" & prot.ToString)
    6.             Throw New ArgumentException("This mapping already exists!", "Port;prot")
    7.             'Remove(Port, Protocol.UDP)
    8.         Else
    9.             msg("        passed If Exists(" & Port.ToString & ", " & prot & ")")
    10.         End If
    11.  
    12.         ' Check
    13.         If Not IsPrivateIP(localIP) Then
    14.             msg("       IsPrivateIP(LocalIp) = False. THROWING EXCEPTION: 'This is not a local IP address!'," & localIP)
    15.             Throw New ArgumentException("This is not a local IP address!", "localIP")
    16.         Else
    17.             msg("       passed IF NOT IsPrivateIP(" & localIP & ")")
    18.         End If
    19.  
    20.         ' Final check!
    21.         If Not staticEnabled Then
    22.             msg("       staticEnabled=FALSE - THROWING EXCEPTION: 'UPnP is not enabled, or there was an error with UPnP Initialization.'")
    23.             Throw New ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization.")
    24.         Else
    25.             msg("       passed 'If Not staticEnabled'")
    26.         End If
    27.         msg("       adding UPNP with:")
    28.         msg("       staticMapping.Add(" & Port.ToString & ", " & prot.ToString() & ", " & Port.ToString & ", " & localIP & ", True," & desc & ")")
    29.         ' Okay, continue on
    30.         Try
    31.             Application.DoEvents()
    32.             Application.DoEvents()
    33.             Application.DoEvents()
    34.             staticMapping.Add(Port, prot.ToString(), Port, localIP, True, desc)
    35.             Application.DoEvents()
    36.             Application.DoEvents()
    37.             Application.DoEvents()
    38.  
    39.         Catch ex As Exception
    40.             MsgBox(ex.ToString)
    41.             msg(" ---ERROR:" & ex.ToString)
    42.         End Try
    43.     End Sub

    the call is:
    OpenPort Code:
    1. Function OpenPort(ByVal SubjectPort As Integer) As Boolean
    2.         Dim MyUPnPMap As New ServerUPnP
    3.         msg("OPEN PORT...", System.ConsoleColor.Black, System.ConsoleColor.Cyan)
    4.         msg(" PRINT() check...")
    5.         MyUPnPMap.Print()
    6.         Application.DoEvents()
    7.         msg("   TCP...", System.ConsoleColor.Black, System.ConsoleColor.Cyan)
    8.         msg("     mapping....")
    9.         If MyUPnPMap.Exists(SubjectPort, ServerUPnP.Protocol.TCP) Then
    10.             msg(" EXISTS=True; RETURNING FALSE!")
    11.             Return False 'already in use
    12.         Else
    13.             msg("  Port not in use. Adding...")
    14.             MyUPnPMap.Add(ServerUPnP.LocalIP, SubjectPort, ServerUPnP.Protocol.TCP, "Dust Server")
    15.             Return True 'successfully added
    16.         End If
    17.         'msg("   UDP...", System.ConsoleColor.Black, System.ConsoleColor.Cyan)
    18.  
    19.         'If MyUPnPMap.Exists(SubjectPort, ServerUPnP.Protocol.UDP) Then
    20.         '    msg(" EXISTS=True; RETURNING FALSE!")
    21.         '    Return False 'already in use
    22.         'Else
    23.         '    msg("  Port not in use. Adding...")
    24.         '    MyUPnPMap.Add(ServerUPnP.LocalIP, SubjectPort, ServerUPnP.Protocol.UDP, "Dust Server")
    25.         '    Return True 'successfully added
    26.         'End If
    27.  
    28.     End Function

    where subject port = 45000. the TCP is hardcoded (i want UDP but it's currently set to TCP due to debugging. i was checking to see if it was a protocol problem)
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    PS:

    machine i'm coding on says:
    ---ERROR:System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
    at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
    at DustServer.ServerUPnP.Add(String localIP, Int32 Port, Protocol prot, String desc) in E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustServer\ServerUPnP.vb:line 132

    testing machine returned the
    "Not set to the instance of an object" details box that was in the image.
    yes, i defined it with the DIM AS NEW keyword.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  17. #17

    Re: VB.NET and UPnP?

    I'll usually see a NullReferenceException when UPnP isn't enabled. I try and make sure everything is good before running routines, but, sometimes one slips through. I'll look into it though.

    As for the COMException, I have no idea what to do about that.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    i know what you mean. it's a very generalized error:

    "0x80040208
    EVENT_E_USER_EXCEPTION
    A user-supplied component or subscriber raised an exception."

    http://msdn.microsoft.com/en-us/libr...rot.10%29.aspx

    as you can tell from my debugging: i tried closing the port before opening it, no good.
    (no error with closing, just with opening. those routines seem to work fine)

    tried both tcp and udp, no good.
    (both cause same error)

    and different ports.
    (same error)

    Out of curiosity - are there any 2 UDP ports that are open by default with UPNP by windows 7?
    i can always just switch ports if that's an option.
    Last edited by Waxy; Jan 3rd, 2012 at 08:43 PM.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    is this any help?
    NATUPNP.h

    (i don't speak C)
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    also a thought:
    can you pass me a simple port open/close/print() source for me to compile in vs.net 2010?

    if it will compile and run on my machine then it will prove that another routine may be interfering with the upnp clas.

    if it doesnt compile and run then it may prove to be a hardware problem.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  21. #21

    Re: VB.NET and UPnP?

    You already have it; that DLL -is- the simple open/close/print source.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    could it matter that, while dynamic returns true, static returns false here:
    Code:
        Private Sub GetStaticMappings()
            Try
    
                staticMapping = upnpnat.StaticPortMappingCollection()
            Catch ex As NotImplementedException
                staticEnabled = False
            End Try
            msg("STATIC:  " & staticEnabled.ToString)
        End Sub
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  23. #23

    Re: VB.NET and UPnP?

    ...yes that would make a big difference actually.

    I need to rewrite some coding for the library to be more effective...I'll work on that.

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    Thank you formless
    and thanks for making the UPNP routines available. They are a major holdup in the project.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    full output if that helps: (includes pre- and post- PRINT routine output)
    Code:
    ---------- version 1.8.0-alpha
    Microsoft Windows 7 Ultimate Edition  (Build 7600)
    ---------- version 0.9.1
     >> Sub New() of UPNPNAT:
     >> EXTERNAL IP=68.231.10.67
     >>     DYNAMIC:
     >>    True
     >>
     >>    STATIC:
     >>    False
     >>
     >> OPEN PORT...
     >>  PRINT() check...
     >>
     >> IP: 192.168.1.15
     >> Port: 55474
     >> Description: BitTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 55474
     >> Description: BitTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.27
     >> Port: 53814
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.31
     >> Port: 42416
     >> Description: uTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.31
     >> Port: 63909
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.16
     >> Port: 55184
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 25824
     >> Description: LimeUDP27408B0DD8
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 25824
     >> Description: LimeTCP27408B0DD8
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 42416
     >> Description: uTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 42416
     >> Description: uTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 61634
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.35
     >> Port: 42416
     >> Description: uTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.35
     >> Port: 42416
     >> Description: uTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.36
     >> Port: 49356
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 53585
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 59903
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 22450
     >> Description: BitTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 22450
     >> Description: BitTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 54990
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 53385
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 62077
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 55945
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 55705
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 65424
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 63500
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 59997
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 53252
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 61529
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 49508
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 6783
     >> Description: SplashtopStreamer
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 6784
     >> Description: SplashtopStreamer
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 6785
     >> Description: SplashtopStreamer
     >>
     >>
     >>    UDP...
     >>      mapping....
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >>   Port not in use. Adding...
     >>               IS Private IP(192.168.1.13) ....
     >>                      TRUE - Quad2=168
     >>  BEGINNING UPNP ADD IN CLASS:  Sub Add(LocalIP=192.168.1.13, Port=45000, prot=UDP, desc=Dust Server)
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >>         passed If Exists(45000, UDP)
     >>               IS Private IP(192.168.1.13) ....
     >>                      TRUE - Quad2=168
     >>        passed IF NOT IsPrivateIP(192.168.1.13)
     >>        passed 'If Not staticEnabled'
     >>        adding UPNP with:
     >>        staticMapping.Add(45000, UDP, 45000, 192.168.1.13, True,Dust Server)
     >>        Variables Are:
     >>                staticEnabled As Boolean = True
     >>                dynamicEnabled As Boolean= False
     >>                ExternalIP As String =68.231.10.67
     >>                Port as Integer = 45000
     >>                prot as protocol = 1|| prot.tostring =UDP
     >>                LocalIp as String = 192.168.1.13
     >>                bEnabled as Boolean for Add function =True
     >>                desc Discription as String =Dust Server
     >>
     >>        Result:
     >>  ---ERROR:System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
       at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
       at DustServer.ServerUPnP.Add(String localIP, Int32 Port, Protocol prot, String desc) in E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustServer\ServerUPnP.vb:line 207
     >> Sub New() of UPNPNAT:
     >> EXTERNAL IP=68.231.10.67
     >>     DYNAMIC:
     >>    True
     >>
     >>    STATIC:
     >>    False
     >>
     >>  PRINT() check...
     >>
     >> IP: 192.168.1.15
     >> Port: 55474
     >> Description: BitTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 55474
     >> Description: BitTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.27
     >> Port: 53814
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.31
     >> Port: 42416
     >> Description: uTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.31
     >> Port: 63909
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.16
     >> Port: 55184
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 25824
     >> Description: LimeUDP27408B0DD8
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 25824
     >> Description: LimeTCP27408B0DD8
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 42416
     >> Description: uTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 42416
     >> Description: uTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.15
     >> Port: 61634
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.35
     >> Port: 42416
     >> Description: uTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.35
     >> Port: 42416
     >> Description: uTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.36
     >> Port: 49356
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 53585
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 59903
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 22450
     >> Description: BitTorrent (TCP)
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 22450
     >> Description: BitTorrent (UDP)
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 54990
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 53385
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 62077
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 55945
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 55705
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 65424
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 63500
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 59997
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 53252
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 61529
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.10
     >> Port: 49508
     >> Description: Teredo
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 6783
     >> Description: SplashtopStreamer
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 6784
     >> Description: SplashtopStreamer
     >>
     >>
     >>
     >> IP: 192.168.1.13
     >> Port: 6785
     >> Description: SplashtopStreamer
     >>
     >>
     >> Starting Virtual Grapics Device...
    
    WARNING: Could not load mesh, because file could not be opened: : 20kdm2.bsp
    Falling back to Echo Broadcast Server Base. No AI or Server Side collision checking.
    
     >> Loading Map...
    
    WARNING: Could not create archive for: E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustClient\Bin\Release/bin/defaultmedia/map-20kdm2.pk3
    Falling back to Echo Broadcast Server Base. No AI or Server Side collision checking.
    
    Could not open file of texture: E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustClient\Bin\Release/bin/defaultmedia/particle.bmp - irrelevant: virtual graphics device does not require texture.
    
     >>
     >> Informing Master Server/Server Browsers....
     >> OK
     >> MASTER SERVER says:  Server Acknowledged.
    
     >> Opening Network Port for clients...
     >> Server started...
     >>
     >> Public Sub StartListener()
     >>
     >> ________________
     >> CHANGING MAP TO Mayatowna1
    Loaded texture: E:/Program Files/Microsoft Visual Studio 10.0/My Documents/Projects/DUST/DustClient/Bin/Release/Bin/maps/Mayatowna1/Ground_ground2b.jpg
    Loaded texture: E:/Program Files/Microsoft Visual Studio 10.0/My Documents/Projects/DUST/DustClient/Bin/Release/Bin/maps/Mayatowna1/Ground_ground2b-bmp.jpg
    Could not lock texture for making normal map. - irrelevant, virtual graphics device.
    Loaded texture: E:/Program Files/Microsoft Visual Studio 10.0/My Documents/Projects/DUST/DustClient/Bin/Release/Bin/maps/Mayatowna1/floor_bluewoody1.jpg
    Loaded mesh: E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustClient\Bin\Release\Bin\maps\Mayatowna1\Mayatowna1.obj
    Needed 93ms to create OctreeTriangleSelector.(1311 nodes, 72401 polys)
     >> ________________
     >> MAP LOADED AND SERVER READY FOR PLAYERS!
     >>
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  26. #26

    Re: VB.NET and UPnP?

    The issue is that I'm adding to the StaticMapping, regardless of if it's available or not. I need to fix the code to add to either both or just one if the other isn't available.

    I'll fix it in the future when I have some more time.

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    this is odd...
    i just noticed: when doing New()
    Code:
     >> Sub New() of UPNPNAT:
     >> EXTERNAL IP=68.231.10.67
     >>     DYNAMIC:
     >>    True
     >>
     >>    STATIC:
     >>    False
    but when doing .add():
    Code:
      Variables Are:
     >>                staticEnabled As Boolean = True
     >>                dynamicEnabled As Boolean= False
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  28. #28

    Re: VB.NET and UPnP?

    I'll have to look into it; I'm not sure.

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    it was an error on my part. correct output:

    Code:
     >> Sub New() of UPNPNAT:
     >> EXTERNAL IP=68.231.10.67
     >>     STATIC:
     >>    True
     >>
     >>    DYNAMIC:
     >>    False
    Code:
     >>    UDP...
     >>      mapping....
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >>   Port not in use. Adding...
     >>               IS Private IP(192.168.1.13) ....
     >>                      TRUE - Quad2=168
     >>  BEGINNING UPNP ADD IN CLASS:  Sub Add(LocalIP=192.168.1.13, Port=45000, prot=UDP, desc=Dust Server)
     >> Checking Function Exists(port=45000, Prot=UDP) ....
     >>  Mapping For-Each check passed.
     >>  Returning FALSE
     >>
     >>         passed If Exists(45000, UDP)
     >>               IS Private IP(192.168.1.13) ....
     >>                      TRUE - Quad2=168
     >>        passed IF NOT IsPrivateIP(192.168.1.13)
     >>        passed 'If Not staticEnabled'
     >>        adding UPNP with:
     >>        staticMapping.Add(45000, UDP, 45000, 192.168.1.13, True,Dust Server)
     >>        Variables Are:
     >>                staticEnabled As Boolean = True
     >>                dynamicEnabled As Boolean= False
     >>                ExternalIP As String =68.231.10.67
     >>                Port as Integer = 45000
     >>                prot as protocol = 1|| prot.tostring =UDP
     >>                LocalIp as String = 192.168.1.13
     >>                bEnabled as Boolean for Add function =True
     >>                desc Discription as String =Dust Server
     >>
     >>        Result:
     >> EXTERNAL IP=68.231.10.67
     >>  ---ERROR:System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
       at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
       at DustServer.ServerUPnP.Add(String localIP, Int32 Port, Protocol prot, String desc) in E:\Program Files\Microsoft Visual Studio 10.0\My Documents\Projects\DUST\DustServer\ServerUPnP.vb:line 208
     >> Sub New() of UPNPNAT:
     >> EXTERNAL IP=68.231.10.67
     >>     STATIC:
     >>    True
     >>
     >>    DYNAMIC:
     >>    False
     >>
     >>  PRINT() check...

    Cisco Linksys E3000
    Last edited by Waxy; Jan 17th, 2012 at 01:59 AM.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: VB.NET and UPnP?

    think i found the incompatibility with your routines.
    i resorted to a different Library than UPnPNAT.
    here's the link to the service difference if you choose to update it:
    http://managedupnp.codeplex.com/discussions/286523

    i'll code it and test it tonite if i have time, but it should be the solution i need.












    (sent from my ipad)
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  31. #31
    New Member
    Join Date
    Feb 2013
    Posts
    3

    Re: VB.NET and UPnP?

    Hey, Waxy. Did you succeed to compete this task? I'm having the same problem. What about managed upnp? Was it any help?

  32. #32
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: VB.NET and UPnP?

    For the life of me I cannot get this to work. I'm always receiving Object reference not set to an instance of an object.

    at this line:
    Code:
     For Each mapping As NATUPNPLib.IStaticPortMapping In staticMapping
    under this: Public Function Exists(ByVal Port As Integer, ByVal Prot As Protocol) As Boolean




    here's my code:
    Dim portforward As New UPNP
    If portforward.Exists(6550, UPNP.Protocol.UDP) Then
    Debugger.Break()
    End If

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