2013-10-06 12:23:11 +00:00
|
|
|
package ssdp
|
2013-09-27 23:09:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-06-08 12:45:53 +00:00
|
|
|
ssdpDiscover = `"ssdp:discover"`
|
|
|
|
ntsAlive = `ssdp:alive`
|
|
|
|
ntsByebye = `ssdp:byebye`
|
|
|
|
ntsUpdate = `ssdp:update`
|
|
|
|
ssdpUDP4Addr = "239.255.255.250:1900"
|
|
|
|
ssdpSearchPort = 1900
|
|
|
|
methodSearch = "M-SEARCH"
|
|
|
|
methodNotify = "NOTIFY"
|
2018-03-15 19:11:26 +00:00
|
|
|
|
|
|
|
// SSDPAll is a value for searchTarget that searches for all devices and services.
|
|
|
|
SSDPAll = "ssdp:all"
|
|
|
|
// UPNPRootDevice is a value for searchTarget that searches for all root devices.
|
|
|
|
UPNPRootDevice = "upnp:rootdevice"
|
2013-09-27 23:09:38 +00:00
|
|
|
)
|
|
|
|
|
2020-05-10 11:36:36 +00:00
|
|
|
// HTTPUClient is the interface required to perform HTTP-over-UDP requests.
|
|
|
|
type HTTPUClient interface {
|
|
|
|
Do(
|
|
|
|
req *http.Request,
|
|
|
|
timeout time.Duration,
|
|
|
|
numSends int,
|
|
|
|
) ([]*http.Response, error)
|
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
// SSDPRawSearch performs a fairly raw SSDP search request, and returns the
|
|
|
|
// unique response(s) that it receives. Each response has the requested
|
|
|
|
// searchTarget, a USN, and a valid location. maxWaitSeconds states how long to
|
|
|
|
// wait for responses in seconds, and must be a minimum of 1 (the
|
|
|
|
// implementation waits an additional 100ms for responses to arrive), 2 is a
|
|
|
|
// reasonable value for this. numSends is the number of requests to send - 3 is
|
|
|
|
// a reasonable value for this.
|
2020-05-10 11:36:36 +00:00
|
|
|
func SSDPRawSearch(
|
|
|
|
httpu HTTPUClient,
|
|
|
|
searchTarget string,
|
|
|
|
maxWaitSeconds int,
|
|
|
|
numSends int,
|
|
|
|
) ([]*http.Response, error) {
|
2013-09-27 23:09:38 +00:00
|
|
|
if maxWaitSeconds < 1 {
|
|
|
|
return nil, errors.New("ssdp: maxWaitSeconds must be >= 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := http.Request{
|
|
|
|
Method: methodSearch,
|
|
|
|
// TODO: Support both IPv4 and IPv6.
|
|
|
|
Host: ssdpUDP4Addr,
|
|
|
|
URL: &url.URL{Opaque: "*"},
|
|
|
|
Header: http.Header{
|
|
|
|
// Putting headers in here avoids them being title-cased.
|
|
|
|
// (The UPnP discovery protocol uses case-sensitive headers)
|
|
|
|
"HOST": []string{ssdpUDP4Addr},
|
|
|
|
"MX": []string{strconv.FormatInt(int64(maxWaitSeconds), 10)},
|
|
|
|
"MAN": []string{ssdpDiscover},
|
|
|
|
"ST": []string{searchTarget},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
allResponses, err := httpu.Do(&req, time.Duration(maxWaitSeconds)*time.Second+100*time.Millisecond, numSends)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-15 19:11:26 +00:00
|
|
|
|
|
|
|
isExactSearch := searchTarget != SSDPAll && searchTarget != UPNPRootDevice
|
|
|
|
|
2021-06-26 16:01:14 +00:00
|
|
|
seenIDs := make(map[string]bool)
|
2020-05-10 11:36:36 +00:00
|
|
|
var responses []*http.Response
|
2013-09-27 23:09:38 +00:00
|
|
|
for _, response := range allResponses {
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
log.Printf("ssdp: got response status code %q in search response", response.Status)
|
|
|
|
continue
|
|
|
|
}
|
2018-03-15 19:11:26 +00:00
|
|
|
if st := response.Header.Get("ST"); isExactSearch && st != searchTarget {
|
2014-01-06 20:14:53 +00:00
|
|
|
continue
|
2013-09-27 23:09:38 +00:00
|
|
|
}
|
|
|
|
usn := response.Header.Get("USN")
|
2021-06-26 16:01:14 +00:00
|
|
|
loc, err := response.Location()
|
|
|
|
if err != nil {
|
|
|
|
// No usable location in search response - discard.
|
|
|
|
continue
|
2013-09-27 23:09:38 +00:00
|
|
|
}
|
2021-06-26 16:01:14 +00:00
|
|
|
id := loc.String() + "\x00" + usn
|
|
|
|
if _, alreadySeen := seenIDs[id]; !alreadySeen {
|
|
|
|
seenIDs[id] = true
|
2013-09-27 23:09:38 +00:00
|
|
|
responses = append(responses, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses, nil
|
|
|
|
}
|