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,可見操作成功,進入等待下一輪的更新
 
【轉】https://blog.csdn.net/swanabin/article/details/52074193


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM