mzh/blog

SSDP协议笔记

近来在研究SSDP,Simple Service Discovery Protocol (简单服务发现协议)。 这是用来实现无配置,自发现局域网内部服务的协议。 由IPv4下有固定的239.255.255.250:1900这一固定的地址来负责多播数据。 不过,从我的学习经历来说,要啃这种东西,最好的方法还是用例子搞懂名词,并实践一次。 其实SSDP协议的请求就三种: byebye, alive, discovery

byebye请求

NOTIFY * HTTP/1.1
Host: 239.255.255.250:1900
NT: someunique:idscheme3
NTS: ssdp:byebye
USN: someunique:idscheme3

5.3.5. Shouldn’t the NT and NTS values be switched? Yes, they should. Commands such as ssdp:alive and ssdp:byebye should be NT values and the service type, where necessary, should be the NTS. The current mix-up is a consequence of a previous design where the NT header was used in a manner much like we use the USN today. This really needs to change.

alive(服务上线/广播存活/心跳包)

NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=100
LOCATION: http://10.5.4.81:49155/TxMediaRenderer_desc.xml
NT: upnp:rootdevice
NTS: ssdp:alive
USN: uuid:001e4fd3fa0e0000_MR::upnp:rootdevice

discovery请求

M-SEARCH * HTTP/1.1
Host:239.255.255.250:1900
ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1
Man:"ssdp:discover"
MX:3

服务发现的现实流程

            +---------+ +---------+               +-----------+
            | Client  | | Server  |               | Multicast |
            +---------+ +---------+               +-----------+
---------------\ |           |                          |
| Initialized  |-|           |                          |
---------------- |           |                          |
                 |           |                          |
                 | discovery |                          |
                 |------------------------------------->|
                 |           |                          |
                 |           |          Client wants ST |
                 |           |< ------------------------|
                 |           | -------------------\     |
                 |           |-| In discovery ST? |     |
                 |           | --------------------     |
                 |           |                          |
                 |           | (In ST) alive            |
                 |           |------------------------->|
                 |           |                          |
                 |           |           Here is Server |
                 |< ------------------------------------|
                 |           |                          |

好了,这个协议就这么Simple~