2013-09-28 16:57:51 +00:00
|
|
|
// goupnp is an implementation of a client for UPnP devices.
|
2013-09-26 22:11:06 +00:00
|
|
|
package goupnp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2013-09-28 16:57:51 +00:00
|
|
|
"net/url"
|
2013-09-26 22:11:06 +00:00
|
|
|
)
|
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
// Non-exhaustive set of UPnP service types.
|
2013-09-26 22:11:06 +00:00
|
|
|
const (
|
2013-09-28 16:57:51 +00:00
|
|
|
ServiceTypeLayer3Forwarding = "urn:schemas-upnp-org:service:Layer3Forwarding:1"
|
2013-09-29 10:23:10 +00:00
|
|
|
ServiceTypeWANCommonInterfaceConfig = "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"
|
2013-09-28 16:57:51 +00:00
|
|
|
// WANPPPConnection is typically useful with regard to the external IP and
|
|
|
|
// port forwarding.
|
2013-09-28 17:07:05 +00:00
|
|
|
// http://upnp.org/specs/gw/UPnP-gw-WANPPPConnection-v1-Service.pdf
|
2013-09-29 10:23:10 +00:00
|
|
|
ServiceTypeWANPPPConnection = "urn:schemas-upnp-org:service:WANPPPConnection:1"
|
2013-09-26 22:11:06 +00:00
|
|
|
)
|
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
// Non-exhaustive set of UPnP device types.
|
|
|
|
const (
|
|
|
|
// Device type for InternetGatewayDevice.
|
|
|
|
// http://upnp.org/specs/gw/upnp-gw-internetgatewaydevice-v1-device.pdf
|
|
|
|
DeviceTypeInternetGatewayDevice = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
|
|
|
|
)
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// ContextError is an error that wraps an error with some context information.
|
2013-09-28 16:57:51 +00:00
|
|
|
type ContextError struct {
|
|
|
|
Context string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ContextError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %v", err.Context, err.Err)
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// MaybeRootDevice contains either a RootDevice or an error.
|
2013-09-28 16:57:51 +00:00
|
|
|
type MaybeRootDevice struct {
|
|
|
|
Root *RootDevice
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoverDevices attempts to find targets of the given type. searchTarget is
|
2013-09-29 15:15:35 +00:00
|
|
|
// typically a value from a DeviceType* or ServiceType* constant. An error is
|
|
|
|
// returned for errors while attempting to send the query. An error or
|
|
|
|
// RootDevice is returned for each discovered service.
|
2013-09-28 16:57:51 +00:00
|
|
|
func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) {
|
2013-09-27 23:09:38 +00:00
|
|
|
httpu, err := NewHTTPUClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-28 16:57:51 +00:00
|
|
|
defer httpu.Close()
|
|
|
|
responses, err := SSDPRawSearch(httpu, string(searchTarget), 2, 3)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
results := make([]MaybeRootDevice, len(responses))
|
|
|
|
for i, response := range responses {
|
|
|
|
maybe := &results[i]
|
2013-09-27 23:09:38 +00:00
|
|
|
loc, err := response.Location()
|
|
|
|
if err != nil {
|
2013-09-28 16:57:51 +00:00
|
|
|
maybe.Err = ContextError{"unexpected bad location from search", err}
|
2013-09-27 23:09:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-09-28 16:57:51 +00:00
|
|
|
locStr := loc.String()
|
|
|
|
root := new(RootDevice)
|
|
|
|
if err := requestXml(locStr, DeviceXMLNamespace, root); err != nil {
|
|
|
|
maybe.Err = ContextError{fmt.Sprintf("error requesting root device details from %q", locStr), err}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
urlBase, err := url.Parse(root.URLBaseStr)
|
2013-09-27 23:09:38 +00:00
|
|
|
if err != nil {
|
2013-09-28 16:57:51 +00:00
|
|
|
maybe.Err = ContextError{fmt.Sprintf("error parsing URLBase %q from %q: %v", root.URLBaseStr, locStr), err}
|
2013-09-27 23:09:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-09-28 16:57:51 +00:00
|
|
|
root.SetURLBase(urlBase)
|
|
|
|
maybe.Root = root
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
func requestXml(url string, defaultSpace string, doc interface{}) error {
|
|
|
|
resp, err := http.Get(url)
|
2013-09-26 22:11:06 +00:00
|
|
|
if err != nil {
|
2013-09-28 16:57:51 +00:00
|
|
|
return err
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
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 {
|
2013-09-28 16:57:51 +00:00
|
|
|
return fmt.Errorf("goupnp: got response status %s from %q",
|
|
|
|
resp.Status, url)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
decoder := xml.NewDecoder(resp.Body)
|
2013-09-28 16:57:51 +00:00
|
|
|
decoder.DefaultSpace = defaultSpace
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
return decoder.Decode(doc)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|