Fix #41 - collisions with same USN as multiple locations.

This commit is contained in:
John Beisley 2021-06-26 17:01:14 +01:00
parent 16e24d5762
commit 33cdcbb30d
2 changed files with 10 additions and 12 deletions

View File

@ -51,7 +51,8 @@ func (err ContextError) Error() string {
// MaybeRootDevice contains either a RootDevice or an error. // MaybeRootDevice contains either a RootDevice or an error.
type MaybeRootDevice struct { type MaybeRootDevice struct {
// Identifier of the device. // Identifier of the device. Note that this in combination with Location
// uniquely identifies a result from DiscoverDevices.
USN string USN string
// Set iff Err == nil. // Set iff Err == nil.

View File

@ -72,7 +72,7 @@ func SSDPRawSearch(
isExactSearch := searchTarget != SSDPAll && searchTarget != UPNPRootDevice isExactSearch := searchTarget != SSDPAll && searchTarget != UPNPRootDevice
seenUSNs := make(map[string]bool) seenIDs := make(map[string]bool)
var responses []*http.Response var responses []*http.Response
for _, response := range allResponses { for _, response := range allResponses {
if response.StatusCode != 200 { if response.StatusCode != 200 {
@ -83,17 +83,14 @@ func SSDPRawSearch(
continue continue
} }
usn := response.Header.Get("USN") usn := response.Header.Get("USN")
if usn == "" { loc, err := response.Location()
// Empty/missing USN in search response - using location instead.
location, err := response.Location()
if err != nil { if err != nil {
// No usable location in search response - discard. // No usable location in search response - discard.
continue continue
} }
usn = location.String() id := loc.String() + "\x00" + usn
} if _, alreadySeen := seenIDs[id]; !alreadySeen {
if _, alreadySeen := seenUSNs[usn]; !alreadySeen { seenIDs[id] = true
seenUSNs[usn] = true
responses = append(responses, response) responses = append(responses, response)
} }
} }