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 5, 2022
1 parent 730ab65 commit 03eb93f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions goupnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/url"
"time"

"github.com/huin/goupnp/httpu"
"github.com/huin/goupnp/ssdp"
)

Expand Down Expand Up @@ -63,6 +64,9 @@ 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).
localAddr string

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

return results, nil
Expand Down
5 changes: 5 additions & 0 deletions httpu/httpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,14 @@ func (httpu *HTTPUClient) Do(
continue
}

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

responses = append(responses, response)
}

// Timeout reached - return discovered responses.
return responses, nil
}

const LocalAddress = "goupnp-local-address"
13 changes: 12 additions & 1 deletion service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ServiceClient struct {
RootDevice *RootDevice
Location *url.URL
Service *Service
localAddr string
}

// NewServiceClients discovers services, and returns clients for them. err will
Expand All @@ -36,7 +37,7 @@ func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []e
continue
}

deviceClients, err := NewServiceClientsFromRootDevice(maybeRootDevice.Root, maybeRootDevice.Location, searchTarget)
deviceClients, err := newServiceClientsFromRootDevice(maybeRootDevice.Root, maybeRootDevice.Location, searchTarget, maybeRootDevice.localAddr)
if err != nil {
errors = append(errors, err)
continue
Expand All @@ -61,6 +62,10 @@ func NewServiceClientsByURL(loc *url.URL, searchTarget string) ([]ServiceClient,
// a given root device. The loc parameter is simply assigned to the
// Location attribute of the returned ServiceClient(s).
func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searchTarget string) ([]ServiceClient, error) {
return newServiceClientsFromRootDevice(rootDevice, loc, searchTarget, "")
}

func newServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searchTarget string, lAddr string) ([]ServiceClient, error) {
device := &rootDevice.Device
srvs := device.FindService(searchTarget)
if len(srvs) == 0 {
Expand All @@ -75,6 +80,7 @@ func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searc
RootDevice: rootDevice,
Location: loc,
Service: srv,
localAddr: lAddr,
})
}
return clients, nil
Expand All @@ -86,3 +92,8 @@ func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searc
func (client *ServiceClient) GetServiceClient() *ServiceClient {
return client
}

// LocalAddr returns the address from which the device was discovered (if known - otherwise empty).
func (client *ServiceClient) LocalAddr() string {
return client.localAddr
}

1 comment on commit 03eb93f

@mh-cbon
Copy link
Owner Author

@mh-cbon mh-cbon commented on 03eb93f Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a mirror to the unpnp website ?

$ go generate ./...
2022/03/06 00:20:00 could not acquire spec for av1: could not download "av1.zip.download" from "http://upnp.org/specs/av/UPnP-av-TestFiles-20070927.zip": 500 Internal Server Error
dcps/av1/gen.go:1: running "goupnpdcpgen": exit status 1
2022/03/06 00:20:01 could not acquire spec for internetgateway1: could not download "internetgateway1.zip.download" from "http://upnp.org/specs/gw/UPnP-gw-IGD-TestFiles-20010921.zip": 500 Internal Server Error
dcps/internetgateway1/gen.go:1: running "goupnpdcpgen": exit status 1
2022/03/06 00:20:01 could not acquire spec for internetgateway2: could not download "internetgateway2.zip.download" from "http://upnp.org/specs/gw/UPnP-gw-IGD-Testfiles-20110224.zip": 500 Internal Server Error
dcps/internetgateway2/gen.go:1: running "goupnpdcpgen": exit status 1

Please sign in to comment.