status-go/vendor/github.com/koron/go-ssdp/announce.go

111 lines
2.7 KiB
Go
Raw Permalink Normal View History

2019-06-09 07:24:20 +00:00
package ssdp
import (
"bytes"
"fmt"
"net"
2023-05-19 20:23:55 +00:00
"github.com/koron/go-ssdp/internal/multicast"
2019-06-09 07:24:20 +00:00
)
// AnnounceAlive sends ssdp:alive message.
2023-05-19 20:23:55 +00:00
// location should be a string or a ssdp.LocationProvider.
func AnnounceAlive(nt, usn string, location interface{}, server string, maxAge int, localAddr string) error {
locProv, err := toLocationProvider(location)
if err != nil {
return err
}
2019-06-09 07:24:20 +00:00
// dial multicast UDP packet.
2023-05-19 20:23:55 +00:00
conn, err := multicast.Listen(&multicast.AddrResolver{Addr: localAddr})
2019-06-09 07:24:20 +00:00
if err != nil {
return err
}
defer conn.Close()
// build and send message.
2023-05-19 20:23:55 +00:00
addr, err := multicast.SendAddr()
2019-06-09 07:24:20 +00:00
if err != nil {
return err
}
2023-05-19 20:23:55 +00:00
msg := &aliveDataProvider{
host: addr,
nt: nt,
usn: usn,
location: locProv,
server: server,
maxAge: maxAge,
2021-10-19 13:43:41 +00:00
}
if _, err := conn.WriteTo(msg, addr); err != nil {
2019-06-09 07:24:20 +00:00
return err
}
return nil
}
2023-05-19 20:23:55 +00:00
type aliveDataProvider struct {
host net.Addr
nt string
usn string
location LocationProvider
server string
maxAge int
}
func (p *aliveDataProvider) Bytes(ifi *net.Interface) []byte {
return buildAlive(p.host, p.nt, p.usn, p.location.Location(nil, ifi), p.server, p.maxAge)
}
var _ multicast.DataProvider = (*aliveDataProvider)(nil)
func buildAlive(raddr net.Addr, nt, usn, location, server string, maxAge int) []byte {
// bytes.Buffer#Write() is never fail, so we can omit error checks.
2019-06-09 07:24:20 +00:00
b := new(bytes.Buffer)
b.WriteString("NOTIFY * HTTP/1.1\r\n")
fmt.Fprintf(b, "HOST: %s\r\n", raddr.String())
fmt.Fprintf(b, "NT: %s\r\n", nt)
fmt.Fprintf(b, "NTS: %s\r\n", "ssdp:alive")
fmt.Fprintf(b, "USN: %s\r\n", usn)
if location != "" {
fmt.Fprintf(b, "LOCATION: %s\r\n", location)
}
if server != "" {
fmt.Fprintf(b, "SERVER: %s\r\n", server)
}
fmt.Fprintf(b, "CACHE-CONTROL: max-age=%d\r\n", maxAge)
b.WriteString("\r\n")
2023-05-19 20:23:55 +00:00
return b.Bytes()
2019-06-09 07:24:20 +00:00
}
// AnnounceBye sends ssdp:byebye message.
func AnnounceBye(nt, usn, localAddr string) error {
// dial multicast UDP packet.
2023-05-19 20:23:55 +00:00
conn, err := multicast.Listen(&multicast.AddrResolver{Addr: localAddr})
2019-06-09 07:24:20 +00:00
if err != nil {
return err
}
defer conn.Close()
// build and send message.
2023-05-19 20:23:55 +00:00
addr, err := multicast.SendAddr()
2021-10-19 13:43:41 +00:00
if err != nil {
return err
}
msg, err := buildBye(addr, nt, usn)
2019-06-09 07:24:20 +00:00
if err != nil {
return err
}
2023-05-19 20:23:55 +00:00
if _, err := conn.WriteTo(multicast.BytesDataProvider(msg), addr); err != nil {
2019-06-09 07:24:20 +00:00
return err
}
return nil
}
func buildBye(raddr net.Addr, nt, usn string) ([]byte, error) {
b := new(bytes.Buffer)
// FIXME: error should be checked.
b.WriteString("NOTIFY * HTTP/1.1\r\n")
fmt.Fprintf(b, "HOST: %s\r\n", raddr.String())
fmt.Fprintf(b, "NT: %s\r\n", nt)
fmt.Fprintf(b, "NTS: %s\r\n", "ssdp:byebye")
fmt.Fprintf(b, "USN: %s\r\n", usn)
b.WriteString("\r\n")
return b.Bytes(), nil
}