Skip to content

Commit

Permalink
huin#48: add local address to the response
Browse files Browse the repository at this point in the history
  • Loading branch information
mh-cbon committed Mar 13, 2022
1 parent c67c0be commit fd5b2ea
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 23 deletions.
5 changes: 3 additions & 2 deletions cmd/goupnpdcpgen/codetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

var packageTmpl = template.Must(template.New("package").Parse(`{{$name := .Metadata.Name}}
// Client for UPnP Device Control Protocol {{.Metadata.OfficialName}}.
// {{if .DocURL}}
// This DCP is documented in detail at: {{.DocURL}}{{end}}
// {{if gt (len .DocURLs) 0 }}
// This DCP is documented in detail at: {{range .DocURLs}}
// - {{.}}{{end}}{{end}}
//
// Typically, use one of the New* functions to create clients for services.
package {{$name}}
Expand Down
4 changes: 2 additions & 2 deletions cmd/goupnpdcpgen/dcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// DCP collects together information about a UPnP Device Control Protocol.
type DCP struct {
Metadata DCPMetadata
DocURL string
DocURLs []string
DeviceTypes map[string]*URNParts
ServiceTypes map[string]*URNParts
Services []SCPDWithURN
Expand All @@ -30,7 +30,7 @@ func newDCP(metadata DCPMetadata) *DCP {
}

func (dcp *DCP) Reset() {
dcp.DocURL = ""
dcp.DocURLs = dcp.DocURLs[:0]
dcp.DeviceTypes = make(map[string]*URNParts)
dcp.ServiceTypes = make(map[string]*URNParts)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/goupnpdcpgen/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func acquireFile(specFilename string, xmlSpecURL string) error {
tmpFilename := specFilename + ".download"
defer os.Remove(tmpFilename)

if filexists(specFilename) {
if fileExists(specFilename) {
return nil
}

Expand Down Expand Up @@ -100,6 +100,7 @@ func copyFile(dst string, src string) error {
}
return writeFile(dst, f)
}

func writeFile(dst string, r io.ReadCloser) error {
defer r.Close()
f, err := os.Create(dst)
Expand All @@ -110,7 +111,7 @@ func writeFile(dst string, r io.ReadCloser) error {
return err
}

func filexists(p string) bool {
func fileExists(p string) bool {
f, err := os.Open(p)
if err != nil {
return !os.IsNotExist(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/goupnpdcpgen/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var dcpMetadata = []DCPMetadata{
Src: multiProvider{
openconnectivitydotorg{
SpecsURL: allSpecsURL,
DocPath: "*/*/UPnP-gw-*v{3,4}*.pdf",
DocPath: "*/*/UPnP-av-*v{3,4}*.pdf",
XMLSpecZipPath: "*/*/UPnP-av-TestFiles-*.zip",
XMLServicePath: []string{"*/*/service/*3.xml", "*/*/service/*4.xml"},
XMLDevicePath: []string{"*/*/device/*3.xml", "*/*/device/*4.xml"},
Expand Down
18 changes: 9 additions & 9 deletions cmd/goupnpdcpgen/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"archive/zip"
"fmt"
"path/filepath"
"strings"

"github.com/hashicorp/go-multierror"
)
Expand All @@ -20,7 +19,7 @@ type upnpdotorg struct {
}

func (u upnpdotorg) process(tmpdir, name string, dcp *DCP) error {
dcp.DocURL = u.DocURL
dcp.DocURLs = append(dcp.DocURLs, u.DocURL)
specFilename := filepath.Join(tmpdir, name+".zip")
err := acquireFile(specFilename, u.XMLSpecURL)
if err != nil {
Expand All @@ -45,11 +44,13 @@ func (u upnpdotorg) process(tmpdir, name string, dcp *DCP) error {
const allSpecsURL = "https://openconnectivity.org/upnp-specs/upnpresources.zip"

type openconnectivitydotorg struct {
DocPath string // Optional - Glob to the related documentation about the DCP.
SpecsURL string // The HTTP location of the zip archive containing all XML spec.
XMLSpecZipPath string // Glob to the zip XML spec file.
XMLServicePath []string // Glob to the device XMl files.
XMLDevicePath []string // Glob to the service XMl files.
DocPath string // Optional - Glob to the related documentation about the DCP.
SpecsURL string // The HTTP location of the zip archive containing all XML spec.
XMLSpecZipPath string // Glob to the zip XML spec file within upnpresources.zip.
// Glob to the services XML files within the ZIP matching XMLSpecZipPath.
XMLServicePath []string
// Glob to the devices XML files within the ZIP matching XMLSpecZipPath.
XMLDevicePath []string
// Any special-case functions to run against the DCP before writing it out.
Hacks []DCPHackFn
}
Expand Down Expand Up @@ -89,9 +90,8 @@ func (o openconnectivitydotorg) process(tmpdir, name string, dcp *DCP) error {
}

for _, d := range globFiles(o.DocPath, allSpecsArchive.File) {
dcp.DocURL += d.Name + ", "
dcp.DocURLs = append(dcp.DocURLs, d.Name)
}
dcp.DocURL = strings.TrimSuffix(dcp.DocURL, ", ")
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion dcps/av1/av1.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Client for UPnP Device Control Protocol MediaServer v1 and MediaRenderer v1.
//
// This DCP is documented in detail at: http://upnp.org/specs/av/av1/
// This DCP is documented in detail at:
// - http://upnp.org/specs/av/av1/
//
// Typically, use one of the New* functions to create clients for services.
package av1
Expand Down
8 changes: 7 additions & 1 deletion dcps/internetgateway1/internetgateway1.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Client for UPnP Device Control Protocol Internet Gateway Device v1.
//
// This DCP is documented in detail at: standardizeddcps/DeviceProtection_1/UPnP-gw-DeviceProtection-v1-Service-20110224.pdf, standardizeddcps/Internet Gateway_2/UPnP-gw-WANIPv6FirewallControl-v1-Service-20101210.pdf, standardizeddcps/WLAN Access Point Device_1/UPnP-gw-LinkAuthentication-v1-Service-20031017.pdf, standardizeddcps/WLAN Access Point Device_1/UPnP-gw-RadiusClient-v1-Service-20031017.pdf, standardizeddcps/WLAN Access Point Device_1/UPnP-gw-WLANAccessPointDevice-v1-Device-20031017.pdf, standardizeddcps/WLAN Access Point Device_1/UPnP-gw-WLANConfiguration-v1-Service-20031017.pdf
// This DCP is documented in detail at:
// - standardizeddcps/DeviceProtection_1/UPnP-gw-DeviceProtection-v1-Service-20110224.pdf
// - standardizeddcps/Internet Gateway_2/UPnP-gw-WANIPv6FirewallControl-v1-Service-20101210.pdf
// - standardizeddcps/WLAN Access Point Device_1/UPnP-gw-LinkAuthentication-v1-Service-20031017.pdf
// - standardizeddcps/WLAN Access Point Device_1/UPnP-gw-RadiusClient-v1-Service-20031017.pdf
// - standardizeddcps/WLAN Access Point Device_1/UPnP-gw-WLANAccessPointDevice-v1-Device-20031017.pdf
// - standardizeddcps/WLAN Access Point Device_1/UPnP-gw-WLANConfiguration-v1-Service-20031017.pdf
//
// Typically, use one of the New* functions to create clients for services.
package internetgateway1
Expand Down
6 changes: 5 additions & 1 deletion dcps/internetgateway2/internetgateway2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Client for UPnP Device Control Protocol Internet Gateway Device v2.
//
// This DCP is documented in detail at: standardizeddcps/Internet Gateway_2/UPnP-gw-InternetGatewayDevice-v2-Device-20101210.pdf, standardizeddcps/Internet Gateway_2/UPnP-gw-WANConnectionDevice-v2-Device-20100910.pdf, standardizeddcps/Internet Gateway_2/UPnP-gw-WANDevice-v2-Device-20100910.pdf, standardizeddcps/Internet Gateway_2/UPnP-gw-WANIPConnection-v2-Service-20100910.pdf
// This DCP is documented in detail at:
// - standardizeddcps/Internet Gateway_2/UPnP-gw-InternetGatewayDevice-v2-Device-20101210.pdf
// - standardizeddcps/Internet Gateway_2/UPnP-gw-WANConnectionDevice-v2-Device-20100910.pdf
// - standardizeddcps/Internet Gateway_2/UPnP-gw-WANDevice-v2-Device-20100910.pdf
// - standardizeddcps/Internet Gateway_2/UPnP-gw-WANIPConnection-v2-Service-20100910.pdf
//
// Typically, use one of the New* functions to create clients for services.
package internetgateway2
Expand Down
4 changes: 2 additions & 2 deletions goupnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type MaybeRootDevice struct {
// the discovery of a device, regardless of if there was an error probing it.
Location *url.URL

// The address from which the device was discovered (if known - otherwise nil).
// The address from which the device was discovered (if known - otherwise empty string).
localAddr string

// Any error encountered probing a discovered device.
Expand Down Expand Up @@ -103,7 +103,7 @@ func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) {
} else {
maybe.Root = root
}
maybe.localAddr = response.Header.Get(httpu.LocalAddress)
maybe.localAddr = response.Header.Get(httpu.LocalAddressHeader)
}

return results, nil
Expand Down
7 changes: 5 additions & 2 deletions httpu/httpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func (httpu *HTTPUClient) Do(
}

// Set the related local address used to discover the device.
response.Header.Add(LocalAddress, httpu.conn.LocalAddr().(*net.UDPAddr).IP.String())
response.Header.Add(LocalAddressHeader,
httpu.conn.LocalAddr().(*net.UDPAddr).IP.String())

responses = append(responses, response)
}
Expand All @@ -153,4 +154,6 @@ func (httpu *HTTPUClient) Do(
return responses, nil
}

const LocalAddress = "goupnp-local-address"
// LocalAddressHeader is the name of the HTTP response header
// containing the net address that read the response.
const LocalAddressHeader = "goupnp-local-address"

0 comments on commit fd5b2ea

Please sign in to comment.