Mainly relies on COM interoperability to achieve
Add COM reference using UPNP 1.0 Type Library npnp. dll
It encapsulates UPNP operations.
1. Supported custom types
Public enum ProtocolType
{
UDP,
TCP
}
/// <Summary>
/// Port ing information
/// </Summary>
Public class PortMappingInfo
{
Public string InternalIP;
Public int ExternalPort;
Public int InternalPort;
Public ProtocolType type;
Public string Description;
Public PortMappingInfo (string internalIP, int externalPort, int internalPort, ProtocolType type, string description)
{
This. InternalIP = internalIP;
This. ExternalPort = externalPort;
This. InternalPort = internalPort;
This. type = type;
This. Description = description;
}
}
2. UPnP class implementation
Using System;
Using System. Collections. Generic;
Using System. Collections;
Using System. Text;
Using System. Net;
Using System. Xml;
Using System. IO;
Using System. Net. Sockets;
Using NATUPNPLib;
Namespace Network
{
Public sealed class UPnPNat
{
Private UPnPNATClass _ uPnPNAT;
Public UPnPNat ()
{
Try
{
UPnPNATClass nat = new UPnPNATClass ();
// If (nat. NATEventManager! = Null & nat. StaticPortMappingCollection! = Null)
_ UPnPNAT = nat;
}
Catch {}
If (_ uPnPNAT = null)
Throw new NotSupportedException ("No upnp nat activity can be configured ");
}
/// <Summary>
/// Add Port ing
/// </Summary>
/// <Param name = "ExternalPort"> external port (the port opened by the NAT device of the router) </param>
/// <Param name = "InternalPort"> internal port: local port </param>
/// <Param name = "type"> protocol type UDP or TCP </param>
/// <Param name = "InternalClient"> Intranet IP address or host name </param>
/// <Param name = "enable"> enable </param>
/// <Param name = "description"> description </param>
Public void AddStaticPortMapping (int ExternalPort, int InternalPort, ProtocolType type, string InternalClient, bool enable, string description)
{
_ UPnPNAT. StaticPortMappingCollection. Add (ExternalPort, type. ToString (). ToUpper (), InternalPort, InternalClient, enable, description );
}
/// <Summary>
/// Remove port ing
/// </Summary>
/// <Param name = "ExternalPort"> external port number </param>
/// <Param name = "type"> protocol type </param>
Public void RemoveStaticPortMapping (int ExternalPort, ProtocolType type)
{
_ UPnPNAT. StaticPortMappingCollection. Remove (ExternalPort, type. ToString (). ToUpper ());
}
Public UPnPNATClass UPnPNAT
{
Get {return _ uPnPNAT ;}
}
/// <Summary>
/// Obtain port ing information
/// </Summary>
Public PortMappingInfo [] PortMappingsInfos
{
Get
{
System. Collections. ArrayList portMappings = new ArrayList ();
Int count = _ uPnPNAT. StaticPortMappingCollection. Count;
IEnumerator enumerator = _ uPnPNAT. StaticPortMappingCollection. GetEnumerator ();
Enumerator. Reset ();
For (int I = 0; I <count; I ++)
{
IStaticPortMapping mapping = null;
Try
{
If (enumerator. MoveNext ())
Mapping = (IStaticPortMapping) enumerator. Current;
}
Catch {}
If (mapping! = Null)
PortMappings. Add (new PortMappingInfo (mapping. InternalClient, mapping. ExternalPort, mapping. InternalPort, mapping. Protocol = "TCP "? ProtocolType. TCP: ProtocolType. UDP, mapping. Description ));
}
PortMappingInfo [] portMappingInfos = new PortMappingInfo [portMappings. Count];
PortMappings. CopyTo (portMappingInfos );
Return portMappingInfos;
}
}
}
}
Using the port ing function of NAT devices (routers) is an important method for implementing P2P. When UPNP is enabled, an Intranet computer is directly exposed to the Internet.