upnp 端口映射

简介
处于内网的机器,因为没有一个公有的Ip,外面是无法主动连接到内网的设备。如果希望外面可以连接到处于内网的指定设备。UPNP端口映射是一个较简易的方案。当然这并不是唯一方案,穿透和转发也是一个较普遍的方式,本文只讨论UPNP端口映射的实现方式。UPNP端口映射将一个外部端口映射到一个【内网Ip:端口】,当然也指明了使用的协议是UDP,TCP,还是此映射同时适应UDP和TCP。

UPNP使用的常用命令:
     "GetStatusInfo",
     "GetExternalIPAddress",
     "AddPortMapping",
     "GetSpecificPortMappingEntry",

1. 建立UDP socket,并绑定到一个自定的端口
2、通过UDP发送组播包:M-search,端口1900,包的内容如下所示
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1
MAN: "ssdp:discover"
MX: 2
3、接收M-search的回应,并做相应的处理
HTTP/1.1 200 OK
CACHE-CONTROL: max-age=100
DATE: Thu, 01 Jan 1970 21:39:02 GMT
EXT:
LOCATION: http://192.168.1.1:1900/igd.xml
SERVER: Mercury Router MR804 Router, UPnP/1.0
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1
USN: uuid:6ec73f58-1dd2-11b2-9465-e6838914e4d9::urn:schemas-upnp-org:device:InternetGatewayDevice:1
1》获取path(lacation) ,此为 : /igd.xml                                        #path#
     端口为: 1900 (有些路由并不一定是UPNP的端口1900)           #ServerPort#
     ip:192.168.1.1 (有时候我们的内网中可能存在多个路由,并且都开启了upnp服务,这时候我们要判断哪一个才是网关路由,并且只处理网关路由的回应)
2》获取ST,此为:urn:schemas-upnp-org:device:InternetGatewayDevice:1        #ST#
4、建立tcp连接,--》【网关:ServerPort】
注意:有些路由不支持keep-alive特性,做每一次交互都做一次连接
5、获取path位置的xml数据:
GET /igd.xml HTTP/1.1
Host: 192.168.1.1:1900
Connection: Close
User-Agent: MSWindows/6.1.7601, UPnP/1.0, MiniUPnPc/1.6
6、接收并分析得到的xml数据
HTTP/1.1 200 OK
CONTENT-LENGTH: 2733
CONTENT-TYPE: text/xml
DATE: Thu, 01 Jan 1970 21:39:02 GMT
LAST-MODIFIED: Tue, 28 Oct 2003 08:46:08 GMT
SERVER: Mercury Router MR804 Router, UPnP/1.0
CONNECTION: close

<?xml version="1.0"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<URLBase></URLBase>
<device>
<deviceType>urn:schemas-upnp-org:device:InternetGatewayDevice:1</deviceType>
<presentationURL>http://192.168.1.1:80       </presentationURL>
<friendlyName            >Mercury Router MR804</friendlyName>
<manufacturer                         >Mercury</manufacturer>
<manufacturerURL    >http://www.mercurycom.com.cn</manufacturerURL>
<modelDescription                       >MR804 8.0</modelDescription>
<modelName                           >MR804</modelName>
<modelNumber                             >8.0</modelNumber>
<UDN>uuid:6ec73f58-1dd2-11b2-9465-e6838914e4d9</UDN>
<UPC>123456789001</UPC>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType>
<serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId>
<controlURL>/l3f</controlURL>
<eventSubURL>/l3f</eventSubURL>
<SCPDURL>/l3f.xml</SCPDURL>
</service>
</serviceList>
<deviceList>
<device>
<deviceType>urn:schemas-upnp-org:device:WANDevice:1</deviceType>
<friendlyName>WAN Device</friendlyName>
<manufacturer                         >Mercury</manufacturer>
<manufacturerURL    >http://www.mercurycom.com.cn</manufacturerURL>
<modelDescription>WAN Device</modelDescription>
<modelName>WAN Device</modelName>
<modelNumber>1</modelNumber>
<modelURL></modelURL>
<serialNumber>12345678900001</serialNumber>
<UDN>uuid:80104052-1dd2-11b2-a699-e6838914e4d9</UDN>
<UPC>123456789001</UPC>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1</serviceType>
<serviceId>urn:upnp-org:serviceId:WANCommonInterfaceConfig</serviceId>
<controlURL>/ifc</controlURL>
<eventSubURL>/ifc</eventSubURL>
<SCPDURL>/ifc.xml</SCPDURL>
</service>
</serviceList>
<deviceList>
<device>
<deviceType>urn:schemas-upnp-org:device:WANConnectionDevice:1</deviceType>
<friendlyName>WAN Connection Device</friendlyName>
<manufacturer                         >Mercury</manufacturer>
<manufacturerURL    >http://www.mercurycom.com.cn</manufacturerURL>
<modelDescription>WAN Connection Device</modelDescription>
<modelName>WAN Connection Device</modelName>
<modelNumber>1</modelNumber>
<modelURL></modelURL>
<serialNumber>12345678900001</serialNumber>
<UDN>uuid:8c99e7b0-1dd2-11b2-8291-e6838914e4d9</UDN>
<UPC>123456789001</UPC>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>
<serviceId>urn:upnp-org:serviceId:WANIPConnection</serviceId>
<controlURL>/ipc</controlURL>
<eventSubURL>/ipc</eventSubURL>
<SCPDURL>/ipc.xml</SCPDURL>
</service>
</serviceList>
</device>
</deviceList>
</device>
</deviceList>
</device>
</root>
获取端口映射服务<serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>
的控制路径<controlURL>:/ipc                #ControlUrl#
7、发送获取外网IP的命令请求:GetExternalIPAddress
POST /ipc HTTP/1.1
Host: 192.168.1.1:1900
User-Agent: MSWindows/6.1.7601, UPnP/1.0, MiniUPnPc/1.6
Content-Length: 285
Content-Type: text/xml
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"
Connection: Close
Cache-Control: no-cache
Pragma: no-cache

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:GetExternalIPAddress></s:Body></s:Envelope>
8、分析接收数据
HTTP/1.1 200 OK
CONNECTION: close
SERVER: Mercury Router MR804 Router, UPnP/1.0
CONTENT-LENGTH: 404
CONTENT-TYPE: text/xml; charset="utf-8"

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewExternalIPAddress>113.119.126.148</NewExternalIPAddress></u:GetExternalIPAddressResponse></SOAP-ENV:Body>
</SOAP-ENV:Envelope>

解析得到NewExternalIPAddress标签中的外网ip:113.119.126.148
9、发送命令获取指定的外部端口的详细信息{内部端口,ip,协议,外部端口,。。。}
使用命令GetSpecificPortMappingEntry,并在NewExternalPort指定要查询的外部端口
POST /ipc HTTP/1.1
Host: 192.168.1.1:1900
User-Agent: MSWindows/6.1.7601, UPnP/1.0, MiniUPnPc/1.6
Content-Length: 399
Content-Type: text/xml
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetSpecificPortMappingEntry"
Connection: Close
Cache-Control: no-cache
Pragma: no-cache

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetSpecificPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>5500</NewExternalPort><NewProtocol>TCP</NewProtocol></u:GetSpecificPortMappingEntry></s:Body></s:Envelope>
10、接收数据并并分析指定的端口有没有被映射
HTTP/1.1 200 OK
CONNECTION: close
SERVER: Mercury Router MR804 Router, UPnP/1.0
CONTENT-LENGTH: 574
CONTENT-TYPE: text/xml; charset="utf-8"

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<u:GetSpecificPortMappingEntryResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewInternalPort>80</NewInternalPort><NewInternalClient>192.168.1.83</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>JUAN UPNP</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:GetSpecificPortMappingEntryResponse></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如上所示:外部端口5500已经做了映射,映射到{192.168.1.83:80,TCP}
1.1》映射到期望的客户端
1.2》没有映射到期望的客户端,如映射到别的ip
2》未映射,会返回500错误:HTTP/1.0 500 Internal Server Error
HTTP/1.1 500 Internal Server Error
CONTENT-LENGTH: 481
CONTENT-TYPE: text/xml; charset="utf-8"
DATE: Thu, 01 Jan 1970 00:00:02 GMT
EXT:
SERVER: Mercury Router MR804 Router, UPnP/1.0

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
<errorCode>402</errorCode>
<errorDescription>Invalid ExternalPort</errorDescription>
</UPnPError>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如果已经做了期望的映射,则进入等待下一轮的更新
否则做端口映射
11、发送命令添加端口映射:
POST /ipc HTTP/1.1
Host: 192.168.1.1:1900
User-Agent: MSWindows/6.1.7601, UPnP/1.0, MiniUPnPc/1.6
Content-Length: 589
Content-Type: text/xml
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"
Connection: Close
Cache-Control: no-cache
Pragma: no-cache

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>5500</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>80</NewInternalPort><NewInternalClient>192.168.1.83</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>JUAN UPNP</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>
12、接收并分析是否映射成功:
HTTP/1.1 200 OK
CONNECTION: close
SERVER: Mercury Router MR804 Router, UPnP/1.0
CONTENT-LENGTH: 332
CONTENT-TYPE: text/xml; charset="utf-8"

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
返回HTTP/1.1 200 OK,可见操作成功,进入等待下一轮的更新
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要删除华为路由器上的UPnP端口映射表,可以按照以下步骤进行操作: 1. 在计算机中打开一个浏览器,输入路由器的IP地址(通常是192.168.1.1或192.168.0.1)并按下Enter键。这将打开路由器的管理页面。 2. 使用正确的用户名和密码登录到路由器管理页面。如果你没有更改过默认设置,一般是admin/admin或者admin/password。登录成功后,会显示路由器的设置页面。 3. 在路由器的设置页面中,找到并点击“高级设置”或“高级选项”等类似的标签。 4. 在高级设置页面中,继续寻找并点击“UPnP”或“UPnP设置”等相关选项。 5. 在UPnP设置页面中,应该可以看到当前路由器上存在的所有端口映射规则。你可以查找要删除的端口映射规则所在的列表。 6. 找到要删除的端口映射规则后,可以选择其旁边的“删除”、“清除”或类似的选项。有时候,可能需要先选中要删除的规则,然后点击页面底部的“删除”按钮。 7. 点击删除选项后,路由器会提示你确认删除操作。请确认你确实要删除该端口映射规则,并点击确认或确定按钮。 8. 删除成功后,路由器会重新加载设置,并将你删除的端口映射规则从列表中移除。 请注意,不同型号的华为路由器的管理页面可能会有所不同,以上的操作步骤仅供参考。如果你不清楚具体操作步骤,建议查阅华为路由器的用户手册或咨询华为客服进行具体指导。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值