2013-09-26 22:11:06 +00:00
|
|
|
package goupnp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Search Target for InternetGatewayDevice.
|
2013-09-27 23:09:38 +00:00
|
|
|
SearchTargetIGD = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
|
2013-09-26 22:11:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DiscoverIGD attempts to find Internet Gateway Devices.
|
|
|
|
//
|
|
|
|
// TODO: Fix implementation to discover multiple. Currently it will find a
|
|
|
|
// maximum of one.
|
2013-09-27 23:09:38 +00:00
|
|
|
func DiscoverIGD() ([]*IGD, error) {
|
|
|
|
httpu, err := NewHTTPUClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
responses, err := SSDPRawSearch(httpu, SearchTargetIGD, 2, 3)
|
|
|
|
|
|
|
|
results := make([]*IGD, 0, len(responses))
|
|
|
|
for _, response := range responses {
|
|
|
|
loc, err := response.Location()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("goupnp: unexpected bad location from search: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
igd, err := requestIgd(loc.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("goupnp: error requesting IGD: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
results = append(results, igd)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IGD defines the interface for an Internet Gateway Device.
|
|
|
|
type IGD struct {
|
|
|
|
xml xmlRootDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
func requestIgd(serviceUrl string) (*IGD, error) {
|
|
|
|
resp, err := http.Get(serviceUrl)
|
2013-09-26 22:11:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
defer resp.Body.Close()
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("goupnp: got response status %s from IGD at %q",
|
|
|
|
resp.Status, serviceUrl)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
decoder := xml.NewDecoder(resp.Body)
|
|
|
|
decoder.DefaultSpace = deviceXmlNs
|
|
|
|
var xml xmlRootDevice
|
|
|
|
if err = decoder.Decode(&xml); err != nil {
|
|
|
|
return nil, err
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
log.Printf("%+v", xml)
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
return &IGD{xml}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (igd *IGD) Device() *Device {
|
|
|
|
return &Device{
|
|
|
|
igd.xml.URLBase,
|
|
|
|
igd.xml.Device,
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
}
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
func (igd *IGD) String() string {
|
|
|
|
return fmt.Sprintf("IGD{UDN: %q friendlyName: %q}",
|
|
|
|
igd.xml.Device.UDN, igd.xml.Device.FriendlyName)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
type Device struct {
|
|
|
|
urlBase string
|
|
|
|
xml xmlDevice
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
func (device *Device) String() string {
|
|
|
|
return fmt.Sprintf("Device{friendlyName: %q}", device.xml.FriendlyName)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
func (device *Device) Devices() []*Device {
|
|
|
|
devices := make([]*Device, len(device.xml.Devices))
|
|
|
|
for i, childXml := range device.xml.Devices {
|
|
|
|
devices[i] = &Device{
|
|
|
|
device.urlBase,
|
|
|
|
childXml,
|
|
|
|
}
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
return devices
|
|
|
|
}
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
func (device *Device) Services() []*Service {
|
|
|
|
srvs := make([]*Service, len(device.xml.Services))
|
|
|
|
for i, childXml := range device.xml.Services {
|
|
|
|
srvs[i] = &Service{
|
|
|
|
device.urlBase,
|
|
|
|
childXml,
|
|
|
|
}
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
return srvs
|
|
|
|
}
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
type Service struct {
|
|
|
|
urlBase string
|
|
|
|
xml xmlService
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
func (srv *Service) String() string {
|
|
|
|
return fmt.Sprintf("Service{serviceType: %q}", srv.xml.ServiceType)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|