UPnP related information

Source: Internet
Author: User

Address: http://e2tox.cnblogs.com/archive/2006/07/13/449836.html

UPnP stands for universal plug-and-play ). UPnP is an architecture designed for the peer-to-peer network connection of smart home appliances, wireless devices, and PCs of various sizes. It is designed to provide easy-to-use, flexible, and standard-based connections for active-Hoc networks or unmanaged networks in homes, small businesses, public places, or connected to the Internet. (Refer to here .)

The automatic port ing we use here is only a small application of UPnP. According to the UPnP specifications, step 1 of the UPnP network is addressing (to obtain an IP address, this is not a problem I want to solve .)
Step 1 is to find that the control point searches for devices of interest on the Internet, and the device declares its service to the control point on the network. For automatic port ing, the router with UPnP function is found.
This process involves two steps. First, use a datagram socket to send a multicast request to 239.00000000250: 1900 in the following format:

M-SEARCH * HTTP/1.1
HOST: 239.00000000250: 1900
MAN: "SSDP: Discover"
MX: 3
St: UPNP: rootdevice

The meaning of this multicast request is as follows: The M-SEARCH SSDP Protocol defines the search request method. The host must be the multicast address. The value of man must also be "SSDP: Discover", which must contain double quotation marks. MX indicates the maximum wait time. You can set it by yourself. St indicates the search target. We use the root device here. In addition, we need to add "RN" to the end of each line to indicate a line break. (For details, see upnpnat. Discovery ()).

Step 2: If your network has an UPnP Device, the device must send a response to the source IP address and port of the multicast channel that sends the query request to be found. So you can receive the Response Message from the address 239.00000000250: 1900. Similar to the following message.

HTTP/1.1 200 OK
Cache-control: Max-age = 100
Date: Sun, 15 Jan 2006 06:51:02 GMT
Ext:
Location: http: // 192.168.14.1: 1900/IgD. xml
Server: TP-LINK wireless router wr541g/5, UPnP/1.0
St: UPNP: rootdevice
USN: UUID: upnp-InternetGatewayDevice-192168141678900001: UPNP: rootdevice

Next, we will get the message from it. First, we must find "200 OK", indicating that there is no error, otherwise everything is free of discussion. Next, we need to find the location item and get the device description URL. (ProgramIn the final result, it is a subcharacter search .)

here, our first step is "discovery.
Step 2 is the description. In step 1, we often get a device description URL. in step 2, we need to download an XML file through a URL. Find the device type, service type, control URL, event-triggered URL, and so on.
download the device description file in two steps. (See the upnpnat: get_description () function in the source code)
1. parse the URL of the description file to obtain the host, port, and path ). (parseurl function)
2. connect to host: Port (tcp_connect function)
3. construction is similar

Get path HTTP/1.1
HOST: Host: Port

And send it to the vro through the TCP socket. (Sprintf, send function ).

4. To receive data, I use the Recv function with the flag being msg_waitall. The function is blocked until all data is read. The data is stored in STD: String description_info.

I think the process of Downloading this file through a browser is similar.

Then parse the XML file. (See the upnpnat: parser_description () function in the source code)

We can find "device" childnode "of" root "," devicetype "is" urn: Schemas-UPnP-org: Device: internetgatewaydevice: 1, obtain the "devicelist" of this "device", and mark it as.

Find "device" childnode "of" devicetype "A:" urn: Schemas-UPnP-org: Device: wandevice: 1 ", and obtain" devicelist "of" device ", as B.

Find "device" childnode "of" devicetype "B is" urn: Schemas-UPnP-org: Device: wanconnectiondevice: 1 ", and obtain" servicelist "of" device ", c.

Find "servicetype" in C is "urn: Schemas-UPnP-org: Service: wanipconnection: 1" or "urn: Schemas-UPnP-org: Service: wanpppconnection: 1 "service" childnode, marked as D.
Get D's "controlurl" and save it in STD: String control_url.

However, the obtained control_url is generally relative URL, so you need to find the value of "urlbase" under "root". (if it is empty, use "htpp: // XXX" of describe_url. xxx. xxx. XXX: XXXX "partially replaced .)

Finally, add urlbasr to the relative control_url to obtain the complete control_url.

So far, the second step "Get control URL" is complete.

Step 2 is control. Some functions are implemented by sending Control Messages (also described in XML) to the control URL obtained in step 1. For automatic port ing, you can view, add, and delete ports.
Here I will first describe the formats of various control information. (The RN below indicates a line break. I cannot enter a backslash .)

  • Add port ing. "Addportmapping"
  • "<Newremotehost> </newremotehost> RN" "<newexternalport> Externalport </Newexternalport> RN "" <newprotocol> Protocol </Newprotocol> RN "" <newinternalport> Internalport </Newinternalport> N"
    "<Newinternalclient> Internalclient </Newinternalclient> RN"
    "<Newenabled> 1 </newenabled> RN"
    "<Newportmappingdescription> Portmappingdescription "" </Newportmappingdescription> RN"
    "<Newleaseduration> Leaseduration </Newleaseduration> RN"
  • Delete port ing "deleteportmapping"
  • "<Newremotehost> </newremotehost> RN" "<newexternalport>Externalport</Newexternalport> RN "" <newprotocol>Protocol</Newprotocol> RN"
  • Obtain the port ing information "getgenericportmappingentry"
  • " portmappingindex " " RN" " RN "" RN "" RN "" RN "" 1 RN "" "" RN "" RN "

The ITALIC part must be programmed. Externalport external port. Internalport internal port. Generally, the two are mapped ports. Set protocal to TCP or UDP. Interclient is generally a local IP address. Portmappingdescription: Enter the description of port ing, for example, what program creates this port. Leaseduration is the ing duration, and 0 indicates that it is not permanent. Portmappingindex is the port ing index, which is the first ing in the route.

Let's take a look at the structure of the XML document below.

" RN "
" " s: encodingstyle = "
" "http://schemas.xmlsoap.org/soap/encoding/"> RN "
" RN "
" actionname xmlns: U = " servicetype "> RN "
" actionparams actionname RN "
" RN "
" RN "

Enter "addportmapping" "deleteportmapping" "getgenericportmappingentry" in the actionname field ". Enter the service type of the device in servicetype. "Urn: Schemas-UPnP-org: Service: wanipconnection: 1" or "urn: Schemas-UPnP-org: Service: wanpppconnection: 1 ". Fill in various control information above in actionparams.

Add the HTTP header.

"PostPathHTTP/1.1rn"
"Host:Host:PortRN"
"Soapaction :"Servicetype#Actionname"RN"
"Content-Type: text/XML; charset =" UTF-8 "RN"
"Content-Length:ContentlengthRnrn"

The path host port is very obvious. The length of the XML document on contentlength.

Connect to host: Port and send it to the entire information to complete control.

Step 2 event triggering and Step 2 display are useless in automatic port ing. If you are interested, read the document by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.