Skip to content

Commit 78fc57d

Browse files
committed
Dynamic port allocation for ssdp searching
1 parent a09958d commit 78fc57d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

devices/devices.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package devices
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"sort"
78

89
"github.com/alexballas/go-ssdp"
@@ -21,7 +22,31 @@ var (
2122
func LoadSSDPservices(delay int) (map[string]string, error) {
2223
// Reset device list every time we call this.
2324
urlList := make(map[string]string)
24-
list, err := ssdp.Search(ssdp.All, delay, "239.255.255.250:1900")
25+
26+
port := 1900
27+
28+
var (
29+
address *net.UDPAddr
30+
tries int
31+
)
32+
33+
for address == nil && tries < 100 {
34+
addr := net.UDPAddr{
35+
Port: port,
36+
IP: net.ParseIP("239.255.255.250"),
37+
}
38+
39+
_, err := net.ListenUDP("udp", &addr)
40+
if err != nil {
41+
port++
42+
tries++
43+
continue
44+
}
45+
46+
address = &addr
47+
}
48+
49+
list, err := ssdp.Search(ssdp.All, delay, address.String())
2550
if err != nil {
2651
return nil, fmt.Errorf("LoadSSDPservices search error: %w", err)
2752
}

0 commit comments

Comments
 (0)