ssdp

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2022 License: MIT Imports: 14 Imported by: 0

README

SSDP library

GoDoc Actions/Go Go Report Card

Based on https://tools.ietf.org/html/draft-cai-ssdp-v1-03.

Examples

There are tiny snippets for example. See also examples/ directory for working examples.

import "github.com/koron/go-ssdp"

ad, err := ssdp.Advertise(
    "my:device",                        // send as "ST"
    "unique:id",                        // send as "USN"
    "http://192.168.0.1:57086/foo.xml", // send as "LOCATION"
    "go-ssdp sample",                   // send as "SERVER"
    1800)                               // send as "maxAge" in "CACHE-CONTROL"
if err != nil {
    panic(err)
}

// run Advertiser infinitely.
quit := make(chan bool)
<-quit
Send alive periodically
import "time"

aliveTick := time.Tick(300 * time.Second)

for {
    select {
    case <-aliveTick:
        ad.Alive()
    }
}
Send bye when quiting
import (
    "os"
    "os/signal"
)

// to detect CTRL-C is pressed.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)

loop:
for {
    select {
    case <-aliveTick:
        ad.Alive()
    case <-quit:
        break loop
    }
}

// send/multicast "byebye" message.
ad.Bye()
// teminate Advertiser.
ad.Close()
Limitate interfaces to multicast

go-ssdp will send multicast messages to all IPv4 interfaces as default. When you want to limitate interfaces, see below snippet.

import (
    "github.com/koron/go-ssdp"
    "net"
)

en0, err := net.InterfaceByName("en0")
if err != nil {
    panic(err)
}
ssdp.Interfaces = []net.Interface{*en0}

go-ssdp will send multicast message only "en0" after this.

Documentation

Overview

Package ssdp provides ...

Index

Constants

View Source
const (
	// All is a search type to search all services and devices.
	All = "ssdp:all"

	// RootDevice is a search type to search UPnP root devices.
	RootDevice = "upnp:rootdevice"
)

Variables

View Source
var Interfaces []net.Interface

Interfaces specify target interfaces to multicast. If no interfaces are specified, all interfaces will be used.

View Source
var Logger *log.Logger

Logger is default logger for SSDP module.

Functions

func AnnounceAlive

func AnnounceAlive(nt, usn, location, server, metadata string, maxAge int, localAddr string) error

AnnounceAlive sends ssdp:alive message.

func AnnounceBye

func AnnounceBye(nt, usn, localAddr string) error

AnnounceBye sends ssdp:byebye message.

func SetMulticastRecvAddrIPv4

func SetMulticastRecvAddrIPv4(addr string) error

SetMulticastRecvAddrIPv4 updates multicast address where to receive packets. This never fail now.

func SetMulticastSendAddrIPv4

func SetMulticastSendAddrIPv4(addr string) error

SetMulticastSendAddrIPv4 updates a UDP address to send multicast packets. This never fail now.

Types

type Advertiser

type Advertiser struct {
	// contains filtered or unexported fields
}

Advertiser is a server to advertise a service.

func Advertise(st, usn, location, server, metadata string, maxAge int) (*Advertiser, error)

Advertise starts advertisement of service.

func (*Advertiser) Alive

func (a *Advertiser) Alive() error

Alive announces ssdp:alive message.

func (*Advertiser) Bye

func (a *Advertiser) Bye() error

Bye announces ssdp:byebye message.

func (*Advertiser) Close

func (a *Advertiser) Close() error

Close stops advertisement.

type AliveHandler

type AliveHandler func(*AliveMessage)

AliveHandler is handler of Alive message.

type AliveMessage

type AliveMessage struct {
	// From is a sender of this message
	From net.Addr

	// Type is a property of "NT"
	Type string

	// USN is a property of "USN"
	USN string

	// Location is a property of "LOCATION"
	Location string

	// Server is a property of "SERVER"
	Server string
	// contains filtered or unexported fields
}

AliveMessage represents SSDP's ssdp:alive message.

func (*AliveMessage) Header

func (m *AliveMessage) Header() http.Header

Header returns all properties in alive message.

func (*AliveMessage) MaxAge

func (m *AliveMessage) MaxAge() int

MaxAge extracts "max-age" value from "CACHE-CONTROL" property.

type ByeHandler

type ByeHandler func(*ByeMessage)

ByeHandler is handler of Bye message.

type ByeMessage

type ByeMessage struct {
	// From is a sender of this message
	From net.Addr

	// Type is a property of "NT"
	Type string

	// USN is a property of "USN"
	USN string
	// contains filtered or unexported fields
}

ByeMessage represents SSDP's ssdp:byebye message.

func (*ByeMessage) Header

func (m *ByeMessage) Header() http.Header

Header returns all properties in bye message.

type Monitor

type Monitor struct {
	Alive  AliveHandler
	Bye    ByeHandler
	Search SearchHandler
	// contains filtered or unexported fields
}

Monitor monitors SSDP's alive and byebye messages.

func (*Monitor) Close

func (m *Monitor) Close() error

Close closes monitoring.

func (*Monitor) Start

func (m *Monitor) Start() error

Start starts to monitor SSDP messages.

type SearchHandler

type SearchHandler func(*SearchMessage)

SearchHandler is handler of Search message.

type SearchMessage

type SearchMessage struct {
	From net.Addr
	Type string
	// contains filtered or unexported fields
}

SearchMessage represents SSDP's ssdp:discover message.

func (*SearchMessage) Header

func (s *SearchMessage) Header() http.Header

Header returns all properties in search message.

type Service

type Service struct {
	// Type is a property of "ST"
	Type string

	// USN is a property of "USN"
	USN string

	// Location is a property of "LOCATION"
	Location string

	// Server is a property of "SERVER"
	Server string
	// contains filtered or unexported fields
}

Service is discovered service.

func Search(searchType string, waitSec int, localAddr string) ([]Service, error)

Search searches services by SSDP.

func (*Service) Header

func (s *Service) Header() http.Header

Header returns all properties in response of search.

func (*Service) MaxAge

func (s *Service) MaxAge() int

MaxAge extracts "max-age" value from "CACHE-CONTROL" property.

Directories

Path Synopsis
examples
bye

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL