forked from scottjg/upnp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchGatewayMsg.go
More file actions
121 lines (112 loc) · 2.96 KB
/
SearchGatewayMsg.go
File metadata and controls
121 lines (112 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package upnp
import (
"log"
"net"
"strings"
"time"
// "net/http"
)
type Gateway struct {
GatewayName string
Host string
DeviceDescUrl string
Cache string
ST string
USN string
deviceType string // Device URN "urn:schemas-upnp-org:service:WANIPConnection:1"
ControlURL string
ServiceType string
}
type SearchGateway struct {
searchMessage string
upnp *Upnp
}
func (this *SearchGateway) Send() bool {
this.buildRequest()
c := make(chan string)
go this.send(c)
result := <-c
if result == "" {
// time out
this.upnp.Active = false
return false
}
this.resolve(result)
this.upnp.Gateway.ServiceType = "urn:schemas-upnp-org:service:WANIPConnection:1"
this.upnp.Active = true
return true
}
func (this *SearchGateway) send(c chan string) {
// Send multicast messages, to bring the port, the format as:"239.255.255.250:1900"
var conn *net.UDPConn
defer func() {
if r := recover(); r != nil {
// Time out
}
}()
go func(conn *net.UDPConn) {
defer func() {
if r := recover(); r != nil {
// No overtime
}
}()
// The timeout is 3 seconds
time.Sleep(time.Second * 3)
c <- ""
conn.Close()
}(conn)
remotAddr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
if err != nil {
log.Println("The multicast address format is incorrect")
}
locaAddr, err := net.ResolveUDPAddr("udp", this.upnp.LocalHost+":")
if err != nil {
log.Println("The local ip address is not in the correct format")
}
conn, err = net.ListenUDP("udp", locaAddr)
defer conn.Close()
if err != nil {
log.Println("Failed to bind udp multicast socket")
}
_, err = conn.WriteToUDP([]byte(this.searchMessage), remotAddr)
if err != nil {
log.Println("An error occurred sending msg to the multicast address")
}
buf := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(buf)
if err != nil {
log.Println("An error occurred reading the multicast address search message")
}
result := string(buf[:n])
c <- result
}
func (this *SearchGateway) buildRequest() {
this.searchMessage = "M-SEARCH * HTTP/1.1\r\n" +
"HOST: 239.255.255.250:1900\r\n" +
"ST: urn:schemas-upnp-org:service:WANIPConnection:1\r\n" +
"MAN: \"ssdp:discover\"\r\n" + "MX: 3\r\n\r\n"
}
func (this *SearchGateway) resolve(result string) {
this.upnp.Gateway = &Gateway{}
lines := strings.Split(result, "\r\n")
for _, line := range lines {
// According to the first colon is divided into two strings
nameValues := strings.SplitAfterN(line, ":", 2)
if len(nameValues) < 2 {
continue
}
switch strings.ToUpper(strings.Trim(strings.Split(nameValues[0], ":")[0], " ")) {
case "ST":
this.upnp.Gateway.ST = nameValues[1]
case "CACHE-CONTROL":
this.upnp.Gateway.Cache = nameValues[1]
case "LOCATION":
urls := strings.Split(strings.Split(nameValues[1], "//")[1], "/")
this.upnp.Gateway.Host = urls[0]
this.upnp.Gateway.DeviceDescUrl = "/" + urls[1]
case "SERVER":
this.upnp.Gateway.GatewayName = nameValues[1]
default:
}
}
}