学习UPnP

转载自http://blog.csdn.net/ohatn/article/details/6255488

UPnP(Universal plug-and-play 即通用即插即用)是由通用即插即用论坛 (UPnP™ Forum)提出的一套网络协议。该协议的目标是使家庭网络(数据共享、通信和娱乐)和公司网络中的各种设备能够相互无缝连接,并简化相关网络的实现。UPnP 通过定义和发布基于开放、因特网通讯网协议标准的 UPnP 设备控制协议来实现这一目标。
简单地理解,UPnP就是一种动态端口映射。
详细一点说,假设内网某台机器连接的网关设备支持 UPNP igd 接口并开启了此项功能,那么网关设备就能够响应内网机器的请求,执行一些和网关相关的操作,比如将内网机器的某个端口映射到外网某个固定端口(即 NAT)。这样就能够使外网能够直接访问到内网机器的某端口。

那么,UPnP应用在什么地方呢?
我们比较熟悉的eMule、BitComet等工具,就使用了UPnP,所以我们可以通过打开ADSL的UPnP来提高下载速度。
更远一些的应用前景可能在数字家电上。支持UPnP的数字家电,在得到用户的授权后,能够自动到家中的网关上开启端口映射,这样用户就可以在外网对家中的家电进行控制。

下面简单介绍一下UPnP端口映射的过程和简单原理:
(下面文章中的控制点表示内网中的机器)

第0步:寻址,即获取一个本地IP地址。

第1步:发现,即搜索网络中的UPnP设备。又分两个小的步骤。
第1.1步:搜索UPnP设备。
控制点生成一个UDP的MulticastSocket,绑定多播地址239.255.255.250 1900端口,然后发送一个搜索请求,该请求在UPnP Device Architecture中的定义如下:
M-SEARCH * HTTP/1.1
HOST:239.255.255.250:1900
MAN:”ssdp:discover”
MX:seconds to delay response
ST:search target

这个多播请求的含义如下:
M-SEARCH表示SSDP协议定义的搜索请求方法。
HOST的值必须是这个地址。
MAN的值必须是”ssdp:discover”,注意不能少了双引号。
MX的表示UPnP设备收到该请求后的延时时间,以便控制点能够处理请求。
ST表示搜索目标,
如果搜索所有的设备和服务,则为ssdp:all,
如果是搜索根设备,则为upnp:rootdevice,
象大部分程序中使用UPnP是为了找到支持UPnP的网关来动态映射端口,
则赋值为urn:schemas-upnp-org:device:InternetGatewayDevice:1。
另外在编程时需要在每一行后面加上”/r/n” 表示换行。
该请求基于HTTPMU(HTTP Multicast over UDP),上面这些信息都是属于HTTP Header,没有HTTP Body,而HTTP Header和HTTP Body之间应该有一个空行分隔。编写时需要注意不能少了那个空行。

第1.2步:网络上的UPnP设备返回响应。如果该UPnP设备和要搜索的UPnP设备匹配,则该设备会返回一个响应,响应的格式在UPnP Device Architecture中的定义如下:
HTTP/1.1 200 OK
CACHE-CONTROL: max-age = seconds until advertisement expires
DATE: when response was generated
EXT:
LOCATION: URL for UPnP description for root device
SERVER: OS/version UPnP/1.0 product/version
ST: search target
USN: advertisement UUID

max-age通知消息存活时间,如果超过此时间间隔,控制点可以认为设备不存在。
LOCATION表示该设备的描述文件,用于确定该设备包含哪些逻辑设备和哪些服务等。
其中包含设备描述的URL地址。
USN表示Unique Service Name。

接收到数据包后,就要从中获取需要的消息。
首先,必须找到”200 OK”。
接着,要找到LOCATION项,并从中获得设备描述URL。
这一部分的逻辑实际上就是一个子字符串的查找。

第2步:描述,得到UPnP设备属性和描述信息。
第2.1步:下载设备描述文件。
2.1.1 解析描述文件的URL,获取主机/端口/路径;
2.1.2 建立TCP连接到host:post;
2.1.3 构造类似
GET path HTTP/1.1
Host: host:port

的信息(注意第二行下面要有一个空行),并发送到设备;
2.1.4 接收设备响应的数据。

第2.2步:解析XML文件。
设备响应的格式在UPnP Device Architecture中的定义如下:

<?xml version="1.0"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
  <specVersion>
    <major>1</major>
    <minor>0</minor>
  </specVersion>
  <URLBase>base URL for all relative URLs</URLBase>
  <device>
    <deviceType>urn:schemas-upnp-org:device:deviceType:v </deviceType>
    <friendlyName>short user-friendly title</friendlyName>
    <manufacturer>manufacturer name</manufacturer>
    <manufacturerURL>URL to manufacturer site</manufacturerURL>
    <modelDescription>long user-friendly title</modelDescription>
    <modelName>model name</modelName>
    <modelNumber>model number</modelNumber>
    <modelURL>URL to model site</modelURL>
    <serialNumber>manufacturer's serial number</serialNumber>
    <UDN>uuid:UUID</UDN>
    <UPC>Universal Product Code</UPC>
    <iconList>
      <icon>
        <mimetype>image/format</mimetype>
        <width>horizontal pixels</width>
        <height>vertical pixels</height>
        <depth>color depth</depth>
        <url>URL to icon</url>
      </icon>
      XML to declare other icons, if any, go here
    </iconList>
    <serviceList>
      <service>
        <serviceType>urn:schemas-upnp-org:service:serviceType:v </serviceType>
        <serviceId>urn:upnp-org:serviceId:serviceID </serviceId>
        <SCPDURL>URL to service description</SCPDURL>
        <controlURL>URL for control</controlURL>
        <eventSubURL>URL for eventing</eventSubURL>
      </service>
      Declarations for other services defined by a UPnP Forum working committee (if any) go here
      Declarations for other services added by UPnP vendor (if any) go here
    </serviceList>
    <deviceList>
      Description of embedded devices defined by a UPnP Forum working committee (if any) go here
      Description of embedded devices added by UPnP vendor (if any) go here
    </deviceList>
    <presentationURL>URL for presentation</presentationURL>
  </device>
</root>

上面的设备描述中有一个ServiceList节点,该节点下每个Service节点都包含一个controlURL节点,但这个URL是一个相对URL,再组合URLBase节点的属性值,即得到该服务的控制URL。

第3步:控制,对UPnP设备发送控制命令(如增删端口映射)或查询属性。
这个操作是通过发送相应SOAP消息到该服务的控制URL上来完成的。该信息在UPnP Device Architecture中的定义如下:
POST path of control URL HTTP/1.1
HOST: host of control URL :port of control URL
CONTENT-LENGTH: bytes in body
CONTENT-TYPE: text/xml; charset=”utf-8”
SOAPACTION: “urn:schemas-upnp-org:service:serviceType:v #actionName “

<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <u:actionName xmlns:u="urn:schemas-upnp-org:service:serviceType:v ">
      <argumentName >in arg value</argumentName >
      other in args and their values go here, if any
    </u:actionName >
  </s:Body>
</s:Envelope>

在上面的SOAP消息中:
在actionName处填入各种控制信息,如:
AddPortMapping
或者DeletePortMapping
或者GetGenericPortMappingEntry。
在serviceType处填入设备的服务类型,如:
urn:schemas-upnp-org:service:WANIPConnection:1
或者urn:schemas-upnp-org:service:WANPPPConnection:1。
在 argumentName 处填入各种控制信息,各种控制消息的格式如下:
增加端口映射AddPortMapping:

<NewRemoteHost></NewRemoteHost>
  <NewExternalPort>ExternalPort</NewExternalPort>
  <NewProtocol>Protocol</NewProtocol>
  <NewInternalPort>InternalPort</NewInternalPort>
  <NewInternalClient>InternalClient</NewInternalClient>
  <NewEnabled>1</NewEnabled>
  <NewPortMappingDescription>PortMappingDescription</NewPortMappingDescription>
  <NewLeaseDuration>LeaseDuration</NewLeaseDuration>

注:Protocol 一般填TCP或UDP。
PortMappingDescription 填写端口映射的描述,比如什么程序建立了这个端口。
InternalClient 填写本地IP地址。
LeaseDuration 填写映射的持续时间,用0表示不永久。

删除端口映射DeletePortMapping:

<NewRemoteHost></NewRemoteHost>
  <NewExternalPort>ExternalPort</NewExternalPort>
  <NewProtocol>Protocol</NewProtocol>

获得端口映射信息GetGenericPortMappingEntry:

 <NewPortMappingIndex>PortMappingIndex</NewPortMappingIndex>
  <NewRemoteHost></NewRemoteHost>
  <NewExternalPort></NewExternalPort>
  <NewProtocol></NewProtocol>
  <NewInternalPort></NewInternalPort>
  <NewInternalClient></NewInternalClient>
  <NewEnabled>1</NewEnabled>
  <NewPortMappingDescription></NewPortMappingDescription>
  <NewLeaseDuration></NewLeaseDuration>

注: PortMappingIndex 填写端口映射索引,即路由上第几个映射。

以在IGD设备上增加端口映射这个操作来举例,将网关的5678端口映射到内网10.0.0.1的8765端口,该操作被发送的SOAP消息如下:
POST /upnp/control/wanpppcpppoe HTTP/1.0
CONTENT-TYPE: text/xml; charset=”utf-8”
HOST: 10.0.0.138:80
CONTENT-LENGTH: 649
SOAPACTION: “urn:schemas-upnp-org:service:WANPPPConnection:1#AddPortMapping”

<?xml version="1.0" encoding="utf-8"?>
<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:WANPPPConnection:1">
   <NewRemoteHost></NewRemoteHost>
   <NewExternalPort>5678</NewExternalPort>
   <NewProtocol>tcp</NewProtocol>
   <NewInternalPort>8765</NewInternalPort>
   <NewInternalClient>10.0.0.1</NewInternalClient>
   <NewEnabled></NewEnabled>
   <NewPortMappingDescription></NewPortMappingDescription>
   <NewLeaseDuration></NewLeaseDuration>
  </u:AddPortMapping>
 </s:Body>
</s:Envelope>

操作成功,设备的应答是:
HTTP/1.0 200 OK
CONTENT-TYPE: text/xml; charset=”utf-8”
SERVER: SpeedTouch 510 4.0.2.0.1 UPnP/1.0 (14E31Y7)
CONTENT-LENGTH: 304
Connection: close
EXT:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <m:AddPortMappingResponse xmlns:m="urn:schemas-upnp-org:service:WANPPPConnection:1"></m:AddPortMappingResponse>
  </s:Body>
</s:Envelope>

接收到应答后,一般检测到“200 OK”即可认为成功。

第4步:事件触发。在设备一些属性变化了的时候,如果控制点订阅了它的事件通知,则它会发送相应的通知给控制点。

第5步:展示。

UPnP Device Architecture的翻译版可以在CSDN资源中找,名为UPnP设备架构。

以上文章取自网上多篇文章,在此表示感谢。

相关链接:
http://www.upnp.org/download/UPnPDA10_20000613.htm
http://blog.csdn.net/motiandashao/archive/2007/03/30/1547184.aspx
http://hi.baidu.com/neron/blog/item/8a7db81e136d2617403417d9.html/cmtid/02b0ec1fd579d56ef724e410

猜你喜欢

转载自blog.csdn.net/wufeiqing/article/details/50485089